From b2e41372409a0a6c6061584c03b99605650ce224 Mon Sep 17 00:00:00 2001 From: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com> Date: Sat, 31 Oct 2026 07:15:36 +0000 Subject: [PATCH] Sockets: add Listener::on_disconnect() hook New virtual AbstractClientSocket::Listener::on_disconnect(error_message), empty by default, called when an established connection is lost (the server closed the connection or a socket error occurred). It is not called by close(). The Windows implementation fires it in 'Fixes to ClientSocket_WinSocket'; the POSIX and Qt implementations adopt it in their own follow-up PRs. The sysbotbase connection overrides it to log the disconnect and update the status line, so a sys-botbase restart shows 'Disconnected: ...' instead of a zombie 'Last Ack: Ns ago'. Also log the actual error_message in on_connect_finished() (previously only the 'Connecting To: ...' line was logged, hiding the underlying error). --- Common/Cpp/Sockets/AbstractClientSocket.h | 5 +++++ .../Controllers/SysbotBase/SysbotBase_Connection.cpp | 8 +++++++- .../Controllers/SysbotBase/SysbotBase_Connection.h | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Common/Cpp/Sockets/AbstractClientSocket.h b/Common/Cpp/Sockets/AbstractClientSocket.h index 1c328ddad5..301f88c40c 100644 --- a/Common/Cpp/Sockets/AbstractClientSocket.h +++ b/Common/Cpp/Sockets/AbstractClientSocket.h @@ -35,6 +35,11 @@ class AbstractClientSocket{ // Called when the socket receives data from the server. virtual void on_receive_data(const void* data, size_t bytes){} + + // Called when an established connection is lost (the server closed + // the connection or a socket error occurred). It is not called by + // "close()". + virtual void on_disconnect(const std::string& error_message){} }; void add_listener(Listener& listener){ diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp index d536ec641e..5348de011d 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp @@ -151,7 +151,7 @@ void TcpSysbotBase_Connection::thread_loop(){ void TcpSysbotBase_Connection::on_connect_finished(const std::string& error_message){ try{ if (!error_message.empty()){ - m_logger.log(m_connecting_message, COLOR_RED); + m_logger.log(m_connecting_message + " - " + error_message, COLOR_RED); set_status_line0(m_connecting_message, COLOR_RED); return; } @@ -162,6 +162,12 @@ void TcpSysbotBase_Connection::on_connect_finished(const std::string& error_mess write_data("getVersion\r\n"); }catch (...){} } +void TcpSysbotBase_Connection::on_disconnect(const std::string& error_message){ + try{ + m_logger.log("sys-botbase disconnected: " + error_message, COLOR_RED); + set_status_line1("Disconnected: " + error_message, COLOR_RED); + }catch (...){} +} void TcpSysbotBase_Connection::on_receive_data(const void* data, size_t bytes){ // cout << "on_receive_data(): " << std::string((const char*)data, bytes - 2) << endl; diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h index 550ab82fed..e3b5b5b9dc 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h @@ -49,6 +49,7 @@ class TcpSysbotBase_Connection : public ControllerConnection, private ClientSock virtual void on_connect_finished(const std::string& error_message) override; virtual void on_receive_data(const void* data, size_t bytes) override; + virtual void on_disconnect(const std::string& error_message) override; void process_message(const std::string& message, WallClock timestamp); void set_mode(const std::string& sbb_version);