| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Buffers; |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.IO.Pipelines; |
| | | 6 | | using System.Net.Quic; |
| | | 7 | | using System.Net.Sockets; |
| | | 8 | | using System.Runtime.Versioning; |
| | | 9 | | |
| | | 10 | | namespace IceRpc.Transports.Quic.Internal; |
| | | 11 | | |
| | | 12 | | [SupportedOSPlatform("linux")] |
| | | 13 | | [SupportedOSPlatform("macos")] |
| | | 14 | | [SupportedOSPlatform("windows")] |
| | | 15 | | internal class QuicPipeWriter : ReadOnlySequencePipeWriter |
| | | 16 | | { |
| | 1 | 17 | | public override bool CanGetUnflushedBytes => true; |
| | | 18 | | |
| | 1 | 19 | | public override long UnflushedBytes => _pipe.Writer.UnflushedBytes; |
| | | 20 | | |
| | 12 | 21 | | internal Task Closed { get; } |
| | | 22 | | |
| | | 23 | | private bool _isCompleted; |
| | | 24 | | private readonly Action _completeCallback; |
| | | 25 | | private readonly int _minSegmentSize; |
| | | 26 | | |
| | | 27 | | // We use a helper Pipe instead of a StreamPipeWriter over _stream because StreamPipeWriter does not provide a |
| | | 28 | | // WriteAsync with an endStream/completeWrites parameter while QuicStream does. |
| | | 29 | | private readonly Pipe _pipe; |
| | | 30 | | private readonly QuicStream _stream; |
| | | 31 | | |
| | | 32 | | private readonly Action _throwIfConnectionClosedOrDisposed; |
| | | 33 | | |
| | 7 | 34 | | public override void Advance(int bytes) => _pipe.Writer.Advance(bytes); |
| | | 35 | | |
| | | 36 | | // QuicPipeWriter does not support this method: the IceRPC core does not need it. And when the application code |
| | | 37 | | // installs a payload writer interceptor, this interceptor should never call it on "next". |
| | 0 | 38 | | public override void CancelPendingFlush() => throw new NotSupportedException(); |
| | | 39 | | |
| | | 40 | | public override void Complete(Exception? exception = null) |
| | 805 | 41 | | { |
| | 805 | 42 | | if (!_isCompleted) |
| | 503 | 43 | | { |
| | 503 | 44 | | if (exception is null && _pipe.Writer.UnflushedBytes > 0) |
| | 1 | 45 | | { |
| | 1 | 46 | | throw new InvalidOperationException( |
| | 1 | 47 | | $"Completing a {nameof(QuicPipeWriter)} without an exception is not allowed when this pipe writer ha |
| | | 48 | | } |
| | | 49 | | |
| | 502 | 50 | | _isCompleted = true; |
| | | 51 | | |
| | 502 | 52 | | if (exception is null) |
| | 497 | 53 | | { |
| | | 54 | | // Unlike Slic, it's important to complete the writes and not abort the stream. Data might still be |
| | | 55 | | // buffered for send on the QUIC stream and aborting the stream would discard this data. |
| | 497 | 56 | | _stream.CompleteWrites(); |
| | 497 | 57 | | } |
| | | 58 | | else |
| | 5 | 59 | | { |
| | | 60 | | // We don't use the application error code, it's irrelevant. |
| | 5 | 61 | | _stream.Abort(QuicAbortDirection.Write, errorCode: 0); |
| | 5 | 62 | | } |
| | | 63 | | |
| | 502 | 64 | | _completeCallback(); |
| | | 65 | | |
| | 502 | 66 | | _pipe.Writer.Complete(); |
| | 502 | 67 | | _pipe.Reader.Complete(); |
| | 502 | 68 | | } |
| | 804 | 69 | | } |
| | | 70 | | |
| | | 71 | | public override ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken) => |
| | | 72 | | // WriteAsync will flush the internal buffer |
| | 2 | 73 | | WriteAsync(ReadOnlySequence<byte>.Empty, endStream: false, cancellationToken); |
| | | 74 | | |
| | 2 | 75 | | public override Memory<byte> GetMemory(int sizeHint) => _pipe.Writer.GetMemory(sizeHint); |
| | | 76 | | |
| | 5 | 77 | | public override Span<byte> GetSpan(int sizeHint) => _pipe.Writer.GetSpan(sizeHint); |
| | | 78 | | |
| | | 79 | | public override ValueTask<FlushResult> WriteAsync( |
| | | 80 | | ReadOnlyMemory<byte> source, |
| | | 81 | | CancellationToken cancellationToken) => |
| | 5596 | 82 | | WriteAsync(new ReadOnlySequence<byte>(source), endStream: false, cancellationToken); |
| | | 83 | | |
| | | 84 | | public override async ValueTask<FlushResult> WriteAsync( |
| | | 85 | | ReadOnlySequence<byte> source, |
| | | 86 | | bool endStream, |
| | | 87 | | CancellationToken cancellationToken) |
| | 5605 | 88 | | { |
| | 5605 | 89 | | if (_isCompleted) |
| | 0 | 90 | | { |
| | 0 | 91 | | throw new InvalidOperationException("Writing is not allowed once the writer is completed."); |
| | | 92 | | } |
| | | 93 | | |
| | 5605 | 94 | | _throwIfConnectionClosedOrDisposed(); |
| | | 95 | | |
| | | 96 | | try |
| | 5603 | 97 | | { |
| | 5603 | 98 | | if (_pipe.Writer.UnflushedBytes > 0) |
| | 5 | 99 | | { |
| | 5 | 100 | | if (!source.IsEmpty && source.Length < _minSegmentSize) |
| | 3 | 101 | | { |
| | | 102 | | // When source fits in the last segment of _pipe.Writer, we copy it into _pipe.Writer. |
| | | 103 | | |
| | 3 | 104 | | Memory<byte> pipeMemory = _pipe.Writer.GetMemory(); |
| | 3 | 105 | | if (source.Length <= pipeMemory.Length) |
| | 2 | 106 | | { |
| | 2 | 107 | | source.CopyTo(pipeMemory.Span); |
| | 2 | 108 | | _pipe.Writer.Advance((int)source.Length); |
| | 2 | 109 | | source = ReadOnlySequence<byte>.Empty; |
| | 2 | 110 | | } |
| | | 111 | | else |
| | 1 | 112 | | { |
| | 1 | 113 | | _pipe.Writer.Advance(0); |
| | 1 | 114 | | } |
| | 3 | 115 | | } |
| | | 116 | | |
| | | 117 | | // Flush the internal pipe. |
| | 5 | 118 | | FlushResult flushResult = await _pipe.Writer.FlushAsync(CancellationToken.None).ConfigureAwait(false); |
| | 5 | 119 | | Debug.Assert(!flushResult.IsCanceled && !flushResult.IsCompleted); |
| | | 120 | | |
| | | 121 | | // Read the data from the pipe. |
| | 5 | 122 | | bool tryReadOk = _pipe.Reader.TryRead(out ReadResult readResult); |
| | 5 | 123 | | Debug.Assert(tryReadOk); |
| | 5 | 124 | | Debug.Assert(!readResult.IsCanceled && !readResult.IsCompleted && readResult.Buffer.Length > 0); |
| | | 125 | | |
| | | 126 | | try |
| | 5 | 127 | | { |
| | | 128 | | // Write buffered data to the stream |
| | 5 | 129 | | await WriteSequenceAsync( |
| | 5 | 130 | | readResult.Buffer, |
| | 5 | 131 | | completeWrites: endStream && source.IsEmpty, |
| | 5 | 132 | | cancellationToken).ConfigureAwait(false); |
| | 4 | 133 | | } |
| | | 134 | | finally |
| | 5 | 135 | | { |
| | 5 | 136 | | _pipe.Reader.AdvanceTo(readResult.Buffer.End); |
| | 5 | 137 | | } |
| | | 138 | | |
| | 4 | 139 | | if (source.IsEmpty) |
| | 3 | 140 | | { |
| | | 141 | | // We're done, we don't want to write again an empty sequence. |
| | 3 | 142 | | return new FlushResult(isCanceled: false, isCompleted: endStream); |
| | | 143 | | } |
| | 1 | 144 | | } |
| | | 145 | | |
| | 5599 | 146 | | if (source.IsEmpty && !endStream) |
| | 2 | 147 | | { |
| | | 148 | | // Nothing to do; this typically corresponds to a call to FlushAsync when there was no unflushed bytes. |
| | 2 | 149 | | return new FlushResult(isCanceled: false, isCompleted: false); |
| | | 150 | | } |
| | | 151 | | else |
| | 5597 | 152 | | { |
| | 5597 | 153 | | await WriteSequenceAsync(source, completeWrites: endStream, cancellationToken).ConfigureAwait(false); |
| | 5591 | 154 | | return new FlushResult(isCanceled: false, isCompleted: endStream); |
| | | 155 | | } |
| | | 156 | | } |
| | 6 | 157 | | catch (QuicException exception) when (exception.QuicError == QuicError.StreamAborted) |
| | 4 | 158 | | { |
| | 4 | 159 | | return new FlushResult(isCanceled: false, isCompleted: true); |
| | | 160 | | } |
| | 2 | 161 | | catch (QuicException exception) |
| | 2 | 162 | | { |
| | 2 | 163 | | throw exception.ToIceRpcException(); |
| | | 164 | | } |
| | 0 | 165 | | catch (SocketException exception) |
| | 0 | 166 | | { |
| | 0 | 167 | | throw exception.ToIceRpcException(); |
| | | 168 | | } |
| | | 169 | | // We don't wrap other exceptions |
| | | 170 | | |
| | | 171 | | async ValueTask WriteSequenceAsync( |
| | | 172 | | ReadOnlySequence<byte> sequence, |
| | | 173 | | bool completeWrites, |
| | | 174 | | CancellationToken cancellationToken) |
| | 5602 | 175 | | { |
| | 5602 | 176 | | if (sequence.IsSingleSegment) |
| | 5600 | 177 | | { |
| | 5600 | 178 | | await _stream.WriteAsync(sequence.First, completeWrites, cancellationToken).ConfigureAwait(false); |
| | 5593 | 179 | | } |
| | | 180 | | else |
| | 2 | 181 | | { |
| | 2 | 182 | | ReadOnlySequence<byte>.Enumerator enumerator = sequence.GetEnumerator(); |
| | 2 | 183 | | bool hasMore = enumerator.MoveNext(); |
| | 2 | 184 | | Debug.Assert(hasMore); |
| | | 185 | | do |
| | 5 | 186 | | { |
| | 5 | 187 | | ReadOnlyMemory<byte> buffer = enumerator.Current; |
| | 5 | 188 | | hasMore = enumerator.MoveNext(); |
| | 5 | 189 | | await _stream.WriteAsync(buffer, completeWrites: completeWrites && !hasMore, cancellationToken) |
| | 5 | 190 | | .ConfigureAwait(false); |
| | 5 | 191 | | } |
| | 5 | 192 | | while (hasMore); |
| | 2 | 193 | | } |
| | 5595 | 194 | | } |
| | 5600 | 195 | | } |
| | | 196 | | |
| | 522 | 197 | | internal QuicPipeWriter( |
| | 522 | 198 | | QuicStream stream, |
| | 522 | 199 | | MemoryPool<byte> pool, |
| | 522 | 200 | | int minSegmentSize, |
| | 522 | 201 | | Action completeCallback, |
| | 522 | 202 | | Action throwIfConnectionClosedOrDisposed) |
| | 522 | 203 | | { |
| | 522 | 204 | | _stream = stream; |
| | 522 | 205 | | _minSegmentSize = minSegmentSize; |
| | 522 | 206 | | _completeCallback = completeCallback; |
| | | 207 | | |
| | | 208 | | // This callback is used to check if the connection is closed or disposed before calling WriteAsync on the QUIC |
| | | 209 | | // stream. This check works around the use of the QuicError.OperationAborted error code for both reporting the |
| | | 210 | | // abortion of the in-progress write call and for reporting a closed connection before the operation process |
| | | 211 | | // starts. In this latter case, we want to report ConnectionAborted. |
| | 522 | 212 | | _throwIfConnectionClosedOrDisposed = throwIfConnectionClosedOrDisposed; |
| | | 213 | | |
| | | 214 | | // Create a pipe that never pauses on flush or write. The QUIC _stream.WriteAsync will block if the QUIC flow |
| | | 215 | | // control doesn't permit sending more data. |
| | | 216 | | // The readerScheduler doesn't matter (we don't call _pipe.Reader.ReadAsync) and the writerScheduler doesn't |
| | | 217 | | // matter (_pipe.Writer.FlushAsync never blocks). |
| | 522 | 218 | | _pipe = new(new PipeOptions( |
| | 522 | 219 | | pool: pool, |
| | 522 | 220 | | minimumSegmentSize: minSegmentSize, |
| | 522 | 221 | | pauseWriterThreshold: 0, |
| | 522 | 222 | | useSynchronizationContext: false)); |
| | | 223 | | |
| | 522 | 224 | | Closed = ClosedAsync(); |
| | | 225 | | |
| | | 226 | | async Task ClosedAsync() |
| | 522 | 227 | | { |
| | | 228 | | try |
| | 522 | 229 | | { |
| | 522 | 230 | | await _stream.WritesClosed.ConfigureAwait(false); |
| | 418 | 231 | | } |
| | 104 | 232 | | catch |
| | 104 | 233 | | { |
| | | 234 | | // Ignore failures. |
| | 104 | 235 | | } |
| | 522 | 236 | | } |
| | 522 | 237 | | } |
| | | 238 | | } |