|
TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
|
NetData-protocol-aware TCP server with request dispatching and optional compression. More...
#include <tec_socket_server_nd.hpp>
Classes | |
| struct | DataInOut |
| Container carrying context for one request/response cycle. More... | |
Public Types | |
| using | Params = TParams |
| using | Lock = std::lock_guard< std::mutex > |
| using | ID = NetData::ID |
| using | ServerNd = SocketServerNd< Params > |
| using | HandlerFunc = std::function< void(ServerNd *, DataInOut)> |
| Signature of request handler functions. | |
Public Types inherited from tec::SocketServer< TParams > | |
| using | Params = TParams |
Parameter type — must inherit from tec::SocketServerParams | |
Public Member Functions | |
| SocketServerNd (const Params ¶ms) | |
| Constructs server and automatically registers default echo handler (ID=0) | |
| template<typename Derived > | |
| void | register_handler (Derived *server, ID id, void(Derived::*handler)(DataInOut dio)) |
| Registers a request handler for a specific NetData message ID. | |
Public Member Functions inherited from tec::SocketServer< TParams > | |
| 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 | |
| virtual Status | dispatch (ID id, DataInOut dio) |
| Finds and invokes the handler registered for the given message ID. | |
| virtual void | reply_error (Status status, NetData::ID request_id, SocketNd *sock) |
| Sends a minimal error reply containing only status code. | |
| virtual Status | compress (NetData *nd) |
| Applies compression to the NetData message if enabled in parameters. | |
| virtual Status | uncompress (NetData *nd) |
| Decompresses the NetData message if header indicates compression. | |
| virtual Status | preprocess (NetData *nd) |
| Pre-processes incoming message (currently only decompression) | |
| virtual Status | postprocess (NetData *nd) |
| Post-processes outgoing message (currently only compression) | |
| void | on_net_data (const Socket *s) override |
| Main entry point for handling new data on a client connection. | |
| virtual void | echo (DataInOut dio) |
| Default handler (ID=0) — echoes the received message back. | |
Protected Member Functions inherited from tec::SocketServer< TParams > | |
| 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. | |
Additional Inherited Members | |
Protected Attributes inherited from tec::SocketServer< TParams > | |
| 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. | |
NetData-protocol-aware TCP server with request dispatching and optional compression.
SocketServerNd<TParams> extends SocketServer<TParams> to handle the NetData message framing protocol (header + payload) and provides:
| TParams | Must provide at least:
|
Signature of request handler functions.
Handler receives full context (server instance + in/out data) and is responsible for:
|
inlineexplicit |
Constructs server and automatically registers default echo handler (ID=0)
| params | Configuration parameters (compression settings, listen address, etc.) |
|
inlineprotectedvirtual |
Applies compression to the NetData message if enabled in parameters.
| nd | Message to compress (modified in-place) |
|
inlineprotectedvirtual |
|
inlineprotectedvirtual |
Default handler (ID=0) — echoes the received message back.
| dio | Context containing input/output NetData and status |
|
inlineoverrideprotectedvirtual |
Main entry point for handling new data on a client connection.
Tries to read structured NetData message first. If parsing fails with EBADMSG → falls back to raw string handler (legacy mode). Otherwise performs the full NetData request cycle:
| s | Base Socket pointer received from acceptor loop |
Reimplemented from tec::SocketServer< TParams >.
|
inlineprotectedvirtual |
Post-processes outgoing message (currently only compression)
| nd | Message prepared by handler (modified in-place) |
|
inlineprotectedvirtual |
Pre-processes incoming message (currently only decompression)
| nd | Received message (modified in-place) |
|
inline |
Registers a request handler for a specific NetData message ID.
Replaces any previously registered handler for the same ID. Uses dynamic_cast + lambda to allow handlers to be member functions of classes derived from SocketServerNd.
| Derived | Type of the actual server class (must derive from SocketServerNd) |
| server | Pointer to the derived server instance (usually this) |
| id | Message identifier this handler should process |
| handler | Member function pointer of form void Derived::handler(DataInOut) |
|
inlineprotectedvirtual |
Sends a minimal error reply containing only status code.
Used when processing fails before or after handler execution. Reply uses the same request_id so client can correlate it.
| status | Error status to send |
| request_id | Original request identifier |
| sock | Client connection |
|
inlineprotectedvirtual |
Decompresses the NetData message if header indicates compression.
| nd | Message to decompress (modified in-place) |