From ace55da9b87738dd76975e807ead92b0ff862bbb Mon Sep 17 00:00:00 2001 From: Giovanni Bajo Date: Wed, 17 Jan 2024 10:44:54 +0100 Subject: [PATCH 1/2] nall: correctly handle TCP peer disconnections in tcp-socket.cpp Currently, when a client disconnects, the server in Ares fails to notice and to call the onDisconnect hook, which in turns cause the server to stop responding (it won't accept a new client, and will never receive more data from the disconnected one). This commit fixes it by correctly handling the recv() return code to detect peer disconnections (return value 0) and any other kind of error (return value < 0). --- nall/tcptext/tcp-socket.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/nall/tcptext/tcp-socket.cpp b/nall/tcptext/tcp-socket.cpp index 0ff85915d4..6e1353c88b 100644 --- a/nall/tcptext/tcp-socket.cpp +++ b/nall/tcptext/tcp-socket.cpp @@ -212,7 +212,7 @@ NALL_HEADER_INLINE auto Socket::open(u32 port, bool useIPv4) -> bool { while(!stopServer) { - if(fdClient < 0) { + if(fdClient < 0 || wantKickClient) { std::this_thread::sleep_for(std::chrono::milliseconds(CLIENT_SLEEP_MS)); continue; } @@ -228,6 +228,13 @@ NALL_HEADER_INLINE auto Socket::open(u32 port, bool useIPv4) -> bool { if constexpr(TCP_LOG_MESSAGES) { printf("%.4f | TCP <: [%d]: %.*s ([%d]: %.*s)\n", (f64)chrono::millisecond() / 1000.0, length, length, (char*)receiveBuffer.data(), length, length, (char*)packet); } + } else if(length == 0) { + disconnectClient(); + } else { + if (errno != EAGAIN) { + printf("TCP server: error receiving data from client: %s\n", strerror(errno)); + disconnectClient(); + } } } }); From ff2370ae74078e8d4ae0569b0600225a99499dc7 Mon Sep 17 00:00:00 2001 From: Giovanni Bajo Date: Wed, 17 Jan 2024 10:59:12 +0100 Subject: [PATCH 2/2] nall: log GDB client connections and disconnections --- nall/gdb/server.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nall/gdb/server.cpp b/nall/gdb/server.cpp index 2f28cb049e..eeb9ffb575 100644 --- a/nall/gdb/server.cpp +++ b/nall/gdb/server.cpp @@ -427,7 +427,6 @@ namespace nall::GDB { if(requestDisconnect) { requestDisconnect = false; - printf("GDB ending session, disconnecting client\n"); if(!noAckMode) { sendText("+"); } @@ -500,11 +499,13 @@ namespace nall::GDB { } auto Server::onConnect() -> void { + printf("GDB client connected\n"); resetClientData(); hasActiveClient = true; } auto Server::onDisconnect() -> void { + printf("GDB client disconnected\n"); hadHandshake = false; resetClientData(); }