From af7a9093a0d6a9c7427e08c5ee042d692c94c70b Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 8 Dec 2023 10:44:16 +0800 Subject: [PATCH] 1 --- a8/asiotcpclient.cc | 37 +++++++++++++++++++++++++++---------- a8/asiotcpclient.h | 3 ++- a8/websocketclient.cc | 4 ++-- a8/websocketclient.h | 2 +- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/a8/asiotcpclient.cc b/a8/asiotcpclient.cc index 8610fbd..f847038 100644 --- a/a8/asiotcpclient.cc +++ b/a8/asiotcpclient.cc @@ -60,6 +60,7 @@ namespace a8 void AsioTcpClient::SendBuff(const char* buff, unsigned int bufflen) { + //a8::XPrintf("SendBuff bufflen:%d\n", {bufflen}); if (bufflen > 0) { a8::SendQueueNode* p = (a8::SendQueueNode*)malloc(sizeof(a8::SendQueueNode)); memset(p, 0, sizeof(SendQueueNode)); @@ -140,10 +141,11 @@ namespace a8 } DoRead(); } else { + a8::XPrintf("DoRead error %s\n", {ec.message()}); actived_ = false; connected_ = false; if (on_disconnect) { - on_disconnect(this); + on_disconnect(this, ec.value()); } } }); @@ -158,7 +160,8 @@ namespace a8 bot_node_ = nullptr; send_buffer_mutex_->unlock(); } - if (work_node_) { + if (work_node_ && !sending_) { + sending_ = true; char* buf = work_node_->buff + work_node_->sent_bytes; int buf_len = work_node_->bufflen - work_node_->sent_bytes; asio::async_write @@ -167,16 +170,30 @@ namespace a8 [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); + send_buffer_mutex_->lock(); + if (work_node_) { + 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); + } + if (!work_node_) { + sending_ = false; + } + send_buffer_mutex_->unlock(); + DoSend(); + return; } - DoSend(); + send_buffer_mutex_->unlock(); } else { - abort(); + a8::XPrintf("DoSend error %s\n", {ec.message()}); + actived_ = false; + connected_ = false; + if (on_disconnect) { + on_disconnect(this, ec.value()); + } } }); diff --git a/a8/asiotcpclient.h b/a8/asiotcpclient.h index c86ab10..69f5213 100644 --- a/a8/asiotcpclient.h +++ b/a8/asiotcpclient.h @@ -14,7 +14,7 @@ namespace a8 public: std::function on_error; std::function on_connect; - std::function on_disconnect; + std::function on_disconnect; std::function on_socketread; AsioTcpClient(std::shared_ptr io_context, const std::string& remote_ip, @@ -47,6 +47,7 @@ namespace a8 SendQueueNode *top_node_ = nullptr; SendQueueNode *bot_node_ = nullptr; volatile SendQueueNode *work_node_ = nullptr; + volatile bool sending_ = false; std::array buffer_; void SetActive(bool active); diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index 4819003..21d11ec 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -66,10 +66,10 @@ namespace a8 socket->SendBuff(data.data(), data.size()); }; tcp_client_->on_disconnect = - [this] (a8::AsioTcpClient* socket) + [this] (a8::AsioTcpClient* socket, int err) { if (on_disconnect) { - on_disconnect(this); + on_disconnect(this, err); } }; tcp_client_->on_socketread = diff --git a/a8/websocketclient.h b/a8/websocketclient.h index 6f7d007..ac44a8a 100644 --- a/a8/websocketclient.h +++ b/a8/websocketclient.h @@ -19,7 +19,7 @@ namespace a8 std::function on_error; std::function on_connect; - std::function on_disconnect; + std::function on_disconnect; std::function on_decode_userpacket; void Open();