* net异常处理补全
This commit is contained in:
+36
-28
@@ -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;
|
||||
}
|
||||
|
||||
+1
-1
Submodule project/dl_chess updated: 5004e31d70...9c4fe2ffc6
+1
-1
Submodule project/dl_chess_server updated: fcd1354ba4...1f157be6c4
Reference in New Issue
Block a user