예제 #1
0
        private SocketRequest GetRequestSocket()
        {
            if (RequestBody == null)
            {
                return(null);
            }
            if (RequestBody != null)
            {
                if (RequestBody.Controller != null)
                {
                    LogController.WriteLog(new ServerLog($"Processing request for action '{RequestBody.Controller}.{RequestBody.Action}'...", RequestBody.Controller, RequestBody.Action));
                }
            }
            SocketRequest request    = null;
            string        controller = "";
            string        action     = "";

            try
            {
                string path = RequestBody.Action;
                controller = RequestBody.Controller;
                action     = RequestBody.Action;

                request            = new SocketRequest();
                request.Client     = socket;
                request.Action     = action;
                request.Controller = GetController(controller);

                var parameters = typedObjManager.GetParameters(RequestBody.Parameters,
                                                               action, request.Controller);
                parameters.ForEach(p => request.AddParameter(p));

                if (!typedObjManager.ExistsAction(action, controller))
                {
                    throw new Exception($"Action '{action}' not exists in controller '{controller}'");
                }

                return(request);
            }
            catch (Exception ex)
            {
                LogController.WriteLog(new ServerLog(ex.Message + "\n" + ex.StackTrace, controller, action, ServerLogType.ERROR));
                request.HasErrors            = true;
                request.InternalErrorMessage = ex.Message;
                return(request);
            }
        }
예제 #2
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);
        }
예제 #3
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);
            }
        }