public async Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { var bytesRead = await _innerStream.ReadAsync(buffer, offset, count, cancellationToken); Console.WriteLine(Encoding.ASCII.GetString(buffer, offset, count)); return(bytesRead); }
public virtual async Task <int> ReadAsync(INetworkStream networkStream, byte[] buffer) { ThrowIf.ArgumentIsNull(networkStream, nameof(networkStream)); ThrowIf.ArgumentIsNull(buffer, nameof(buffer)); int bytesReceived = await networkStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); return(bytesReceived); }
/// <summary> /// Ensure that the buffer is full for a read operation. /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Returns a value indicating whether there was no more data to fill the buffer.</returns> async Task <bool> ReadBufferAsync(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (_index >= _bytesRead) { _index = 0; _buffer = new byte[_bufferLength]; _bytesRead = await _stream.ReadAsync(_buffer, 0, _buffer.Length, cancellationToken).ConfigureAwait(false); } return(_bytesRead > 0); }
/// <summary> /// Relay all received by one stream to another stream. /// </summary> public static Task RelayToAsync( this INetworkStream readStream, INetworkStream writeStream, CancellationToken token) { return(Task.Run(async() => { // Use a buffer that is as large as possible, but does not exceed // any of the two stream's capabilities. int bufferSize = Math.Min( writeStream.MaxWriteSize, Math.Max( MaxBufferSize, readStream.MinReadSize)); var buffer = new byte[bufferSize]; while (true) { token.ThrowIfCancellationRequested(); try { TraceSources.Compute.TraceVerbose( "NetworkStream [{0} > {1}]: Reading...", readStream, writeStream); int bytesRead = await readStream.ReadAsync( buffer, 0, buffer.Length, token).ConfigureAwait(false); if (bytesRead > 0) { TraceSources.Compute.TraceVerbose( "NetworkStream [{0} > {1}]: Relaying {2} bytes", readStream, writeStream, bytesRead); await writeStream.WriteAsync(buffer, 0, bytesRead, token).ConfigureAwait(false); } else { TraceSources.Compute.TraceVerbose( "NetworkStream [{0} > {1}]: gracefully closed connection", readStream, writeStream); // Propagate. await writeStream.CloseAsync(token).ConfigureAwait(false); break; } } catch (NetworkStreamClosedException e) { TraceSources.Compute.TraceWarning( "NetworkStream [{0} > {1}]: forcefully closed connection: {2}", readStream, writeStream, e.Message); // Propagate. await writeStream.CloseAsync(token).ConfigureAwait(false); break; } catch (Exception e) { TraceSources.Compute.TraceWarning( "NetworkStream [{0} > {1}]: Caught unhandled exception: {2} {3}", readStream, writeStream, e.Message, e.StackTrace); throw; } } })); }