From bdfeb6a02a8dd5d73adb12d0efef7dab199c2a57 Mon Sep 17 00:00:00 2001 From: taynpg Date: Sun, 15 Dec 2024 23:19:56 +0800 Subject: [PATCH] =?UTF-8?q?bugfix=EF=BC=9A=E9=87=8D=E8=A6=81BUG=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=EF=BC=8C=E9=97=AE=E9=A2=98=E7=82=B9=E6=98=AF=EF=BC=8C?= =?UTF-8?q?=E7=BC=93=E5=86=B2=E5=8C=BA=E5=8F=AA=E8=A7=A3=E6=9E=90=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E7=BB=84=E5=80=BC=E5=B0=B1=E7=BB=93=E6=9D=9F=E4=BA=86?= =?UTF-8?q?=EF=BC=8C=E5=A6=82=E6=9E=9C=E6=9C=89=E5=A4=9A=E7=BB=84=E5=B0=86?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E6=9C=AA=E8=A7=A3=E6=9E=90=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5=EF=BC=88=E9=9D=9E=E8=A7=A3=E6=9E=90=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=EF=BC=89=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- net/net_base.cpp | 16 +++++++++++----- server/server.cpp | 1 + util/util.cpp | 25 +++++++++++++++---------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/net/net_base.cpp b/net/net_base.cpp index 87a4adb..0764ace 100644 --- a/net/net_base.cpp +++ b/net/net_base.cpp @@ -63,12 +63,18 @@ void CTcpClient::async_recv() } } else { buffer_.push(tmp_buf_.data(), length); - auto* frame = CTransProtocal::parse(buffer_); - if (frame) { - if (fun_) { - fun_(frame); + + bool done = true; + while (done) { + auto* frame = CTransProtocal::parse(buffer_); + if (frame) { + if (fun_) { + fun_(frame); + } + delete frame; + } else { + done = false; } - delete frame; } async_recv(); } diff --git a/server/server.cpp b/server/server.cpp index d7cd153..4ace7ec 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -165,6 +165,7 @@ void CTcpServer::handle_frame() break; } default: + logger_->warn("No Mathched type."); break; } delete buf; diff --git a/util/util.cpp b/util/util.cpp index 87be4bb..40d939f 100644 --- a/util/util.cpp +++ b/util/util.cpp @@ -1,5 +1,4 @@ #include "util.h" -#include #include std::shared_ptr get_logger(const std::string& mark, const std::string& log_file) @@ -45,27 +44,33 @@ CFrameBuffer* CTransProtocal::parse(CMutBuffer& buffer) if (find < 0) { return result; } - int16_t type = *(reinterpret_cast(buffer.get_data() + find + 2)); - char mark = *(buffer.get_data() + find + 2 + 2); - int32_t len = *(reinterpret_cast(buffer.get_data() + find + 2 + 2 + 1 + 32 + 32)); + // reinterpret_cast 的指针转换直接访问内存可能导致未对齐问题(某些架构下) + int16_t type{}; + char mark{}; + int32_t len{}; + std::memcpy(&type, buffer.get_data() + find + 2, sizeof(type)); + std::memcpy(&mark, buffer.get_data() + find + 2 + 2, sizeof(mark)); + std::memcpy(&len, buffer.get_data() + find + 2 + 2 + 1 + 32 + 32, sizeof(len)); + int32_t tail_index = find + 2 + 2 + 1 + 32 + 32 + 4 + len; if (buffer.get_len() - 2 < tail_index || len < 0) { return result; } - unsigned char taila = *((unsigned char*)(buffer.get_data() + tail_index)); - unsigned char tailb = *((unsigned char*)(buffer.get_data() + tail_index + 1)); - if (taila != tail[0] || tailb != tail[1]) { + if (std::memcmp(buffer.get_data() + tail_index, tail, sizeof(tail)) != 0) { return result; } result = new CFrameBuffer(); - result->data_ = new char[len]; - std::memset(result->data_, 0x0, len); + if (len > 0) { + result->data_ = new char[len](); + } result->len_ = len; result->fid_ = std::string(buffer.get_data() + find + 2 + 2 + 1); result->tid_ = std::string(buffer.get_data() + find + 2 + 2 + 1 + 32); result->mark_ = mark; result->type_ = static_cast(type); - std::memcpy(result->data_, buffer.get_data() + find + 2 + 2 + 1 + 4 + 32 + 32, len); + if (len > 0) { + std::memcpy(result->data_, buffer.get_data() + find + 2 + 2 + 1 + 4 + 32 + 32, len); + } buffer.remove_of(0, tail_index + 2); return result; }