Ocean
PackagedSocket.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_SOCKET_H
9 #define FACEBOOK_NETWORK_PACKAGED_SOCKET_H
10 
11 #include "ocean/network/Network.h"
12 #include "ocean/network/Socket.h"
13 
14 #include "ocean/base/Memory.h"
15 
16 #include "ocean/io/Bitstream.h"
17 
18 #include <queue>
19 
20 namespace Ocean
21 {
22 
23 namespace Network
24 {
25 
26 /**
27  * This class is the base class for all packaged sockets.
28  * @ingroup network
29  */
30 class OCEAN_NETWORK_EXPORT PackagedSocket : virtual public Socket
31 {
32  protected:
33 
34  /**
35  * This class implements a memory block.
36  */
37  class OCEAN_NETWORK_EXPORT MemoryBlock
38  {
39  public:
40 
41  /**
42  * Default constructor creating an empty memory block.
43  */
44  MemoryBlock() = default;
45 
46  /**
47  * Creates a new memory block.
48  * @param size The number of bytes the memory bock will contain, with range [1, infinity)
49  */
50  explicit MemoryBlock(const size_t size);
51 
52  /**
53  * Creates a new memory block and copied memory from source data.
54  * @param data The source data to be copied, must be valid
55  * @param size The number of bytes in the source data, with range [1, infinity)
56  */
57  MemoryBlock(const void* data, const size_t size);
58 
59  /**
60  * Returns the pointer to the start of the memory block.
61  * @return Pointer to the start of the memory
62  */
63  inline const void* data() const;
64 
65  /**
66  * Returns the pointer to the start of the memory block.
67  * @return Pointer to the start of the memory
68  */
69  inline void* data();
70 
71  /**
72  * Returns the pointer to the offset location within the memory block.
73  * @return Pointer to offset location within the memory
74  */
75  inline const void* offsetData() const;
76 
77  /**
78  * Returns the pointer to the offset location within the memory block.
79  * @return Pointer to offset location within the memory
80  */
81  inline void* offsetData();
82 
83  /**
84  * Returns the overall number of bytes in the memory block.
85  * @return The memory block's size in bytes (the capacity may still be larger), with range [0, infinity)
86  */
87  inline size_t size() const;
88 
89  /**
90  * Returns the number of remaining bytes.
91  * @return The memory's remaining bytes, with range [0, infinity)
92  */
93  inline size_t remainingBytes() const;
94 
95  /**
96  * Resets the memory offset location back to the start.
97  */
98  inline void resetOffset();
99 
100  /**
101  * Moves the offset within the memory block.
102  * @param delta The number of bytes the offset will be moved, with range [0, remainingBytes()]
103  */
104  inline void moveOffset(const size_t delta);
105 
106  /**
107  * Resizes the memory block.
108  * @param size The new size in bytes, with range [0, infinity)
109  */
110  void resize(const size_t size);
111 
112  /**
113  * Returns whether this memory block holds memory.
114  * @return True, if so
115  */
116  inline bool isValid() const;
117 
118  protected:
119 
120  /// The actual memory, can be larger than 'size_'.
122 
123  /// The actual size of the usable memory, in bytes.
124  size_t size_ = 0;
125 
126  /// The offset within the memory.
127  size_t offset_ = 0;
128  };
129 
130  /**
131  * Definition of a queue holding memory blocks.
132  */
133  typedef std::queue<MemoryBlock> MemoryBlockQueue;
134 
135  /**
136  * Definition of a package header.
137  */
139  {
140  public:
141 
142  /**
143  * Creates an invalid header.
144  */
145  PackageHeader() = default;
146 
147  /**
148  * Creates a valid header with specified size field.
149  * @param size The number of bytes the payload will have, with range [1, infinity)
150  */
151  explicit inline PackageHeader(const size_t size);
152 
153  /**
154  * Returns whether this header is valid.
155  * @return True, if so
156  */
157  inline bool isValid() const;
158 
159  /**
160  * Returns the number of bytes the payload will have.
161  * @return The payload size in bytes, with range [1, infinity)
162  */
163  inline size_t size() const;
164 
165  protected:
166 
167  /**
168  * Returns the unique tag for this header.
169  * @return The header's unique tag
170  */
171  static constexpr uint64_t oceanTag();
172 
173  protected:
174 
175  /// The header's tag.
176  uint64_t tag_ = 0ull;
177 
178  /// The header's version.
179  uint64_t version_ = 0ull;
180 
181  /// The number of bytes the payload will have.
182  uint64_t size_ = 0ull;
183  };
184 
185  static_assert(sizeof(PackageHeader) == sizeof(uint64_t) * 3, "Invalid header!");
186 
187  public:
188 
189  /**
190  * Definition of a message id.
191  */
192  typedef uint32_t MessageId;
193 
194  /**
195  * Returns an invalid message id.
196  * @return Invalid message id
197  */
198  static constexpr MessageId invalidMessageId();
199 
200  /**
201  * Returns the size of the package management header in bytes.
202  * @return The header's size in bytes
203  */
204  static constexpr size_t packageManagmentHeaderSize();
205 
206  /**
207  * Returns the maximal size of a packaged message.
208  * @return The maximal size of a packaged message in bytes
209  */
210  static constexpr size_t maximalPackagedMessageSize();
211 
212  protected:
213 
214  /**
215  * Creates a new packaged socket.
216  */
218 
219  /**
220  * Extracts a memory block from a queue with memory block.
221  * @param sourceQueue The queue from which the memory will be extracted
222  * @param targetMemory The target memory block to be filled, must be valid, must have remaining bytes
223  * @return True, if the target memory block has been filled and no byte is remaining anymore
224  */
225  static bool extractNextPackage(MemoryBlockQueue& sourceQueue, MemoryBlock& targetMemory);
226 };
227 
228 inline const void* PackagedSocket::MemoryBlock::data() const
229 {
230  ocean_assert(size_ < memory_.size());
231 
232  return memory_.constdata<uint8_t>();
233 }
234 
236 {
237  ocean_assert(size_ <= memory_.size());
238 
239  return memory_.data<uint8_t>();
240 }
241 
242 inline const void* PackagedSocket::MemoryBlock::offsetData() const
243 {
244  ocean_assert(size_ < memory_.size());
245  ocean_assert(offset_ < size_);
246 
247  return memory_.constdata<uint8_t>() + offset_;
248 }
249 
251 {
252  ocean_assert(size_ <= memory_.size());
253  ocean_assert(offset_ < size_);
254 
255  return memory_.data<uint8_t>() + offset_;
256 }
257 
259 {
260  return size_;
261 }
262 
264 {
265  offset_ = 0;
266 }
267 
268 inline void PackagedSocket::MemoryBlock::moveOffset(const size_t delta)
269 {
270  ocean_assert(delta <= remainingBytes());
271 
272  offset_ += delta;
273 
274  ocean_assert(offset_ <= size_);
275 }
276 
278 {
279  ocean_assert(offset_ <= size_);
280  return size_ - offset_;
281 }
282 
284 {
285  return bool(memory_);
286 }
287 
289  tag_(oceanTag()),
290  size_(size)
291 {
292  // nothing to do here
293 }
294 
296 {
297  return tag_ == oceanTag() && version_ == 0ull && size_ != 0ull;
298 }
299 
301 {
302  return size_t(size_);
303 }
304 
306 {
307  return IO::Tag::string2tag("_OCNPKG_");
308 }
309 
311 {
312  return MessageId(-1);
313 }
314 
316 {
317  return sizeof(MessageId) + sizeof(unsigned int) + sizeof(unsigned int) + sizeof(unsigned int) + sizeof(unsigned int);
318 }
319 
321 {
322  return 1024 * 1024 * 1024; // 1GB
323 }
324 
325 }
326 
327 }
328 
329 #endif // FACEBOOK_NETWORK_PACKAGED_SOCKET_H
static constexpr unsigned long long string2tag(const char tagString[8])
Converts a string with exactly eight characters to a unique tag value.
Definition: Bitstream.h:409
This class implements an object able to allocate memory.
Definition: base/Memory.h:22
size_t size() const
Returns the size of the memory in bytes.
Definition: base/Memory.h:386
const void * constdata() const
Returns the pointer to the read-only memory which is allocated by this object.
Definition: base/Memory.h:298
This class implements a memory block.
Definition: PackagedSocket.h:38
MemoryBlock(const size_t size)
Creates a new memory block.
void resetOffset()
Resets the memory offset location back to the start.
Definition: PackagedSocket.h:263
size_t size() const
Returns the overall number of bytes in the memory block.
Definition: PackagedSocket.h:258
MemoryBlock(const void *data, const size_t size)
Creates a new memory block and copied memory from source data.
Memory memory_
The actual memory, can be larger than 'size_'.
Definition: PackagedSocket.h:121
void moveOffset(const size_t delta)
Moves the offset within the memory block.
Definition: PackagedSocket.h:268
const void * offsetData() const
Returns the pointer to the offset location within the memory block.
Definition: PackagedSocket.h:242
MemoryBlock()=default
Default constructor creating an empty memory block.
void resize(const size_t size)
Resizes the memory block.
bool isValid() const
Returns whether this memory block holds memory.
Definition: PackagedSocket.h:283
size_t size_
The actual size of the usable memory, in bytes.
Definition: PackagedSocket.h:124
const void * data() const
Returns the pointer to the start of the memory block.
Definition: PackagedSocket.h:228
size_t remainingBytes() const
Returns the number of remaining bytes.
Definition: PackagedSocket.h:277
Definition of a package header.
Definition: PackagedSocket.h:139
size_t size() const
Returns the number of bytes the payload will have.
Definition: PackagedSocket.h:300
PackageHeader()=default
Creates an invalid header.
bool isValid() const
Returns whether this header is valid.
Definition: PackagedSocket.h:295
static constexpr uint64_t oceanTag()
Returns the unique tag for this header.
Definition: PackagedSocket.h:305
This class is the base class for all packaged sockets.
Definition: PackagedSocket.h:31
static bool extractNextPackage(MemoryBlockQueue &sourceQueue, MemoryBlock &targetMemory)
Extracts a memory block from a queue with memory block.
PackagedSocket()
Creates a new packaged socket.
static constexpr size_t packageManagmentHeaderSize()
Returns the size of the package management header in bytes.
Definition: PackagedSocket.h:315
uint32_t MessageId
Definition of a message id.
Definition: PackagedSocket.h:185
static constexpr MessageId invalidMessageId()
Returns an invalid message id.
Definition: PackagedSocket.h:310
static constexpr size_t maximalPackagedMessageSize()
Returns the maximal size of a packaged message.
Definition: PackagedSocket.h:320
std::queue< MemoryBlock > MemoryBlockQueue
Definition of a queue holding memory blocks.
Definition: PackagedSocket.h:133
This class is the base class for all sockets.
Definition: Socket.h:31
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15