This commit is contained in:
azw 2023-05-06 14:43:52 +00:00
parent ed93a7c237
commit 77fb8a70b2
2 changed files with 9 additions and 13 deletions

View File

@ -26,7 +26,6 @@ namespace a8
); );
send_buffer_mutex_ = std::make_shared<std::mutex>(); send_buffer_mutex_ = std::make_shared<std::mutex>();
io_context_ = std::make_shared<asio::io_context>(); io_context_ = std::make_shared<asio::io_context>();
resolver_ = std::make_shared<asio::ip::tcp::resolver>(*io_context_);
socket_ = std::make_shared<asio::ip::tcp::socket>(*io_context_); socket_ = std::make_shared<asio::ip::tcp::socket>(*io_context_);
new std::thread(&AsioTcpClient::WorkerThreadProc, this); new std::thread(&AsioTcpClient::WorkerThreadProc, this);
} }
@ -98,15 +97,12 @@ namespace a8
{ {
actived_ = true; actived_ = true;
connected_ = false; connected_ = false;
socket_->async_connect
#if 0 (*endpoint_,
asio::async_connect(*socket_, endpoints, [this] (const asio::error_code& ec)
[this] (const asio::error_code& ec, {
const tcp::endpoint& endpoint) HandleConnect(ec);
{ });
HandleConnect(ec, endpoint);
});
#endif
} }
void AsioTcpClient::ActiveStop() void AsioTcpClient::ActiveStop()
@ -115,7 +111,7 @@ namespace a8
connected_ = false; connected_ = false;
} }
void AsioTcpClient::HandleConnect(const asio::error_code& err, const tcp::endpoint& endpoint) void AsioTcpClient::HandleConnect(const asio::error_code& err)
{ {
if (err) { if (err) {
actived_ = false; actived_ = false;
@ -193,6 +189,7 @@ namespace a8
while (true) { while (true) {
try { try {
io_context_->run(); io_context_->run();
io_context_->reset();
int i = 0; int i = 0;
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n"; std::cerr << "Exception: " << e.what() << "\n";

View File

@ -28,7 +28,7 @@ namespace a8
void SendBuff(const char* buff, unsigned int bufflen); void SendBuff(const char* buff, unsigned int bufflen);
private: private:
void HandleConnect(const asio::error_code& err, const tcp::endpoint& endpoint); void HandleConnect(const asio::error_code& err);
void DoRead(); void DoRead();
void DoSend(); void DoSend();
void WorkerThreadProc(); void WorkerThreadProc();
@ -39,7 +39,6 @@ namespace a8
std::shared_ptr<asio::ip::tcp::endpoint> endpoint_; std::shared_ptr<asio::ip::tcp::endpoint> endpoint_;
std::shared_ptr<asio::io_context> io_context_; std::shared_ptr<asio::io_context> io_context_;
std::shared_ptr<asio::ip::tcp::resolver> resolver_;
std::shared_ptr<asio::ip::tcp::socket> socket_; std::shared_ptr<asio::ip::tcp::socket> socket_;
volatile bool actived_ = false; volatile bool actived_ = false;
volatile bool connected_ = false; volatile bool connected_ = false;