Documentation of SFML 2.6.0

Warning: this page refers to an old version of SFML. Click here to switch to the latest version.
Loading...
Searching...
No Matches
Packet.hpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2023 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#ifndef SFML_PACKET_HPP
26#define SFML_PACKET_HPP
27
29// Headers
31#include <SFML/Network/Export.hpp>
32#include <string>
33#include <vector>
34
35
36namespace sf
37{
38class String;
39class TcpSocket;
40class UdpSocket;
41
47class SFML_NETWORK_API Packet
48{
49 // A bool-like type that cannot be converted to integer or pointer types
50 typedef bool (Packet::*BoolType)(std::size_t);
51
52public:
53
61
66 virtual ~Packet();
67
78 void append(const void* data, std::size_t sizeInBytes);
79
90 std::size_t getReadPosition() const;
91
100 void clear();
101
115 const void* getData() const;
116
128 std::size_t getDataSize() const;
129
142 bool endOfPacket() const;
143
144public:
145
184 operator BoolType() const;
185
190 Packet& operator >>(bool& data);
191
195 Packet& operator >>(Int8& data);
196
200 Packet& operator >>(Uint8& data);
201
205 Packet& operator >>(Int16& data);
206
210 Packet& operator >>(Uint16& data);
211
215 Packet& operator >>(Int32& data);
216
220 Packet& operator >>(Uint32& data);
221
225 Packet& operator >>(Int64& data);
226
230 Packet& operator >>(Uint64& data);
231
235 Packet& operator >>(float& data);
236
240 Packet& operator >>(double& data);
241
245 Packet& operator >>(char* data);
246
250 Packet& operator >>(std::string& data);
251
255 Packet& operator >>(wchar_t* data);
256
260 Packet& operator >>(std::wstring& data);
261
265 Packet& operator >>(String& data);
266
271 Packet& operator <<(bool data);
272
276 Packet& operator <<(Int8 data);
277
281 Packet& operator <<(Uint8 data);
282
286 Packet& operator <<(Int16 data);
287
291 Packet& operator <<(Uint16 data);
292
296 Packet& operator <<(Int32 data);
297
301 Packet& operator <<(Uint32 data);
302
306 Packet& operator <<(Int64 data);
307
311 Packet& operator <<(Uint64 data);
312
316 Packet& operator <<(float data);
317
321 Packet& operator <<(double data);
322
326 Packet& operator <<(const char* data);
327
331 Packet& operator <<(const std::string& data);
332
336 Packet& operator <<(const wchar_t* data);
337
341 Packet& operator <<(const std::wstring& data);
342
346 Packet& operator <<(const String& data);
347
348protected:
349
350 friend class TcpSocket;
351 friend class UdpSocket;
352
371 virtual const void* onSend(std::size_t& size);
372
390 virtual void onReceive(const void* data, std::size_t size);
391
392private:
393
398 bool operator ==(const Packet& right) const;
399 bool operator !=(const Packet& right) const;
400
411 bool checkSize(std::size_t size);
412
414 // Member data
416 std::vector<char> m_data;
417 std::size_t m_readPos;
418 std::size_t m_sendPos;
419 bool m_isValid;
420};
421
422} // namespace sf
423
424
425#endif // SFML_PACKET_HPP
426
427
Utility class to build blocks of data to transfer over the network.
Definition: Packet.hpp:48
std::size_t getDataSize() const
Get the size of the data contained in the packet.
void clear()
Clear the packet.
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 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.
virtual void onReceive(const void *data, std::size_t size)
Called after the packet is received over the network.
virtual ~Packet()
Virtual destructor.
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:46
Specialized socket using the TCP protocol.
Definition: TcpSocket.hpp:47
Specialized socket using the UDP protocol.
Definition: UdpSocket.hpp:46