src/network/MessageIn.cpp

00001 #include "MessageIn.h"
00002 
00003 #include "NetPacket.h"
00004 #include "crc32.h"
00005 
00006 MessageIn::MessageIn(NetPacket* packet)
00007         :pos(0),
00008         myPacket(packet)
00009 {
00010         this->myType = read2(); 
00011 }
00012 
00013 MessageIn::~MessageIn(){
00014         if(this->myPacket)
00015                 delete this->myPacket;
00016 }
00017 
00018 char MessageIn::read1(){
00019     char value = -1;
00020     if (this->myPacket)
00021     {
00022         if (pos < (this->myPacket)->len)
00023         {
00024             value = (this->myPacket)->data[pos];
00025             pos += 1;
00026         }
00027     }
00028     return value;
00029 }
00030 
00031 Uint16 MessageIn::read2(){      
00032     Uint16 value = 0;
00033     if (this->myPacket)
00034     {
00035         if (pos + 2 <= (this->myPacket)->len)
00036         {
00037             Uint16 tmp;
00038             memcpy(&tmp, (this->myPacket)->data + pos, 2);
00039             value = SDL_SwapBE16(tmp);
00040             pos += 2;
00041         }
00042     }
00043     return value;
00044 }
00045 
00046 Uint32 MessageIn::read4(){      
00047     Uint32 value = 0;
00048     if (this->myPacket)
00049     {
00050         if (pos + 4 <= (this->myPacket)->len)
00051         {
00052             Uint32 tmp;
00053             memcpy(&tmp, (this->myPacket)->data + pos, 4);
00054             value = SDL_SwapBE32(tmp);
00055             pos += 4;
00056         }
00057     }
00058     return value;
00059 }
00060 
00061 Uint32 MessageIn::getCRC(){
00062          Uint32 value = 0;
00063          if (this->myPacket)
00064     {
00065             Uint32 tmp;
00066             memcpy(&tmp, (this->myPacket)->data + pos, 4);
00067             value = SDL_SwapBE32(tmp);
00068             pos += 4;
00069     }
00070     return value;               
00071 }
00072 
00073 bool MessageIn::checkCRC()
00074 {
00075         bool ret = false;
00076         char *tmpBuf;
00077         CRC32 CRC;
00078         Uint32 localCRC;
00079         Uint32 pckCRC;
00080         
00081         tmpBuf = new char [((this->myPacket)->len - 4)];
00082         memcpy((char *) tmpBuf, (this->myPacket)->data, ((this->myPacket)->len - 4));
00083         
00084         CRC.Init();
00085         localCRC = CRC.GetCRC(tmpBuf, ((this->myPacket)->len - 4));
00086         pckCRC = this->getCRC();
00087         
00088         if (localCRC == pckCRC)
00089         {
00090                 ret = true;
00091         }
00092         
00093         delete [] tmpBuf;
00094         
00095         return ret;
00096 }
00097 
00098 std::string MessageIn::readString(Sint16 len)
00099 {
00100     if (this->myPacket)
00101     {
00102         if (len < 0) { // Get string length
00103             len = read2();
00104         }
00105         if (len < 0 || this->pos + len > this->myPacket->len) { // Make sure the string isn't erroneous
00106             this->pos = (this->myPacket->len) + 1;
00107             return "";
00108         }
00109 
00110         // Read the string
00111         char const *stringBeg = (this->myPacket->data) + (this->pos);
00112         char const *stringEnd = (char const *)memchr(stringBeg, '\0', len);
00113         std::string finalString(stringBeg, stringEnd ? stringEnd - stringBeg : len);
00114         this->pos += len;
00115         return finalString;
00116     }
00117     return "";
00118 }
00119 
00120 void MessageIn::reset(){
00121         this->myType = MSG_INVALID;     
00122         this->pos = 0;
00123         if(this->myPacket)
00124                 delete this->myPacket;
00125 }

Generated on Mon Dec 3 04:30:13 2007 for lo-testserver by  doxygen 1.3.9.1