Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            File.WriteAllText(AppProperties.ServerLogPath, "");  // Erase server log file.

            // Set console window position on desktop.
            //try
            //{
            //    //Console.SetWindowPosition(0, 100);
            //    Console.WindowLeft = 0;
            //    Console.WindowTop = 0;
            //}
            //catch (Exception error)
            //{
            //    Console.WriteLine("Error: " + error.Message);
            //    File.AppendAllText(AppProperties.ServerLogPath, "Error: " +
            //        error.Message + Environment.NewLine);
            //}

            ServerTCPConnection.Start();
            //ServerTCPConnection.GetConnection();

            Knowledgebase.Connect();
            Knowledgebase.LoadLang();

            File.AppendAllText(AppProperties.ServerLogPath, "Entering server loop." +
                               Environment.NewLine);
            bool continueRunning = true;
            // For writing to the console only after a number of passes through the server loop.
            int cycleCount = 0;

            while (continueRunning)
            {
                if (cycleCount == 0)
                {
                    Console.Write(DateTime.Now.ToLongTimeString() +
                                  "\r\nServer running...\n");
                }
                if (cycleCount == 0 || cycleCount == 10)
                {
                    Console.Write("Getting commands...\r\n");
                }
                Thread.Sleep(50);
                string commands = ServerTCPConnection.HandleConnection();
                if (!string.IsNullOrEmpty(commands))
                {
                    continueRunning = ProcessCommands(commands);
                }
                cycleCount++;
                if (cycleCount == 20)
                {
                    cycleCount = 0;
                }
            }

            Console.WriteLine("Closing Server...");
            File.AppendAllText(AppProperties.ServerLogPath, "Closing server." +
                               Environment.NewLine);
            Thread.Sleep(300);
            //return 0;
        }
Ejemplo n.º 2
0
        private static bool ProcessCommands(string commands)
        {
            bool continueRunning = true;

            if (commands != null)
            {
                Console.WriteLine("Commands received [" + commands + "]");
                string response = "";
                //commands = commands.ToLower();

                if (commands == "clearstream")
                {
                    // For clearing the client/network stream.
                    ServerTCPConnection.ReturnResponse("stream cleared");
                }
                else if (commands == "closeserver")
                {
                    ServerTCPConnection.ReturnResponse("closing server");
                    Console.WriteLine("Closing server...");
                    ServerTCPConnection.connected = false;
                    continueRunning = false;
                }
                //else if (commands == "serverconnect")
                //{
                //    ServerTCPConnection.ReturnResponse("server connected");
                //}
                else if (commands == "disconnect")
                {
                    ServerTCPConnection.ReturnResponse("server disconnected");
                    Console.WriteLine("Closing connection...");
                    ServerTCPConnection.connected = false;
                }
                else if (commands == "writetolog")
                {
                    response = Knowledgebase.WriteToLog();
                    ServerTCPConnection.ReturnResponse(response);
                }
                else if (commands == "clearlog")
                {
                    response = Knowledgebase.ClearLog();
                    ServerTCPConnection.ReturnResponse(response);
                }
                else if (commands == "runcode")
                {
                    response = RunCode();
                    ServerTCPConnection.ReturnResponse(response);
                }
                else if (commands == "runsql")
                {
                    response = Knowledgebase.RunSQL();
                    ServerTCPConnection.ReturnResponse(response);
                }
                else if (commands == "getnets")
                {
                    string netsData;
                    netsData = Knowledgebase.GetNetsData();
                    ServerTCPConnection.ReturnResponse(netsData);
                }
                else if (commands == "python")
                {
                    //commands = @"print('Hello world!')";
                    response = Knowledgebase.StartPython();
                    ServerTCPConnection.ReturnResponse(response);
                }
                else if (commands.StartsWith("[py]"))
                {
                    if (commands[4..] == "exit()")