refactored for .NET standard 2

This commit is contained in:
Chris Wheeler 2019-06-22 18:43:30 -04:00
parent caa5604e9f
commit d0863fbdc4
22 changed files with 229 additions and 165 deletions

View File

@ -1,11 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
<LangVersion>7.1</LangVersion> <LangVersion>7.1</LangVersion>
<OutputType>Library</OutputType>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

View File

@ -111,7 +111,9 @@ namespace Capnp.Net.Runtime.Tests
Assert.AreEqual(expectedCount - i, length); Assert.AreEqual(expectedCount - i, length);
for (int j = 0; j < length; j++) for (int j = 0; j < length; j++)
{ {
Assert.AreEqual((ulong)(length - j), frame.Segments[i].Span[j]); var expected = (ulong) (length - j);
var actual = frame.Segments[i].Span[j];
Assert.AreEqual(expected, actual);
} }
} }
@ -165,7 +167,7 @@ namespace Capnp.Net.Runtime.Tests
txPump.Send(PackFrame(4)); txPump.Send(PackFrame(4));
txPump.Send(PackFrame(5)); txPump.Send(PackFrame(5));
Assert.IsTrue(SpinWait.SpinUntil(() => bc.Count == 8, 500)); Assert.IsTrue(SpinWait.SpinUntil(() => bc.Count == 8, 50000));
UnpackAndVerifyFrame(bc.Take(), 1); UnpackAndVerifyFrame(bc.Take(), 1);
UnpackAndVerifyFrame(bc.Take(), 8); UnpackAndVerifyFrame(bc.Take(), 8);

View File

@ -1,8 +1,8 @@
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using Capnp.Rpc; using Capnp.Rpc;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks.Dataflow;
namespace Capnp.Net.Runtime.Tests namespace Capnp.Net.Runtime.Tests
{ {

View File

@ -1,11 +1,13 @@
using System;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Capnp.Rpc; using Capnp.Rpc;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.Log­ging; using Microsoft.Extensions.Logging;
using System.Diagnostics; using System.Diagnostics;
using Exception = Capnp.Rpc.Exception;
namespace Capnp.Net.Runtime.Tests namespace Capnp.Net.Runtime.Tests
{ {
@ -41,7 +43,7 @@ namespace Capnp.Net.Runtime.Tests
int MediumTimeout => Debugger.IsAttached ? Timeout.Infinite : 2000; int MediumTimeout => Debugger.IsAttached ? Timeout.Infinite : 2000;
[TestMethod] [TestMethod, Timeout(10000)]
public void CreateAndDispose() public void CreateAndDispose()
{ {
(var server, var client) = SetupClientServerPair(); (var server, var client) = SetupClientServerPair();
@ -52,21 +54,29 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void ConnectAndDispose() public void ConnectAndDispose()
{ {
(var server, var client) = SetupClientServerPair(); (var server, var client) = SetupClientServerPair();
using (server) using (server)
using (client) using (client)
{
try
{ {
Assert.IsTrue(client.WhenConnected.Wait(MediumTimeout)); Assert.IsTrue(client.WhenConnected.Wait(MediumTimeout));
SpinWait.SpinUntil(() => server.ConnectionCount > 0, MediumTimeout); SpinWait.SpinUntil(() => server.ConnectionCount > 0, MediumTimeout);
Assert.AreEqual(1, server.ConnectionCount); Assert.AreEqual(1, server.ConnectionCount);
} }
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void ConnectNoServer() public void ConnectNoServer()
{ {
using (var client = new TcpRpcClient("localhost", TcpPort)) using (var client = new TcpRpcClient("localhost", TcpPort))
@ -75,7 +85,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void ConnectAndBootstrap() public void ConnectAndBootstrap()
{ {
(var server, var client) = SetupClientServerPair(); (var server, var client) = SetupClientServerPair();
@ -94,7 +104,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void ConnectNoBootstrap() public void ConnectNoBootstrap()
{ {
(var server, var client) = SetupClientServerPair(); (var server, var client) = SetupClientServerPair();
@ -112,7 +122,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void CallReturn() public void CallReturn()
{ {
(var server, var client) = SetupClientServerPair(); (var server, var client) = SetupClientServerPair();
@ -153,7 +163,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void CallCancelOnServer() public void CallCancelOnServer()
{ {
(var server, var client) = SetupClientServerPair(); (var server, var client) = SetupClientServerPair();
@ -188,7 +198,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void CallCancelOnClient() public void CallCancelOnClient()
{ {
ExpectingLogOutput = false; ExpectingLogOutput = false;
@ -233,7 +243,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void CallReturnAfterClientSideCancel() public void CallReturnAfterClientSideCancel()
{ {
ExpectingLogOutput = false; ExpectingLogOutput = false;
@ -295,7 +305,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void CallServerSideException() public void CallServerSideException()
{ {
(var server, var client) = SetupClientServerPair(); (var server, var client) = SetupClientServerPair();
@ -332,7 +342,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void PipelineBeforeReturn() public void PipelineBeforeReturn()
{ {
(var server, var client) = SetupClientServerPair(); (var server, var client) = SetupClientServerPair();
@ -405,7 +415,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void PipelineAfterReturn() public void PipelineAfterReturn()
{ {
(var server, var client) = SetupClientServerPair(); (var server, var client) = SetupClientServerPair();
@ -481,7 +491,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void PipelineMultiple() public void PipelineMultiple()
{ {
(var server, var client) = SetupClientServerPair(); (var server, var client) = SetupClientServerPair();
@ -598,7 +608,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void PipelineCallAfterDisposal() public void PipelineCallAfterDisposal()
{ {
(var server, var client) = SetupClientServerPair(); (var server, var client) = SetupClientServerPair();
@ -635,7 +645,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void PipelineCallDuringDisposal() public void PipelineCallDuringDisposal()
{ {
(var server, var client) = SetupClientServerPair(); (var server, var client) = SetupClientServerPair();

View File

@ -12,7 +12,7 @@ namespace Capnp.Net.Runtime.Tests
[TestClass] [TestClass]
public class TcpRpcAdvancedStuff: TestBase public class TcpRpcAdvancedStuff: TestBase
{ {
[TestMethod] [TestMethod, Timeout(10000)]
public void MultiConnect() public void MultiConnect()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -50,7 +50,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void TwoClients() public void TwoClients()
{ {
using (var server = SetupServer()) using (var server = SetupServer())

View File

@ -95,7 +95,7 @@ namespace Capnp.Net.Runtime.Tests
Assert.AreEqual(expected, line.Result); Assert.AreEqual(expected, line.Result);
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void BasicClient() public void BasicClient()
{ {
LaunchCompatTestProcess("server:Interface", stdout => LaunchCompatTestProcess("server:Interface", stdout =>
@ -126,7 +126,7 @@ namespace Capnp.Net.Runtime.Tests
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void BasicServer() public void BasicServer()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -143,7 +143,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void PipelineClient() public void PipelineClient()
{ {
LaunchCompatTestProcess("server:Pipeline", stdout => LaunchCompatTestProcess("server:Pipeline", stdout =>
@ -180,7 +180,7 @@ namespace Capnp.Net.Runtime.Tests
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void PipelineServer() public void PipelineServer()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -198,7 +198,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void ReleaseClient() public void ReleaseClient()
{ {
LaunchCompatTestProcess("server:MoreStuff", stdout => LaunchCompatTestProcess("server:MoreStuff", stdout =>
@ -231,7 +231,7 @@ namespace Capnp.Net.Runtime.Tests
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void ReleaseServer() public void ReleaseServer()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -259,7 +259,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void ReleaseOnCancelClient() public void ReleaseOnCancelClient()
{ {
// Since we have a threaded model, there is no way to deterministically provoke the situation // Since we have a threaded model, there is no way to deterministically provoke the situation
@ -337,7 +337,7 @@ namespace Capnp.Net.Runtime.Tests
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void ReleaseOnCancelServer() public void ReleaseOnCancelServer()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -354,7 +354,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void TestTailCallClient() public void TestTailCallClient()
{ {
LaunchCompatTestProcess("server:TailCaller", stdout => LaunchCompatTestProcess("server:TailCaller", stdout =>
@ -389,7 +389,7 @@ namespace Capnp.Net.Runtime.Tests
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void TestTailCallServer() public void TestTailCallServer()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -408,7 +408,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void CancelationServer() public void CancelationServer()
{ {
LaunchCompatTestProcess("server:MoreStuff", stdout => LaunchCompatTestProcess("server:MoreStuff", stdout =>
@ -432,13 +432,13 @@ namespace Capnp.Net.Runtime.Tests
cts.Cancel(); cts.Cancel();
Assert.IsTrue(destroyed.Task.Wait(MediumNonDbgTimeout)); Assert.IsTrue(destroyed.Task.Wait(MediumNonDbgTimeout));
Assert.IsFalse(cancelTask.IsCompletedSuccessfully); Assert.IsFalse(cancelTask.IsCompleted && !cancelTask.IsCanceled);
} }
} }
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void CancelationClient() public void CancelationClient()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -455,7 +455,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void PromiseResolveServer() public void PromiseResolveServer()
{ {
LaunchCompatTestProcess("server:MoreStuff", stdout => LaunchCompatTestProcess("server:MoreStuff", stdout =>
@ -497,7 +497,7 @@ namespace Capnp.Net.Runtime.Tests
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void PromiseResolveClient() public void PromiseResolveClient()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -516,7 +516,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void RetainAndReleaseServer() public void RetainAndReleaseServer()
{ {
var destructionPromise = new TaskCompletionSource<int>(); var destructionPromise = new TaskCompletionSource<int>();
@ -594,7 +594,7 @@ namespace Capnp.Net.Runtime.Tests
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void RetainAndReleaseClient() public void RetainAndReleaseClient()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -614,7 +614,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void CancelServer() public void CancelServer()
{ {
LaunchCompatTestProcess("server:MoreStuff", stdout => LaunchCompatTestProcess("server:MoreStuff", stdout =>
@ -655,7 +655,7 @@ namespace Capnp.Net.Runtime.Tests
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void CancelClient() public void CancelClient()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -672,7 +672,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void SendTwiceServer() public void SendTwiceServer()
{ {
LaunchCompatTestProcess("server:MoreStuff", stdout => LaunchCompatTestProcess("server:MoreStuff", stdout =>
@ -716,7 +716,7 @@ namespace Capnp.Net.Runtime.Tests
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void SendTwiceClient() public void SendTwiceClient()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -736,7 +736,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void EmbargoServer() public void EmbargoServer()
{ {
LaunchCompatTestProcess("server:MoreStuff", stdout => LaunchCompatTestProcess("server:MoreStuff", stdout =>
@ -803,7 +803,7 @@ namespace Capnp.Net.Runtime.Tests
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void EmbargoClient() public void EmbargoClient()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -873,13 +873,13 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void EmbargoErrorServer() public void EmbargoErrorServer()
{ {
LaunchCompatTestProcess("server:MoreStuff", EmbargoErrorImpl); LaunchCompatTestProcess("server:MoreStuff", EmbargoErrorImpl);
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void RepeatedEmbargoError() public void RepeatedEmbargoError()
{ {
LaunchCompatTestProcess("server:MoreStuff", stdout => LaunchCompatTestProcess("server:MoreStuff", stdout =>
@ -891,7 +891,7 @@ namespace Capnp.Net.Runtime.Tests
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void EmbargoErrorClient() public void EmbargoErrorClient()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -907,7 +907,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void EmbargoNullServer() public void EmbargoNullServer()
{ {
LaunchCompatTestProcess("server:MoreStuff", stdout => LaunchCompatTestProcess("server:MoreStuff", stdout =>
@ -956,7 +956,7 @@ namespace Capnp.Net.Runtime.Tests
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void EmbargoNullClient() public void EmbargoNullClient()
{ {
using (var server = SetupServer()) using (var server = SetupServer())
@ -972,7 +972,7 @@ namespace Capnp.Net.Runtime.Tests
} }
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void CallBrokenPromiseServer() public void CallBrokenPromiseServer()
{ {
LaunchCompatTestProcess("server:MoreStuff", stdout => LaunchCompatTestProcess("server:MoreStuff", stdout =>
@ -1009,7 +1009,7 @@ namespace Capnp.Net.Runtime.Tests
}); });
} }
[TestMethod] [TestMethod, Timeout(10000)]
public void CallBrokenPromiseClient() public void CallBrokenPromiseClient()
{ {
using (var server = SetupServer()) using (var server = SetupServer())

View File

@ -212,7 +212,7 @@ namespace Capnp.Net.Runtime.Tests
cts.Cancel(); cts.Cancel();
Assert.IsTrue(destroyed.Task.Wait(MediumNonDbgTimeout)); Assert.IsTrue(destroyed.Task.Wait(MediumNonDbgTimeout));
Assert.IsFalse(cancelTask.IsCompletedSuccessfully); Assert.IsFalse(cancelTask.IsCompleted && !cancelTask.IsCanceled);
} }
} }

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>Capnp</RootNamespace> <RootNamespace>Capnp</RootNamespace>
<LangVersion>7.2</LangVersion> <LangVersion>7.2</LangVersion>
</PropertyGroup> </PropertyGroup>

View File

@ -82,6 +82,8 @@ namespace Capnp
throw new ArgumentException("Segment must not have zero length"); throw new ArgumentException("Segment must not have zero length");
} }
lock (_writeLock) lock (_writeLock)
{ {
_writer.Write(frame.Segments.Count - 1); _writer.Write(frame.Segments.Count - 1);
@ -99,8 +101,7 @@ namespace Capnp
foreach (var segment in frame.Segments) foreach (var segment in frame.Segments)
{ {
var bytes = MemoryMarshal.Cast<ulong, byte>(segment.Span); var bytes = MemoryMarshal.Cast<ulong, byte>(segment.Span).ToArray();
_writer.Write(bytes); _writer.Write(bytes);
} }
} }
@ -123,7 +124,7 @@ namespace Capnp
/// to be part of normal operation. It does pass exceptions which arise due to I/O errors or invalid data. /// to be part of normal operation. It does pass exceptions which arise due to I/O errors or invalid data.
/// </summary> /// </summary>
/// <exception cref="ArgumentException">The underlying stream does not support reading or is already closed.</exception> /// <exception cref="ArgumentException">The underlying stream does not support reading or is already closed.</exception>
/// <exception cref="OverflowException">Received invalid data.</exception> /// <exception cref="InvalidDataException">Encountered Invalid Framing Data</exception>
/// <exception cref="OutOfMemoryException">Received a message with too many or too big segments, probably dues to invalid data.</exception> /// <exception cref="OutOfMemoryException">Received a message with too many or too big segments, probably dues to invalid data.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception> /// <exception cref="IOException">An I/O error occurs.</exception>
public void Run() public void Run()
@ -135,55 +136,19 @@ namespace Capnp
while (true) while (true)
{ {
IsWaitingForData = true; IsWaitingForData = true;
var frame = reader.ReadWireFrame();
uint scountm = reader.ReadUInt32();
int scount = checked((int)(scountm + 1));
var buffers = new Memory<ulong>[scount];
for (uint i = 0; i < scount; i++)
{
int size = checked((int)reader.ReadUInt32());
// This implementation will never send empty segments.
// Other implementations should not. An empty segment may also
// indicate and end-of-stream (stream closed) condition.
if (size == 0)
{
Logger.LogInformation("Received zero-sized segment, stopping interaction");
return;
}
buffers[i] = new Memory<ulong>(new ulong[size]);
}
if ((scount & 1) == 0)
{
// Padding
reader.ReadUInt32();
}
for (uint i = 0; i < scount; i++)
{
var buffer = MemoryMarshal.Cast<ulong, byte>(buffers[i].Span);
int got = reader.Read(buffer);
if (got != buffer.Length)
{
Logger.LogWarning("Received incomplete frame");
throw new EndOfStreamException("Expected more bytes according to framing header");
}
}
IsWaitingForData = false; IsWaitingForData = false;
FrameReceived?.Invoke(frame);
FrameReceived?.Invoke(new WireFrame(new ArraySegment<Memory<ulong>>(buffers, 0, scount)));
} }
} }
} }
catch (EndOfStreamException) catch (EndOfStreamException)
{ {
Logger.LogWarning("Encountered End of Stream");
}
catch (InvalidDataException e)
{
Logger.LogWarning(e.Message);
} }
catch (ObjectDisposedException) catch (ObjectDisposedException)
{ {

View File

@ -21,19 +21,26 @@ namespace Capnp
/// <exception cref="EndOfStreamException">The end of the stream is reached.</exception> /// <exception cref="EndOfStreamException">The end of the stream is reached.</exception>
/// <exception cref="ObjectDisposedException">The stream is closed.</exception> /// <exception cref="ObjectDisposedException">The stream is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception> /// <exception cref="IOException">An I/O error occurs.</exception>
/// <exception cref="OverflowException">Encountered invalid framing data.</exception> /// <exception cref="InvalidDataException">Encountered invalid framing data.</exception>
/// <exception cref="OutOfMemoryException">Too many or too large segments, probably due to invalid framing data.</exception> /// <exception cref="OutOfMemoryException">Too many or too large segments, probably due to invalid framing data.</exception>
public static WireFrame ReadSegments(Stream stream) public static WireFrame ReadSegments(Stream stream)
{ {
using (var reader = new BinaryReader(stream, Encoding.Default, true)) using (var reader = new BinaryReader(stream, Encoding.Default, true))
{ {
uint scountm = reader.ReadUInt32(); return reader.ReadWireFrame();
uint scount = checked(scountm + 1); }
}
public static WireFrame ReadWireFrame(this BinaryReader reader)
{
uint scount = reader.ReadUInt32();
if(scount++ == UInt32.MaxValue) throw new InvalidDataException("Encountered Invalid Framing Data");
var buffers = new Memory<ulong>[scount]; var buffers = new Memory<ulong>[scount];
for (uint i = 0; i < scount; i++) for (uint i = 0; i < scount; i++)
{ {
uint size = reader.ReadUInt32(); uint size = reader.ReadUInt32();
if(size==0) throw new EndOfStreamException("Stream Closed");
buffers[i] = new Memory<ulong>(new ulong[size]); buffers[i] = new Memory<ulong>(new ulong[size]);
} }
@ -43,18 +50,29 @@ namespace Capnp
reader.ReadUInt32(); reader.ReadUInt32();
} }
for (uint i = 0; i < scount; i++) FillBuffersFromFrames(buffers, scount, reader);
{
var buffer = MemoryMarshal.Cast<ulong, byte>(buffers[i].Span);
if (reader.Read(buffer) != buffer.Length)
{
throw new EndOfStreamException("Expected more bytes according to framing header");
}
}
return new WireFrame(buffers); return new WireFrame(buffers);
} }
public static void FillBuffersFromFrames(Memory<ulong>[] buffers, uint segmentCount, BinaryReader reader)
{
for (uint i = 0; i < segmentCount; i++)
{
var buffer = MemoryMarshal.Cast<ulong, byte>(buffers[i].Span.ToArray());
var tmpBuffer = reader.ReadBytes(buffer.Length);
if (tmpBuffer.Length != buffer.Length)
{
throw new InvalidDataException("Expected more bytes according to framing header");
}
for (int j = 0; j < buffers[i].Length; j++)
{
var value = BitConverter.ToUInt64(tmpBuffer, j*8);
buffers[i].Span[j] = value;
}
}
} }
} }
} }

View File

@ -208,7 +208,7 @@ namespace Capnp
var utf8Bytes = PrimitiveCast<byte>().Data; var utf8Bytes = PrimitiveCast<byte>().Data;
if (utf8Bytes.Length == 0) return string.Empty; if (utf8Bytes.Length == 0) return string.Empty;
var utf8GytesNoZterm = utf8Bytes.Slice(0, utf8Bytes.Length - 1); var utf8GytesNoZterm = utf8Bytes.Slice(0, utf8Bytes.Length - 1);
return Encoding.UTF8.GetString(utf8GytesNoZterm); return Encoding.UTF8.GetString(utf8GytesNoZterm.ToArray());
} }
IEnumerable<T> Enumerate() IEnumerable<T> Enumerate()

View File

@ -24,10 +24,10 @@ namespace Capnp
Coder<ulong>.Fn = (x, y) => x ^ y; Coder<ulong>.Fn = (x, y) => x ^ y;
Coder<float>.Fn = (x, y) => Coder<float>.Fn = (x, y) =>
{ {
int xi = BitConverter.SingleToInt32Bits(x); int xi = x.SingleToInt32();
int yi = BitConverter.SingleToInt32Bits(y); int yi = y.SingleToInt32();
int zi = xi ^ yi; int zi = xi ^ yi;
return BitConverter.Int32BitsToSingle(zi); return BitConverter.ToSingle(BitConverter.GetBytes(zi), 0);
}; };
Coder<double>.Fn = (x, y) => Coder<double>.Fn = (x, y) =>
{ {

View File

@ -52,7 +52,7 @@ namespace Capnp.Rpc
internal override void Export(IRpcEndpoint endpoint, CapDescriptor.WRITER writer) internal override void Export(IRpcEndpoint endpoint, CapDescriptor.WRITER writer)
{ {
if (WhenResolved.IsCompletedSuccessfully) if (WhenResolved.ReplacementTaskIsCompletedSuccessfully())
{ {
WhenResolved.Result.Export(endpoint, writer); WhenResolved.Result.Export(endpoint, writer);
} }

View File

@ -77,7 +77,8 @@ namespace Capnp.Rpc
{ {
lock (_reentrancyBlocker) lock (_reentrancyBlocker)
{ {
if (_resolvedCap.Task.IsCompletedSuccessfully)
if (_resolvedCap.Task.ReplacementTaskIsCompletedSuccessfully())
{ {
_resolvedCap.Task.Result.Export(endpoint, writer); _resolvedCap.Task.Result.Export(endpoint, writer);
} }

View File

@ -179,13 +179,9 @@ namespace Capnp.Rpc
uint RandId() uint RandId()
{ {
uint id = 0; var holder = new byte[4];
var idSpan = MemoryMarshal.CreateSpan(ref id, 1); _random.NextBytes(holder);
var idBytes = MemoryMarshal.Cast<uint, byte>(idSpan); return BitConverter.ToUInt32(holder,0);
_random.NextBytes(idBytes);
return id;
} }
uint AllocateExport(Skeleton providedCapability, out bool first) uint AllocateExport(Skeleton providedCapability, out bool first)
@ -237,7 +233,7 @@ namespace Capnp.Rpc
lock (_reentrancyBlocker) lock (_reentrancyBlocker)
{ {
while (!_questionTable.TryAdd(questionId, question)) while (!_questionTable.ReplacementTryAdd(questionId, question))
{ {
questionId = RandId(); questionId = RandId();
var oldQuestion = question; var oldQuestion = question;
@ -277,7 +273,7 @@ namespace Capnp.Rpc
{ {
uint id = RandId(); uint id = RandId();
while (!_pendingDisembargos.TryAdd(id, tcs)) while (!_pendingDisembargos.ReplacementTryAdd(id, tcs))
{ {
id = RandId(); id = RandId();
} }
@ -323,7 +319,7 @@ namespace Capnp.Rpc
bool added; bool added;
lock (_reentrancyBlocker) lock (_reentrancyBlocker)
{ {
added = _answerTable.TryAdd(req.QuestionId, pendingAnswer); added = _answerTable.ReplacementTryAdd(req.QuestionId, pendingAnswer);
} }
if (!added) if (!added)
@ -382,7 +378,7 @@ namespace Capnp.Rpc
bool added; bool added;
lock (_reentrancyBlocker) lock (_reentrancyBlocker)
{ {
added = _answerTable.TryAdd(req.QuestionId, pendingAnswer); added = _answerTable.ReplacementTryAdd(req.QuestionId, pendingAnswer);
} }
if (!added) if (!added)
@ -876,7 +872,7 @@ namespace Capnp.Rpc
lock (_reentrancyBlocker) lock (_reentrancyBlocker)
{ {
exists = _pendingDisembargos.Remove(disembargo.Context.ReceiverLoopback, out tcs); exists = _pendingDisembargos.ReplacementTryRemove(disembargo.Context.ReceiverLoopback, out tcs);
} }
if (exists) if (exists)
@ -950,7 +946,7 @@ namespace Capnp.Rpc
lock (_reentrancyBlocker) lock (_reentrancyBlocker)
{ {
exists = _answerTable.Remove(finish.QuestionId, out answer); exists = _answerTable.ReplacementTryRemove(finish.QuestionId, out answer);
} }
if (exists) if (exists)
@ -988,7 +984,7 @@ namespace Capnp.Rpc
if (rc.RefCount == 0) if (rc.RefCount == 0)
{ {
_exportTable.Remove(id); _exportTable.Remove(id);
_revExportTable.Remove(rc.Cap, out uint _); _revExportTable.ReplacementTryRemove(rc.Cap, out uint _);
} }
} }
catch (System.Exception) catch (System.Exception)

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -62,6 +63,10 @@ namespace Capnp.Rpc
{ {
throw new RpcException("TcpRpcClient is unable to connect", exception); throw new RpcException("TcpRpcClient is unable to connect", exception);
} }
catch (Exception e)
{
Logger.LogError("UNHANDLED EXCEPTION");
}
} }
async Task Connect(string host, int port) async Task Connect(string host, int port)
@ -103,6 +108,7 @@ namespace Capnp.Rpc
{ {
_rpcEngine = new RpcEngine(); _rpcEngine = new RpcEngine();
_client = new TcpClient(); _client = new TcpClient();
_client.ExclusiveAddressUse = false;
WhenConnected = Connect(host, port); WhenConnected = Connect(host, port);
} }
@ -119,7 +125,7 @@ namespace Capnp.Rpc
throw new InvalidOperationException("Connection not yet established"); throw new InvalidOperationException("Connection not yet established");
} }
if (!WhenConnected.IsCompletedSuccessfully) if (!WhenConnected.ReplacementTaskIsCompletedSuccessfully())
{ {
throw new InvalidOperationException("Connection not successfully established"); throw new InvalidOperationException("Connection not successfully established");
} }
@ -143,8 +149,9 @@ namespace Capnp.Rpc
Logger.LogError("Unable to join connection task within timeout"); Logger.LogError("Unable to join connection task within timeout");
} }
} }
catch (System.Exception) catch (System.Exception e)
{ {
Logger.LogError(e, "Failure disposing client");
} }
if (_pumpThread != null && !_pumpThread.Join(500)) if (_pumpThread != null && !_pumpThread.Join(500))

View File

@ -188,12 +188,10 @@ namespace Capnp.Rpc
{ {
_rpcEngine = new RpcEngine(); _rpcEngine = new RpcEngine();
_listener = new TcpListener(localAddr, port); _listener = new TcpListener(localAddr, port);
_listener.ExclusiveAddressUse = false;
_listener.Start(); _listener.Start();
_acceptorThread = new Thread(() => _acceptorThread = new Thread(AcceptClients);
{
AcceptClients();
});
_acceptorThread.Start(); _acceptorThread.Start();
} }

View File

@ -2528,7 +2528,7 @@ namespace Capnp.Rpc
} }
} }
public class Exception : ICapnpSerializable public class Exception : System.Exception, ICapnpSerializable
{ {
void ICapnpSerializable.Deserialize(DeserializerState arg_) void ICapnpSerializable.Deserialize(DeserializerState arg_)
{ {

View File

@ -285,9 +285,9 @@ namespace Capnp
public static float ReadDataFloat<T>(this T d, ulong bitOffset, float defaultValue = 0) public static float ReadDataFloat<T>(this T d, ulong bitOffset, float defaultValue = 0)
where T : IStructDeserializer where T : IStructDeserializer
{ {
int defaultBits = BitConverter.SingleToInt32Bits(defaultValue); int defaultBits = defaultValue.SingleToInt32();
int bits = (int)d.StructReadData(bitOffset, 32) ^ defaultBits; int bits = (int)d.StructReadData(bitOffset, 32) ^ defaultBits;
return BitConverter.Int32BitsToSingle(bits); return bits.Int32ToSingle();
} }
/// <summary> /// <summary>
@ -301,8 +301,8 @@ namespace Capnp
public static void WriteData<T>(this T d, ulong bitOffset, float value, float defaultValue = 0.0f) public static void WriteData<T>(this T d, ulong bitOffset, float value, float defaultValue = 0.0f)
where T : IStructSerializer where T : IStructSerializer
{ {
int bits = BitConverter.SingleToInt32Bits(value); int bits = value.SingleToInt32();
int defaultBits = BitConverter.SingleToInt32Bits(defaultValue); int defaultBits = defaultValue.SingleToInt32();
WriteData(d, bitOffset, bits, defaultBits); WriteData(d, bitOffset, bits, defaultBits);
} }

View File

@ -1068,7 +1068,7 @@ namespace Capnp
{ {
var bytes = ListGetBytes(); var bytes = ListGetBytes();
if (bytes.Length == 0) return string.Empty; if (bytes.Length == 0) return string.Empty;
return Encoding.UTF8.GetString(bytes.Slice(0, bytes.Length - 1)); return Encoding.UTF8.GetString(bytes.Slice(0, bytes.Length - 1).ToArray());
} }
/// <summary> /// <summary>
@ -1164,8 +1164,8 @@ namespace Capnp
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is out of bounds.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is out of bounds.</exception>
public void ListWriteValue(int index, float value, float defaultValue = 0) public void ListWriteValue(int index, float value, float defaultValue = 0)
{ {
int rcastValue = BitConverter.SingleToInt32Bits(value); int rcastValue = value.SingleToInt32();
int rcastDefaultValue = BitConverter.SingleToInt32Bits(defaultValue); int rcastDefaultValue = defaultValue.SingleToInt32();
ListWriteValue(index, rcastValue, rcastDefaultValue); ListWriteValue(index, rcastValue, rcastDefaultValue);
} }

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace Capnp
{
public static class UtilityExtensions
{
/// <summary>
/// This method exists until NET Standard 2.1 is released
/// </summary>
/// <param name="thisDict"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <typeparam name="K"></typeparam>
/// <typeparam name="V"></typeparam>
/// <returns></returns>
public static bool ReplacementTryAdd<K, V>(this Dictionary<K, V> thisDict, K key, V value)
{
if (thisDict.ContainsKey(key))
return false;
thisDict.Add(key, value);
return true;
}
/// <summary>
/// This method exists until NET Standard 2.1 is released
/// </summary>
/// <param name="thisDict"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <typeparam name="K"></typeparam>
/// <typeparam name="V"></typeparam>
/// <returns></returns>
public static bool ReplacementTryRemove<K, V>(this Dictionary<K, V> thisDict, K key, out V value)
{
if (!thisDict.ContainsKey(key))
{
value = default;
return false;
}
value = thisDict[key];
return thisDict.Remove(key);
}
/// <summary>
/// This method exists until NET Standard 2.1 is released
/// </summary>
/// <param name="task"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static bool ReplacementTaskIsCompletedSuccessfully(this Task task)
{
return task.IsCompleted && !task.IsCanceled && !task.IsFaulted;
}
public static int SingleToInt32(this float value)
{
var valueBytes = BitConverter.GetBytes(value);
return BitConverter.ToInt32(valueBytes,0);
}
public static float Int32ToSingle(this int value) =>
BitConverter.ToSingle(BitConverter.GetBytes(value),0);
}
}

View File

@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Capnp.Net.Runtime", "Capnp.
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "capnpc-csharp", "capnpc-csharp\capnpc-csharp.csproj", "{D19E5EA7-D35B-4A1F-99CB-ED136316B577}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "capnpc-csharp", "capnpc-csharp\capnpc-csharp.csproj", "{D19E5EA7-D35B-4A1F-99CB-ED136316B577}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "capnpc-binblob", "capnpc-binblob\capnpc-binblob.csproj", "{8C17F147-D784-4584-80FF-21BE03AC0D17}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Capnp.Net.Runtime.Tests", "Capnp.Net.Runtime.Tests\Capnp.Net.Runtime.Tests.csproj", "{9ED38750-F83F-4B10-B3A3-4FD6183F9E86}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Capnp.Net.Runtime.Tests", "Capnp.Net.Runtime.Tests\Capnp.Net.Runtime.Tests.csproj", "{9ED38750-F83F-4B10-B3A3-4FD6183F9E86}"
EndProject EndProject
Global Global