diff --git a/src/NodeRpcProxy/NodeRpcProxy.cpp b/src/NodeRpcProxy/NodeRpcProxy.cpp index 37f60fd04d..a0b4a3bc67 100644 --- a/src/NodeRpcProxy/NodeRpcProxy.cpp +++ b/src/NodeRpcProxy/NodeRpcProxy.cpp @@ -95,12 +95,14 @@ NodeRpcProxy::NodeRpcProxy(const std::string& nodeHost, unsigned short nodePort, std::stringstream userAgent; userAgent << "NodeRpcProxy"; m_requestHeaders = { {"User-Agent", userAgent.str()}, { "Connection", "keep-alive" } }; + resetInternalState(); } NodeRpcProxy::~NodeRpcProxy() { try { shutdown(); + } catch (std::exception&) { } } @@ -181,7 +183,11 @@ void NodeRpcProxy::workerThread(const INode::Callback& initialized_callback) { m_dispatcher = &dispatcher; ContextGroup contextGroup(dispatcher); m_context_group = &contextGroup; - + httplib::Client httpClient(m_node_url); + m_httpClient = &httpClient; + m_httpClient->enable_server_certificate_verification(false); + m_httpClient->set_connection_timeout(1000); + m_httpClient->set_keep_alive(true); Event httpEvent(dispatcher); m_httpEvent = &httpEvent; m_httpEvent->set(); @@ -213,6 +219,7 @@ void NodeRpcProxy::workerThread(const INode::Callback& initialized_callback) { m_dispatcher = nullptr; m_context_group = nullptr; + m_httpClient = nullptr; m_httpEvent = nullptr; m_connected = false; m_rpcProxyObserverManager.notify(&INodeRpcProxyObserver::connectionStatusUpdated, m_connected); @@ -983,11 +990,7 @@ std::error_code NodeRpcProxy::binaryCommand(const std::string& comm, const Reque try { EventLock eventLock(*m_httpEvent); - httplib::Client httpClient(m_node_url); - httpClient.enable_server_certificate_verification(false); - httpClient.set_connection_timeout(1000); - - const auto rsp = httpClient.Post(rpc_url.c_str(), m_requestHeaders, storeToBinaryKeyValue(req), "application/octet-stream"); + const auto rsp = m_httpClient->Post(rpc_url.c_str(), m_requestHeaders, storeToBinaryKeyValue(req), "application/octet-stream"); if (rsp) { if (rsp->status == 200) { if (!loadFromBinaryKeyValue(res, rsp->body)) { @@ -1013,11 +1016,7 @@ std::error_code NodeRpcProxy::jsonCommand(const std::string& comm, const Request try { EventLock eventLock(*m_httpEvent); - httplib::Client httpClient(m_node_url); - httpClient.enable_server_certificate_verification(false); - httpClient.set_connection_timeout(1000); - - const auto rsp = httpClient.Post(rpc_url.c_str(), m_requestHeaders, storeToJson(req), "application/json"); + const auto rsp = m_httpClient->Post(rpc_url.c_str(), m_requestHeaders, storeToJson(req), "application/json"); if (rsp) { if (rsp->status == 200) { if (!loadFromJson(res, rsp->body)) { @@ -1047,11 +1046,7 @@ std::error_code NodeRpcProxy::jsonRpcCommand(const std::string& method, const Re jsReq.setParams(req); JsonRpc::JsonRpcResponse jsRes; - httplib::Client httpClient(m_node_url); - httpClient.enable_server_certificate_verification(false); - httpClient.set_connection_timeout(1000); - - const auto rsp = httpClient.Post(rpc_url.c_str(), m_requestHeaders, jsReq.getBody(), "application/json"); + const auto rsp = m_httpClient->Post(rpc_url.c_str(), m_requestHeaders, jsReq.getBody(), "application/json"); if (rsp) { if (rsp->status == 200) { jsRes.parse(rsp->body); diff --git a/src/NodeRpcProxy/NodeRpcProxy.h b/src/NodeRpcProxy/NodeRpcProxy.h index fa392f794d..eb655de5e8 100644 --- a/src/NodeRpcProxy/NodeRpcProxy.h +++ b/src/NodeRpcProxy/NodeRpcProxy.h @@ -174,6 +174,8 @@ class NodeRpcProxy : public CryptoNote::INode { unsigned int m_rpcTimeout; + httplib::Client* m_httpClient = nullptr; + httplib::Headers m_requestHeaders; System::Event* m_httpEvent = nullptr;