| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Transports; |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.IO.Pipelines; |
| | | 7 | | |
| | | 8 | | namespace IceRpc.Internal; |
| | | 9 | | |
| | | 10 | | /// <summary>A helper class to write data to a duplex connection. It provides a PipeWriter-like API but is not a |
| | | 11 | | /// PipeWriter. Like a PipeWriter, its methods shouldn't be called concurrently.</summary> |
| | | 12 | | internal class IceDuplexConnectionWriter : IBufferWriter<byte>, IDisposable |
| | | 13 | | { |
| | 16 | 14 | | public long UnflushedBytes => _pipe.Writer.UnflushedBytes; |
| | | 15 | | |
| | | 16 | | private readonly IDuplexConnection _connection; |
| | | 17 | | private readonly Pipe _pipe; |
| | 372 | 18 | | private readonly SequenceCoupler _sequenceCoupler = new(); |
| | | 19 | | |
| | 64957 | 20 | | public void Advance(int bytes) => _pipe.Writer.Advance(bytes); |
| | | 21 | | |
| | | 22 | | public void Dispose() |
| | 372 | 23 | | { |
| | 372 | 24 | | _pipe.Writer.Complete(); |
| | 372 | 25 | | _pipe.Reader.Complete(); |
| | 372 | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | public Memory<byte> GetMemory(int sizeHint = 0) => _pipe.Writer.GetMemory(sizeHint); |
| | | 29 | | |
| | 64959 | 30 | | public Span<byte> GetSpan(int sizeHint = 0) => _pipe.Writer.GetSpan(sizeHint); |
| | | 31 | | |
| | | 32 | | /// <summary>Constructs a duplex connection writer.</summary> |
| | | 33 | | /// <param name="connection">The duplex connection to write to.</param> |
| | | 34 | | /// <param name="pool">The memory pool to use.</param> |
| | | 35 | | /// <param name="minimumSegmentSize">The minimum segment size for buffers allocated from <paramref name="pool"/>. |
| | | 36 | | /// </param> |
| | 372 | 37 | | internal IceDuplexConnectionWriter(IDuplexConnection connection, MemoryPool<byte> pool, int minimumSegmentSize) |
| | 372 | 38 | | { |
| | 372 | 39 | | _connection = connection; |
| | | 40 | | |
| | | 41 | | // The readerScheduler doesn't matter (we don't call _pipe.Reader.ReadAsync) and the writerScheduler doesn't |
| | | 42 | | // matter (_pipe.Writer.FlushAsync never blocks). |
| | 372 | 43 | | _pipe = new Pipe(new PipeOptions( |
| | 372 | 44 | | pool: pool, |
| | 372 | 45 | | minimumSegmentSize: minimumSegmentSize, |
| | 372 | 46 | | pauseWriterThreshold: 0, |
| | 372 | 47 | | useSynchronizationContext: false)); |
| | 372 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary>Flush the buffered data.</summary> |
| | | 51 | | internal ValueTask FlushAsync(CancellationToken cancellationToken) => |
| | 232 | 52 | | WriteAsync(ReadOnlySequence<byte>.Empty, cancellationToken); |
| | | 53 | | |
| | | 54 | | /// <summary>Writes a sequence of bytes.</summary> |
| | | 55 | | internal async ValueTask WriteAsync(ReadOnlySequence<byte> source, CancellationToken cancellationToken) |
| | 5636 | 56 | | { |
| | | 57 | | // Ice only calls WriteAsync or FlushAsync with unflushed bytes. |
| | 5636 | 58 | | Debug.Assert(_pipe.Writer.UnflushedBytes > 0); |
| | | 59 | | |
| | 5636 | 60 | | _ = await _pipe.Writer.FlushAsync(CancellationToken.None).ConfigureAwait(false); |
| | 5636 | 61 | | _pipe.Reader.TryRead(out ReadResult readResult); |
| | 5636 | 62 | | Debug.Assert(!readResult.IsCompleted && !readResult.IsCanceled); |
| | | 63 | | |
| | | 64 | | try |
| | 5636 | 65 | | { |
| | 5636 | 66 | | await _connection.WriteAsync( |
| | 5636 | 67 | | _sequenceCoupler.Concat(readResult.Buffer, source), |
| | 5636 | 68 | | cancellationToken).ConfigureAwait(false); |
| | 5626 | 69 | | } |
| | | 70 | | finally |
| | 5636 | 71 | | { |
| | 5636 | 72 | | _pipe.Reader.AdvanceTo(readResult.Buffer.End); |
| | 5636 | 73 | | } |
| | 5626 | 74 | | } |
| | | 75 | | } |