|
TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
|
Templated client socket actor for establishing and managing connections. More...
#include <tec_socket_client.hpp>
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 ¶ms) | |
| 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. | |
| 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 () |
| 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. | |
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:
| TParams | The parameter type, which must derive from SocketClientParams for configuration. |
|
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.
| params | The configuration parameters for the client. |
|
inlinevirtual |
Destructor that ensures the socket is terminated if still open.
Calls terminate() if sockfd_ is not EOF to cleanly close the connection.
|
inlineconstexprprotected |
Returns a pointer to the internal buffer.
|
inlineconstexprprotected |
Returns the size of the internal buffer.
|
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).
| request | The incoming request (std::any). |
| reply | Optional reply object (std::any). |
Implements tec::Actor.
Reimplemented in tec::SocketClientNd< TParams >.
|
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).
| reply | Pointer to SocketCharStreamOut to store the received string. |
|
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.
| str_in | Pointer to the input string to send. |
| str_out | Pointer to the output string to receive (optional). |
|
inlineprotectedvirtual |
Sends a request and receives a reply in one operation.
Calls send_string followed by recv_string. Terminates the connection if send fails.
| request | Pointer to SocketCharStreamIn for sending. |
| reply | Pointer to SocketCharStreamOut for receiving (optional). |
|
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.
| request | Pointer to SocketCharStreamIn containing the string. |
|
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.
| sockfd | The socket file descriptor. |
|
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.
| sig_stopped | Signal to trigger on exit. |
Implements tec::Actor.
|
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.
| sig_started | Signal to trigger on exit (success or failure). |
| status | Pointer to Status object to report errors. |
Implements tec::Actor.