protected internal Stream GetStream(Func <Stream> messageStreamFunc, bool isRequest) { // Check whether we have an existing buffering read stream when reading if (!this.writing) { BufferingReadStream existingBufferingReadStream = this.TryGetBufferingReadStream(); if (existingBufferingReadStream != null) { Debug.Assert(this.useBufferingReadStream.HasValue, "UseBufferingReadStream must have been set."); return(existingBufferingReadStream); } } // Get the message stream Stream messageStream = messageStreamFunc(); ValidateMessageStream(messageStream, isRequest); // When reading, wrap the stream in a byte counting stream if a max message size was specified. // When requested, wrap the stream in a non-disposing stream. bool needByteCountingStream = !this.writing && this.maxMessageSize > 0; if (this.disableMessageStreamDisposal && needByteCountingStream) { messageStream = MessageStreamWrapper.CreateNonDisposingStreamWithMaxSize(messageStream, this.maxMessageSize); } else if (this.disableMessageStreamDisposal) { messageStream = MessageStreamWrapper.CreateNonDisposingStream(messageStream); } else if (needByteCountingStream) { messageStream = MessageStreamWrapper.CreateStreamWithMaxSize(messageStream, this.maxMessageSize); } // If a buffering read stream is required, create it now if (!this.writing && this.useBufferingReadStream == true) { Debug.Assert(!this.writing, "The buffering read stream should only be used when reading."); this.bufferingReadStream = new BufferingReadStream(messageStream); messageStream = this.bufferingReadStream; } return(messageStream); }
/// <summary> /// Gets the buffering read stream if one is available; otherwise returns null. /// </summary> /// <returns>The <see cref="BufferingReadStream"/> currently being used or null if no buffering stream is currently being used.</returns> private BufferingReadStream TryGetBufferingReadStream() { if (this.bufferingReadStream == null) { return(null); } // We already have a buffering read stream; reset it if necessary and return it; // if the stream is not buffering anymore we are done with payload kind detection // and don't need the buffering stream anymore - we start the actual reading now. BufferingReadStream stream = this.bufferingReadStream; if (this.bufferingReadStream.IsBuffering) { this.bufferingReadStream.ResetStream(); } else { this.bufferingReadStream = null; } return(stream); }
/// <summary> /// Gets the buffering read stream if one is available; otherwise returns null. /// </summary> /// <returns>The <see cref="BufferingReadStream"/> currently being used or null if no buffering stream is currently being used.</returns> private BufferingReadStream TryGetBufferingReadStream() { if (this.bufferingReadStream == null) { return null; } // We already have a buffering read stream; reset it if necessary and return it; // if the stream is not buffering anymore we are done with payload kind detection // and don't need the buffering stream anymore - we start the actual reading now. BufferingReadStream stream = this.bufferingReadStream; if (this.bufferingReadStream.IsBuffering) { this.bufferingReadStream.ResetStream(); } else { this.bufferingReadStream = null; } return stream; }
/// <summary> /// Asynchronously get the stream backing this message. /// </summary> /// <param name="streamFuncAsync">A function that returns a task for the stream backing the message.</param> /// <param name="isRequest">true if the message is a request message; false for a response message.</param> /// <returns>A task that when completed returns the stream backing the message.</returns> protected internal Task<Stream> GetStreamAsync(Func<Task<Stream>> streamFuncAsync, bool isRequest) { // Check whether we have an existing buffering read stream when reading if (!this.writing) { Stream existingBufferingReadStream = this.TryGetBufferingReadStream(); Debug.Assert(!this.writing || existingBufferingReadStream == null, "The buffering read stream should only be used when reading."); if (existingBufferingReadStream != null) { Debug.Assert(this.useBufferingReadStream.HasValue, "UseBufferingReadStream must have been set."); return TaskUtils.GetCompletedTask(existingBufferingReadStream); } } Task<Stream> task = streamFuncAsync(); ValidateMessageStreamTask(task, isRequest); // Wrap it in a non-disposing stream if requested task = task.FollowOnSuccessWith( streamTask => { Stream messageStream = streamTask.Result; ValidateMessageStream(messageStream, isRequest); // When reading, wrap the stream in a byte counting stream if a max message size was specified. // When requested, wrap the stream in a non-disposing stream. bool needByteCountingStream = !this.writing && this.maxMessageSize > 0; if (this.disableMessageStreamDisposal && needByteCountingStream) { messageStream = MessageStreamWrapper.CreateNonDisposingStreamWithMaxSize(messageStream, this.maxMessageSize); } else if (this.disableMessageStreamDisposal) { messageStream = MessageStreamWrapper.CreateNonDisposingStream(messageStream); } else if (needByteCountingStream) { messageStream = MessageStreamWrapper.CreateStreamWithMaxSize(messageStream, this.maxMessageSize); } return messageStream; }); // When we are reading, also buffer the input stream if (!this.writing) { task = task .FollowOnSuccessWithTask( streamTask => { return BufferedReadStream.BufferStreamAsync(streamTask.Result); }) .FollowOnSuccessWith( streamTask => { BufferedReadStream bufferedReadStream = streamTask.Result; return (Stream)bufferedReadStream; }); // If requested also create a buffering stream for payload kind detection if (this.useBufferingReadStream == true) { task = task.FollowOnSuccessWith( streamTask => { Stream messageStream = streamTask.Result; this.bufferingReadStream = new BufferingReadStream(messageStream); messageStream = this.bufferingReadStream; return messageStream; }); } } return task; }
protected internal Stream GetStream(Func<Stream> messageStreamFunc, bool isRequest) { // Check whether we have an existing buffering read stream when reading if (!this.writing) { BufferingReadStream existingBufferingReadStream = this.TryGetBufferingReadStream(); if (existingBufferingReadStream != null) { Debug.Assert(this.useBufferingReadStream.HasValue, "UseBufferingReadStream must have been set."); return existingBufferingReadStream; } } // Get the message stream Stream messageStream = messageStreamFunc(); ValidateMessageStream(messageStream, isRequest); // When reading, wrap the stream in a byte counting stream if a max message size was specified. // When requested, wrap the stream in a non-disposing stream. bool needByteCountingStream = !this.writing && this.maxMessageSize > 0; if (this.disableMessageStreamDisposal && needByteCountingStream) { messageStream = MessageStreamWrapper.CreateNonDisposingStreamWithMaxSize(messageStream, this.maxMessageSize); } else if (this.disableMessageStreamDisposal) { messageStream = MessageStreamWrapper.CreateNonDisposingStream(messageStream); } else if (needByteCountingStream) { messageStream = MessageStreamWrapper.CreateStreamWithMaxSize(messageStream, this.maxMessageSize); } // If a buffering read stream is required, create it now if (!this.writing && this.useBufferingReadStream == true) { Debug.Assert(!this.writing, "The buffering read stream should only be used when reading."); this.bufferingReadStream = new BufferingReadStream(messageStream); messageStream = this.bufferingReadStream; } return messageStream; }
/// <summary> /// Asynchronously get the stream backing this message. /// </summary> /// <param name="streamFuncAsync">A function that returns a task for the stream backing the message.</param> /// <param name="isRequest">true if the message is a request message; false for a response message.</param> /// <returns>A task that when completed returns the stream backing the message.</returns> protected internal Task <Stream> GetStreamAsync(Func <Task <Stream> > streamFuncAsync, bool isRequest) { // Check whether we have an existing buffering read stream when reading if (!this.writing) { Stream existingBufferingReadStream = this.TryGetBufferingReadStream(); Debug.Assert(!this.writing || existingBufferingReadStream == null, "The buffering read stream should only be used when reading."); if (existingBufferingReadStream != null) { Debug.Assert(this.useBufferingReadStream.HasValue, "UseBufferingReadStream must have been set."); return(TaskUtils.GetCompletedTask(existingBufferingReadStream)); } } Task <Stream> task = streamFuncAsync(); ValidateMessageStreamTask(task, isRequest); // Wrap it in a non-disposing stream if requested task = task.FollowOnSuccessWith( streamTask => { Stream messageStream = streamTask.Result; ValidateMessageStream(messageStream, isRequest); // When reading, wrap the stream in a byte counting stream if a max message size was specified. // When requested, wrap the stream in a non-disposing stream. bool needByteCountingStream = !this.writing && this.maxMessageSize > 0; if (this.disableMessageStreamDisposal && needByteCountingStream) { messageStream = MessageStreamWrapper.CreateNonDisposingStreamWithMaxSize(messageStream, this.maxMessageSize); } else if (this.disableMessageStreamDisposal) { messageStream = MessageStreamWrapper.CreateNonDisposingStream(messageStream); } else if (needByteCountingStream) { messageStream = MessageStreamWrapper.CreateStreamWithMaxSize(messageStream, this.maxMessageSize); } return(messageStream); }); // When we are reading, also buffer the input stream if (!this.writing) { task = task .FollowOnSuccessWithTask( streamTask => { return(BufferedReadStream.BufferStreamAsync(streamTask.Result)); }) .FollowOnSuccessWith( streamTask => { BufferedReadStream bufferedReadStream = streamTask.Result; return((Stream)bufferedReadStream); }); // If requested also create a buffering stream for payload kind detection if (this.useBufferingReadStream == true) { task = task.FollowOnSuccessWith( streamTask => { Stream messageStream = streamTask.Result; this.bufferingReadStream = new BufferingReadStream(messageStream); messageStream = this.bufferingReadStream; return(messageStream); }); } } return(task); }