DuplexPipeStreamExtensions.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Buffers;
  3. using System.IO;
  4. using System.IO.Pipelines;
  5. using System.Runtime.CompilerServices;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace FastGithub.FlowAnalyze
  9. {
  10. static class DuplexPipeStreamExtensions
  11. {
  12. public static Stream AsStream(this IDuplexPipe duplexPipe, bool throwOnCancelled = false)
  13. {
  14. return new DuplexPipeStream(duplexPipe, throwOnCancelled);
  15. }
  16. private class DuplexPipeStream : Stream
  17. {
  18. private readonly PipeReader input;
  19. private readonly PipeWriter output;
  20. private readonly bool throwOnCancelled;
  21. private volatile bool cancelCalled;
  22. public DuplexPipeStream(IDuplexPipe duplexPipe, bool throwOnCancelled = false)
  23. {
  24. this.input = duplexPipe.Input;
  25. this.output = duplexPipe.Output;
  26. this.throwOnCancelled = throwOnCancelled;
  27. }
  28. public void CancelPendingRead()
  29. {
  30. this.cancelCalled = true;
  31. this.input.CancelPendingRead();
  32. }
  33. public override bool CanRead => true;
  34. public override bool CanSeek => false;
  35. public override bool CanWrite => true;
  36. public override long Length
  37. {
  38. get
  39. {
  40. throw new NotSupportedException();
  41. }
  42. }
  43. public override long Position
  44. {
  45. get
  46. {
  47. throw new NotSupportedException();
  48. }
  49. set
  50. {
  51. throw new NotSupportedException();
  52. }
  53. }
  54. public override long Seek(long offset, SeekOrigin origin)
  55. {
  56. throw new NotSupportedException();
  57. }
  58. public override void SetLength(long value)
  59. {
  60. throw new NotSupportedException();
  61. }
  62. public override int Read(byte[] buffer, int offset, int count)
  63. {
  64. ValueTask<int> vt = ReadAsyncInternal(new Memory<byte>(buffer, offset, count), default);
  65. return vt.IsCompleted ?
  66. vt.Result :
  67. vt.AsTask().GetAwaiter().GetResult();
  68. }
  69. public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default)
  70. {
  71. return ReadAsyncInternal(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask();
  72. }
  73. public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default)
  74. {
  75. return ReadAsyncInternal(destination, cancellationToken);
  76. }
  77. public override void Write(byte[] buffer, int offset, int count)
  78. {
  79. WriteAsync(buffer, offset, count).GetAwaiter().GetResult();
  80. }
  81. public override async Task WriteAsync(byte[]? buffer, int offset, int count, CancellationToken cancellationToken)
  82. {
  83. await this.output.WriteAsync(buffer.AsMemory(offset, count), cancellationToken);
  84. }
  85. public override async ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default)
  86. {
  87. await this.output.WriteAsync(source, cancellationToken);
  88. }
  89. public override void Flush()
  90. {
  91. FlushAsync(CancellationToken.None).GetAwaiter().GetResult();
  92. }
  93. public override async Task FlushAsync(CancellationToken cancellationToken)
  94. {
  95. await this.output.FlushAsync(cancellationToken);
  96. }
  97. [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))]
  98. private async ValueTask<int> ReadAsyncInternal(Memory<byte> destination, CancellationToken cancellationToken)
  99. {
  100. while (true)
  101. {
  102. var result = await this.input.ReadAsync(cancellationToken);
  103. var readableBuffer = result.Buffer;
  104. try
  105. {
  106. if (this.throwOnCancelled && result.IsCanceled && this.cancelCalled)
  107. {
  108. // Reset the bool
  109. this.cancelCalled = false;
  110. throw new OperationCanceledException();
  111. }
  112. if (!readableBuffer.IsEmpty)
  113. {
  114. // buffer.Count is int
  115. var count = (int)Math.Min(readableBuffer.Length, destination.Length);
  116. readableBuffer = readableBuffer.Slice(0, count);
  117. readableBuffer.CopyTo(destination.Span);
  118. return count;
  119. }
  120. if (result.IsCompleted)
  121. {
  122. return 0;
  123. }
  124. }
  125. finally
  126. {
  127. this.input.AdvanceTo(readableBuffer.End, readableBuffer.End);
  128. }
  129. }
  130. }
  131. public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
  132. {
  133. return TaskToApm.Begin(ReadAsync(buffer, offset, count), callback, state);
  134. }
  135. public override int EndRead(IAsyncResult asyncResult)
  136. {
  137. return TaskToApm.End<int>(asyncResult);
  138. }
  139. public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
  140. {
  141. return TaskToApm.Begin(WriteAsync(buffer, offset, count), callback, state);
  142. }
  143. public override void EndWrite(IAsyncResult asyncResult)
  144. {
  145. TaskToApm.End(asyncResult);
  146. }
  147. }
  148. }
  149. }