コード例 #1
0
        public void ReadAndPositionTest()
        {
            ReceiveBuffer target = new ReceiveBuffer(); // TODO: 初始化为适当的值
            var           bytes  = new byte[] {
                1,
                3,
                0, 1,
                0, 0, 0, 1,
                0, 0, 0, 0, 0, 0, 0, 5,
                255,
                255, 255
            };

            target.Add(bytes, 0, bytes.Length);

            target.Position = 0;
            Assert.IsTrue(target.ReadBoolean() && target.Position == 1);
            Assert.IsTrue(target.ReadByte() == 3 && target.Position == 1 + 1);
            Assert.IsTrue(target.ReadInt16() == 1 && target.Position == 1 + 1 + 2);
            Assert.IsTrue(target.ReadInt32() == 1 && target.Position == 1 + 1 + 2 + 4);
            Assert.IsTrue(target.ReadInt64() == 5 && target.Position == 1 + 1 + 2 + 4 + 8);
            Assert.IsTrue(target.ReadArray(1)[0] == 255 && target.Position == 1 + 1 + 2 + 4 + 8 + 1);
            Assert.IsTrue(target.ReadArray().Length == 2 && target.Position == 1 + 1 + 2 + 4 + 8 + 1 + 2);
        }
コード例 #2
0
ファイル: FastPacket.cs プロジェクト: tenove/NetworkSocket
        /// <summary>
        /// 解析一个数据包
        /// 不足一个封包时返回null
        /// </summary>
        /// <param name="buffer">接收到的历史数据</param>
        /// <exception cref="ProtocolException"></exception>
        /// <returns></returns>
        public static FastPacket From(ReceiveBuffer buffer)
        {
            if (buffer.Length < 4)
            {
                return(null);
            }

            buffer.Position = 0;
            var       totalBytes    = buffer.ReadInt32();
            const int packegMaxSize = 10 * 1204 * 1024; // 10M

            if (totalBytes > packegMaxSize)
            {
                throw new ProtocolException();
            }

            // 少于15字节是异常数据,清除收到的所有数据
            const int packetMinSize = 15;

            if (totalBytes < packetMinSize)
            {
                throw new ProtocolException();
            }

            // 数据包未接收完整
            if (buffer.Length < totalBytes)
            {
                return(null);
            }

            // api名称数据长度
            var apiNameLength = buffer.ReadByte();

            if (totalBytes < apiNameLength + packetMinSize)
            {
                throw new ProtocolException();
            }

            // api名称数据
            var apiNameBytes = buffer.ReadArray(apiNameLength);
            // 标识符
            var id = buffer.ReadInt64();
            // 是否为客户端封包
            var isFromClient = buffer.ReadBoolean();
            // 是否异常
            var isException = buffer.ReadBoolean();
            // 实体数据
            var body = buffer.ReadArray(totalBytes - buffer.Position);

            // 清空本条数据
            buffer.Clear(totalBytes);

            var apiName = Encoding.UTF8.GetString(apiNameBytes);
            var packet  = new FastPacket(apiName, id, isFromClient)
            {
                TotalBytes    = totalBytes,
                ApiNameLength = apiNameLength,
                IsException   = isException,
                Body          = body
            };

            return(packet);
        }
コード例 #3
0
ファイル: FastPacket.cs プロジェクト: dzvane/NetworkSocket
        /// <summary>
        /// 解析一个数据包       
        /// 不足一个封包时返回null
        /// </summary>
        /// <param name="buffer">接收到的历史数据</param>
        /// <exception cref="ProtocolException"></exception>
        /// <returns></returns>
        public static FastPacket From(ReceiveBuffer buffer)
        {
            if (buffer.Length < 4)
            {
                return null;
            }

            buffer.Position = 0;
            var totalBytes = buffer.ReadInt32();
            const int packegMaxSize = 10 * 1204 * 1024; // 10M
            if (totalBytes > packegMaxSize)
            {
                throw new ProtocolException();
            }

            // 少于15字节是异常数据,清除收到的所有数据
            const int packetMinSize = 15;
            if (totalBytes < packetMinSize)
            {
                throw new ProtocolException();
            }

            // 数据包未接收完整
            if (buffer.Length < totalBytes)
            {
                return null;
            }

            // api名称数据长度
            var apiNameLength = buffer.ReadByte();
            if (totalBytes < apiNameLength + packetMinSize)
            {
                throw new ProtocolException();
            }

            // api名称数据
            var apiNameBytes = buffer.ReadArray(apiNameLength);
            // 标识符
            var id = buffer.ReadInt64();
            // 是否为客户端封包
            var isFromClient = buffer.ReadBoolean();
            // 是否异常
            var isException = buffer.ReadBoolean();
            // 实体数据
            var body = buffer.ReadArray(totalBytes - buffer.Position);

            // 清空本条数据
            buffer.Clear(totalBytes);

            var apiName = Encoding.UTF8.GetString(apiNameBytes);
            var packet = new FastPacket(apiName, id, isFromClient)
            {
                TotalBytes = totalBytes,
                ApiNameLength = apiNameLength,
                IsException = isException,
                Body = body
            };
            return packet;
        }