1 ////////////////////////////////////////////////////////////
\r
3 // SFML - Simple and Fast Multimedia Library
\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
25 #ifndef SFML_PACKET_HPP
\r
26 #define SFML_PACKET_HPP
\r
28 ////////////////////////////////////////////////////////////
\r
30 ////////////////////////////////////////////////////////////
\r
31 #include <SFML/Config.hpp>
\r
38 ////////////////////////////////////////////////////////////
\r
39 /// Packet wraps data to send / to receive through the network
\r
40 ////////////////////////////////////////////////////////////
\r
41 class SFML_API Packet
\r
45 ////////////////////////////////////////////////////////////
\r
46 /// Default constructor
\r
48 ////////////////////////////////////////////////////////////
\r
51 ////////////////////////////////////////////////////////////
\r
52 /// Virtual destructor
\r
54 ////////////////////////////////////////////////////////////
\r
57 ////////////////////////////////////////////////////////////
\r
58 /// Append data to the end of the packet
\r
60 /// \param Data : Pointer to the bytes to append
\r
61 /// \param SizeInBytes : Number of bytes to append
\r
63 ////////////////////////////////////////////////////////////
\r
64 void Append(const void* Data, std::size_t SizeInBytes);
\r
66 ////////////////////////////////////////////////////////////
\r
67 /// Clear the packet data
\r
69 ////////////////////////////////////////////////////////////
\r
72 ////////////////////////////////////////////////////////////
\r
73 /// Get a pointer to the data contained in the packet
\r
74 /// Warning : the returned pointer may be invalid after you
\r
75 /// append data to the packet
\r
77 /// \return Pointer to the data
\r
79 ////////////////////////////////////////////////////////////
\r
80 const char* GetData() const;
\r
82 ////////////////////////////////////////////////////////////
\r
83 /// Get the size of the data contained in the packet
\r
85 /// \return Data size, in bytes
\r
87 ////////////////////////////////////////////////////////////
\r
88 std::size_t GetDataSize() const;
\r
90 ////////////////////////////////////////////////////////////
\r
91 /// Tell if the reading position has reached the end of the packet
\r
93 /// \return True if all data have been read into the packet
\r
95 ////////////////////////////////////////////////////////////
\r
96 bool EndOfPacket() const;
\r
98 ////////////////////////////////////////////////////////////
\r
99 /// Return the validity of packet
\r
101 /// \return True if last data extraction from packet was successful
\r
103 ////////////////////////////////////////////////////////////
\r
104 operator bool() const;
\r
106 ////////////////////////////////////////////////////////////
\r
107 /// Operator >> overloads to extract data from the packet
\r
109 ////////////////////////////////////////////////////////////
\r
110 Packet& operator >>(bool& Data);
\r
111 Packet& operator >>(Int8& Data);
\r
112 Packet& operator >>(Uint8& Data);
\r
113 Packet& operator >>(Int16& Data);
\r
114 Packet& operator >>(Uint16& Data);
\r
115 Packet& operator >>(Int32& Data);
\r
116 Packet& operator >>(Uint32& Data);
\r
117 Packet& operator >>(float& Data);
\r
118 Packet& operator >>(double& Data);
\r
119 Packet& operator >>(char* Data);
\r
120 Packet& operator >>(std::string& Data);
\r
121 Packet& operator >>(wchar_t* Data);
\r
122 Packet& operator >>(std::wstring& Data);
\r
124 ////////////////////////////////////////////////////////////
\r
125 /// Operator << overloads to put data into the packet
\r
127 ////////////////////////////////////////////////////////////
\r
128 Packet& operator <<(bool Data);
\r
129 Packet& operator <<(Int8 Data);
\r
130 Packet& operator <<(Uint8 Data);
\r
131 Packet& operator <<(Int16 Data);
\r
132 Packet& operator <<(Uint16 Data);
\r
133 Packet& operator <<(Int32 Data);
\r
134 Packet& operator <<(Uint32 Data);
\r
135 Packet& operator <<(float Data);
\r
136 Packet& operator <<(double Data);
\r
137 Packet& operator <<(const char* Data);
\r
138 Packet& operator <<(const std::string& Data);
\r
139 Packet& operator <<(const wchar_t* Data);
\r
140 Packet& operator <<(const std::wstring& Data);
\r
144 friend class SocketTCP;
\r
145 friend class SocketUDP;
\r
147 ////////////////////////////////////////////////////////////
\r
148 /// Check if the packet can extract a given size of bytes
\r
150 /// \param Size : Size to check
\r
152 /// \return True if Size bytes can be read from the packet's data
\r
154 ////////////////////////////////////////////////////////////
\r
155 bool CheckSize(std::size_t Size);
\r
157 ////////////////////////////////////////////////////////////
\r
158 /// Called before the packet is sent to the network
\r
160 /// \param DataSize : Variable to fill with the size of data to send
\r
162 /// \return Pointer to the array of bytes to send
\r
164 ////////////////////////////////////////////////////////////
\r
165 virtual const char* OnSend(std::size_t& DataSize);
\r
167 ////////////////////////////////////////////////////////////
\r
168 /// Called after the packet has been received from the network
\r
170 /// \param Data : Pointer to the array of received bytes
\r
171 /// \param DataSize : Size of the array of bytes
\r
173 ////////////////////////////////////////////////////////////
\r
174 virtual void OnReceive(const char* Data, std::size_t DataSize);
\r
176 ////////////////////////////////////////////////////////////
\r
178 ////////////////////////////////////////////////////////////
\r
179 std::vector<char> myData; ///< Data stored in the packet
\r
180 std::size_t myReadPos; ///< Current reading position in the packet
\r
181 bool myIsValid; ///< Reading state of the packet
\r
187 #endif // SFML_PACKET_HPP
\r