• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/net/PacketBuilder.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
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         //IPacketListener *m_listener;
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(/*IPacketListener *listener*/);
00075         virtual ~PacketBuilder();
00076 
00077         //inline void SetListener(IPacketListener *listener) { m_listener = listener; }
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