diff --git a/include/opendht/http.h b/include/opendht/http.h index ca5f86aaf62aaacc1e771f0c69dcfe175f3bab90..199dc6e51f6c4219b65c251a623f2524b62039bc 100644 --- a/include/opendht/http.h +++ b/include/opendht/http.h @@ -99,6 +99,7 @@ public: void async_read(const size_t bytes, BytesHandlerCb cb); void timeout(const std::chrono::seconds timeout, HandlerCb cb = {}); + void close(); private: unsigned int id_; diff --git a/src/http.cpp b/src/http.cpp index a27ae1838d16d57b30cd730cf71db0a13d10d039..276e919a1f5c89bbf1d7d843cc621998665b6525 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -117,19 +117,21 @@ Connection::Connection(asio::io_context& ctx, std::shared_ptr<dht::crypto::Certi Connection::~Connection() { + close(); +} + +void +Connection::close() +{ + if (!is_open()) + return; asio::error_code ec; - if (is_open()){ - if (ssl_ctx_){ - ssl_socket_->cancel(ec); - ssl_socket_->close(ec); - } - else { - socket_->cancel(ec); - socket_->close(ec); - } - if (ec and logger_) - logger_->e("[http:client] [connection:%i] error closing: %s", id_, ec.message().c_str()); - } + if (ssl_ctx_) + ssl_socket_->close(ec); + else + socket_->close(ec); + if (ec and logger_) + logger_->e("[http:client] [connection:%i] error closing: %s", id_, ec.message().c_str()); } unsigned int @@ -442,7 +444,7 @@ Request::~Request() void Request::cancel() { - terminate(asio::error::eof); + conn_->close(); } unsigned int @@ -899,8 +901,10 @@ Request::handle_response_header(const asio::error_code& ec) void Request::handle_response_body(const asio::error_code& ec, const size_t bytes) { - if (ec == asio::error::operation_aborted) + if (ec == asio::error::operation_aborted){ + terminate(ec); return; + } else if ((ec == asio::error::eof) or (ec == asio::error::connection_reset)){ terminate(ec); return;