Esempio n. 1
0
        public Task <bool> Initialize()
        {
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            Task.Run(async() =>
            {
                await BackgroundTaskWrapper.RunBackgroundTask(this.webSocketTokenSource, async(tokenSource) =>
                {
                    if (await this.ListenForConnection())
                    {
                        if (ChannelSession.Settings.DiagnosticLogging)
                        {
                            this.Send(new WebSocketPacket()
                            {
                                type = "debug"
                            });
                        }

                        await this.ReceiveInternal();
                        await this.ShutdownWebsocket();
                    }
                    else
                    {
                        this.OnDisconnected();
                        this.webSocketTokenSource.Cancel();
                    }
                });
            }, this.webSocketTokenSource.Token);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            return(Task.FromResult(true));
        }
Esempio n. 2
0
        private async Task TimerCommandsBackground()
        {
            this.timerCommandIndexes = new Dictionary <string, int>();

            int totalTime = 0;

            int nonGroupTotalTime  = 0;
            int startTotalMessages = ChannelSession.Chat.Messages.Count;

            timerCommandIndexes[string.Empty] = 0;

            await BackgroundTaskWrapper.RunBackgroundTask(this.backgroundThreadCancellationTokenSource, async (tokenSource) =>
            {
                tokenSource.Token.ThrowIfCancellationRequested();

                await Task.Delay(1000 * 60, tokenSource.Token);

                tokenSource.Token.ThrowIfCancellationRequested();

                if (!ChannelSession.Settings.DisableAllTimers)
                {
                    totalTime++;

                    foreach (var kvp in ChannelSession.Settings.CommandGroups)
                    {
                        if (kvp.Value.TimerInterval > 0 && totalTime % kvp.Value.TimerInterval == 0)
                        {
                            if (!timerCommandIndexes.ContainsKey(kvp.Key))
                            {
                                timerCommandIndexes[kvp.Key] = 0;
                            }

                            IEnumerable <TimerCommand> groupTimers = ChannelSession.Settings.TimerCommands.Where(c => c.IsEnabled && !string.IsNullOrEmpty(c.GroupName) && c.GroupName.Equals(kvp.Key));
                            await this.RunTimerFromGroup(kvp.Key, groupTimers);
                        }
                    }

                    nonGroupTotalTime++;

                    if (nonGroupTotalTime >= ChannelSession.Settings.TimerCommandsInterval)
                    {
                        if ((ChannelSession.Chat.Messages.Count - startTotalMessages) >= ChannelSession.Settings.TimerCommandsMinimumMessages)
                        {
                            IEnumerable <TimerCommand> nonGroupTimers = ChannelSession.Settings.TimerCommands.Where(c => c.IsEnabled && string.IsNullOrEmpty(c.GroupName));
                            await this.RunTimerFromGroup(string.Empty, nonGroupTimers);

                            startTotalMessages = ChannelSession.Chat.Messages.Count;
                            nonGroupTotalTime  = 0;
                        }
                    }
                }
            });
        }