El Octavio  1.0
This is a video game about adventures.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Packet.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2018 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
32#include <string>
33#include <vector>
34
35
36namespace sf
37{
38class String;
39class TcpSocket;
40class UdpSocket;
41
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
77 void append(const void* data, std::size_t sizeInBytes);
78
87 void clear();
88
102 const void* getData() const;
103
115 std::size_t getDataSize() const;
116
129 bool endOfPacket() const;
130
131public:
132
171 operator BoolType() const;
172
177 Packet& operator >>(bool& data);
178
183
188
193
198
203
208
213
218
222 Packet& operator >>(float& data);
223
227 Packet& operator >>(double& data);
228
232 Packet& operator >>(char* data);
233
237 Packet& operator >>(std::string& data);
238
242 Packet& operator >>(wchar_t* data);
243
247 Packet& operator >>(std::wstring& data);
248
253
258 Packet& operator <<(bool data);
259
264
269
274
279
284
289
294
299
303 Packet& operator <<(float data);
304
308 Packet& operator <<(double data);
309
313 Packet& operator <<(const char* data);
314
318 Packet& operator <<(const std::string& data);
319
323 Packet& operator <<(const wchar_t* data);
324
328 Packet& operator <<(const std::wstring& data);
329
333 Packet& operator <<(const String& data);
334
335protected:
336
337 friend class TcpSocket;
338 friend class UdpSocket;
339
358 virtual const void* onSend(std::size_t& size);
359
377 virtual void onReceive(const void* data, std::size_t size);
378
379private:
380
385 bool operator ==(const Packet& right) const;
386 bool operator !=(const Packet& right) const;
387
398 bool checkSize(std::size_t size);
399
401 // Member data
403 std::vector<char> m_data;
404 std::size_t m_readPos;
405 std::size_t m_sendPos;
406 bool m_isValid;
407};
408
409} // namespace sf
410
411
412#endif // SFML_PACKET_HPP
413
414
#define SFML_NETWORK_API
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.
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
signed long long Int64
Definition: Config.hpp:229
unsigned short Uint16
Definition: Config.hpp:218
signed short Int16
Definition: Config.hpp:217
unsigned char Uint8
Definition: Config.hpp:214
signed char Int8
Definition: Config.hpp:213
SFML_NETWORK_API std::istream & operator>>(std::istream &stream, IpAddress &address)
Overload of >> operator to extract an IP address from an input stream.
unsigned int Uint32
Definition: Config.hpp:222
signed int Int32
Definition: Config.hpp:221
SFML_NETWORK_API bool operator==(const IpAddress &left, const IpAddress &right)
Overload of == operator to compare two IP addresses.
SFML_NETWORK_API bool operator!=(const IpAddress &left, const IpAddress &right)
Overload of != operator to compare two IP addresses.
unsigned long long Uint64
Definition: Config.hpp:230
SFML_NETWORK_API std::ostream & operator<<(std::ostream &stream, const IpAddress &address)
Overload of << operator to print an IP address to an output stream.