| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Buffers; |
| | | 6 | | using System.Collections; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.IO.Pipelines; |
| | | 9 | | using System.Net; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using System.Xml; |
| | | 13 | | using CoreWCF.Configuration; |
| | | 14 | | using CoreWCF.Runtime; |
| | | 15 | | using CoreWCF.Security; |
| | | 16 | | using Microsoft.Extensions.DependencyInjection; |
| | | 17 | | |
| | | 18 | | namespace CoreWCF.Channels.Framing |
| | | 19 | | { |
| | | 20 | | internal class ServerSingletonConnectionReaderMiddleware |
| | | 21 | | { |
| | | 22 | | private readonly HandshakeDelegate _next; |
| | 83 | 23 | | private readonly Hashtable _serviceChannelDispatcherCache = new Hashtable(); |
| | | 24 | | private readonly IServiceScopeFactory _servicesScopeFactory; |
| | 83 | 25 | | private readonly AsyncLock _lock = new AsyncLock(); |
| | | 26 | | |
| | 83 | 27 | | public ServerSingletonConnectionReaderMiddleware(HandshakeDelegate next, IServiceScopeFactory servicesScopeFacto |
| | | 28 | | { |
| | 83 | 29 | | _next = next; |
| | 83 | 30 | | _servicesScopeFactory = servicesScopeFactory; |
| | 83 | 31 | | } |
| | | 32 | | |
| | | 33 | | public async Task OnConnectedAsync(FramingConnection connection) |
| | | 34 | | { |
| | | 35 | | IServiceChannelDispatcher channelDispatcher; |
| | 50 | 36 | | if (_serviceChannelDispatcherCache.ContainsKey(connection.ServiceDispatcher)) |
| | | 37 | | { |
| | 22 | 38 | | channelDispatcher = (IServiceChannelDispatcher)_serviceChannelDispatcherCache[connection.ServiceDispatch |
| | | 39 | | } |
| | | 40 | | else |
| | | 41 | | { |
| | 28 | 42 | | await using (await _lock.TakeLockAsync()) |
| | | 43 | | { |
| | 28 | 44 | | if (_serviceChannelDispatcherCache.ContainsKey(connection.ServiceDispatcher)) |
| | | 45 | | { |
| | 0 | 46 | | channelDispatcher = (IServiceChannelDispatcher)_serviceChannelDispatcherCache[connection.Service |
| | | 47 | | } |
| | | 48 | | else |
| | | 49 | | { |
| | 28 | 50 | | BindingElementCollection be = connection.ServiceDispatcher.Binding.CreateBindingElements(); |
| | 28 | 51 | | TransportBindingElement tbe = be.Find<TransportBindingElement>(); |
| | 28 | 52 | | ITransportFactorySettings settings = new NetFramingTransportSettings |
| | 28 | 53 | | { |
| | 28 | 54 | | CloseTimeout = connection.ServiceDispatcher.Binding.CloseTimeout, |
| | 28 | 55 | | OpenTimeout = connection.ServiceDispatcher.Binding.OpenTimeout, |
| | 28 | 56 | | ReceiveTimeout = connection.ServiceDispatcher.Binding.ReceiveTimeout, |
| | 28 | 57 | | SendTimeout = connection.ServiceDispatcher.Binding.SendTimeout, |
| | 28 | 58 | | ManualAddressing = tbe.ManualAddressing, |
| | 28 | 59 | | BufferManager = connection.BufferManager, |
| | 28 | 60 | | MaxReceivedMessageSize = tbe.MaxReceivedMessageSize, |
| | 28 | 61 | | MessageEncoderFactory = connection.MessageEncoderFactory |
| | 28 | 62 | | }; |
| | | 63 | | // Even though channel is reused for multiple connections, there are some scoped dependencies us |
| | 28 | 64 | | var replyChannel = new ConnectionOrientedTransportReplyChannel(settings, null, _servicesScopeFac |
| | 28 | 65 | | channelDispatcher = await connection.ServiceDispatcher.CreateServiceChannelDispatcherAsync(reply |
| | 28 | 66 | | _serviceChannelDispatcherCache[connection.ServiceDispatcher] = channelDispatcher; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | // TODO: I think that the receive timeout starts counting at the start of the preamble on .NET Framework. Th |
| | | 72 | | // after the preamble has completed. This probably needs to be addressed otherwise worse case you could end |
| | | 73 | | // I believe the preamble should really use the OpenTimeout but that's not how this is implemented on .NET F |
| | 50 | 74 | | var timeoutHelper = new TimeoutHelper(connection.ServiceDispatcher.Binding.ReceiveTimeout); |
| | 50 | 75 | | StreamedFramingRequestContext requestContext = await ReceiveRequestAsync(connection, timeoutHelper.Remaining |
| | 50 | 76 | | await channelDispatcher.DispatchAsync(requestContext); |
| | 50 | 77 | | await requestContext.ReplySent; |
| | 50 | 78 | | } |
| | | 79 | | |
| | | 80 | | public async Task<StreamedFramingRequestContext> ReceiveRequestAsync(FramingConnection connection, TimeSpan time |
| | | 81 | | { |
| | 50 | 82 | | (Message requestMessage, Stream inputStream) = await ReceiveAsync(connection, timeout); |
| | 50 | 83 | | return new StreamedFramingRequestContext(connection, requestMessage, inputStream); |
| | 50 | 84 | | } |
| | | 85 | | |
| | | 86 | | public async Task<(Message, Stream)> ReceiveAsync(FramingConnection connection, TimeSpan timeout) |
| | | 87 | | { |
| | 50 | 88 | | TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); |
| | 50 | 89 | | ReadOnlySequence<byte> buffer = ReadOnlySequence<byte>.Empty; |
| | | 90 | | for (; ; ) |
| | | 91 | | { |
| | 50 | 92 | | ReadResult readResult = await connection.Input.ReadAsync(); |
| | 50 | 93 | | await Task.Yield(); |
| | 50 | 94 | | if (readResult.IsCompleted || readResult.Buffer.Length == 0) |
| | | 95 | | { |
| | 0 | 96 | | if (!readResult.IsCompleted) |
| | | 97 | | { |
| | 0 | 98 | | connection.Input.AdvanceTo(readResult.Buffer.Start); |
| | | 99 | | } |
| | | 100 | | //EnsureDecoderAtEof(connection); |
| | 0 | 101 | | connection.EOF = true; |
| | | 102 | | } |
| | | 103 | | |
| | 50 | 104 | | if (connection.EOF) |
| | | 105 | | { |
| | 0 | 106 | | return (null, null); |
| | | 107 | | } |
| | | 108 | | |
| | 50 | 109 | | buffer = readResult.Buffer; |
| | 50 | 110 | | bool atEnvelopeStart = DecodeBytes(connection, ref buffer); |
| | 50 | 111 | | connection.Input.AdvanceTo(buffer.Start); |
| | 50 | 112 | | if (atEnvelopeStart) |
| | | 113 | | { |
| | | 114 | | break; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | |
| | 0 | 118 | | if (connection.EOF) |
| | | 119 | | { |
| | 0 | 120 | | return (null, null); |
| | | 121 | | } |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | // we're ready to read a message |
| | 50 | 125 | | Stream connectionStream = new SingletonInputConnectionStream(connection, connection.ServiceDispatcher.Bindin |
| | 50 | 126 | | Stream inputStream = new MaxMessageSizeStream(connectionStream, connection.MaxReceivedMessageSize); |
| | | 127 | | //using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBo |
| | | 128 | | //{ |
| | | 129 | | // if (DiagnosticUtility.ShouldUseActivity) |
| | | 130 | | // { |
| | | 131 | | // ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityProcessingMessage, TraceUtility.Retri |
| | | 132 | | // } |
| | | 133 | | |
| | | 134 | | Message message; |
| | | 135 | | try |
| | | 136 | | { |
| | 50 | 137 | | message = await connection.MessageEncoderFactory.Encoder.ReadMessageAsync( |
| | 50 | 138 | | inputStream, connection.MaxBufferSize, connection.FramingDecoder.ContentType); |
| | 50 | 139 | | } |
| | 0 | 140 | | catch (XmlException xmlException) |
| | | 141 | | { |
| | 0 | 142 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 143 | | new ProtocolException(SR.MessageXmlProtocolError, xmlException)); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | //if (DiagnosticUtility.ShouldUseActivity) |
| | | 147 | | //{ |
| | | 148 | | // TraceUtility.TransferFromTransport(message); |
| | | 149 | | //} |
| | | 150 | | |
| | 50 | 151 | | PrepareMessage(connection, message); |
| | | 152 | | |
| | 50 | 153 | | return (message, inputStream); |
| | | 154 | | //} |
| | 50 | 155 | | } |
| | | 156 | | |
| | | 157 | | private void PrepareMessage(FramingConnection connection, Message message) |
| | | 158 | | { |
| | 50 | 159 | | message.Properties.Via = connection.Via; |
| | 50 | 160 | | message.Properties.Security = (connection.SecurityMessageProperty != null) ? (SecurityMessageProperty)connec |
| | | 161 | | |
| | 50 | 162 | | IPEndPoint remoteEndPoint = connection.RemoteEndpoint; |
| | | 163 | | |
| | | 164 | | // pipes will return null |
| | 50 | 165 | | if (remoteEndPoint != null) |
| | | 166 | | { |
| | 50 | 167 | | var remoteEndpointProperty = new RemoteEndpointMessageProperty(remoteEndPoint); |
| | 50 | 168 | | message.Properties.Add(RemoteEndpointMessageProperty.Name, remoteEndpointProperty); |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | // TODO: ChannelBindingToken |
| | | 172 | | //if (this.channelBindingToken != null) |
| | | 173 | | //{ |
| | | 174 | | // ChannelBindingMessageProperty property = new ChannelBindingMessageProperty(this.channelBindingToken, f |
| | | 175 | | // property.AddTo(message); |
| | | 176 | | // property.Dispose(); //message.Properties.Add() creates a copy... |
| | | 177 | | //} |
| | 50 | 178 | | } |
| | | 179 | | |
| | | 180 | | private bool DecodeBytes(FramingConnection connection, ref ReadOnlySequence<byte> buffer) |
| | | 181 | | { |
| | 50 | 182 | | var decoder = connection.FramingDecoder as ServerSingletonDecoder; |
| | | 183 | | Fx.Assert(decoder != null, "FramingDecoder must be a non-null ServerSingletonDecoder"); |
| | 100 | 184 | | while (!connection.EOF && buffer.Length > 0) |
| | | 185 | | { |
| | 100 | 186 | | int bytesRead = decoder.Decode(buffer); |
| | 100 | 187 | | if (bytesRead > 0) |
| | | 188 | | { |
| | 50 | 189 | | buffer = buffer.Slice(bytesRead); |
| | | 190 | | } |
| | | 191 | | |
| | 100 | 192 | | switch (decoder.CurrentState) |
| | | 193 | | { |
| | | 194 | | case ServerSingletonDecoder.State.EnvelopeStart: |
| | | 195 | | // we're at the envelope |
| | 50 | 196 | | return true; |
| | | 197 | | |
| | | 198 | | case ServerSingletonDecoder.State.End: |
| | 0 | 199 | | connection.EOF = true; |
| | 0 | 200 | | return false; |
| | | 201 | | } |
| | | 202 | | } |
| | | 203 | | |
| | 0 | 204 | | return false; |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | // ensures that the reader is notified at end-of-stream, and takes care of the framing chunk headers |
| | | 208 | | private class SingletonInputConnectionStream : Stream |
| | | 209 | | { |
| | | 210 | | private readonly FramingConnection _connection; |
| | | 211 | | private readonly IDefaultCommunicationTimeouts _timeouts; |
| | | 212 | | private readonly SingletonMessageDecoder _decoder; |
| | 50 | 213 | | private ReadOnlySequence<byte> _buffer = ReadOnlySequence<byte>.Empty; |
| | | 214 | | private bool _atEof; |
| | | 215 | | private int _chunkBytesRemaining; |
| | | 216 | | private TimeoutHelper _timeoutHelper; |
| | | 217 | | |
| | 50 | 218 | | public SingletonInputConnectionStream(FramingConnection connection, |
| | 50 | 219 | | IDefaultCommunicationTimeouts defaultTimeouts) |
| | | 220 | | { |
| | 50 | 221 | | _connection = connection; |
| | 50 | 222 | | _timeouts = defaultTimeouts; |
| | 50 | 223 | | _decoder = new SingletonMessageDecoder(connection.Logger); |
| | 50 | 224 | | _chunkBytesRemaining = 0; |
| | 50 | 225 | | _timeoutHelper = new TimeoutHelper(_timeouts.ReceiveTimeout); |
| | 50 | 226 | | } |
| | | 227 | | |
| | 12 | 228 | | public override bool CanRead => true; |
| | | 229 | | |
| | 0 | 230 | | public override bool CanSeek => false; |
| | | 231 | | |
| | 0 | 232 | | public override bool CanWrite => false; |
| | | 233 | | |
| | 0 | 234 | | public override long Length => throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedExc |
| | | 235 | | |
| | | 236 | | public override long Position |
| | | 237 | | { |
| | | 238 | | get |
| | | 239 | | { |
| | 0 | 240 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.SeekNotSuppor |
| | | 241 | | } |
| | | 242 | | set |
| | | 243 | | { |
| | 0 | 244 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.SeekNotSuppor |
| | | 245 | | } |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | private void AbortReader() |
| | | 249 | | { |
| | 0 | 250 | | _connection.Abort(); |
| | 0 | 251 | | } |
| | | 252 | | |
| | | 253 | | public override void Close() |
| | | 254 | | { |
| | 50 | 255 | | _connection.EOF = _atEof; |
| | 50 | 256 | | } |
| | | 257 | | |
| | | 258 | | // run chunk data through the decoder |
| | | 259 | | private void DecodeData(ReadOnlySequence<byte> buffer) |
| | | 260 | | { |
| | 5278 | 261 | | while (buffer.Length > 0) |
| | | 262 | | { |
| | 2832 | 263 | | int bytesRead = _decoder.Decode(buffer); |
| | 2832 | 264 | | buffer = buffer.Slice(bytesRead); |
| | | 265 | | Fx.Assert(_decoder.CurrentState == SingletonMessageDecoder.State.ReadingEnvelopeBytes || _decoder.Cu |
| | | 266 | | } |
| | 2446 | 267 | | } |
| | | 268 | | |
| | | 269 | | // run the current data through the decoder to get valid message bytes |
| | | 270 | | private void DecodeSize(ref ReadOnlySequence<byte> buffer) |
| | | 271 | | { |
| | 1043 | 272 | | while (buffer.Length > 0) |
| | | 273 | | { |
| | 1043 | 274 | | int bytesRead = _decoder.Decode(buffer); |
| | | 275 | | |
| | 1043 | 276 | | if (bytesRead > 0) |
| | | 277 | | { |
| | 507 | 278 | | buffer = buffer.Slice(bytesRead); |
| | | 279 | | } |
| | | 280 | | |
| | 1043 | 281 | | switch (_decoder.CurrentState) |
| | | 282 | | { |
| | | 283 | | case SingletonMessageDecoder.State.ChunkStart: |
| | 386 | 284 | | _chunkBytesRemaining = _decoder.ChunkSize; |
| | 386 | 285 | | return; |
| | | 286 | | case SingletonMessageDecoder.State.End: |
| | 50 | 287 | | ProcessEof(); |
| | 50 | 288 | | return; |
| | | 289 | | } |
| | | 290 | | } |
| | 0 | 291 | | } |
| | | 292 | | |
| | | 293 | | private void EnsureBuffer(CancellationToken token) |
| | | 294 | | { |
| | 2982 | 295 | | EnsureBufferAsync(token).GetAwaiter().GetResult(); |
| | 2982 | 296 | | } |
| | | 297 | | |
| | | 298 | | private async Task EnsureBufferAsync(CancellationToken token) |
| | | 299 | | { |
| | 2982 | 300 | | if (_buffer.Length == 0 && !_atEof) |
| | | 301 | | { |
| | 92 | 302 | | if (!_connection.Input.TryRead(out ReadResult readResult)) |
| | | 303 | | { |
| | 43 | 304 | | readResult = await _connection.Input.ReadAsync(token).ConfigureAwait(false); |
| | | 305 | | } |
| | | 306 | | |
| | 92 | 307 | | if (readResult.IsCompleted) |
| | | 308 | | { |
| | 0 | 309 | | _atEof = true; |
| | 0 | 310 | | return; |
| | | 311 | | } |
| | | 312 | | |
| | 92 | 313 | | _buffer = readResult.Buffer; |
| | | 314 | | } |
| | 2982 | 315 | | } |
| | | 316 | | |
| | 0 | 317 | | public override void Flush() { /* NOP */ } |
| | | 318 | | |
| | | 319 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 320 | | { |
| | | 321 | | // TODO: Create a ReadByte override which is optimized for that single case |
| | 2210 | 322 | | CancellationToken ct = _timeoutHelper.GetCancellationToken(); |
| | 2210 | 323 | | int result = 0; |
| | 92 | 324 | | while (true) |
| | | 325 | | { |
| | 5092 | 326 | | if (count == 0) |
| | | 327 | | { |
| | 2110 | 328 | | return result; |
| | | 329 | | } |
| | | 330 | | |
| | | 331 | | try |
| | | 332 | | { |
| | 2982 | 333 | | EnsureBuffer(ct); |
| | 2982 | 334 | | } |
| | 0 | 335 | | catch (OperationCanceledException oce) |
| | | 336 | | { |
| | 0 | 337 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.Format(SR.Rece |
| | | 338 | | } |
| | | 339 | | |
| | 2982 | 340 | | if (_atEof) |
| | | 341 | | { |
| | 100 | 342 | | return result; |
| | | 343 | | } |
| | | 344 | | |
| | 2882 | 345 | | if (_chunkBytesRemaining > 0) // We're in the middle of a chunk. |
| | | 346 | | { |
| | | 347 | | // How many bytes to copy into the buffer passed to this method. The read from the input pipe mi |
| | | 348 | | // from the next chunk and we're not ready to consume them yet. Also we can't copy more bytes th |
| | 2446 | 349 | | int bytesToCopy = Math.Min((int)Math.Min((int)_buffer.Length, _chunkBytesRemaining), count); |
| | | 350 | | |
| | | 351 | | // When copying a ReadOnlySequence to a Span, they must be the same size so create a temporary |
| | | 352 | | // ReadOnlySequence which has the same number of bytes as we wish to copy. |
| | 2446 | 353 | | ReadOnlySequence<byte> _fromBuffer = _buffer.Slice(_buffer.Start, bytesToCopy); |
| | | 354 | | |
| | | 355 | | // keep decoder up to date |
| | 2446 | 356 | | DecodeData(_fromBuffer); |
| | | 357 | | |
| | | 358 | | // Consume those bytes from our buffer |
| | 2446 | 359 | | _buffer = _buffer.Slice(bytesToCopy); |
| | | 360 | | |
| | | 361 | | // TODO: Possible perf improvement would be to call ReadAsync and save the Task<ReadResult> with |
| | | 362 | | // likely have been completed before the next call to avoid blocking waiting for the Task to com |
| | | 363 | | |
| | | 364 | | // Create Span of the right size to copy the bytes to. |
| | 2446 | 365 | | var _toBuffer = new Span<byte>(buffer, offset, bytesToCopy); |
| | 2446 | 366 | | _fromBuffer.CopyTo(_toBuffer); |
| | | 367 | | // Fix up counts |
| | 2446 | 368 | | result += bytesToCopy; |
| | 2446 | 369 | | offset += bytesToCopy; |
| | 2446 | 370 | | count -= bytesToCopy; |
| | 2446 | 371 | | _chunkBytesRemaining -= bytesToCopy; |
| | | 372 | | } |
| | | 373 | | else |
| | | 374 | | { |
| | | 375 | | // We are starting a new chunk. Read the size, and loop around again |
| | 436 | 376 | | DecodeSize(ref _buffer); |
| | | 377 | | } |
| | | 378 | | |
| | | 379 | | // If the buffer has been exhausted, advance the input pipe to consume them and release the buffer |
| | 2882 | 380 | | if (_buffer.Length == 0) |
| | | 381 | | { |
| | 92 | 382 | | _connection.Input.AdvanceTo(_buffer.End); |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | //if (atEof) |
| | | 386 | | //{ |
| | | 387 | | // _connection.Input.Complete(); |
| | | 388 | | //} |
| | | 389 | | } |
| | | 390 | | } |
| | | 391 | | |
| | | 392 | | public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellati |
| | | 393 | | { |
| | 0 | 394 | | CancellationToken ct = new TimeoutHelper(_timeouts.ReceiveTimeout).GetCancellationToken(); |
| | 0 | 395 | | int result = 0; |
| | 0 | 396 | | while (true) |
| | | 397 | | { |
| | 0 | 398 | | if (count == 0) |
| | | 399 | | { |
| | 0 | 400 | | return result; |
| | | 401 | | } |
| | | 402 | | |
| | | 403 | | try |
| | | 404 | | { |
| | 0 | 405 | | await EnsureBufferAsync(ct); |
| | 0 | 406 | | } |
| | 0 | 407 | | catch (OperationCanceledException oce) |
| | | 408 | | { |
| | 0 | 409 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.Format(SR.Rece |
| | | 410 | | } |
| | | 411 | | |
| | 0 | 412 | | if (_atEof) |
| | | 413 | | { |
| | 0 | 414 | | return result; |
| | | 415 | | } |
| | | 416 | | |
| | 0 | 417 | | if (_chunkBytesRemaining > 0) // We're in the middle of a chunk. |
| | | 418 | | { |
| | | 419 | | // How many bytes to copy into the buffer passed to this method. The read from the input pipe mi |
| | | 420 | | // from the next chunk and we're not ready to consume them yet. Also we can't copy more bytes th |
| | 0 | 421 | | int bytesToCopy = Math.Min((int)Math.Min((int)_buffer.Length, _chunkBytesRemaining), count); |
| | | 422 | | |
| | | 423 | | // When copying a ReadOnlySequence to a Span, they must be the same size so create a temporary |
| | | 424 | | // ReadOnlySequence which has the same number of bytes as we wish to copy. |
| | 0 | 425 | | ReadOnlySequence<byte> _fromBuffer = _buffer.Slice(_buffer.Start, bytesToCopy); |
| | | 426 | | |
| | | 427 | | // keep decoder up to date |
| | 0 | 428 | | DecodeData(_fromBuffer); |
| | | 429 | | |
| | | 430 | | // Consume those bytes from our buffer |
| | 0 | 431 | | _buffer = _buffer.Slice(bytesToCopy); |
| | | 432 | | |
| | | 433 | | // Create an ArraySegment of the right size to copy the bytes to. The synchronous Read method us |
| | | 434 | | // you can't instantiate a Span<T> in an async method but there's an implicit case of ArraySegme |
| | 0 | 435 | | var _toBuffer = new ArraySegment<byte>(buffer, offset, bytesToCopy); |
| | 0 | 436 | | _fromBuffer.CopyTo(_toBuffer); |
| | | 437 | | // Fix up counts |
| | 0 | 438 | | result += bytesToCopy; |
| | 0 | 439 | | offset += bytesToCopy; |
| | 0 | 440 | | count -= bytesToCopy; |
| | 0 | 441 | | _chunkBytesRemaining -= bytesToCopy; |
| | | 442 | | } |
| | | 443 | | else |
| | | 444 | | { |
| | | 445 | | // We are starting a new chunk. Read the size, and loop around again |
| | 0 | 446 | | DecodeSize(ref _buffer); |
| | | 447 | | } |
| | | 448 | | |
| | | 449 | | // If the buffer has been exhausted, advance the input pipe to consume them and release the buffer |
| | 0 | 450 | | if (_buffer.Length == 0) |
| | | 451 | | { |
| | 0 | 452 | | _connection.Input.AdvanceTo(_buffer.End); |
| | | 453 | | } |
| | | 454 | | } |
| | 0 | 455 | | } |
| | | 456 | | |
| | | 457 | | public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object |
| | | 458 | | { |
| | 0 | 459 | | return ReadAsync(buffer, offset, count).ToApm(callback, state); |
| | | 460 | | } |
| | | 461 | | |
| | | 462 | | public override int EndRead(IAsyncResult result) |
| | | 463 | | { |
| | 0 | 464 | | return result.ToApmEnd<int>(); |
| | | 465 | | } |
| | | 466 | | |
| | 0 | 467 | | public override long Seek(long offset, SeekOrigin origin) => throw DiagnosticUtility.ExceptionUtility.ThrowH |
| | | 468 | | |
| | 0 | 469 | | public override void SetLength(long value) => throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new |
| | | 470 | | |
| | | 471 | | private void ProcessEof() |
| | | 472 | | { |
| | 50 | 473 | | if (!_atEof) |
| | | 474 | | { |
| | 50 | 475 | | _atEof = true; |
| | 50 | 476 | | if (_chunkBytesRemaining > 0 || _decoder.CurrentState != SingletonMessageDecoder.State.End) |
| | | 477 | | { |
| | 0 | 478 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(_decoder.CreatePrematureEOFException() |
| | | 479 | | } |
| | | 480 | | } |
| | 50 | 481 | | } |
| | | 482 | | |
| | 0 | 483 | | public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException(); |
| | | 484 | | } |
| | | 485 | | } |
| | | 486 | | } |