D:/Programmation/Cpp/SFML/src/SFML/Network/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 
00035 sfPacket::sfPacket() :
00036 myReadPos(0)
00037 {
00038 
00039 }
00040 
00041 
00045 sfPacket::~sfPacket()
00046 {
00047 
00048 }
00049 
00050 
00054 void sfPacket::Append(const void* Data, std::size_t SizeInBytes)
00055 {
00056     const char* Bytes = static_cast<const char*>(Data);
00057     std::copy(Bytes, Bytes + SizeInBytes, std::back_inserter(myData));
00058 }
00059 
00060 
00064 void sfPacket::Clear()
00065 {
00066     myData.clear();
00067     myReadPos = 0;
00068 }
00069 
00070 
00076 const char* sfPacket::GetData() const
00077 {
00078     return &myData[0];
00079 }
00080 
00081 
00085 sfUint32 sfPacket::GetDataSize() const
00086 {
00087     return static_cast<sfUint32>(myData.size());
00088 }
00089 
00090 
00094 sfPacket& sfPacket::operator >>(sfInt8& Data)
00095 {
00096     Data = *reinterpret_cast<const sfInt8*>(GetData() + myReadPos);
00097     myReadPos += sizeof(Data);
00098     return *this;
00099 }
00100 sfPacket& sfPacket::operator >>(sfUint8& Data)
00101 {
00102     Data = *reinterpret_cast<const sfUint8*>(GetData() + myReadPos);
00103     myReadPos += sizeof(Data);
00104     return *this;
00105 }
00106 sfPacket& sfPacket::operator >>(sfInt16& Data)
00107 {
00108     Data = ntohs(*reinterpret_cast<const sfInt16*>(GetData() + myReadPos));
00109     myReadPos += sizeof(Data);
00110     return *this;
00111 }
00112 sfPacket& sfPacket::operator >>(sfUint16& Data)
00113 {
00114     Data = ntohs(*reinterpret_cast<const sfUint16*>(GetData() + myReadPos));
00115     myReadPos += sizeof(Data);
00116     return *this;
00117 }
00118 sfPacket& sfPacket::operator >>(sfInt32& Data)
00119 {
00120     Data = ntohl(*reinterpret_cast<const sfInt32*>(GetData() + myReadPos));
00121     myReadPos += sizeof(Data);
00122     return *this;
00123 }
00124 sfPacket& sfPacket::operator >>(sfUint32& Data)
00125 {
00126     Data = ntohl(*reinterpret_cast<const sfUint32*>(GetData() + myReadPos));
00127     myReadPos += sizeof(Data);
00128     return *this;
00129 }
00130 sfPacket& sfPacket::operator >>(float& Data)
00131 {
00132     Data = *reinterpret_cast<const float*>(GetData() + myReadPos);
00133     myReadPos += sizeof(Data);
00134     return *this;
00135 }
00136 sfPacket& sfPacket::operator >>(double& Data)
00137 {
00138     Data = *reinterpret_cast<const double*>(GetData() + myReadPos);
00139     myReadPos += sizeof(Data);
00140     return *this;
00141 }
00142 sfPacket& sfPacket::operator >>(char* Data)
00143 {
00144     // First extract string length
00145     sfUint32 Length;
00146     *this >> Length;
00147 
00148     // Then extract characters
00149     strncpy(Data, GetData() + myReadPos, Length);
00150     Data[Length] = '\0';
00151 
00152     // Update reading position
00153     myReadPos += Length;
00154 
00155     return *this;
00156 }
00157 sfPacket& sfPacket::operator >>(std::string& Data)
00158 {
00159     // First extract string length
00160     sfUint32 Length;
00161     *this >> Length;
00162 
00163     // Then extract characters
00164     Data.assign(GetData() + myReadPos, Length);
00165 
00166     // Update reading position
00167     myReadPos += Length;
00168 
00169     return *this;
00170 }
00171 
00172 
00176 sfPacket& sfPacket::operator <<(sfInt8 Data)
00177 {
00178     Append(&Data, sizeof(Data));
00179     return *this;
00180 }
00181 sfPacket& sfPacket::operator <<(sfUint8 Data)
00182 {
00183     Append(&Data, sizeof(Data));
00184     return *this;
00185 }
00186 sfPacket& sfPacket::operator <<(sfInt16 Data)
00187 {
00188     sfInt16 ToWrite = htons(Data);
00189     Append(&ToWrite, sizeof(ToWrite));
00190     return *this;
00191 }
00192 sfPacket& sfPacket::operator <<(sfUint16 Data)
00193 {
00194     sfUint16 ToWrite = htons(Data);
00195     Append(&ToWrite, sizeof(ToWrite));
00196     return *this;
00197 }
00198 sfPacket& sfPacket::operator <<(sfInt32 Data)
00199 {
00200     sfInt32 ToWrite = htonl(Data);
00201     Append(&ToWrite, sizeof(ToWrite));
00202     return *this;
00203 }
00204 sfPacket& sfPacket::operator <<(sfUint32 Data)
00205 {
00206     sfUint32 ToWrite = htonl(Data);
00207     Append(&ToWrite, sizeof(ToWrite));
00208     return *this;
00209 }
00210 sfPacket& sfPacket::operator <<(float Data)
00211 {
00212     Append(&Data, sizeof(Data));
00213     return *this;
00214 }
00215 sfPacket& sfPacket::operator <<(double Data)
00216 {
00217     Append(&Data, sizeof(Data));
00218     return *this;
00219 }
00220 sfPacket& sfPacket::operator <<(const char* Data)
00221 {
00222     // First insert string length
00223     sfUint32 Length = static_cast<sfUint32>(strlen(Data));
00224     *this << Length;
00225 
00226     // Then insert characters
00227     Append(Data, Length);
00228 
00229     return *this;
00230 }
00231 sfPacket& sfPacket::operator <<(const std::string& Data)
00232 {
00233     // First insert string length
00234     sfUint32 Length = static_cast<sfUint32>(Data.size());
00235     *this << Length;
00236 
00237     // Then insert characters
00238     Append(Data.c_str(), Length);
00239 
00240     return *this;
00241 }
00242 
00243 
00247 void sfPacket::OnSend()
00248 {
00249     // Nothing by default
00250 }
00251 
00252 
00256 void sfPacket::OnReceive()
00257 {
00258     // Nothing by default
00259 }

Generated for SFML by  doxygen 1.5.2