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:
1 parent
00759450ff
commit
914eb0a232
3 files changed
+143
-940
No files matched your search
@@ -105,6 +105,7 @@ target_link_libraries(EchoionCore
|
|||||||
Void::Text
|
Void::Text
|
||||||
Void::Time
|
Void::Time
|
||||||
Void::Math
|
Void::Math
|
||||||
|
Void::Net
|
||||||
PRIVATE
|
PRIVATE
|
||||||
Threads::Threads
|
Threads::Threads
|
||||||
qjs
|
qjs
|
||||||
|
|||||||
@@ -1,28 +1,24 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "boost/asio/ssl/context.hpp"
|
|
||||||
#include <NetworkInterface.hpp>
|
#include <NetworkInterface.hpp>
|
||||||
#include <NetworkTypes.hpp>
|
#include <NetworkTypes.hpp>
|
||||||
#include <Types.hpp>
|
#include <Types.hpp>
|
||||||
#include <Void/Data/Json.hpp>
|
#include <Void/Data/Json.hpp>
|
||||||
#include <Void/Data/Reflect.hpp>
|
#include <Void/Data/Reflect.hpp>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <format>
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <queue>
|
#include <optional>
|
||||||
#include <thread>
|
#include <string_view>
|
||||||
|
#include <vector>
|
||||||
// Boost.Beast includes needed for template function
|
|
||||||
#include <boost/beast.hpp>
|
|
||||||
#include <boost/beast/http.hpp>
|
|
||||||
|
|
||||||
namespace Echoion::Core
|
namespace Echoion::Core
|
||||||
{
|
{
|
||||||
|
|
||||||
// Forward declaration for URL parsing
|
// HTTP client implementing Api::INetworkClient. A thin adapter over
|
||||||
struct UrlComponents;
|
// Void::Net::HttpClient (HTTP/HTTPS, TLS, SOCKS5 + HTTP-CONNECT proxies);
|
||||||
|
// retry/backoff is layered here via Void::Retry.
|
||||||
class NetworkClient : public Api::INetworkClient
|
class NetworkClient : public Api::INetworkClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -30,7 +26,6 @@ namespace Echoion::Core
|
|||||||
NetworkClient();
|
NetworkClient();
|
||||||
~NetworkClient() override;
|
~NetworkClient() override;
|
||||||
|
|
||||||
// Initialize with optional proxy
|
|
||||||
Api::Result<void> initialize(
|
Api::Result<void> initialize(
|
||||||
const std::optional<Api::ProxyConfig>& proxy = std::nullopt);
|
const std::optional<Api::ProxyConfig>& proxy = std::nullopt);
|
||||||
void shutdown();
|
void shutdown();
|
||||||
@@ -86,9 +81,17 @@ namespace Echoion::Core
|
|||||||
return parseJson<T>(response->bodyAsString());
|
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:
|
private:
|
||||||
|
|
||||||
// Parse a response body into a reflectable type T via Void::Json.
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
[[nodiscard]] static Api::Result<T>
|
[[nodiscard]] static Api::Result<T>
|
||||||
parseJson(const std::string& body)
|
parseJson(const std::string& body)
|
||||||
@@ -111,128 +114,17 @@ namespace Echoion::Core
|
|||||||
return std::move(*value);
|
return std::move(*value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
// Execute with retry/backoff (Void::Retry); single attempt via
|
||||||
|
// Void::Net::HttpClient.
|
||||||
// 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
|
|
||||||
[[nodiscard]] Api::Result<Api::HttpResponse>
|
[[nodiscard]] Api::Result<Api::HttpResponse>
|
||||||
executeWithRetry(const Api::HttpRequest& req);
|
executeWithRetry(const Api::HttpRequest& req);
|
||||||
[[nodiscard]] Api::Result<Api::HttpResponse>
|
[[nodiscard]] Api::Result<Api::HttpResponse>
|
||||||
executeRequest(const Api::HttpRequest& req);
|
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_;
|
std::optional<Api::ProxyConfig> proxy_;
|
||||||
Api::RetryConfig retryConfig_;
|
Api::RetryConfig retryConfig_;
|
||||||
mutable std::mutex mutex_;
|
mutable std::mutex mutex_;
|
||||||
std::atomic<bool> initialized_{false};
|
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
|
} // namespace Echoion::Core
|
||||||
+124
-814
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user