SFML 3.0.1
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
30#include <SFML/Network/Export.hpp>
31
32#include <string>
33#include <vector>
34
35#include <cstddef>
36#include <cstdint>
37
38
39 namespace sf
40{
41 class String;
42
48 class SFML_NETWORK_API Packet
49{
50 public:
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
203 explicit operator bool() const;
204
209 Packet& operator>>(bool& data);
210
214 Packet& operator>>(std::int8_t& data);
215
219 Packet& operator>>(std::uint8_t& data);
220
224 Packet& operator>>(std::int16_t& data);
225
229 Packet& operator>>(std::uint16_t& data);
230
234 Packet& operator>>(std::int32_t& data);
235
239 Packet& operator>>(std::uint32_t& data);
240
244 Packet& operator>>(std::int64_t& data);
245
249 Packet& operator>>(std::uint64_t& data);
250
254 Packet& operator>>(float& data);
255
259 Packet& operator>>(double& data);
260
264 Packet& operator>>(char* data);
265
269 Packet& operator>>(std::string& data);
270
274 Packet& operator>>(wchar_t* data);
275
279 Packet& operator>>(std::wstring& data);
280
284 Packet& operator>>(String& data);
285
290 Packet& operator<<(bool data);
291
295 Packet& operator<<(std::int8_t data);
296
300 Packet& operator<<(std::uint8_t data);
301
305 Packet& operator<<(std::int16_t data);
306
310 Packet& operator<<(std::uint16_t data);
311
315 Packet& operator<<(std::int32_t data);
316
320 Packet& operator<<(std::uint32_t data);
321
325 Packet& operator<<(std::int64_t data);
326
330 Packet& operator<<(std::uint64_t data);
331
335 Packet& operator<<(float data);
336
340 Packet& operator<<(double data);
341
345 Packet& operator<<(const char* data);
346
350 Packet& operator<<(const std::string& data);
351
355 Packet& operator<<(const wchar_t* data);
356
360 Packet& operator<<(const std::wstring& data);
361
365 Packet& operator<<(const String& data);
366
367protected:
368 friend class TcpSocket;
369 friend class UdpSocket;
370
389 virtual const void* onSend(std::size_t& size);
390
408 virtual void onReceive(const void* data, std::size_t size);
409
410private:
421 bool checkSize(std::size_t size);
422
424 // Member data
426 std::vector<std::byte> m_data;
427 std::size_t m_readPos{};
428 std::size_t m_sendPos{};
429 bool m_isValid{true};
430};
431
432} // namespace sf
433
434
Export.hpp
SFML_NETWORK_API
#define SFML_NETWORK_API
Definition Network/Export.hpp:42
sf::Packet::operator=
Packet & operator=(const Packet &)=default
Copy assignment.
sf::Packet::getDataSize
std::size_t getDataSize() const
Get the size of the data contained in the packet.
sf::Packet::clear
void clear()
Clear the packet.
sf::Packet::Packet
Packet(Packet &&) noexcept=default
Move constructor.
sf::Packet::getReadPosition
std::size_t getReadPosition() const
Get the current reading position in the packet.
sf::Packet::endOfPacket
bool endOfPacket() const
Tell if the reading position has reached the end of the packet.
sf::Packet::Packet
Packet()=default
Default constructor.
sf::Packet::append
void append(const void *data, std::size_t sizeInBytes)
Append data to the end of the packet.
sf::Packet::getData
const void * getData() const
Get a pointer to the data contained in the packet.
sf::Packet::TcpSocket
friend class TcpSocket
Definition Packet.hpp:368
sf::Packet::~Packet
virtual ~Packet()=default
Virtual destructor.
sf::Packet::onReceive
virtual void onReceive(const void *data, std::size_t size)
Called after the packet is received over the network.
sf::Packet::Packet
Packet(const Packet &)=default
Copy constructor.
sf::Packet::UdpSocket
friend class UdpSocket
Definition Packet.hpp:369
sf::Packet::onSend
virtual const void * onSend(std::size_t &size)
Called before the packet is sent over the network.
sf::String
Utility string class that automatically handles conversions between types and encodings.
Definition String.hpp:89
sf
Definition AudioResource.hpp:36