Beispiel #1
0
        public void Stop()
        {
            //lock (syncRoot)
            {
                if (allocations != null)
                {
                    allocations.Clear();
                    allocations.Removed -= Allocation_Removed;
                    allocations          = null;
                }

                if (turnServer != null)
                {
                    turnServer.Dispose();
                    turnServer = null;
                }

                Authentificater = null;
            }
        }
Beispiel #2
0
 public void Dispose()
 {
     serversManager.Dispose();
 }
Beispiel #3
0
        static void Main(string[] args)
        {
            int serverPort = 5070;
            if (args.Length >= 1)
                int.TryParse(args[0], out serverPort);
            Console.WriteLine("Server Port: {0}", serverPort);

            IPEndPoint real = null, fake = null;
            if (args.Length >= 4)
            {
                int fakePort;
                IPAddress realIp, fakeIp;
                if (IPAddress.TryParse(args[1], out realIp) && IPAddress.TryParse(args[2], out fakeIp) &&
                    int.TryParse(args[3], out fakePort))
                {
                    real = new IPEndPoint(realIp, serverPort);
                    fake = new IPEndPoint(fakeIp, fakePort);
                    Console.WriteLine("Fake and real IP parsed.");
                    Console.WriteLine("Real - Fake: {0} <=> {1}", real, fake);
                }
            }

            Console.Write(@"Load certificate...");
            var certificate = new X509Certificate2("SocketServers.pfx");
            Console.WriteLine(@"Ok");

            Console.Write(@"Initialize...");

            int port = 6000;
            IPAddress address = IPAddress.Parse(@"200.200.200.200");
            var serversManager = new ServersManager<BaseConnection2>(new ServersManagerConfig() { /*TcpOffsetOffset = 256, */TlsCertificate = certificate, });
            serversManager.FakeAddressAction =
                (ServerEndPoint real1) =>
                {
                    if (real != null && real.Equals(real1))
                        return fake;
                    return new IPEndPoint(address, port++);
                };
            serversManager.Bind(new ProtocolPort() { Protocol = ServerProtocol.Tcp, Port = serverPort, });
            serversManager.Bind(new ProtocolPort() { Protocol = ServerProtocol.Udp, Port = serverPort, });
            serversManager.Bind(new ProtocolPort() { Protocol = ServerProtocol.Tls, Port = serverPort + 1, });
            serversManager.ServerAdded += ServersManager_ServerAdded;
            serversManager.ServerRemoved += ServersManager_ServerRemoved;
            serversManager.ServerInfo += ServersManager_ServerInfo;
            serversManager.Received += ServersManager_Received;
            serversManager.Sent += ServersManager_Sent;
            serversManager.NewConnection += ServersManager_NewConnection;
            serversManager.EndConnection += ServersManager_EndConnection;

            //serversManager.Logger.Enable("test-log.pcap");

            //for (int i = 0; i < 10; i++)
            //	serversManager.BuffersPool.Get();

            Console.WriteLine(@"Ok");

            /////////////////////////////////////////////////////////////////////////

            Console.WriteLine(@"Starting...");

            try
            {
                serversManager.Start(true);
                Console.WriteLine(@"Started!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(@"Failed to start");
                Console.WriteLine(@"Error: {0}", ex.Message);
            }

            /////////////////////////////////////////////////////////////////////////

            //CreateSomeGarbage(serversManager);

            Console.WriteLine(@"Press any key to stop server...");

            while (Console.KeyAvailable == false)
            {
                Console.Write("Connections: {0} - {1} = {2}\t\r", openedConnections, closedConnections, openedConnections - closedConnections);
                Thread.Sleep(500);
            }
            Console.ReadKey(true);

            Console.WriteLine();

            /////////////////////////////////////////////////////////////////////////

            Console.WriteLine(@"Stats:");
            Console.WriteLine(@"  Buffers Created : {0}", EventArgsManager.Created);
            Console.WriteLine(@"  Buffers Queued  : {0}", EventArgsManager.Queued);

            /////////////////////////////////////////////////////////////////////////

            Console.WriteLine(@"Stoping...");

            try
            {
                serversManager.Dispose();
                Console.WriteLine(@"Stopped");
            }
            catch (Exception ex)
            {
                Console.WriteLine(@"Failed to stop");
                Console.WriteLine(@"Error: {0}", ex.Message);
            }

            serversManager.ServerAdded -= ServersManager_ServerAdded;
            serversManager.ServerInfo -= ServersManager_ServerInfo;
            serversManager.ServerRemoved -= ServersManager_ServerRemoved;
            serversManager.Received -= ServersManager_Received;
            serversManager.Sent -= ServersManager_Sent;
            serversManager.NewConnection -= ServersManager_NewConnection;
            serversManager.EndConnection -= ServersManager_EndConnection;

            /////////////////////////////////////////////////////////////////////////

            //CreateSomeGarbage(serversManager);

            for (int i = 0; i < 20; i++)
            {
                if (EventArgsManager.Created == EventArgsManager.Queued)
                    break;
                Console.Write("\rWaiting for buffers: {0} seconds", i / 2);
                Thread.Sleep(500);
            }
            Console.WriteLine();

            if (EventArgsManager.Created != EventArgsManager.Queued)
            {
                Console.WriteLine(@"Lost buffers:");
                Console.WriteLine(@"  Buffers Created : {0}", EventArgsManager.Created);
                Console.WriteLine(@"  Buffers Queued  : {0}", EventArgsManager.Queued);

                Console.WriteLine("  GC for gen #0 {0}, #1 {1}, #2 {2}", GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2));
                Console.Write(@"  Trying to garbage lost buffers...");
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                if (EventArgsManager.Created != EventArgsManager.Queued)
                {
                    Console.WriteLine("Failed {0}", EventArgsManager.Created - EventArgsManager.Queued);
                    Console.WriteLine("  GC for gen #0 {0}, #1 {1}, #2 {2}", GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2));
                }
                else
                    Console.WriteLine("Ok");
                Console.ReadKey(true);
            }
        }