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-2024 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
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
#define SFML_NETWORK_API
Utility class to build blocks of data to transfer over the network.
Definition Packet.hpp:49
Packet & operator=(const Packet &)=default
Copy assignment.
Packet(Packet &&) noexcept=default
Move constructor.
Packet()=default
Default constructor.
virtual ~Packet()=default
Virtual destructor.
Packet(const Packet &)=default
Copy constructor.
Utility string class that automatically handles conversions between types and encodings.
Definition String.hpp:89
Specialized socket using the TCP protocol.
Definition TcpSocket.hpp:54
Specialized socket using the UDP protocol.
Definition UdpSocket.hpp:50