Skip to content

Commit

Permalink
refactor: code readability and other improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dnzbk committed Jul 29, 2024
1 parent aeb33ee commit b15457e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
9 changes: 4 additions & 5 deletions daemon/connect/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ namespace HttpClient
return m_localIP;
}

std::future<Response> HttpClient::GET(const std::string& host)
std::future<Response> HttpClient::GET(std::string host_)
{
return std::async(std::launch::async, [&]
return std::async(std::launch::async, [this, host = std::move(host_)]
{
auto endpoints = m_resolver.resolve(host, GetProtocol());
auto socket = GetSocket();
Expand Down Expand Up @@ -99,9 +99,8 @@ namespace HttpClient
std::string HttpClient::ReadBody(Socket& socket, boost::asio::streambuf& buf)
{
boost::system::error_code ec;
asio::read(socket, buf, ec);

if (ec != asio::error::eof)
asio::read_until(socket, buf, "\0", ec);
if (ec && ec != asio::error::eof)
{
throw std::runtime_error("Failed to read the response body: " + ec.message());
}
Expand Down
2 changes: 1 addition & 1 deletion daemon/connect/HttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace HttpClient
HttpClient operator=(const HttpClient&) = delete;
~HttpClient() = default;

std::future<Response> GET(const std::string& host);
std::future<Response> GET(std::string host);
const std::string GetLocalIP() const;

private:
Expand Down
6 changes: 3 additions & 3 deletions daemon/system/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ namespace System

try
{
auto httpClient = std::make_unique<HttpClient::HttpClient>();
auto result = httpClient->GET(IP_SERVICE).get();
HttpClient::HttpClient httpClient;
auto result = httpClient.GET(IP_SERVICE).get();
if (result.statusCode == 200)
{
network.publicIP = std::move(result.body);
network.privateIP = httpClient->GetLocalIP();
network.privateIP = httpClient.GetLocalIP();
}
}
catch (const std::exception& e)
Expand Down
24 changes: 12 additions & 12 deletions daemon/util/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Util
static bool AlphaNum(const char* str);

template<typename T, typename U>
static constexpr bool Equal(T t, U u) noexcept
static constexpr bool CmpEqual(T t, U u) noexcept
{
if constexpr (std::is_signed_v<T> == std::is_signed_v<U>)
{
Expand All @@ -81,13 +81,13 @@ class Util
}

template<typename T, typename U>
static constexpr bool NotEqual(T t, U u) noexcept
static constexpr bool CmpNotEqual(T t, U u) noexcept
{
return !Equal(t, u);
return !CmpEqual(t, u);
}

template<typename T, typename U>
static constexpr bool Less(T t, U u) noexcept
static constexpr bool CmpLess(T t, U u) noexcept
{
if constexpr (std::is_signed_v<T> == std::is_signed_v<U>)
{
Expand All @@ -104,21 +104,21 @@ class Util
}

template<typename T, typename U>
static constexpr bool Greater(T t, U u) noexcept
static constexpr bool CmpGreater(T t, U u) noexcept
{
return Less(u, t);
return CmpLess(u, t);
}

template<typename T, typename U>
static constexpr bool LessEqual(T t, U u) noexcept
static constexpr bool CmpLessEqual(T t, U u) noexcept
{
return !Less(u, t);
return !CmpLess(u, t);
}

template<typename T, typename U>
static constexpr bool GreaterEqual(T t, U u) noexcept
static constexpr bool CmpGreaterEqual(T t, U u) noexcept
{
return !Less(t, u);
return !CmpLess(t, u);
}

template <typename From, typename To,
Expand All @@ -127,7 +127,7 @@ class Util
{
if constexpr (std::is_unsigned_v<From> && std::is_signed_v<To>)
{
if (Greater(num, std::numeric_limits<To>::max()))
if (CmpGreater(num, std::numeric_limits<To>::max()))
{
return 0;
}
Expand All @@ -143,7 +143,7 @@ class Util

return static_cast<To>(num);
}
else if (Greater(num, std::numeric_limits<To>::max()) || Less(num, std::numeric_limits<To>::min()))
else if (CmpGreater(num, std::numeric_limits<To>::max()) || CmpLess(num, std::numeric_limits<To>::min()))
{
return 0;
}
Expand Down

0 comments on commit b15457e

Please sign in to comment.