public void OnUpdate() { while (true) { if (m_ReceiveCount < 5) { ++m_ReceiveCount; lock (m_ReceiveQueue) { if (m_ReceiveQueue.Count > 0) { byte[] buffer = m_ReceiveQueue.Dequeue(); byte[] protocodeBuffer = new byte[4]; byte[] protoContent = new byte[buffer.Length - 5]; bool isCompress = false; using (MemoryStreamExt ms = new MemoryStreamExt(buffer)) { isCompress = ms.ReadBool(); ms.Read(protocodeBuffer, 0, protocodeBuffer.Length); Array.Reverse(protocodeBuffer); int protoCode = BitConverter.ToInt32(protocodeBuffer, 0); //LogSystem.Log("=============================================================收到消息" + protoCode); ms.Read(protoContent, 0, protoContent.Length); if (isCompress) { //protoContent = GZipCompress.DeCompress(protoContent); } // if (protoCode == OP_PLAYER_CLOSE.CODE) { // LogSystem.LogSpecial("服务器返回关闭消息,网络连接断开"); Close(true); } //else if (protoCode == OP_SYS_HEART.CODE) { // lastHeart = OP_SYS_HEART.decode(protoContent); } // else { //return; // NetDispatcher.Instance.Dispatch(protoCode, protoContent); } } } else { break; } } } else { m_ReceiveCount = 0; break; } } m_SocketStateDic[m_CurrentState].OnUpdate(); }
/// <summary> /// 接收数据回调 /// </summary> /// <param name="ar"></param> private void ReceiveCallBack(IAsyncResult ar) { if (Socket == null || !Socket.Connected) { //Debug.Log("接收数据回调返回"); return; } try { int nLength = Socket.EndReceive(ar); //Debug.Log("从套接字缓冲区读取了" + nLength.ToString() + "长度的数据"); if (nLength > 0) { m_ReceiveMemoryStream.Position = m_ReceiveMemoryStream.Length; m_ReceiveMemoryStream.Write(m_ReceiveBuffer, 0, nLength); if (m_ReceiveMemoryStream.Length > DATA_HEAD_LENGTH) { while (true) { m_ReceiveMemoryStream.Position = 0; m_ReceiveMemoryStream.Read(m_HeadData, 0, DATA_HEAD_LENGTH); int currentMsgLen = ((m_HeadData[0] & 0xff) << 24) + ((m_HeadData[1] & 0xff) << 16) + ((m_HeadData[2] & 0xff) << 8) + (m_HeadData[3] & 0xff); int currentFullMsgLen = DATA_HEAD_LENGTH + currentMsgLen; if (m_ReceiveMemoryStream.Length >= currentFullMsgLen) // 说明至少接收到了一个整包 { Debug.Log("客户端收到了一个" + currentFullMsgLen + "长度的消息"); byte[] buffer = new byte[currentMsgLen]; m_ReceiveMemoryStream.Position = DATA_HEAD_LENGTH; m_ReceiveMemoryStream.Read(buffer, 0, currentMsgLen); //buffer = EncryptUtil.NetEncrypt(buffer, SystemProxy.Instance.NetKey, SystemProxy.Instance.NetCorrected); m_ReceiveQueue.Enqueue(buffer); //===============处理剩余字节数组====================== int remainLen = (int)m_ReceiveMemoryStream.Length - currentFullMsgLen; if (remainLen > 0) { m_ReceiveMemoryStream.Position = currentFullMsgLen; byte[] remainBuffer = new byte[remainLen]; m_ReceiveMemoryStream.Read(remainBuffer, 0, remainLen); m_ReceiveMemoryStream.Position = 0; m_ReceiveMemoryStream.SetLength(0); m_ReceiveMemoryStream.Write(remainBuffer, 0, remainBuffer.Length); remainBuffer = null; } else // 没有剩余字节 { m_ReceiveMemoryStream.Position = 0; m_ReceiveMemoryStream.SetLength(0); break; } } else { break; } } } ReceiveMessage(); } else { Debug.LogWarning(string.Format("{0}断开连接", m_isClientClose ? "客户端断的" : "服务器断的")); Close(false); } } catch (Exception ex) { Debug.LogWarning(string.Format("{0}断开连接,{1}", m_isClientClose ? "客户端断的" : "服务器断的", ex)); Close(false); } }