CppServer  1.0.0.0
C++ Server Library
socket.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_NANOMSG_SOCKET_H
10 #define CPPSERVER_NANOMSG_SOCKET_H
11 
12 #include "message.h"
13 
14 #include <string>
15 #include <tuple>
16 
17 namespace CppServer {
18 namespace Nanomsg {
19 
21 
29 class Socket
30 {
31 public:
33 
38  Socket(const Socket&) = delete;
39  Socket(Socket&&) noexcept = default;
40  ~Socket();
41 
42  Socket& operator=(const Socket&) = delete;
43  Socket& operator=(Socket&&) noexcept = default;
44 
46  Domain domain() const noexcept { return _domain; }
48  Protocol protocol() const noexcept { return _protocol; }
50  int socket() const noexcept { return _socket; }
52  int endpoint() const noexcept { return _endpoint; }
54  const std::string& address() const noexcept { return _address; }
55 
57  uint64_t established_connections() const noexcept;
59  uint64_t accepted_connections() const noexcept;
61  uint64_t dropped_connections() const noexcept;
63  uint64_t broken_connections() const noexcept;
65  uint64_t connect_errors() const noexcept;
67  uint64_t bind_errors() const noexcept;
69  uint64_t accept_errors() const noexcept;
71  uint64_t current_connections() const noexcept;
73  uint64_t messages_sent() const noexcept;
75  uint64_t messages_received() const noexcept;
77  uint64_t bytes_sent() const noexcept;
79  uint64_t bytes_received() const noexcept;
80 
82  bool IsOpened() const noexcept { return (_socket >= 0); }
84  bool IsConnected() const noexcept { return (_endpoint >= 0); }
85 
87 
90  bool Open();
92 
95  bool Close();
97 
100  bool Reopen();
101 
103 
110  bool SetSocketOption(int level, int option, const void* value, size_t size);
112 
119  bool GetSocketOption(int level, int option, void* value, size_t* size);
120 
122 
130  bool Bind(const std::string& address);
132 
140  bool Connect(const std::string& address);
142 
152  bool Link(const std::string& address);
154 
161  bool Disconnect();
162 
164 
169  size_t Send(const void* buffer, size_t size);
171 
175  size_t Send(const std::string& text) { return Send(text.data(), text.size()); }
177 
181  size_t Send(const Message& message) { return Send(message.buffer(), message.size()); }
182 
184 
189  size_t TrySend(const void* buffer, size_t size);
191 
195  size_t TrySend(const std::string& text) { return TrySend(text.data(), text.size()); }
197 
201  size_t TrySend(const Message& message) { return TrySend(message.buffer(), message.size()); }
202 
204 
208  size_t Receive(Message& message);
209 
211 
215  size_t TryReceive(Message& message);
216 
218 
222  std::tuple<size_t, bool> ReceiveSurvey(Message& message);
223 
225 
229  std::tuple<size_t, bool> TryReceiveSurvey(Message& message);
230 
232  static void Terminate();
233 
234 private:
235  Domain _domain;
236  Protocol _protocol;
237  int _socket;
238  int _endpoint;
239  std::string _address;
240 };
241 
242 } // namespace Nanomsg
243 } // namespace CppServer
244 
245 #endif // CPPSERVER_NANOMSG_SOCKET_H
uint64_t established_connections() const noexcept
Get the number of connections successfully established that were initiated from this socket...
Definition: socket.cpp:43
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...
Definition: socket.cpp:73
bool Reopen()
Reopen the socket.
Definition: socket.cpp:128
size_t TrySend(const Message &message)
Try to send a message to the socket in non-blocking mode.
Definition: socket.h:201
bool SetSocketOption(int level, int option, const void *value, size_t size)
Set the socket option.
Definition: socket.cpp:136
bool Close()
Close the socket.
Definition: socket.cpp:114
bool Link(const std::string &address)
Link the socket to the remote endpoint.
Definition: socket.cpp:210
static void Terminate()
Terminate all socket operations.
Definition: socket.cpp:409
uint64_t connect_errors() const noexcept
Get the number of errors encountered by this socket trying to connect to a remote peer...
Definition: socket.cpp:63
size_t TrySend(const void *buffer, size_t size)
Try to send data to the socket in non-blocking mode.
Definition: socket.cpp:271
Nanomsg message.
Definition: message.h:29
std::tuple< size_t, bool > ReceiveSurvey(Message &message)
Receive a respond to the survey from the socket in non-blocking mode.
Definition: socket.cpp:351
bool GetSocketOption(int level, int option, void *value, size_t *size)
Get the socket option.
Definition: socket.cpp:152
size_t size() const noexcept
Get the message size.
Definition: message.h:71
Socket & operator=(const Socket &)=delete
uint64_t broken_connections() const noexcept
Get the number of established connections that were closed by this socket, typically due to protocol ...
Definition: socket.cpp:58
Nanomsg message definition.
Domain domain() const noexcept
Get the socket domain.
Definition: socket.h:46
C++ Server project definitions.
Definition: asio.h:24
Socket(Domain domain, Protocol protocol)
Initialize and open socket with a given domain and protocol.
Definition: socket.cpp:19
std::tuple< size_t, bool > TryReceiveSurvey(Message &message)
Try to receive a respond to the survey from the socket in non-blocking mode.
Definition: socket.cpp:379
size_t Send(const std::string &text)
Send a text string to the socket.
Definition: socket.h:175
uint64_t bind_errors() const noexcept
Get the number of errors encountered by this socket trying to bind to a local address.
Definition: socket.cpp:68
bool Bind(const std::string &address)
Bind the socket to the local endpoint.
Definition: socket.cpp:168
uint64_t current_connections() const noexcept
Get the number of connections currently estabalished to this socket.
Definition: socket.cpp:78
uint64_t messages_sent() const noexcept
Get the number messages sent by this socket.
Definition: socket.cpp:83
size_t TrySend(const std::string &text)
Try to send a text string to the socket in non-blocking mode.
Definition: socket.h:195
int endpoint() const noexcept
Get the socket endpoint.
Definition: socket.h:52
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by this socket.
Definition: socket.cpp:93
Nanomsg socket.
Definition: socket.h:29
uint8_t * buffer() noexcept
Get the message buffer.
Definition: message.h:67
Protocol protocol() const noexcept
Get the socket protocol.
Definition: socket.h:48
bool IsConnected() const noexcept
Is socket connected?
Definition: socket.h:84
uint64_t messages_received() const noexcept
Get the number messages received by this socket.
Definition: socket.cpp:88
Protocol
Nanomsg protocol.
Definition: nanomsg.h:56
size_t Receive(Message &message)
Receive a message from the socket in non-blocking mode.
Definition: socket.cpp:297
uint64_t bytes_received() const noexcept
Get the number of bytes received by this socket.
Definition: socket.cpp:98
size_t Send(const void *buffer, size_t size)
Send data to the socket.
Definition: socket.cpp:247
Domain
Nanomsg domain.
Definition: nanomsg.h:41
uint64_t dropped_connections() const noexcept
Get the number of established connections that were dropped by this socket.
Definition: socket.cpp:53
uint64_t accepted_connections() const noexcept
Get the number of connections successfully established that were accepted by this socket...
Definition: socket.cpp:48
size_t Send(const Message &message)
Send a message to the socket.
Definition: socket.h:181
bool Open()
Open the socket.
Definition: socket.cpp:103
bool Connect(const std::string &address)
Connect the socket to the remote endpoint.
Definition: socket.cpp:189
bool IsOpened() const noexcept
Is socket opened?
Definition: socket.h:82
const std::string & address() const noexcept
Get the socket address.
Definition: socket.h:54
size_t TryReceive(Message &message)
Try to receive a message from the socket in non-blocking mode.
Definition: socket.cpp:323
int socket() const noexcept
Get the socket handler.
Definition: socket.h:50
bool Disconnect()
Disconnect the socket from the endpoint.
Definition: socket.cpp:226