Packet Class Reference

A Packet class. More...

#include <common.h>

List of all members.

Public Member Functions

 Packet ()
 Packet (int buffer_length)
int fillPayload (int size, char *inputstream)
char * getPayload ()
void setPayloadSize (int size)
int getBufferSize ()
int getPayloadSize ()
int getHeaderSize ()
PacketHdraccessHeader ()
void extractHeader (char *streambuf)
int makePacket (char *streambuf)

Static Public Attributes

static const int DEFAULT_PAYLOAD_SIZE = 512

Protected Attributes

int size_
 Packet length in Bytes.
int length_
 Maximum allocated Size of Payload buffer. it is no less than the size_.
char * payload_
 Payload Pointer.
PacketHdrheader_
 Header Pointer;.


Detailed Description

A Packet class.

Packet is an entity which contains a set of ordered information bits. This packet object only carries a pointer to payload information, not any socket address information.

Definition at line 89 of file common.h.


Constructor & Destructor Documentation

Packet::Packet int  buffer_length  ) 
 

Alternate Packet Constructor: Specify a large buffer.... This is useful to define a packet receiving buffer.

Definition at line 73 of file common.cpp.

References header_, length_, payload_, and size_.

00074 {
00075   size_ = 0;
00076   length_ = buffer_length;
00077   payload_ = new char[buffer_length];
00078   header_ = new PacketHdr();
00079 }


Member Function Documentation

PacketHdr* Packet::accessHeader  )  [inline]
 

Get the packet header

Definition at line 119 of file common.h.

References header_.

00119 { return header_;} 

void Packet::extractHeader char *  streambuf  ) 
 

Extract packet header from the incoming stream

Definition at line 131 of file common.cpp.

References PacketHdr::accessInfo(), header_, and PacketHdr::setHeaderSize().

Referenced by ReceivingPort::receivePacket().

00132 {
00133   char* p= streambuf; 
00134   int a  = *(p++);
00135   header_->setHeaderSize(a);
00136   memcpy( header_->accessInfo(), p ,a);   
00137 }

int Packet::fillPayload int  size,
char *  inputstream
 

A function to fill payload. user can specify the content of payload for applications like audio/video playback...

Definition at line 105 of file common.cpp.

References payload_, and setPayloadSize().

Referenced by ReceivingPort::receivePacket().

00106 {
00107   setPayloadSize(size);
00108   if (memcpy((char *)payload_, (char *)inputstream,  size) == NULL) {
00109     throw "Fill payload Failed";
00110   }
00111   return 0;
00112 }

int Packet::getBufferSize  )  [inline]
 

get the size of packet buffer where payload is stored.

Definition at line 107 of file common.h.

References length_.

00107 {return length_;}

int Packet::getHeaderSize  )  [inline]
 

get the size of the packet header

Definition at line 115 of file common.h.

References PacketHdr::getSize(), and header_.

Referenced by ReceivingPort::receivePacket().

00115 { return header_->getSize();}

char* Packet::getPayload  )  [inline]
 

get a pointer to the payload

Returns:
a char pointer

Definition at line 102 of file common.h.

References payload_.

00102 { return payload_;} 

int Packet::getPayloadSize  )  [inline]
 

get the size of the packet

Definition at line 111 of file common.h.

References size_.

00111 { return size_;}

int Packet::makePacket char *  streambuf  ) 
 

Assemble header and payload and headersize in a single stream

Definition at line 118 of file common.cpp.

References PacketHdr::accessInfo(), PacketHdr::getSize(), header_, payload_, and size_.

Referenced by SendingPort::sendPacket().

00119 {
00120   streambuf[0]= ( header_->getSize() ) & 0xff;
00121   streambuf[1]=  0x00;
00122   memcpy(streambuf+1, header_->accessInfo(), header_->getSize());
00123   memcpy(streambuf+1+header_->getSize(), payload_, size_);
00124   
00125   return 1+size_+ header_->getSize();
00126 }

void Packet::setPayloadSize int  size  ) 
 

set packet size. As packet already has a default buffer, there are two ways to determine the payload

  • set size only, let the payload be as it is --> SetPayloadSize
    1. if necessary, adjust the payload buffer size.
  • set size and also fill the payload with speficic data (e.g. for Audio and Video Applications...)
    See also:
    fillPayload

Definition at line 91 of file common.cpp.

References length_, payload_, and size_.

Referenced by fillPayload().

00092 {
00093   size_ =  size;
00094   if (size > length_) {
00095     if (payload_ != NULL) delete [] payload_;
00096     length_ = (int)(1.5 * size);
00097     payload_ =  new char[length_];
00098   }
00099 }


Member Data Documentation

const int Packet::DEFAULT_PAYLOAD_SIZE = 512 [static]
 

Default payload size is 512 Bytes

Definition at line 95 of file common.h.

Referenced by Packet().


The documentation for this class was generated from the following files:
Generated on Fri Feb 9 12:49:37 2007 for Common_Classes_Projects_ECE544 by  doxygen 1.4.6