| | | 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; |
| | | 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 |
| | 35995 | 19 | | { |
| | 35995 | 20 | | ulong id = Volatile.Read(ref _id); |
| | 35995 | 21 | | if (id == ulong.MaxValue) |
| | 0 | 22 | | { |
| | 0 | 23 | | throw new InvalidOperationException("The stream ID isn't allocated yet."); |
| | | 24 | | } |
| | 35995 | 25 | | return id; |
| | 35995 | 26 | | } |
| | | 27 | | |
| | | 28 | | set |
| | 8037 | 29 | | { |
| | 8037 | 30 | | Debug.Assert(_id == ulong.MaxValue); |
| | 8037 | 31 | | Volatile.Write(ref _id, value); |
| | 8037 | 32 | | } |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public PipeReader Input => |
| | 23405 | 36 | | _inputPipeReader ?? throw new InvalidOperationException("A local unidirectional stream has no Input."); |
| | | 37 | | |
| | | 38 | | /// <inheritdoc/> |
| | 68638 | 39 | | public bool IsBidirectional { get; } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc/> |
| | 62026 | 42 | | public bool IsRemote { get; } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc/> |
| | 54285 | 45 | | public bool IsStarted => Volatile.Read(ref _id) != ulong.MaxValue; |
| | | 46 | | |
| | | 47 | | public PipeWriter Output => |
| | 17784 | 48 | | _outputPipeWriter ?? throw new InvalidOperationException("A remote unidirectional stream has no Output."); |
| | | 49 | | |
| | 4346 | 50 | | public Task WritesClosed => _writesClosedTcs.Task; |
| | | 51 | | |
| | 12588 | 52 | | internal int WindowUpdateThreshold => _connection.StreamWindowUpdateThreshold; |
| | | 53 | | |
| | | 54 | | private bool _closeReadsOnWritesClosure; |
| | | 55 | | private readonly SlicConnection _connection; |
| | 8068 | 56 | | private ulong _id = ulong.MaxValue; |
| | | 57 | | private readonly SlicPipeReader? _inputPipeReader; |
| | | 58 | | // This mutex protects _writesClosePending, _closeReadsOnWritesClosure. |
| | 8068 | 59 | | private readonly object _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; |
| | 8068 | 64 | | private readonly TaskCompletionSource _writesClosedTcs = new(TaskCreationOptions.RunContinuationsAsynchronously); |
| | | 65 | | private bool _writesClosePending; |
| | | 66 | | |
| | 8068 | 67 | | internal SlicStream(SlicConnection connection, bool isBidirectional, bool isRemote) |
| | 8068 | 68 | | { |
| | 8068 | 69 | | _connection = connection; |
| | | 70 | | |
| | 8068 | 71 | | IsBidirectional = isBidirectional; |
| | 8068 | 72 | | IsRemote = isRemote; |
| | | 73 | | |
| | 8068 | 74 | | if (!IsBidirectional) |
| | 5529 | 75 | | { |
| | 5529 | 76 | | if (IsRemote) |
| | 2754 | 77 | | { |
| | | 78 | | // Write-side of remote unidirectional stream is marked as closed. |
| | 2754 | 79 | | TrySetWritesClosed(); |
| | 2754 | 80 | | } |
| | | 81 | | else |
| | 2775 | 82 | | { |
| | | 83 | | // Read-side of local unidirectional stream is marked as closed. |
| | 2775 | 84 | | TrySetReadsClosed(); |
| | 2775 | 85 | | } |
| | 5529 | 86 | | } |
| | | 87 | | |
| | 8068 | 88 | | if (IsRemote || IsBidirectional) |
| | 5293 | 89 | | { |
| | 5293 | 90 | | _inputPipeReader = new SlicPipeReader(this, _connection); |
| | 5293 | 91 | | } |
| | | 92 | | |
| | 8068 | 93 | | if (!IsRemote || IsBidirectional) |
| | 5314 | 94 | | { |
| | 5314 | 95 | | _outputPipeWriter = new SlicPipeWriter(this, _connection); |
| | 5314 | 96 | | } |
| | 8068 | 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) => |
| | 20189 | 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) |
| | 1246 | 112 | | { |
| | 1246 | 113 | | if (TrySetReadsClosed()) |
| | 601 | 114 | | { |
| | 601 | 115 | | Debug.Assert(_inputPipeReader is not null); |
| | 601 | 116 | | _inputPipeReader.CompleteReads(closeException); |
| | 601 | 117 | | } |
| | 1246 | 118 | | if (TrySetWritesClosed()) |
| | 715 | 119 | | { |
| | 715 | 120 | | Debug.Assert(_outputPipeWriter is not null); |
| | 715 | 121 | | _outputPipeWriter.CompleteWrites(closeException); |
| | 715 | 122 | | } |
| | 1246 | 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) |
| | 8803 | 131 | | { |
| | 8803 | 132 | | bool writeReadsClosedFrame = false; |
| | | 133 | | |
| | 8803 | 134 | | lock (_mutex) |
| | 8803 | 135 | | { |
| | 8803 | 136 | | if (IsStarted && !_state.HasFlag(State.ReadsClosed)) |
| | 4597 | 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. |
| | 4597 | 143 | | if (graceful && |
| | 4597 | 144 | | IsBidirectional && |
| | 4597 | 145 | | IsRemote && |
| | 4597 | 146 | | !_state.HasFlag(State.WritesClosed) && |
| | 4597 | 147 | | !_writesClosePending) |
| | 866 | 148 | | { |
| | 866 | 149 | | _closeReadsOnWritesClosure = true; |
| | 866 | 150 | | } |
| | 3731 | 151 | | else if (!graceful || IsRemote) |
| | 2667 | 152 | | { |
| | | 153 | | // If forcefully closed because the input was completed before the data was fully read or if writes |
| | | 154 | | // are already closed and the stream is a remote stream, we send the StreamReadsClosed frame to |
| | | 155 | | // notify the peer that reads are closed. |
| | 2667 | 156 | | writeReadsClosedFrame = true; |
| | 2667 | 157 | | } |
| | 4597 | 158 | | } |
| | 8803 | 159 | | } |
| | | 160 | | |
| | 8803 | 161 | | if (writeReadsClosedFrame) |
| | 2667 | 162 | | { |
| | 2667 | 163 | | if (IsRemote) |
| | 2539 | 164 | | { |
| | | 165 | | // If it's a remote stream, we close writes before sending the StreamReadsClosed frame to ensure |
| | | 166 | | // _connection._bidirectionalStreamCount or _connection._unidirectionalStreamCount is decreased before |
| | | 167 | | // the peer receives the frame. This is necessary to prevent a race condition where the peer could |
| | | 168 | | // release the connection's bidirectional or unidirectional stream semaphore before this connection's |
| | | 169 | | // stream count is actually decreased. |
| | 2539 | 170 | | TrySetReadsClosed(); |
| | 2539 | 171 | | } |
| | | 172 | | |
| | | 173 | | try |
| | 2667 | 174 | | { |
| | 2667 | 175 | | WriteStreamFrame(FrameType.StreamReadsClosed, encode: null, writeReadsClosedFrame: false); |
| | 2667 | 176 | | } |
| | 0 | 177 | | catch (IceRpcException) |
| | 0 | 178 | | { |
| | | 179 | | // Ignore connection failures. |
| | 0 | 180 | | } |
| | | 181 | | |
| | 2667 | 182 | | if (!IsRemote) |
| | 128 | 183 | | { |
| | | 184 | | // We can now close reads to permit a new stream to be started. The peer will receive the |
| | | 185 | | // StreamReadsClosed frame before the new stream sends a Stream frame. |
| | 128 | 186 | | TrySetReadsClosed(); |
| | 128 | 187 | | } |
| | 2667 | 188 | | } |
| | | 189 | | else |
| | 6136 | 190 | | { |
| | 6136 | 191 | | TrySetReadsClosed(); |
| | 6136 | 192 | | } |
| | 8803 | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary>Closes the write-side of the stream. It's only called by <see cref="SlicPipeWriter.Complete" /> and |
| | | 196 | | /// never called concurrently.</summary> |
| | | 197 | | /// <param name="graceful"><see langword="true" /> if the application wrote all the stream data on the stream <see |
| | | 198 | | /// cref="Output" />; otherwise, <see langword="false" />.</param> |
| | | 199 | | internal void CloseWrites(bool graceful) |
| | 5258 | 200 | | { |
| | 5258 | 201 | | bool writeWritesClosedFrame = false; |
| | 5258 | 202 | | bool writeReadsClosedFrame = false; |
| | | 203 | | |
| | 5258 | 204 | | lock (_mutex) |
| | 5258 | 205 | | { |
| | 5258 | 206 | | if (IsStarted && !_state.HasFlag(State.WritesClosed) && !_writesClosePending) |
| | 1196 | 207 | | { |
| | 1196 | 208 | | writeReadsClosedFrame = _closeReadsOnWritesClosure; |
| | 1196 | 209 | | _writesClosePending = true; |
| | 1196 | 210 | | writeWritesClosedFrame = true; |
| | 1196 | 211 | | } |
| | 5258 | 212 | | } |
| | | 213 | | |
| | 5258 | 214 | | if (writeWritesClosedFrame) |
| | 1196 | 215 | | { |
| | 1196 | 216 | | if (IsRemote) |
| | 480 | 217 | | { |
| | | 218 | | // If it's a remote stream, we close writes before sending the StreamLast or StreamWritesClosed |
| | | 219 | | // frame to ensure _connection._bidirectionalStreamCount or _connection._unidirectionalStreamCount |
| | | 220 | | // is decreased before the peer receives the frame. This is necessary to prevent a race condition |
| | | 221 | | // where the peer could release the connection's bidirectional or unidirectional stream semaphore |
| | | 222 | | // before this connection's stream count is actually decreased. |
| | 480 | 223 | | TrySetWritesClosed(); |
| | 480 | 224 | | } |
| | | 225 | | |
| | 1196 | 226 | | if (graceful) |
| | 1112 | 227 | | { |
| | | 228 | | try |
| | 1112 | 229 | | { |
| | 1112 | 230 | | WriteStreamFrame(FrameType.StreamLast, encode: null, writeReadsClosedFrame); |
| | 1112 | 231 | | } |
| | 0 | 232 | | catch (IceRpcException) |
| | 0 | 233 | | { |
| | | 234 | | // Ignore connection failures. |
| | 0 | 235 | | } |
| | | 236 | | |
| | | 237 | | // If the stream is a local stream, writes are not closed until the StreamReadsClosed frame is |
| | | 238 | | // received from the peer (see ReceivedReadsClosedFrame). This ensures that the connection's |
| | | 239 | | // bidirectional or unidirectional stream semaphore is released only once the peer consumed the |
| | | 240 | | // buffered data. |
| | 1112 | 241 | | } |
| | | 242 | | else |
| | 84 | 243 | | { |
| | | 244 | | try |
| | 84 | 245 | | { |
| | 84 | 246 | | WriteStreamFrame(FrameType.StreamWritesClosed, encode: null, writeReadsClosedFrame); |
| | 84 | 247 | | } |
| | 0 | 248 | | catch (IceRpcException) |
| | 0 | 249 | | { |
| | | 250 | | // Ignore connection failures. |
| | 0 | 251 | | } |
| | | 252 | | |
| | 84 | 253 | | if (!IsRemote) |
| | 53 | 254 | | { |
| | | 255 | | // We can now close writes to allow starting a new stream. Since the sending of frames is |
| | | 256 | | // serialized over the connection, the peer will receive this StreamWritesClosed frame before |
| | | 257 | | // a new stream sends a StreamFrame frame. |
| | 53 | 258 | | TrySetWritesClosed(); |
| | 53 | 259 | | } |
| | 84 | 260 | | } |
| | 1196 | 261 | | } |
| | | 262 | | else |
| | 4062 | 263 | | { |
| | 4062 | 264 | | TrySetWritesClosed(); |
| | 4062 | 265 | | } |
| | 5258 | 266 | | } |
| | | 267 | | |
| | | 268 | | /// <summary>Notifies the stream of the amount of data consumed by the connection to send a <see |
| | | 269 | | /// cref="FrameType.Stream" /> or <see cref="FrameType.StreamLast" /> frame.</summary> |
| | | 270 | | /// <param name="size">The size of the stream frame.</param> |
| | 18161 | 271 | | internal void ConsumedSendCredit(int size) => _outputPipeWriter!.ConsumedSendCredit(size); |
| | | 272 | | |
| | | 273 | | /// <summary>Fills the given writer with stream data received on the connection.</summary> |
| | | 274 | | /// <param name="bufferWriter">The destination buffer writer.</param> |
| | | 275 | | /// <param name="byteCount">The amount of stream data to read.</param> |
| | | 276 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 277 | | internal ValueTask FillBufferWriterAsync( |
| | | 278 | | IBufferWriter<byte> bufferWriter, |
| | | 279 | | int byteCount, |
| | | 280 | | CancellationToken cancellationToken) => |
| | 18883 | 281 | | _connection.FillBufferWriterAsync(bufferWriter, byteCount, cancellationToken); |
| | | 282 | | |
| | | 283 | | /// <summary>Notifies the stream of the reception of a <see cref="FrameType.Stream" /> or <see |
| | | 284 | | /// cref="FrameType.StreamLast" /> frame.</summary> |
| | | 285 | | /// <param name="size">The size of the data carried by the stream frame.</param> |
| | | 286 | | /// <param name="endStream"><see langword="true" /> if the received stream frame is the <see |
| | | 287 | | /// cref="FrameType.StreamLast" /> frame; otherwise, <see langword="false" />.</param> |
| | | 288 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 289 | | internal ValueTask<bool> ReceivedDataFrameAsync(int size, bool endStream, CancellationToken cancellationToken) |
| | 18900 | 290 | | { |
| | 18900 | 291 | | Debug.Assert(_inputPipeReader is not null); |
| | 18900 | 292 | | if (_state.HasFlag(State.ReadsClosed)) |
| | 16 | 293 | | { |
| | 16 | 294 | | return new(false); |
| | | 295 | | } |
| | | 296 | | else |
| | 18884 | 297 | | { |
| | 18884 | 298 | | if (endStream && !IsRemote) |
| | 1064 | 299 | | { |
| | | 300 | | // For a local stream we can close reads after we have received the StreamLast frame. For remote |
| | | 301 | | // streams reads are closed after the application has consumed all the data. |
| | 1064 | 302 | | CloseReads(graceful: true); |
| | 1064 | 303 | | } |
| | 18884 | 304 | | return _inputPipeReader.ReceivedDataFrameAsync(size, endStream, cancellationToken); |
| | | 305 | | } |
| | 18900 | 306 | | } |
| | | 307 | | |
| | | 308 | | /// <summary>Notifies the stream of the reception of a <see cref="FrameType.StreamReadsClosed" /> frame.</summary> |
| | | 309 | | internal void ReceivedReadsClosedFrame() |
| | 2577 | 310 | | { |
| | 2577 | 311 | | TrySetWritesClosed(); |
| | 2577 | 312 | | _outputPipeWriter?.CompleteWrites(exception: null); |
| | 2577 | 313 | | } |
| | | 314 | | |
| | | 315 | | /// <summary>Notifies the stream of the reception of a <see cref="FrameType.StreamWindowUpdate" /> frame.</summary> |
| | | 316 | | /// <param name="frame">The body of the <see cref="FrameType.StreamWindowUpdate" /> frame.</param> |
| | | 317 | | internal void ReceivedWindowUpdateFrame(StreamWindowUpdateBody frame) |
| | 1991 | 318 | | { |
| | 1991 | 319 | | if (frame.WindowSizeIncrement > SlicTransportOptions.MaxWindowSize) |
| | 0 | 320 | | { |
| | 0 | 321 | | throw new IceRpcException( |
| | 0 | 322 | | IceRpcError.IceRpcError, |
| | 0 | 323 | | $"The window update is trying to increase the window size to a value larger than allowed."); |
| | | 324 | | } |
| | 1991 | 325 | | _outputPipeWriter!.ReceivedWindowUpdateFrame((int)frame.WindowSizeIncrement); |
| | 1991 | 326 | | } |
| | | 327 | | |
| | | 328 | | /// <summary>Notifies the stream of the reception of a <see cref="FrameType.StreamWritesClosed" /> frame.</summary> |
| | | 329 | | internal void ReceivedWritesClosedFrame() |
| | 80 | 330 | | { |
| | 80 | 331 | | TrySetReadsClosed(); |
| | | 332 | | |
| | | 333 | | // Read operations will return a TruncatedData error if the peer closed writes. |
| | 80 | 334 | | _inputPipeReader?.CompleteReads(new IceRpcException(IceRpcError.TruncatedData)); |
| | 80 | 335 | | } |
| | | 336 | | |
| | | 337 | | /// <summary>Notifies the stream of the window update.</summary> |
| | | 338 | | /// <param name="size">The amount of data consumed by the application on the stream <see cref="Input" />.</param> |
| | | 339 | | internal void WindowUpdate(int size) |
| | 2067 | 340 | | { |
| | | 341 | | try |
| | 2067 | 342 | | { |
| | | 343 | | // Notify the sender of the window update to permit the sending of additional data. |
| | 2067 | 344 | | WriteStreamFrame( |
| | 2067 | 345 | | FrameType.StreamWindowUpdate, |
| | 2067 | 346 | | new StreamWindowUpdateBody((ulong)size).Encode, |
| | 2067 | 347 | | writeReadsClosedFrame: false); |
| | 2067 | 348 | | } |
| | 0 | 349 | | catch (IceRpcException) |
| | 0 | 350 | | { |
| | | 351 | | // Ignore connection failures. |
| | 0 | 352 | | } |
| | 2067 | 353 | | } |
| | | 354 | | |
| | | 355 | | /// <summary>Writes a <see cref="FrameType.Stream" /> or <see cref="FrameType.StreamLast" /> frame on the |
| | | 356 | | /// connection.</summary> |
| | | 357 | | /// <param name="source1">The first stream frame data source.</param> |
| | | 358 | | /// <param name="source2">The second stream frame data source.</param> |
| | | 359 | | /// <param name="endStream"><see langword="true" /> to write a <see cref="FrameType.StreamLast" /> frame; otherwise, |
| | | 360 | | /// <see langword="false" />.</param> |
| | | 361 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 362 | | internal ValueTask<FlushResult> WriteStreamFrameAsync( |
| | | 363 | | ReadOnlySequence<byte> source1, |
| | | 364 | | ReadOnlySequence<byte> source2, |
| | | 365 | | bool endStream, |
| | | 366 | | CancellationToken cancellationToken) |
| | 15595 | 367 | | { |
| | 15595 | 368 | | bool writeReadsClosedFrame = false; |
| | 15595 | 369 | | if (endStream) |
| | 3475 | 370 | | { |
| | 3475 | 371 | | lock (_mutex) |
| | 3475 | 372 | | { |
| | 3475 | 373 | | writeReadsClosedFrame = _closeReadsOnWritesClosure; |
| | 3475 | 374 | | _writesClosePending = true; |
| | 3475 | 375 | | } |
| | 3475 | 376 | | } |
| | | 377 | | |
| | 15595 | 378 | | return _connection.WriteStreamDataFrameAsync( |
| | 15595 | 379 | | this, |
| | 15595 | 380 | | source1, |
| | 15595 | 381 | | source2, |
| | 15595 | 382 | | endStream, |
| | 15595 | 383 | | writeReadsClosedFrame, |
| | 15595 | 384 | | cancellationToken); |
| | 15595 | 385 | | } |
| | | 386 | | |
| | | 387 | | /// <summary>Notifies the stream that the <see cref="FrameType.StreamLast" /> was written by the |
| | | 388 | | /// connection.</summary> |
| | | 389 | | internal void WroteLastStreamFrame() |
| | 2577 | 390 | | { |
| | 2577 | 391 | | if (IsRemote) |
| | 1159 | 392 | | { |
| | 1159 | 393 | | TrySetWritesClosed(); |
| | 1159 | 394 | | } |
| | | 395 | | // For local streams, writes will be closed only once the peer sends the StreamReadsClosed frame. |
| | | 396 | | |
| | 2577 | 397 | | _writesClosedTcs.TrySetResult(); |
| | 2577 | 398 | | } |
| | | 399 | | |
| | | 400 | | /// <summary>Throws the connection closure exception if the connection is closed.</summary> |
| | 15638 | 401 | | internal void ThrowIfConnectionClosed() => _connection.ThrowIfClosed(); |
| | | 402 | | |
| | 12904 | 403 | | private bool TrySetReadsClosed() => TrySetState(State.ReadsClosed); |
| | | 404 | | |
| | | 405 | | private bool TrySetWritesClosed() |
| | 12331 | 406 | | { |
| | 12331 | 407 | | if (TrySetState(State.WritesClosed)) |
| | 8058 | 408 | | { |
| | 8058 | 409 | | _writesClosedTcs.TrySetResult(); |
| | 8058 | 410 | | return true; |
| | | 411 | | } |
| | | 412 | | else |
| | 4273 | 413 | | { |
| | 4273 | 414 | | return false; |
| | | 415 | | } |
| | 12331 | 416 | | } |
| | | 417 | | |
| | | 418 | | private bool TrySetState(State state) |
| | 25235 | 419 | | { |
| | 25235 | 420 | | if (_state.TrySetFlag(state, out int newState)) |
| | 16122 | 421 | | { |
| | 16122 | 422 | | if (newState.HasFlag(State.ReadsClosed | State.WritesClosed)) |
| | 8058 | 423 | | { |
| | | 424 | | // The stream reads and writes are closed, it's time to release the stream to either allow creating or |
| | | 425 | | // accepting a new stream. |
| | 8058 | 426 | | if (IsStarted) |
| | 8037 | 427 | | { |
| | 8037 | 428 | | _connection.ReleaseStream(this); |
| | 8037 | 429 | | } |
| | 8058 | 430 | | } |
| | 16122 | 431 | | return true; |
| | | 432 | | } |
| | | 433 | | else |
| | 9113 | 434 | | { |
| | 9113 | 435 | | return false; |
| | | 436 | | } |
| | 25235 | 437 | | } |
| | | 438 | | |
| | | 439 | | private void WriteStreamFrame(FrameType frameType, EncodeAction? encode, bool writeReadsClosedFrame) => |
| | 5930 | 440 | | _connection.WriteStreamFrame(stream: this, frameType, encode, writeReadsClosedFrame); |
| | | 441 | | |
| | | 442 | | [Flags] |
| | | 443 | | private enum State : int |
| | | 444 | | { |
| | | 445 | | ReadsClosed = 1, |
| | | 446 | | WritesClosed = 2 |
| | | 447 | | } |
| | | 448 | | } |