11 #include "errors/fatal.h" 12 #include "threads/thread.h" 21 _socket(domain, protocol),
23 _threading(threading),
29 _thread = CppCommon::Thread::Start([
this]() { ServerLoop(); });
41 if (_thread.joinable())
48 assert(!
IsStarted() &&
"Nanomsg server is already started!");
54 if (_socket.
Bind(_address))
66 catch (CppCommon::SystemException& ex)
68 onError(ex.system_error(), ex.string());
75 assert(
IsStarted() &&
"Nanomsg server is not started!");
93 catch (CppCommon::SystemException& ex)
95 onError(ex.system_error(), ex.string());
111 void Server::ServerLoop()
137 CppCommon::Thread::Yield();
140 catch (CppCommon::SystemException& ex)
142 onError(ex.system_error(), ex.string());
144 catch (std::exception& ex)
150 fatality(
"Nanomsg server 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 onStarted()
Handle server started notification.
size_t Send(const void *buffer, size_t size)
Send data to the client.
size_t TrySend(const void *buffer, size_t size)
Try to send data to the socket in non-blocking mode.
bool Start()
Start the server.
size_t TryReceive(Message &message)
Try to receive a message from the client in non-blocking mode.
C++ Server project definitions.
bool Restart()
Restart the server.
bool Bind(const std::string &address)
Bind the socket to the local endpoint.
bool Stop()
Stop the server.
virtual void onThreadCleanup()
Cleanup thread handler.
virtual void onIdle()
Handle server idle notification.
virtual void onThreadInitialize()
Initialize thread handler.
Nanomsg server definition.
Server(Domain domain, Protocol protocol, const std::string &address, bool threading=true)
Initialize server with a given domain, protocol and endpoint address.
virtual void onReceived(Message &message)
Handle message received notification.
bool IsStarted() const noexcept
Is the server started?
Protocol
Nanomsg protocol.
size_t Receive(Message &message)
Receive a message from the socket in non-blocking mode.
size_t Send(const void *buffer, size_t size)
Send data to the socket.
size_t Receive(Message &message)
Receive a message from the client.
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 client in non-blocking mode.
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 onStopped()
Handle server stopped notification.