Ejemplo n.º 1
0
        private bool PreHandleInterceptors(List <IHandlerInterceptor> interceptors,
                                           SocketRequest request)
        {
            foreach (var interceptor in interceptors)
            {
                var handleResult = interceptor.PreHandle(request);
                if (handleResult.CancelActionInvoke)
                {
                    var response = (handleResult.ResponseSuccess
                        ? ResponseStatus.SUCCESS
                        : ResponseStatus.ERROR);

                    request.ProcessResponse(ActionResult.Json(
                                                new OperationResult("", response, handleResult.Message), response, handleResult.Message), socket);
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        public override object DoInBackGround(int p)
        {
            SocketRequest request = null;

            try
            {
                request = GetRequestSocket();
                if (request == null)
                {
                    return(string.Empty);
                }
                if (request.HasErrors)
                {
                    request.ProcessResponse(ActionResult.Json("", ResponseStatus.ERROR, request.InternalErrorMessage), socket);
                    return(string.Empty);
                }

                controller = request.Controller;
                method     = controller.GetType().GetMethod(request.Action);

                if (ActionLocker.ActionHasLock(controller, method.Name))
                {
                    request.ProcessResponse(ActionResult.Json("", ResponseStatus.LOCKED, $"This action is already being performed by another remote client and is currently blocked. Try again later"), socket);
                    return(string.Empty);
                }

                foreach (ParameterInfo pi in method.GetParameters())
                {
                    if (pi.ParameterType == typeof(SocketRequest))//.Equals("MobileAppServer.ServerObjects.SocketRequest"))
                    {
                        request.AddParameter(new RequestParameter("request", request));
                        continue;
                    }

                    if (!request.Parameters.Any(rp => rp.Name.Equals(pi.Name)))
                    {
                        throw new Exception($"O parâmetro '{pi.Name}', necessário em {request.Controller.GetType().Name}/{request.Action}, não foi informado.");
                    }
                }

                //GLOBAL Interceptors
                var globalInterceptors = Server.GlobalInstance.Interceptors.Where(i =>
                                                                                  i.ActionName.Equals("") && i.ControllerName.Equals("")).ToList();
                if (!PreHandleInterceptors(globalInterceptors, new SocketRequest(request.Controller, request.Action, RequestBody.Parameters, socket)))
                {
                    return(string.Empty);
                }

                //CONTROLLER (all actions) Interceptors
                var controllerInterceptors = Server.GlobalInstance.Interceptors.Where(i =>
                                                                                      i.ActionName.Equals("") && i.ControllerName.Equals(controller.GetType().Name)).ToList();
                if (!PreHandleInterceptors(controllerInterceptors, new SocketRequest(request.Controller, request.Action, RequestBody.Parameters, socket)))
                {
                    return(string.Empty);
                }

                //CONTROLLER (specific action) Interceptors
                var controllerActionInterceptors = Server.GlobalInstance.Interceptors.Where(i =>
                                                                                            i.ActionName.Equals(method.Name) && i.ControllerName.Equals(controller.GetType().Name)).ToList();
                if (!PreHandleInterceptors(controllerActionInterceptors, new SocketRequest(request.Controller, request.Action, RequestBody.Parameters, socket)))
                {
                    return(string.Empty);
                }

                object[] methodParameters = new object[request.Parameters.Count];
                for (int i = 0; i < request.Parameters.Count; i++)
                {
                    methodParameters[i] = request.Parameters[i].Value;
                }

                LogController.WriteLog(new ServerLog($"Invoking action '{controller.GetType().Name}.{method.Name}'...", controller.GetType().Name, method.Name));
                Stopwatch w = new Stopwatch();
                w.Start();
                ActionResult result = (ActionResult)method.Invoke(controller, methodParameters);
                w.Stop();

                if (w.ElapsedMilliseconds > 10000)
                {
                    ServerAlertManager.CreateAlert(new ServerAlert(request.Controller.GetType().Name, request.Action,
                                                                   $"The action it is taking considerable time to execute ({w.ElapsedMilliseconds} ms). Review your code to improve performance.'"));
                }

                ActionLocker.ReleaseLock(controller, method.Name);

                LogController.WriteLog(new ServerLog($"Request completed in {w.ElapsedMilliseconds}ms", controller.GetType().Name, method.Name));
                request.ProcessResponse(result, socket);

                return(result);
            }
            catch (Exception ex)
            {
                if (controller != null &&
                    method != null)
                {
                    ActionLocker.ReleaseLock(controller, method.Name);
                }

                if (request != null)
                {
                    request.ProcessResponse(ActionResult.Json("", ResponseStatus.ERROR, $"Process request error: \n{ ex.Message}"), socket);
                }
                return(string.Empty);
            }
        }