MessageIn Class Reference

#include <MessageIn.h>

Inheritance diagram for MessageIn:

Message List of all members.

Public Member Functions

 MessageIn (NetPacket *packet)
virtual ~MessageIn ()
message_type getType ()
char read1 ()
Uint16 read2 ()
Uint32 read4 ()
Uint32 getCRC ()
bool checkCRC ()
std::string readString (Sint16 length=-1)
void reset ()

Private Attributes

Uint32 pos
NetPacketmyPacket
message_type myType

Detailed Description

Message built from a given packet. Usually it is an inbound Message sent from a remote host.

Definition at line 17 of file MessageIn.h.


Constructor & Destructor Documentation

MessageIn::MessageIn NetPacket packet  ) 
 

Builds a message from a given packet. Tipically used to manage a received packet.

Author:
stonedz
Since:
pre-alpha
Parameters:
packet Packet to build the message from.

Definition at line 6 of file MessageIn.cpp.

References myType, and read2().

00007         :pos(0),
00008         myPacket(packet)
00009 {
00010         this->myType = read2(); 
00011 }

MessageIn::~MessageIn  )  [virtual]
 

Virtual destructor.

Author:
stonedz
Since:
pre-alpha

Definition at line 13 of file MessageIn.cpp.

00013                      {
00014         if(this->myPacket)
00015                 delete this->myPacket;
00016 }


Member Function Documentation

bool MessageIn::checkCRC  ) 
 

Check the message crc against crc calculated locally.

Author:
dndvault
Since:
pre-alpha
Returns:
True if ok, false if CRCs are different.

Definition at line 73 of file MessageIn.cpp.

References getCRC().

Referenced by ClientFSM::authenticate().

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 }

Uint32 MessageIn::getCRC  ) 
 

Reads and returns message's CRC.

Bytes are transformed from Network Byte Order to Little Endian.This method MUST be called after any other read method. It will usually be used after a readString() call.

Author:
stonedz
Since:
pre-alpha
Returns:
Uint32 CRC, 0 if error.
Todo:
TODO Check for other than x86 compatibility.
See also:
readString

Definition at line 61 of file MessageIn.cpp.

References pos.

Referenced by checkCRC().

00061                         {
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 }

message_type MessageIn::getType  )  [inline]
 

Returns the type of the message. for type definitions see Message.h.

Author:
stonedz
Since:
pre-alpha
Returns:
Message type.
See also:
Message.h

Definition at line 48 of file MessageIn.h.

Referenced by ClientFSM::Login(), and ClientFSM::Ready().

00048 { return myType; }

char MessageIn::read1  ) 
 

Reads a single byte from the packet.

Author:
themanaworld

stonedz

Since:
pre-alpha
Returns:
The byte read. 0 if error.

Definition at line 18 of file MessageIn.cpp.

References pos.

00018                      {
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 }

Uint16 MessageIn::read2  ) 
 

Reads 2 bytes from packet.

Bytes are transformed from Network Byte Order to Little Endian.

Author:
themanaworld

stonedz

Since:
pre-alpha
Returns:
The two bytes read. 0 if error.
Todo:
TODO Check for other than x86 compatibility.

Definition at line 31 of file MessageIn.cpp.

References pos.

Referenced by MessageIn(), ClientFSM::move(), and readString().

00031                        {        
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 }

Uint32 MessageIn::read4  ) 
 

Reads 4 bytes from packet.

Bytes are transformed from Network Byte Order to Little Endian.

Author:
themanaworld

stonedz

Since:
pre-alpha
Returns:
The four bytes read. 0 if error.
Todo:
TODO Check for other than x86 compatibility.

Definition at line 46 of file MessageIn.cpp.

References pos.

00046                        {        
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 }

std::string MessageIn::readString Sint16  length = -1  ) 
 

Reads a string from the message, if the lenght is not given then it retrieves string's lenght from the first two bytes of the message.

It's usually the unique method used to get message's body.

Author:
themanaworld

stonedz

Since:
pre-alpha
Parameters:
lenght String's lenght to read.
Returns:
The string read if ok, an empty string if fail.
See also:
getCRC

Definition at line 98 of file MessageIn.cpp.

References NetPacket::data, NetPacket::len, myPacket, pos, and read2().

Referenced by ClientFSM::authenticate(), and ClientFSM::chat().

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 }

void MessageIn::reset  ) 
 

Resets current message and deletes the packet associated with it.

Author:
stonedz
Since:
pre-alpha

Definition at line 120 of file MessageIn.cpp.

References myType, and pos.

00120                      {
00121         this->myType = MSG_INVALID;     
00122         this->pos = 0;
00123         if(this->myPacket)
00124                 delete this->myPacket;
00125 }


Member Data Documentation

NetPacket* MessageIn::myPacket [private]
 

The packet the message is built on.

Definition at line 135 of file MessageIn.h.

Referenced by readString().

message_type MessageIn::myType [private]
 

Message type.

Definition at line 136 of file MessageIn.h.

Referenced by MessageIn(), and reset().

Uint32 MessageIn::pos [private]
 

Current position in the packet.

Definition at line 134 of file MessageIn.h.

Referenced by getCRC(), read1(), read2(), read4(), readString(), and reset().


The documentation for this class was generated from the following files:
Generated on Mon Dec 3 04:30:14 2007 for lo-testserver by  doxygen 1.3.9.1