qpacket

development status
stability
access

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:



};
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 & );

};

This page is copyright
© 1997 Mike Warren
All rights reserved.