示例#1
0
        private void WriteToFile <T>(IEnumerable <T> events, string fileName)
            where T : struct
        {
            if (events.Count() == 0)
            {
                return;
            }

            if (!Directory.Exists(@".\Telemetry.Data\"))
            {
                Directory.CreateDirectory(@".\Telemetry.Data\");
            }
            fileName = $@".\Telemetry.Data\{fileName}.tlm";

            using (TextWriter writer = new StreamWriter(fileName, true, coreServer.GetConfiguration().ServerEncoding))
            {
                foreach (T evt in events.ToList())
                {
                    writer.WriteLine(TransformToCSVLine <T>(evt));
                }

                writer.Flush();
                writer.Close();
            }
        }
 private void BasicControllerProcess()
 {
     basicControllerRequestProcessor = new BasicControllerRequestProcess(this);
     if (coreServer.GetConfiguration().IsSingleThreaded)
     {
         basicControllerRequestProcessor.DoInBackGround(uriRequest);
         Dispose();
     }
     else
     {
         basicControllerRequestProcessor.OnCompleted += BasicControllerRequestProc_OnCompleted;
         WaitPendingRequestsCompletations();
         basicControllerRequestProcessor.Execute(uriRequest);
     }
 }
        public override string DoInBackGround(string receivedData)
        {
            try
            {
                SocketRequest req = new SocketRequest(basicController, "RunAction", new System.Collections.Generic.List <RequestParameter>(),
                                                      preProcessor.clientSocket);

                Stopwatch s = new Stopwatch();
                s.Start();
                object resultObject = basicController.RunAction(receivedData, req);
                s.Stop();

                string resultString = string.Empty;

                if (resultObject != null)
                {
                    resultString = (resultObject.GetType() == typeof(string)
                        ? resultObject.ToString()
                        : JsonConvert.SerializeObject(resultObject, AppServerConfigurator.SerializerSettings));
                }

                byte[] resultBytes = coreServer.GetConfiguration().ServerEncoding.GetBytes(resultString);
                preProcessor.clientSocket.Send(resultBytes);
            }
            catch (Exception ex)
            {
                preProcessor.clientSocket.Send(encoder.ConvertToByteArray($"Error: {ex.Message}"));
                logger.WriteLog($"Basic Server Module Error: {ex.Message}", ServerLogType.ERROR);
            }

            preProcessor.Dispose();
            return(string.Empty);
        }
示例#4
0
        public static void AddLock(IController controller, string actionName)
        {
            IServiceManager    manager    = ServiceManager.GetInstance();
            ICoreServerService coreServer = manager.GetService <ICoreServerService>("realserver");

            if (coreServer.GetConfiguration().IsSingleThreaded)
            {
                throw new Exception("Action blocking not allowed for single-threaded servers");
            }

            lockedActions.Add(new KeyValuePair <IController, string>(controller, actionName));
        }
        private ISocketClientConnection GetClient()
        {
            IServiceManager manager = ServiceManager.GetInstance();

            logging = manager.GetService <ILoggingService>();

            ICoreServerService      coreServer = manager.GetService <ICoreServerService>();
            ServerConfiguration     config     = coreServer.GetConfiguration();
            ISocketClientConnection connection = SocketConnectionFactory
                                                 .GetConnection(new SocketClientSettings("localhost", config.Port, config.ServerEncoding));

            return(connection);
        }
示例#6
0
        public override string DoInBackGround(string receivedData)
        {
            try
            {
                Stopwatch s = new Stopwatch();
                s.Start();
                ActionResult result = basicController.RunAction(receivedData);
                s.Stop();

                string resultToJson = JsonConvert.SerializeObject(result, AppServerConfigurator.SerializerSettings);

                byte[] resultBytes = coreServer.GetConfiguration().ServerEncoding.GetBytes(resultToJson);
                preProcessor.clientSocket.Send(resultBytes);
            }
            catch (Exception ex)
            {
                preProcessor.clientSocket.Send(encoder.ConvertToByteArray($"Error: {ex.Message}"));
                logger.WriteLog($"Basic Server Module Error: {ex.Message}", ServerLogType.ERROR);
            }

            preProcessor.Dispose();
            return(string.Empty);
        }
        public byte[] ConvertToByteArray(string str)
        {
            Encoding encoding = coreServer.GetConfiguration().ServerEncoding;

            return(encoding.GetBytes(str));
        }
        internal void ComputeStatisticks(ActionResult result)
        {
            if (AppServerConfigurator.DisableStatisticsCalculating)
            {
                return;
            }
            if (result.Type == 1)
            {
                return;
            }
            StringBuilder json = new StringBuilder();

            using (StringWriter sw = new StringWriter(json))
            {
                using (JsonWriter jw = new JsonTextWriter(sw))
                {
                    JsonSerializer js = new JsonSerializer();
                    js.ApplyCustomSettings();
                    js.Serialize(jw, result.Content);
                }
            }

            int bufferSize = coreServer.GetConfiguration().BufferSize;

            byte[] bytes             = encoder.ConvertToByteArray(json.ToString());
            int    lenght            = bytes.Length;
            double percentBufferUsed = (lenght / (double)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: {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);
            }
        }
示例#9
0
 public ServerConfiguration GetConfiguration()
 {
     return(realServer.GetConfiguration());
 }