Loading...
Searching...
No Matches
Packet.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2025 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
31
32#include <string>
33#include <vector>
34
35#include <cstddef>
36#include <cstdint>
37
38
39namespace sf
40{
41class String;
42
49{
50public:
57 Packet() = default;
58
63 virtual ~Packet() = default;
64
69 Packet(const Packet&) = default;
70
75 Packet& operator=(const Packet&) = default;
76
81 Packet(Packet&&) noexcept = default;
82
87 Packet& operator=(Packet&&) noexcept = default;
88
99 void append(const void* data, std::size_t sizeInBytes);
100
111 [[nodiscard]] std::size_t getReadPosition() const;
112
121 void clear();
122
136 [[nodiscard]] const void* getData() const;
137
149 [[nodiscard]] std::size_t getDataSize() const;
150
163 [[nodiscard]] bool endOfPacket() const;
164
199 explicit operator bool() const;
200
205 Packet& operator>>(bool& data);
206
210 Packet& operator>>(std::int8_t& data);
211
215 Packet& operator>>(std::uint8_t& data);
216
220 Packet& operator>>(std::int16_t& data);
221
225 Packet& operator>>(std::uint16_t& data);
226
230 Packet& operator>>(std::int32_t& data);
231
235 Packet& operator>>(std::uint32_t& data);
236
240 Packet& operator>>(std::int64_t& data);
241
245 Packet& operator>>(std::uint64_t& data);
246
250 Packet& operator>>(float& data);
251
255 Packet& operator>>(double& data);
256
260 Packet& operator>>(char* data);
261
265 Packet& operator>>(std::string& data);
266
270 Packet& operator>>(wchar_t* data);
271
275 Packet& operator>>(std::wstring& data);
276
280 Packet& operator>>(String& data);
281
286 Packet& operator<<(bool data);
287
291 Packet& operator<<(std::int8_t data);
292
296 Packet& operator<<(std::uint8_t data);
297
301 Packet& operator<<(std::int16_t data);
302
306 Packet& operator<<(std::uint16_t data);
307
311 Packet& operator<<(std::int32_t data);
312
316 Packet& operator<<(std::uint32_t data);
317
321 Packet& operator<<(std::int64_t data);
322
326 Packet& operator<<(std::uint64_t data);
327
331 Packet& operator<<(float data);
332
336 Packet& operator<<(double data);
337
341 Packet& operator<<(const char* data);
342
346 Packet& operator<<(const std::string& data);
347
351 Packet& operator<<(const wchar_t* data);
352
356 Packet& operator<<(const std::wstring& data);
357
361 Packet& operator<<(const String& data);
362
363protected:
364 friend class TcpSocket;
365 friend class UdpSocket;
366
385 virtual const void* onSend(std::size_t& size);
386
404 virtual void onReceive(const void* data, std::size_t size);
405
406private:
417 bool checkSize(std::size_t size);
418
420 // Member data
422 std::vector<std::byte> m_data;
423 std::size_t m_readPos{};
424 std::size_t m_sendPos{};
425 bool m_isValid{true};
426};
427
428} // namespace sf
429
430
#define SFML_NETWORK_API
Packet & operator=(const Packet &)=default
Copy assignment.
std::size_t getDataSize() const
Get the size of the data contained in the packet.
void clear()
Clear the packet.
Packet(Packet &&) noexcept=default
Move constructor.
std::size_t getReadPosition() const
Get the current reading position in the packet.
bool endOfPacket() const
Tell if the reading position has reached the end of the packet.
Packet()=default
Default constructor.
void append(const void *data, std::size_t sizeInBytes)
Append data to the end of the packet.
const void * getData() const
Get a pointer to the data contained in the packet.
friend class TcpSocket
Definition Packet.hpp:364
virtual ~Packet()=default
Virtual destructor.
virtual void onReceive(const void *data, std::size_t size)
Called after the packet is received over the network.
Packet(const Packet &)=default
Copy constructor.
friend class UdpSocket
Definition Packet.hpp:365
virtual const void * onSend(std::size_t &size)
Called before the packet is sent over the network.
Utility string class that automatically handles conversions between types and encodings.
Definition String.hpp:89