11 #include "errors/fatal.h" 12 #include "string/format.h" 37 catch (CppCommon::SystemException& ex)
39 fatality(CppCommon::SystemException(ex.string()));
45 return nn_get_statistic(_socket, NN_STAT_ESTABLISHED_CONNECTIONS);
50 return nn_get_statistic(_socket, NN_STAT_ACCEPTED_CONNECTIONS);
55 return nn_get_statistic(_socket, NN_STAT_DROPPED_CONNECTIONS);
60 return nn_get_statistic(_socket, NN_STAT_BROKEN_CONNECTIONS);
65 return nn_get_statistic(_socket, NN_STAT_CONNECT_ERRORS);
70 return nn_get_statistic(_socket, NN_STAT_BIND_ERRORS);
75 return nn_get_statistic(_socket, NN_STAT_ACCEPT_ERRORS);
80 return nn_get_statistic(_socket, NN_STAT_CURRENT_CONNECTIONS);
85 return nn_get_statistic(_socket, NN_STAT_MESSAGES_SENT);
90 return nn_get_statistic(_socket, NN_STAT_MESSAGES_RECEIVED);
95 return nn_get_statistic(_socket, NN_STAT_BYTES_SENT);
100 return nn_get_statistic(_socket, NN_STAT_BYTES_RECEIVED);
108 _socket = nn_socket((
int)_domain, (
int)_protocol);
110 throwex CppCommon::SystemException(
"Failed to open a new nanomsg socket (domain={}, protocol={})! Nanomsg error: {}"_format(_domain, _protocol, nn_strerror(nn_errno())));
119 int result = nn_close(_socket);
121 throwex CppCommon::SystemException(
"Cannot close the nanomsg socket! Nanomsg error: {}"_format(nn_strerror(nn_errno())));
141 int result = nn_setsockopt(_socket, level, option, value, size);
144 if (nn_errno() == ETERM)
147 throwex CppCommon::SystemException(
"Cannot set the nanomsg socket option! Nanomsg error: {}"_format(nn_strerror(nn_errno())));
157 int result = nn_getsockopt(_socket, level, option, value, size);
160 if (nn_errno() == ETERM)
163 throwex CppCommon::SystemException(
"Cannot get the nanomsg socket option! Nanomsg error: {}"_format(nn_strerror(nn_errno())));
176 int result = nn_bind(_socket,
address.c_str());
179 if (nn_errno() == ETERM)
182 throwex CppCommon::SystemException(
"Cannot bind the nanomsg socket to the given endpoint '{}'! Nanomsg error: {}"_format(
address, nn_strerror(nn_errno())));
197 int result = nn_connect(_socket,
address.c_str());
200 if (nn_errno() == ETERM)
203 throwex CppCommon::SystemException(
"Cannot connect the nanomsg socket to the given endpoint '{}'! Nanomsg error: {}"_format(
address, nn_strerror(nn_errno())));
215 int result = nn_connect(_socket,
address.c_str());
218 if (nn_errno() == ETERM)
221 throwex CppCommon::SystemException(
"Cannot link the nanomsg socket to the given endpoint '{}'! Nanomsg error: {}"_format(
address, nn_strerror(nn_errno())));
234 int result = nn_shutdown(_socket, _endpoint);
237 if (nn_errno() == ETERM)
240 throwex CppCommon::SystemException(
"Cannot disconnect the nanomsg socket from the endpoint '{}'! Nanomsg error: {}"_format(_address, nn_strerror(nn_errno())));
249 assert((buffer !=
nullptr) &&
"Pointer to the buffer should not be equal to 'nullptr'!");
250 assert((size > 0) &&
"Buffer size should be greater than zero!");
251 if ((buffer ==
nullptr) || (size == 0))
260 int result = nn_send(_socket, buffer, size, 0);
263 if (nn_errno() == ETERM)
266 throwex CppCommon::SystemException(
"Cannot send {} bytes to the nanomsg socket! Nanomsg error: {}"_format(size, nn_strerror(nn_errno())));
273 assert((buffer !=
nullptr) &&
"Pointer to the buffer should not be equal to 'nullptr'!");
274 assert((size > 0) &&
"Buffer size should be greater than zero!");
275 if ((buffer ==
nullptr) || (size == 0))
284 int result = nn_send(_socket, buffer, size, NN_DONTWAIT);
287 if (nn_errno() == EAGAIN)
289 else if (nn_errno() == ETERM)
292 throwex CppCommon::SystemException(
"Cannot send {} bytes to the nanomsg socket in non-blocking mode! Nanomsg error: {}"_format(size, nn_strerror(nn_errno())));
308 void* data =
nullptr;
309 int result = nn_recv(_socket, &data, NN_MSG, 0);
312 if (nn_errno() == ETERM)
315 throwex CppCommon::SystemException(
"Cannot receive a message from the nanomsg socket! Nanomsg error: {}"_format(nn_strerror(nn_errno())));
318 message._buffer = (uint8_t*)data;
319 message._size = result;
320 return message.
size();
334 void* data =
nullptr;
335 int result = nn_recv(_socket, &data, NN_MSG, NN_DONTWAIT);
338 if (nn_errno() == EAGAIN)
340 else if (nn_errno() == ETERM)
343 throwex CppCommon::SystemException(
"Cannot receive a message from the nanomsg socket in non-blocking mode! Nanomsg error: {}"_format(nn_strerror(nn_errno())));
346 message._buffer = (uint8_t*)data;
347 message._size = result;
348 return message.
size();
357 return std::make_tuple(0,
true);
360 return std::make_tuple(0,
true);
362 void* data =
nullptr;
363 int result = nn_recv(_socket, &data, NN_MSG, 0);
366 if (nn_errno() == ETIMEDOUT)
367 return std::make_tuple(0,
true);
368 else if (nn_errno() == ETERM)
369 return std::make_tuple(0,
true);
371 throwex CppCommon::SystemException(
"Cannot receive a survey respond from the nanomsg socket! Nanomsg error: {}"_format(nn_strerror(nn_errno())));
374 message._buffer = (uint8_t*)data;
375 message._size = result;
376 return std::make_tuple(message.
size(),
false);
385 return std::make_tuple(0,
true);
388 return std::make_tuple(0,
true);
390 void* data =
nullptr;
391 int result = nn_recv(_socket, &data, NN_MSG, NN_DONTWAIT);
394 if (nn_errno() == EAGAIN)
395 return std::make_tuple(0,
false);
396 else if (nn_errno() == ETIMEDOUT)
397 return std::make_tuple(0,
true);
398 else if (nn_errno() == ETERM)
399 return std::make_tuple(0,
true);
401 throwex CppCommon::SystemException(
"Cannot receive a survey respond from the nanomsg socket in non-blocking mode! Nanomsg error: {}"_format(nn_strerror(nn_errno())));
404 message._buffer = (uint8_t*)data;
405 message._size = result;
406 return std::make_tuple(message.
size(),
false);
uint64_t established_connections() const noexcept
Get the number of connections successfully established that were initiated from this socket...
uint64_t accept_errors() const noexcept
Get the number of errors encountered by this socket trying to accept a a connection from a remote pee...
bool Reopen()
Reopen the socket.
bool SetSocketOption(int level, int option, const void *value, size_t size)
Set the socket option.
bool Close()
Close the socket.
bool Link(const std::string &address)
Link the socket to the remote endpoint.
static void Terminate()
Terminate all socket operations.
uint64_t connect_errors() const noexcept
Get the number of errors encountered by this socket trying to connect to a remote peer...
size_t TrySend(const void *buffer, size_t size)
Try to send data to the socket in non-blocking mode.
std::tuple< size_t, bool > ReceiveSurvey(Message &message)
Receive a respond to the survey from the socket in non-blocking mode.
bool GetSocketOption(int level, int option, void *value, size_t *size)
Get the socket option.
size_t size() const noexcept
Get the message size.
uint64_t broken_connections() const noexcept
Get the number of established connections that were closed by this socket, typically due to protocol ...
C++ Server project definitions.
Socket(Domain domain, Protocol protocol)
Initialize and open socket with a given domain and protocol.
std::tuple< size_t, bool > TryReceiveSurvey(Message &message)
Try to receive a respond to the survey from the socket in non-blocking mode.
uint64_t bind_errors() const noexcept
Get the number of errors encountered by this socket trying to bind to a local address.
bool Bind(const std::string &address)
Bind the socket to the local endpoint.
uint64_t current_connections() const noexcept
Get the number of connections currently estabalished to this socket.
uint64_t messages_sent() const noexcept
Get the number messages sent by this socket.
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by this socket.
bool IsConnected() const noexcept
Is socket connected?
uint64_t messages_received() const noexcept
Get the number messages received by this socket.
Protocol
Nanomsg protocol.
size_t Receive(Message &message)
Receive a message from the socket in non-blocking mode.
uint64_t bytes_received() const noexcept
Get the number of bytes received by this socket.
size_t Send(const void *buffer, size_t size)
Send data to the socket.
void Clear()
Clear the message buffer.
uint64_t dropped_connections() const noexcept
Get the number of established connections that were dropped by this socket.
Nanomsg socket definition.
uint64_t accepted_connections() const noexcept
Get the number of connections successfully established that were accepted by this socket...
bool Open()
Open the socket.
bool Connect(const std::string &address)
Connect the socket to the remote endpoint.
bool IsOpened() const noexcept
Is socket opened?
const std::string & address() const noexcept
Get the socket address.
size_t TryReceive(Message &message)
Try to receive a message from the socket in non-blocking mode.
bool Disconnect()
Disconnect the socket from the endpoint.