diff --git a/include/opendht/dht_proxy_client.h b/include/opendht/dht_proxy_client.h index f506495f9a692ddaa5dc865dec225f722efcbc6f..363d287306fd0a26efaeaa4df763a517968282e7 100644 --- a/include/opendht/dht_proxy_client.h +++ b/include/opendht/dht_proxy_client.h @@ -285,7 +285,6 @@ private: */ struct InfoState; void getProxyInfos(); - void handleProxyStatus(const asio::error_code &ec, std::shared_ptr<InfoState> infoState); void queryProxyInfo(std::shared_ptr<InfoState> infoState, sa_family_t family, std::shared_ptr<http::Resolver> resolver); void onProxyInfos(const Json::Value& val, const sa_family_t family); SockAddr parsePublicAddress(const Json::Value& val); @@ -373,8 +372,6 @@ private: std::mutex lockCallbacks_; Sp<InfoState> infoState_; - Sp<asio::steady_timer> statusTimer_; - mutable std::mutex statusLock_; /** * Retrieve if we can connect to the proxy (update statusIpvX_) @@ -410,6 +407,7 @@ private: std::atomic_bool isDestroying_ {false}; Json::StreamWriterBuilder jsonBuilder_; + Json::CharReaderBuilder jsonReaderBuilder_; std::shared_ptr<dht::Logger> logger_; std::shared_ptr<http::Request> buildRequest(const std::string& target = {}); diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h index 900c3801742ce480d0fa1404ff14ae6e0c0f9682..3edc7bc9d155a087236c2ed6b07868fdaaa93e5f 100644 --- a/include/opendht/dht_proxy_server.h +++ b/include/opendht/dht_proxy_server.h @@ -317,6 +317,7 @@ private: std::shared_ptr<DhtRunner> dht_; Json::StreamWriterBuilder jsonBuilder_; + Json::CharReaderBuilder jsonReaderBuilder_; // http server std::thread serverThread_; diff --git a/src/dht_proxy_client.cpp b/src/dht_proxy_client.cpp index dceee268406682d669e5630b666b73c9c3e9fd7b..2406dac39cd57da0793b7dd21466bf7c181e82ad 100644 --- a/src/dht_proxy_client.cpp +++ b/src/dht_proxy_client.cpp @@ -157,8 +157,6 @@ DhtProxyClient::stop() cancelAllListeners(); if (infoState_) infoState_->cancel = true; - if (statusTimer_) - statusTimer_->cancel(); if (httpClientThread_.joinable()) httpClientThread_.join(); } @@ -284,7 +282,6 @@ DhtProxyClient::get(const InfoHash& key, GetCallback cb, DoneCallback donecb, Va request->add_on_body_callback([this, key, opstate, filter, cb](const char* at, size_t length){ try { - Json::CharReaderBuilder rbuilder; auto body = std::string(at, length); // one value per body line std::string data_line; @@ -293,7 +290,7 @@ DhtProxyClient::get(const InfoHash& key, GetCallback cb, DoneCallback donecb, Va std::string err; Json::Value json; auto* char_data = static_cast<const char*>(&data_line[0]); - auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader()); + auto reader = std::unique_ptr<Json::CharReader>(jsonReaderBuilder_.newCharReader()); if (!reader->parse(char_data, char_data + data_line.size(), &json, &err)){ opstate->ok.store(false); return; @@ -543,38 +540,17 @@ DhtProxyClient::getProxyInfos() { if (logger_) logger_->d("[proxy:client] [info] requesting proxy server node information"); - std::lock_guard<std::mutex> l(statusLock_); - auto infoState = std::make_shared<InfoState>(); - if (infoState_) - infoState_->cancel = true; - infoState_ = infoState; { std::lock_guard<std::mutex> l(lockCurrentProxyInfos_); + if (infoState_) + infoState_->cancel = true; + infoState_ = infoState; if (statusIpv4_ == NodeStatus::Disconnected) statusIpv4_ = NodeStatus::Connecting; if (statusIpv6_ == NodeStatus::Disconnected) statusIpv6_ = NodeStatus::Connecting; } - // Try to contact the proxy and set the status to connected when done. - // will change the connectivity status - if (!statusTimer_) - statusTimer_ = std::make_shared<asio::steady_timer>(httpContext_); - statusTimer_->expires_at(std::chrono::steady_clock::now()); - statusTimer_->async_wait(std::bind(&DhtProxyClient::handleProxyStatus, this, - std::placeholders::_1, infoState)); -} - -void -DhtProxyClient::handleProxyStatus(const asio::error_code& ec, std::shared_ptr<InfoState> infoState) -{ - if (ec == asio::error::operation_aborted) - return; - else if (ec){ - if (logger_) - logger_->e("[proxy:client] [status] handling error: %s", ec.message().c_str()); - return; - } if (logger_) logger_->d("[proxy:client] [status] sending request"); @@ -599,7 +575,6 @@ DhtProxyClient::queryProxyInfo(std::shared_ptr<InfoState> infoState, sa_family_t if (state == http::Request::State::DONE) { if (infoState->cancel.load()) return; - //ok->store(false); if (response.status_code != 200) { if (logger_) logger_->e("[proxy:client] [status] ipv%i failed with code=%i", @@ -610,10 +585,8 @@ DhtProxyClient::queryProxyInfo(std::shared_ptr<InfoState> infoState, sa_family_t } else { std::string err; Json::Value proxyInfos; - Json::CharReaderBuilder rbuilder; - auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader()); + auto reader = std::unique_ptr<Json::CharReader>(jsonReaderBuilder_.newCharReader()); if (!reader->parse(response.body.data(), response.body.data() + response.body.size(), &proxyInfos, &err)){ - //ok->store(false); onProxyInfos(Json::Value{}, family); return; } @@ -946,7 +919,6 @@ DhtProxyClient::sendListen(const restinio::http_request_header_t header, request->add_on_body_callback([this, reqid, opstate, cb](const char* at, size_t length){ try { - Json::CharReaderBuilder rbuilder; auto body = std::string(at, length); // one value per body line std::string data_line; @@ -955,7 +927,7 @@ DhtProxyClient::sendListen(const restinio::http_request_header_t header, std::string err; Json::Value json; auto* char_data = static_cast<const char*>(&data_line[0]); - auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader()); + auto reader = std::unique_ptr<Json::CharReader>(jsonReaderBuilder_.newCharReader()); if (!reader->parse(char_data, char_data + data_line.size(), &json, &err)){ opstate->ok.store(false); return; diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp index ee724c802d41ee5287e13bc85210cc6028c9d9a5..e2b5bc08088df80641daee7fd4c37ed41c006bcd 100644 --- a/src/dht_proxy_server.cpp +++ b/src/dht_proxy_server.cpp @@ -633,9 +633,8 @@ DhtProxyServer::subscribe(restinio::request_handle_t request, try { std::string err; Json::Value root; - Json::CharReaderBuilder rbuilder; auto* char_data = reinterpret_cast<const char*>(request->body().data()); - auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader()); + auto reader = std::unique_ptr<Json::CharReader>(jsonReaderBuilder_.newCharReader()); if (!reader->parse(char_data, char_data + request->body().size(), &root, &err)){ auto response = this->initHttpResponse( request->create_response(restinio::status_bad_request())); @@ -775,9 +774,8 @@ DhtProxyServer::unsubscribe(restinio::request_handle_t request, try { std::string err; Json::Value root; - Json::CharReaderBuilder rbuilder; auto* char_data = reinterpret_cast<const char*>(request->body().data()); - auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader()); + auto reader = std::unique_ptr<Json::CharReader>(jsonReaderBuilder_.newCharReader()); if (!reader->parse(char_data, char_data + request->body().size(), &root, &err)){ auto response = this->initHttpResponse( @@ -968,9 +966,8 @@ DhtProxyServer::put(restinio::request_handle_t request, try { std::string err; Json::Value root; - Json::CharReaderBuilder rbuilder; auto* char_data = reinterpret_cast<const char*>(request->body().data()); - auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader()); + auto reader = std::unique_ptr<Json::CharReader>(jsonReaderBuilder_.newCharReader()); if (reader->parse(char_data, char_data + request->body().size(), &root, &err)){ auto value = std::make_shared<dht::Value>(root); @@ -1082,9 +1079,8 @@ RequestStatus DhtProxyServer::putSigned(restinio::request_handle_t request, try { std::string err; Json::Value root; - Json::CharReaderBuilder rbuilder; auto* char_data = reinterpret_cast<const char*>(request->body().data()); - auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader()); + auto reader = std::unique_ptr<Json::CharReader>(jsonReaderBuilder_.newCharReader()); if (reader->parse(char_data, char_data + request->body().size(), &root, &err)){ @@ -1145,9 +1141,8 @@ DhtProxyServer::putEncrypted(restinio::request_handle_t request, try { std::string err; Json::Value root; - Json::CharReaderBuilder rbuilder; auto* char_data = reinterpret_cast<const char*>(request->body().data()); - auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader()); + auto reader = std::unique_ptr<Json::CharReader>(jsonReaderBuilder_.newCharReader()); if (reader->parse(char_data, char_data + request->body().size(), &root, &err)){ InfoHash to(root["to"].asString());