Exemplo n.º 1
0
        public void Should_be_able_to_send_message_from_server()
        {
            var server = new BlyncTcpServer(new TcpServerSettings
            {
                Port = Port
            });

            server.ClientConnectedEvent += (sender, args) =>
            {
                var tcpClientContext = sender as TcpClientContext;
                if (tcpClientContext != null)
                {
                    var message = Encoding.UTF8.GetBytes("MyEvent");
                    tcpClientContext.Send(message, new CancellationToken()).Wait();
                }
                server.Stop();
            };

            server.Start();

            var client   = new TcpClient("127.0.0.1", Port);
            var stream   = client.GetStream();
            var buffer   = new Byte[4096];
            var bytes    = stream.Read(buffer, 0, buffer.Length);
            var response = Encoding.UTF8.GetString(buffer, 0, bytes);

            Assert.That(response, Is.EqualTo("MyEvent"));
        }
Exemplo n.º 2
0
        public void Should_send_timeout_if_client_is_inactive_for_50_millisseconds()
        {
            var stopwatch = new Stopwatch();

            var server = new BlyncTcpServer(new TcpServerSettings
            {
                Port = Port,
                ConnectionTimeoutPeriod = 50,
                PacketStreamFactory     = new JsonPacketStreamFactory()
            });

            server.ClientDisconnectedEvent += (sender, args) => server.Stop();

            server.Start();

            var client = new TcpClient("127.0.0.1", Port);
            var stream = client.GetStream();
            var buffer = new byte[4096];

            stopwatch.Start();
            var bytes = stream.Read(buffer, 0, buffer.Length);

            stopwatch.Stop();

            var response = Encoding.UTF8.GetString(buffer, 0, bytes);

            Assert.That(response, Is.EqualTo("Client timed out"));
            Assert.That(stopwatch.ElapsedMilliseconds, Is.GreaterThanOrEqualTo(50).And.LessThan(70));
        }
Exemplo n.º 3
0
        public void Should_be_able_to_start_server()
        {
            var server = new BlyncTcpServer(new TcpServerSettings {
                Port = Port
            });

            server.Start();
            server.Stop();
        }
Exemplo n.º 4
0
        public void Should_be_able_to_connect_to_server()
        {
            var server = new BlyncTcpServer(new TcpServerSettings
            {
                ConnectionTimeoutPeriod = 5000,
                PacketStreamFactory     = new JsonPacketStreamFactory(),
                Port = Port
            });
            var client = new BlyncTcpClient(new TcpClientSettings
            {
                ConnectionTimeoutPeriod = 10000,
                Hostname            = "127.0.0.1",
                PacketStreamFactory = new JsonPacketStreamFactory(),
                Port = Port
            });

            bool serverConnected    = false;
            bool severDisconnected  = false;
            bool clientConnected    = false;
            bool clientDisconnected = false;

            server.ClientConnectedEvent    += (sender, args) => serverConnected = true;
            server.ClientDisconnectedEvent += (sender, args) =>
            {
                severDisconnected = true;
                server.Stop();
            };
            client.ClientConnectedEvent += (sender, args) =>
            {
                clientConnected = true;
                client.Stop();
            };
            client.ClientDisconnectedEvent += (sender, args) => clientDisconnected = true;

            server.Start();
            client.Start();

            var delay = Task.Delay(TimeSpan.FromSeconds(1));

            while (!delay.IsCompleted &&
                   !(serverConnected && severDisconnected && clientConnected && clientDisconnected))
            {
                // Wait
            }

            Assert.That(serverConnected, Is.True);
            Assert.That(severDisconnected, Is.True);
            Assert.That(clientConnected, Is.True);
            Assert.That(clientDisconnected, Is.True);
        }
Exemplo n.º 5
0
        public void Should_be_able_to_recieve_packet_from_server()
        {
            var packetProtocol = new JsonProtocol();

            var server = new BlyncTcpServer(new TcpServerSettings
            {
                ConnectionTimeoutPeriod = 5000,
                PacketStreamFactory     = new JsonPacketStreamFactory(),
                Port = Port
            });
            var client = new BlyncTcpClient(new TcpClientSettings
            {
                ConnectionTimeoutPeriod = 10000,
                Hostname            = "127.0.0.1",
                PacketStreamFactory = new JsonPacketStreamFactory(),
                Port = Port
            });

            var expectedMessage = packetProtocol.SetPacketContents(0, "Hello World!");

            server.ClientConnectedEvent += (sender, args) =>
            {
                var clientContext = sender as TcpClientContext;
                if (clientContext != null)
                {
                    clientContext.Send(expectedMessage, new CancellationToken()).Wait();
                }
            };

            byte[] actualMessage = null;
            client.MessageReceivedEvent += (sender, args) =>
            {
                actualMessage = args;
                client.Stop();
                server.Stop();
            };

            server.Start();
            client.Start();

            var timeoutTask = Task.Delay(TimeSpan.FromSeconds(1));

            while (!timeoutTask.IsCompleted && actualMessage == null)
            {
                // Wait
            }

            Assert.That(actualMessage, Is.EqualTo(expectedMessage));
        }
Exemplo n.º 6
0
        public void Should_be_notified_when_client_sends_a_message()
        {
            var expectedMessage = new JsonProtocol().SetPacketContents(0, "Hello world!");

            var server = new BlyncTcpServer(new TcpServerSettings
            {
                Port = Port,
                PacketStreamFactory = new JsonPacketStreamFactory()
            });

            byte[] actualMessage = null;

            server.MessageReceivedEvent += (sender, args) =>
            {
                var context = sender as TcpClientContext;
                if (context != null)
                {
                    actualMessage = args;
                }
                server.Stop();
            };

            server.Start();

            var tcpClient = new TcpClient("127.0.0.1", Port);
            var stream    = tcpClient.GetStream();

            stream.Write(expectedMessage, 0, expectedMessage.Length);

            var timeoutTask = Task.Delay(TimeSpan.FromSeconds(1));

            while (!timeoutTask.IsCompleted && actualMessage == null)
            {
                // Wait
            }

            Assert.That(actualMessage, Is.EqualTo(expectedMessage));
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            var port = Int32.Parse(args[0]);

            Console.WriteLine("Listening to port {0}", port);

            var protocol = new JsonProtocol();

            var server = new BlyncTcpServer(new TcpServerSettings
            {
                ConnectionTimeoutPeriod = 15000,
                PacketStreamFactory     = new JsonPacketStreamFactory(),
                Port = port
            });

            server.ClientConnectedEvent += (sender, eventArgs) =>
            {
                var client = sender as TcpClientContext;
                if (client != null)
                {
                    Console.WriteLine("Client connected [{0}]", client.Id);

                    var redPacket = protocol.SetPacketContents(1, protocol.Serialize(new JsonInstructionPacket {
                        Color = "red"
                    }));
                    var greenPacket = protocol.SetPacketContents(1, protocol.Serialize(new JsonInstructionPacket {
                        Color = "green"
                    }));

                    for (int i = 0; i < 10; i++)
                    {
                        if (i % 2 == 0)
                        {
                            client.Send(redPacket, new CancellationToken()).Wait();
                        }
                        else
                        {
                            client.Send(greenPacket, new CancellationToken()).Wait();
                        }
                        Thread.Sleep(2000);
                    }
                }
            };

            server.ClientDisconnectedEvent += (sender, eventArgs) =>
            {
                var client = sender as TcpClientContext;
                if (client != null)
                {
                    Console.WriteLine("Client disconnected [{0}]", client.Id);
                }
            };


            server.MessageReceivedEvent += (sender, bytes) =>
            {
                var client = sender as TcpClientContext;
                if (client != null)
                {
                    Console.WriteLine("Client disconnected [{0}]", client.Id);
                    var type    = protocol.GetPacketType(bytes);
                    var length  = protocol.GetPacketLength(bytes);
                    var content = protocol.GetPacketContents(bytes, length);
                    Console.WriteLine("    [{0}] {1}", type, content);
                }
            };

            try
            {
                server.Start();
                Thread.Sleep(400000);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
                if (exception.InnerException != null)
                {
                    foreach (var innerExceptionMessage in exception.InnerException.Message)
                    {
                        Console.WriteLine("    {0}", innerExceptionMessage);
                    }
                }
            }
        }