public async Task MessageReceivedWithStreamAndUsingBufferStreamShouldWork() { var serverReceivedDataEvent = new AsyncAutoResetEvent(false); int currentMessageSize = -1; using var server = TcpConnectionFactory.CreateServer(15022, new ServerConnectionSettings(useBufferedStream: true)); using var client = TcpConnectionFactory.CreateClient(IPAddress.Loopback, 15022, new ClientConnectionSettings(useBufferedStream: true)); client.Start(connectionStateChangedAction: (connection, fromState, toState) => { }); server.Start(receivedActionStreamAsync: async(connection, stream, cancellationToken) => { var bytesRead = await stream.ReadAsync(new byte[stream.Length], 0, (int)stream.Length, cancellationToken); Assert.AreEqual(currentMessageSize, bytesRead); serverReceivedDataEvent.Set(); }, connectionStateChangedAction: (connection, fromState, toState) => { }); await client.WaitForStateAsync(ConnectionState.Connected); await server.WaitForStateAsync(ConnectionState.Connected); await client.SendDataAsync(new byte[currentMessageSize = 10]); (await serverReceivedDataEvent.WaitAsync(10000)).ShouldBeTrue(); await client.SendDataAsync(new byte[currentMessageSize = 120]); (await serverReceivedDataEvent.WaitAsync(10000)).ShouldBeTrue(); }
public async Task MessageLargerThan64KBShouldBeTransimittedWithoutProblems() { var serverReceivedDataEvent = new AsyncAutoResetEvent(false); const int messageSize = 1024 * 128; //128kb using var server = TcpConnectionFactory.CreateServer(15010); using var client = TcpConnectionFactory.CreateClient(IPAddress.Loopback, 15010); client.Start(connectionStateChangedAction: (connection, fromState, toState) => { }); server.Start((connection, data) => { Assert.AreEqual(messageSize, data.Length); serverReceivedDataEvent.Set(); }, connectionStateChangedAction: (connection, fromState, toState) => { }); await client.WaitForStateAsync(ConnectionState.Connected); await server.WaitForStateAsync(ConnectionState.Connected); await client.SendDataAsync(new byte[messageSize]); (await serverReceivedDataEvent.WaitAsync(10000)).ShouldBeTrue(); }
public void StartServerAfterClientShouldWork() { using var server = TcpConnectionFactory.CreateServer( 15003); using var client = TcpConnectionFactory.CreateClient( IPAddress.Loopback, 15003); client.Start(connectionStateChangedAction: (connection, fromState, toState) => { }); server.Start(connectionStateChangedAction: (connection, fromState, toState) => { }); AssertEx.IsTrue(() => server.State == ConnectionState.Connected); AssertEx.IsTrue(() => client.State == ConnectionState.Connected); server.Stop(); client.Stop(); AssertEx.IsTrue(() => server.State == ConnectionState.Disconnected); AssertEx.IsTrue(() => server.State == ConnectionState.Disconnected); }
public void ServerShouldMoveStateToLinkErrorIfClientDoesntConnect() { using var connectionLinkErrorEvent = new AutoResetEvent(false); using var connectionOkEvent = new AutoResetEvent(false); using var server = TcpConnectionFactory.CreateServer(15006, new ServerConnectionSettings(connectionTimeoutMilliseconds: 1000)); server.Start(connectionStateChangedAction: (connection, fromState, toState) => { if (toState == ConnectionState.LinkError) { connectionLinkErrorEvent.Set(); } else if (toState == ConnectionState.Connected) { connectionOkEvent.Set(); } }); connectionLinkErrorEvent.WaitOne(10000).ShouldBeTrue(); var client = TcpConnectionFactory.CreateClient(IPAddress.Loopback, 15006); client.Start(); connectionOkEvent.WaitOne(10000).ShouldBeTrue(); }
public void ClientShouldMoveStateToLinkErrorIfServerDoesntExist() { using var connectionLinkErrorEvent = new AutoResetEvent(false); using var connectionOkEvent = new AutoResetEvent(false); using var client = TcpConnectionFactory.CreateClient(IPAddress.Loopback, 15005); client.Start(connectionStateChangedAction: (connection, fromState, toState) => { if (toState == ConnectionState.LinkError) { connectionLinkErrorEvent.Set(); } else if (toState == ConnectionState.Connected) { connectionOkEvent.Set(); } }); connectionLinkErrorEvent.WaitOne(10000).ShouldBeTrue(); var server = TcpConnectionFactory.CreateServer(15005); server.Start(); connectionOkEvent.WaitOne(10000).ShouldBeTrue(); }
public void ClientShouldMoveStateToLinkErrorIfServerDoesntExist() { using var connectionLinkErrorEvent = new AutoResetEvent(false); using var connectionOkEvent = new AutoResetEvent(false); using var client = TcpConnectionFactory.CreateClient(IPAddress.Loopback, 15005); client.Start(connectionStateChangedAction: (connection, fromState, toState) => { if (toState == ConnectionState.LinkError) { connectionLinkErrorEvent.Set(); } else if (toState == ConnectionState.Connected) { connectionOkEvent.Set(); } }, receivedActionStreamAsync: (connection, stream, cancellationToken) => { System.Diagnostics.Debug.Assert(false); return(Task.CompletedTask); }); connectionLinkErrorEvent.WaitOne(10000).ShouldBeTrue(); var server = TcpConnectionFactory.CreateServer(15005); server.Start(); connectionOkEvent.WaitOne(10000).ShouldBeTrue(); }
private static void RunServer() { using var server = TcpConnectionFactory.CreateServer(15000); server.Start( receivedAction: (connection, data) => Console.WriteLine($"Message from client: {Encoding.UTF8.GetString(data)}"), connectionStateChangedAction: (connection, fromState, toState) => Console.WriteLine($"Server connection state changed from {fromState} to {toState}")); RunConnection(server); }
public async Task CancelServerPendingConnectionShouldJustWork() { using var server = TcpConnectionFactory.CreateServer(15001); server.Start(connectionStateChangedAction: (connection, fromState, toState) => { }); server.Stop(); await server.WaitForStateAsync(ConnectionState.Disconnected); }
public async Task SendMessagesUsingMemoryBuffer() { using var server = TcpConnectionFactory.CreateServer(11001); using var client = TcpConnectionFactory.CreateClient(IPAddress.Loopback, 11001); var receivedFromClientEvent = new AsyncAutoResetEvent(false); var receivedFromServerEvent = new AsyncAutoResetEvent(false); client.Start( receivedActionStreamAsync: async(connection, stream, cancellationToken) => { using var memoryOwner = MemoryPool <byte> .Shared.Rent((int)stream.Length); await stream.ReadAsync(memoryOwner.Memory, cancellationToken); if (Encoding.UTF8.GetString(memoryOwner.Memory.Span) == "SENT FROM SERVER") { receivedFromServerEvent.Set(); } }, connectionStateChangedAction: (c, fromState, toState) => { } ); server.Start( receivedActionStreamAsync: async(connection, stream, cancellationToken) => { using var memoryOwner = MemoryPool <byte> .Shared.Rent((int)stream.Length); await stream.ReadAsync(memoryOwner.Memory, cancellationToken); if (Encoding.UTF8.GetString(memoryOwner.Memory.Span) == "SENT FROM CLIENT") { receivedFromClientEvent.Set(); } }, connectionStateChangedAction: (c, fromState, toState) => { } ); //WaitHandle.WaitAll(new[] { clientConnectedEvent, serverConnectedEvent }, 4000).ShouldBeTrue(); await client.WaitForStateAsync(ConnectionState.Connected); await server.WaitForStateAsync(ConnectionState.Connected); await client.SendDataAsync(new Memory <byte>(Encoding.UTF8.GetBytes("SENT FROM CLIENT"))); await server.SendDataAsync(new Memory <byte>(Encoding.UTF8.GetBytes("SENT FROM SERVER"))); //WaitHandle.WaitAll(new[] { receivedFromClientEvent, receivedFromServerEvent }, 10000).ShouldBeTrue(); (await receivedFromClientEvent.WaitAsync(10000)).ShouldBe(true); (await receivedFromServerEvent.WaitAsync(10000)).ShouldBe(true); }
public void ClientShouldReconnectToServerAfterServerRestart() { _logger.Verbose("Begin test ClientShouldReconnectToServerAfterServerRestart"); using var server = TcpConnectionFactory.CreateServer(15004); using var client = TcpConnectionFactory.CreateClient(IPAddress.Loopback, 15004); server.Start(connectionStateChangedAction: (connection, fromState, toState) => { _logger.Verbose($"Server state change from {fromState} to {toState}"); }); client.Start(connectionStateChangedAction: (connection, fromState, toState) => { _logger.Verbose($"Client state change from {fromState} to {toState}"); }); AssertEx.IsTrue(() => server.State == ConnectionState.Connected); AssertEx.IsTrue(() => client.State == ConnectionState.Connected); _logger.Verbose($"Stop server"); server.Stop(); AssertEx.IsTrue(() => server.State == ConnectionState.Disconnected); AssertEx.IsTrue(() => client.State == ConnectionState.LinkError); _logger.Verbose($"Start server"); server.Start(); _logger.Verbose($"Stop server"); server.Stop(); _logger.Verbose($"Stop client"); client.Stop(); AssertEx.IsTrue(() => server.State == ConnectionState.Disconnected); AssertEx.IsTrue(() => client.State == ConnectionState.Disconnected); _logger.Verbose("End test ClientShouldReconnectToServerAfterServerRestart"); }