Пример #1
0
        protected void Run(int port = 0)
        {
            var endpoint = GetServerEndpoint();

            IListener listener = Listener;

            if (ParseUtils.ParseSize(options.Bandwidth) is int bandwidth)
            {
                listener = new ThrottlingListener(listener, bandwidth);
            }

            IConnector connector = new SimpleConnector(endpoint, listener);

            if (options.Chaos)
            {
                var chaosConfiguration = new ChaosConfiguration
                {
                    Reject =
                    {
                        Percentage = 0.5
                    },
                    Abort =
                    {
                        Percentage      =                  1,
                        UpstreamBytes   = new Range <long>(0, 1024 * 1024 * 10),
                        DownstreamBytes = new Range <long>(0, 1024 * 1024 * 10)
                    }
                };
                var chaosConnector = new ChaosConnector(chaosConfiguration, connector);

                //chaosConnector.Rejected += (s, e) => Console.WriteLine("REJECTED");
                //chaosConnector.Aborted += (s, e) => Console.WriteLine($"ABORTED reason {e.Reason}, upstream {e.UpstreamTransferred}, downstream {e.DownstreamTransferred}");

                connector = chaosConnector;
            }

            proxy = new ProxyServer(new IPEndPoint(IPAddress.Loopback, port), connector);
            proxy.ExceptionOccured += (s, e) => Console.WriteLine($"EXCEPTION: {e.Exception.Message} ({e.Exception.GetType().FullName})");
            proxy.Start();

            Running(proxy.EndPoint);
        }
Пример #2
0
        private static void SimulateNetworkFailureEchoServer()
        {
            using (var echoServer = new EchoServer(new IPEndPoint(IPAddress.Loopback, 0)))
            {
                echoServer.Start();

                var configuration = new ChaosConfiguration
                {
                    Reject =
                    {
                        Percentage = 0.5
                    }
                };
                var connector = new ChaosConnector(
                    configuration,
                    new SimpleConnector(echoServer.EndPoint)
                    );

                using (var proxyServer = new ProxyServer(new IPEndPoint(IPAddress.Loopback, 0), connector))
                {
                    proxyServer.Start();

                    var block = Encoding.UTF8.GetBytes("Hello world!");

                    int errors = 0;

                    for (int i = 0; i < 100; i++)
                    {
                        using (var echoClient = new EchoPingClient(proxyServer.EndPoint, block))
                        {
                            echoClient.ExceptionOccured += (s, e) => Interlocked.Increment(ref errors);
                            echoClient.Start();
                            echoClient.Ping();
                        }
                    }

                    Console.WriteLine(errors);
                }
            }
        }