Beispiel #1
0
        override public async Task <bool> ProcessUpdatesAsync(CancellationToken cts)
        {
            this.cts = cts;
            //#if DEBUG == false
            try
            //#endif
            {
                // first start
                if (firstRun)
                {
                    await ExecuteEvent(new GameEvent(GameEvent.EventType.Start, "Server started", null, null, this));

                    firstRun = false;
                }

                if ((DateTime.Now - LastPoll).TotalMinutes < 2 && ConnectionErrors >= 1)
                {
                    return(true);
                }

                try
                {
                    // trying to reduce the polling rate as every 450ms is unnecessary
                    if ((DateTime.Now - LastPoll).TotalSeconds >= 10)
                    {
                        int polledPlayerCount = await PollPlayersAsync();

                        if (ConnectionErrors > 0)
                        {
                            Logger.WriteVerbose($"Connection has been reestablished with {IP}:{Port}");
                            Throttled = false;
                        }
                        ConnectionErrors = 0;
                        LastPoll         = DateTime.Now;
                    }
                }

                catch (NetworkException e)
                {
                    ConnectionErrors++;
                    if (ConnectionErrors == 1)
                    {
                        Logger.WriteError($"{e.Message} {IP}:{Port}, reducing polling rate");
                        Logger.WriteDebug($"Internal Exception: {e.Data["internal_exception"]}");
                        Throttled = true;
                    }
                    return(true);
                }

                LastMessage = DateTime.Now - start;
                lastCount   = DateTime.Now;

                if ((DateTime.Now - tickTime).TotalMilliseconds >= 1000)
                {
                    foreach (var Plugin in SharedLibraryCore.Plugins.PluginImporter.ActivePlugins)
                    {
                        if (cts.IsCancellationRequested)
                        {
                            break;
                        }

                        await Plugin.OnTickAsync(this);
                    }
                    tickTime = DateTime.Now;
                }

                if ((lastCount - playerCountStart).TotalMinutes >= SharedLibraryCore.Helpers.PlayerHistory.UpdateInterval)
                {
                    while (PlayerHistory.Count > ((60 / SharedLibraryCore.Helpers.PlayerHistory.UpdateInterval) * 12)) // 12 times a hour for 12 hours
                    {
                        PlayerHistory.Dequeue();
                    }
                    PlayerHistory.Enqueue(new SharedLibraryCore.Helpers.PlayerHistory(ClientNum));
                    playerCountStart = DateTime.Now;
                }

                if (LastMessage.TotalSeconds > Manager.GetApplicationSettings().Configuration().AutoMessagePeriod &&
                    BroadcastMessages.Count > 0 &&
                    ClientNum > 0)
                {
                    await Broadcast(Utilities.ProcessMessageToken(Manager.GetMessageTokens(), BroadcastMessages[NextMessage]));

                    NextMessage = NextMessage == (BroadcastMessages.Count - 1) ? 0 : NextMessage + 1;
                    start       = DateTime.Now;
                }

                if (LogFile == null)
                {
                    return(true);
                }

                if (l_size != LogFile.Length())
                {
                    lines = l_size != -1 ? await LogFile.Tail(12) : lines;

                    if (lines != oldLines)
                    {
                        l_size = LogFile.Length();
                        int end = (lines.Length == oldLines.Length) ? lines.Length - 1 : Math.Abs((lines.Length - oldLines.Length)) - 1;

                        for (count = 0; count < lines.Length; count++)
                        {
                            if (lines.Length < 1 && oldLines.Length < 1)
                            {
                                continue;
                            }

                            if (lines[count] == oldLines[oldLines.Length - 1])
                            {
                                continue;
                            }

                            if (lines[count].Length < 10) // it's not a needed line
                            {
                                continue;
                            }

                            else
                            {
                                GameEvent event_ = EventParser.GetEvent(this, lines[count]);
                                if (event_ != null)
                                {
                                    if (event_.Origin == null)
                                    {
                                        continue;
                                    }

                                    await ExecuteEvent(event_);
                                }
                            }
                        }
                    }
                }
                oldLines = lines;
                l_size   = LogFile.Length();
                if (Manager.ShutdownRequested())
                {
                    foreach (var plugin in SharedLibraryCore.Plugins.PluginImporter.ActivePlugins)
                    {
                        await plugin.OnUnloadAsync();
                    }

                    for (int i = 0; i < Players.Count; i++)
                    {
                        await RemovePlayer(i);
                    }
                }
                return(true);
            }
            //#if !DEBUG
            catch (NetworkException)
            {
                Logger.WriteError($"Could not communicate with {IP}:{Port}");
                return(false);
            }

            catch (InvalidOperationException)
            {
                Logger.WriteWarning("Event could not parsed properly");
                Logger.WriteDebug($"Log Line: {lines[count]}");
                return(false);
            }

            catch (Exception E)
            {
                Logger.WriteError($"Encountered error on {IP}:{Port}");
                Logger.WriteDebug("Error Message: " + E.Message);
                Logger.WriteDebug("Error Trace: " + E.StackTrace);
                return(false);
            }
            //#endif
        }