]> git.sesse.net Git - casparcg/blob - SFML-1.6/include/SFML/Network/Packet.hpp
(no commit message)
[casparcg] / SFML-1.6 / include / SFML / Network / Packet.hpp
1 ////////////////////////////////////////////////////////////\r
2 //\r
3 // SFML - Simple and Fast Multimedia Library\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)\r
5 //\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
8 //\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
12 //\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
17 //\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
20 //\r
21 // 3. This notice may not be removed or altered from any source distribution.\r
22 //\r
23 ////////////////////////////////////////////////////////////\r
24 \r
25 #ifndef SFML_PACKET_HPP\r
26 #define SFML_PACKET_HPP\r
27 \r
28 ////////////////////////////////////////////////////////////\r
29 // Headers\r
30 ////////////////////////////////////////////////////////////\r
31 #include <SFML/Config.hpp>\r
32 #include <string>\r
33 #include <vector>\r
34 \r
35 \r
36 namespace sf\r
37 {\r
38 ////////////////////////////////////////////////////////////\r
39 /// Packet wraps data to send / to receive through the network\r
40 ////////////////////////////////////////////////////////////\r
41 class SFML_API Packet\r
42 {\r
43 public :\r
44 \r
45     ////////////////////////////////////////////////////////////\r
46     /// Default constructor\r
47     ///\r
48     ////////////////////////////////////////////////////////////\r
49     Packet();\r
50 \r
51     ////////////////////////////////////////////////////////////\r
52     /// Virtual destructor\r
53     ///\r
54     ////////////////////////////////////////////////////////////\r
55     virtual ~Packet();\r
56 \r
57     ////////////////////////////////////////////////////////////\r
58     /// Append data to the end of the packet\r
59     ///\r
60     /// \param Data :        Pointer to the bytes to append\r
61     /// \param SizeInBytes : Number of bytes to append\r
62     ///\r
63     ////////////////////////////////////////////////////////////\r
64     void Append(const void* Data, std::size_t SizeInBytes);\r
65 \r
66     ////////////////////////////////////////////////////////////\r
67     /// Clear the packet data\r
68     ///\r
69     ////////////////////////////////////////////////////////////\r
70     void Clear();\r
71 \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
76     ///\r
77     /// \return Pointer to the data\r
78     ///\r
79     ////////////////////////////////////////////////////////////\r
80     const char* GetData() const;\r
81 \r
82     ////////////////////////////////////////////////////////////\r
83     /// Get the size of the data contained in the packet\r
84     ///\r
85     /// \return Data size, in bytes\r
86     ///\r
87     ////////////////////////////////////////////////////////////\r
88     std::size_t GetDataSize() const;\r
89 \r
90     ////////////////////////////////////////////////////////////\r
91     /// Tell if the reading position has reached the end of the packet\r
92     ///\r
93     /// \return True if all data have been read into the packet\r
94     ///\r
95     ////////////////////////////////////////////////////////////\r
96     bool EndOfPacket() const;\r
97 \r
98     ////////////////////////////////////////////////////////////\r
99     /// Return the validity of packet\r
100     ///\r
101     /// \return True if last data extraction from packet was successful\r
102     ///\r
103     ////////////////////////////////////////////////////////////\r
104     operator bool() const;\r
105 \r
106     ////////////////////////////////////////////////////////////\r
107     /// Operator >> overloads to extract data from the packet\r
108     ///\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
123 \r
124     ////////////////////////////////////////////////////////////\r
125     /// Operator << overloads to put data into the packet\r
126     ///\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
141 \r
142 private :\r
143 \r
144     friend class SocketTCP;\r
145     friend class SocketUDP;\r
146 \r
147     ////////////////////////////////////////////////////////////\r
148     /// Check if the packet can extract a given size of bytes\r
149     ///\r
150     /// \param Size : Size to check\r
151     ///\r
152     /// \return True if Size bytes can be read from the packet's data\r
153     ///\r
154     ////////////////////////////////////////////////////////////\r
155     bool CheckSize(std::size_t Size);\r
156 \r
157     ////////////////////////////////////////////////////////////\r
158     /// Called before the packet is sent to the network\r
159     ///\r
160     /// \param DataSize : Variable to fill with the size of data to send\r
161     ///\r
162     /// \return Pointer to the array of bytes to send\r
163     ///\r
164     ////////////////////////////////////////////////////////////\r
165     virtual const char* OnSend(std::size_t& DataSize);\r
166 \r
167     ////////////////////////////////////////////////////////////\r
168     /// Called after the packet has been received from the network\r
169     ///\r
170     /// \param Data :     Pointer to the array of received bytes\r
171     /// \param DataSize : Size of the array of bytes\r
172     ///\r
173     ////////////////////////////////////////////////////////////\r
174     virtual void OnReceive(const char* Data, std::size_t DataSize);\r
175 \r
176     ////////////////////////////////////////////////////////////\r
177     // Member data\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
182 };\r
183 \r
184 } // namespace sf\r
185 \r
186 \r
187 #endif // SFML_PACKET_HPP\r