ReadRawBytes() приватный Метод

Reads a fixed size of bytes from the input.
/// the end of the stream or the current limit was reached ///
private ReadRawBytes ( int size ) : byte[]
size int
Результат byte[]
Пример #1
0
        public void ResetSizeCounter()
        {
            CodedInputStream input = CodedInputStream.CreateInstance(
                new SmallBlockInputStream(new byte[256], 8));

            input.SetSizeLimit(16);
            input.ReadRawBytes(16);

            Assert.Throws <InvalidProtocolBufferException>(() => input.ReadRawByte());

            input.ResetSizeCounter();
            input.ReadRawByte(); // No exception thrown.

            Assert.Throws <InvalidProtocolBufferException>(() => input.ReadRawBytes(16));
        }
Пример #2
0
        public IMessage Decode(byte[] msg)
        {
            IMessage message = null;

            CodedInputStream stream = new CodedInputStream(msg);

            //data length,Protobuf 变长头, 也就是消息长度
            int varint32 = (int)stream.ReadInt32();

            if (varint32 <= (msg.Length - (int)stream.Position))
            {
                try
                {
                    byte[] body = stream.ReadRawBytes(varint32);
                    message = Message.Parser.ParseFrom(body);
                }
                catch (Exception exception)
                {
                    //"[ProtobufEncoding] Decode exception :{0}", exception.Message);
                }
            }
            else
            {
                //"网络数据读取不完整,丢包了?");
            }

            return(message);
        }
Пример #3
0
        /// <summary>
        /// 将数据解码
        /// </summary>
        public void DecodeAsync(byte[] msg)
        {
            if (msg.Length <= 0)
            {
                return;
            }
            if (buffer.Data == null)
            {
                buffer.Data = new byte[8192];
            }
            //把收取上来的自己全部缓存到本地 buffer 中
            Array.Copy(msg, 0, buffer.Data, buffer.Length, msg.Length);
            buffer.Length += msg.Length;

            //string.Format("cache buff length: {0}, offset: {1}", buffer.Length, buffer.Offset));


            CodedInputStream stream = new CodedInputStream(buffer.Data);

            while (!stream.IsAtEnd)
            {
                //标记读取的Position, 在长度不够时进行数组拷贝,到下一次在进行解析
                int markReadIndex = (int)stream.Position;

                //data length,Protobuf 变长头, 也就是消息长度
                int varint32 = (int)stream.ReadInt32();
                if (varint32 <= (buffer.Length - (int)stream.Position))
                {
                    try
                    {
                        byte[] body = stream.ReadRawBytes(varint32);

                        Message message = Message.Parser.ParseFrom(body);

                        SocketManager.Instance.DispatchProto(message);
                        //TODO: dispatcher message, 这里就可以用多线程进行协议分发
                    }
                    catch (Exception exception)
                    {
                        //exception.Message);
                    }
                }
                else
                {
                    //本次数据不够长度,缓存进行下一次解析
                    byte[] dest       = new byte[8192];
                    int    remainSize = buffer.Length - markReadIndex;
                    Array.Copy(buffer.Data, markReadIndex, dest, 0, remainSize);

                    /**
                     * 缓存未处理完的字节
                     */
                    buffer.Data   = dest;
                    buffer.Offset = 0;
                    buffer.Length = remainSize;
                    break;
                }
            }
        }
Пример #4
0
        public void CreateCodedInput_FromArraySegment()
        {
            byte[]           data             = new byte[] { 0, 1, 2, 3, 4, 5, 6 };
            ByteString       bs               = UnsafeByteOperations.UnsafeWrap(data.AsMemory(2, 3));
            CodedInputStream codedInputStream = bs.CreateCodedInput();

            byte[] bytes = codedInputStream.ReadRawBytes(3);

            Assert.AreEqual(3, bytes.Length);
            Assert.AreEqual(2, bytes[0]);
            Assert.AreEqual(3, bytes[1]);
            Assert.AreEqual(4, bytes[2]);
            Assert.IsTrue(codedInputStream.IsAtEnd);
        }
Пример #5
0
        public void ResetSizeCounter()
        {
            CodedInputStream input = new CodedInputStream(
                new SmallBlockInputStream(new byte[256], 8));
            input.SetSizeLimit(16);
            input.ReadRawBytes(16);

            Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawByte());

            input.ResetSizeCounter();
            input.ReadRawByte(); // No exception thrown.

            Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawBytes(16));
        }