diff --git a/src/client.cpp b/src/client.cpp index a11b46970607b2ba779416339fa1b812cca4ca37..af9ae397a7d3e6c743da539f2f3fb42ff28c622c 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -43,16 +43,6 @@ Client::on_resolve(std::shared_ptr<Session> s, if (ec) return s->on_failure(s->id, ec); - // Add new session to the list of active sessions so that we can access - // it if the user decides to cancel the corresponding request before - // it completes. - // Because active sessions list can be accessed from multiple threads, - // - // we guard it with a mutex to avoid data corruption. - std::unique_lock<std::mutex> lock(active_sessions_guard_); - active_sessions_[s->id] = s; - lock.unlock(); - boost::asio::async_connect( s->socket.next_layer(), results.begin(), @@ -68,10 +58,6 @@ Client::on_connect(std::shared_ptr<Session> s, boost::system::error_code ec) return s->on_failure(s->id, ec); } - // Check if the request is already cancelled and we shouldn't move forward. - if (s->is_cancelled) - return on_request_complete(s); - // Perform the SSL handshake s->socket.async_handshake( boost::asio::ssl::stream_base::client, @@ -86,10 +72,6 @@ Client::on_handshake(std::shared_ptr<Session> s, boost::system::error_code ec) return s->on_failure(s->id, ec); } - // Check if the request is already cancelled and we shouldn't move forward. - if (s->is_cancelled) - return on_request_complete(s); - boost::beast::http::async_write(s->socket, s->request, std::bind(&Client::on_write, @@ -111,9 +93,6 @@ Client::on_write(std::shared_ptr<Session> s, return s->on_failure(s->id, ec); } - if (s->is_cancelled) - return on_request_complete(s); - // Receive the HTTP response http::async_read( s->socket, @@ -148,16 +127,6 @@ Client::do_request(std::shared_ptr<Session> s) std::placeholders::_2)); } -void -Client::cancel_request(RequestID request_id) -{ - std::unique_lock<std::mutex> lock(active_sessions_guard_); - - auto it = active_sessions_.find(request_id); - if (it != active_sessions_.end()) - it->second->is_cancelled = true; -} - void Client::remove_session(std::shared_ptr<Session> s) { @@ -182,15 +151,6 @@ Client::remove_session(std::shared_ptr<Session> s) // TODO: propagate the error. std::cout << "shutdown: " << ec.message() << std::endl; }); - - // Remove the session from the map of active sessions. - std::unique_lock<std::mutex> lock(active_sessions_guard_); - - auto it = active_sessions_.find(s->id); - if (it != active_sessions_.end()) - active_sessions_.erase(it); - - lock.unlock(); } void @@ -198,16 +158,7 @@ Client::on_request_complete(std::shared_ptr<Session> s) { remove_session(s); - boost::system::error_code ec; - - if (s->error_code == 0 && s->is_cancelled) { - ec = boost::asio::error::operation_aborted; - s->on_failure(s->id, ec); - return; - } else { - ec = s->error_code; - } - + boost::system::error_code ec(s->error_code); s->on_success(s->id, s->parser.get(), ec); } diff --git a/src/client.hpp b/src/client.hpp index c6227b748ef7484304a05561a7feb3690c681ccc..f9112cdf0dbbef11781c4b5627386dfe825a6ea8 100644 --- a/src/client.hpp +++ b/src/client.hpp @@ -12,10 +12,11 @@ #include <boost/thread/thread.hpp> #include <json.hpp> +#include <mtx/requests.hpp> +#include <mtx/responses.hpp> + #include "crypto.hpp" #include "errors.hpp" -#include "mtx/requests.hpp" -#include "mtx/responses.hpp" #include "session.hpp" #include "utils.hpp" @@ -45,12 +46,8 @@ public: //! Wait for the client to close. void close(); - //! Cancels the request. - void cancel_request(RequestID request_id); //! Make a new request. void do_request(std::shared_ptr<Session> session); - //! Return the number of pending requests. - int active_sessions() const { return active_sessions_.size(); } //! Add an access token. void set_access_token(const std::string &token) { access_token_ = token; } //! Retrieve the access token. @@ -267,10 +264,6 @@ private: boost::asio::io_service ios_; - //! Keeps tracks for the active sessions. - std::map<RequestID, std::shared_ptr<Session>> active_sessions_; - //! Used to synchronize access to `active_sessions_`. - std::mutex active_sessions_guard_; //! Used to prevent the event loop from shutting down. std::unique_ptr<boost::asio::io_service::work> work_; //! Worker threads for the requests. diff --git a/src/session.hpp b/src/session.hpp index 277b07a79bdc30b3aaec302b6d53da5a16f9707b..1637c412eb009b35cb2950167ca447c9786fcbe6 100644 --- a/src/session.hpp +++ b/src/session.hpp @@ -36,7 +36,6 @@ struct Session , id{id} , on_success{on_success} , on_failure{on_failure} - , is_cancelled{false} { parser.header_limit(8192); parser.body_limit(1 * 1024 * 1024 * 1024); // 1 GiB @@ -61,8 +60,6 @@ struct Session SuccessCallback on_success; //! Function to be called when the request fails. FailureCallback on_failure; - //! Whether or not the request has been cancelled. - std::atomic_bool is_cancelled; }; } }