Пример #1
0
        /// <summary>
        ///  Starts listening for and accepting network connections.
        /// </summary>
        public static void beginNetworkOperations()
        {
            if (netControllerThread != null)
            {
                // already running
                Logging.info("Network server thread is already running.");
                return;
            }

            if (IxianHandler.publicPort <= 0 || IxianHandler.publicPort > 65535)
            {
                Logging.error("Cannot start network server, public port is invalid");
                return;
            }

            if (CoreConfig.preventNetworkOperations)
            {
                Logging.warn("Not starting NetworkClientManager thread due to preventNetworkOperations flag being set.");
                return;
            }

            TLC = new ThreadLiveCheck();
            netControllerThread      = new Thread(networkOpsLoop);
            netControllerThread.Name = "Network_Server_Controller_Thread";
            connectedClients         = new List <RemoteEndpoint>();
            continueRunning          = true;

            // Read the server port from the configuration
            NetOpsData nod = new NetOpsData();

            nod.listenAddress = new IPEndPoint(IPAddress.Any, IxianHandler.publicPort);
            netControllerThread.Start(nod);

            Logging.info(string.Format("Public network node address: {0} port {1}", IxianHandler.publicIP, IxianHandler.publicPort));
        }
Пример #2
0
        private static void networkOpsLoop(object data)
        {
            if (data is NetOpsData)
            {
                try
                {
                    NetOpsData netOpsData = (NetOpsData)data;
                    listener = new TcpListener(netOpsData.listenAddress);
                    listener.Start();
                }
                catch (Exception e)
                {
                    Logging.error(string.Format("Exception starting server: {0}", e.ToString()));
                    return;
                }
            }
            else
            {
                Logging.error(String.Format("NetworkServer.networkOpsLoop called with incorrect data object. Expected 'NetOpsData', got '{0}'", data.GetType().ToString()));
                return;
            }
            // housekeeping tasks
            while (continueRunning)
            {
                TLC.Report();
                try
                {
                    handleDisconnectedClients();
                }catch (Exception e)
                {
                    Logging.error("Fatal exception occured in NetworkServer.networkOpsLoop: " + e);
                }
                // Use a blocking mechanism
                try
                {
                    Socket handlerSocket = listener.AcceptSocket();
                    acceptConnection(handlerSocket);
                }
                catch (SocketException)
                {
                    // Could be an interupt request
                }
                catch (Exception)
                {
                    if (continueRunning)
                    {
                        Logging.error("Exception occured in network server while trying to accept socket connection.");
                        restartNetworkOperations();
                    }
                    return;
                }

                // Sleep to prevent cpu usage
                Thread.Sleep(100);
            }
            Logging.info("Server listener thread ended.");
        }