コード例 #1
0
        public UInt16 Write(Byte[] buf)
        {
            UInt16 offset = NetPacketHeader.GetHeadLen();

            if (m_datas == null)
            {
            }
            else
            {
                Array.ConstrainedCopy(m_datas, 0, buf, offset, m_datas.Length);
                offset += (UInt16)m_datas.Length;
            }
            m_header.WriteHead(buf);
            return(offset);
        }
コード例 #2
0
        private bool _TcpRecv()
        {
            int recvlen = 0;

            if (m_pNetPacketHeader.PacketId > 0)
            {
                recvlen = m_pNetPacketHeader.BodyLength;
                //-m_pNetPacketHeader.GetHeadLen();
            }
            else
            {
                recvlen = NetPacketHeader.GetHeadLen();
            }

            if (m_TcpSocket.Available <= 0)
            {
                return(false);
            }

            m_nRecvBufferOffset += m_TcpSocket.Receive(m_Recvbuffer, m_nRecvBufferOffset, recvlen - m_nRecvBufferOffset, 0);

            if (m_nRecvBufferOffset < recvlen)
            {
                Debug.Log("bao ti shu liang bu gou");
                return(false);
            }

            if (m_pNetPacketHeader.PacketId > 0)
            {
                NetPacket packet = null;

                //				string str = "";
                //				Debug.Log("m_pNetPacketHeader.BodyLength:" + m_pNetPacketHeader.BodyLength);
                //				for(int i = 0;i < m_pNetPacketHeader.BodyLength;i++){
                //					str += (m_Recvbuffer[i] + "_");
                //				}
                //				Debug.Log("m_Recvbuffer:" + str);
                packet = new NetPacket(m_pNetPacketHeader);
                packet.Read(m_Recvbuffer, m_pNetPacketHeader.BodyLength);
                m_RecvQueue.Enqueue(packet);

                m_pNetPacketHeader.PacketId = 0;
                m_nRecvBufferOffset         = 0;
            }
            else
            {   //read packet header
                if (m_pNetPacketHeader.ReadHead(m_Recvbuffer))
                {
                    if (m_pNetPacketHeader.BodyLength <= 0)
                    {//dose not have body data
                        NetPacket msg = new NetPacket(m_pNetPacketHeader);
                        m_RecvQueue.Enqueue(msg);
                        //						Debug.Log("m_pNetPacketHeader.PacketId:" + m_pNetPacketHeader.PacketId);
                        //						Debug.Log("msg:" + msg.Count);
                        m_pNetPacketHeader.PacketId = 0;
                    }
                    m_nRecvBufferOffset = 0;
                }
                else
                {
                    m_TcpSocket.Close(1);
                    return(false);
                }
            }
            return(true);
        }