public override async ValueTask <ReadResult> ReadAsync(CancellationToken cancellationToken) { if (_bufferHasUnexaminedData == false && IsCompleted == false) { try { // memory based on default page size for MemoryPool being used var memory = _buffer.GetTrailingMemory(); _logger.LogTrace("TODO: ReadStarting"); var bytes = await _socket.ReceiveAsync(memory, cancellationToken); _logger.LogTrace("TODO: ReadComplete {bytes}", bytes); if (bytes != 0) { // advance rolling memory based on number of bytes received _buffer.TrailingMemoryFilled(bytes); // the new bytes have not been examined yet. this flag // is true until the parser calls AdvanceTo with // an examined SequencePosition corresponding to the tail _bufferHasUnexaminedData = true; } else { // reading 0 bytes means the remote client has // sent FIN and no more bytes will be received _isCompleted = true; _connection.FireConnectionClosed(); } } catch (TaskCanceledException) { _logger.LogTrace("TODO: ReadCanceled"); _isCanceled = true; _connection.FireConnectionClosed(); } catch (Exception ex) { _logger.LogTrace(ex, "TODO: ReadFailed"); // Return ReadResult.IsCompleted == true from now on // because we assume any read exceptions are not temporary _isCompleted = true; _connection.FireConnectionClosed(); #if NETSTANDARD2_0 FireWriterCompleted(ex); #endif } } return(new ReadResult( _buffer.GetOccupiedMemory(), isCanceled: IsCanceled, isCompleted: IsCompleted)); }
public override async ValueTask <ReadResult> ReadAsync(CancellationToken cancellationToken) { if (_bufferHasUnexaminedData == false && IsCompleted == false) { try { // memory based on default page size for MemoryPool being used var memory = _buffer.GetTrailingMemory(); if (_options.HighVolumeLogging) { _logger.ReadStarting(_connection.ConnectionId, memory.Length); } var bytes = await _socket.ReceiveAsync(memory, cancellationToken); if (_options.HighVolumeLogging) { _logger.ReadSucceeded(_connection.ConnectionId, bytes); } if (bytes != 0) { // advance rolling memory based on number of bytes received _buffer.TrailingMemoryFilled(bytes); // the new bytes have not been examined yet. this flag // is true until the parser calls AdvanceTo with // an examined SequencePosition corresponding to the tail _bufferHasUnexaminedData = true; } else { // reading 0 bytes means the remote client has // sent FIN and no more bytes will be received _isCompleted = true; } } catch (TaskCanceledException) { _logger.ReadCanceled(_connection.ConnectionId); _isCanceled = true; } catch (Exception error) { _logger.ReadFailed(_connection.ConnectionId, error); // Return ReadResult.IsCompleted == true from now on // because we assume any read exceptions are not temporary _isCompleted = true; // inform the protocol layer the remote client is abnormally unreadable _connection.FireConnectionClosed(); } } return(new ReadResult( _buffer.GetOccupiedMemory(), isCanceled: IsCanceled, isCompleted: IsCompleted)); }