Transparently encodes a single message.

At construction, the passed message is first appended to the WriteBuffer object passed to CreateAsync. Afterwards, when data is written to this stream then it is first encoded and the encoded form is then appended to the WriteBuffer object.

Caution: DisposeAsync must be called in the end.

If necessary, the message plus payload is automatically partitioned into multiple packets such that the unencoded length of each packet does not exceed 1024 bytes.

Inheritance: NonSeekableStream
        private async Task <NonSeekableStream> WriteMessageCoreAsync(
            S101Message message, CancellationToken cancellationToken)
        {
            this.AssertNotDisposed();

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if ((this.stream != null) && this.stream.CanWrite)
            {
                throw new InvalidOperationException(
                          "DisposeAsync() has not been called on the payload stream of the previous message.");
            }

            this.stream = await MessageEncodingStream.CreateAsync(this.writeBuffer, message, cancellationToken);

            if (!message.CanHavePayload)
            {
                await this.stream.DisposeAsync(cancellationToken);

                this.stream = null;
            }

            return(this.stream);
        }
Exemple #2
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal static async Task <MessageEncodingStream> CreateAsync(
            WriteBuffer rawBuffer, S101Message message, CancellationToken cancellationToken)
        {
            message.PacketFlags =
                PacketFlags.FirstPacket | (message.CanHaveMultiplePackets ? PacketFlags.None : PacketFlags.LastPacket);
            var framingStream = await FramingStream.CreateAsync(rawBuffer, cancellationToken);

            var result = new MessageEncodingStream(message, rawBuffer, framingStream);
            await message.WriteToAsync(result.unframedBuffer, cancellationToken);

            return(result);
        }
 private async Task DisposeCoreAsync(CancellationToken cancellationToken)
 {
     try
     {
         if (this.stream != null)
         {
             await this.stream.DisposeAsync(cancellationToken);
         }
     }
     finally
     {
         this.disposed = true;
         this.stream   = null;
     }
 }
Exemple #4
0
 private async Task DisposeCoreAsync(CancellationToken cancellationToken)
 {
     try
     {
         if (this.stream != null)
         {
             await this.stream.DisposeAsync(cancellationToken);
         }
     }
     finally
     {
         this.disposed = true;
         this.stream = null;
     }
 }
Exemple #5
0
        private async Task<NonSeekableStream> WriteMessageCoreAsync(
            S101Message message, CancellationToken cancellationToken)
        {
            this.AssertNotDisposed();

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if ((this.stream != null) && this.stream.CanWrite)
            {
                throw new InvalidOperationException(
                    "DisposeAsync() has not been called on the payload stream of the previous message.");
            }

            this.stream = await MessageEncodingStream.CreateAsync(this.writeBuffer, message, cancellationToken);

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

            return this.stream;
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal static async Task<MessageEncodingStream> CreateAsync(
            WriteBuffer rawBuffer, S101Message message, CancellationToken cancellationToken)
        {
            message.PacketFlags =
                PacketFlags.FirstPacket | (message.CanHaveMultiplePackets ? PacketFlags.None : PacketFlags.LastPacket);
            var framingStream = await FramingStream.CreateAsync(rawBuffer, cancellationToken);
            var result = new MessageEncodingStream(message, rawBuffer, framingStream);
            await message.WriteToAsync(result.unframedBuffer, cancellationToken);
            return result;
        }