| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Buffers; |
| | | 4 | | using System.IO.Pipelines; |
| | | 5 | | using System.Net.Quic; |
| | | 6 | | using System.Net.Sockets; |
| | | 7 | | |
| | | 8 | | namespace IceRpc.Transports.Quic.Internal; |
| | | 9 | | |
| | | 10 | | /// <summary>Implements a PipeReader over a QuicStream.</summary> |
| | | 11 | | internal class QuicPipeReader : PipeReader |
| | | 12 | | { |
| | | 13 | | private bool _isCompleted; |
| | | 14 | | private readonly Action _completeCallback; |
| | | 15 | | private readonly Action _throwIfConnectionClosedOrDisposed; |
| | | 16 | | private readonly PipeReader _pipeReader; |
| | | 17 | | private readonly QuicStream _stream; |
| | | 18 | | |
| | | 19 | | // StreamPipeReader.AdvanceTo does not call the underlying stream and as a result does not throw any QuicException. |
| | 3513 | 20 | | public override void AdvanceTo(SequencePosition consumed) => AdvanceTo(consumed, consumed); |
| | | 21 | | |
| | | 22 | | public override void AdvanceTo(SequencePosition consumed, SequencePosition examined) => |
| | 26702 | 23 | | _pipeReader.AdvanceTo(consumed, examined); |
| | | 24 | | |
| | 4 | 25 | | public override void CancelPendingRead() => _pipeReader.CancelPendingRead(); |
| | | 26 | | |
| | | 27 | | public override void Complete(Exception? exception = null) |
| | 1040 | 28 | | { |
| | 1040 | 29 | | if (!_isCompleted) |
| | 1002 | 30 | | { |
| | 1002 | 31 | | _isCompleted = true; |
| | | 32 | | |
| | | 33 | | // We don't use the application error code, it's irrelevant. |
| | 1002 | 34 | | _stream.Abort(QuicAbortDirection.Read, errorCode: 0); |
| | | 35 | | |
| | | 36 | | // This does not call _stream.Dispose since leaveOpen is set to true. |
| | 1002 | 37 | | _pipeReader.Complete(); |
| | | 38 | | |
| | 1002 | 39 | | _completeCallback(); |
| | 1002 | 40 | | } |
| | 1040 | 41 | | } |
| | | 42 | | |
| | | 43 | | public override async Task CopyToAsync(Stream destination, CancellationToken cancellationToken) |
| | 0 | 44 | | { |
| | 0 | 45 | | _throwIfConnectionClosedOrDisposed(); |
| | | 46 | | try |
| | 0 | 47 | | { |
| | 0 | 48 | | await _pipeReader.CopyToAsync(destination, cancellationToken).ConfigureAwait(false); |
| | 0 | 49 | | } |
| | 0 | 50 | | catch (QuicException exception) |
| | 0 | 51 | | { |
| | 0 | 52 | | throw exception.ToIceRpcException(); |
| | | 53 | | } |
| | 0 | 54 | | catch (SocketException exception) |
| | 0 | 55 | | { |
| | 0 | 56 | | throw exception.ToIceRpcException(); |
| | | 57 | | } |
| | | 58 | | // We don't catch and wrap other exceptions. It could be for example an InvalidOperationException when |
| | | 59 | | // attempting to read while another read is in progress. |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | public override async Task CopyToAsync(PipeWriter writer, CancellationToken cancellationToken) |
| | 0 | 63 | | { |
| | 0 | 64 | | _throwIfConnectionClosedOrDisposed(); |
| | | 65 | | try |
| | 0 | 66 | | { |
| | 0 | 67 | | await _pipeReader.CopyToAsync(writer, cancellationToken).ConfigureAwait(false); |
| | 0 | 68 | | } |
| | 0 | 69 | | catch (QuicException exception) |
| | 0 | 70 | | { |
| | 0 | 71 | | throw exception.ToIceRpcException(); |
| | | 72 | | } |
| | 0 | 73 | | catch (SocketException exception) |
| | 0 | 74 | | { |
| | 0 | 75 | | throw exception.ToIceRpcException(); |
| | | 76 | | } |
| | | 77 | | // We don't catch and wrap other exceptions. It could be for example an InvalidOperationException when |
| | | 78 | | // attempting to read while another read is in progress. |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | public override async ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default) |
| | 26737 | 82 | | { |
| | | 83 | | // First check if there's buffered data. If the connection is closed, we still want to return this data. |
| | 26737 | 84 | | if (TryRead(out ReadResult readResult)) |
| | 2 | 85 | | { |
| | 2 | 86 | | return readResult; |
| | | 87 | | } |
| | | 88 | | |
| | 26735 | 89 | | _throwIfConnectionClosedOrDisposed(); |
| | | 90 | | |
| | | 91 | | try |
| | 26731 | 92 | | { |
| | 26731 | 93 | | return await _pipeReader.ReadAsync(cancellationToken).ConfigureAwait(false); |
| | | 94 | | } |
| | 18 | 95 | | catch (QuicException exception) |
| | 18 | 96 | | { |
| | 18 | 97 | | throw exception.ToIceRpcException(); |
| | | 98 | | } |
| | 0 | 99 | | catch (SocketException exception) |
| | 0 | 100 | | { |
| | 0 | 101 | | throw exception.ToIceRpcException(); |
| | | 102 | | } |
| | | 103 | | // We don't catch and wrap other exceptions. It could be for example an InvalidOperationException when |
| | | 104 | | // attempting to read while another read is in progress. |
| | 26711 | 105 | | } |
| | | 106 | | |
| | | 107 | | // StreamPipeReader.TryRead does not call the underlying QuicStream and as a result does not throw any |
| | | 108 | | // QuicException. |
| | 26737 | 109 | | public override bool TryRead(out ReadResult result) => _pipeReader.TryRead(out result); |
| | | 110 | | |
| | | 111 | | protected override async ValueTask<ReadResult> ReadAtLeastAsyncCore( |
| | | 112 | | int minimumSize, |
| | | 113 | | CancellationToken cancellationToken) |
| | 2 | 114 | | { |
| | | 115 | | try |
| | 2 | 116 | | { |
| | 2 | 117 | | return await _pipeReader.ReadAtLeastAsync(minimumSize, cancellationToken).ConfigureAwait(false); |
| | | 118 | | } |
| | 0 | 119 | | catch (QuicException exception) |
| | 0 | 120 | | { |
| | 0 | 121 | | throw exception.ToIceRpcException(); |
| | | 122 | | } |
| | 0 | 123 | | catch (SocketException exception) |
| | 0 | 124 | | { |
| | 0 | 125 | | throw exception.ToIceRpcException(); |
| | | 126 | | } |
| | | 127 | | // We don't catch and wrap other exceptions. It could be for example an InvalidOperationException when |
| | | 128 | | // attempting to read while another read is in progress. |
| | 2 | 129 | | } |
| | | 130 | | |
| | 1038 | 131 | | internal QuicPipeReader( |
| | 1038 | 132 | | QuicStream stream, |
| | 1038 | 133 | | MemoryPool<byte> pool, |
| | 1038 | 134 | | int minimumSegmentSize, |
| | 1038 | 135 | | Action completeCallback, |
| | 1038 | 136 | | Action throwIfConnectionClosed) |
| | 1038 | 137 | | { |
| | 1038 | 138 | | _stream = stream; |
| | 1038 | 139 | | _completeCallback = completeCallback; |
| | | 140 | | |
| | | 141 | | // This callback is used to check if the connection is closed or disposed before calling ReadAsync or TryRead on |
| | | 142 | | // the pipe reader. This check works around the use of the QuicError.OperationAborted error code for both |
| | | 143 | | // reporting the abortion of the in-progress read call and for reporting a closed connection before the |
| | | 144 | | // operation process starts. In this latter case, we want to report ConnectionAborted. |
| | 1038 | 145 | | _throwIfConnectionClosedOrDisposed = throwIfConnectionClosed; |
| | | 146 | | |
| | 1038 | 147 | | _pipeReader = Create( |
| | 1038 | 148 | | _stream, |
| | 1038 | 149 | | new StreamPipeReaderOptions(pool, minimumSegmentSize, minimumReadSize: -1, leaveOpen: true)); |
| | 1038 | 150 | | } |
| | | 151 | | } |