From d7d147e7cf88a20bc37118b60fc7da1fdb959a99 Mon Sep 17 00:00:00 2001 From: Lveyou <1339484752@qq.com> Date: Wed, 11 Mar 2026 11:35:34 +0800 Subject: [PATCH] =?UTF-8?q?*=20net=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= =?UTF-8?q?=E8=A1=A5=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dl/io/dl_net.cpp | 64 +++++++++++++++++++++++------------------ project/dl_chess | 2 +- project/dl_chess_server | 2 +- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/dl/io/dl_net.cpp b/dl/io/dl_net.cpp index 623e3a62..b68fc882 100644 --- a/dl/io/dl_net.cpp +++ b/dl/io/dl_net.cpp @@ -17,6 +17,8 @@ class ClientImp public: asio::io_context _ioContext; asio::ip::tcp::socket _socket; + std::string _remoteIp; + uint16_t _remotePort; ClientImp() : _socket{ _ioContext } @@ -24,12 +26,7 @@ public: void _handle_error(asio::error_code ec) { - std::string str; - - str = fmt::format("连接中断->[{}:{}]:", - _socket.remote_endpoint().address().to_string(), - _socket.remote_endpoint().port()); - + std::string str = fmt::format("连接中断->[{}:{}]:", _remoteIp, _remotePort); if (ec == asio::error::eof) { @@ -63,12 +60,29 @@ public: void Start() { //_socketId = _socket.native_handle(); + + try + { + _remoteIp = _socket.remote_endpoint().address().to_string(); + _remotePort = _socket.remote_endpoint().port(); + } + catch (...) + { + log_info("创建连接失败!"); + return; + } + + log_info("创建连接->[{}:{}]", _remoteIp, _remotePort); + _read_head(); _write_head(); } void Send(uint32_t msg_id, BufferView buf); + ServerImp* _parent; asio::ip::tcp::socket _socket; + std::string _remoteIp; + uint16_t _remotePort; MsgHead _head; Buffer _buf; TcpMessageList _msgListSend; @@ -136,7 +150,7 @@ public: { if (ec) { - _handle_error(ec, sc->_socket); + _handle_error(sc, ec); delete sc; } else @@ -147,20 +161,15 @@ public: _allConnect[socket_id] = sc; sc->Start(); - log_info("创建连接->[{}:{}]", - sc->_socket.remote_endpoint().address().to_string(), - sc->_socket.remote_endpoint().port()); } _accept(); } - void _handle_error(asio::error_code ec, const asio::ip::tcp::socket& socket) + void _handle_error(ServerConnect* sc, std::error_code ec) { - std::string str = fmt::format("连接中断->[{}:{}]:", - socket.remote_endpoint().address().to_string(), - socket.remote_endpoint().port()); + std::string str = fmt::format("连接中断->[{}:{}]:", sc->_remoteIp, sc->_remotePort); if (ec == asio::error::eof) { @@ -181,9 +190,7 @@ public: void _remove_connect(ServerConnect* sc) { - log_info("移除连接->[{}:{}]", - sc->_socket.remote_endpoint().address().to_string(), - sc->_socket.remote_endpoint().port()); + log_info("移除连接->[{}:{}]", sc->_remoteIp, sc->_remotePort); sc->_bDelete = true; @@ -340,6 +347,9 @@ bool Client::Connent(std::string_view ip, uint16_t port) _tRecv = g_thread->Create(); _tRecv->Start(_thread_recv, this); + _imp->_remoteIp = _imp->_socket.remote_endpoint().address().to_string(); + _imp->_remotePort = _imp->_socket.remote_endpoint().port(); + return true; } catch (std::exception& e) @@ -439,8 +449,8 @@ void Client::_thread_send(void* p) "消息id:{}\n" "数据大小:{}\n" "{}", - c->_imp->_socket.remote_endpoint().address().to_string(), - c->_imp->_socket.remote_endpoint().port(), + c->_imp->_remoteIp, + c->_imp->_remotePort, msg._head._id, msg._head._size, to_string_auto(msg._buf)); #endif @@ -478,8 +488,8 @@ void Client::_thread_recv(void* p) "消息id:{}\n" "数据大小:{}\n" "{}", - c->_imp->_socket.remote_endpoint().address().to_string(), - c->_imp->_socket.remote_endpoint().port(), + c->_imp->_remoteIp, + c->_imp->_remotePort, msg._head._id, msg._head._size, to_string_auto(msg._buf)); @@ -761,7 +771,7 @@ void ServerConnect::_read_head() } if (ec) { - _parent->_handle_error(ec, _socket); + _parent->_handle_error(this, ec); _parent->_remove_connect(this); return; } @@ -773,9 +783,7 @@ void ServerConnect::_read_head() } if (_head._magic != NETWORK_MESSAGE_TAG) { - log_warn("无效包<-[{}:{}]", - _socket.remote_endpoint().address().to_string(), - _socket.remote_endpoint().port()); + log_warn("无效包<-[{}:{}]", _remoteIp, _remotePort); return; } _read_tail(_head._size); @@ -798,7 +806,7 @@ void ServerConnect::_read_tail(uint32_t size) } if (ec) { - _parent->_handle_error(ec, _socket); + _parent->_handle_error(this, ec); _parent->_remove_connect(this); return; } @@ -838,7 +846,7 @@ void ServerConnect::_write_head() } if (ec) { - _parent->_handle_error(ec, _socket); + _parent->_handle_error(this, ec); _parent->_remove_connect(this); return; } @@ -867,7 +875,7 @@ void ServerConnect::_write_tail(uint32_t size) } if (ec) { - _parent->_handle_error(ec, _socket); + _parent->_handle_error(this, ec); _parent->_remove_connect(this); return; } diff --git a/project/dl_chess b/project/dl_chess index 5004e31d..9c4fe2ff 160000 --- a/project/dl_chess +++ b/project/dl_chess @@ -1 +1 @@ -Subproject commit 5004e31d703e61dd401c06bacce36394d8503e37 +Subproject commit 9c4fe2ffc675db9978dd13b6ea29b388097dd3fc diff --git a/project/dl_chess_server b/project/dl_chess_server index fcd1354b..1f157be6 160000 --- a/project/dl_chess_server +++ b/project/dl_chess_server @@ -1 +1 @@ -Subproject commit fcd1354ba4659ea20ac2e3952ff1004bb08a1099 +Subproject commit 1f157be6c4d78207e33991fbfa2d5a3a663b20c0