diff --git a/a8/asiotcpclient.cc b/a8/asiotcpclient.cc index f9e2d0e..5233a21 100644 --- a/a8/asiotcpclient.cc +++ b/a8/asiotcpclient.cc @@ -26,7 +26,6 @@ namespace a8 ); send_buffer_mutex_ = std::make_shared(); io_context_ = std::make_shared(); - resolver_ = std::make_shared(*io_context_); socket_ = std::make_shared(*io_context_); new std::thread(&AsioTcpClient::WorkerThreadProc, this); } @@ -98,15 +97,12 @@ namespace a8 { actived_ = true; connected_ = false; - - #if 0 - asio::async_connect(*socket_, endpoints, - [this] (const asio::error_code& ec, - const tcp::endpoint& endpoint) - { - HandleConnect(ec, endpoint); - }); - #endif + socket_->async_connect + (*endpoint_, + [this] (const asio::error_code& ec) + { + HandleConnect(ec); + }); } void AsioTcpClient::ActiveStop() @@ -115,7 +111,7 @@ namespace a8 connected_ = false; } - void AsioTcpClient::HandleConnect(const asio::error_code& err, const tcp::endpoint& endpoint) + void AsioTcpClient::HandleConnect(const asio::error_code& err) { if (err) { actived_ = false; @@ -193,6 +189,7 @@ namespace a8 while (true) { try { io_context_->run(); + io_context_->reset(); int i = 0; } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; diff --git a/a8/asiotcpclient.h b/a8/asiotcpclient.h index 8197a90..a676003 100644 --- a/a8/asiotcpclient.h +++ b/a8/asiotcpclient.h @@ -28,7 +28,7 @@ namespace a8 void SendBuff(const char* buff, unsigned int bufflen); private: - void HandleConnect(const asio::error_code& err, const tcp::endpoint& endpoint); + void HandleConnect(const asio::error_code& err); void DoRead(); void DoSend(); void WorkerThreadProc(); @@ -39,7 +39,6 @@ namespace a8 std::shared_ptr endpoint_; std::shared_ptr io_context_; - std::shared_ptr resolver_; std::shared_ptr socket_; volatile bool actived_ = false; volatile bool connected_ = false;