NS2中的Packet类

来源:互联网 发布:dnf老是网络中断2016 编辑:程序博客网 时间:2024/04/29 21:29
NS2——Packet类
Packet类定义了分组的结构,提供了处理Packet对象的一系列成员函数。
定义在ns/common/packet.h中:
class Packet : public Event {
private:
    unsigned char* bits_;    // header bits    
/*
    该指针指向的字符数组中存储着所有的分组头,通常把该分组叫做"bag of bits",或BOB。p->bits_就是BOB数组。
    变量bits_包含BOB的第一个字节的地址,BOB是被激活的各个分组头中定义的所有structure连接在一起形成的。
    BOB在模拟过程中保持固定的大小,这个大小记录在变量Packet::hdrlen_中,hdrlen_的值在模拟配置阶段中由PacketHeaderManager类的有关代码负责设置。
*/

    AppData* data_;        // variable size buffer for 'data'    指向分组的数据部分,模拟中通常不用这个指针
    static void init(Packet*);     // initialize pkt hdr
    bool fflag_;
protected:
    static Packet* free_;    // packet free list
    int    ref_count_;    // free the pkt until count to 0
public:
    Packet* next_;        // for queues and the free list
    static int hdrlen_;    //记录BOB的大小

    Packet() : bits_(0), data_(0), ref_count_(0), next_(0) { }
    inline unsigned char* const bits() { return (bits_); }
    inline Packet* copy() const;
    inline Packet* refcopy() { ++ref_count_; return this; }
    inline int& ref_count() { return (ref_count_); }
    static inline Packet* alloc();
    static inline Packet* alloc(int);
    inline void allocdata(int);
    // dirty hack for diffusion data
    inline void initdata() { data_  = 0;}
    static inline void free(Packet*);
    inline unsigned char* access(int off) const {
        if (off < 0)
            abort();
        return (&bits_[off]);
    }
    // This is used for backward compatibility, i.e., assuming user data
    // is PacketData and return its pointer.
    inline unsigned char* accessdata() const {
        if (data_ == 0)
            return 0;
        assert(data_->type() == PACKET_DATA);
        return (((PacketData*)data_)->data());
    }
    // This is used to access application-specific data, not limited
    // to PacketData.
    inline AppData* userdata() const {
        return data_;
    }
    inline void setdata(AppData* d) {
        if (data_ != NULL)
            delete data_;
        data_ = d;
    }
    inline int datalen() const { return data_ ? data_->size() : 0; }

    // Monarch extn

    static void dump_header(Packet *p, int offset, int length);

    
        PacketStamp    txinfo_;  

    
        u_int8_t        incoming;

};
... ...
... ...

//hdr_cmn结构
/*
    这个结构主要定义了用于跟踪分组流向/度量时间以及长度等常用指标的字段。
*/
struct hdr_cmn {
    enum dir_t { DOWN= -1, NONE= 0, UP= 1 };
    packet_t ptype_;    // packet type (see above)
    int    size_;        // simulated packet size    分组大小,是应用数据的长度和IP层/运输层/应用层分组头长度之和。
    int    uid_;        // unique id
    int    error_;        // error flag
    int     errbitcnt_;     // # of corrupted bits jahn
    int     fecsize_;
    double    ts_;        // timestamp: for q-delay measurement
    int    iface_;        // receiving interface (label)
    double label_;//required by CSFQ
    dir_t    direction_;    // direction: 0=none, 1=up, -1=down
    // source routing
        char src_rt_valid;
    double ts_arr_; // Required by Marker of JOBS

    //Monarch extn begins
    nsaddr_t prev_hop_;     // IP addr of forwarding hop
    nsaddr_t next_hop_;    // next hop for this packet
    int      addr_type_;    // type of next_hop_ addr
    nsaddr_t last_hop_;     // for tracing on multi-user channels

        // called if pkt can't obtain media or isn't ack'd. not called if
        // droped by a queue
        FailureCallback xmit_failure_;
        void *xmit_failure_data_;

        /*
         * MONARCH wants to know if the MAC layer is passing this back because
         * it could not get the RTS through or because it did not receive
         * an ACK.
         */
        int     xmit_reason_;
#define XMIT_REASON_RTS 0x01
#define XMIT_REASON_ACK 0x02

        // filled in by GOD on first transmission, used for trace analysis
        int num_forwards_;    // how many times this pkt was forwarded
        int opt_num_forwards_;   // optimal #forwards
    // Monarch extn ends;

    // tx time for this packet in sec
    double txtime_;
    inline double& txtime() { return(txtime_); }

    static int offset_;    // offset for this header
    inline static int& offset() { return offset_; }
    inline static hdr_cmn* access(const Packet* p) {
        return (hdr_cmn*) p->access(offset_);
    }
    
        /* per-field member functions */
    inline packet_t& ptype() { return (ptype_); }
    inline int& size() { return (size_); }
    inline int& uid() { return (uid_); }
    inline int& error() { return error_; }
    inline int& errbitcnt() {return errbitcnt_; }
    inline int& fecsize() {return fecsize_; }
    inline double& timestamp() { return (ts_); }
    inline int& iface() { return (iface_); }
    inline double& label() {return (label_);}
    inline void setlabel(double label) {label_=label;}
    inline dir_t& direction() { return (direction_); }
    // monarch_begin
    inline nsaddr_t& next_hop() { return (next_hop_); }
    inline int& addr_type() { return (addr_type_); }
    inline int& num_forwards() { return (num_forwards_); }
    inline int& opt_num_forwards() { return (opt_num_forwards_); }
        //monarch_end
};