MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
packet.cpp
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 
41 #ifdef MEZZNETWORK
42 
43 #ifndef _networkpacket_cpp
44 #define _networkpacket_cpp
45 
46 #include "Network/packet.h"
47 #include <memory>
48 #include <cstring>
49 
50 #ifdef WINDOWS
51 #include <winsock2.h>
52 #else
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #endif
56 
57 namespace Mezzanine
58 {
59  namespace Network
60  {
61  Packet::Packet()
62  {
63  }
64 
65  Packet::~Packet()
66  {
67  Clear();
68  }
69 
70  bool Packet::VerifySize(const size_t& Bytes) const
71  {
72  return RawData.size() >= (ReadPos+Bytes);
73  }
74 
75  void Packet::Append(const void* Data, size_t Bytes)
76  {
77  if(!Data || (Bytes <= 0))
78  return;
79  size_t First = RawData.size();
80  RawData.resize(First + Bytes);
81  std::memcpy(&RawData[First],Data,Bytes);
82  }
83 
84  void Packet::Clear()
85  {
86  RawData.clear();
87  }
88 
89  size_t Packet::GetPacketSize() const
90  {
91  return RawData.size();
92  }
93 
94  bool Packet::EndOfPacket() const
95  {
96  return (ReadPos >= RawData.size());
97  }
98 
99  const std::vector<char>& Packet::GetRawData() const
100  {
101  return RawData;
102  }
103 
104  Packet& Packet::operator >>(bool& Data)
105  {
106  return *this;
107  }
108 
109  Packet& Packet::operator >>(Int8& Data)
110  {
111  if(!VerifySize(sizeof(Data)))
112  return *this;
113 
114  Data = *reinterpret_cast<const Int8*>(RawData[ReadPos]);
115  ReadPos+=sizeof(Data);
116  return *this;
117  }
118 
119  Packet& Packet::operator >>(UInt8& Data)
120  {
121  if(!VerifySize(sizeof(Data)))
122  return *this;
123 
124  Data = *reinterpret_cast<const UInt8*>(RawData[ReadPos]);
125  ReadPos+=sizeof(Data);
126  return *this;
127  }
128 
129  Packet& Packet::operator >>(Int16& Data)
130  {
131  if(!VerifySize(sizeof(Data)))
132  return *this;
133 
134  Data = *reinterpret_cast<const Int16*>(RawData[ReadPos]);
135  ReadPos+=sizeof(Data);
136  return *this;
137  }
138 
139  Packet& Packet::operator >>(UInt16& Data)
140  {
141  if(!VerifySize(sizeof(Data)))
142  return *this;
143 
144  Data = *reinterpret_cast<const UInt16*>(RawData[ReadPos]);
145  ReadPos+=sizeof(Data);
146  return *this;
147  }
148 
149  Packet& Packet::operator >>(Int32& Data)
150  {
151  if(!VerifySize(sizeof(Data)))
152  return *this;
153 
154  Data = *reinterpret_cast<const Int32*>(RawData[ReadPos]);
155  ReadPos+=sizeof(Data);
156  return *this;
157  }
158 
159  Packet& Packet::operator >>(UInt32& Data)
160  {
161  if(!VerifySize(sizeof(Data)))
162  return *this;
163 
164  Data = *reinterpret_cast<const UInt32*>(RawData[ReadPos]);
165  ReadPos+=sizeof(Data);
166  return *this;
167  }
168 
169  Packet& Packet::operator >>(float& Data)
170  {
171  if(!VerifySize(sizeof(Data)))
172  return *this;
173 
174  Data = *reinterpret_cast<const float*>(RawData[ReadPos]);
175  ReadPos+=sizeof(Data);
176  return *this;
177  }
178 
179  Packet& Packet::operator >>(double& Data)
180  {
181  if(!VerifySize(sizeof(Data)))
182  return *this;
183 
184  Data = *reinterpret_cast<const double*>(RawData[ReadPos]);
185  ReadPos+=sizeof(Data);
186  return *this;
187  }
188 
189  Packet& Packet::operator >>(char* Data)
190  {
191  UInt32 Length = 0;
192  *this >> Length;
193 
194  if(Length && VerifySize(sizeof(char)*Length))
195  {
196  Data = new char[Length+1];
197  std::memcpy(Data,&RawData[ReadPos],sizeof(char)*Length);
198  Data[Length] = '\0';
199  ReadPos+=(sizeof(char)*Length);
200  }
201 
202  return *this;
203  }
204 
205  Packet& Packet::operator >>(String& Data)
206  {
207  UInt32 Length = 0;
208  *this >> Length;
209 
210  if(Length && VerifySize(sizeof(char)*Length))
211  {
212  Data.clear();
213  for( Whole X = 0 ; X < Length ; ++X )
214  {
215  Data.push_back(static_cast<char>(RawData[ReadPos+X]));
216  }
217  ReadPos+=(sizeof(char)*Length);
218  }
219 
220  return *this;
221  }
222 
223  Packet& Packet::operator >>(wchar_t* Data)
224  {
225  UInt32 Length = 0;
226  *this >> Length;
227 
228  if(Length && VerifySize(sizeof(wchar_t)*Length))
229  {
230  Data = new wchar_t[Length+1];
231  std::memcpy(Data,&RawData[ReadPos],sizeof(wchar_t)*Length);
232  Data[Length] = '\0';
233  ReadPos+=(sizeof(wchar_t)*Length);
234  }
235 
236  return *this;
237  }
238 
239  Packet& Packet::operator >>(WideString& Data)
240  {
241  UInt32 Length = 0;
242  *this >> Length;
243 
244  if(Length && VerifySize(sizeof(wchar_t)*Length))
245  {
246  Data.clear();
247  wchar_t* Str = new wchar_t[Length+1];
248  std::memcpy(Str,&RawData[ReadPos],sizeof(wchar_t)*Length);
249  Str[Length+1] = wchar_t('\0');
250  Data.assign(Str);
251  ReadPos+=(sizeof(wchar_t)*Length);
252  }
253 
254  return *this;
255  }
256 
257  /*Packet& Packet::operator >>(const UTF16String& Data)
258  {
259  return *this;
260  }
261 
262  Packet& Packet::operator >>(const UTF32String& Data)
263  {
264  return *this;
265  }*/
266 
267  Packet& Packet::operator <<(bool Data)
268  {
269  *this << static_cast<UInt8>(Data);
270  return *this;
271  }
272 
273  Packet& Packet::operator <<(const Int8 Data)
274  {
275  Append(&Data,sizeof(Data));
276  return *this;
277  }
278 
279  Packet& Packet::operator <<(const UInt8 Data)
280  {
281  Append(&Data,sizeof(Data));
282  return *this;
283  }
284 
285  Packet& Packet::operator <<(Int16 Data)
286  {
287  Int16 Converted = htons(Data);
288  Append(&Converted,sizeof(Converted));
289  return *this;
290  }
291 
292  Packet& Packet::operator <<(UInt16 Data)
293  {
294  UInt16 Converted = htons(Data);
295  Append(&Converted,sizeof(Converted));
296  return *this;
297  }
298 
299  Packet& Packet::operator <<(Int32 Data)
300  {
301  Int32 Converted = htonl(Data);
302  Append(&Converted,sizeof(Converted));
303  return *this;
304  }
305 
306  Packet& Packet::operator <<(UInt32 Data)
307  {
308  UInt32 Converted = htonl(Data);
309  Append(&Converted,sizeof(Converted));
310  return *this;
311  }
312 
313  Packet& Packet::operator <<(const float Data)
314  {
315  Append(&Data,sizeof(Data));
316  return *this;
317  }
318 
319  Packet& Packet::operator <<(const double Data)
320  {
321  Append(&Data,sizeof(Data));
322  return *this;
323  }
324 
325  Packet& Packet::operator <<(const char* Data)
326  {
327  String Converted(Data);
328  *this << Converted;
329  return *this;
330  }
331 
332  Packet& Packet::operator <<(const String& Data)
333  {
334  UInt32 Length = Data.length();
335 
336  if(Length > 0)
337  {
338  *this << Length;
339  Append(Data.c_str(),sizeof(char)*Length);
340  }
341 
342  return *this;
343  }
344 
345  Packet& Packet::operator <<(const wchar_t* Data)
346  {
347  WideString Converted(Data);
348  *this << Converted;
349  return *this;
350  }
351 
352  Packet& Packet::operator <<(const WideString& Data)
353  {
354  UInt32 Length = Data.length();
355 
356  if(Length > 0)
357  {
358  *this << Length;
359  Append(Data.c_str(),sizeof(char)*Length);
360  }
361 
362  return *this;
363  }
364 
365  /*Packet& Packet::operator <<(const UTF16String& Data)
366  {
367  return *this;
368  }
369 
370  Packet& Packet::operator <<(const UTF32String& Data)
371  {
372  return *this;
373  }*/
374  }//Network
375 }//Mezzanine
376 
377 #endif
378 
379 #endif //MEZZNETWORK