Пример #1
0
        internal void ComputeStatisticks(ActionResult result)
        {
            if (result.Type == 1)
            {
                return;
            }
            string json = JsonConvert.SerializeObject(result.Content);

            var    bytes             = Server.GlobalInstance.ServerEncoding.GetBytes(json);
            var    lenght            = bytes.Length;
            double percentBufferUsed = (lenght / (double)Server.GlobalInstance.BufferSize) * 100;

            result.ResponseLenght = lenght;
            result.PercentUsage   = percentBufferUsed;

            if (percentBufferUsed >= 100)
            {
                string msg = $@"
The action response on the controller is using around {percentBufferUsed.ToString("N2")}% of the buffer quota configured on the server.
Server Buffer Size: {Server.GlobalInstance.BufferSize}
Response Size:      {lenght}
Operation has stopped.";

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(msg);
                Console.ForegroundColor = ConsoleColor.Gray;

                var alert = new ServerAlert
                {
                    Action     = Action,
                    Controller = Controller.GetType().Name,
                    Date       = DateTime.Now,
                    Message    = msg
                };

                ServerAlertManager.CreateAlert(alert);
                throw new Exception(msg);
            }

            if (percentBufferUsed >= 80)
            {
                string msg = $"\nThe action response on the controller is using around {percentBufferUsed.ToString("N2")}% of the buffer quota configured on the server. Review your code as soon as possible before the server collapses and begins to give incomplete responses to connected clients.";
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(msg);
                Console.ForegroundColor = ConsoleColor.Gray;

                var alert = new ServerAlert
                {
                    Action     = Action,
                    Controller = Controller.GetType().Name,
                    Date       = DateTime.Now,
                    Message    = msg
                };

                ServerAlertManager.CreateAlert(alert);
            }
        }
Пример #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);
            }
        }