11 #include "errors/fatal.h" 12 #include "threads/thread.h" 21 _socket(domain, protocol),
23 _threading(threading),
29 _thread = CppCommon::Thread::Start([
this]() { ClientLoop(); });
41 if (_thread.joinable())
48 assert(!
IsConnected() &&
"Nanomsg client is already connected!");
66 catch (CppCommon::SystemException& ex)
68 onError(ex.system_error(), ex.string());
75 assert(
IsConnected() &&
"Nanomsg client is not connected!");
93 catch (CppCommon::SystemException& ex)
95 onError(ex.system_error(), ex.string());
111 void Client::ClientLoop()
137 CppCommon::Thread::Yield();
140 catch (CppCommon::SystemException& ex)
142 onError(ex.system_error(), ex.string());
144 catch (std::exception& ex)
150 fatality(
"Nanomsg client thread terminated!");
164 return _socket.
Send(buffer, size);
166 catch (CppCommon::SystemException& ex)
168 onError(ex.system_error(), ex.string());
180 return _socket.
TrySend(buffer, size);
182 catch (CppCommon::SystemException& ex)
184 onError(ex.system_error(), ex.string());
196 size_t result = _socket.
Receive(message);
204 catch (CppCommon::SystemException& ex)
206 onError(ex.system_error(), ex.string());
226 catch (CppCommon::SystemException& ex)
228 onError(ex.system_error(), ex.string());
bool Reopen()
Reopen the socket.
virtual void onDisconnected()
Handle client disconnected notification.
size_t Receive(Message &message)
Receive a message from the server.
virtual void onError(int error, const std::string &message)
Handle error notification.
size_t TrySend(const void *buffer, size_t size)
Try to send data to the socket in non-blocking mode.
bool Reconnect()
Reconnect the client.
C++ Server project definitions.
bool Disconnect()
Disconnect the client.
virtual void onConnected()
Handle client connected notification.
virtual void onIdle()
Handle client idle notification.
bool IsConnected() const noexcept
Is the client connected?
size_t Send(const void *buffer, size_t size)
Send data to the server.
size_t TrySend(const void *buffer, size_t size)
Try to send data to the server in non-blocking mode.
size_t TryReceive(Message &message)
Try to receive a message from the server in non-blocking mode.
Client(Domain domain, Protocol protocol, const std::string &address, bool threading=true)
Initialize client with a given domain, protocol and endpoint address.
Protocol
Nanomsg protocol.
size_t Receive(Message &message)
Receive a message from the socket in non-blocking mode.
bool Connect()
Connect the client.
size_t Send(const void *buffer, size_t size)
Send data to the socket.
Nanomsg client definition.
virtual void onReceived(Message &message)
Handle message received notification.
virtual void onThreadInitialize()
Initialize thread handler.
bool Connect(const std::string &address)
Connect the socket to the remote endpoint.
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.
virtual void onThreadCleanup()
Cleanup thread handler.