⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netmessage.h

📁 这是一个简单的使用WinAPI基于WinSock的ICP/IP程序
💻 H
📖 第 1 页 / 共 2 页
字号:
        virtual                  void   EmptyBuffer();

protected:
// reads the message members from m_pData
        virtual                  void   ReadBody();
// reads m_header from CNetStream*, then creates a message
// and reads data of the message from the stream
		static                   void   Read(CNetStream*, CNetMsg*&);
        virtual                  void   ReadChildren(CNetStream*);
// reads the message members from the prototype
        virtual                  void   ReadBody(const CNetMsg&);
// write the message members into m_pData
        virtual                  void   WriteBody();
// writes its member variables into the preallocated m_pData.
// The size of the data buffer is known by calling GetMsgSize()
// this calls m_header.Write()
        virtual                  void   Write(CNetStream*);
		virtual                  void   WriteChildren(CNetStream*);
protected:
  CNetMsgHdr m_header;
};

// a list of arbitrary CNetMsg derivables
class CNetList : public CNetMsg
{
public:
  struct MSG {
     ulong  m_nElemN;
  };
protected:
// define the type of callback function
  typedef bool (*DataCallBack)(void*, CNetList*);
public:
                                        CNetList();
                                        CNetList(const CNetList&);
// resets member variables (destroys m_pData)
		virtual                        ~CNetList();

        virtual           CNetObject&   operator=(const CNetObject& obj);
        virtual                  bool   operator==(const CNetObject& obj);
        virtual              CNetMsg*   Clone() const { return new CNetList;}

// if this is a complex class (aggregates other CNetMsg derivables) return true
		virtual                  bool   HasChildren() const;

		                         ulong  GetElemN() const
								 { return m_vecElem.size(); }

							  CNetMsg*  GetElem(ulong n)
							  { if (m_vecElem.size() > 0 && m_vecElem.size() > n)
							      return m_vecElem[n]; 
							    return 0;
							  }

                                 void   SetMaxElem(ulong nElemN)
								 { m_data.m_nElemN = nElemN; }
                                 ulong  GetMaxElemN() const
								 { return m_data.m_nElemN; }

                                 bool   AddElem(CNetMsg*);
                                 void   DeleteAllElem();

// allows to a data provider with postponeded data supply
                                 bool   SetDataRequestCallback(void*, DataCallBack);
                                 bool   SetDataArrivedCallback(void*, DataCallBack);

// returns the size in bytes of members of this message that can be sent accross the network
		                        ulong   GetSize_Write() const {return GetSize_ulong();}
                                ulong   GetSize_Read() const  {return GetSize_ulong();}

// returns the class id as defined in the "NetMsgNames.h" file
        virtual                 ulong   GetClassId() const {return ciNetList;}
                                void    Dump(ostream&);
protected:
// reads the message members from m_pData
        virtual                  void   ReadBody();
		virtual                  void   ReadChildren(CNetStream*);
// write the message members into m_pData
        virtual                  void   WriteBody();
		virtual                  void   WriteChildren(CNetStream*);
protected:
               MSG m_data;
  vector<CNetMsg*> m_vecElem;
      DataCallBack m_pfnOutOfData;
			 void* m_pCaller;
};

// a packet of raw data up to MAX_NET_PACKET in size
class CNetPacket : public CNetMsg
{
public:
        struct MSG {
		  ulong m_nSize;
          char  m_szPacket[MAX_NET_PACKET];
		  ulong m_nSeqN;
        };

public:
                                                CNetPacket();
                                                CNetPacket(ulong, ulong, MSG);
                                                CNetPacket(const char*, ulong, ulong);

        virtual                                 ~CNetPacket() {}

        virtual                      CNetMsg*   Clone() const {return new CNetPacket;}

        virtual                   CNetObject&   operator=(const CNetObject& obj);
        virtual                          bool   operator==(const CNetObject& obj);

		virtual                          bool   HasChildren() const {return false;}

                                   const MSG&   GetMSG() const {return m_data;}

// returns the class id as defined in the "NetMsgNames.h" file
                                ulong           GetClassId() const {return ciNetPacket;}

// returns the size in bytes of members of this message that can be sent accross the network
                                ulong           GetSize_Write() const;
					static		ulong           GetMaxSize_Packet() {return MAX_NET_PACKET;}

// dumps the data into the ostream
                                void            Dump(ostream&);
protected:
// reads the message members from m_pData
                                void            ReadBody();
// reads the message members from the prototype
                                void            ReadBody(const CNetMsg&);
// write the message members into m_pData
                        void                    WriteBody();
public:
                               void             SetData(const char*, ulong, ulong);
                        const char*             GetData() const {return m_data.m_szPacket;}
						      ulong             GetSize() const {return m_data.m_nSize;}
							  ulong             GetSeqN() const {return m_data.m_nSeqN;}

protected:
        MSG     m_data;
};

class CNetStatus : public CNetMsg
{
public:
        struct MSG {
          ulong                 m_nTextSz;
          string                m_strText;
          ulong                 m_nCode;
        };

public:
                                        CNetStatus();
                                        CNetStatus(ulong, ulong, MSG);
        virtual                        ~CNetStatus();

        virtual              CNetMsg*   Clone() const {return new CNetStatus;}


        virtual            CNetObject&  operator=(const CNetObject& obj);
        virtual                  bool   operator==(const CNetObject& obj);

		virtual                  bool   HasChildren() const {return false;}

                            const MSG&  GetMSG() const {return m_data;}

// returns the class id as defined in the "NetMsgNames.h" file
                                ulong   GetClassId() const {return ciNetStatus;}
// returns the size in bytes of members of this message that can be sent accross the network
        virtual                 ulong   GetSize_Write() const;

// dumps the data into the ostream
                                 void   Dump(ostream&);
protected:
// reads the message members from m_pData
                                 void   ReadBody();
// reads the message members from the prototype
                                 void   ReadBody(const CNetMsg&);
// write the message members into m_pData
                                 void   WriteBody();

public:
                                 void   SetCode(ulong);
                                 void   SetText(const char*);
                                ulong   GetCode() const {return m_data.m_nCode;}
                           const char*  GetText() const {return m_data.m_strText.c_str();}
protected:
        MSG                             m_data;
};

class CMsgFactory
{
public:
                         CMsgFactory() {}
  virtual               ~CMsgFactory() {}

  virtual   CNetMsg*     CreateMessage(long) = 0;
};

extern CMsgFactory*  g_getDefaultMsgFactory();

#endif // !defined(AFX_NETMsg_H__C4F3191C_99C0_454A_BFAB_CE57E3CAC7A9__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -