Ocean
ConnectionOrientedServer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef FACEBOOK_NETWORK_CONNECTION_ORIENTED_SERVER_H
9 #define FACEBOOK_NETWORK_CONNECTION_ORIENTED_SERVER_H
10 
11 #include "ocean/network/Network.h"
12 #include "ocean/network/Address4.h"
13 #include "ocean/network/Server.h"
14 
15 #include "ocean/base/Callback.h"
16 
17 namespace Ocean
18 {
19 
20 namespace Network
21 {
22 
23 /**
24  * This class is the base class for all connection oriented servers.
25  * @ingroup network
26  */
27 class OCEAN_NETWORK_EXPORT ConnectionOrientedServer : virtual public Server
28 {
29  public:
30 
31  /**
32  * Definition of a connection id.
33  */
34  typedef unsigned int ConnectionId;
35 
36  /**
37  * Returns an invalid connection id.
38  * @return Invalid connection id
39  */
40  static constexpr ConnectionId invalidConnectionId();
41 
42  /**
43  * Definition of a connection request callback function.<br>
44  * Parameter 0 provides the address of the requesting client.<br>
45  * Parameter 1 provides the port of the requesting client.<br>
46  * Parameter 2 provides the potential connection id if the connection is accepted.<br>
47  * Return True, to accept the connection.<br>
48  */
50 
51  /**
52  * Definition of a disconnect callback function.<br>
53  * Parameter 0 provides the id of the connection which has been disconnected
54  */
56 
57  /**
58  * Definition of a receive callback function.
59  * Parameter 0 provides the id of the connection from which the data is provided.<br>
60  * Parameter 1 provides the buffer which has been received, must be copied.<br>
61  * Parameter 2 provides the size of the buffer which has been received, in bytes
62  */
64 
65  protected:
66 
67  /**
68  * Definition of a connection object holding the socket and remote address and port.
69  */
71  {
72  public:
73 
74  /**
75  * Creates an empty connection object.
76  */
77  inline ConnectionObject() = default;
78 
79  /**
80  * Creates a new connection object.
81  */
82  inline ConnectionObject(const SocketId socketId, const Address4& address, const Port& port);
83 
84  /**
85  * Returns the socket id of this connection object.
86  * @return Socket id
87  */
88  inline SocketId id() const;
89 
90  /**
91  * Returns the remote address of this connection object.
92  * @return Remote address
93  */
94  inline const Address4& address() const;
95 
96  /**
97  * Returns the remote port of this connection object.
98  * @return Remote port
99  */
100  inline const Port& port() const;
101 
102  protected:
103 
104  /// Socket id of the connection.
105  SocketId socketId_ = invalidSocketId();
106 
107  /// Remote address of the connection.
109 
110  /// Remote port of the connection.
112  };
113 
114  /**
115  * Definition of a map mapping connection ids to sockets.
116  */
117  typedef std::unordered_map<ConnectionId, ConnectionObject> ConnectionMap;
118 
119  public:
120 
121  /**
122  * Sends data over a specified connection.
123  * @param connectionId The id of the connection which is used to send the data
124  * @param data The data to send, can be nullptr if 'size == 0'
125  * @param size The size of the data to send, in bytes, with range [0, infinity)
126  * @return SR_SUCCEEDED, if succeeded
127  */
128  virtual SocketResult send(const ConnectionId connectionId, const void* data, const size_t size);
129 
130  /**
131  * Sends a message over a specified connection.
132  * @param connectionId The id of the connection which is used to send the data
133  * @param message The message to send, must not be empty
134  * @return SR_SUCCEEDED, if succeeded
135  */
136  virtual SocketResult send(const ConnectionId connectionId, const std::string& message);
137 
138  /**
139  * Disconnects a specified connection.
140  * @param connectionId The id of the connection which will be disconnected
141  * @return True, if the connection was established
142  */
143  virtual bool disconnect(const ConnectionId connectionId) = 0;
144 
145  /**
146  * Returns the number of active connections of this server.
147  * @return Number of active connections
148  */
149  virtual size_t connections() const;
150 
151  /**
152  * Returns the remote address and port of a specified connection.
153  * @param connectionId The id of the connection from which the address and port will be returned
154  * @param address Returning remote address
155  * @param port Returning remote port
156  * @return True, if the connection exists
157  */
158  bool connectionProperties(const ConnectionId connectionId, Address4& address, Port& port);
159 
160  /**
161  * Sets the connection request callback function.
162  * @param callback The callback function to set
163  */
164  inline void setConnectionRequestCallback(const ConnectionRequestCallback& callback);
165 
166  /**
167  * Sets the disconnect callback function.
168  * @param callback The callback function to set
169  */
170  inline void setDisconnectCallback(const DisconnectCallback& callback);
171 
172  /**
173  * Sets the receive callback function.
174  * @param callback The callback function to set
175  */
176  inline void setReceiveCallback(const ReceiveCallback& callback);
177 
178  protected:
179 
180  /**
181  * Creates a new connection oriented server.
182  */
184 
185  /**
186  * Destructs a connection oriented server.
187  */
189 
190  /**
191  * The scheduler event function.
192  * Socket::onScheduler().
193  */
194  bool onScheduler() override;
195 
196  /**
197  * Internal event function to send data.
198  * @param connectionId The id of the connection
199  * @param data The data to send, must be valid
200  * @param size The size of the data in bytes, with range [1, infinity)
201  * @return The number of bytes which have been sent, with range [0, size]
202  */
203  virtual size_t onSend(const ConnectionId connectionId, const void* data, const size_t size);
204 
205  /**
206  * Internal event function for received data.
207  * @param connectionId The id of the connection
208  * @param data The data that has been received, must be valid
209  * @param size The size of the data in bytes, with range [1, infinity)
210  */
211  virtual void onReceived(const ConnectionId connectionId, const void* data, const size_t size);
212 
213  protected:
214 
215  /// Map holding all valid connections.
217 
218  /// Connection counter.
219  ConnectionId connectionCounter_ = ConnectionId(invalidConnectionId() + 1);
220 
221  /// Connection request callback function.
223 
224  /// Disconnect callback function.
226 
227  /// Receive callback function.
229 };
230 
232 {
233  return ConnectionId(-1);
234 }
235 
236 inline ConnectionOrientedServer::ConnectionObject::ConnectionObject(const SocketId socketId, const Address4& address, const Port& port) :
237  socketId_(socketId),
238  address_(address),
239  port_(port)
240 {
241  // nothing to do here
242 }
243 
245 {
246  return socketId_;
247 }
248 
250 {
251  return address_;
252 }
253 
255 {
256  return port_;
257 }
258 
260 {
261  const ScopedLock scopedLock(lock_);
262  connectionRequestCallback_ = callback;
263 }
264 
266 {
267  const ScopedLock scopedLock(lock_);
268  disconnectCallback_ = callback;
269 }
270 
272 {
273  const ScopedLock scopedLock(lock_);
274  receiveCallback_ = callback;
275 }
276 
277 }
278 
279 }
280 
281 #endif // FACEBOOK_NETWORK_CONNECTION_ORIENTED_SERVER_H
This class wraps an address number with 32 bits.
Definition: Address4.h:26
Definition of a connection object holding the socket and remote address and port.
Definition: ConnectionOrientedServer.h:71
const Address4 & address() const
Returns the remote address of this connection object.
Definition: ConnectionOrientedServer.h:249
Port port_
Remote port of the connection.
Definition: ConnectionOrientedServer.h:111
SocketId id() const
Returns the socket id of this connection object.
Definition: ConnectionOrientedServer.h:244
ConnectionObject()=default
Creates an empty connection object.
Address4 address_
Remote address of the connection.
Definition: ConnectionOrientedServer.h:108
const Port & port() const
Returns the remote port of this connection object.
Definition: ConnectionOrientedServer.h:254
This class is the base class for all connection oriented servers.
Definition: ConnectionOrientedServer.h:28
void setConnectionRequestCallback(const ConnectionRequestCallback &callback)
Sets the connection request callback function.
Definition: ConnectionOrientedServer.h:259
virtual SocketResult send(const ConnectionId connectionId, const std::string &message)
Sends a message over a specified connection.
void setDisconnectCallback(const DisconnectCallback &callback)
Sets the disconnect callback function.
Definition: ConnectionOrientedServer.h:265
virtual size_t connections() const
Returns the number of active connections of this server.
ConnectionRequestCallback connectionRequestCallback_
Connection request callback function.
Definition: ConnectionOrientedServer.h:222
Callback< void, const ConnectionId > DisconnectCallback
Definition of a disconnect callback function.
Definition: ConnectionOrientedServer.h:55
ReceiveCallback receiveCallback_
Receive callback function.
Definition: ConnectionOrientedServer.h:228
unsigned int ConnectionId
Definition of a connection id.
Definition: ConnectionOrientedServer.h:34
DisconnectCallback disconnectCallback_
Disconnect callback function.
Definition: ConnectionOrientedServer.h:225
virtual SocketResult send(const ConnectionId connectionId, const void *data, const size_t size)
Sends data over a specified connection.
bool onScheduler() override
The scheduler event function.
virtual size_t onSend(const ConnectionId connectionId, const void *data, const size_t size)
Internal event function to send data.
Callback< void, const ConnectionId, const void *, const size_t > ReceiveCallback
Definition of a receive callback function.
Definition: ConnectionOrientedServer.h:63
ConnectionOrientedServer()
Creates a new connection oriented server.
Callback< bool, const Address4 &, const Port &, const ConnectionId > ConnectionRequestCallback
Definition of a connection request callback function.
Definition: ConnectionOrientedServer.h:49
~ConnectionOrientedServer() override
Destructs a connection oriented server.
static constexpr ConnectionId invalidConnectionId()
Returns an invalid connection id.
Definition: ConnectionOrientedServer.h:231
virtual bool disconnect(const ConnectionId connectionId)=0
Disconnects a specified connection.
std::unordered_map< ConnectionId, ConnectionObject > ConnectionMap
Definition of a map mapping connection ids to sockets.
Definition: ConnectionOrientedServer.h:117
ConnectionMap connectionMap_
Map holding all valid connections.
Definition: ConnectionOrientedServer.h:216
bool connectionProperties(const ConnectionId connectionId, Address4 &address, Port &port)
Returns the remote address and port of a specified connection.
void setReceiveCallback(const ReceiveCallback &callback)
Sets the receive callback function.
Definition: ConnectionOrientedServer.h:271
virtual void onReceived(const ConnectionId connectionId, const void *data, const size_t size)
Internal event function for received data.
This class wraps a port number with 16 bits.
Definition: Port.h:26
This class is the base class for all server.
Definition: Server.h:26
SocketId socketId_
Socket id.
Definition: Socket.h:184
SOCKET SocketId
Definition of a socket id.
Definition: Socket.h:42
SocketResult
Definition of individual result values.
Definition: Socket.h:62
Lock lock_
Socket lock.
Definition: Socket.h:187
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15