Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 1 addition & 39 deletions Common/Cpp/Sockets/AbstractClientSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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){}
};

Expand All @@ -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,
Expand Down
44 changes: 4 additions & 40 deletions Common/Cpp/Sockets/AbstractServerSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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){}
};

Expand All @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions Common/Cpp/Sockets/ClientSocket_POSIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 0 additions & 2 deletions Common/Cpp/Sockets/ClientSocket_WinSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions Common/Cpp/Sockets/ServerSocket_POSIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
{
Expand Down
2 changes: 0 additions & 2 deletions Common/Cpp/Sockets/ServerSocket_WinSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
{
Expand Down
Loading