Skip to content

Sockets: add AbstractServerSocket - #1785

Merged
Mysticial merged 1 commit into
mainfrom
sockets-abstract-server-socket
Oct 31, 2026
Merged

Mysticial merged 1 commit into
mainfrom
sockets-abstract-server-socket

Conversation

@pa-devbot

Copy link
Copy Markdown
Collaborator

Abstract class only - no backends yet (Qt/POSIX/WinSocket to follow).

Mirrors the shape of AbstractClientSocket:

  • One connection at a time. Implementations use one worker thread for the accept loop and one thread-pool thread per active connection; a second incoming connection while one is active is rejected (on_connect_rejected).
  • State machine: NOT_RUNNING -> LISTENING -> CONNECTED -> LISTENING, with DESTRUCTING while close() is in flight.
  • Listener: on_thread_start, on_listen_finished(error_message) (the result of start(), like on_connect_finished for the client), on_connect, on_connect_rejected, on_receive_data, on_disconnect(error_message) (same semantics as the client's - not fired by close()/close_connection()).
  • API: start(address, port) (async, result via listener), close_connection() (drop the client, keep listening), close() (full teardown, idempotent), send() (to the active connection, 0 if none).

Design notes:

  • The accept thread hands the connection off to the pool with plain dispatch (not dispatch_now_blocking) - an earlier task blocking on a later task can deadlock the pool (see the note in ThreadPool.h).
  • Callbacks run on the worker threads, so close()/close_connection() must not be called from within a listener callback (same contract as ClientSocket).
  • Extensions deliberately left out of v1: per-connection ids (for concurrent connections later), the client's address in on_connect, and get_port() for bind-port-0.

Abstract class for a minimal TCP server, mirroring the shape of
AbstractClientSocket:

- One connection at a time. Implementations use one worker thread for
  the accept loop and one thread-pool thread per active connection; a
  second incoming connection while one is active is rejected.
- State machine: NOT_RUNNING -> LISTENING -> CONNECTED -> LISTENING,
  with DESTRUCTING while close() is in flight.
- Listener hooks: on_thread_start, on_listen_finished (result of
  start()), on_connect, on_connect_rejected, on_receive_data, and
  on_disconnect (same semantics as AbstractClientSocket::Listener::
  on_disconnect - not fired by close()).
- start(address, port) reports its result through the listener, the
  same way connect() does for the client.
- close_connection() drops the active client and keeps listening;
  close() is the full teardown and is idempotent.
@Mysticial
Mysticial merged commit b3bb4db into main Oct 31, 2026
7 checks passed
@Mysticial
Mysticial deleted the sockets-abstract-server-socket branch October 31, 2026 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants