Example #1
0
        //Threads
        public void hostingLoop()
        {
            clearState();

            //Start hosting server
            stopwatch.Start();

            stampedConsoleWriteLine("Hosting server on port " + settings.port + "...");

            clients = new ServerClient[settings.maxClients];
            for (int i = 0; i < clients.Length; i++)
            {
            clients[i] = new ServerClient(this, i);
            }

            clientMessageQueue = new ConcurrentQueue<ClientMessage>();

            numClients = 0;
            numInGameClients = 0;
            numInFlightClients = 0;

            listenThread = new Thread(new ThreadStart(listenForClients));
            commandThread = new Thread(new ThreadStart(handleCommands));
            connectionThread = new Thread(new ThreadStart(handleConnections));
            outgoingMessageThread = new Thread(new ThreadStart(sendOutgoingMessages));

            threadException = null;

            loadBanList();

            tcpListener = new TcpListener(IPAddress.Any, settings.port);
            listenThread.Start();

            try
            {
            udpClient = new UdpClient(settings.port);
            udpClient.BeginReceive(asyncUDPReceive, null);
            }
            catch
            {
            udpClient = null;
            }

            Console.WriteLine("Enter /help to view server commands.");

            commandThread.Start();
            connectionThread.Start();
            outgoingMessageThread.Start();

            //Begin listening for HTTP requests

            httpListener = new HttpListener(); //Might need a replacement as HttpListener needs admin rights
            try
            {
            httpListener.Prefixes.Add("http://*:" + settings.httpPort + '/');
            httpListener.Start();
            httpListener.BeginGetContext(asyncHTTPCallback, httpListener);
            }
            catch (Exception e)
            {
            stampedConsoleWriteLine("Error starting http server: " + e);
            stampedConsoleWriteLine("Please try running the server as an administrator");
            }

            while (!quit)
            {
            //Check for exceptions that occur in threads
            lock (threadExceptionLock)
            {
                if (threadException != null)
                {
                    Exception e = threadException;
                    threadExceptionStackTrace = e.StackTrace;
                    throw e;
                }
            }

            Thread.Sleep(SLEEP_TIME);
            }

            clearState();
            stopwatch.Stop();

            stampedConsoleWriteLine("Server session ended.");
        }
Example #2
0
        //Threads
        public void HostingLoop()
        {
            //create all threads
            ClearState();
            ServerStopwatch.Start();
            StampedConsoleWriteLine("Hosting server on port " + Configuration.Port + "...");
            Clients = new ServerClient[Configuration.MaxClients];
            for (int i = 0; i < Clients.Length; i++)
                Clients[i] = new ServerClient(this, i);

            ClientMessageQueue = new ConcurrentQueue<ClientMessage>();
            NumClients = 0;
            ClientsInGame = 0;
            ClientsInFlight = 0;

            ListenThread = new Thread(new ThreadStart(ListenForClients));
            CommandThread = new Thread(new ThreadStart(HandleCommands));
            ConnectionThread = new Thread(new ThreadStart(HandleConnections));
            OutgoingMessageThread = new Thread(new ThreadStart(SendOutgoingMessages));
            CurrentThreadException = null;

            LoadBanList();
            tcpListener = new TcpListener(IPAddress.Any, Configuration.Port);
            ListenThread.Start();
            try
            {
                UdpConnection = new UdpClient(Configuration.Port);
                UdpConnection.BeginReceive(AsyncUdpReceive, null);
            }
            catch
            {
                UdpConnection = null;
            }

            Console.WriteLine("Enter /help to view server commands.");
            CommandThread.Start();
            ConnectionThread.Start();
            OutgoingMessageThread.Start();

            //Begin listening for HTTP requests
            httpListener = new HttpListener(); //Might need a replacement as HttpListener needs admin rights
            try
            {
                httpListener.Prefixes.Add("http://*:" + Configuration.HttpPort + '/');
                httpListener.Start();
                httpListener.BeginGetContext(AsyncHttpCallback, httpListener);
            }
            catch (Exception e)
            {
                StampedConsoleWriteLine("Error starting http server: " + e);
                StampedConsoleWriteLine("Please try running the server as an administrator");
            }

            while (!Quit)
            {
                //Check for exceptions that occur in threads
                lock (CurrentThreadExceptionLock)
                {
                    if (CurrentThreadException != null)
                    {
                        Exception e = CurrentThreadException;
                        CurrentThreadExceptionStackTrace = e.StackTrace;
                        throw e;
                    }
                }
                Thread.Sleep(SleepTime);
            }
            ClearState();
            ServerStopwatch.Stop();
            StampedConsoleWriteLine("Server session ended.");
        }
Example #3
0
        public void hostingLoop()
        {
            stopwatch.Start();

            Console.WriteLine("Hosting server on port " + port + "...");

            clients = new ServerClient[maxClients];
            for (int i = 0; i < clients.Length; i++)
            {
                clients[i] = new ServerClient();
                clients[i].clientIndex = i;
                clients[i].parent = this;
            }

            numClients = 0;

            listenThread = new Thread(new ThreadStart(listenForClients));

            tcpListener = new TcpListener(IPAddress.Any, port);
            listenThread.Start();

            //Try to forward the port using UPnP
            bool upnp_enabled = false;
            try
            {
                if (UPnP.NAT.Discover())
                {
                    Console.WriteLine("NAT Firewall discovered! Users won't be able to connect unless port "+port+" is forwarded.");
                    Console.WriteLine("External IP: " + UPnP.NAT.GetExternalIP().ToString());
                    UPnP.NAT.ForwardPort(port, ProtocolType.Tcp, "KLF (TCP)");
                    Console.WriteLine("Forwarded port "+port+" with UPnP");
                    upnp_enabled = true;
                }
            }
            catch (Exception)
            {
                //Console.WriteLine(e);
            }

            Console.WriteLine("Commands:");
            Console.WriteLine("/quit - quit");

            while (true)
            {
                String input = Console.ReadLine();

                if (input != null && input.Length > 0)
                {

                    if (input.ElementAt(0) == '/')
                    {
                        if (input == "/quit")
                            break;
                        else if (input == "/crash")
                        {
                            Object o = null; //You asked for it!
                            o.ToString();
                        }
                    }
                    else
                    {
                        //Send a message to all clients
                        for (int i = 0; i < clients.Length; i++)
                        {
                            clients[i].mutex.WaitOne();

                            if (clientIsReady(i))
                            {
                                sendServerMessage(clients[i].tcpClient, input);
                            }

                            clients[i].mutex.ReleaseMutex();
                        }
                    }

                }
            }

            //End listen and client threads
            listenThread.Abort();

            for (int i = 0; i < clients.Length; i++)
            {
                clients[i].mutex.WaitOne();

                if (clients[i].tcpClient != null)
                {
                    clients[i].tcpClient.Close();
                }

                if (clients[i].messageThread != null && clients[i].messageThread.IsAlive)
                    clients[i].messageThread.Abort();

                clients[i].mutex.ReleaseMutex();

            }

            if (upnp_enabled)
            {
                //Delete port forwarding rule
                try
                {
                    UPnP.NAT.DeleteForwardingRule(port, ProtocolType.Tcp);
                }
                catch (Exception)
                {
                }
            }

            tcpListener.Stop();

            clients = null;

            Console.WriteLine("Server session ended.");

            stopwatch.Stop();
        }