Ejemplo n.º 1
0
        private int ProcessPackets()
        {
            Debug.Assert(null != m_Object);

            int ret = (int)SESSION_ERROR.SUCCESS;

            if (m_ReceiveBuffer.GetCanReadSize() < Packet.PACKET_HEAD_LENGTH)
            {
                return(ret);
            }

            int pkt_size = m_ReceiveBuffer.PeekPacketSize();

            if (pkt_size < Packet.PACKET_HEAD_LENGTH)
            {
                // 消息头部未接收全

                return(ret);
            }

            if (m_ReceiveBuffer.GetCanReadSize() < pkt_size)
            {
                // 消息未接收全

                return(ret);
            }

            if (pkt_size > Packet.DEFAULT_PACKET_BUF_SIZE)
            {
                // 消息大小异常

                ret = (int)SESSION_ERROR.E_PACKET_SIZE;
                return(ret);
            }

            Packet pkt = PacketManager.Instance.AllocatePacket();

            pkt.Initialize();

            m_ReceiveBuffer.ReadBytes(pkt.Buf, pkt_size);

            pkt.SetSize();

            Debug.Assert(pkt.Size == pkt_size);
            Debug.Assert(pkt.Valid());

            ret = PacketProcessManager.Instance.ProcessPacket(m_Object, pkt);

            if ((int)PacketProcessManager.PACKET_PROC_ERROR.SUCCESS != ret)
            {
                ret = (int)SESSION_ERROR.E_PACKET_PROC;
            }

            PacketManager.Instance.ReleasePacket(pkt);
            return(ret);
        }
Ejemplo n.º 2
0
        public bool ProcessSend(SocketAsyncEventArgs args)
        {
            Debug.Assert(null != args);
            Debug.Assert(m_SendEventArgs == args);

            if (m_SendBuffer.GetCanReadSize() <= 0)
            {
                return(false);
            }

            int buf_size = m_SendBuffer.GetCanReadSize();

            Debug.Assert(buf_size >= Packet.PACKET_HEAD_LENGTH);

            m_SendBuffer.ReadBytes(args.Buffer, buf_size);

            args.SetBuffer(0, buf_size);
            return(true);
        }
Ejemplo n.º 3
0
        private void Test_Share_Net_ReadWriteBuffer()
        {
            Packet pkt_orig = PacketManager.Instance.AllocatePacket();

            CAssert.IsNotNull(pkt_orig);

            pkt_orig.SetPacketID(Protocol.CLI_GW_ENTER_TEST);
            CAssert.AreEqual(pkt_orig.GetPacketID(), Protocol.CLI_GW_ENTER_TEST);

            int    int_test    = 10000001;
            short  short_test  = 10001;
            double double_test = 123456789.987654321;
            float  float_test  = 12345.12345f;
            string str_test    = "This is a testing string.";

            pkt_orig.AddInt(int_test);
            pkt_orig.AddShort(short_test);
            pkt_orig.AddDouble(double_test);
            pkt_orig.AddFloat(float_test);
            pkt_orig.AddString(str_test);


            byte[]          buffer = new byte[ReadWriteBuffer.BUFFER_MAX_SIZE];
            ReadWriteBuffer rw_buf = new ReadWriteBuffer(buffer);

            CAssert.IsNotNull(rw_buf);
            CAssert.AreEqual(0, rw_buf.GetCanReadSize());
            CAssert.AreEqual(ReadWriteBuffer.BUFFER_MAX_SIZE, rw_buf.GetCanWriteSize());

            rw_buf.WriteBytes(pkt_orig.Buf, pkt_orig.Size);
            CAssert.AreEqual(rw_buf.GetCanReadSize(), (int)pkt_orig.Size);

            Packet pkt_read = PacketManager.Instance.AllocatePacket();

            CAssert.IsNotNull(pkt_read);

            rw_buf.ReadBytes(pkt_read.Buf, rw_buf.GetCanReadSize());

            pkt_read.SetSize();

            CAssert.AreEqual(0, rw_buf.GetCanReadSize());
            CAssert.AreEqual(ReadWriteBuffer.BUFFER_MAX_SIZE, rw_buf.GetCanWriteSize());

            CAssert.AreEqual(pkt_orig.Size, pkt_read.Size);
            CAssert.AreEqual(pkt_orig.Buf, pkt_read.Buf);

            int w_count = 0;

            while (rw_buf.GetCanWriteSize() >= pkt_orig.Size)
            {
                rw_buf.WriteBytes(pkt_orig.Buf, pkt_orig.Size);
                ++w_count;
            }

            CAssert.AreEqual(pkt_orig.Size * w_count, rw_buf.GetCanReadSize());

            int r_count = 0;

            while (rw_buf.GetCanReadSize() > 0)
            {
                int pkt_size = rw_buf.PeekPacketSize();
                CAssert.IsTrue(rw_buf.GetCanReadSize() >= pkt_size);

                Packet pkt_tmp = PacketManager.Instance.AllocatePacket();
                CAssert.IsNotNull(pkt_tmp);

                pkt_tmp.Initialize();
                rw_buf.ReadBytes(pkt_tmp.Buf, pkt_size);

                pkt_tmp.SetSize();

                ++r_count;

                CAssert.AreEqual(pkt_tmp.Size, pkt_orig.Size);
                CAssert.AreEqual(pkt_tmp.Buf, pkt_orig.Buf);
            }

            CAssert.AreEqual(r_count, w_count);

            CAssert.AreEqual(0, rw_buf.GetCanReadSize());
            CAssert.AreEqual(ReadWriteBuffer.BUFFER_MAX_SIZE, rw_buf.GetCanWriteSize());

            rw_buf.Release();
            //CAssert.IsNull(rw_buf.Buffer);
        }