Packet.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/Network/Packet.hpp>
00029 #include <SFML/Network/Sockets.hpp>
00030 
00031 
00032 namespace sf
00033 {
00037 Packet::Packet() :
00038 myReadPos(0)
00039 {
00040 
00041 }
00042 
00043 
00047 Packet::~Packet()
00048 {
00049 
00050 }
00051 
00052 
00056 void Packet::Append(const void* Data, std::size_t SizeInBytes)
00057 {
00058     const char* Bytes = static_cast<const char*>(Data);
00059     std::copy(Bytes, Bytes + SizeInBytes, std::back_inserter(myData));
00060 }
00061 
00062 
00066 void Packet::Clear()
00067 {
00068     myData.clear();
00069     myReadPos = 0;
00070 }
00071 
00072 
00078 const char* Packet::GetData() const
00079 {
00080     return &myData[0];
00081 }
00082 
00083 
00087 Uint32 Packet::GetDataSize() const
00088 {
00089     return static_cast<Uint32>(myData.size());
00090 }
00091 
00092 
00096 Packet& Packet::operator >>(Int8& Data)
00097 {
00098     Data = *reinterpret_cast<const Int8*>(GetData() + myReadPos);
00099     myReadPos += sizeof(Data);
00100     return *this;
00101 }
00102 Packet& Packet::operator >>(Uint8& Data)
00103 {
00104     Data = *reinterpret_cast<const Uint8*>(GetData() + myReadPos);
00105     myReadPos += sizeof(Data);
00106     return *this;
00107 }
00108 Packet& Packet::operator >>(Int16& Data)
00109 {
00110     Data = ntohs(*reinterpret_cast<const Int16*>(GetData() + myReadPos));
00111     myReadPos += sizeof(Data);
00112     return *this;
00113 }
00114 Packet& Packet::operator >>(Uint16& Data)
00115 {
00116     Data = ntohs(*reinterpret_cast<const Uint16*>(GetData() + myReadPos));
00117     myReadPos += sizeof(Data);
00118     return *this;
00119 }
00120 Packet& Packet::operator >>(Int32& Data)
00121 {
00122     Data = ntohl(*reinterpret_cast<const Int32*>(GetData() + myReadPos));
00123     myReadPos += sizeof(Data);
00124     return *this;
00125 }
00126 Packet& Packet::operator >>(Uint32& Data)
00127 {
00128     Data = ntohl(*reinterpret_cast<const Uint32*>(GetData() + myReadPos));
00129     myReadPos += sizeof(Data);
00130     return *this;
00131 }
00132 Packet& Packet::operator >>(float& Data)
00133 {
00134     Data = *reinterpret_cast<const float*>(GetData() + myReadPos);
00135     myReadPos += sizeof(Data);
00136     return *this;
00137 }
00138 Packet& Packet::operator >>(double& Data)
00139 {
00140     Data = *reinterpret_cast<const double*>(GetData() + myReadPos);
00141     myReadPos += sizeof(Data);
00142     return *this;
00143 }
00144 Packet& Packet::operator >>(char* Data)
00145 {
00146     // First extract string length
00147     Uint32 Length;
00148     *this >> Length;
00149 
00150     // Then extract characters
00151     strncpy(Data, GetData() + myReadPos, Length);
00152     Data[Length] = '\0';
00153 
00154     // Update reading position
00155     myReadPos += Length;
00156 
00157     return *this;
00158 }
00159 Packet& Packet::operator >>(std::string& Data)
00160 {
00161     // First extract string length
00162     Uint32 Length;
00163     *this >> Length;
00164 
00165     // Then extract characters
00166     Data.assign(GetData() + myReadPos, Length);
00167 
00168     // Update reading position
00169     myReadPos += Length;
00170 
00171     return *this;
00172 }
00173 
00174 
00178 Packet& Packet::operator <<(Int8 Data)
00179 {
00180     Append(&Data, sizeof(Data));
00181     return *this;
00182 }
00183 Packet& Packet::operator <<(Uint8 Data)
00184 {
00185     Append(&Data, sizeof(Data));
00186     return *this;
00187 }
00188 Packet& Packet::operator <<(Int16 Data)
00189 {
00190     Int16 ToWrite = htons(Data);
00191     Append(&ToWrite, sizeof(ToWrite));
00192     return *this;
00193 }
00194 Packet& Packet::operator <<(Uint16 Data)
00195 {
00196     Uint16 ToWrite = htons(Data);
00197     Append(&ToWrite, sizeof(ToWrite));
00198     return *this;
00199 }
00200 Packet& Packet::operator <<(Int32 Data)
00201 {
00202     Int32 ToWrite = htonl(Data);
00203     Append(&ToWrite, sizeof(ToWrite));
00204     return *this;
00205 }
00206 Packet& Packet::operator <<(Uint32 Data)
00207 {
00208     Uint32 ToWrite = htonl(Data);
00209     Append(&ToWrite, sizeof(ToWrite));
00210     return *this;
00211 }
00212 Packet& Packet::operator <<(float Data)
00213 {
00214     Append(&Data, sizeof(Data));
00215     return *this;
00216 }
00217 Packet& Packet::operator <<(double Data)
00218 {
00219     Append(&Data, sizeof(Data));
00220     return *this;
00221 }
00222 Packet& Packet::operator <<(const char* Data)
00223 {
00224     // First insert string length
00225     Uint32 Length = static_cast<Uint32>(strlen(Data));
00226     *this << Length;
00227 
00228     // Then insert characters
00229     Append(Data, Length);
00230 
00231     return *this;
00232 }
00233 Packet& Packet::operator <<(const std::string& Data)
00234 {
00235     // First insert string length
00236     Uint32 Length = static_cast<Uint32>(Data.size());
00237     *this << Length;
00238 
00239     // Then insert characters
00240     Append(Data.c_str(), Length);
00241 
00242     return *this;
00243 }
00244 
00245 
00249 void Packet::OnSend()
00250 {
00251     // Nothing by default
00252 }
00253 
00254 
00258 void Packet::OnReceive()
00259 {
00260     // Nothing by default
00261 }
00262 
00263 } // namespace sf