public async Task ProcessServerPackagesSequence()
        {
            var port = this.GetPortNumber(PacketServerPort);

            var serializer = new BinarySerializer(BinarySerializationMethod.UnsafeSerialization);
            var queue      = new WaitableQueue <Packet>();

            using (var packetServer = new PacketServer(IPAddress.Any, port))
            {
                packetServer.OnNewPacketReceived += (sender, value) => queue.Enqueue(value);
                packetServer.Start();

                using (var client = new TcpClient("localhost", port))
                    using (var stream = client.GetStream())
                    {
                        var packetSequence = PacketFixture.GetPacketSequence(PacketSequenceLength);

                        foreach (var packet in packetSequence)
                        {
                            await serializer.Serialize(packet, stream).WithTimeOut(Timeout);
                        }

                        foreach (var packet in packetSequence)
                        {
                            var answer = await queue.Dequeue(Timeout);

                            Assert.AreEqual <PacketEx>(packet, answer);
                        }
                    }
            }
        }
        public async Task ProcessServerPackages()
        {
            var port = this.GetPortNumber(PacketServerPort);

            var serializer = new BinarySerializer(BinarySerializationMethod.UnsafeSerialization);

            using (var packetServer = new PacketServer(IPAddress.Any, port))
            {
                packetServer.Start();

                using (var client = new TcpClient("localhost", port))
                    using (var stream = client.GetStream())
                    {
                        var packetSequence = PacketFixture.GetPacketSequence(PacketSequenceLength);

                        foreach (var packet in packetSequence)
                        {
                            await packetServer.SendPacket(packet).WithTimeOut(Timeout);

                            var answer = await serializer.Deserialize <Packet>(stream).WithTimeOut(Timeout);

                            Assert.AreEqual <PacketEx>(packet, answer);
                        }
                    }
            }
        }
        public async Task ProcessDuplexExchange()
        {
            var port = this.GetPortNumber(PacketServerPort);

            var serializer = new BinarySerializer(BinarySerializationMethod.UnsafeSerialization);
            var queue      = new WaitableQueue <Packet>();

            using (var packetServer = new PacketServer(IPAddress.Any, port))
            {
                packetServer.OnNewPacketReceived += (sender, value) => queue.Enqueue(value);
                packetServer.Start();

                using (var client = new TcpClient("localhost", port))
                    using (var stream = client.GetStream())
                    {
                        var serverSequence = PacketFixture.GetPacketSequence(PacketSequenceLength);
                        var clientSequence = PacketFixture.GetPacketSequence(PacketSequenceLength);

                        foreach (var(serverPacket, clientPacket) in serverSequence.JoinByIndex(clientSequence))
                        {
                            await Task.WhenAll(packetServer.SendPacket(serverPacket),
                                               serializer.Serialize(clientPacket, stream));

                            var results = await Task.WhenAll(queue.Dequeue(Timeout), serializer.Deserialize <Packet>(stream).WithTimeOut(Timeout));

                            Assert.AreEqual <PacketEx>(clientPacket, results[0]);
                            Assert.AreEqual <PacketEx>(serverPacket, results[1]);
                        }
                    }
            }
        }
        public async Task ProcessDuplexExchangeConcurrent()
        {
            var port = this.GetPortNumber(PacketServerPort);

            var serializer = new BinarySerializer(BinarySerializationMethod.UnsafeSerialization);
            var queue      = new WaitableQueue <Packet>();

            using (var packetServer = new PacketServer(IPAddress.Any, port))
            {
                packetServer.OnNewPacketReceived += (sender, value) => queue.Enqueue(value);
                packetServer.Start();

                using (var client = new TcpClient("localhost", port))
                    using (var stream = client.GetStream())
                    {
                        var  serverSequence = PacketFixture.GetPacketSequence(PacketSequenceLength);
                        Task serverTask     = null;
                        foreach (var packet in serverSequence)
                        {
                            var task = packetServer.SendPacket(packet);
                            serverTask = serverTask?.ContinueWith(t => task).Unwrap() ?? task;
                        }

                        var clientSequence = PacketFixture.GetPacketSequence(PacketSequenceLength);
                        var clientTask     = Task.CompletedTask;
                        foreach (var packet in clientSequence)
                        {
                            // ReSharper disable once AccessToDisposedClosure
                            clientTask = clientTask.ContinueWith(t => serializer.Serialize(packet, stream)).Unwrap();
                        }

                        await Task.WhenAll(serverTask, clientTask).WithTimeOut(Timeout);

                        foreach (var(serverPacket, clientPacket) in serverSequence.JoinByIndex(clientSequence))
                        {
                            var results = await Task.WhenAll(queue.Dequeue(Timeout), serializer.Deserialize <Packet>(stream).WithTimeOut(Timeout));

                            Assert.AreEqual <PacketEx>(clientPacket, results[0]);
                            Assert.AreEqual <PacketEx>(serverPacket, results[1]);
                        }
                    }
            }
        }
Beispiel #5
0
        private static void Main()
        {
            try
            {
                LoadConfiguration();
            }
            catch (FormatException e)
            {
                Logger.Error("Unable to parse IPAddress: {0}", e.Message);
                return;
            }
            catch (Exception e)
            {
                Logger.Error("Parameters loading failed: {0}", e.Message);
                return;
            }

            try
            {
                var exitEvent = new ManualResetEvent(false);

                var packetServer = new PacketServer(PacketServerIpAddress, PacketServerPort);
                var proxyServer  = new ProxyServer(ExternalServerIpAddress, ExternalServerPort)
                {
                    PacketReceiver = packetServer
                };

                packetServer.Start();
                proxyServer.Start();

                Logger.Info($"Started packet server at {PacketServerIpAddress}:{PacketServerPort}");
                Logger.Info($"Started proxy server at {ExternalServerIpAddress}:{ExternalServerPort}");
                Logger.Info("Waiting for agent...");

                exitEvent.WaitOne();
            }
            catch (Exception e)
            {
                Logger.Fatal(e);
            }
        }