![]() | |
![]() |
![]() |
![]() |
![]() |
overview |
qpacket handles some of the encoding and decoding tasks for each packet. Through the use of qpacket::addBE*() and qpacket::addLE*() functions (where the * is one of int, short, byte, float, or string) endianess is taken care of automatically (i.e. using qpacket::addBEint() makes sure that the integer passed is encoded into the packet in big endian (Motrolla) order.)NOTE that I think I have named the endians wrong (i.e. Motrolla chips are really little endian, while Intel's are actually called big endian) but I don't feel like changing it right now ;)
Other important functions are qpacket::update() and qpacket::init(). These two functions are very similar, but make sure you understand their differences. qpacket::init() should be called only after filling the buffer with fresh data since it determines packet type and size from the first 4 bytes in the buffer.
Also, before calling qpacket::update(), make sure to set the packet type with qpacket::changeType() (i.e. qpacket::changeType( qpacket::control )).
qpacket::reset() just resets the read and write positions but doesn't change the data in the buffer (the memory is not reallocated nor is it reset to 0's).
To receive data from, say, a recv call, use the functions qpacket::getBuffer() and qpacket::getBufferSize() to get a pointer to the buffer and its size, respectivly.
member variables |
class qpacket { public: enum type { control, reliableFragment, reliableEnd, acknowledge, unreliable, unknown }; private: int size; int maxSize; int readPos; int writePos; char * data; type pType; char t1[ 4 ]; char t2[ 4 ]; public: };
- type
- enum of the different packet types.
- size
- The actual amount of data currently in the packet (less the 4 byte header)
- maxSize
- Amount of allocated memory.
- readPos, writePos
- Where the next byte will be read or written within data
- data
- The data itself...
- pType
- The "nice" packet type.
- t1, t2
- Used to swap bytes during endianess convertion (actually I think only t1 is used; I will be looking into it ;) ).
member functions |
class qpacket { public: enum type { control, reliableFragment, reliableEnd, acknowledge, unreliable, unknown }; private: public: qpacket( int x=QP_MAX_PACKET_DATA ); void copy( qpacket & ); ~qpacket() { delete[] data; } void reset (); int write( FILE *, int x, vector & ); int init(); int update(); type getType(); void dumpHex(); void dumpChar(); void changeType( type x ); void setReadPos( int x ); void ffwd(); int send( qsocket * ); int getSize(); int getReadPos(); int getWritePos(); int getMaxSize(); char * getBuffer(); int getNumber(); int changeNumber( int ); int readLEint(); int readBEint(); short readLEshort(); short readBEshort(); float readLEfloat(); float readBEfloat(); unsigned char readByte(); float readAngle(); float readCoord(); char * readString(); void addLEint( int ); void addBEint( int ); void addLEshort( short ); void addBEshort( short ); void addLEfloat( float ); void addBEfloat( float ); void addByte( char x ); void addData( char * x, int s ); void addAngle( float ); void addCoord( float ); void addString( char * ); qpacket & operator += ( qpacket & ); };
- ctors
- The variable specifies the amount of memory to allocate right away (more is added if needed)
- copy
- Copies two packets (G++ was having problems with qpacket::qpacket( qpacket & ) for some odd reason)
- reset
- Puts read/write pointers to beginning, makes size 0.
- write
- Writes a .DEM-compliant packet to the specified file stream. The vector is the facing and the x is where to start writing in the data. THIS WAS A HACK AND WILL BE FIXED (when I feel like it ;) )
- init
- allocates memory
- update
- Adds packet length and type to top (header) pType should be correct before entry; use qpacket::changeType.
- dumpHex, dumpChar
- Prints the packet out in hex and character formats.
- changeType
- Sets the packet type. (usage: qpacket::changeType( qpacket::control ))
- setReadPos
- Set where the read position goes. Takes the header into account.
- ffwd
- Sets read and write positions to the end of the packet
- send
- Launches the packet through the qsocket. This function does call qpacket::update before sending the packet.
- getBuffer
- Don't use this if you can help it. Returns a pointer to the data.
- changeNumber
- Changes the packet number. Assumes this is an unreliable packet.
- ADD functions
- The addLE* functions add a Little Endian (really big; Intel in any case) * to the packet, taking into consideration the endianess of the current machine. The addBE* work similarily.
- READ functions
- Same as above. readLEint, for example, returns an integer assuming the next 4 bytes from the data buffer are an integer in Little Endian (really big; Intel) format.
- operator +=
- Adds the right-hand operand to the end of the current packet. Makes some assumptions; read the header for the function in the code.