MessageOut Class Reference

#include <MessageOut.h>

Inheritance diagram for MessageOut:

Message List of all members.

Public Member Functions

 MessageOut (const message_type m_type)
virtual ~MessageOut ()
void write1 (char value)
void write2 (Uint16 value)
void write4 (Uint32 value)
char * getData ()
Uint32 getDataSize ()
const NetPacketgetPacket ()
void writeString (const string &string, Sint16 length=-1)
Uint32 addCRC ()

Private Member Functions

void expandData (Uint16 size)

Private Attributes

char * myData
Uint32 myDataSize
Uint32 pos
NetPacketmyPacket

Detailed Description

A Message that will be sent trought network.

Todo:
TODO Comment the implementation.

Definition at line 20 of file MessageOut.h.


Constructor & Destructor Documentation

MessageOut::MessageOut const message_type  m_type  ) 
 

Builds a message of a given type. Tipically used to create a new message that will be sent. For type definition see Message.h .

Author:
stonedz
Since:
pre-alpha
Parameters:
m_type Message's type. See above for definitions.
See also:
Message.h

Definition at line 5 of file MessageOut.cpp.

References write2().

00006         :myData(NULL),
00007         myDataSize(0),
00008         pos(0),
00009         myPacket(NULL)
00010 {
00011         write2(m_type); //Writes the message type in the first two bytes of the message.
00012 }

MessageOut::~MessageOut  )  [virtual]
 

Virtual destructor.

Author:
stonedz
Since:
pre-alpha

Definition at line 14 of file MessageOut.cpp.

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


Member Function Documentation

Uint32 MessageOut::addCRC  ) 
 

Computes than add the CRC at message's bottom.

CRC is computed then added at the end of the message's payload. Since its lenght is fixed it (the lenght) will not be added to lenght message's field.

Author:
stonedz
Since:
pre-alpha
Returns:
CRC value.

Definition at line 77 of file MessageOut.cpp.

References write4().

Referenced by Server::generalChatMsg(), and ClientFSM::SendKA_ACK().

00077                          {
00078         CRC32 msgCRC;
00079         Uint32 crc;
00080 
00081         msgCRC.Init();
00082         crc = msgCRC.GetCRC(this->myData, (this->pos));
00083 
00084         write4(crc);
00085 
00086         return crc;
00087 }

void MessageOut::expandData Uint16  size  )  [private]
 

Expands current allocated memory for myData using realloc.

WARNING: more memory may be allocated by realloc due to performance reasons.

Author:
themanaworld

stonedz

Since:
pre-alpha
Parameters:
size The new size of the allocated memory, in bytes.

Definition at line 88 of file MessageOut.cpp.

References myData, and myDataSize.

Referenced by write1(), write2(), write4(), and writeString().

00088                                       {
00089 
00090         this->myData = (char*)realloc(this->myData, size);
00091         this->myDataSize = size;
00092 }

char * MessageOut::getData  ) 
 

Returns current message's data.

Author:
stonedz
Since:
pre-alpha
Returns:
Pointer to current message's data.

Definition at line 40 of file MessageOut.cpp.

00040                          {
00041         return this->myData;
00042 }

Uint32 MessageOut::getDataSize  ) 
 

Returns current data's lenght.

Author:
stonedz
Since:
pre-alpha
Returns:
Lenght in bytes.

Definition at line 44 of file MessageOut.cpp.

00044                               {
00045         return this->myDataSize;
00046 }

const NetPacket * MessageOut::getPacket  ) 
 

Return a packed built from this message.

WARNING: Modifications to the message after a call to getPacket won't be considered. The memory allocated for the packet is deleted in message's destructor.

Author:
themanaworld

stonedz

Since:
pre-alpha
Returns:
A pointer to a packet instance.

Definition at line 48 of file MessageOut.cpp.

References myPacket.

Referenced by Connection::putMessage().

00048                                       {
00049         if (!this->myPacket){
00050         this->myPacket = new NetPacket(this->myData, this->myDataSize);
00051     }
00052     return this->myPacket;
00053 }

void MessageOut::write1 char  value  ) 
 

Writes a single byte to myData.

Author:
themanaworld

stonedz

Since:
pre-alpha
Parameters:
value Byte to write.

Definition at line 20 of file MessageOut.cpp.

References expandData(), myData, and pos.

00020                                  {
00021     expandData((this->pos) + 1);
00022     myData[this->pos] = value;
00023     this->pos += 1;
00024 }

void MessageOut::write2 Uint16  value  ) 
 

Writes an unsigned 2 byte integer to myData.

The data written are in Network Byte Order.

Author:
themanaworld

stonedz

Since:
pre-alpha
Parameters:
value 2 bytes to write.
Todo:
TODO Check for other than x86 compatibility.

Definition at line 26 of file MessageOut.cpp.

References expandData(), and pos.

Referenced by MessageOut(), ClientFSM::SendKA_ACK(), and writeString().

00026                                    {
00027         expandData((this->pos) + 2);
00028         Uint16 tmp = SDL_SwapBE16(value);
00029         memcpy((this->myData)+(this->pos), &tmp, 2);
00030     this->pos += 2;
00031 }

void MessageOut::write4 Uint32  value  ) 
 

Writes an unsigned 4 byte integer to myData.

The data written are in Network Byte Order.

Author:
themanaworld

stonedz

Since:
pre-alpha
Parameters:
value 4 bytes to write.
Todo:
TODO Check for other than x86 compatibility.

Definition at line 33 of file MessageOut.cpp.

References expandData(), and pos.

Referenced by addCRC().

00033                                    {
00034         expandData((this->pos) + 4);
00035         Uint32 tmp = SDL_SwapBE32(value);
00036         memcpy((this->myData)+(this->pos), &tmp, 4);
00037     this->pos += 4;
00038 }

void MessageOut::writeString const string &  string,
Sint16  length = -1
 

Writes a string to the message.

If the lenght parameter isn't specified, which is the default behaviur, string lenght will be automatically written on two initial bytes. If you specify the lenght it means that you know what you're doing, because you will have to deal with a manual approach in message's building.

Author:
themanaworld

stonedz

Since:
pre-alpha
Parameters:
string Constant reference to the string we want to write.
lenght String lenght, if not specified it'll be calculated by this method.

Definition at line 55 of file MessageOut.cpp.

References expandData(), pos, and write2().

Referenced by Server::generalChatMsg().

00055                                                                {
00056     Uint16 stringLength = string.length();
00057     if (length < 0)
00058     {
00059         // Write the length at the start if not fixed.
00060         write2(stringLength);
00061         length = stringLength;
00062     }
00063     else if (length < stringLength)
00064     {
00065         stringLength = length; // Make sure the length of the string is no longer than specified
00066     }
00067     expandData((this->pos) + length);
00068     memcpy((this->myData) + (this->pos), string.c_str(), stringLength); // Write the actual string.
00069 
00070     if (length > stringLength) // Pad remaining space with zeros.
00071     {
00072         memset((this->myData) + (this->pos) + stringLength, '\0', length - stringLength);
00073     }
00074     this->pos += length;
00075 }


Member Data Documentation

char* MessageOut::myData [private]
 

Current message's data.

Definition at line 156 of file MessageOut.h.

Referenced by expandData(), and write1().

Uint32 MessageOut::myDataSize [private]
 

Current data's lenght.

Definition at line 157 of file MessageOut.h.

Referenced by expandData().

NetPacket* MessageOut::myPacket [private]
 

A packet to be sent or received from network.

Definition at line 159 of file MessageOut.h.

Referenced by getPacket().

Uint32 MessageOut::pos [private]
 

Pointer to current position in data.

Definition at line 158 of file MessageOut.h.

Referenced by write1(), write2(), write4(), and writeString().


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