47#ifndef _POSIX_C_SOURCE
48#define _POSIX_C_SOURCE 200809L
56#include <sys/socket.h>
91template <
typename TParams>
96 std::vector<char> buffer_;
139 std::is_base_of<SocketClientParams, Params>::value,
140 "Not derived from tec::SocketClientParams class");
170 ::memset(&hints, 0,
sizeof(hints));
171 hints.ai_family =
params_.family;
172 hints.ai_socktype =
params_.socktype;
173 hints.ai_protocol =
params_.protocol;
177 addrinfo* servinfo{NULL};
179 ::snprintf(port_str, 15,
"%d",
params_.port);
180 int ecode = ::getaddrinfo(
params_.addr.c_str(), port_str,
183 std::string emsg = ::gai_strerror(ecode);
184 TEC_TRACE(
"Error resolving address: {}", emsg);
195 for( p = servinfo; p != NULL; p = p->ai_next ) {
196 fd = ::socket(p->ai_family, p->ai_socktype, p->ai_protocol);
202 if( ::connect(fd, p->ai_addr, p->ai_addrlen) != -1 ) {
212 ::freeaddrinfo(servinfo);
257 TEC_ENTER(
"SocketClient::process_request");
262 if( reply.has_value() ) {
263 rep = std::any_cast<SocketCharStreamOut*>(reply);
289 if (status && (str_out !=
nullptr)) {
290 *str_out = *reply.str;
322 if (request && request->
str) {
324 Bytes data(request->
str->data(), request->
str->size() + 1);
345 if (reply ==
nullptr) {
349 if (reply->
str ==
nullptr) {
357 *reply->
str =
static_cast<const char*
>(data.
data());
373 TEC_ENTER(
"SocketClient::send_recv_string");
Abstract base class defining the actor lifecycle and request handling interface.
Definition tec_actor.hpp:63
virtual Status terminate()
Mimics Daemon's behavior.
Definition tec_actor.hpp:183
A byte buffer class with stream-like read/write semantics.
Definition tec_memfile.hpp:50
const void * data() const
Returns a const pointer to the buffer's data.
Definition tec_memfile.hpp:247
A thread-safe signal mechanism for inter-thread synchronization.
Definition tec_signal.hpp:44
Templated client socket actor for establishing and managing connections.
Definition tec_socket_client.hpp:92
virtual Status send_recv_string(const SocketCharStreamIn *request, SocketCharStreamOut *reply)
Sends a request and receives a reply in one operation.
Definition tec_socket_client.hpp:372
TParams Params
Type alias for the template parameter TParams. This allows easy reference to the params type within t...
Definition tec_socket_client.hpp:101
virtual Status recv_string(SocketCharStreamOut *reply)
Receives a string from the socket.
Definition tec_socket_client.hpp:343
Status request_str(const std::string *str_in, std::string *str_out)
Convenience method to send a string request and receive a response.
Definition tec_socket_client.hpp:284
constexpr size_t get_buffer_size()
Returns the size of the internal buffer.
Definition tec_socket_client.hpp:122
constexpr char * get_buffer()
Returns a pointer to the internal buffer.
Definition tec_socket_client.hpp:116
virtual Status send_string(const SocketCharStreamIn *request)
Sends a string over the socket.
Definition tec_socket_client.hpp:320
void start(Signal *sig_started, Status *status) override
Starts the client by resolving the address and establishing a connection.
Definition tec_socket_client.hpp:163
void shutdown(Signal *sig_stopped) override
Shuts down the client connection.
Definition tec_socket_client.hpp:237
Params params_
Instance of the parameters used for configuration. This holds settings like address,...
Definition tec_socket_client.hpp:106
int sockfd_
Socket file descriptor for the established connection. Initialized to EOF (-1) and set upon successfu...
Definition tec_socket_client.hpp:110
Status process_request(Request request, Reply reply) override
Processes incoming requests, handling SocketCharStreamIn types.
Definition tec_socket_client.hpp:256
virtual Status set_socket_options(int sockfd)
Virtual hook to set custom socket options after connection.
Definition tec_socket_client.hpp:307
virtual ~SocketClient()
Destructor that ensures the socket is terminated if still open.
Definition tec_socket_client.hpp:148
SocketClient(const Params ¶ms)
Constructs a SocketClient with the given parameters.
Definition tec_socket_client.hpp:133
#define TEC_ENTER(name)
Logs an entry message for a named context (e.g., function).
Definition tec_trace.hpp:211
#define TEC_TRACE(...)
Logs a formatted trace message.
Definition tec_trace.hpp:222
@ Invalid
Invalid data or state.
@ NetErr
Network-related error.
@ NotImplemented
Not implemented.
Helper struct to signal termination on exit.
Definition tec_signal.hpp:110
Input descriptor for character stream mode.
Definition tec_socket.hpp:242
const std::string * str
Pointer to the received null-terminated string.
Definition tec_socket.hpp:244
Output descriptor for character stream mode.
Definition tec_socket.hpp:254
std::string * str
Pointer to the string that will receive the response.
Definition tec_socket.hpp:256
Lightweight wrapper around a connected socket file descriptor.
Definition tec_socket.hpp:272
static Status recv(Bytes &data, const Socket *sock, size_t length)
Receive data from a socket into a MemFile (Bytes).
Definition tec_socket.hpp:329
static Status send(const Bytes &data, const Socket *sock)
Send the entire contents of a MemFile (Bytes) through a socket.
Definition tec_socket.hpp:395
char addr[INET6_ADDRSTRLEN]
Peer address as a null-terminated string (IPv4 or IPv6).
Definition tec_socket.hpp:274
Core interface for TEC actors with lifecycle management and request processing.
Common definitions and utilities for the tec namespace.
A byte buffer class with stream-like read/write semantics.
Defines a flexible message type and helper functions for the tec namespace.
std::any Reply
Type alias for a reply object that can hold any object.
Definition tec_message.hpp:55
std::any Request
Type alias for a request object that can hold any object.
Definition tec_message.hpp:49
std::string format(const T &arg)
Formats a single argument into a string.
Definition tec_print.hpp:171
Generic BSD socket parameters and helpers.
Defines error handling types and utilities for the tec namespace.
Provides a thread-safe tracing utility for debugging in the tec namespace.