Packet.hpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
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/Config.hpp>
32#include <string>
33#include <vector>
34
35
36namespace sf
37{
41class SFML_API Packet
42{
43public :
44
49 Packet();
50
55 virtual ~Packet();
56
64 void Append(const void* Data, std::size_t SizeInBytes);
65
70 void Clear();
71
80 const char* GetData() const;
81
88 std::size_t GetDataSize() const;
89
96 bool EndOfPacket() const;
97
104 operator bool() const;
105
110 Packet& operator >>(bool& Data);
111 Packet& operator >>(Int8& Data);
112 Packet& operator >>(Uint8& Data);
113 Packet& operator >>(Int16& Data);
114 Packet& operator >>(Uint16& Data);
115 Packet& operator >>(Int32& Data);
116 Packet& operator >>(Uint32& Data);
117 Packet& operator >>(float& Data);
118 Packet& operator >>(double& Data);
119 Packet& operator >>(char* Data);
120 Packet& operator >>(std::string& Data);
121 Packet& operator >>(wchar_t* Data);
122 Packet& operator >>(std::wstring& Data);
123
128 Packet& operator <<(bool Data);
129 Packet& operator <<(Int8 Data);
130 Packet& operator <<(Uint8 Data);
131 Packet& operator <<(Int16 Data);
132 Packet& operator <<(Uint16 Data);
133 Packet& operator <<(Int32 Data);
134 Packet& operator <<(Uint32 Data);
135 Packet& operator <<(float Data);
136 Packet& operator <<(double Data);
137 Packet& operator <<(const char* Data);
138 Packet& operator <<(const std::string& Data);
139 Packet& operator <<(const wchar_t* Data);
140 Packet& operator <<(const std::wstring& Data);
141
142private :
143
144 friend class SocketTCP;
145 friend class SocketUDP;
146
155 bool CheckSize(std::size_t Size);
156
165 virtual const char* OnSend(std::size_t& DataSize);
166
174 virtual void OnReceive(const char* Data, std::size_t DataSize);
175
177 // Member data
179 std::vector<char> myData;
180 std::size_t myReadPos;
181 bool myIsValid;
182};
183
184} // namespace sf
185
186
187#endif // SFML_PACKET_HPP
const char * GetData() const
Get a pointer to the data contained in the packet Warning : the returned pointer may be invalid after...
Definition Packet.cpp:85
void Clear()
Clear the packet data.
Definition Packet.cpp:72
Packet()
Default constructor.
Definition Packet.cpp:38
bool EndOfPacket() const
Tell if the reading position has reached the end of the packet.
Definition Packet.cpp:103
void Append(const void *Data, std::size_t SizeInBytes)
Append data to the end of the packet.
Definition Packet.cpp:58
std::size_t GetDataSize() const
Get the size of the data contained in the packet.
Definition Packet.cpp:94