00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _packetbuilder_h
00018 #define _packetbuilder_h
00019
00020 #include <spl/net/Packet.h>
00021
00022
00029 class PacketBuilder;
00030 typedef RefCountPtrCast<PacketBuilder, IStreamReadListener, IStreamReadListenerPtr> PacketBuilderPtr;
00031
00032 REGISTER_TYPEOF(30, PacketBuilderPtr);
00033
00036 class PacketBuilder : public IStreamReadListener
00037 {
00038 protected:
00039 enum PacketReadState
00040 {
00041 PKT_ENDIAN,
00042 PKT_SIZE_MCB,
00043 PKT_SIZE_LCB,
00044 PKT_DT,
00045 PKT_DT_LEN_MCB,
00046 PKT_DT_LEN_LCB,
00047 PKT_CHARSIZE,
00048 PKT_DATA,
00049 PKT_ERROR,
00050 PKT_CLOSED
00051 };
00052
00053
00054 PacketListenerDelegateDispatch m_listeners;
00055 enum PacketReadState m_state;
00056
00057 bool m_isLittleEndian;
00058 bool m_revbytes;
00059 int16 m_pktsize;
00060 char m_datatype;
00061 int16 m_datalen;
00062 int8 m_charsize;
00063 Vector<byte> m_buf;
00064 int m_readPos;
00065
00066 void _ParseData();
00067 StringPtr _ParseString();
00068
00069 virtual void IStreamRead_OnRead(const Array<byte>& buf, int len);
00070 virtual void IStreamRead_OnError(const String& msg);
00071 virtual void IStreamRead_OnClose();
00072
00073 public:
00074 PacketBuilder();
00075 virtual ~PacketBuilder();
00076
00077
00078 inline PacketListenerDelegateDispatch& Delegates() { return m_listeners; }
00079
00080 #if defined(DEBUG) || defined(_DEBUG)
00081 virtual void CheckMem() const;
00082 virtual void ValidateMem() const;
00083 #endif
00084 };
00085
00086 REGISTER_TYPEOF(32, PacketBuilder);
00087
00090 #endif