MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
packet.h
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_h
44 #define _networkpacket_h
45 
46 #include "datatypes.h"
47 
48 namespace Mezzanine
49 {
50  namespace Network
51  {
52  ///////////////////////////////////////////////////////////////////////////////
53  /// @class Packet
54  /// @headerfile networkpacket.h
55  /// @brief SFML style packet for encapsulating data for transmission across a network.
56  /// @details
57  ///////////////////////////////////////
58  class MEZZ_LIB Packet
59  {
60  protected:
61  std::vector<char> RawData;
62  size_t ReadPos;
63  /// @internal
64  /// @brief Verifies the remaining length of the packet for extraction.
65  bool VerifySize(const size_t& Bytes) const;
66  public:
67  /// @brief Class constructor.
68  Packet();
69  /// @brief Class destructor.
70  virtual ~Packet();
71  /// @brief Adds data to this packet.
72  /// @param Data Pointer to an array of raw data.
73  /// @param Bytes The amount of bytes contained in Data.
74  /// @warning This will blindly append data without checks as to what it is. Nor
75  /// will this check for endianess. Use with caution.
76  virtual void Append(const void* Data, size_t Bytes);
77  /// @brief Clears this packet of all data.
78  virtual void Clear();
79  /// @brief Gets the size of this packet.
80  /// @remarks This is also the size of the Data vector being stored by this class.
81  /// @return Returns a size_t indicating the size of this packet in bytes.
82  virtual size_t GetPacketSize() const;
83  /// @brief Gets whether or not the reading of this packet has reached the end.
84  /// @return Returns a bool indicating if the read position has reached the end.
85  virtual bool EndOfPacket() const;
86  /// @brief Gets the vector storing the raw data contained in this packet.
87  /// @return Returns a const reference to the vector storing the data of this packet.
88  virtual const std::vector<char>& GetRawData() const;
89  //streaming operators
90  /// @brief
91  /// @param Data
92  /// @return Returns a reference to this packet.
93  Packet& operator >>(bool& Data);
94  /// @brief
95  /// @param Data
96  /// @return Returns a reference to this packet.
97  Packet& operator >>(Int8& Data);
98  /// @brief
99  /// @param Data
100  /// @return Returns a reference to this packet.
101  Packet& operator >>(UInt8& Data);
102  /// @brief
103  /// @param Data
104  /// @return Returns a reference to this packet.
105  Packet& operator >>(Int16& Data);
106  /// @brief
107  /// @param Data
108  /// @return Returns a reference to this packet.
109  Packet& operator >>(UInt16& Data);
110  /// @brief
111  /// @param Data
112  /// @return Returns a reference to this packet.
113  Packet& operator >>(Int32& Data);
114  /// @brief
115  /// @param Data
116  /// @return Returns a reference to this packet.
117  Packet& operator >>(UInt32& Data);
118  /// @brief
119  /// @param Data
120  /// @return Returns a reference to this packet.
121  Packet& operator >>(float& Data);
122  /// @brief
123  /// @param Data
124  /// @return Returns a reference to this packet.
125  Packet& operator >>(double& Data);
126  /// @brief
127  /// @param Data
128  /// @return Returns a reference to this packet.
129  /// @warning To assure proper array size, this function will create a char array of it's own.
130  /// After this function is complete, it is up to the user to clean up the array when they are done.
131  Packet& operator >>(char* Data);
132  /// @brief
133  /// @param Data
134  /// @return Returns a reference to this packet.
135  Packet& operator >>(String& Data);
136  /// @brief
137  /// @param Data
138  /// @return Returns a reference to this packet.
139  /// @warning To assure proper array size, this function will create a char array of it's own.
140  /// After this function is complete, it is up to the user to clean up the array when they are done.
141  Packet& operator >>(wchar_t* Data);
142  /// @brief
143  /// @param Data
144  /// @return Returns a reference to this packet.
145  Packet& operator >>(WideString& Data);
146  /*/// @brief Streams a 16-bit unicode string out of the packet.
147  /// @param Data The string to be streamed to.
148  /// @return Returns a reference to this packet.
149  Packet& operator >>(const UTF16String& Data);
150  /// @brief Streams a 32-bit unicode string out of the packet.
151  /// @param Data The string to be streamed to.
152  /// @return Returns a reference to this packet.
153  Packet& operator >>(const UTF32String& Data);*/
154  /// @brief Streams a bool into the packet.
155  /// @param Data The bool to be streamed.
156  /// @return Returns a reference to this packet.
157  Packet& operator <<(const bool Data);
158  /// @brief Streams a 8-bit int into the packet.
159  /// @param Data The 8-bit int to be streamed.
160  /// @return Returns a reference to this packet.
161  Packet& operator <<(const Int8 Data);
162  /// @brief Streams a 8-bit unsigned int into the packet.
163  /// @param Data The 8-bit unsigned int to be streamed.
164  /// @return Returns a reference to this packet.
165  Packet& operator <<(const UInt8 Data);
166  /// @brief Streams a 16-bit int into the packet.
167  /// @param Data The 16-bit int to be streamed.
168  /// @return Returns a reference to this packet.
169  Packet& operator <<(Int16 Data);
170  /// @brief Streams a 16-bit unsigned int into the packet.
171  /// @param Data The 16-bit unsigned int to be streamed.
172  /// @return Returns a reference to this packet.
173  Packet& operator <<(UInt16 Data);
174  /// @brief Streams a 32-bit int into the packet.
175  /// @param Data The 32-bit int to be streamed.
176  /// @return Returns a reference to this packet.
177  Packet& operator <<(Int32 Data);
178  /// @brief Streams a 32-bit unsigned int into the packet.
179  /// @param Data The 32-bit unsigned int to be streamed.
180  /// @return Returns a reference to this packet.
181  Packet& operator <<(UInt32 Data);
182  /// @brief Streams a float into the packet.
183  /// @param Data The float to be streamed.
184  /// @return Returns a reference to this packet.
185  Packet& operator <<(const float Data);
186  /// @brief Streams a double into the packet.
187  /// @param Data The double-precision float to be streamed.
188  /// @return Returns a reference to this packet.
189  Packet& operator <<(const double Data);
190  /// @brief Streams a character array into the packet.
191  /// @param Data The char array to be streamed.
192  /// @return Returns a reference to this packet.
193  Packet& operator <<(const char* Data);
194  /// @brief Streams a String into the packet.
195  /// @param Data The string to be streamed.
196  /// @return Returns a reference to this packet.
197  Packet& operator <<(const String& Data);
198  /// @brief Streams a wide character array into the packet.
199  /// @param Data The char array to be streamed.
200  /// @return Returns a reference to this packet.
201  Packet& operator <<(const wchar_t* Data);
202  /// @brief Streams a wide String into the packet.
203  /// @param Data The string to be streamed.
204  /// @return Returns a reference to this packet.
205  Packet& operator <<(const WideString& Data);
206  /*/// @brief Streams a 16-bit unicode string into the packet.
207  /// @param Data The string to be streamed.
208  /// @return Returns a reference to this packet.
209  Packet& operator <<(const UTF16String& Data);
210  /// @brief Streams a 32-bit unicode string into the packet.
211  /// @param Data The string to be streamed.
212  /// @return Returns a reference to this packet.
213  Packet& operator <<(const UTF32String& Data);*/
214  };//Packet
215  }//Network
216 }//Mezzanine
217 
218 #endif
219 
220 #endif //MEZZNETWORK