private string RecvAll(ref HeaderBytes msgHeader, ref DataBytes msgData, ref int nRecved, ref bool bWaiting) { string result; if (msgData == null) { SocketError socketError; int recveNum = m_Socket.Receive(msgHeader.Bytes, nRecved, msgHeader.Bytes.Length - nRecved, SocketFlags.None, out socketError); if (recveNum < 0) { result = "消息头小于0"; return(result); } if (socketError != SocketError.Success && socketError != SocketError.WouldBlock) { result = "网络错误:" + socketError.ToString(); m_ConnectState = EConnectState.PostConnecting; m_bNetStateChanged = true; //GameDll.CGameProcedure.s_EventManager.OnNetStateChanged.SafeInvoke((int)NetworkProtol.Tcp, (int)m_ConnectState); return(result); } if (recveNum == 0) { bWaiting = true; result = null; return(result); } nRecved += recveNum; if (nRecved == msgHeader.Bytes.Length) { ushort msgType = 0; ushort msgLength = 0; if (!WfPacket.ParseHeader(msgHeader.Bytes, ref msgType, ref msgLength)) { result = string.Concat(new object[] { "error ParseHeader type:", msgType, "len:", msgLength }); return(result); } if (msgLength < msgHeader.Bytes.Length) { result = string.Concat(new object[] { "error ParseHeader < msglen:", msgLength, "headerLength:", msgHeader.Bytes.Length }); return(result); } if (msgLength == msgHeader.Bytes.Length) { //这里虽然只有一个是一个很简单的只带有消息头的消息,例如心跳,但是我依然用了完整消息大小 msgData = PooledClassManager <DataBytes> .CreateClass(); msgData.SetBytes(msgLength + 1); Array.Copy(msgHeader.Bytes, 0, msgData.GetBytes(), 0, msgHeader.Bytes.Length); WfPacket item = PooledClassManager <WfPacket> .CreateClass(); item.InitRead(msgData); msgData = null; nRecved = 0; object privateLockObject; Monitor.Enter(privateLockObject = m_PrivateLockObject); try { m_RecvQueue.Enqueue(item); } finally { Monitor.Exit(privateLockObject); } } else { msgData = PooledClassManager <DataBytes> .CreateClass(); msgData.SetBytes(msgLength); Array.Copy(msgHeader.Bytes, 0, msgData.GetBytes(), 0, msgHeader.Bytes.Length); nRecved = msgHeader.Bytes.Length; } } } if (msgData != null) { SocketError socketError; int recveNum = m_Socket.Receive(msgData.GetBytes(), nRecved, msgData.GetLength() - nRecved, SocketFlags.None, out socketError); //Debug.Log("底层函数接收数据:" + socketError.ToString()); if (recveNum < 0) { result = "ReceiveData < 0"; return(result); } if (socketError != SocketError.Success && socketError != SocketError.WouldBlock) { result = "ReceiveData Failed"; return(result); } if (recveNum == 0) { bWaiting = true; result = null; return(result); } nRecved += recveNum; if (nRecved > msgData.GetLength()) { result = "ReceiveData IO error"; return(result); } if (nRecved == msgData.GetLength()) { WfPacket item = PooledClassManager <WfPacket> .CreateClass(); item.InitRead(msgData); msgData = null; nRecved = 0; object privateLockObject; Monitor.Enter(privateLockObject = m_PrivateLockObject); try { m_RecvQueue.Enqueue(item); } finally { Monitor.Exit(privateLockObject); } } else { bWaiting = true; } } result = null; return(result); }
private static string RecvAll(DP_Msg data, ref byte[] msgHeader, ref byte[] msgData, ref int nRecved, ref bool bWaiting) { string result; if (msgData == null) { SocketError socketError; int receivedCount = data.clientSocket.Receive(msgHeader, nRecved, msgHeader.Length - nRecved, SocketFlags.None, out socketError); if (receivedCount < 0) { result = "ReceiveHeader < 0"; return(result); } if (socketError != SocketError.Success && socketError != SocketError.WouldBlock) { result = "ReceiveHeader Failed"; return(result); } if (receivedCount == 0) { bWaiting = true; result = null; return(result); } nRecved += receivedCount; if (nRecved == msgHeader.Length) { ushort msgType = 0; ushort msgLength = 0; if (!WfPacket.ParseHeader(msgHeader, ref msgType, ref msgLength)) { result = string.Concat(new object[] { "error ParseHeader type:", msgType, "len:", msgLength }); return(result); } if (msgLength < msgHeader.Length) { result = string.Concat(new object[] { "error ParseHeader < msglen:", msgLength, "headerLength:", msgHeader.Length }); return(result); } if (msgLength == msgHeader.Length) { msgData = new byte[msgLength + 1]; Array.Copy(msgHeader, 0, msgData, 0, msgHeader.Length); WfPacket item = new WfPacket(msgData); msgData = null; nRecved = 0; lock (data.m_Lock) { data.m_RecvQueue.Enqueue(item); } } else { msgData = new byte[msgLength]; Array.Copy(msgHeader, 0, msgData, 0, msgHeader.Length); nRecved = msgHeader.Length; } } } if (msgData != null) { SocketError socketError; int num = data.clientSocket.Receive(msgData, nRecved, msgData.Length - nRecved, SocketFlags.None, out socketError); if (num < 0) { result = "ReceiveData < 0"; return(result); } if (socketError != SocketError.Success && socketError != SocketError.WouldBlock) { result = "ReceiveData Failed"; return(result); } if (num == 0) { bWaiting = true; result = null; return(result); } nRecved += num; if (nRecved > msgData.Length) { result = "ReceiveData IO error"; return(result); } if (nRecved == msgData.Length) { WfPacket item = new WfPacket(msgData); msgData = null; nRecved = 0; lock (data.m_Lock) { data.m_RecvQueue.Enqueue(item); } } else { bWaiting = true; } } result = null; return(result); }