TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_socket_client_nd.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-05 02:43:24 by magnolia>
2/*----------------------------------------------------------------------
3------------------------------------------------------------------------
4Copyright (c) 2020-2026 The Emacs Cat (https://github.com/olddeuteronomy/tec).
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17------------------------------------------------------------------------
18----------------------------------------------------------------------*/
19
46#pragma once
47
48#ifndef _POSIX_C_SOURCE
49#define _POSIX_C_SOURCE 200809L // This line fixes the "storage size of ‘hints’ isn’t known" issue.
50#endif
51
52#include <any>
53#include <cerrno>
54#include <cstdlib>
55
56#include <sys/types.h>
57#include <sys/socket.h>
58#include <netdb.h>
59
60#include "tec/tec_def.hpp" // IWYU pragma: keep
61#include "tec/tec_trace.hpp"
62#include "tec/tec_status.hpp"
63#include "tec/tec_message.hpp"
68
69
70namespace tec {
71
95template <typename TParams>
96class SocketClientNd: public SocketClient<TParams> {
97public:
100 using Params = TParams;
101
109 explicit SocketClientNd(const Params& params)
110 : SocketClient<Params>(params)
111 {
112 }
113
119 virtual ~SocketClientNd() = default;
120
132 Status process_request(Request request, Reply reply) override {
133 TEC_ENTER("SocketClientNd::process_request");
134 if( request.type() == typeid(const NetData::StreamIn*) &&
135 reply.type() == typeid(NetData::StreamOut*)) {
136 // NetData: Both request and reply are required.
137 if (!request.has_value() || !reply.has_value()) {
138 return {EINVAL, Error::Kind::Invalid};
139 }
140 const NetData::StreamIn* req = std::any_cast<const NetData::StreamIn*>(request);
141 NetData::StreamOut* rep = std::any_cast<NetData::StreamOut*>(reply);
142 if (req->nd == nullptr || rep->nd == nullptr) {
143 return {EFAULT, Error::Kind::Invalid};
144 }
145 return send_recv_nd(req->nd, rep->nd);
146 }
147
148 // Default processing.
149 return SocketClient<Params>::process_request(request, reply);
150 }
151
161 Status request_nd(NetData* nd_in, NetData* nd_out) {
162 TEC_ENTER("SocketClientNd::request");
163 auto status = send_recv_nd(nd_in, nd_out);
164 return status;
165 }
166
167protected:
168
177 virtual Status send_nd(NetData* nd) {
178 TEC_ENTER("SocketClientNd::send_nd");
179 // Socket helper.
180 SocketNd sock{this->sockfd_, this->params_.addr.c_str(), this->params_.port,
181 this->get_buffer(), this->get_buffer_size()};
182 return SocketNd::send_nd(nd, &sock);
183 }
184
193 virtual Status recv_nd(NetData* nd) {
194 TEC_ENTER("SocketClientNd::recv_nd");
195 // Socket helper.
196 SocketNd sock{this->sockfd_, this->params_.addr.c_str(), this->params_.port,
197 this->get_buffer(), this->get_buffer_size()};
198 auto status = SocketNd::recv_nd(nd, &sock);
199 nd->rewind();
200 return status;
201 }
202
203 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
204 *
205 * Pre- and postprocessing
206 *
207 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
208
217 virtual Status compress(NetData* nd) {
218 NdCompress cmpr(this->params_.compression,
219 this->params_.compression_level,
220 this->params_.compression_min_size);
221 return cmpr.compress(*nd);
222 }
223
232 virtual Status uncompress(NetData* nd) {
233 NdCompress cmpr(this->params_.compression,
234 this->params_.compression_level,
235 this->params_.compression_min_size);
236 return cmpr.uncompress(*nd);
237 }
238
247 virtual Status preprocess(NetData* nd) {
248 return compress(nd);
249 }
250
260 return uncompress(nd);
261 }
262
263 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
264 *
265 * Data send/receive
266 *
267 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
268
280 virtual Status send_recv_nd(NetData* nd_in, NetData* nd_out) {
281 TEC_ENTER("SocketClientNd::send_recv_nd");
282 //
283 // Preprocess the input.
284 //
285 auto status = preprocess(nd_in);
286 if (status) {
287 //
288 // Send a request.
289 //
290 status = send_nd(nd_in);
291 if (status) {
292 //
293 // Receive a reply.
294 //
295 status = recv_nd(nd_out);
296
297 }
298 }
299 //
300 // Postprocess the output.
301 //
302 if (status) {
303 status = postprocess(nd_out);
304 }
305 else {
306 this->terminate();
307 }
308 return status;
309 }
310
311}; // class SocketClientNd
312
313} // namespace tec
virtual Status terminate()
Mimics Daemon's behavior.
Definition tec_actor.hpp:183
Compression wrapper for NetData objects with pluggable backends.
Definition tec_nd_compress.hpp:89
virtual Status compress(NetData &nd) const
Compresses the payload of a NetData object in-place (if configured)
Definition tec_nd_compress.hpp:161
virtual Status uncompress(NetData &nd) const
Decompresses the payload of a NetData object in-place (if compressed)
Definition tec_nd_compress.hpp:202
Lightweight binary serialization container optimized for network communication.
Definition tec_net_data.hpp:51
void rewind()
Resets read position to the beginning of the buffer.
Definition tec_net_data.hpp:119
Templated client socket actor for NetData stream handling.
Definition tec_socket_client_nd.hpp:96
virtual Status send_nd(NetData *nd)
Sends a NetData object over the socket.
Definition tec_socket_client_nd.hpp:177
virtual ~SocketClientNd()=default
Default destructor.
SocketClientNd(const Params &params)
Constructs a SocketClientNd with the given parameters.
Definition tec_socket_client_nd.hpp:109
TParams Params
Type alias for the template parameter TParams. This allows easy reference to the params type within t...
Definition tec_socket_client_nd.hpp:100
virtual Status recv_nd(NetData *nd)
Receives a NetData object from the socket.
Definition tec_socket_client_nd.hpp:193
Status process_request(Request request, Reply reply) override
Processes incoming requests, handling NetData types.
Definition tec_socket_client_nd.hpp:132
Status request_nd(NetData *nd_in, NetData *nd_out)
Convenience method to send a NetData request and receive a response.
Definition tec_socket_client_nd.hpp:161
virtual Status uncompress(NetData *nd)
Uncompresses the NetData using configured parameters.
Definition tec_socket_client_nd.hpp:232
virtual Status postprocess(NetData *nd)
Postprocesses the NetData after receiving (default: uncompress).
Definition tec_socket_client_nd.hpp:259
virtual Status send_recv_nd(NetData *nd_in, NetData *nd_out)
Sends a NetData request and receives a reply with pre/postprocessing.
Definition tec_socket_client_nd.hpp:280
virtual Status preprocess(NetData *nd)
Preprocesses the NetData before sending (default: compress).
Definition tec_socket_client_nd.hpp:247
virtual Status compress(NetData *nd)
Compresses the NetData using configured parameters.
Definition tec_socket_client_nd.hpp:217
Templated client socket actor for establishing and managing connections.
Definition tec_socket_client.hpp:92
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
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
#define TEC_ENTER(name)
Logs an entry message for a named context (e.g., function).
Definition tec_trace.hpp:211
@ Invalid
Invalid data or state.
Helper type for ADL customization of input operations.
Definition tec_net_data.hpp:478
Helper type for ADL customization of output operations.
Definition tec_net_data.hpp:493
Specialized socket wrapper optimized for sending/receiving NetData protocol messages.
Definition tec_socket_nd.hpp:63
static Status send_nd(const NetData *nd, const SocketNd *sock)
Sends a complete NetData message (header + payload) over the socket.
Definition tec_socket_nd.hpp:96
static Status recv_nd(NetData *nd, const SocketNd *sock)
Receives one complete NetData message (header + payload)
Definition tec_socket_nd.hpp:148
char addr[INET6_ADDRSTRLEN]
Peer address as a null-terminated string (IPv4 or IPv6).
Definition tec_socket.hpp:274
Common definitions and utilities for the tec namespace.
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
Compression wrapper for NetData objects with pluggable backends.
Lightweight binary serialization optimized for network communication.
Definition of the SocketClient class template.
NetData BSD socket operations.
Defines error handling types and utilities for the tec namespace.
Provides a thread-safe tracing utility for debugging in the tec namespace.