Example #1
0
        private async void Start()
        {
            //Let's start the listener on our listen port. Allow all IPs from the server list.
            _            = _logHandler.LogMessage($"Starting SRCDSLogService on {_port}", channel: false);
            _logReceiver = new LogReceiver(_port, CreateIpEndPoints());

            await Task.Run(() =>
            {
                if (_dataService.RSettings.ProgramSettings.Debug)
                {
                    _logReceiver.ListenRaw(msg => { Console.WriteLine($"RAW LOG FROM {_logReceiver.lastServer.Address}: " + msg); });
                }

                _logReceiver.Listen <GenericCommand>(genericCommand => { HandleIngameCommand(_logReceiver.lastServer, genericCommand); });
            });

            //We are going to use this loop to make sure log services are still running. Should they fail, we will tear
            //down and rebuild.
            while (true)
            {
                bool portBound = IPGlobalProperties.GetIPGlobalProperties()
                                 .GetActiveUdpListeners().Any(p => p.Port == _port);

                if (!portBound)
                {
                    if (_restartCount < RESTART_LIMIT)
                    {
                        try
                        {
                            _logReceiver.Dispose();
                        }
                        catch (Exception e)
                        {
                            await _logHandler.LogMessage($"Attempted dispose, but had an issue.\n{e}", alert : true, color : LOG_COLOR);
                        }

                        _restartCount++;

                        //Restart the service
                        Start();
                        await _logHandler.LogMessage($"Log service has been restarted {_restartCount} times!", color : LOG_COLOR);

                        //Reset the retry count in 15 minutes after the first issue
                        //I expect the log service to die sometimes for whatever reason.
                        if (_restartCount == 0)
                        {
                            _ = Task.Run(async() =>
                            {
                                await Task.Delay(TimeSpan.FromMinutes(15));
                                _restartCount = 0;
                            });
                        }
                    }
                    else
                    {
                        await _logHandler.LogMessage($"The SrcdsLogService has restarted over {RESTART_LIMIT} in the last 15 minutes. " +
                                                     $"I will not restart the service again.", alert : true, color : LOG_COLOR);
                    }
                    return;
                }

                //Hol up for a bit before rechecking
                await Task.Delay(5000);
            }
        }