| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Transports.Internal; |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.IO.Pipelines; |
| | | 7 | | using ZeroC.Slice.Codec; |
| | | 8 | | |
| | | 9 | | namespace IceRpc.Transports.Slic.Internal; |
| | | 10 | | |
| | | 11 | | /// <summary>The stream implementation for Slic.</summary> |
| | | 12 | | /// <remarks>The stream implementation implements flow control to ensure data isn't buffered indefinitely if the |
| | | 13 | | /// application doesn't consume it.</remarks> |
| | | 14 | | internal class SlicStream : IMultiplexedStream |
| | | 15 | | { |
| | | 16 | | public ulong Id |
| | | 17 | | { |
| | | 18 | | get |
| | 17829 | 19 | | { |
| | 17829 | 20 | | ulong id = Volatile.Read(ref _id); |
| | 17829 | 21 | | if (id == ulong.MaxValue) |
| | 0 | 22 | | { |
| | 0 | 23 | | throw new InvalidOperationException("The stream ID isn't allocated yet."); |
| | | 24 | | } |
| | 17829 | 25 | | return id; |
| | 17829 | 26 | | } |
| | | 27 | | |
| | | 28 | | set |
| | 4253 | 29 | | { |
| | 4253 | 30 | | Debug.Assert(_id == ulong.MaxValue); |
| | 4253 | 31 | | Volatile.Write(ref _id, value); |
| | 4253 | 32 | | } |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public PipeReader Input => |
| | 13588 | 36 | | _inputPipeReader ?? throw new InvalidOperationException("A local unidirectional stream has no Input."); |
| | | 37 | | |
| | | 38 | | /// <inheritdoc/> |
| | 36352 | 39 | | public bool IsBidirectional { get; } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc/> |
| | 33797 | 42 | | public bool IsRemote { get; } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc/> |
| | 25575 | 45 | | public bool IsStarted => Volatile.Read(ref _id) != ulong.MaxValue; |
| | | 46 | | |
| | | 47 | | public PipeWriter Output => |
| | 9168 | 48 | | _outputPipeWriter ?? throw new InvalidOperationException("A remote unidirectional stream has no Output."); |
| | | 49 | | |
| | 2284 | 50 | | public Task WritesClosed => _writesClosedTcs.Task; |
| | | 51 | | |
| | 7283 | 52 | | internal int WindowUpdateThreshold => _connection.StreamWindowUpdateThreshold; |
| | | 53 | | |
| | | 54 | | private bool _closeReadsOnWritesClosure; |
| | | 55 | | private readonly SlicConnection _connection; |
| | 4276 | 56 | | private ulong _id = ulong.MaxValue; |
| | | 57 | | private readonly SlicPipeReader? _inputPipeReader; |
| | | 58 | | // This mutex protects _writesClosePending, _closeReadsOnWritesClosure. |
| | 4276 | 59 | | private readonly Lock _mutex = new(); |
| | | 60 | | private readonly SlicPipeWriter? _outputPipeWriter; |
| | | 61 | | // FlagEnumExtensions operations are used to update the state. These operations are atomic and don't require mutex |
| | | 62 | | // locking. |
| | | 63 | | private int _state; |
| | 4276 | 64 | | private readonly TaskCompletionSource _writesClosedTcs = new(TaskCreationOptions.RunContinuationsAsynchronously); |
| | | 65 | | private bool _writesClosePending; |
| | | 66 | | |
| | 4276 | 67 | | internal SlicStream(SlicConnection connection, bool isBidirectional, bool isRemote) |
| | 4276 | 68 | | { |
| | 4276 | 69 | | _connection = connection; |
| | | 70 | | |
| | 4276 | 71 | | IsBidirectional = isBidirectional; |
| | 4276 | 72 | | IsRemote = isRemote; |
| | | 73 | | |
| | 4276 | 74 | | if (!IsBidirectional) |
| | 2877 | 75 | | { |
| | 2877 | 76 | | if (IsRemote) |
| | 1434 | 77 | | { |
| | | 78 | | // Write-side of remote unidirectional stream is marked as closed. |
| | 1434 | 79 | | TrySetWritesClosed(); |
| | 1434 | 80 | | } |
| | | 81 | | else |
| | 1443 | 82 | | { |
| | | 83 | | // Read-side of local unidirectional stream is marked as closed. |
| | 1443 | 84 | | TrySetReadsClosed(); |
| | 1443 | 85 | | } |
| | 2877 | 86 | | } |
| | | 87 | | |
| | 4276 | 88 | | if (IsRemote || IsBidirectional) |
| | 2833 | 89 | | { |
| | 2833 | 90 | | _inputPipeReader = new SlicPipeReader(this, _connection); |
| | 2833 | 91 | | } |
| | | 92 | | |
| | 4276 | 93 | | if (!IsRemote || IsBidirectional) |
| | 2842 | 94 | | { |
| | 2842 | 95 | | _outputPipeWriter = new SlicPipeWriter(this, _connection); |
| | 2842 | 96 | | } |
| | 4276 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary>Acquires send credit.</summary> |
| | | 100 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 101 | | /// <returns>The available send credit.</returns> |
| | | 102 | | /// <remarks>This method should be called before sending a <see cref="FrameType.Stream"/> or <see |
| | | 103 | | /// cref="FrameType.StreamLast"/> frame to ensure enough send credit is available. If no send credit is available, |
| | | 104 | | /// it will block until send credit is available. The send credit matches the size of the peer's flow-control |
| | | 105 | | /// window.</remarks> |
| | | 106 | | internal ValueTask<int> AcquireSendCreditAsync(CancellationToken cancellationToken) => |
| | 9298 | 107 | | _outputPipeWriter!.AcquireSendCreditAsync(cancellationToken); |
| | | 108 | | |
| | | 109 | | /// <summary>Closes the read and write sides of the stream and notifies the stream <see cref="Input" /> and <see |
| | | 110 | | /// cref="Output" /> of the reads and writes closure.</summary> |
| | | 111 | | internal void Close(Exception closeException) |
| | 747 | 112 | | { |
| | 747 | 113 | | if (TrySetReadsClosed()) |
| | 360 | 114 | | { |
| | 360 | 115 | | Debug.Assert(_inputPipeReader is not null); |
| | 360 | 116 | | _inputPipeReader.CompleteReads(closeException); |
| | 360 | 117 | | } |
| | 747 | 118 | | if (TrySetWritesClosed()) |
| | 426 | 119 | | { |
| | 426 | 120 | | Debug.Assert(_outputPipeWriter is not null); |
| | 426 | 121 | | _outputPipeWriter.CompleteWrites(closeException); |
| | 426 | 122 | | } |
| | 747 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary>Closes the read-side of the stream. It's only called by <see cref="SlicPipeReader.Complete" />, <see |
| | | 126 | | /// cref="SlicPipeReader.TryRead" /> or <see cref="SlicPipeReader.ReadAsync" /> and never called concurrently. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <param name="graceful"><see langword="true" /> if the application consumed all the stream data from the stream |
| | | 129 | | /// <see cref="Input" />; otherwise, <see langword="false" />.</param> |
| | | 130 | | internal void CloseReads(bool graceful) |
| | 4773 | 131 | | { |
| | 4773 | 132 | | bool writeReadsClosedFrame = false; |
| | | 133 | | |
| | | 134 | | lock (_mutex) |
| | 4773 | 135 | | { |
| | 4773 | 136 | | if (IsStarted && !_state.HasFlag(State.ReadsClosed)) |
| | 2410 | 137 | | { |
| | | 138 | | // As an optimization, if reads are gracefully closed once the buffered data is consumed but before |
| | | 139 | | // writes are closed, we don't send the StreamReadsClosed frame just yet. Instead, when writes are |
| | | 140 | | // closed, CloseWrites will bundle the sending of the StreamReadsClosed with the sending of the |
| | | 141 | | // StreamLast or StreamWritesClosed frame. This allows to send both frames with a single write on the |
| | | 142 | | // duplex connection. If the peer closes its reads first, ReceivedReadsClosedFrame sends the frame on |
| | | 143 | | // its own. |
| | 2410 | 144 | | if (graceful && |
| | 2410 | 145 | | IsBidirectional && |
| | 2410 | 146 | | IsRemote && |
| | 2410 | 147 | | !_state.HasFlag(State.WritesClosed) && |
| | 2410 | 148 | | !_writesClosePending) |
| | 498 | 149 | | { |
| | 498 | 150 | | _closeReadsOnWritesClosure = true; |
| | 498 | 151 | | } |
| | 1912 | 152 | | else if (!graceful || IsRemote) |
| | 1344 | 153 | | { |
| | | 154 | | // If forcefully closed because the input was completed before the data was fully read or if writes |
| | | 155 | | // are already closed and the stream is a remote stream, we send the StreamReadsClosed frame to |
| | | 156 | | // notify the peer that reads are closed. |
| | 1344 | 157 | | writeReadsClosedFrame = true; |
| | 1344 | 158 | | } |
| | 2410 | 159 | | } |
| | 4773 | 160 | | } |
| | | 161 | | |
| | 4773 | 162 | | if (writeReadsClosedFrame) |
| | 1344 | 163 | | { |
| | 1344 | 164 | | if (IsRemote) |
| | 1266 | 165 | | { |
| | | 166 | | // If it's a remote stream, we close writes before sending the StreamReadsClosed frame to ensure |
| | | 167 | | // _connection._bidirectionalStreamCount or _connection._unidirectionalStreamCount is decreased before |
| | | 168 | | // the peer receives the frame. This is necessary to prevent a race condition where the peer could |
| | | 169 | | // release the connection's bidirectional or unidirectional stream semaphore before this connection's |
| | | 170 | | // stream count is actually decreased. |
| | 1266 | 171 | | TrySetReadsClosed(); |
| | 1266 | 172 | | } |
| | | 173 | | |
| | | 174 | | try |
| | 1344 | 175 | | { |
| | 1344 | 176 | | WriteStreamFrame(FrameType.StreamReadsClosed, encode: null, writeReadsClosedFrame: false); |
| | 1344 | 177 | | } |
| | 0 | 178 | | catch (IceRpcException) |
| | 0 | 179 | | { |
| | | 180 | | // Ignore connection failures. |
| | 0 | 181 | | } |
| | | 182 | | |
| | 1344 | 183 | | if (!IsRemote) |
| | 78 | 184 | | { |
| | | 185 | | // We can now close reads to permit a new stream to be started. The peer will receive the |
| | | 186 | | // StreamReadsClosed frame before the new stream sends a Stream frame. |
| | 78 | 187 | | TrySetReadsClosed(); |
| | 78 | 188 | | } |
| | 1344 | 189 | | } |
| | | 190 | | else |
| | 3429 | 191 | | { |
| | 3429 | 192 | | TrySetReadsClosed(); |
| | 3429 | 193 | | } |
| | 4773 | 194 | | } |
| | | 195 | | |
| | | 196 | | /// <summary>Closes the write-side of the stream. It's only called by <see cref="SlicPipeWriter.Complete" /> and |
| | | 197 | | /// never called concurrently.</summary> |
| | | 198 | | /// <param name="graceful"><see langword="true" /> if the application wrote all the stream data on the stream <see |
| | | 199 | | /// cref="Output" />; otherwise, <see langword="false" />.</param> |
| | | 200 | | internal void CloseWrites(bool graceful) |
| | 2811 | 201 | | { |
| | 2811 | 202 | | bool writeWritesClosedFrame = false; |
| | 2811 | 203 | | bool writeReadsClosedFrame = false; |
| | | 204 | | |
| | | 205 | | lock (_mutex) |
| | 2811 | 206 | | { |
| | 2811 | 207 | | if (IsStarted && !_state.HasFlag(State.WritesClosed) && !_writesClosePending) |
| | 645 | 208 | | { |
| | | 209 | | // The frame written below can't be canceled, so it claims the deferred StreamReadsClosed frame. This |
| | | 210 | | // also keeps ReceivedReadsClosedFrame from sending it before the stream is released. |
| | 645 | 211 | | writeReadsClosedFrame = _closeReadsOnWritesClosure; |
| | 645 | 212 | | _closeReadsOnWritesClosure = false; |
| | 645 | 213 | | _writesClosePending = true; |
| | 645 | 214 | | writeWritesClosedFrame = true; |
| | 645 | 215 | | } |
| | 2811 | 216 | | } |
| | | 217 | | |
| | 2811 | 218 | | if (writeWritesClosedFrame) |
| | 645 | 219 | | { |
| | 645 | 220 | | if (IsRemote) |
| | 257 | 221 | | { |
| | | 222 | | // If it's a remote stream, we close writes before sending the StreamLast or StreamWritesClosed |
| | | 223 | | // frame to ensure _connection._bidirectionalStreamCount or _connection._unidirectionalStreamCount |
| | | 224 | | // is decreased before the peer receives the frame. This is necessary to prevent a race condition |
| | | 225 | | // where the peer could release the connection's bidirectional or unidirectional stream semaphore |
| | | 226 | | // before this connection's stream count is actually decreased. |
| | 257 | 227 | | TrySetWritesClosed(); |
| | 257 | 228 | | } |
| | | 229 | | |
| | 645 | 230 | | if (graceful) |
| | 594 | 231 | | { |
| | | 232 | | try |
| | 594 | 233 | | { |
| | 594 | 234 | | WriteStreamFrame(FrameType.StreamLast, encode: null, writeReadsClosedFrame); |
| | 594 | 235 | | } |
| | 0 | 236 | | catch (IceRpcException) |
| | 0 | 237 | | { |
| | | 238 | | // Ignore connection failures. |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | // If the stream is a local stream, writes are not closed until the StreamReadsClosed frame is |
| | | 242 | | // received from the peer (see ReceivedReadsClosedFrame). This ensures that the connection's |
| | | 243 | | // bidirectional or unidirectional stream semaphore is released only once the peer consumed the |
| | | 244 | | // buffered data. |
| | 594 | 245 | | } |
| | | 246 | | else |
| | 51 | 247 | | { |
| | | 248 | | try |
| | 51 | 249 | | { |
| | 51 | 250 | | WriteStreamFrame(FrameType.StreamWritesClosed, encode: null, writeReadsClosedFrame); |
| | 51 | 251 | | } |
| | 0 | 252 | | catch (IceRpcException) |
| | 0 | 253 | | { |
| | | 254 | | // Ignore connection failures. |
| | 0 | 255 | | } |
| | | 256 | | |
| | 51 | 257 | | if (!IsRemote) |
| | 28 | 258 | | { |
| | | 259 | | // We can now close writes to allow starting a new stream. Since the sending of frames is |
| | | 260 | | // serialized over the connection, the peer will receive this StreamWritesClosed frame before |
| | | 261 | | // a new stream sends a StreamFrame frame. |
| | 28 | 262 | | TrySetWritesClosed(); |
| | 28 | 263 | | } |
| | 51 | 264 | | } |
| | 645 | 265 | | } |
| | 2166 | 266 | | else if (!IsStarted) |
| | 18 | 267 | | { |
| | | 268 | | // The peer knows nothing about an unstarted stream, so its writes are closed right away. |
| | 18 | 269 | | TrySetWritesClosed(); |
| | 18 | 270 | | } |
| | | 271 | | // Otherwise, the stream either already closed writes or, for a local stream, queued a StreamLast frame. Once |
| | | 272 | | // the peer's StreamReadsClosed frame is received (see ReceivedReadsClosedFrame), it closes writes and releases |
| | | 273 | | // its stream-count permit. |
| | 2811 | 274 | | } |
| | | 275 | | |
| | | 276 | | /// <summary>Notifies the stream of the amount of data consumed by the connection to send a <see |
| | | 277 | | /// cref="FrameType.Stream" /> or <see cref="FrameType.StreamLast" /> frame.</summary> |
| | | 278 | | /// <param name="size">The size of the stream frame.</param> |
| | 8287 | 279 | | internal void ConsumedSendCredit(int size) => _outputPipeWriter!.ConsumedSendCredit(size); |
| | | 280 | | |
| | | 281 | | /// <summary>Fills the given writer with stream data received on the connection.</summary> |
| | | 282 | | /// <param name="bufferWriter">The destination buffer writer.</param> |
| | | 283 | | /// <param name="byteCount">The amount of stream data to read.</param> |
| | | 284 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 285 | | internal ValueTask FillBufferWriterAsync( |
| | | 286 | | IBufferWriter<byte> bufferWriter, |
| | | 287 | | int byteCount, |
| | | 288 | | CancellationToken cancellationToken) => |
| | 8758 | 289 | | _connection.FillBufferWriterAsync(bufferWriter, byteCount, cancellationToken); |
| | | 290 | | |
| | | 291 | | /// <summary>Notifies the stream of the reception of a <see cref="FrameType.Stream" /> or <see |
| | | 292 | | /// cref="FrameType.StreamLast" /> frame.</summary> |
| | | 293 | | /// <param name="size">The size of the data carried by the stream frame.</param> |
| | | 294 | | /// <param name="endStream"><see langword="true" /> if the received stream frame is the <see |
| | | 295 | | /// cref="FrameType.StreamLast" /> frame; otherwise, <see langword="false" />.</param> |
| | | 296 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 297 | | internal ValueTask<bool> ReceivedDataFrameAsync(int size, bool endStream, CancellationToken cancellationToken) |
| | 8775 | 298 | | { |
| | 8775 | 299 | | Debug.Assert(_inputPipeReader is not null); |
| | 8775 | 300 | | if (_state.HasFlag(State.ReadsClosed)) |
| | 17 | 301 | | { |
| | 17 | 302 | | return new(false); |
| | | 303 | | } |
| | | 304 | | else |
| | 8758 | 305 | | { |
| | 8758 | 306 | | if (endStream && !IsRemote) |
| | 568 | 307 | | { |
| | | 308 | | // For a local stream we can close reads after we have received the StreamLast frame. For remote |
| | | 309 | | // streams reads are closed after the application has consumed all the data. |
| | 568 | 310 | | CloseReads(graceful: true); |
| | 568 | 311 | | } |
| | 8758 | 312 | | return _inputPipeReader.ReceivedDataFrameAsync(size, endStream, cancellationToken); |
| | | 313 | | } |
| | 8775 | 314 | | } |
| | | 315 | | |
| | | 316 | | /// <summary>Notifies the stream of the reception of a <see cref="FrameType.StreamReadsClosed" /> frame.</summary> |
| | | 317 | | internal void ReceivedReadsClosedFrame() |
| | 1723 | 318 | | { |
| | | 319 | | // Writes are closed before the deferral is captured: a CloseReads that runs afterwards sees writes closed and |
| | | 320 | | // sends the StreamReadsClosed frame itself. |
| | 1723 | 321 | | TrySetWritesClosed(); |
| | | 322 | | |
| | | 323 | | bool writeReadsClosedFrame; |
| | | 324 | | lock (_mutex) |
| | 1723 | 325 | | { |
| | | 326 | | // A pending StreamLast write may no longer carry the deferred StreamReadsClosed frame, so it's sent on its |
| | | 327 | | // own. |
| | 1723 | 328 | | writeReadsClosedFrame = _closeReadsOnWritesClosure; |
| | 1723 | 329 | | _closeReadsOnWritesClosure = false; |
| | 1723 | 330 | | } |
| | | 331 | | |
| | 1723 | 332 | | _outputPipeWriter?.CompleteWrites(exception: null); |
| | | 333 | | |
| | 1723 | 334 | | if (writeReadsClosedFrame) |
| | 10 | 335 | | { |
| | | 336 | | try |
| | 10 | 337 | | { |
| | 10 | 338 | | WriteStreamFrame(FrameType.StreamReadsClosed, encode: null, writeReadsClosedFrame: false); |
| | 10 | 339 | | } |
| | 0 | 340 | | catch (IceRpcException) |
| | 0 | 341 | | { |
| | | 342 | | // Ignore connection failures. |
| | 0 | 343 | | } |
| | 10 | 344 | | } |
| | 1723 | 345 | | } |
| | | 346 | | |
| | | 347 | | /// <summary>Notifies the stream of the reception of a <see cref="FrameType.StreamWindowUpdate" /> frame.</summary> |
| | | 348 | | /// <param name="frame">The body of the <see cref="FrameType.StreamWindowUpdate" /> frame.</param> |
| | | 349 | | internal void ReceivedWindowUpdateFrame(StreamWindowUpdateBody frame) |
| | 1198 | 350 | | { |
| | | 351 | | // The connection rejects window updates on remote unidirectional streams, the only streams with no output |
| | | 352 | | // pipe writer. |
| | 1198 | 353 | | Debug.Assert(_outputPipeWriter is not null); |
| | | 354 | | |
| | 1198 | 355 | | if (frame.WindowSizeIncrement == 0) |
| | 1 | 356 | | { |
| | 1 | 357 | | throw new InvalidDataException( |
| | 1 | 358 | | $"Received {nameof(FrameType.StreamWindowUpdate)} frame with a zero window size increment."); |
| | | 359 | | } |
| | 1197 | 360 | | if (frame.WindowSizeIncrement > SlicTransportOptions.MaxWindowSize) |
| | 0 | 361 | | { |
| | 0 | 362 | | throw new InvalidDataException( |
| | 0 | 363 | | "The window update is trying to increase the window size to a value larger than allowed."); |
| | | 364 | | } |
| | 1197 | 365 | | _outputPipeWriter.ReceivedWindowUpdateFrame((int)frame.WindowSizeIncrement); |
| | 1196 | 366 | | } |
| | | 367 | | |
| | | 368 | | /// <summary>Notifies the stream of the reception of a <see cref="FrameType.StreamWritesClosed" /> frame.</summary> |
| | | 369 | | internal void ReceivedWritesClosedFrame() |
| | 49 | 370 | | { |
| | 49 | 371 | | TrySetReadsClosed(); |
| | | 372 | | |
| | | 373 | | // Read operations will return a TruncatedData error if the peer closed writes. |
| | 49 | 374 | | _inputPipeReader?.CompleteReads(new IceRpcException(IceRpcError.TruncatedData)); |
| | 49 | 375 | | } |
| | | 376 | | |
| | | 377 | | /// <summary>Notifies the stream of the window update.</summary> |
| | | 378 | | /// <param name="size">The amount of data consumed by the application on the stream <see cref="Input" />.</param> |
| | | 379 | | internal void WindowUpdate(int size) |
| | 1248 | 380 | | { |
| | | 381 | | try |
| | 1248 | 382 | | { |
| | | 383 | | // Notify the sender of the window update to permit the sending of additional data. |
| | 1248 | 384 | | WriteStreamFrame( |
| | 1248 | 385 | | FrameType.StreamWindowUpdate, |
| | 1248 | 386 | | new StreamWindowUpdateBody((ulong)size).Encode, |
| | 1248 | 387 | | writeReadsClosedFrame: false); |
| | 1248 | 388 | | } |
| | 0 | 389 | | catch (IceRpcException) |
| | 0 | 390 | | { |
| | | 391 | | // Ignore connection failures. |
| | 0 | 392 | | } |
| | 1248 | 393 | | } |
| | | 394 | | |
| | | 395 | | /// <summary>Writes a <see cref="FrameType.Stream" /> or <see cref="FrameType.StreamLast" /> frame on the |
| | | 396 | | /// connection.</summary> |
| | | 397 | | /// <param name="source1">The first stream frame data source.</param> |
| | | 398 | | /// <param name="source2">The second stream frame data source.</param> |
| | | 399 | | /// <param name="endStream"><see langword="true" /> to write a <see cref="FrameType.StreamLast" /> frame; otherwise, |
| | | 400 | | /// <see langword="false" />.</param> |
| | | 401 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 402 | | internal ValueTask<FlushResult> WriteStreamFrameAsync( |
| | | 403 | | ReadOnlySequence<byte> source1, |
| | | 404 | | ReadOnlySequence<byte> source2, |
| | | 405 | | bool endStream, |
| | | 406 | | CancellationToken cancellationToken) |
| | 7985 | 407 | | { |
| | 7985 | 408 | | if (!endStream) |
| | 6172 | 409 | | { |
| | | 410 | | // Hot path: a non-final stream frame requires no writes-closure bookkeeping, so forward the inner |
| | | 411 | | // ValueTask directly without an async state machine. |
| | 6172 | 412 | | return _connection.WriteStreamDataFrameAsync( |
| | 6172 | 413 | | this, |
| | 6172 | 414 | | source1, |
| | 6172 | 415 | | source2, |
| | 6172 | 416 | | endStream: false, |
| | 6172 | 417 | | writeReadsClosedFrame: false, |
| | 6172 | 418 | | cancellationToken); |
| | | 419 | | } |
| | | 420 | | |
| | 1813 | 421 | | return WriteLastStreamFrameAsync(); |
| | | 422 | | |
| | | 423 | | async ValueTask<FlushResult> WriteLastStreamFrameAsync() |
| | 1813 | 424 | | { |
| | | 425 | | bool writeReadsClosedFrame; |
| | | 426 | | lock (_mutex) |
| | 1813 | 427 | | { |
| | 1813 | 428 | | writeReadsClosedFrame = _closeReadsOnWritesClosure; |
| | 1813 | 429 | | _writesClosePending = true; |
| | 1813 | 430 | | } |
| | | 431 | | |
| | | 432 | | try |
| | 1813 | 433 | | { |
| | 1813 | 434 | | return await _connection.WriteStreamDataFrameAsync( |
| | 1813 | 435 | | this, |
| | 1813 | 436 | | source1, |
| | 1813 | 437 | | source2, |
| | 1813 | 438 | | endStream: true, |
| | 1813 | 439 | | writeReadsClosedFrame, |
| | 1813 | 440 | | cancellationToken).ConfigureAwait(false); |
| | | 441 | | } |
| | 1007 | 442 | | catch when (!_writesClosedTcs.Task.IsCompleted) |
| | 2 | 443 | | { |
| | | 444 | | // The write failed before the StreamLast frame was queued on the connection (WroteLastStreamFrame |
| | | 445 | | // completes _writesClosedTcs when the frame is queued), so roll back _writesClosePending so that |
| | | 446 | | // completing the stream output can still close writes and notify the peer with a StreamWritesClosed |
| | | 447 | | // frame. The bundled reads closure isn't lost either since _closeReadsOnWritesClosure wasn't cleared. |
| | | 448 | | lock (_mutex) |
| | 2 | 449 | | { |
| | 2 | 450 | | _writesClosePending = false; |
| | 2 | 451 | | } |
| | 2 | 452 | | throw; |
| | | 453 | | } |
| | 806 | 454 | | } |
| | 7985 | 455 | | } |
| | | 456 | | |
| | | 457 | | /// <summary>Notifies the stream that the <see cref="FrameType.StreamLast" /> was written by the |
| | | 458 | | /// connection.</summary> |
| | | 459 | | internal void WroteLastStreamFrame() |
| | 1401 | 460 | | { |
| | 1401 | 461 | | if (IsRemote) |
| | 624 | 462 | | { |
| | 624 | 463 | | TrySetWritesClosed(); |
| | 624 | 464 | | } |
| | | 465 | | // For local streams, writes will be closed only once the peer sends the StreamReadsClosed frame. |
| | | 466 | | |
| | 1401 | 467 | | _writesClosedTcs.TrySetResult(); |
| | 1401 | 468 | | } |
| | | 469 | | |
| | | 470 | | /// <summary>Throws the connection closure exception if the connection is closed.</summary> |
| | 8012 | 471 | | internal void ThrowIfConnectionClosed() => _connection.ThrowIfClosed(); |
| | | 472 | | |
| | 7012 | 473 | | private bool TrySetReadsClosed() => TrySetState(State.ReadsClosed); |
| | | 474 | | |
| | | 475 | | private bool TrySetWritesClosed() |
| | 4831 | 476 | | { |
| | 4831 | 477 | | if (TrySetState(State.WritesClosed)) |
| | 4271 | 478 | | { |
| | 4271 | 479 | | _writesClosedTcs.TrySetResult(); |
| | 4271 | 480 | | return true; |
| | | 481 | | } |
| | | 482 | | else |
| | 560 | 483 | | { |
| | 560 | 484 | | return false; |
| | | 485 | | } |
| | 4831 | 486 | | } |
| | | 487 | | |
| | | 488 | | private bool TrySetState(State state) |
| | 11843 | 489 | | { |
| | 11843 | 490 | | if (_state.TrySetFlag(state, out int newState)) |
| | 8545 | 491 | | { |
| | 8545 | 492 | | if (newState.HasFlag(State.ReadsClosed | State.WritesClosed)) |
| | 4271 | 493 | | { |
| | | 494 | | // The stream reads and writes are closed, it's time to release the stream to either allow creating or |
| | | 495 | | // accepting a new stream. |
| | 4271 | 496 | | _connection.ReleaseStream(this); |
| | 4271 | 497 | | } |
| | 8545 | 498 | | return true; |
| | | 499 | | } |
| | | 500 | | else |
| | 3298 | 501 | | { |
| | 3298 | 502 | | return false; |
| | | 503 | | } |
| | 11843 | 504 | | } |
| | | 505 | | |
| | | 506 | | private void WriteStreamFrame(FrameType frameType, EncodeAction? encode, bool writeReadsClosedFrame) => |
| | 3247 | 507 | | _connection.WriteStreamFrame(stream: this, frameType, encode, writeReadsClosedFrame); |
| | | 508 | | |
| | | 509 | | [Flags] |
| | | 510 | | private enum State : int |
| | | 511 | | { |
| | | 512 | | ReadsClosed = 1, |
| | | 513 | | WritesClosed = 2 |
| | | 514 | | } |
| | | 515 | | } |