Esempio n. 1
0
        private bool CanCommandBeRunBasedOnActions(CommandInstanceModel commandInstance)
        {
            HashSet <ActionTypeEnum> actionTypes = commandInstance.GetActionTypes();

            foreach (ActionTypeEnum actionType in actionTypes)
            {
                if (this.perActionTypeInUse.Contains(actionType))
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 2
0
        private async Task BackgroundCommandTypeRunner(CommandTypeEnum type)
        {
            CommandInstanceModel instance = null;

            do
            {
                instance = await this.commandQueueLock.WaitAndRelease(() =>
                {
                    CommandInstanceModel commandInstance = null;
                    if (ChannelSession.Settings.CommandServiceLockType == CommandServiceLockTypeEnum.PerCommandType)
                    {
                        if (perCommandTypeInstances.ContainsKey(type) && perCommandTypeInstances[type].Count > 0)
                        {
                            commandInstance = perCommandTypeInstances[type].RemoveFirst();
                        }
                        else
                        {
                            perCommandTypeTasks[type] = null;
                        }
                    }
                    else if (ChannelSession.Settings.CommandServiceLockType == CommandServiceLockTypeEnum.PerActionType)
                    {
                        if (instance != null)
                        {
                            foreach (ActionTypeEnum actionType in instance.GetActionTypes())
                            {
                                this.perActionTypeInUse.Remove(actionType);
                            }
                        }

                        commandInstance = this.perActionTypeInstances.FirstOrDefault(c => this.CanCommandBeRunBasedOnActions(c));
                        if (commandInstance != null)
                        {
                            this.perActionTypeInstances.Remove(commandInstance);
                            foreach (ActionTypeEnum actionType in commandInstance.GetActionTypes())
                            {
                                this.perActionTypeInUse.Add(actionType);
                            }
                        }
                    }
                    else if (ChannelSession.Settings.CommandServiceLockType == CommandServiceLockTypeEnum.VisualAudioActions ||
                             ChannelSession.Settings.CommandServiceLockType == CommandServiceLockTypeEnum.Singular)
                    {
                        if (singularInstances.Count > 0)
                        {
                            commandInstance = singularInstances.RemoveFirst();
                        }
                        else
                        {
                            singularTask = null;
                        }
                    }
                    return(Task.FromResult(commandInstance));
                });

                if (instance != null && instance.State == CommandInstanceStateEnum.Pending)
                {
                    await this.RunDirectly(instance);
                }
            } while (instance != null);
        }
Esempio n. 3
0
        public async Task Queue(CommandInstanceModel commandInstance)
        {
            if (commandInstance.Parameters.User != null)
            {
                commandInstance.Parameters.User.UpdateLastActivity();
            }

            CommandModelBase command = commandInstance.Command;

            if (command != null)
            {
                if (!command.IsEnabled || !command.HasWork)
                {
                    return;
                }

                await this.ValidateCommand(commandInstance);
            }

            lock (this.commandInstances)
            {
                this.commandInstances.Insert(0, commandInstance);
            }

            if (commandInstance.State == CommandInstanceStateEnum.Pending)
            {
                CommandTypeEnum type = commandInstance.QueueCommandType;
                if (commandInstance.DontQueue)
                {
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                    AsyncRunner.RunAsync(() => this.RunDirectly(commandInstance));
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                }
                else
                {
                    await this.commandQueueLock.WaitAndRelease(() =>
                    {
                        if (ChannelSession.Settings.CommandServiceLockType == CommandServiceLockTypeEnum.PerCommandType)
                        {
                            perCommandTypeInstances[type].Add(commandInstance);
                            if (perCommandTypeTasks[type] == null)
                            {
                                perCommandTypeTasks[type] = AsyncRunner.RunAsync(() => this.BackgroundCommandTypeRunner(type));
                            }
                        }
                        else if (ChannelSession.Settings.CommandServiceLockType == CommandServiceLockTypeEnum.PerActionType)
                        {
                            this.perActionTypeInstances.Add(commandInstance);
                            if (this.CanCommandBeRunBasedOnActions(commandInstance))
                            {
                                this.perActionTypeTasks.Add(AsyncRunner.RunAsync(() => this.BackgroundCommandTypeRunner(type)));
                            }
                        }
                        else if (ChannelSession.Settings.CommandServiceLockType == CommandServiceLockTypeEnum.VisualAudioActions)
                        {
                            HashSet <ActionTypeEnum> actionTypes = commandInstance.GetActionTypes();
                            if (actionTypes.Contains(ActionTypeEnum.Overlay) || actionTypes.Contains(ActionTypeEnum.OvrStream) || actionTypes.Contains(ActionTypeEnum.Sound) ||
                                actionTypes.Contains(ActionTypeEnum.StreamingSoftware) || actionTypes.Contains(ActionTypeEnum.TextToSpeech))
                            {
                                singularInstances.Add(commandInstance);
                                if (singularTask == null)
                                {
                                    singularTask = AsyncRunner.RunAsync(() => this.BackgroundCommandTypeRunner(type));
                                }
                            }
                            else
                            {
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                                AsyncRunner.RunAsync(() => this.RunDirectly(commandInstance));
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                            }
                        }
                        else if (ChannelSession.Settings.CommandServiceLockType == CommandServiceLockTypeEnum.Singular)
                        {
                            singularInstances.Add(commandInstance);
                            if (singularTask == null)
                            {
                                singularTask = AsyncRunner.RunAsync(() => this.BackgroundCommandTypeRunner(type));
                            }
                        }
                        return(Task.FromResult(0));
                    });
                }
            }

            this.OnCommandInstanceAdded(this, commandInstance);
        }