From 60f3ef1f7c01c39af7f051e2181a56294f63e5ca Mon Sep 17 00:00:00 2001 From: azw Date: Sat, 6 May 2023 12:18:48 +0000 Subject: [PATCH] 1 --- a8/asiotcpclient.cc | 56 +++++++++++++++++++++++++++++++++++++-------- a8/asiotcpclient.h | 4 ++++ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/a8/asiotcpclient.cc b/a8/asiotcpclient.cc index 31f7b43..2ebd599 100644 --- a/a8/asiotcpclient.cc +++ b/a8/asiotcpclient.cc @@ -2,17 +2,8 @@ #include #include -#include #include -#include -#include -#include -#include -#include - -#include - #include #include @@ -26,6 +17,10 @@ namespace a8 AsioTcpClient::AsioTcpClient() { 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); } AsioTcpClient::~AsioTcpClient() @@ -74,6 +69,7 @@ namespace a8 bot_node_ = p; } send_buffer_mutex_->unlock(); + DoSend(); } } @@ -152,6 +148,48 @@ namespace a8 }); } + void AsioTcpClient::DoSend() + { + if (!work_node_) { + send_buffer_mutex_->lock(); + work_node_ = top_node_; + top_node_ = nullptr; + bot_node_ = nullptr; + send_buffer_mutex_->unlock(); + } + if (work_node_) { + char* buf = work_node_->buff + work_node_->sent_bytes; + int buf_len = work_node_->bufflen - work_node_->sent_bytes; + asio::async_write + (*socket_, + asio::buffer(buf, buf_len), + [this] (const asio::error_code& ec, std::size_t bytes_transferred) + { + if (!ec) { + work_node_->sent_bytes += bytes_transferred; + if (work_node_->sent_bytes >= work_node_->bufflen) { + auto pdelnode = work_node_; + work_node_ = work_node_->next; + free(pdelnode->buff); + free((void*)pdelnode); + } + DoSend(); + } else { + abort(); + } + }); + + } + } + + void AsioTcpClient::WorkerThreadProc() + { + while (true) { + io_context_->run(); + int i = 0; + } + } + } #endif diff --git a/a8/asiotcpclient.h b/a8/asiotcpclient.h index 121f066..6cbcb23 100644 --- a/a8/asiotcpclient.h +++ b/a8/asiotcpclient.h @@ -31,8 +31,11 @@ namespace a8 private: void HandleConnect(const asio::error_code& err, const tcp::endpoint& endpoint); void DoRead(); + void DoSend(); + void WorkerThreadProc(); private: + std::shared_ptr io_context_; std::shared_ptr resolver_; std::shared_ptr socket_; volatile bool actived_ = false; @@ -40,6 +43,7 @@ namespace a8 std::shared_ptr send_buffer_mutex_; SendQueueNode *top_node_ = nullptr; SendQueueNode *bot_node_ = nullptr; + volatile SendQueueNode *work_node_ = nullptr; std::array buffer_; void SetActive(bool active);