Ocean
Loading...
Searching...
No Matches
ConnectionOrientedClient.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_CLIENT_H
9#define FACEBOOK_NETWORK_CONNECTION_ORIENTED_CLIENT_H
10
14
15#include "ocean/base/Callback.h"
16
17namespace Ocean
18{
19
20namespace Network
21{
22
23/**
24 * This class is the base class for all connection oriented clients.
25 * @ingroup network
26 */
27class OCEAN_NETWORK_EXPORT ConnectionOrientedClient : virtual public Client
28{
29 public:
30
31 /**
32 * Definition of a data receive callback function.
33 * The first parameter provides the received data, which must be copied
34 * The second parameter provides the size of the received data, in bytes
35 */
37
38 /**
39 * Definition of a disconnection callback function.
40 */
42
43 public:
44
45 /**
46 * Connects to a connection-oriented server.
47 * @param address Remote address of the server to connect
48 * @param port Remote port of the server to connect
49 * @param timeout The timeout in milliseconds, with range [0, infinity)
50 * @return True, if succeeded
51 */
52 virtual bool connect(const Address4& address, const Port& port, const unsigned int timeout = 1000u);
53
54 /**
55 * Reconnects the client by the usage of the most recent address and port.
56 * @param timeout The timeout in milliseconds, with range [0, infinity)
57 * @return True, if succeeded
58 */
59 virtual bool connect(const unsigned int timeout = 1000u);
60
61 /**
62 * Disconnects the client.
63 * @return True, if the client was connected
64 */
65 virtual bool disconnect();
66
67 /**
68 * Returns whether this client is connected.
69 * @return True, if so
70 */
71 virtual bool isConnected() const;
72
73 /**
74 * Sends data via the established connection.
75 * If the resource is busy SR_FAILED will be returned and the caller needs to send the data later<br>
76 * @param data The data to send, can be nullptr if 'size == 0'
77 * @param size The size of the data to send, in bytes, with range [0, infinity)
78 * @return SR_SUCCEEDED, if succeeded
79 */
80 virtual SocketResult send(const void* data, const size_t size);
81
82 /**
83 * Sets a message via the established connection.
84 * @param message The message to send, must be valid
85 * @return SR_SUCCEEDED, if succeeded
86 */
87 inline SocketResult send(const std::string& message);
88
89 /**
90 * Returns the (remote) receiver address.
91 * @return Receiver address
92 */
93 inline Address4 receiverAddress() const;
94
95 /**
96 * Returns the (remote) receiver port.
97 * @return Receiver port
98 */
99 inline Port receiverPort() const;
100
101 /**
102 * Sets the receive callback function.
103 * @param callback The callback function to set
104 */
105 inline void setReceiveCallback(const ReceiveCallback& callback);
106
107 /**
108 * Sets the disconnect callback function.
109 * @param callback The callback function to set
110 */
111 inline void setDisconnectCallback(const DisconnectCallback& callback);
112
113 protected:
114
115 /**
116 * Creates a new connection oriented client.
117 */
119
120 /**
121 * Destructs a connection oriented client.
122 */
124
125 /**
126 * The scheduler event function.
127 * Socket::onScheduler().
128 */
129 bool onScheduler() override;
130
131 /**
132 * Internal event function to send data.
133 * @param data The data to send, must be valid
134 * @param size The size of the data in bytes, with range [1, infinity)
135 * @return The number of bytes which have been sent, with range [0, size]
136 */
137 virtual size_t onSend(const void* data, const size_t size);
138
139 /**
140 * Internal event function for received data.
141 * @param data The data that has been received, must be valid
142 * @param size The size of the data in bytes, with range [1, infinity)
143 */
144 virtual void onReceived(const void* data, const size_t size);
145
146 protected:
147
148 /// Receiver address
150
151 /// Receiver port
153
154 /// Receive callback function.
156
157 /// Disconnect callback function.
159
160 /// Determines whether a connection is established.
161 bool isConnected_ = false;
162};
163
165{
166 ocean_assert(strlen(message.c_str()) == message.length() && message.c_str()[message.length()] == '\0');
167 return send(message.c_str(), message.length() + 1);
168}
169
171{
172 const ScopedLock scopedLock(lock_);
173 return receiverAddress_;
174}
175
177{
178 const ScopedLock scopedLock(lock_);
179 return receiverPort_;
180}
181
183{
184 const ScopedLock scopedLock(lock_);
185 receiveCallback_ = callback;
186}
187
189{
190 const ScopedLock scopedLock(lock_);
191 disconnectCallback_ = callback;
192}
193
194}
195
196}
197
198#endif // FACEBOOK_NETWORK_CONNECTION_ORIENTED_CLIENT_H
This class implements a container for callback functions.
Definition Callback.h:3456
This class wraps an address number with 32 bits.
Definition Address4.h:26
This class is the base class for all clients.
Definition Client.h:25
This class is the base class for all connection oriented clients.
Definition ConnectionOrientedClient.h:28
virtual SocketResult send(const void *data, const size_t size)
Sends data via the established connection.
Address4 receiverAddress() const
Returns the (remote) receiver address.
Definition ConnectionOrientedClient.h:170
~ConnectionOrientedClient() override
Destructs a connection oriented client.
Port receiverPort_
Receiver port.
Definition ConnectionOrientedClient.h:152
ConnectionOrientedClient()
Creates a new connection oriented client.
Port receiverPort() const
Returns the (remote) receiver port.
Definition ConnectionOrientedClient.h:176
bool onScheduler() override
The scheduler event function.
virtual bool isConnected() const
Returns whether this client is connected.
virtual bool disconnect()
Disconnects the client.
virtual void onReceived(const void *data, const size_t size)
Internal event function for received data.
Address4 receiverAddress_
Receiver address.
Definition ConnectionOrientedClient.h:149
virtual bool connect(const unsigned int timeout=1000u)
Reconnects the client by the usage of the most recent address and port.
DisconnectCallback disconnectCallback_
Disconnect callback function.
Definition ConnectionOrientedClient.h:158
ReceiveCallback receiveCallback_
Receive callback function.
Definition ConnectionOrientedClient.h:155
void setDisconnectCallback(const DisconnectCallback &callback)
Sets the disconnect callback function.
Definition ConnectionOrientedClient.h:188
void setReceiveCallback(const ReceiveCallback &callback)
Sets the receive callback function.
Definition ConnectionOrientedClient.h:182
Callback< void, const void *, const size_t > ReceiveCallback
Definition of a data receive callback function.
Definition ConnectionOrientedClient.h:36
Callback< void > DisconnectCallback
Definition of a disconnection callback function.
Definition ConnectionOrientedClient.h:41
virtual size_t onSend(const void *data, const size_t size)
Internal event function to send data.
virtual bool connect(const Address4 &address, const Port &port, const unsigned int timeout=1000u)
Connects to a connection-oriented server.
This class wraps a port number with 16 bits.
Definition Port.h:26
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