diff --git a/Benchmarking/Benchmark/Benchmark.csproj b/Benchmarking/Benchmark/Benchmark.csproj index f9f1474..0471ab5 100644 --- a/Benchmarking/Benchmark/Benchmark.csproj +++ b/Benchmarking/Benchmark/Benchmark.csproj @@ -7,8 +7,8 @@ - - + + diff --git a/Benchmarking/EchoServiceCapnp/EchoServiceCapnp.csproj b/Benchmarking/EchoServiceCapnp/EchoServiceCapnp.csproj index 5954408..57d08f5 100644 --- a/Benchmarking/EchoServiceCapnp/EchoServiceCapnp.csproj +++ b/Benchmarking/EchoServiceCapnp/EchoServiceCapnp.csproj @@ -6,8 +6,8 @@ - - + + diff --git a/Capnp.Net.Runtime/Rpc/MidlayerExtensions.cs b/Capnp.Net.Runtime/Rpc/MidlayerExtensions.cs index 200aecd..eb6fbae 100644 --- a/Capnp.Net.Runtime/Rpc/MidlayerExtensions.cs +++ b/Capnp.Net.Runtime/Rpc/MidlayerExtensions.cs @@ -17,7 +17,7 @@ namespace Capnp.Rpc /// Buffer size (bytes). You should choose it according to the maximum expected raw capnp frame size public static void AddBuffering(this ISupportsMidlayers obj, int bufferSize) { - obj.InjectMidlayer(s => new Util.DuplexBufferedStream(new Util.AsyncNetworkStreamAdapter(s), bufferSize)); + obj.InjectMidlayer(s => new Util.DuplexBufferedStream(s, bufferSize)); } /// @@ -27,7 +27,7 @@ namespace Capnp.Rpc /// or public static void AddBuffering(this ISupportsMidlayers obj) { - obj.InjectMidlayer(s => new Util.DuplexBufferedStream(new Util.AsyncNetworkStreamAdapter(s))); + obj.InjectMidlayer(s => new Util.DuplexBufferedStream(s)); } } } diff --git a/Capnp.Net.Runtime/Util/AsyncNetworkStreamAdapter.cs b/Capnp.Net.Runtime/Util/AsyncNetworkStreamAdapter.cs deleted file mode 100644 index 9c9481e..0000000 --- a/Capnp.Net.Runtime/Util/AsyncNetworkStreamAdapter.cs +++ /dev/null @@ -1,111 +0,0 @@ -using System; -using System.IO; -using System.Net.Sockets; -using System.Threading; - -namespace Capnp.Util -{ - internal class AsyncNetworkStreamAdapter : Stream - { - // Async I/O pays off for large payloads. Perf. profiling gave a threshold around 200kB - const int DefaultAsyncThreshold = 200000; - - readonly NetworkStream _stream; - readonly int _asyncThreshold; - readonly object _reentrancyBlocker = new object(); - Exception? _bufferedException; - - public AsyncNetworkStreamAdapter(Stream stream, int asyncThreshold) - { - _asyncThreshold = asyncThreshold; - _stream = stream as NetworkStream ?? throw new ArgumentException("stream argument must be a NetworkStream"); - } - - public AsyncNetworkStreamAdapter(Stream stream): this(stream, DefaultAsyncThreshold) - { - } - - public override bool CanRead => true; - - public override bool CanSeek => false; - - public override bool CanWrite => true; - - public override long Length => 0; - - public override long Position - { - get => 0; - set => throw new NotSupportedException(); - } - - public override void Flush() - { - _stream.FlushAsync(); - //_stream.Flush(); - } - - public override int Read(byte[] buffer, int offset, int count) - { - return _stream.Read(buffer, offset, count); - } - - public override long Seek(long offset, SeekOrigin origin) - { - throw new NotSupportedException(); - } - - public override void SetLength(long value) - { - throw new NotSupportedException(); - } - - //void WriteCallback(IAsyncResult ar) - //{ - // try - // { - // _stream.EndWrite(ar); - // } - // catch (Exception exception) - // { - // Volatile.Write(ref _bufferedException, exception); - // } - //} - - public override void Write(byte[] buffer, int offset, int count) - { - _stream.WriteAsync(buffer, offset, count); - //var exception = Volatile.Read(ref _bufferedException); - - //if (exception != null) - //{ - // Dispose(); - // throw exception; - //} - - //if (count >= _asyncThreshold) - // _stream.BeginWrite(buffer, offset, count, WriteCallback, null); - //else - // _stream.Write(buffer, offset, count); - } - - protected override void Dispose(bool disposing) - { - if (disposing) - { - lock (_reentrancyBlocker) - { - try - { - _stream.Dispose(); - } - catch - { - } - } - } - - base.Dispose(disposing); - } - } -}