示例#1
0
        /// <summary>
        /// Produces a MessageReader using the parent's buffer. This MessageReader should **NOT** be recycled.
        /// </summary>
        public MessageReader ReadMessage()
        {
            // Ensure there is at least a header
            if (this.BytesRemaining < 3)
            {
                throw new InvalidDataException($"ReadMessage header is longer than message length: 3 of {this.BytesRemaining}");
            }

            var output = new MessageReader();

            output.Parent   = this;
            output.Buffer   = this.Buffer;
            output.Offset   = this.readHead;
            output.Position = 0;

            output.Length = output.ReadUInt16();
            output.Tag    = output.ReadByte();

            output.Offset  += 3;
            output.Position = 0;

            if (this.BytesRemaining < output.Length + 3)
            {
                throw new InvalidDataException($"Message Length at Position {this.readHead} is longer than message length: {output.Length + 3} of {this.BytesRemaining}");
            }

            this.Position += output.Length + 3;
            return(output);
        }
        ///
        public MessageReader ReadMessage()
        {
            // Ensure there is at least a header
            if (this.readHead + 3 > this.Buffer.Length)
            {
                return(null);
            }

            var output = new MessageReader();

            output.Buffer   = this.Buffer;
            output.Offset   = this.readHead;
            output.Position = 0;

            output.Length = output.ReadUInt16();
            output.Tag    = output.ReadByte();

            output.Offset  += 3;
            output.Position = 0;

            this.Position += output.Length + 3;
            return(output);
        }