GetBuffer() public method

获取消息包数据缓存 - 包括包头和包身
public GetBuffer ( int &nBufferSize ) : Byte[]
nBufferSize int
return Byte[]
Exemplo n.º 1
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// 发送子线程回调函数
        /// </summary>
        private void __SendThreadFunc()
        {
            while (false == m_IPAddrMsg.m_IsNeedClose && true == m_IPAddrMsg.m_IsConnect && null != m_Socket)
            {
                SocketNetPacket sendpack = null;

                //取出消息缓存中最前一条数据
                lock (m_SendObject)
                {
                    if (m_SendPackList.Count != 0)
                    {
                        sendpack = m_SendPackList.First.Value;
                        m_SendPackList.RemoveFirst();
                    }
                }

                //发包给服务器
                if (null != sendpack)
                {
                    try
                    {
                        int    nBufferSize = 0;
                        Byte[] sendBuffer  = sendpack.GetBuffer(out nBufferSize);

                        int sendsize = m_Socket.Send(sendBuffer, 0, nBufferSize, SocketFlags.None);
                        if (sendsize != nBufferSize)
                        {
                            Debug.LogError("NetTCPSocketConnect::__SendThreadFunc send buffer size error:");
                            m_IPAddrMsg.m_IsNeedClose = true;
                            break;
                        }

                        m_unSendTotalBytes += (uint)sendsize;
                    }
                    catch (System.Exception exception)
                    {
                        Debug.LogError("NetTCPSocketConnect::__SendThreadFunc send buffer size error:" + exception.ToString());
                        m_IPAddrMsg.m_IsNeedClose = true;
                        break;
                    }
                }
                else
                {
                    m_ManualSendEvent.WaitOne();
                }
            }

            if (null == m_Socket)
            {
                Debug.Log("NetTCPSocketConnect::__SendThreadFunc m_Socket is null");
            }
            Debug.Log("NetTCPSocketConnect::__SendThreadFunc m_IPAddrMsg.m_IsNeedClose = " + m_IPAddrMsg.m_IsNeedClose);
            Debug.Log("NetTCPSocketConnect::__SendThreadFunc m_IPAddrMsg.m_IsConnect = " + m_IPAddrMsg.m_IsConnect);
            Debug.Log("NetTCPSocketConnect::__SendThreadFunc send message thread is exit...");
        }
Exemplo n.º 2
0
        //-------------------------------------------------------------------------
        /// <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;
            }
        }
Exemplo n.º 3
0
        //-------------------------------------------------------------------------
        /// <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);
            }
        }