예제 #1
0
        public bool StartServer()
        {
            Datagram.CreateObject().PutPool();


            if (_listener != null)
            {
                return(false);                               // Already started
            }
            try
            {
                Log.Info("Initializing...");

                InacvitityTimeout = Config.GetProperty("InactivityTimeout", 8500);

                if (Endpoint == null)
                {
                    var ip   = IPAddress.Parse(Config.GetProperty("ip", "0.0.0.0"));
                    int port = Config.GetProperty("port", 19132);
                    Endpoint = new IPEndPoint(ip, port);
                }

                ForwardAllPlayers = Config.GetProperty("ForwardAllPlayers", false);
                if (ForwardAllPlayers)
                {
                    var ip   = IPAddress.Parse(Config.GetProperty("ForwardIP", "127.0.0.1"));
                    int port = Config.GetProperty("ForwardPort", 19132);
                    ForwardTarget = new IPEndPoint(ip, port);
                }

                Log.Info("Loading plugins...");
                PluginManager = new PluginManager();
                PluginManager.LoadPlugins();
                Log.Info("Plugins loaded!");

                // Bootstrap server
                PluginManager.ExecuteStartup(this);

                MotdProvider = MotdProvider ?? new MotdProvider();

                IsSecurityEnabled = Config.GetProperty("EnableSecurity", false);
                if (IsSecurityEnabled)
                {
                    // http://www.asp.net/identity/overview/extensibility/overview-of-custom-storage-providers-for-aspnet-identity
                    UserManager = UserManager ?? new UserManager <User>(new DefaultUserStore());
                    RoleManager = RoleManager ?? new RoleManager <Role>(new DefaultRoleStore());
                }

                GreylistManager = GreylistManager ?? new GreylistManager(this);
                SessionManager  = SessionManager ?? new SessionManager();
                LevelManager    = LevelManager ?? new LevelManager();
                PlayerFactory   = PlayerFactory ?? new PlayerFactory();

                PluginManager.EnablePlugins(this, LevelManager);

                // Cache - remove
                LevelManager.GetLevel(null, "Default");

                _listener = new UdpClient(Endpoint);

                if (IsRunningOnMono())
                {
                    _listener.Client.ReceiveBufferSize = 1024 * 1024 * 3;
                    _listener.Client.SendBufferSize    = 4096;
                }
                else
                {
                    //_listener.Client.ReceiveBufferSize = 1600*40000;
                    _listener.Client.ReceiveBufferSize = int.MaxValue;
                    //_listener.Client.SendBufferSize = 1600*40000;
                    _listener.Client.SendBufferSize = int.MaxValue;
                    _listener.DontFragment          = false;
                    _listener.EnableBroadcast       = false;

                    // SIO_UDP_CONNRESET (opcode setting: I, T==3)
                    // Windows:  Controls whether UDP PORT_UNREACHABLE messages are reported.
                    // - Set to TRUE to enable reporting.
                    // - Set to FALSE to disable reporting.

                    uint IOC_IN            = 0x80000000;
                    uint IOC_VENDOR        = 0x18000000;
                    uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
                    _listener.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);

                    //
                    //WARNING: We need to catch errors here to remove the code above.
                    //
                }

                _ackTimer     = new Timer(SendAckQueue, null, 0, 50);
                _cleanerTimer = new Timer(Update, null, 10, Timeout.Infinite);

                _listener.BeginReceive(ReceiveCallback, _listener);

                // Measure latency through system
                //_internalPingTimer = new Timer(delegate(object state)
                //{
                //	var playerSession = _playerSessions.Values.FirstOrDefault();
                //	if (playerSession != null)
                //	{
                //		var ping = new InternalPing();
                //		ping.Timer.Start();
                //		HandlePackage(ping, playerSession);
                //	}
                //}, null, 1000, 1000);

                ServerInfo = new ServerInfo(LevelManager, _playerSessions)
                {
                    MaxNumberOfPlayers = Config.GetProperty("MaxNumberOfPlayers", 1000)
                };
                ServerInfo.MaxNumberOfConcurrentConnects = Config.GetProperty("MaxNumberOfConcurrentConnects", ServerInfo.MaxNumberOfPlayers);

                Log.Info("Server open for business on port " + Endpoint.Port + " ...");

                return(true);
            }
            catch (Exception e)
            {
                Log.Error("Error during startup!", e);
                StopServer();
            }

            return(false);
        }