//------------------------------------------------------------------------- /// <summary> /// 读取包体数据 /// </summary> /// <returns></returns> private bool __ReadPacketBody() { if (m_ReceiveHead == null) { Debug.LogError("NetTCPSocketConnect::__ReadPacketBody m_ReceiveHead is null"); return false; } // 获取消息头里的消息id和包身上度 Int16 buffersize = BitConverter.ToInt16(m_ReceiveHead, SNetPacketCommon.PACK_LENGTH_OFFSET); Int16 messageid = BitConverter.ToInt16(m_ReceiveHead, SNetPacketCommon.PACK_MESSSAGEID_OFFSET); Int32 bodysize = buffersize - SNetPacketCommon.PACK_HEAD_SIZE; if (bodysize <= 0) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody receive empty pack message id:" + messageid); return true; } SocketNetPacket netPacket = new SocketNetPacket(messageid, bodysize); ///设置包头数据 if (false == netPacket.SetPackHead(m_ReceiveHead)) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody receive headis error"); return false; } int nBufferSize = 0; Byte[] packBuffer = netPacket.GetBuffer(out nBufferSize); try { if (null == m_Socket || false == m_Socket.Connected) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody m_Socket==null|| m_Socket.Connected==fales "); return false; } Int32 receiveSize = m_Socket.Receive(packBuffer, SNetPacketCommon.PACK_HEAD_SIZE, bodysize, SocketFlags.None); if (receiveSize == 0) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody readPackBody read 0 data will close connect"); return false; } // 当要接受的数据超过包的大小时,将消息截断,再接收 while (receiveSize < bodysize) { if (null == m_Socket || false == m_Socket.Connected) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody m_Socket==null|| m_Socket.Connected==falsewww eeee"); return false; } Int32 temsendcount = m_Socket.Receive(packBuffer, receiveSize + SNetPacketCommon.PACK_HEAD_SIZE, bodysize - receiveSize, SocketFlags.None); if (temsendcount == 0) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody readPackBody read 0 data will close connect"); return false; } receiveSize += temsendcount; } m_unRecvTotalBytes += (uint)receiveSize; // 加入收包的消息队列 // lock is begin in here lock (m_ReceiveObject) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody Recv Full Data Len = " + nBufferSize); m_ReceivePackList.AddLast(netPacket); } // unlock is done in here return true; } catch (System.Exception e) { Debug.LogError("NetTCPSocketConnect::__ReadPacketBody Socket receive error: " + e.ToString()); DisConnection(); return false; } }
//------------------------------------------------------------------------- /// <summary> /// 读取包体数据 /// </summary> /// <returns></returns> private bool __ReadPacketBody() { if (m_ReceiveHead == null) { Debug.LogError("NetTCPSocketConnect::__ReadPacketBody m_ReceiveHead is null"); return(false); } // 获取消息头里的消息id和包身上度 Int16 buffersize = BitConverter.ToInt16(m_ReceiveHead, SNetPacketCommon.PACK_LENGTH_OFFSET); Int16 messageid = BitConverter.ToInt16(m_ReceiveHead, SNetPacketCommon.PACK_MESSSAGEID_OFFSET); Int32 bodysize = buffersize - SNetPacketCommon.PACK_HEAD_SIZE; if (bodysize <= 0) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody receive empty pack message id:" + messageid); return(true); } SocketNetPacket netPacket = new SocketNetPacket(messageid, bodysize); ///设置包头数据 if (false == netPacket.SetPackHead(m_ReceiveHead)) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody receive headis error"); return(false); } int nBufferSize = 0; Byte[] packBuffer = netPacket.GetBuffer(out nBufferSize); try { if (null == m_Socket || false == m_Socket.Connected) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody m_Socket==null|| m_Socket.Connected==fales "); return(false); } Int32 receiveSize = m_Socket.Receive(packBuffer, SNetPacketCommon.PACK_HEAD_SIZE, bodysize, SocketFlags.None); if (receiveSize == 0) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody readPackBody read 0 data will close connect"); return(false); } // 当要接受的数据超过包的大小时,将消息截断,再接收 while (receiveSize < bodysize) { if (null == m_Socket || false == m_Socket.Connected) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody m_Socket==null|| m_Socket.Connected==falsewww eeee"); return(false); } Int32 temsendcount = m_Socket.Receive(packBuffer, receiveSize + SNetPacketCommon.PACK_HEAD_SIZE, bodysize - receiveSize, SocketFlags.None); if (temsendcount == 0) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody readPackBody read 0 data will close connect"); return(false); } receiveSize += temsendcount; } m_unRecvTotalBytes += (uint)receiveSize; // 加入收包的消息队列 // lock is begin in here lock (m_ReceiveObject) { Debug.Log("NetTCPSocketConnect::__ReadPacketBody Recv Full Data Len = " + nBufferSize); m_ReceivePackList.AddLast(netPacket); } // unlock is done in here return(true); } catch (System.Exception e) { Debug.LogError("NetTCPSocketConnect::__ReadPacketBody Socket receive error: " + e.ToString()); DisConnection(); return(false); } }