示例#1
0
        private void CheckActivity()
        {
            Thread.Add(() =>
            {
                int hours = 0;

                do
                {
                    if (Core.Settings.Features.NoPingKickTime >= 10)
                    {
                        if ((DateTime.Now - LastValidPing).TotalSeconds >= Core.Settings.Features.NoPingKickTime)
                        {
                            InternalKick("SERVER_NOPING");
                            return;
                        }
                    }

                    if (Core.Settings.Features.AFKKickTime >= 10)
                    {
                        if ((DateTime.Now - LastValidMovement).TotalSeconds >= Core.Settings.Features.AFKKickTime && BusyType == (int)BusyTypes.Inactive)
                        {
                            InternalKick("SERVER_AFK");
                            return;
                        }
                    }

                    if (Core.Settings.Features.AutoRestartTime >= 10)
                    {
                        TimeSpan timeLeft = Core.Listener.StartTime.AddSeconds(Core.Settings.Features.AutoRestartTime) - DateTime.Now;

                        if (timeLeft.TotalSeconds <= 10)
                        {
                            Network.SentToPlayer(new Package.Package(Core, PackageTypes.ChatMessage, -1, Core.Settings.Tokens.ToString("SERVER_RESTARTWARNING", timeLeft.ToString()), Network));
                        }
                        else if (timeLeft.TotalSeconds <= 0)
                        {
                            InternalKick("SERVER_RESTART");
                            return;
                        }
                    }

                    if ((DateTime.Now - JoinTime).TotalHours >= hours + 1)
                    {
                        hours++;
                        Network.SentToPlayer(new Package.Package(Core, PackageTypes.ChatMessage, -1, Core.Settings.Tokens.ToString("SERVER_LOGINTIME", hours.ToString()), Network));
                    }

                    Thread.Sleep(1000);
                } while (Network.IsActive);
            });
        }
        public void Start()
        {
            Thread.Add(() =>
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();

                do
                {
                    try
                    {
                        if (sw.Elapsed.TotalHours >= 1 || !IsActive)
                        {
                            sw.Restart();
                            IsActive = true;

                            if (CanUpdate)
                            {
                                Season     = GenerateSeason(Core.Settings.World.Season);
                                Weather    = GenerateWeather(Season, Core.Settings.World.Weather);
                                TimeOffset = Core.Settings.World.TimeOffset;
                                DoDayCycle = Core.Settings.World.DoDayCycle;
                            }

                            Core.Logger.Log(Core.World.ToString());
                            Core.TcpClientCollection.UpdateWorld();
                        }

                        if (sw.Elapsed.TotalHours < 1)
                        {
                            Thread.Sleep(1000);
                        }
                    }
                    catch (Exception) { }
                } while (IsActive);
            });
        }
        public Networking(Core core, TcpClient tcpClient)
        {
            Core      = core;
            TcpClient = tcpClient;

            Reader = new StreamReader(tcpClient.GetStream());
            Writer = new StreamWriter(tcpClient.GetStream())
            {
                AutoFlush = true
            };

            IsActive = true;

            Thread.Add(() =>
            {
                int errorCount = 0;

                do
                {
                    try
                    {
                        if (tcpClient.Available > -1)
                        {
                            string packageString = Reader.ReadLine();

                            if (!string.IsNullOrWhiteSpace(packageString))
                            {
                                ThreadPool.QueueWorkItem(() =>
                                {
                                    Core.Logger.Debug($"Receive: {packageString}", this);
                                    Package.Package package = new Package.Package(Core, packageString, this);

                                    if (package.IsValid)
                                    {
                                        package.Handle();
                                    }
                                });

                                errorCount = 0;
                            }
                            else
                            {
                                errorCount++;

                                if (errorCount > 10)
                                {
                                    Dispose();
                                }
                            }
                        }
                        else
                        {
                            Dispose();
                        }
                    }
                    catch (ThreadAbortException) { return; }
                    catch (Exception)
                    {
                        if (IsActive)
                        {
                            Dispose();
                        }
                    }
                } while (IsActive);
            });
        }
        private async void StartListening()
        {
            try
            {
                string publicAddress = await IPAddressHelper.GetPublicIPAsync();

                string privateAddress = await IPAddressHelper.GetPrivateIPAsync();

                if (string.IsNullOrEmpty(publicAddress) || string.IsNullOrEmpty(privateAddress))
                {
                    Core.Logger.Log("Network is not available.", "Warning");
                    Dispose();
                }
                else
                {
                    if (Core.Settings.Server.GameModes.OfflineMode)
                    {
                        Core.Logger.Log("Players with offline profile can join the server.");
                    }

                    Thread.Add(() =>
                    {
                        IsActive = true;

                        do
                        {
                            try
                            {
                                TcpClient client = Listener.AcceptTcpClient();

                                ThreadPool.QueueWorkItem(() =>
                                {
                                    if (client != null)
                                    {
                                        Core.TcpClientCollection.Add(client);
                                    }
                                });
                            }
                            catch (ThreadAbortException) { return; }
                            catch (Exception) { }
                        } while (IsActive);
                    });

                    if (CheckPortOpen(publicAddress))
                    {
                        Core.Logger.Log($"Server started. Players can join using the following address: {publicAddress}:{Core.Settings.Server.Port.ToString()} (Global), {privateAddress}:{Core.Settings.Server.Port.ToString()} (Local) and with the following GameMode: {Core.Settings.Server.GameModes.ToString()}.");

                        if (Core.Settings.Server.CheckPort)
                        {
                            StartPortCheck(publicAddress);
                        }
                    }
                    else
                    {
                        Core.Logger.Log($"The specific port {Core.Settings.Server.Port.ToString()} is not opened. External/Global IP will not accept new players.");
                        Core.Logger.Log($"Server started. Players can join using the following address: {privateAddress}:{Core.Settings.Server.Port.ToString()} (Local) and with the following GameMode: {Core.Settings.Server.GameModes.ToString()}.");
                    }
                }
            }
            catch (Exception)
            {
                Dispose();
            }
        }