Main Page | Class Hierarchy | Class List | File List | Class Members

Packet Class Reference

A Packet class. More...

#include <common.h>

List of all members.

Public Member Functions

 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 99 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 75 of file common.cpp.

References header_, length_, payload_, and size_.

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


Member Function Documentation

PacketHdr* Packet::accessHeader  )  [inline]
 

Get the packet header

Definition at line 129 of file common.h.

References header_.

00129 { return header_;} 

void Packet::extractHeader char *  streambuf  ) 
 

Extract packet header from the incoming stream

Definition at line 133 of file common.cpp.

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

Referenced by ReceivingPort::receivePacket().

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

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 107 of file common.cpp.

References payload_, and setPayloadSize().

Referenced by ReceivingPort::receivePacket().

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

int Packet::getBufferSize  )  [inline]
 

get the size of packet buffer where payload is stored.

Definition at line 117 of file common.h.

References length_.

00117 {return length_;}

int Packet::getHeaderSize  )  [inline]
 

get the size of the packet header

Definition at line 125 of file common.h.

References PacketHdr::getSize(), and header_.

Referenced by ReceivingPort::receivePacket().

00125 { return header_->getSize();}

char* Packet::getPayload  )  [inline]
 

get a pointer to the payload

Returns:
a char pointer

Definition at line 112 of file common.h.

References payload_.

00112 { return payload_;} 

int Packet::getPayloadSize  )  [inline]
 

get the size of the packet

Definition at line 121 of file common.h.

References size_.

00121 { return size_;}

int Packet::makePacket char *  streambuf  ) 
 

Assemble header and payload and headersize in a single stream

Definition at line 120 of file common.cpp.

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

Referenced by SendingPort::sendPacket().

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

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 93 of file common.cpp.

References length_, payload_, and size_.

Referenced by fillPayload().

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


Member Data Documentation

const int Packet::DEFAULT_PAYLOAD_SIZE = 512 [static]
 

Default payload size is 512 Bytes

Definition at line 105 of file common.h.


The documentation for this class was generated from the following files:
Generated on Fri Feb 17 17:55:49 2006 for Common_Classes_Projects_ECE544 by  doxygen 1.4.2