Ocean
Loading...
Searching...
No Matches
PackagedConnectionlessClient.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_PACKAGED_CONNECTIONLESS_CLIENT_H
9#define FACEBOOK_NETWORK_PACKAGED_CONNECTIONLESS_CLIENT_H
10
15#include "ocean/network/Port.h"
16
17namespace Ocean
18{
19
20namespace Network
21{
22
23
24/**
25 * This class is the base class for all packaged connectionless clients.
26 * The client splits a message into as many datagrams as needed, each carrying the 20 byte package management header documented at PackagedSocket::packageManagmentHeaderSize():
27 * @code
28 * send(data, 3000), with a maximal package size of 1280 bytes and therefore 1260 payload bytes per datagram:
29 *
30 * datagram 0 [ messageId=7 | messageSize=3000 | dataStartPosition=0 | packageIndex=0 | totalPackages=3 ] 1260 payload bytes
31 * datagram 1 [ messageId=7 | messageSize=3000 | dataStartPosition=1260 | packageIndex=1 | totalPackages=3 ] 1260 payload bytes
32 * datagram 2 [ messageId=7 | messageSize=3000 | dataStartPosition=2520 | packageIndex=2 | totalPackages=3 ] 480 payload bytes
33 * @endcode
34 * The message id is incremented per message, so datagrams of different messages can be interleaved on the wire.
35 * @ingroup network
36 */
37class OCEAN_NETWORK_EXPORT PackagedConnectionlessClient :
38 virtual public Client,
39 virtual public PackagedSocket
40{
41 public:
42
43 /**
44 * Sends data to a specified recipient.
45 * @param address Recipient address
46 * @param port Recipient port
47 * @param data The data to send, can be nullptr if 'size == 0'
48 * @param size The size of the data to send in bytes, with range [0, infinity)
49 * @return SR_SUCCEEDED, if succeeded
50 */
51 SocketResult send(const Address4& address, const Port& port, const void* data, const size_t size);
52
53 /**
54 * Sends a message to a specified recipient.
55 * @param address Recipient address
56 * @param port Recipient port
57 * @param message The message to send, must be valid
58 * @return SR_SUCCEEDED, if succeeded
59 */
60 inline SocketResult send(const Address4& address, const Port& port, const std::string& message);
61
62 /**
63 * Returns the maximal size of a single package for this client.
64 * @return Maximal package size in bytes.
65 */
66 inline size_t maximalPackageSize() const;
67
68 protected:
69
70 /**
71 * Creates a new packaged connectionless client object.
72 */
74
75 /**
76 * Destructs a packaged connectionless client object.
77 */
79
80 protected:
81
82 /// Client message counter.
83 MessageId messageCounter_ = 0u;
84
85 /// Maximal package size of this connectionless socket (including the header).
86 size_t maximalPackageSize_ = 0;
87
88 /// Intermediate buffer storing individual parts of a large message.
90};
91
92inline PackagedConnectionlessClient::SocketResult PackagedConnectionlessClient::send(const Address4& address, const Port& port, const std::string& message)
93{
94 ocean_assert(strlen(message.c_str()) == message.length() && message.c_str()[message.length()] == '\0');
95 return send(address, port, message.c_str(), message.length() + 1);
96}
97
102
103}
104
105}
106
107#endif // FACEBOOK_NETWORK_PACKAGED_CONNECTIONLESS_CLIENT_H
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 packaged connectionless clients.
Definition PackagedConnectionlessClient.h:40
~PackagedConnectionlessClient() override
Destructs a packaged connectionless client object.
size_t maximalPackageSize() const
Returns the maximal size of a single package for this client.
Definition PackagedConnectionlessClient.h:98
PackagedConnectionlessClient()
Creates a new packaged connectionless client object.
Buffer clientPackageBuffer_
Intermediate buffer storing individual parts of a large message.
Definition PackagedConnectionlessClient.h:89
SocketResult send(const Address4 &address, const Port &port, const void *data, const size_t size)
Sends data to a specified recipient.
size_t maximalPackageSize_
Maximal package size of this connectionless socket (including the header).
Definition PackagedConnectionlessClient.h:86
This class is the base class for all packaged sockets.
Definition PackagedSocket.h:35
uint32_t MessageId
Definition of a message id.
Definition PackagedSocket.h:215
This class wraps a port number with 16 bits.
Definition Port.h:26
Address4 address() const
Returns the own address of this socket.
std::vector< uint8_t > Buffer
Definition of a vector holding 8 bit values.
Definition Socket.h:76
SocketResult
Definition of individual result values.
Definition Socket.h:62
Port port() const
Returns the own port of this socket.
The namespace covering the entire Ocean framework.
Definition Accessor.h:15