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

Templated client socket actor for establishing and managing connections. More...

#include <tec_socket_client.hpp>

Inheritance diagram for tec::SocketClient< TParams >:
tec::Actor tec::SocketClientNd< TParams >

Public Types

using Params = TParams
 Type alias for the template parameter TParams. This allows easy reference to the params type within the class.
 

Public Member Functions

 SocketClient (const Params &params)
 Constructs a SocketClient with the given parameters.
 
virtual ~SocketClient ()
 Destructor that ensures the socket is terminated if still open.
 
void start (Signal *sig_started, Status *status) override
 Starts the client by resolving the address and establishing a connection.
 
void shutdown (Signal *sig_stopped) override
 Shuts down the client connection.
 
Status process_request (Request request, Reply reply) override
 Processes incoming requests, handling SocketCharStreamIn types.
 
Status request_str (const std::string *str_in, std::string *str_out)
 Convenience method to send a string request and receive a response.
 
- 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

constexpr charget_buffer ()
 Returns a pointer to the internal buffer.
 
constexpr size_t get_buffer_size ()
 Returns the size of the internal buffer.
 
virtual Status set_socket_options (int sockfd)
 Virtual hook to set custom socket options after connection.
 
virtual Status send_string (const SocketCharStreamIn *request)
 Sends a string over the socket.
 
virtual Status recv_string (SocketCharStreamOut *reply)
 Receives a string from the socket.
 
virtual Status send_recv_string (const SocketCharStreamIn *request, SocketCharStreamOut *reply)
 Sends a request and receives a reply in one operation.
 

Protected Attributes

Params params_
 Instance of the parameters used for configuration. This holds settings like address, port, family, socktype, protocol, and buffer_size.
 
int sockfd_
 Socket file descriptor for the established connection. Initialized to EOF (-1) and set upon successful connection.
 

Detailed Description

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

Templated client socket actor for establishing and managing connections.

The SocketClient class provides a mechanism to connect to a server using socket APIs, send and receive data (primarily strings via SocketCharStreamIn/Out), and handle actor lifecycle events like start and shutdown. It uses a template parameter for configuration, ensuring type safety through static assertion.

Key features:

  • Resolves server addresses using getaddrinfo.
  • Establishes connections with fallback on multiple address candidates.
  • Supports sending and receiving null-terminated strings.
  • Provides virtual methods for customization (e.g., socket options, send/receive logic).
  • Integrates with Actor for signal-based start/stop and request processing.
Template Parameters
TParamsThe parameter type, which must derive from SocketClientParams for configuration.
See also
Actor
SocketClientParams
Socket

Constructor & Destructor Documentation

◆ SocketClient()

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

Constructs a SocketClient with the given parameters.

Initializes the actor base, copies the parameters, and sets the socket descriptor to EOF. Performs a static assertion to ensure TParams derives from SocketClientParams.

Parameters
paramsThe configuration parameters for the client.

◆ ~SocketClient()

template<typename TParams >
virtual tec::SocketClient< TParams >::~SocketClient ( )
inlinevirtual

Destructor that ensures the socket is terminated if still open.

Calls terminate() if sockfd_ is not EOF to cleanly close the connection.

Member Function Documentation

◆ get_buffer()

template<typename TParams >
constexpr char * tec::SocketClient< TParams >::get_buffer ( )
inlineconstexprprotected

Returns a pointer to the internal buffer.

Returns
char* Pointer to the buffer's data.

◆ get_buffer_size()

template<typename TParams >
constexpr size_t tec::SocketClient< TParams >::get_buffer_size ( )
inlineconstexprprotected

Returns the size of the internal buffer.

Returns
size_t The buffer size as specified in params_.

◆ process_request()

template<typename TParams >
Status tec::SocketClient< TParams >::process_request ( Request  request,
Reply  reply 
)
inlineoverridevirtual

Processes incoming requests, handling SocketCharStreamIn types.

Checks the request type and delegates to send_recv_string for string streams. Returns NotImplemented for other types (e.g., future NetData support).

Parameters
requestThe incoming request (std::any).
replyOptional reply object (std::any).
Returns
Status indicating success or error.

Implements tec::Actor.

Reimplemented in tec::SocketClientNd< TParams >.

◆ recv_string()

template<typename TParams >
virtual Status tec::SocketClient< TParams >::recv_string ( SocketCharStreamOut reply)
inlineprotectedvirtual

Receives a string from the socket.

Uses Socket::recv to receive into Bytes, then copies to the reply string (assuming null-terminated). Skips if reply is null (notification mode).

Parameters
replyPointer to SocketCharStreamOut to store the received string.
Returns
Status indicating success or error (e.g., Invalid if reply->str is null).

◆ request_str()

template<typename TParams >
Status tec::SocketClient< TParams >::request_str ( const std::string *  str_in,
std::string *  str_out 
)
inline

Convenience method to send a string request and receive a response.

Wraps the input/output strings in SocketCharStreamIn/Out and calls send_recv_string. Copies the response back to str_out if provided and successful.

Parameters
str_inPointer to the input string to send.
str_outPointer to the output string to receive (optional).
Returns
Status indicating success or error.

◆ send_recv_string()

template<typename TParams >
virtual Status tec::SocketClient< TParams >::send_recv_string ( const SocketCharStreamIn request,
SocketCharStreamOut reply 
)
inlineprotectedvirtual

Sends a request and receives a reply in one operation.

Calls send_string followed by recv_string. Terminates the connection if send fails.

Parameters
requestPointer to SocketCharStreamIn for sending.
replyPointer to SocketCharStreamOut for receiving (optional).
Returns
Status indicating overall success or error.

◆ send_string()

template<typename TParams >
virtual Status tec::SocketClient< TParams >::send_string ( const SocketCharStreamIn request)
inlineprotectedvirtual

Sends a string over the socket.

Converts the input string to Bytes (including null terminator) and uses Socket::send. Validates the request before sending.

Parameters
requestPointer to SocketCharStreamIn containing the string.
Returns
Status indicating success or error (e.g., Invalid if request is null).

◆ set_socket_options()

template<typename TParams >
virtual Status tec::SocketClient< TParams >::set_socket_options ( int  sockfd)
inlineprotectedvirtual

Virtual hook to set custom socket options after connection.

This method is called after successful connection but before buffer allocation (in derived classes). Default implementation does nothing.

Parameters
sockfdThe socket file descriptor.
Returns
Status indicating success (empty) or error.

◆ shutdown()

template<typename TParams >
void tec::SocketClient< TParams >::shutdown ( Signal sig_stopped)
inlineoverridevirtual

Shuts down the client connection.

This override of Actor::shutdown calls shutdown to stop RD/WR operations and closes the socket. Resets sockfd_ to EOF.

Parameters
sig_stoppedSignal to trigger on exit.

Implements tec::Actor.

◆ start()

template<typename TParams >
void tec::SocketClient< TParams >::start ( Signal sig_started,
Status status 
)
inlineoverridevirtual

Starts the client by resolving the address and establishing a connection.

This override of Actor::start resolves the server address using getaddrinfo, attempts to create a socket and connect to each resolved address until successful. Allocates the buffer upon success.

Parameters
sig_startedSignal to trigger on exit (success or failure).
statusPointer to Status object to report errors.

Implements tec::Actor.


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