Rewrite NetworkClient as a Void::Net::HttpClient adapter

Replace the ~700-line hand-rolled HTTP stack (Boost.Beast/Asio client,
URL parsing, HTTP-CONNECT + SOCKS5 proxy handshakes, a bespoke 4-thread
packaged_task pool) with a thin adapter over Void::Net::HttpClient that
still implements Api::INetworkClient. Translates Api::HttpRequest/Response/
ProxyConfig <-> Void::Net types; retry/backoff stays on Void::Retry; async
methods use std::async. Link Void::Net.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 4.8 committed 2026-06-22 16:35:11 +03:00
1 parent 00759450ff
commit 914eb0a232
3 files changed
+143 -940

No files matched your search

+1
View File
@@ -105,6 +105,7 @@ target_link_libraries(EchoionCore
Void::Text
Void::Time
Void::Math
Void::Net
PRIVATE
Threads::Threads
qjs
+18 -126
View File
@@ -1,28 +1,24 @@
#pragma once
#include "boost/asio/ssl/context.hpp"
#include <NetworkInterface.hpp>
#include <NetworkTypes.hpp>
#include <Types.hpp>
#include <Void/Data/Json.hpp>
#include <Void/Data/Reflect.hpp>
#include <atomic>
#include <chrono>
#include <format>
#include <future>
#include <mutex>
#include <queue>
#include <thread>
// Boost.Beast includes needed for template function
#include <boost/beast.hpp>
#include <boost/beast/http.hpp>
#include <optional>
#include <string_view>
#include <vector>
namespace Echoion::Core
{
// Forward declaration for URL parsing
struct UrlComponents;
// HTTP client implementing Api::INetworkClient. A thin adapter over
// Void::Net::HttpClient (HTTP/HTTPS, TLS, SOCKS5 + HTTP-CONNECT proxies);
// retry/backoff is layered here via Void::Retry.
class NetworkClient : public Api::INetworkClient
{
public:
@@ -30,7 +26,6 @@ namespace Echoion::Core
NetworkClient();
~NetworkClient() override;
// Initialize with optional proxy
Api::Result<void> initialize(
const std::optional<Api::ProxyConfig>& proxy = std::nullopt);
void shutdown();
@@ -86,9 +81,17 @@ namespace Echoion::Core
return parseJson<T>(response->bodyAsString());
}
// Proxy configuration
void setProxy(const std::optional<Api::ProxyConfig>& proxy);
[[nodiscard]] std::optional<Api::ProxyConfig> getProxy() const;
void clearProxy();
// Retry configuration
void setRetryConfig(const Api::RetryConfig& config);
[[nodiscard]] Api::RetryConfig getRetryConfig() const;
private:
// Parse a response body into a reflectable type T via Void::Json.
template <typename T>
[[nodiscard]] static Api::Result<T>
parseJson(const std::string& body)
@@ -111,128 +114,17 @@ namespace Echoion::Core
return std::move(*value);
}
public:
// Proxy configuration
void setProxy(const std::optional<Api::ProxyConfig>& proxy);
[[nodiscard]] std::optional<Api::ProxyConfig> getProxy() const;
void clearProxy();
// Retry configuration
void setRetryConfig(const Api::RetryConfig& config);
[[nodiscard]] Api::RetryConfig getRetryConfig() const;
private:
// Request execution
// Execute with retry/backoff (Void::Retry); single attempt via
// Void::Net::HttpClient.
[[nodiscard]] Api::Result<Api::HttpResponse>
executeWithRetry(const Api::HttpRequest& req);
[[nodiscard]] Api::Result<Api::HttpResponse>
executeRequest(const Api::HttpRequest& req);
// Proxy and direct execution methods
[[nodiscard]] Api::HttpResponse
executeWithProxy(boost::asio::io_context& ioc,
boost::asio::ssl::context& ctx,
const UrlComponents& urlComp,
const Api::HttpRequest& req,
const Api::ProxyConfig& proxy,
std::chrono::seconds timeout);
[[nodiscard]] Api::HttpResponse
executeWithHttpProxy(boost::asio::io_context& ioc,
boost::asio::ssl::context& ctx,
const UrlComponents& urlComp,
const Api::HttpRequest& req,
const Api::ProxyConfig& proxy,
std::chrono::seconds timeout);
[[nodiscard]] Api::HttpResponse
executeWithSocks5Proxy(boost::asio::io_context& ioc,
boost::asio::ssl::context& ctx,
const UrlComponents& urlComp,
const Api::HttpRequest& req,
const Api::ProxyConfig& proxy,
std::chrono::seconds timeout);
[[nodiscard]] Api::HttpResponse
executeDirect(boost::asio::io_context& ioc,
boost::asio::ssl::context& ctx,
const UrlComponents& urlComp,
const Api::HttpRequest& req,
std::chrono::seconds timeout);
// HTTP request methods
// Note: sendHttpsRequest is a template, implemented in header below
template <typename Stream>
[[nodiscard]] Api::HttpResponse
sendHttpsRequest(Stream& stream,
const UrlComponents& urlComp,
const Api::HttpRequest& req);
[[nodiscard]] Api::HttpResponse
sendHttpRequest(boost::asio::ip::tcp::socket& socket,
const UrlComponents& urlComp,
const Api::HttpRequest& req);
[[nodiscard]] Api::HttpResponse sendHttpRequestThroughProxy(
boost::asio::ip::tcp::socket& socket,
const UrlComponents& urlComp,
const Api::HttpRequest& req,
const Api::ProxyConfig& proxy);
// Request/Response building
[[nodiscard]] boost::beast::http::request<
boost::beast::http::string_body>
buildBeastRequest(const UrlComponents& urlComp,
const Api::HttpRequest& req);
[[nodiscard]] Api::HttpResponse convertBeastResponse(
const boost::beast::http::response<
boost::beast::http::dynamic_body>& beastResponse);
[[nodiscard]] std::vector<Api::HttpHeader>
addDefaultHeaders(const std::vector<Api::HttpHeader>& headers);
std::optional<Api::ProxyConfig> proxy_;
Api::RetryConfig retryConfig_;
mutable std::mutex mutex_;
std::atomic<bool> initialized_{false};
std::atomic<uint32_t> requestId_{0};
// Thread pool for async operations
std::vector<std::jthread> threadPool_;
std::queue<std::packaged_task<Api::Result<Api::HttpResponse>()>>
taskQueue_;
std::mutex taskMutex_;
std::condition_variable taskCv_;
std::atomic<bool> stopThreads_{false};
static constexpr size_t THREAD_POOL_SIZE = 4;
};
// Template implementation for sendHttpsRequest
template <typename Stream>
Api::HttpResponse
NetworkClient::sendHttpsRequest(Stream& stream,
const UrlComponents& urlComp,
const Api::HttpRequest& req)
{
namespace http = boost::beast::http;
// Build request
http::request<http::string_body> request =
buildBeastRequest(urlComp, req);
// Send
http::write(stream, request);
// Read response
boost::beast::flat_buffer buffer;
http::response<http::dynamic_body> response;
http::read(stream, buffer, response);
// Convert to Api::HttpResponse
return convertBeastResponse(response);
}
} // namespace Echoion::Core
+124 -814
View File
File diff suppressed because it is too large. Load diff