TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec::SocketServerNd< TParams > Class Template Reference

NetData-protocol-aware TCP server with request dispatching and optional compression. More...

#include <tec_socket_server_nd.hpp>

Inheritance diagram for tec::SocketServerNd< TParams >:
tec::SocketServer< TParams > tec::Actor

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 &params)
 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 &params)
 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.
 
Actoroperator= (const Actor &)=delete
 Deleted copy assignment operator.
 
Actoroperator= (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 charget_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.
 

Detailed Description

template<typename TParams>
class tec::SocketServerNd< TParams >

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:

  • typed handler registration by NetData::ID
  • automatic compression/decompression (when enabled in params)
  • default echo handler (ID = 0)
  • structured error reply mechanism
  • fallback to raw string handling when message is not valid NetData
Template Parameters
TParamsMust provide at least:
  • bool compression
  • int compression_level
  • size_t compression_min_size
  • other fields required by base SocketServer<TParams>

Member Typedef Documentation

◆ HandlerFunc

template<typename TParams >
using tec::SocketServerNd< TParams >::HandlerFunc = std::function<void (ServerNd*, DataInOut)>

Signature of request handler functions.

Handler receives full context (server instance + in/out data) and is responsible for:

  • reading nd_in
  • preparing nd_out (if reply is needed)
  • updating dio.status

Constructor & Destructor Documentation

◆ SocketServerNd()

template<typename TParams >
tec::SocketServerNd< TParams >::SocketServerNd ( const Params &  params)
inlineexplicit

Constructs server and automatically registers default echo handler (ID=0)

Parameters
paramsConfiguration parameters (compression settings, listen address, etc.)

Member Function Documentation

◆ compress()

template<typename TParams >
virtual Status tec::SocketServerNd< TParams >::compress ( NetData nd)
inlineprotectedvirtual

Applies compression to the NetData message if enabled in parameters.

Parameters
ndMessage to compress (modified in-place)
Returns
success / compression error

◆ dispatch()

template<typename TParams >
virtual Status tec::SocketServerNd< TParams >::dispatch ( ID  id,
DataInOut  dio 
)
inlineprotectedvirtual

Finds and invokes the handler registered for the given message ID.

Parameters
idMessage identifier from the received NetData header
dioRequest/response context (input/output NetData, socket, status)
Returns
Final status after handler execution (or ENOTSUP if no handler found)

◆ echo()

template<typename TParams >
virtual void tec::SocketServerNd< TParams >::echo ( DataInOut  dio)
inlineprotectedvirtual

Default handler (ID=0) — echoes the received message back.

Parameters
dioContext containing input/output NetData and status

◆ on_net_data()

template<typename TParams >
void tec::SocketServerNd< TParams >::on_net_data ( const Socket s)
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:

  1. receive + uncompress
  2. dispatch to registered handler
  3. compress reply
  4. send reply
  5. send error reply on failure (unless connection already closed)
Parameters
sBase Socket pointer received from acceptor loop

Reimplemented from tec::SocketServer< TParams >.

◆ postprocess()

template<typename TParams >
virtual Status tec::SocketServerNd< TParams >::postprocess ( NetData nd)
inlineprotectedvirtual

Post-processes outgoing message (currently only compression)

Parameters
ndMessage prepared by handler (modified in-place)

◆ preprocess()

template<typename TParams >
virtual Status tec::SocketServerNd< TParams >::preprocess ( NetData nd)
inlineprotectedvirtual

Pre-processes incoming message (currently only decompression)

Parameters
ndReceived message (modified in-place)

◆ register_handler()

template<typename TParams >
template<typename Derived >
void tec::SocketServerNd< TParams >::register_handler ( Derived server,
ID  id,
void(Derived::*)(DataInOut dio handler 
)
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.

Template Parameters
DerivedType of the actual server class (must derive from SocketServerNd)
Parameters
serverPointer to the derived server instance (usually this)
idMessage identifier this handler should process
handlerMember function pointer of form void Derived::handler(DataInOut)

◆ reply_error()

template<typename TParams >
virtual void tec::SocketServerNd< TParams >::reply_error ( Status  status,
NetData::ID  request_id,
SocketNd sock 
)
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.

Parameters
statusError status to send
request_idOriginal request identifier
sockClient connection

◆ uncompress()

template<typename TParams >
virtual Status tec::SocketServerNd< TParams >::uncompress ( NetData nd)
inlineprotectedvirtual

Decompresses the NetData message if header indicates compression.

Parameters
ndMessage to decompress (modified in-place)
Returns
success / decompression error

The documentation for this class was generated from the following file: