00001 #ifndef __NPPACKET_H
00002 #define __NPPACKET_H
00003
00004 #include <iostream>
00005 #include "os_spec.h"
00006 #include "Packet.h"
00007 #include "TpPacket.h"
00008 #include "ConnectionInterface.h"
00009
00010 using namespace std;
00011
00012 class NpPacket : public Packet
00013 {
00014 public:
00016 OS_SPEC_ENUM NpType
00017 {
00019 Data = 0x00,
00020
00022 Connect = 0x01,
00023
00025 Established = 0x02,
00026
00028 Fin = 0x03,
00029
00031 Discovery = 0x04,
00032
00034 ImHere = 0x05,
00035
00037 Ping = 0x06,
00038
00040 Pong = 0x07
00041 };
00042
00043
00044
00045
00046
00047 NpPacket();
00048
00049 NpPacket(NpType type, unsigned int vcn);
00050
00051 NpPacket(TpPacket* packet, unsigned int vcn);
00052
00053 NpPacket(unsigned char* rawData);
00054
00055 static NpPacket* createPacket(NpType);
00056
00057 static NpPacket* decode(unsigned char* rawData, NpType packetType);
00058
00059
00060
00061
00062
00063 virtual ~NpPacket();
00064
00065
00066
00067
00068
00069
00070 int getVersionNumber();
00071
00072 unsigned int getVcn();
00073
00074 unsigned char* getData();
00075
00076 unsigned int getDataLength() const;
00077
00078 virtual unsigned char* getRawData();
00079
00080 unsigned int getRawLength() const;
00081
00082 NpType getPacketType() const;
00083
00084 bool isControlPacket() const;
00085
00086 virtual unsigned int getHeaderLength();
00087
00088 static unsigned int getHeaderLength(NpType packetType);
00089
00090
00091
00092
00093
00094
00095
00096 void setVersionNumber(int);
00097
00098 void setVcn(unsigned int vcn);
00099
00100 void setPacketType(NpType type);
00101
00102 static int parseMiniHeader(unsigned char* rawData, unsigned int* rawLength,
00103 int* versionNumber, NpType* packetType);
00104
00105 static NpPacket* read(ConnectionInterface*);
00106
00107 int write(ConnectionInterface*);
00108
00109
00110
00111
00112
00113
00114 virtual void toStream(std::ostream&);
00115
00116 void dumpHeader(std::ostream& out);
00117
00118 void dumpData(std::ostream& out);
00119
00120 char* getControlTypeString() const;
00121
00122 protected:
00124 OS_SPEC_ENUM Offset
00125 {
00126 VersionNumberOffset = 0,
00127 PacketTypeOffset = 0,
00128 LengthOffset = 1,
00129 VcnOffset = 3
00130 };
00131
00132
00133
00134
00136 NpType m_controlType;
00137
00139 unsigned short m_length;
00140
00142 unsigned int m_vcn;
00143
00145 unsigned char *m_data;
00146
00148 unsigned char *m_rawData;
00149
00151 unsigned int m_headerLength;
00152
00154 int m_versionNumber;
00155
00156 };
00157
00158 #endif