Esempio n. 1
0
        public async Task CanCreateWorkingEchoServer_PipelineSocketServer_PipelineSocketClient()
        {
            var endpoint = new IPEndPoint(IPAddress.Loopback, 5010);
            const string MessageToSend = "Hello world!";
            string reply = null;

            using (var server = new SocketListener())
            {
                server.OnConnection(Echo);
                server.Start(endpoint);


                using (var client = await SocketConnection.ConnectAsync(endpoint))
                {
                    var output = client.Output.Alloc();
                    output.Append(MessageToSend, TextEncoding.Utf8);
                    await output.FlushAsync();
                    client.Output.Complete();

                    while (true)
                    {
                        var result = await client.Input.ReadAsync();
                        var input = result.Buffer;

                        // wait for the end of the data before processing anything
                        if (result.IsCompleted)
                        {
                            reply = input.GetUtf8String();
                            client.Input.Advance(input.End);
                            break;
                        }
                        else
                        {
                            client.Input.Advance(input.Start, input.End);
                        }
                    }
                }
            }
            Assert.Equal(MessageToSend, reply);
        }
Esempio n. 2
0
        public void CanCreateWorkingEchoServer_PipelineSocketServer_NonPipelineClient()
        {
            var endpoint = new IPEndPoint(IPAddress.Loopback, 5010);
            const string MessageToSend = "Hello world!";
            string reply = null;

            using (var server = new SocketListener())
            {
                server.OnConnection(Echo);
                server.Start(endpoint);

                reply = SendBasicSocketMessage(endpoint, MessageToSend);
            }
            Assert.Equal(MessageToSend, reply);
        }
Esempio n. 3
0
        public async Task RunStressPingPongTest_Socket()
        {
            var endpoint = new IPEndPoint(IPAddress.Loopback, 5020);

            using (var server = new SocketListener())
            {
                server.OnConnection(PongServer);
                server.Start(endpoint);

                const int SendCount = 500, ClientCount = 5;
                for (int loop = 0; loop < ClientCount; loop++)
                {
                    using (var client = await SocketConnection.ConnectAsync(endpoint))
                    {
                        var tuple = await PingClient(client, SendCount);
                        Assert.Equal(SendCount, tuple.Item1);
                        Assert.Equal(SendCount, tuple.Item2);
                        Console.WriteLine($"Ping: {tuple.Item1}; Pong: {tuple.Item2}; Time: {tuple.Item3}ms");
                    }
                }
            }
        }