|
TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
|
Generic BSD socket server template using actor pattern with configurable parameters. More...
#include <tec_socket_server.hpp>
Public Types | |
| using | Params = TParams |
Parameter type — must inherit from tec::SocketServerParams | |
Public Member Functions | |
| SocketServer (const Params ¶ms) | |
| Constructs server instance with given parameters. | |
| virtual | ~SocketServer ()=default |
| Virtual destructor (default implementation) | |
| void | start (Signal *sig_started, Status *status) override |
| Starts the server: bind → listen → accept loop. | |
| void | shutdown (Signal *sig_stopped) override |
| Gracefully shuts down the server. | |
| Status | process_request (Request request, Reply reply) override |
| Default request processor (not used in socket server) | |
Public Member Functions inherited from tec::Actor | |
| Actor ()=default | |
| Default constructor. | |
| Actor (const Actor &)=delete | |
| Deleted copy constructor. | |
| Actor (Actor &&)=delete | |
| Deleted move constructor. | |
| Actor & | operator= (const Actor &)=delete |
| Deleted copy assignment operator. | |
| Actor & | operator= (Actor &&)=delete |
| Deleted move assignment operator. | |
| virtual | ~Actor ()=default |
| Virtual destructor. | |
| virtual Status | run () |
| Mimics Daemon's behavior. | |
| virtual Status | terminate () |
| Mimics Daemon's behavior. | |
Protected Member Functions | |
| constexpr char * | get_buffer () |
| constexpr size_t | get_buffer_size () const |
| virtual Status | set_socket_options (int fd) |
| Sets common socket options (SO_REUSEADDR, SO_REUSEPORT) | |
| virtual Socket | get_socket_info (int client_fd, sockaddr_storage *client_addr) |
| Creates Socket object from raw file descriptor and peer address. | |
| virtual Status | resolve_and_bind_host () |
| Resolves address/port and binds listening socket. | |
| virtual Status | start_listening () |
| Starts listening on the bound socket. | |
| virtual Status | accept_connection (int *clientfd, sockaddr_storage *client_addr) |
| Accepts one incoming connection (blocking) | |
| virtual void | poll (Signal *sig_started) |
| Main acceptor loop — runs until stop_polling_ is set. | |
| virtual void | process_socket (Socket sock) |
| Decides how to handle newly accepted client socket. | |
| virtual void | dispatch_socket (Socket _sock) |
| Executes client connection handling logic. | |
| virtual void | close_client_connection (Socket *sock) |
| Closes client connection cleanly. | |
| virtual void | on_string (const Socket *sock) |
| Default handler for character-stream / line-based protocols. | |
| virtual void | on_net_data (const Socket *sock) |
| Default handler for binary / structured network protocols. | |
Protected Attributes | |
| Params | params_ |
| Server configuration parameters (address, port, buffer size, threading mode, etc.) | |
| int | listenfd_ |
| Listening socket file descriptor (-1 when not bound/listening) | |
| std::atomic_bool | stop_polling_ |
| Atomic flag used to signal the acceptor/polling loop to exit. | |
| Signal | polling_stopped_ |
| Signal object set when polling loop has fully exited. | |
Generic BSD socket server template using actor pattern with configurable parameters.
Supports both single-threaded (synchronous) and multi-threaded (thread-pool) modes. Implements the kModeCharStream mode – text oriented (default echo server behavior).
Main responsibilities:
| TParams | Must derive from SocketServerParams |
|
inlineexplicit |
Constructs server instance with given parameters.
| params | Configuration (address, port, buffer size, threading mode, socket options…) |
Does not start listening — call start() to begin serving.
|
inlineprotectedvirtual |
Accepts one incoming connection (blocking)
| [out] | clientfd | Accepted client file descriptor |
| [out] | client_addr | Filled peer address structure |
|
inlineprotectedvirtual |
Closes client connection cleanly.
| sock | Socket to close (fd set to -1 after close) |
|
inlineprotectedvirtual |
Executes client connection handling logic.
| _sock | Client socket (moved or copied) |
Dispatches to mode-specific handler (on_string or on_net_data). Always closes connection when handler returns.
|
inlineconstexprprotected |
|
inlineconstexprprotected |
|
inlineprotectedvirtual |
Creates Socket object from raw file descriptor and peer address.
| client_fd | Accepted client socket fd |
| client_addr | Peer address structure (IPv4 or IPv6) |
|
inlineprotectedvirtual |
Default handler for binary / structured network protocols.
| sock | Client connection |
Empty by default — override in derived classes.
Reimplemented in tec::SocketServerNd< TParams >.
Default handler for character-stream / line-based protocols.
| sock | Client connection |
Default implementation: simple echo server. Override in derived classes for real protocol handling.
|
inlineprotectedvirtual |
Main acceptor loop — runs until stop_polling_ is set.
| sig_started | Signal to set once loop has started |
|
inlineoverridevirtual |
Default request processor (not used in socket server)
Implements tec::Actor.
|
inlineprotectedvirtual |
Decides how to handle newly accepted client socket.
| sock | Filled client socket information |
Routes to thread pool (async) or direct dispatch (sync) based on configuration.
|
inlineprotectedvirtual |
Resolves address/port and binds listening socket.
|
inlineprotectedvirtual |
Sets common socket options (SO_REUSEADDR, SO_REUSEPORT)
| fd | Socket file descriptor to configure |
|
inlineoverridevirtual |
Gracefully shuts down the server.
| sig_stopped | Signal to set when shutdown is complete |
Closes listening socket and waits until acceptor loop exits. Running client handlers (in thread pool) are allowed to finish.
Implements tec::Actor.
|
inlineoverridevirtual |
Starts the server: bind → listen → accept loop.
| sig_started | Signal to set once server is ready (bound & listening) |
| status | [out] Result of initialization (binding/listening) |
Called from actor framework. Blocking until server is stopped.
Implements tec::Actor.
|
inlineprotectedvirtual |
Starts listening on the bound socket.