From 466448796dff77a16190519c25879cfe0bc534ec Mon Sep 17 00:00:00 2001 From: azw Date: Sun, 7 May 2023 01:18:20 +0000 Subject: [PATCH] 1 --- a8/asiotcpclient.cc | 19 ++----------------- a8/asiotcpclient.h | 4 +--- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/a8/asiotcpclient.cc b/a8/asiotcpclient.cc index 5233a21..ed5e253 100644 --- a/a8/asiotcpclient.cc +++ b/a8/asiotcpclient.cc @@ -15,7 +15,7 @@ const int MAX_RECV_BUFFERSIZE = 1024 * 64; namespace a8 { - AsioTcpClient::AsioTcpClient(const std::string& remote_ip, int remote_port) + AsioTcpClient::AsioTcpClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port) { remote_address_ = remote_ip; remote_port_ = remote_port; @@ -25,9 +25,7 @@ namespace a8 remote_port_ ); send_buffer_mutex_ = std::make_shared(); - io_context_ = std::make_shared(); - socket_ = std::make_shared(*io_context_); - new std::thread(&AsioTcpClient::WorkerThreadProc, this); + socket_ = std::make_shared(io_context); } AsioTcpClient::~AsioTcpClient() @@ -184,19 +182,6 @@ namespace a8 } } - void AsioTcpClient::WorkerThreadProc() - { - while (true) { - try { - io_context_->run(); - io_context_->reset(); - int i = 0; - } catch (std::exception& e) { - std::cerr << "Exception: " << e.what() << "\n"; - } - } - } - } #endif diff --git a/a8/asiotcpclient.h b/a8/asiotcpclient.h index a676003..a5b6b11 100644 --- a/a8/asiotcpclient.h +++ b/a8/asiotcpclient.h @@ -16,7 +16,7 @@ namespace a8 std::function on_connect; std::function on_disconnect; std::function on_socketread; - AsioTcpClient(const std::string& remote_ip, int remote_port); + AsioTcpClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port); virtual ~AsioTcpClient(); const std::string& GetRemoteAddress() { return remote_address_; } int GetRemotePort() { return remote_port_; } @@ -31,14 +31,12 @@ namespace a8 void HandleConnect(const asio::error_code& err); void DoRead(); void DoSend(); - void WorkerThreadProc(); private: std::string remote_address_; int remote_port_ = 0; std::shared_ptr endpoint_; - std::shared_ptr io_context_; std::shared_ptr socket_; volatile bool actived_ = false; volatile bool connected_ = false;