//接收到数据回调执行 private void ReceiveExec() { if (m_ClientSocket == null) { return; } if (m_ConnectState != ConnectStateType.Connected) { return; } int tempLen = m_ClientSocket.Receive(m_TempReceiveData); if (tempLen > 0) { NetMsgHelper.Instance.AddData(m_TempReceiveData, tempLen);//将刚接收到的数据,未处理过的放入网络辅助层 NetMsgPacket tempMsgPacket = new NetMsgPacket(); //一次取一条,直到把辅助层中已经处理过的数据包取完为止 lock (m_MsgQueue) { while (NetMsgHelper.Instance.GetOneMsg(ref tempMsgPacket))//一次取一条,直到把辅助层中已经处理过的数据包取完为止 { if (tempMsgPacket.msgId > 0) { m_MsgQueue.Enqueue(tempMsgPacket); } } } } }
//处理协议数据协程 xlua //这里仅仅是提取了数据到运作层。至于反序列化,以及收到消息的回调,都在在运作层处理。 IEnumerator DealMsgCorLua() { while (true) { lock (m_MsgQueue) { while (m_MsgQueue.Count > 0) { NetMsgPacket tempMsgPacket = m_MsgQueue.Dequeue(); int tempMsgId = tempMsgPacket.msgId; if (tempMsgId <= 0) { ZLogger.Warning("网络数据不正常,不处理此协议,协议ID:{0}", tempMsgId); } else { try { byte[] tempData = tempMsgPacket.data; NetCallMethod("OnMessage", tempMsgId, tempData); if (tempMsgId == 1201) { m_lostHeartTime = 0; //暂定心跳协议ID为1201,重置心跳网络检测 } } catch (Exception e) { OnDisconnect(NetResultType.Exception, "消息报错: 协议Id:" + tempMsgId + " >> " + e.Message + "|" + e.StackTrace); } } } } yield return(null); } }
/// <summary> /// 从缓冲区中,提取出一个数据包 /// </summary> public bool GetOneMsg(ref NetMsgPacket canMsgPacket) { if (m_MsgTotalLen <= 0) { UpdateLength(); } if (m_MsgTotalLen > 0 && m_MsgTotalLen <= m_CurPos) { canMsgPacket.msgId = m_MsgId; canMsgPacket.data = new byte[m_MsgDataLen]; Array.Copy(m_BufferData, NetConst.MSG_HEAD_LEN, canMsgPacket.data, 0, m_MsgDataLen); m_CurPos -= m_MsgTotalLen; byte[] tempBufferData = new byte[m_CurPos > NetConst.TEMP_MSG_SIZE ? m_CurPos : NetConst.TEMP_MSG_SIZE]; Array.Copy(m_BufferData, m_MsgTotalLen, tempBufferData, 0, m_CurPos); m_BufferData = tempBufferData; tempBufferData = null; m_MsgTotalLen = 0; m_MsgDataLen = 0; m_MsgId = 0; return(true); } return(false); }
//处理信息协程 IEnumerator NetWorkDealCor() { while (true) { for (int i = 0; i < m_DealCorCount; i++) { if (m_ReceiveMessageQue.Count > 0) { NetMsgPacket tempNetPacket = m_ReceiveMessageQue.Dequeue(); NetEvenetCallBack tempEventCallBack; if (m_MsgDelegateDic.TryGetValue(tempNetPacket.msgId, out tempEventCallBack)) { if (tempNetPacket.data != null) { tempEventCallBack(tempNetPacket.data); } } } else { break; } } yield return(null); } }
/// <summary> /// 供外部层调用的接收信息 /// </summary> public void OnMsg(NetMsgPacket canMsgPacket) { NetMsgPacket tempMsgPacket = new NetMsgPacket(); tempMsgPacket = canMsgPacket; m_ReceiveMessageQue.Enqueue(tempMsgPacket); }
/// <summary> /// 供外部层调用的发送消息 /// </summary> public void SendMsg(int canMsgID, byte[] canMsgData) { NetMsgPacket tempMsgData = new NetMsgPacket(); tempMsgData.msgId = canMsgID; tempMsgData.data = canMsgData; m_SendMessageQue.Enqueue(tempMsgData); }
//发送信息协程 IEnumerator NetWorkSendCor() { while (true) { if (m_SendMessageQue.Count > 0) { NetMsgPacket tempMsgPacket = m_SendMessageQue.Dequeue(); if (IsConnected() && !isBreakNet) { if (tempMsgPacket.data != null) { NetMgr.Instance.SendMsg(tempMsgPacket.data, tempMsgPacket.msgId); } } else { BreakSocket(); } } yield return(null); } }