From 0c743f213a07a44980e8965c5b73a0b4545e48a2 Mon Sep 17 00:00:00 2001 From: Alexander Yee Date: Sun, 8 Nov 2026 00:37:14 -0800 Subject: [PATCH] Cleanup Sockets --- Common/Cpp/Sockets/AbstractClientSocket.h | 40 +---------------- Common/Cpp/Sockets/AbstractServerSocket.h | 44 ++----------------- Common/Cpp/Sockets/ClientSocket_POSIX.cpp | 2 - Common/Cpp/Sockets/ClientSocket_WinSocket.cpp | 2 - Common/Cpp/Sockets/ServerSocket_POSIX.cpp | 2 - Common/Cpp/Sockets/ServerSocket_WinSocket.cpp | 2 - 6 files changed, 5 insertions(+), 87 deletions(-) diff --git a/Common/Cpp/Sockets/AbstractClientSocket.h b/Common/Cpp/Sockets/AbstractClientSocket.h index 37f3ddb0d4..2fe9f71070 100644 --- a/Common/Cpp/Sockets/AbstractClientSocket.h +++ b/Common/Cpp/Sockets/AbstractClientSocket.h @@ -25,27 +25,8 @@ class AbstractClientSocket{ }; struct Listener{ - // A listener must outlive the socket that calls it. Callbacks can - // still be delivered after a non-blocking "close()" returns - the - // socket's threads only stop when the socket itself is destroyed - - // so a listener must not be destroyed before the socket that added - // it. Declare the listener before the socket, so that it is - // destroyed after the socket. - // Called at the start of the receiver thread. This can be used to set - // the thread priority of the thread. - virtual void on_thread_start(){} - - // Called when a connection attempt finishes. - // If successful, "error_message" is empty. - // Otherwise, it contains the error message. virtual void on_connect_finished(const std::string& error_message){} - - // 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){} }; @@ -67,30 +48,11 @@ class AbstractClientSocket{ return m_state.load(std::memory_order_relaxed); } - // Requests immediate teardown of the active connection, without waiting - // for the receive thread to exit. This is safe to call from within a - // listener callback (which runs on that thread; the blocking teardown - // there would deadlock). It marks the socket DESTRUCTING and releases - // the underlying socket; the receive thread observes this on its next - // iteration and exits. It cancels any in-flight "send()": its caller - // returns promptly with the number of bytes actually written so far - // (best effort). No "on_disconnect" callback is fired. The socket is - // left in State::DESTRUCTING; the socket's destructor completes the - // teardown (joins the thread and returns it to NOT_RUNNING). A later - // "connect()" on the same socket completes that teardown first, so - // the socket can be reused. Calling "close()" when there is no - // active connection is a no-op. It is safe to call multiple times. + // Close the connection and immediately cancel all ongoing sends. virtual void close() noexcept = 0; virtual void connect(const std::string& address, uint16_t port) = 0; - // Sends data on the active connection. This blocks until every byte - // has been written, the connection is lost, "close()" is called, or - // "timeout" elapses. "std::chrono::milliseconds::max()" (the default) - // waits indefinitely. Returns the number of bytes actually written, - // which may be less than "bytes" if the deadline elapsed, the - // connection was lost, or "close()" cancelled the send. If there is - // no active connection, returns 0. virtual size_t send( const void* data, size_t bytes, diff --git a/Common/Cpp/Sockets/AbstractServerSocket.h b/Common/Cpp/Sockets/AbstractServerSocket.h index ddec7d5f0c..4139a0f120 100644 --- a/Common/Cpp/Sockets/AbstractServerSocket.h +++ b/Common/Cpp/Sockets/AbstractServerSocket.h @@ -30,34 +30,13 @@ class AbstractServerSocket{ }; struct Listener{ - // A listener must outlive the socket that calls it. Callbacks can - // still be delivered after "close_connection()" returns - the - // connection's threads only stop when the connection (or the - // socket) is destroyed - so a listener must not be destroyed - // before the socket that added it. Declare the listener before - // the socket, so that it is destroyed after the socket. - // Called at the start of the thread that will handle a connection. - // This can be used to set the thread priority of the thread. - virtual void on_thread_start(){} - // Called when "start()" finishes. // If successful, "error_message" is empty. // Otherwise, it contains the error message. virtual void on_listen_finished(const std::string& error_message){} - - // Called when a client connects. virtual void on_connect(){} - - // Called when an incoming connection was rejected because a - // client is already connected. virtual void on_connect_rejected(){} - - // Called when the server receives data from the client. virtual void on_receive_data(const void* data, size_t bytes){} - - // Called when the active connection is lost (the client closed - // the connection or a socket error occurred). It is not called - // by "close_and_wait()" or "close_connection()". virtual void on_disconnect(const std::string& error_message){} }; @@ -79,29 +58,14 @@ class AbstractServerSocket{ return m_state.load(std::memory_order_relaxed); } - // Binds to "address" and listens on "port". Use "0.0.0.0" to accept - // connections on all interfaces. The result is reported through - // "Listener::on_listen_finished", after which the state is - // State::LISTENING on success or State::NOT_RUNNING on failure. + // Binds to "address" and listens on "port". + // Use "0.0.0.0" to accept connections on all interfaces. virtual void start(const std::string& address, uint16_t port) = 0; - // Closes the active connection, if any, and keeps listening. Unlike - // the full teardown the destructor performs, this does not wait for - // the worker threads, so it is safe to call from within a listener - // callback. "Listener::on_disconnect" is not called for the - // connection closed this way. - // - // It cancels any in-flight "send()": the send returns promptly with - // the number of bytes actually written so far (best effort). + // Close the connection and immediately cancel all ongoing sends. + // The socket will continue listening for a new incoming connection. virtual void close_connection() = 0; - // Sends data to the active connection. This blocks until every byte - // has been written, the connection is lost, - // "close_and_wait()"/"close_connection()" is called, or "timeout" elapses. - // "std::chrono::milliseconds::max()" (the default) waits indefinitely. - // Returns the number of bytes actually written, which may be less - // than "bytes" if the deadline elapsed, the connection was lost, or - // the send was cancelled. If there is no active connection, returns 0. virtual size_t send( const void* data, size_t bytes, diff --git a/Common/Cpp/Sockets/ClientSocket_POSIX.cpp b/Common/Cpp/Sockets/ClientSocket_POSIX.cpp index 166b1a553a..1ecd99aec4 100644 --- a/Common/Cpp/Sockets/ClientSocket_POSIX.cpp +++ b/Common/Cpp/Sockets/ClientSocket_POSIX.cpp @@ -236,8 +236,6 @@ void ClientSocket_POSIX::thread_loop(std::string address, uint16_t port){ } } void ClientSocket_POSIX::thread_loop_internal(std::string address, uint16_t port){ - m_listeners.run_method(&Listener::on_thread_start); - { sockaddr_in server; std::memset(&server, 0, sizeof(server)); diff --git a/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp b/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp index 13582dac62..865b32cbab 100644 --- a/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp +++ b/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp @@ -235,8 +235,6 @@ void ClientSocket_WinSocket::thread_loop(const std::string& address, uint16_t po void ClientSocket_WinSocket::thread_loop_internal(const std::string& address, uint16_t port){ - m_listeners.run_method(&Listener::on_thread_start); - { sockaddr_in server; server.sin_family = AF_INET; diff --git a/Common/Cpp/Sockets/ServerSocket_POSIX.cpp b/Common/Cpp/Sockets/ServerSocket_POSIX.cpp index dccb4fe3d8..75b2a14ead 100644 --- a/Common/Cpp/Sockets/ServerSocket_POSIX.cpp +++ b/Common/Cpp/Sockets/ServerSocket_POSIX.cpp @@ -403,8 +403,6 @@ void ServerSocket_POSIX::accept_thread_loop(std::string address, uint16_t port, } void ServerSocket_POSIX::connection_thread_loop(){ - m_listeners.run_method(&Listener::on_thread_start); - int socket; bool active; { diff --git a/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp b/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp index 98ff0de025..55889f4b38 100644 --- a/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp +++ b/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp @@ -423,8 +423,6 @@ void ServerSocket_WinSocket::accept_thread_loop(std::string address, uint16_t po } void ServerSocket_WinSocket::connection_thread_loop(){ - m_listeners.run_method(&Listener::on_thread_start); - SOCKET socket; bool active; {