00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _packet_h
00018 #define _packet_h
00019
00020 #include <spl/types.h>
00021 #include <spl/Debug.h>
00022
00023 #include <spl/collection/Array.h>
00024 #include <spl/Date.h>
00025 #include <spl/DateTime.h>
00026 #include <spl/Exception.h>
00027 #include <spl/Memory.h>
00028 #include <spl/RefCountPtr.h>
00029 #include <spl/String.h>
00030 #include <spl/net/Socket.h>
00031 #include <spl/io/Stream.h>
00032 #include <spl/io/StreamReadPump.h>
00033 #include <spl/collection/Vector.h>
00034
00041 class Packet;
00042 typedef RefCountPtr<Packet> PacketPtr;
00043
00044 REGISTER_TYPEOF(20, PacketPtr);
00045
00049 class Packet : public IMemoryValidate
00050 {
00051 protected:
00052 Array<byte> m_abuf;
00053 Vector<byte> m_buf;
00054 bool m_readPacketReady;
00055 int m_readpos;
00056 bool m_readRevByteOrder;
00057 int16 m_rpacketsize;
00058
00059 inline byte _NextByte() { if (m_readpos >= m_buf.Count()) throw new PacketUnderflowException(); return m_buf.ElementAt(m_readpos++); }
00060
00061 void _AppendRaw( byte i );
00062 void _AppendRaw( int16 i );
00063 void _AppendRaw( int32 i );
00064 void _AppendRaw( int64 i );
00065
00066 public:
00067 Packet();
00068 Packet(const Packet& pkt);
00069 virtual ~Packet();
00070
00071 Packet& operator =(const Packet& pkt);
00072
00073 void Clear();
00074 void SendPacket(spl::IStream& sock);
00075 void ReadPacket(spl::IStream& sock);
00076
00077 void Append(int64 i);
00078 void Append(int32 i);
00079 void Append(int16 i);
00080 void Append(byte i);
00081 void Append(bool b);
00082
00083 void Append(double d);
00084 void Append(const DateTime& dtm);
00085 void Append(const Date& dt);
00086 inline void Append(const String& str) { Append(str.GetChars(), str.Length()); }
00087 inline void Append(const char *str) { Append(str, (int)strlen(str)); }
00088 void Append(const char *str, int len);
00089 void Append(const Array<byte>& buf, int start, int len);
00090
00091 inline void Append(const Array<byte>& buf, int len)
00092 {
00093 Append(buf, 0, len);
00094 }
00095
00096 inline void Append(const Array<byte>& buf)
00097 {
00098 Append(buf, 0, buf.Count());
00099 }
00100
00101 int PacketSize();
00102 int64 ReadInt64();
00103 int32 ReadInt32();
00104 int16 ReadInt16();
00105 byte ReadInt8();
00106 bool ReadBool();
00107
00108 double ReadDouble();
00109 StringPtr ReadString();
00110 byte *ReadBuf(int len);
00111 DateTime ReadDateTime();
00112 Date ReadDate();
00113
00114 #if defined(DEBUG) || defined(_DEBUG)
00115 void CheckMem() const;
00116 void ValidateMem() const;
00117 #endif
00118 };
00119
00120 REGISTER_TYPEOF(22, Packet);
00121
00122 class IPacketListener;
00123 typedef RefCountPtr<IPacketListener> IPacketListenerPtr;
00124
00125 REGISTER_TYPEOF(24, IPacketListenerPtr);
00126
00127 class IPacketListener
00128 {
00129 public:
00130 IPacketListener() {}
00131 virtual ~IPacketListener();
00132
00133 virtual void IPacket_OnData(int64 i) = 0;
00134 virtual void IPacket_OnData(int32 i) = 0;
00135 virtual void IPacket_OnData(int16 i) = 0;
00136 virtual void IPacket_OnData(byte i) = 0;
00137 virtual void IPacket_OnData(bool b) = 0;
00138 virtual void IPacket_OnData(double d) = 0;
00139 virtual void IPacket_OnData(StringPtr str) = 0;
00140 virtual void IPacket_OnData(const Vector<byte>& data, const int len) = 0;
00141 virtual void IPacket_OnData(const DateTime& dtm) = 0;
00142 virtual void IPacket_OnData(const Date& i) = 0;
00143
00144 virtual void IStreamRead_OnError(const String& msg) = 0;
00145 virtual void IStreamRead_OnClose() = 0;
00146 };
00147
00148 REGISTER_TYPEOF(26, IPacketListener);
00149
00150 class PacketListenerDelegateDispatch : public IMemoryValidate
00151 {
00152 private:
00153 DelegateDispatchOneParameter<int64> m_onDataInt64;
00154 DelegateDispatchOneParameter<int32> m_onDataInt32;
00155 DelegateDispatchOneParameter<int16> m_onDataInt16;
00156 DelegateDispatchOneParameter<byte> m_onDataByte;
00157 DelegateDispatchOneParameter<bool> m_onDataBool;
00158 DelegateDispatchOneParameter<double> m_onDataDouble;
00159 DelegateDispatchOneParameter<StringPtr> m_onDataString;
00160 DelegateDispatchTwoParameter<const Vector<byte>&, const int> m_onDataBin;
00161 DelegateDispatchOneParameter<const DateTime&> m_onDataDtm;
00162 DelegateDispatchOneParameter<const Date&> m_onDataDate;
00163 DelegateDispatchOneParameter<const String&> m_onError;
00164 DelegateDispatch m_onClose;
00165
00166 public:
00167 inline PacketListenerDelegateDispatch()
00168 : m_onDataInt32()
00169 {
00170 }
00171
00172 virtual ~PacketListenerDelegateDispatch();
00173
00174 inline DelegateDispatchOneParameter<int64>& OnDataInt64() { return m_onDataInt64; }
00175 inline DelegateDispatchOneParameter<int32>& OnDataInt32() { return m_onDataInt32; }
00176 inline DelegateDispatchOneParameter<int16>& OnDataInt16() { return m_onDataInt16; }
00177 inline DelegateDispatchOneParameter<byte>& OnDataByte() { return m_onDataByte; }
00178 inline DelegateDispatchOneParameter<bool>& OnDataBool() { return m_onDataBool; }
00179 inline DelegateDispatchOneParameter<double>& OnDataDouble() { return m_onDataDouble; }
00180 inline DelegateDispatchOneParameter<StringPtr>& OnDataString() { return m_onDataString; }
00181 inline DelegateDispatchTwoParameter<const Vector<byte>&, const int>& OnDataBinary() { return m_onDataBin; }
00182 inline DelegateDispatchOneParameter<const DateTime&>& OnDataDateTime() { return m_onDataDtm; }
00183 inline DelegateDispatchOneParameter<const Date&>& OnDataDate() { return m_onDataDate; }
00184 inline DelegateDispatchOneParameter<const String&>& OnError() { return m_onError; }
00185 inline DelegateDispatch& OnClose() { return m_onClose; }
00186
00187 inline void DispatchOnData(int64 i) { m_onDataInt64.Call(i); }
00188 inline void DispatchOnData(int32 i) { m_onDataInt32.Call(i); }
00189 inline void DispatchOnData(int16 i) { m_onDataInt16.Call(i); }
00190 inline void DispatchOnData(byte i) { m_onDataByte.Call(i); }
00191 inline void DispatchOnData(bool i) { m_onDataBool.Call(i); }
00192 inline void DispatchOnData(double i) { m_onDataDouble.Call(i); }
00193 inline void DispatchOnData(StringPtr i) { m_onDataString.Call(i); }
00194 inline void DispatchOnData(const Vector<byte>& v, const int len) { m_onDataBin.Call(v, len); }
00195 inline void DispatchOnData(const DateTime& dtm) { m_onDataDtm.Call(dtm); }
00196 inline void DispatchOnData(const Date& dt) { m_onDataDate.Call(dt); }
00197 inline void DispatchOnError(const String& msg) { m_onError.Call(msg); }
00198 inline void DispatchOnClose() { m_onClose.Call(); }
00199
00200 void Add(IPacketListener *l);
00201
00202 #ifdef DEBUG
00203 virtual void ValidateMem() const;
00204 virtual void CheckMem() const;
00205 #endif
00206 };
00207
00208 REGISTER_TYPEOF(28, PacketListenerDelegateDispatch);
00209
00212 #endif