internal async Task ReceiveAsync(PipeWriter writer, CancellationToken cancellationToken) { try { FlushResult result; do { var memory = writer.GetMemory(MinimumBufferSize); var bytesRead = await _networkClient.ReceiveAsync( memory, cancellationToken) .ConfigureAwait(false); if (bytesRead == 0) { return; } Logger.Debug("Received {bytesRead} bytes", bytesRead); writer.Advance(bytesRead); result = await writer .FlushAsync(cancellationToken) .ConfigureAwait(false); } while (result.IsCanceled == false && result.IsCompleted == false); } catch (Exception ex) { writer.Complete(ex); throw; } writer.Complete(); }
public async ValueTask <byte[]> ReceiveAsync( CancellationToken cancellationToken = default) { using var memoryOwner = MemoryPool <byte> .Shared.Rent(65536); var memory = memoryOwner.Memory; var length = await _networkClient.ReceiveAsync(memory, cancellationToken) .ConfigureAwait(false); return(memory.Slice(0, length).ToArray()); }
private void RunReceiver() { _receiverTask = Task.Run( async() => { Exception?exception = null; try { FlushResult result; do { var bytes = await _networkClient.ReceiveAsync( _pipe .Writer .GetMemory(), _cancellationTokenSource .Token) .ConfigureAwait(false); _pipe.Writer.Advance(bytes); result = await _pipe .Writer.FlushAsync( _cancellationTokenSource.Token) .ConfigureAwait(false); } while (_cancellationTokenSource .IsCancellationRequested == false && result.IsCompleted == false && result.IsCanceled == false); } catch when(_cancellationTokenSource .IsCancellationRequested) { } catch (Exception exceptionCaught) { exception = exceptionCaught; } finally { await _pipe.Writer.CompleteAsync(exception) .ConfigureAwait(false); } });
public async ValueTask <int> ReceiveAsync(Memory <byte> buffer, CancellationToken cancellationToken = default) { return(await _first .ReceiveAsync(buffer, cancellationToken) .ConfigureAwait(false)); }