Exemplo n.º 1
0
        private void HandleRecvData(NetRecvData recvData)
        {
            List <IServerMsg> lst;

            m_dicServerMsg.TryGetValue(recvData.recvOpcode, out lst);
            if (lst != null)
            {
                for (int i = 0; i < lst.Count; i++)
                {
                    lst[i].HandleMsg(recvData.data);
                }
            }
            m_cMsgDispatcher.Dispatch(recvData.recvOpcode, recvData.data);
        }
Exemplo n.º 2
0
 public override void OnUpdate()
 {
     base.OnUpdate();
     //一般消息处理
     while (m_queueSend.Count > 0)
     {
         NetRecvData recvData = new NetRecvData();
         var         sendData = m_queueSend.Dequeue();
         recvData.recvOpcode = sendData.sendOpcode;
         recvData.data       = sendData.data;
         recvData.len        = 0;//这里的长度直接填0(不需要反序列化)
         m_queueRecv.Enqueue(recvData);
     }
     //每隔50毫秒一次帧同步
     m_fTime += Time.deltaTime;
     if (m_fTime >= FrameSpaceTime)
     {
         m_fTime -= FrameSpaceTime;
         m_nFrameIndex++;
         //获取到所有的帧包
         NetRecvData recvFrameData = new NetRecvData();
         recvFrameData.data       = m_nFrameIndex;
         recvFrameData.recvOpcode = 0;
         int len = m_queueFrameSend.Count;
         if (len > 255)
         {
             len = 255;
         }
         recvFrameData.len = (short)len;
         m_queueRecv.Enqueue(recvFrameData);
         for (int i = 0; i < len; i++)
         {
             NetRecvData recvData = new NetRecvData();
             var         sendData = m_queueFrameSend.Dequeue();
             recvData.recvOpcode = sendData.sendOpcode;
             recvData.data       = sendData.data;
             recvData.len        = 0;//这里的长度直接填0(不需要反序列化)
             m_queueRecv.Enqueue(recvData);
         }
     }
 }
Exemplo n.º 3
0
 public void OnUpdate()
 {
     //处理网络事件
     if (m_cSocketClient != null && m_cSocketClient.Status != SocketClientStatus.DisConnect)
     {
         m_cSocketClient.OnUpdate();
         if (m_cSocketClient.Status == SocketClientStatus.Connected)
         {
             int count = m_cSocketClient.RecvNetData(m_queueRecvData);
             //限制每帧处理包的最大个数可以在这里加
             while (m_queueRecvData.Count > 0)
             {
                 NetRecvData recvData = m_queueRecvData.Dequeue();
                 //如果当前非帧包,直接运行
                 if (!m_cFrameData.AddRecvData(recvData))
                 {
                     HandleRecvData(recvData);
                 }
             }
         }
     }
 }
Exemplo n.º 4
0
 public bool AddRecvData(NetRecvData recvData)
 {
     if (recvData.recvOpcode == 0)
     {
         m_nCurLeaveRecvCount = recvData.len;
         m_nRecvFrameIndex    = (int)recvData.data;
         if (m_nCurLeaveRecvCount > 0)
         {
             List <NetRecvData> queue = GetList();
             m_dicData.Add(m_nRecvFrameIndex, queue);
         }
         else
         {
             m_nRecvFrameIndex++;
         }
         return(true);
     }
     else
     {
         if (m_nCurLeaveRecvCount > 0)
         {
             try
             {
                 m_dicData[m_nRecvFrameIndex].Add(recvData);
                 m_nCurLeaveRecvCount--;
                 if (m_nCurLeaveRecvCount == 0)
                 {
                     m_nRecvFrameIndex++;
                 }
             }
             catch (Exception e)
             {
                 CLog.LogError("接收帧消息顺序发生错误,当前包将丢失,opcode=" + recvData.recvOpcode);
             }
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 5
0
 private void Run()
 {
     while (!m_bStop)
     {
         try
         {
             int opcodeCount = m_cSocket.Receive(m_cBuffer, 0, 2, SocketFlags.None);
             if (CheckReceiveZero(opcodeCount))
             {
                 break;
             }
             short       opcode  = BitConverter.ToInt16(m_cBuffer, 0);
             NetRecvData netData = new NetRecvData();
             //如果是帧包
             if (opcode < 256)
             {
                 netData.recvOpcode = 0;
                 netData.len        = opcode;
                 int frameIndexCount = m_cSocket.Receive(m_cBuffer, 0, 4, SocketFlags.None);
                 if (CheckReceiveZero(frameIndexCount))
                 {
                     break;
                 }
                 netData.data = BitConverter.ToInt32(m_cBuffer, 0);
                 // CLog.Log(string.Format("收到帧包frameCount={0},frameIndex={1},buff={2}",netData.len,netData.data,BitConverter.ToString(m_cBuffer,0,4)));
             }
             else
             {
                 netData.recvOpcode = opcode;
                 //读取长度
                 int lenCount = m_cSocket.Receive(m_cBuffer, 0, 2, SocketFlags.None);
                 if (CheckReceiveZero(lenCount))
                 {
                     break;
                 }
                 netData.len = BitConverter.ToInt16(m_cBuffer, 0);
                 int dataLen = m_cSocket.Receive(m_cBuffer, 0, (int)netData.len, SocketFlags.None);
                 if (CheckReceiveZero(dataLen))
                 {
                     break;
                 }
                 //反序列化
                 try {
                     Type type = m_dicOpcodeToType[netData.recvOpcode];
                     if (ProtoBuf.Serializer.NonGeneric.CanSerialize(type))
                     {
                         m_cStream.Position = 0;
                         m_cStream.Write(m_cBuffer, 0, netData.len);
                         m_cStream.Position = 0;
                         netData.data       = ProtoBuf.Serializer.NonGeneric.Deserialize(type, m_cStream);
                     }
                 }
                 catch (Exception ex)
                 {
                     CLog.LogError("SocketReceiver Deserialize fail!opcode=" + netData.recvOpcode);
                     continue;
                 }
             }
             lock (m_queueData)
             {
                 m_queueData.Enqueue(netData);
             }
         }
         catch (Exception e)
         {
             //忽略WSACancelBlockingCall异常
             if (!e.Message.Contains("WSACancelBlockingCall"))
             {
                 CLog.LogError("SocketReceiver exception:" + e.Message + "\n" + e.StackTrace);
             }
             m_bLostConnect = true;
             break;
         }
     }
 }