CppServer  1.0.0.0
C++ Server Library
client.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_NANOMSG_CLIENT_H
10 #define CPPSERVER_NANOMSG_CLIENT_H
11 
12 #include "socket.h"
13 
14 #include "threads/thread.h"
15 
16 #include <atomic>
17 #include <thread>
18 
19 namespace CppServer {
20 namespace Nanomsg {
21 
23 
29 class Client
30 {
31 public:
33 
39  explicit Client(Domain domain, Protocol protocol, const std::string& address, bool threading = true);
40  Client(const Client&) = delete;
41  Client(Client&&) = default;
42  virtual ~Client();
43 
44  Client& operator=(const Client&) = delete;
45  Client& operator=(Client&&) = default;
46 
48  Socket& socket() noexcept { return _socket; }
49 
51  bool IsConnected() const noexcept { return _socket.IsOpened() && _socket.IsConnected() && _connected; }
52 
54 
57  bool Connect();
59 
62  bool Disconnect();
64 
67  bool Reconnect();
68 
70 
75  size_t Send(const void* buffer, size_t size);
77 
81  size_t Send(const std::string& text) { return Send(text.data(), text.size()); }
83 
87  size_t Send(const Message& message) { return Send(message.buffer(), message.size()); }
88 
90 
95  size_t TrySend(const void* buffer, size_t size);
97 
101  size_t TrySend(const std::string& text) { return TrySend(text.data(), text.size()); }
103 
107  size_t TrySend(const Message& message) { return TrySend(message.buffer(), message.size()); }
108 
110 
114  size_t Receive(Message& message);
115 
117 
121  size_t TryReceive(Message& message);
122 
123 protected:
125 
128  virtual void onThreadInitialize() {}
130 
133  virtual void onThreadCleanup() {}
134 
136  virtual void onConnected() {}
138  virtual void onDisconnected() {}
139 
141  virtual void onIdle() { CppCommon::Thread::Yield(); }
142 
144 
147  virtual void onReceived(Message& message) {}
148 
150 
154  virtual void onError(int error, const std::string& message) {}
155 
156 private:
157  // Nanomsg endpoint address
158  std::string _address;
159  // Nanomsg socket
160  Socket _socket;
161  std::atomic<bool> _connected;
162  // Nanomsg client thread
163  bool _threading;
164  std::thread _thread;
165  std::atomic<bool> _joining;
166  std::atomic<bool> _receiving;
167 
169  void ClientLoop();
170 };
171 
172 } // namespace Nanomsg
173 } // namespace CppServer
174 
175 #endif // CPPSERVER_NANOMSG_CLIENT_H
size_t Send(const std::string &text)
Send a text string to the server.
Definition: client.h:81
virtual void onDisconnected()
Handle client disconnected notification.
Definition: client.h:138
size_t Receive(Message &message)
Receive a message from the server.
Definition: client.cpp:189
virtual void onError(int error, const std::string &message)
Handle error notification.
Definition: client.h:154
size_t Send(const Message &message)
Send a message to the server.
Definition: client.h:87
Nanomsg message.
Definition: message.h:29
size_t size() const noexcept
Get the message size.
Definition: message.h:71
bool Reconnect()
Reconnect the client.
Definition: client.cpp:100
C++ Server project definitions.
Definition: asio.h:24
bool Disconnect()
Disconnect the client.
Definition: client.cpp:73
virtual void onConnected()
Handle client connected notification.
Definition: client.h:136
virtual void onIdle()
Handle client idle notification.
Definition: client.h:141
bool IsConnected() const noexcept
Is the client connected?
Definition: client.h:51
size_t Send(const void *buffer, size_t size)
Send data to the server.
Definition: client.cpp:157
size_t TrySend(const void *buffer, size_t size)
Try to send data to the server in non-blocking mode.
Definition: client.cpp:173
Nanomsg socket.
Definition: socket.h:29
size_t TrySend(const std::string &text)
Try to send a text string to the server in non-blocking mode.
Definition: client.h:101
size_t TryReceive(Message &message)
Try to receive a message from the server in non-blocking mode.
Definition: client.cpp:211
uint8_t * buffer() noexcept
Get the message buffer.
Definition: message.h:67
bool IsConnected() const noexcept
Is socket connected?
Definition: socket.h:84
Client(Domain domain, Protocol protocol, const std::string &address, bool threading=true)
Initialize client with a given domain, protocol and endpoint address.
Definition: client.cpp:19
Client & operator=(const Client &)=delete
Protocol
Nanomsg protocol.
Definition: nanomsg.h:56
bool Connect()
Connect the client.
Definition: client.cpp:46
size_t TrySend(const Message &message)
Try to send a message to the server in non-blocking mode.
Definition: client.h:107
Nanomsg client.
Definition: client.h:29
Domain
Nanomsg domain.
Definition: nanomsg.h:41
virtual void onReceived(Message &message)
Handle message received notification.
Definition: client.h:147
virtual void onThreadInitialize()
Initialize thread handler.
Definition: client.h:128
Nanomsg socket definition.
bool IsOpened() const noexcept
Is socket opened?
Definition: socket.h:82
virtual void onThreadCleanup()
Cleanup thread handler.
Definition: client.h:133
Socket & socket() noexcept
Get the Nanomsg socket.
Definition: client.h:48