public void Client() { using (var server = new TcpRpcServer()) using (var client = new TcpRpcClient()) { Assert.IsFalse(client.IsComputing); Assert.IsFalse(client.IsWaitingForData); Assert.AreEqual(0L, client.SendCount); Assert.AreEqual(0L, client.RecvCount); Assert.ThrowsException <InvalidOperationException>(() => client.GetMain <ITestInterface>()); Assert.ThrowsException <ArgumentNullException>(() => client.AttachTracer(null)); Assert.ThrowsException <ArgumentNullException>(() => client.InjectMidlayer(null)); (var addr, int port) = TcpManager.Instance.GetLocalAddressAndPort(); server.StartAccepting(addr, port); client.Connect(addr.ToString(), port); Assert.ThrowsException <InvalidOperationException>(() => client.Connect(addr.ToString(), port)); Assert.IsTrue(client.WhenConnected.Wait(MediumNonDbgTimeout)); Assert.ThrowsException <InvalidOperationException>(() => client.AttachTracer(new FrameTracing.RpcFrameTracer(Console.Out, false))); Assert.ThrowsException <InvalidOperationException>(() => client.InjectMidlayer(_ => _)); Assert.AreEqual(port, client.RemotePort); Assert.IsTrue(client.LocalPort != 0); Assert.AreEqual(0L, client.SendCount); Assert.AreEqual(0L, client.RecvCount); Assert.IsTrue(SpinWait.SpinUntil(() => client.IsWaitingForData, MediumNonDbgTimeout)); ((IConnection)client).Close(); Assert.IsTrue(SpinWait.SpinUntil(() => client.State == ConnectionState.Down, MediumNonDbgTimeout)); } }
static async Task Main(string[] args) { if (args.Length > 0) { var pair = new EnginePair(); pair.Engine1.Main = new CapnpEchoService(); var echoer = (CapabilityReflection.CreateProxy <IEchoer>(pair.Endpoint2.QueryMain()) as IEchoer); await Run(echoer); } else { using var server = new TcpRpcServer(); server.Main = new CapnpEchoService(); server.AddBuffering(); server.StartAccepting(IPAddress.Any, 5002); using var client = new TcpRpcClient(); client.AddBuffering(); client.Connect("localhost", 5002); await client.WhenConnected; using var echoer = client.GetMain <IEchoer>(); await Run(echoer); } }
protected static TcpRpcServer SetupServer(IPAddress addr, int port) { var server = new TcpRpcServer(); server.AddBuffering(); server.StartAccepting(addr, port); return(server); }
static void Main(string[] args) { using (var server = new TcpRpcServer()) { server.AddBuffering(); server.Main = new CapnpEchoService(); server.StartAccepting(IPAddress.Any, 5002); Console.WriteLine("Press RETURN to stop listening"); Console.ReadLine(); } }
public void Server3() { using (var server = new TcpRpcServer()) { server.Main = new TestInterfaceImpl2(); var tracer = new FrameTracing.RpcFrameTracer(Console.Out); server.OnConnectionChanged += (s, a) => { a.Connection.Close(); }; (var addr, int port) = TcpManager.Instance.GetLocalAddressAndPort(); server.StartAccepting(addr, port); var client1 = new TcpRpcClient(addr.ToString(), port); Assert.IsTrue(client1.WhenConnected.Wait(MediumNonDbgTimeout)); Assert.IsTrue(SpinWait.SpinUntil(() => client1.State == ConnectionState.Down, MediumNonDbgTimeout)); } }
static async Task Main(string[] args) { using var server = new TcpRpcServer(); server.Main = new CapnpEchoService(); server.AddBuffering(); server.StartAccepting(IPAddress.Any, 5002); using var client = new TcpRpcClient("localhost", 5002); await client.WhenConnected; using var echoer = client.GetMain <IEchoer>(); var payload = new byte[20]; new Random().NextBytes(payload); while (true) { var result = await echoer.Echo(payload); if (result.Count != payload.Length) { throw new InvalidOperationException("Echo server malfunction"); } } }
public void Server1() { var cbb = new BufferBlock <IConnection>(); var server = new TcpRpcServer(); server.Main = new TestInterfaceImpl2(); bool init = true; var tracer = new FrameTracing.RpcFrameTracer(Console.Out); (var addr, int port) = TcpManager.Instance.GetLocalAddressAndPort(); server.OnConnectionChanged += (s, a) => { var c = a.Connection; if (init) { Assert.ThrowsException <ArgumentNullException>(() => c.AttachTracer(null)); c.AttachTracer(tracer); Assert.ThrowsException <ArgumentNullException>(() => c.InjectMidlayer(null)); c.InjectMidlayer(_ => _); Assert.IsFalse(c.IsComputing); Assert.IsFalse(c.IsWaitingForData); Assert.AreEqual(ConnectionState.Initializing, c.State); Assert.IsNotNull(c.RemotePort); Assert.AreEqual(port, c.LocalPort); Assert.AreEqual(0L, c.RecvCount); Assert.AreEqual(0L, c.SendCount); } else { Assert.ThrowsException <InvalidOperationException>(() => c.AttachTracer(tracer)); Assert.ThrowsException <InvalidOperationException>(() => c.InjectMidlayer(_ => _)); Assert.AreEqual(ConnectionState.Down, c.State); } cbb.Post(c); }; Assert.ThrowsException <InvalidOperationException>(() => server.StopListening()); server.StartAccepting(addr, port); Assert.IsTrue(server.IsAlive); Assert.ThrowsException <InvalidOperationException>(() => server.StartAccepting(addr, port)); var server2 = new TcpRpcServer(); Assert.ThrowsException <SocketException>(() => server2.StartAccepting(addr, port)); var client1 = new TcpRpcClient(addr.ToString(), port); var c1 = cbb.Receive(TimeSpan.FromMilliseconds(MediumNonDbgTimeout)); Assert.IsNotNull(c1); Assert.AreEqual(1, server.ConnectionCount); Assert.AreEqual(c1, server.Connections[0]); Assert.AreEqual(ConnectionState.Active, c1.State); var proxy = client1.GetMain <ITestInterface>(); Assert.IsTrue(proxy is IResolvingCapability r && r.WhenResolved.WrappedTask.Wait(MediumNonDbgTimeout)); Assert.IsTrue(c1.RecvCount > 0); Assert.IsTrue(c1.SendCount > 0); var client2 = new TcpRpcClient(addr.ToString(), port); var c2 = cbb.Receive(TimeSpan.FromMilliseconds(MediumNonDbgTimeout)); Assert.IsNotNull(c2); Assert.AreEqual(2, server.ConnectionCount); Assert.AreEqual(c2, server.Connections[1]); init = false; client1.Dispose(); var c1d = cbb.Receive(TimeSpan.FromMilliseconds(MediumNonDbgTimeout)); Assert.IsNotNull(c1d); Assert.AreEqual(1, server.ConnectionCount); Assert.AreEqual(c2, server.Connections[0]); Assert.IsTrue(SpinWait.SpinUntil(() => c1d.State == ConnectionState.Down, MediumNonDbgTimeout)); client2.Dispose(); var c2d = cbb.Receive(TimeSpan.FromMilliseconds(MediumNonDbgTimeout)); Assert.IsNotNull(c2d); Assert.AreEqual(0, server.ConnectionCount); Assert.IsTrue(SpinWait.SpinUntil(() => c2d.State == ConnectionState.Down, MediumNonDbgTimeout)); server.StopListening(); Assert.IsFalse(server.IsAlive); Assert.ThrowsException <InvalidOperationException>(() => server.StopListening()); for (int i = 0; i < 100; i++) { server.StartAccepting(addr, port); Assert.IsTrue(server.IsAlive); server.StopListening(); Assert.IsFalse(server.IsAlive); } server.Dispose(); }