public static NetDispatcher Instance()
 {
     if (_instance == null)
     {
         _instance = new NetDispatcher();
     }
     return(_instance);
 }
Esempio n. 2
0
        void dispatchMsgs()
        {
            while (true)
            {
                int nReadSize = m_recvPool.ReadSize;
                int nReadPos  = m_recvPool.ReadPos;
                // 消息没有包头大就推出循环
                if (nReadSize < ConstDefine.NF_PACKET_HEAD_SIZE)
                {
                    break;
                }

                int    offset = 0; // 跳过包头标签定义
                UInt16 nMsgID = BitConverter.ToUInt16(m_recvPool.Buffer, nReadPos + offset);
                offset += sizeof(UInt16);
                UInt32 length = BitConverter.ToUInt32(m_recvPool.Buffer, nReadPos + offset);
                offset += sizeof(UInt32);
                // 消息包还没有完全收完立即退出
                length = ConvertUint32(length);
                nMsgID = ConvertUint16(nMsgID);

                Debug.LogFormat("RecvData {0} \t {1}", length, nMsgID);
                if (nReadSize < length)
                {
                    break;
                }

                m_recvPool.ReadPos += (int)length;

                Debug.LogFormat("Recv:{0}", nMsgID);

                nReadPos += (int)ConstDefine.NF_PACKET_HEAD_SIZE;
                length   -= ConstDefine.NF_PACKET_HEAD_SIZE;

                MemoryStream mem = new MemoryStream(m_recvPool.Buffer, nReadPos, (int)length);
                NetDispatcher.Instance().dispatchMessage(nMsgID, mem);

                if (m_curState != State.CONNECTED)
                {
                    break;
                }
            }
        }
Esempio n. 3
0
 public void AddReceiveCallBack(Msg.EGameMsgID id, MessageDispatcher.MessageHandler netHandler)
 {
     NetDispatcher.Instance().AddReceiveCallBack((UInt16)id, netHandler);
 }