From 1ec5b0e955df435905b5397e5c08920af55aa98e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6llner?= Date: Sat, 1 Feb 2020 13:49:45 +0100 Subject: [PATCH] Improvements w.r.t. issue #37 --- Capnp.Net.Runtime.Tests/TcpRpcStress.cs | 11 ++++++++++- Capnp.Net.Runtime/Framing.cs | 3 ++- Capnp.Net.Runtime/Rpc/IConnection.cs | 7 +++++++ Capnp.Net.Runtime/Rpc/TcpRpcClient.cs | 7 +++++++ Capnp.Net.Runtime/Rpc/TcpRpcServer.cs | 7 +++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Capnp.Net.Runtime.Tests/TcpRpcStress.cs b/Capnp.Net.Runtime.Tests/TcpRpcStress.cs index 6ca2c14..8409ace 100644 --- a/Capnp.Net.Runtime.Tests/TcpRpcStress.cs +++ b/Capnp.Net.Runtime.Tests/TcpRpcStress.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Logging; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; +using System.Net; using System.Text; using System.Threading; @@ -94,9 +95,17 @@ namespace Capnp.Net.Runtime.Tests public void ScatteredTransfer() { - using (var server = SetupServer()) + using (var server = new TcpRpcServer(IPAddress.Any, TcpPort)) using (var client = new TcpRpcClient()) { + server.OnConnectionChanged += (_, e) => + { + if (e.Connection.State == ConnectionState.Initializing) + { + e.Connection.InjectMidlayer(s => new ScatteringStream(s, 7)); + } + }; + client.InjectMidlayer(s => new ScatteringStream(s, 10)); client.Connect("localhost", TcpPort); client.WhenConnected.Wait(); diff --git a/Capnp.Net.Runtime/Framing.cs b/Capnp.Net.Runtime/Framing.cs index bba2875..8e39cc5 100644 --- a/Capnp.Net.Runtime/Framing.cs +++ b/Capnp.Net.Runtime/Framing.cs @@ -110,13 +110,14 @@ namespace Capnp #else var buffer = MemoryMarshal.Cast(buffers[i].Span); - while (buffer.Length > 0) + do { int obtained = reader.Read(buffer); if (obtained == 0) throw StreamClosed(); buffer = buffer.Slice(obtained); } + while (buffer.Length > 0); #endif } } diff --git a/Capnp.Net.Runtime/Rpc/IConnection.cs b/Capnp.Net.Runtime/Rpc/IConnection.cs index 3a15f6f..b18fc5c 100644 --- a/Capnp.Net.Runtime/Rpc/IConnection.cs +++ b/Capnp.Net.Runtime/Rpc/IConnection.cs @@ -52,6 +52,13 @@ namespace Capnp.Rpc /// Connection is not in state 'Initializing' void AttachTracer(IFrameTracer tracer); + /// + /// Installs a midlayer. A midlayer is a protocal layer that resides somewhere between capnp serialization and the raw TCP stream. + /// Thus, we have a hook mechanism for transforming data before it is sent to the TCP connection or after it was received + /// by the TCP connection, respectively. This mechanism may be used for integrating various (de-)compression algorithms. + /// + /// Callback for wrapping the midlayer around its underlying stream + /// is null void InjectMidlayer(Func createFunc); /// diff --git a/Capnp.Net.Runtime/Rpc/TcpRpcClient.cs b/Capnp.Net.Runtime/Rpc/TcpRpcClient.cs index 293f9a5..def2f78 100644 --- a/Capnp.Net.Runtime/Rpc/TcpRpcClient.cs +++ b/Capnp.Net.Runtime/Rpc/TcpRpcClient.cs @@ -222,6 +222,13 @@ namespace Capnp.Rpc }; } + /// + /// Installs a midlayer. A midlayer is a protocal layer that resides somewhere between capnp serialization and the raw TCP stream. + /// Thus, we have a hook mechanism for transforming data before it is sent to the TCP connection or after it was received + /// by the TCP connection, respectively. This mechanism may be used for integrating various (de-)compression algorithms. + /// + /// Callback for wrapping the midlayer around its underlying stream + /// is null public void InjectMidlayer(Func createFunc) { if (createFunc == null) diff --git a/Capnp.Net.Runtime/Rpc/TcpRpcServer.cs b/Capnp.Net.Runtime/Rpc/TcpRpcServer.cs index f3c0553..131f0ce 100644 --- a/Capnp.Net.Runtime/Rpc/TcpRpcServer.cs +++ b/Capnp.Net.Runtime/Rpc/TcpRpcServer.cs @@ -128,6 +128,13 @@ namespace Capnp.Rpc Pump.AttachTracer(tracer); } + /// + /// Installs a midlayer. A midlayer is a protocal layer that resides somewhere between capnp serialization and the raw TCP stream. + /// Thus, we have a hook mechanism for transforming data before it is sent to the TCP connection or after it was received + /// by the TCP connection, respectively. This mechanism may be used for integrating various (de-)compression algorithms. + /// + /// Callback for wrapping the midlayer around its underlying stream + /// is null public void InjectMidlayer(Func createFunc) { if (createFunc == null)