public async Task ConnectionListenerShouldAcceptNewConnection()
        {
            using var multiPeerServer = TcpConnectionFactory.CreateMultiPeerServer(14000);

            using var client = TcpConnectionFactory.CreateClient(IPAddress.Loopback, 14000);

            var receivedBackFromServerEvent = new AsyncAutoResetEvent(false);

            IConnection newConnection = null;

            multiPeerServer.Start((listener, c) =>
            {
                newConnection = c;
                newConnection.Start(
                    receivedActionStreamAsync: async(connection, stream, cancellationToken) =>
                {
                    using var memoryOwner = MemoryPool <byte> .Shared.Rent(4);
                    var buffer            = memoryOwner.Memory.Slice(0, 4);
                    await stream.ReadAsync(buffer, cancellationToken);
                    var pingString = Encoding.UTF8.GetString(buffer.Span);
                    await connection.SendDataAsync(
                        new Memory <byte>(Encoding.UTF8.GetBytes($"SERVER RECEIVED: {pingString}")));
                },
                    connectionStateChangedAction: (c, fromState, toState) =>
                {
                }
                    );
            });

            client.Start(
                receivedActionStreamAsync: async(connection, stream, cancellationToken) =>
            {
                using var memoryOwner = MemoryPool <byte> .Shared.Rent(21);
                var buffer            = memoryOwner.Memory.Slice(0, 21);
                await stream.ReadAsync(buffer, cancellationToken);
                if (Encoding.UTF8.GetString(buffer.Span) == "SERVER RECEIVED: PING")
                {
                    receivedBackFromServerEvent.Set();
                }
            },
                connectionStateChangedAction: (c, fromState, toState) => { }
                );

            await client.WaitForStateAsync(ConnectionState.Connected);

            await client.SendDataAsync(new Memory <byte>(Encoding.UTF8.GetBytes("PING")));

            (await receivedBackFromServerEvent.WaitAsync(100000)).ShouldBeTrue();

            multiPeerServer.Stop();
        }
        public void TcpConnectionShouldBeFullDuplex()
        {
            using var server = TcpConnectionFactory.CreateMultiPeerServer(15025);
            using var client = TcpConnectionFactory.CreateClient(IPAddress.Loopback, 15025);

            server.Start((listener, connection) =>
            {
                connection.Start(async(connection, stream, cancellationToken) =>
                {
                    var buffer = new byte[10];
                    stream.Read(buffer, 0, 10);
                    await connection.SendAsync((outStream, outCancellationToken) =>
                    {
                        outStream.Write(buffer, 0, 10);
                        return(Task.CompletedTask);
                    });
                });
            });

            int receivedMessageBackCount = 0;

            client.Start((connection, stream, cancellationToken) =>
            {
                stream.Read(new byte[10], 0, 10);
                receivedMessageBackCount++;
                return(Task.CompletedTask);
            });

            client.WaitForState(ConnectionState.Connected);

            for (int i = 0; i < 1000; i++)
            {
                client.SendAsync((stream, cancellationToken) =>
                {
                    stream.Write(new byte[10], 0, 10);
                    return(Task.CompletedTask);
                });
            }

            AssertEx.IsTrue(() => receivedMessageBackCount == 1000);
        }
Esempio n. 3
0
        private static void RunServer()
        {
            Console.WriteLine("Starting server...");

            using var server = TcpConnectionFactory.CreateMultiPeerServer(15000);

            server.Start(
                connectionCreated:
                (listener, newClientConnection) =>
            {
                Console.WriteLine($"Client connection accepted");
                _connectedClients.TryAdd(newClientConnection, newClientConnection);

                newClientConnection.Start(
                    receivedAction: (connection, data) =>
                {
                    foreach (var clientConnection in _connectedClients.Keys.Except(new[] { connection }))
                    {
                        clientConnection.SendDataAsync(data).Wait();
                    }
                },
                    connectionStateChangedAction: (connection, fromState, toState) =>
                {
                    if (toState == ConnectionState.Connected)
                    {
                        connection.SendDataAsync(Encoding.UTF8.GetBytes("Welcome from server!")).Wait();
                    }
                    else if (toState == ConnectionState.LinkError)
                    {
                        _connectedClients.TryRemove(connection, out var _);
                    }
                });
            });

            Console.ReadLine();

            Console.WriteLine("Closing server...");
        }