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