From e4853bd29c81bb08b28ee58b17812b814fbd7ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Wed, 30 Oct 2019 14:17:42 -0400 Subject: [PATCH] proxy: use common Json builder --- include/opendht/dht_proxy_client.h | 4 +-- include/opendht/dht_proxy_server.h | 1 + src/dht_proxy_client.cpp | 40 +++++------------------------- src/dht_proxy_server.cpp | 15 ++++------- 4 files changed, 13 insertions(+), 47 deletions(-) diff --git a/include/opendht/dht_proxy_client.h b/include/opendht/dht_proxy_client.h index f506495f..363d2873 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 900c3801..3edc7bc9 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 dceee268..2406dac3 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 ee724c80..e2b5bc08 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()); -- GitLab