📄 node132.html
字号:
<html><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><!--Converted with jLaTeX2HTML 2002 (1.62) JA patch-1.4patched version by: Kenshi Muto, Debian Project.LaTeX2HTML 2002 (1.62),original version by: Nikos Drakos, CBLU, University of Leeds* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>12.2.1 The Packet Class</TITLE><META NAME="description" CONTENT="12.2.1 The Packet Class"><META NAME="keywords" CONTENT="everything"><META NAME="resource-type" CONTENT="document"><META NAME="distribution" CONTENT="global"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"><META NAME="Generator" CONTENT="jLaTeX2HTML v2002 JA patch-1.4"><META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"><LINK REL="STYLESHEET" HREF="everything.css" tppabs="http://www.isi.edu/nsnam/ns/doc/everything.css"><LINK REL="next" HREF="node133.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node133.html"><LINK REL="previous" HREF="node131.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node131.html"><LINK REL="up" HREF="node131.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node131.html"><LINK REL="next" HREF="node133.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node133.html"></HEAD><BODY ><!--Navigation Panel--><A NAME="tex2html3320" HREF="node133.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node133.html"><IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="file:/usr/share/latex2html/icons/next.png"></A> <A NAME="tex2html3314" HREF="node131.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node131.html"><IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="file:/usr/share/latex2html/icons/up.png"></A> <A NAME="tex2html3308" HREF="node131.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node131.html"><IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="file:/usr/share/latex2html/icons/prev.png"></A> <A NAME="tex2html3316" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html"><IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="file:/usr/share/latex2html/icons/contents.png"></A> <A NAME="tex2html3318" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html"><IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="file:/usr/share/latex2html/icons/index.png"></A> <BR><B> Next:</B> <A NAME="tex2html3321" HREF="node133.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node133.html">12.2.2 p_info Class</A><B> Up:</B> <A NAME="tex2html3315" HREF="node131.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node131.html">12.2 Packet Classes</A><B> Previous:</B> <A NAME="tex2html3309" HREF="node131.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node131.html">12.2 Packet Classes</A>   <B> <A NAME="tex2html3317" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html">Contents</A></B>   <B> <A NAME="tex2html3319" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html">Index</A></B> <BR><BR><!--End of Navigation Panel--><H2><A NAME="SECTION03921000000000000000"></A><A NAME="sec:packetclass"></A><BR>12.2.1 The Packet Class</H2><P>The class Packet defines the structure of apacket and provides member functions to handle afree list for objects of this type.It is illustrated in Figure <A HREF="node132.html#pic:packet" tppabs="http://www.isi.edu/nsnam/ns/doc/node132.html#pic:packet">12.1</A> anddefined as follows in <TT>packet.h</TT>:<DIV ALIGN="CENTER"><A NAME="pic:packet"></A><A NAME="5263"></A><TABLE><CAPTION ALIGN="BOTTOM"><STRONG>Figure 12.1:</STRONG>A Packet Object</CAPTION><TR><TD><DIV ALIGN="CENTER">packet</DIV></TD></TR></TABLE></DIV><PRE> class Packet : public Event { private: friend class PacketQueue; u_char* bits_; u_char* data_; /* variable size buffer for 'data' / u_int datalen_; /* length of variable size buffer / protected: static Packet* free_; public: Packet* next_; /* for queues and the free list / static int hdrlen_; Packet() : bits_(0), datalen_(0), next_(0) {} u_char* const bits() { return (bits_); } Packet* copy() const; static Packet* alloc(); static Packet* alloc(int); inline void allocdata(int); static void free(Packet*); inline u_char* access(int off) { if (off \< 0) abort(); return (&bits_[off]); } inline u_char* accessdata() { return data_; } };</PRE>This class holds a pointer to a generic array of unsignedcharacters (commonly called the ``bag of bits'' or BOB for short)where packet header fields are stored.It also holds a pointer to packet ``data'' (which is often not used insimulations).The <TT>bits_</TT> variable contains the address ofthe first byte of the BOB.Effectively BOB is (currently implemented as) a concatenationof all the structures defined for each packet header (by convention,the structures with names beginning <TT>hdr_something</TT>) that havebeen configured in.BOB generally remains a fixed size throughout a simulation, andthe size is recorded in the <TT>Packet::hdrlen_</TT> membervariable.This size is updated during simulator configuration byOTcl<A NAME="tex2html20" HREF="footnode.html#foot5345" tppabs="http://www.isi.edu/nsnam/ns/doc/footnode.html#foot5345"><SUP>12.1</SUP></A>.<P>The other methods of the class Packet are for creating newpackets and storing old (unused) ones on a private free list.Such allocation and deallocation is performed by thefollowing code (in packet.h):<PRE> inline Packet* Packet::alloc() { Packet* p = free_; if (p != 0) free_ = p-\>next_; else { p = new Packet; p-\>bits_ = new u_char[hdrsize_]; if (p == 0 || p-\>bits_ == 0) abort(); } return (p); } /* allocate a packet with an n byte data buffer */ inline Packet* Packet::alloc(int n) { Packet* p = alloc(); if (n \> 0) p-\>allocdata(n); return (p); } /* allocate an n byte data buffer to an existing packet */ inline void Packet::allocdata(int n) { datalen_ = n; data_ = new u_char[n]; if (data_ == 0) abort(); } inline void Packet::free(Packet* p) { p-\>next_ = free_; free_ = p; if (p-\>datalen_) { delete p-\>data_; p-\>datalen_ = 0; } } inline Packet* Packet::copy() const { Packet* p = alloc(); memcpy(p-\>bits(), bits_, hdrlen_); if (datalen_) { p-\>datalen_ = datalen_; p-\>data_ = new u_char[datalen_]; memcpy(p-\>data_, data_, datalen_); } return (p); }</PRE>The []alloc method is a support function commonlyused to create new packets.It is called by []Agent::allocpkt method onbehalf of agents and is thus not normally invoked directly by most objects.It first attempts to locate an old packet on the free list andif this fails allocates a new one using the C++ <TT>new</TT> operator.Note that <TT>Packet</TT> class objects and BOBs areallocated separately.The []free method frees a packet by returning it to the freelist.Note that <I>packets are never returned to the system's memory allocator</I>.Instead, they are stored on a free list when []Packet::free is called.The []copy member creates a new, identical copy of a packetwith the exception of the <TT>uid_</TT> field, which is unique.This function is used by <TT>Replicator</TT> objects to supportmulticast distribution and LANs.<P><HR><!--Navigation Panel--><A NAME="tex2html3320" HREF="node133.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node133.html"><IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="file:/usr/share/latex2html/icons/next.png"></A> <A NAME="tex2html3314" HREF="node131.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node131.html"><IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="file:/usr/share/latex2html/icons/up.png"></A> <A NAME="tex2html3308" HREF="node131.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node131.html"><IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="file:/usr/share/latex2html/icons/prev.png"></A> <A NAME="tex2html3316" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html"><IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="file:/usr/share/latex2html/icons/contents.png"></A> <A NAME="tex2html3318" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html"><IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="file:/usr/share/latex2html/icons/index.png"></A> <BR><B> Next:</B> <A NAME="tex2html3321" HREF="node133.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node133.html">12.2.2 p_info Class</A><B> Up:</B> <A NAME="tex2html3315" HREF="node131.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node131.html">12.2 Packet Classes</A><B> Previous:</B> <A NAME="tex2html3309" HREF="node131.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node131.html">12.2 Packet Classes</A>   <B> <A NAME="tex2html3317" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html">Contents</A></B>   <B> <A NAME="tex2html3319" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html">Index</A></B> <!--End of Navigation Panel--><ADDRESS>2003-09-23</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -