예제 #1
0
        private async void HandleMessage(CoolQScopeEventArgs scope)
        {
            if (RaiseSessionEvent(scope.RouteMessage))
            {
                //return;
            }

            var handled = await HandleApplication(scope);

            if (handled)
            {
                return;
            }
            if (!string.IsNullOrEmpty(scope.RouteMessage.FullCommand))
            {
                HandleCommand(scope);
            }
        }
예제 #2
0
        private void HandleCommand(CoolQScopeEventArgs scope)
        {
            CoolQRouteMessage replyObj = null;

            if (!PluginManager.ContainsPlugin(scope.RouteMessage.CommandName))
            {
                return;
            }

            Type t = PluginManager.GetPluginType(scope.RouteMessage.CommandName);

            CoolQCommandPlugin plugin = PluginManager.CreateInstance <CoolQCommandPlugin>(t);

            if (plugin != null)
            {
                Task.Run(() =>
                {
                    try
                    {
                        if (!plugin.TryInjectParameters(scope.RouteMessage, out var bindingFailedItem))
                        {
                            plugin.OnCommandBindingFailed(new BindingFailedEventArgs(scope, bindingFailedItem));
                            return;
                        }
                        replyObj = plugin.OnMessageReceived(scope);
                    }
                    catch (Exception ex)
                    {
                        plugin.OnErrorOccured(new ExceptionEventArgs(scope, ex));
                    }

                    if (replyObj == null)
                    {
                        return;
                    }
                    SendMessageAsync(replyObj);
                }
                         );
            }
            else
            {
                Logger.Error($"Cannot find plugin: {t.FullName}.");
            }
        }
예제 #3
0
        private async Task <bool> HandleApplication(CoolQScopeEventArgs scope)
        {
            int? priority = int.MinValue;
            bool handled  = false;

            foreach (var appPlugin in PluginManager.ApplicationInstances.OrderByDescending(k =>
                                                                                           k.MiddlewareConfig?.Priority))
            {
                int?p = appPlugin.MiddlewareConfig?.Priority;
                if (p < priority && handled)
                {
                    break;
                }

                priority = appPlugin.MiddlewareConfig?.Priority;

                if (scope.DisabledApplications.Contains(appPlugin))
                {
                    continue;
                }

                CoolQRouteMessage replyObj = null;
                var task = Task.Run(() =>
                {
                    replyObj = ((CoolQApplicationPlugin)appPlugin).OnMessageReceived(scope);
                    if (replyObj != null && !replyObj.Canceled)
                    {
                        SendMessageAsync(replyObj);
                    }
                });

                if (!appPlugin.RunInMultiThreading)
                {
                    await task.ConfigureAwait(false);

                    handled = replyObj?.Handled ?? false;
                }
            }

            return(handled);
        }