Transparently decodes a single S101 message.

At construction, a Message object is first decoded from the ReadBuffer object passed to CreateAsync and made available through the Message property. Afterwards, a call to any of the Read methods of this stream removes data from ReadBuffer object passed to the CreateAsync. The data is then decoded and the decoded form is then returned.

If a message contains multiple packets, their payload is automatically joined such that it can be read through this stream as if the message consisted of only one packet.

Inheritance: NonSeekableStream
        private async Task <bool> ReadCoreAsync(CancellationToken cancellationToken)
        {
            this.AssertNotDisposed();

            if (this.stream != null)
            {
                await this.stream.DisposeAsync(cancellationToken);
            }

            this.stream = await MessageDecodingStream.CreateAsync(
                this.readBuffer, this.discardBuffer, this.OnOutOfFrameByteReceived, cancellationToken);

            return(this.stream.Message != null);
        }
 private async Task DisposeCoreAsync(CancellationToken cancellationToken)
 {
     try
     {
         if (this.stream != null)
         {
             await this.stream.DisposeAsync(cancellationToken);
         }
     }
     finally
     {
         this.disposed = true;
         this.stream   = null;
     }
 }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal static async Task <MessageDecodingStream> CreateAsync(
            ReadBuffer rawBuffer,
            byte[] discardBuffer,
            Action <byte> outOfFrameByteReceived,
            CancellationToken cancellationToken)
        {
            var result     = new MessageDecodingStream(rawBuffer, discardBuffer, outOfFrameByteReceived);
            var newMessage = await S101Message.ReadFromAsync(result.deframedBuffer, cancellationToken);

            if ((newMessage != null) && newMessage.CanHaveMultiplePackets &&
                ((newMessage.PacketFlags & PacketFlags.FirstPacket) == 0))
            {
                throw new S101Exception(string.Format(
                                            CultureInfo.InvariantCulture, "Missing {0} flag in first packet.", PacketFlags.FirstPacket));
            }

            result.message = newMessage;
            return(result);
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal static async Task<MessageDecodingStream> CreateAsync(
            ReadBuffer rawBuffer,
            byte[] discardBuffer,
            Action<byte> outOfFrameByteReceived,
            CancellationToken cancellationToken)
        {
            var result = new MessageDecodingStream(rawBuffer, discardBuffer, outOfFrameByteReceived);
            var newMessage = await S101Message.ReadFromAsync(result.deframedBuffer, cancellationToken);

            if ((newMessage != null) && newMessage.CanHaveMultiplePackets &&
                ((newMessage.PacketFlags & PacketFlags.FirstPacket) == 0))
            {
                throw new S101Exception(string.Format(
                    CultureInfo.InvariantCulture, "Missing {0} flag in first packet.", PacketFlags.FirstPacket));
            }

            result.message = newMessage;
            return result;
        }
Beispiel #5
0
        private async Task<bool> ReadCoreAsync(CancellationToken cancellationToken)
        {
            this.AssertNotDisposed();

            if (this.stream != null)
            {
                await this.stream.DisposeAsync(cancellationToken);
            }

            this.stream = await MessageDecodingStream.CreateAsync(
                this.readBuffer, this.discardBuffer, this.OnOutOfFrameByteReceived, cancellationToken);
            return this.stream.Message != null;
        }
Beispiel #6
0
 private async Task DisposeCoreAsync(CancellationToken cancellationToken)
 {
     try
     {
         if (this.stream != null)
         {
             await this.stream.DisposeAsync(cancellationToken);
         }
     }
     finally
     {
         this.disposed = true;
         this.stream = null;
     }
 }