From 919f80f452098c0e142c9dc49b0b076ce498ec29 Mon Sep 17 00:00:00 2001 From: freeeyes Date: Sun, 8 Oct 2023 17:06:07 +0800 Subject: [PATCH] fix sonar code smell. --- PSS_ASIO/TcpSession/TcpSession.cpp | 45 +++++++++++++++++------------- PSS_ASIO/TcpSession/TcpSession.h | 2 ++ PSS_ASIO/UdpSession/KcpServer.h | 2 +- PSS_ASIO/UdpSession/UdpServer.cpp | 1 - PSS_ASIO/UdpSession/UdpServer.h | 2 +- 5 files changed, 29 insertions(+), 23 deletions(-) diff --git a/PSS_ASIO/TcpSession/TcpSession.cpp b/PSS_ASIO/TcpSession/TcpSession.cpp index 3db64df..bdc2e07 100644 --- a/PSS_ASIO/TcpSession/TcpSession.cpp +++ b/PSS_ASIO/TcpSession/TcpSession.cpp @@ -166,30 +166,35 @@ void CTcpSession::do_write(uint32 connect_id) asio::async_write(self->socket_, asio::buffer(send_buffer->data_.c_str(), send_buffer->buffer_length_), [self, send_buffer, connect_id](std::error_code ec, std::size_t length) { - if (ec) - { - //发送写入消息失败信息 - PSS_LOGGER_DEBUG("[CTcpSession::do_write]({0})write error({1}).", connect_id, ec.message()); - self->send_write_fail_to_logic(send_buffer->data_, length); - if (true == self->is_active_close_) - { - self->close_immediaterly(); - } - } - else - { - self->add_send_finish_size(connect_id, length); - if (true == self->is_active_close_ - && self->send_buffer_size_ == self->send_data_size_) - { - //关闭客户端 - self->close_immediaterly(); - } - } + self->do_write_finish(ec, connect_id, send_buffer, length); }); }); } +void CTcpSession::do_write_finish(std::error_code& ec, uint32 connect_id, std::shared_ptr send_buffer, std::size_t length) +{ + if (ec) + { + //发送写入消息失败信息 + PSS_LOGGER_DEBUG("[CTcpSession::do_write]({0})write error({1}).", connect_id, ec.message()); + send_write_fail_to_logic(send_buffer->data_, length); + if (true == is_active_close_) + { + close_immediaterly(); + } + } + else + { + add_send_finish_size(connect_id, length); + if (true == is_active_close_ + && send_buffer_size_ == send_data_size_) + { + //关闭客户端 + close_immediaterly(); + } + } +} + void CTcpSession::do_write_immediately(uint32 connect_id, const char* data, size_t length) { set_write_buffer(connect_id, data, length); diff --git a/PSS_ASIO/TcpSession/TcpSession.h b/PSS_ASIO/TcpSession/TcpSession.h index d280c0a..c2e365c 100644 --- a/PSS_ASIO/TcpSession/TcpSession.h +++ b/PSS_ASIO/TcpSession/TcpSession.h @@ -62,6 +62,8 @@ class CTcpSession : public std::enable_shared_from_this, public ISe void send_write_fail_to_logic(const std::string& write_fail_buffer, std::size_t buffer_length); + void do_write_finish(std::error_code& ec, uint32 connect_id, std::shared_ptr send_buffer, std::size_t length); + private: tcp::socket socket_; asio::io_context* io_context_ = nullptr; diff --git a/PSS_ASIO/UdpSession/KcpServer.h b/PSS_ASIO/UdpSession/KcpServer.h index 291ebce..a247002 100644 --- a/PSS_ASIO/UdpSession/KcpServer.h +++ b/PSS_ASIO/UdpSession/KcpServer.h @@ -86,7 +86,7 @@ class CKcpServer : public std::enable_shared_from_this, public ISess public: CKcpServer(asio::io_context* io_context, const std::string& server_ip, io_port_type port, uint32 packet_parse_id, uint32 max_recv_size, uint32 max_send_size, CIo_List_Manager* io_list_manager); - virtual ~CKcpServer() = default; + ~CKcpServer() override = default; void start(); diff --git a/PSS_ASIO/UdpSession/UdpServer.cpp b/PSS_ASIO/UdpSession/UdpServer.cpp index 80152f2..87357c2 100644 --- a/PSS_ASIO/UdpSession/UdpServer.cpp +++ b/PSS_ASIO/UdpSession/UdpServer.cpp @@ -86,7 +86,6 @@ void CUdpServer::do_receive() EM_CONNECT_IO_TYPE::CONNECT_IO_UDP); self->io_list_manager_->del_accept_net_io_event(self->server_ip_, self->server_port_, EM_CONNECT_IO_TYPE::CONNECT_IO_UDP); - return; } }); } diff --git a/PSS_ASIO/UdpSession/UdpServer.h b/PSS_ASIO/UdpSession/UdpServer.h index 581bd12..c72794f 100644 --- a/PSS_ASIO/UdpSession/UdpServer.h +++ b/PSS_ASIO/UdpSession/UdpServer.h @@ -42,7 +42,7 @@ class CUdpServer : public std::enable_shared_from_this, public ISess public: CUdpServer(asio::io_context* io_context, const std::string& server_ip, io_port_type port, uint32 packet_parse_id, uint32 max_recv_size, uint32 max_send_size, EM_NET_TYPE em_net_type, CIo_List_Manager* io_list_manager); - virtual ~CUdpServer() = default; + ~CUdpServer() override = default; void start();