Beispiel #1
0
        public Server()
        {
            SettingsLoader = new SettingsLoader();
            LoadSettings();

            CommandHandler = new CommandHandler();
            CommandHandler.AddCommand(new CommandHandler.Command("quit", () => Stop()));
            CommandHandler.AddCommand(new CommandHandler.Command("reloadsettings", () => LoadSettings()));
        }
Beispiel #2
0
        public void Start()
        {
            config = new ConfigLoad();
            data_exchange = new DataExchange(this);
            command_handler = new CommandHandler(this);
            queue_command = new Queue<DataXMLPackage>();

            thread_data = new Thread(data_exchange.dataConector);// поток для приема и передачи данных
            thread_data.Start();
            thread_command = new Thread(command_handler.ReadCommandClient);// поток для обработки комманд клиента
            thread_command.Start();

            System.Console.WriteLine("Сервер запущен!"); //\"" + config.vars["S_SERVER_NAME"] + "\"
            command_handler.ReadCommandConsole();
        }
Beispiel #3
0
        public void HandleCommand(AcceptedClient xClient, string xMessage)
        {
            string command = xMessage.Split(' ')[0].ToLower();
            string message = "";

            string target = "";

            switch (command)
            {
            case "/help":
                message = CommandHandler.GetHelpMessage();
                xClient.WriteMessage(message);
                break;

            case "/getuser":
                message = CommandHandler.GetUserMessage(_clients.Keys.ToList());
                xClient.WriteMessage(message);
                break;

            case "/login":
                string username = CommandHandler.ExtractUsernameFromLogin(xMessage);
                if (xClient.IsLoggedIn)
                {
                    message = CommandHandler.GetErrorAlreadyLoggedIn();
                    xClient.WriteMessage(message);
                }
                else if (IsUsernameValid(username) == false)
                {
                    message = CommandHandler.GetErrorUsernameNotAllowed(username);
                    xClient.WriteMessage(message);
                }
                else if (_clients.ContainsKey(username))
                {
                    message = CommandHandler.GetErrorUserExisting();
                    xClient.WriteMessage(message);
                }
                else
                {
                    _clients.Add(username, xClient);
                    xClient.IsLoggedIn = true;
                    xClient.Name       = username;

                    message = CommandHandler.GetLoginConfirmedMessage(username);
                    xClient.WriteMessage(message);
                }
                break;

            case "/whisper":
                string messageTarget = CommandHandler.ExtractWhisperMessage(xClient.Name, xMessage, out target);

                if (string.IsNullOrWhiteSpace(target) || string.IsNullOrWhiteSpace(messageTarget))
                {
                    message = CommandHandler.GetErrorCommandNotRecognized();
                    xClient.WriteMessage(message);
                }
                else if (_clients.ContainsKey(target) == false)
                {
                    message = CommandHandler.GetErrorUserNotExisting(target);
                    xClient.WriteMessage(message);
                }
                else
                {
                    message = CommandHandler.GetWhisperConfirmed(target);
                    _clients[target].WriteMessage(messageTarget);
                    xClient.WriteMessage(message);
                }

                break;

            case "/logoff":
                xClient.IsLoggedIn = false;
                xClient.WriteMessage("Logoff received. You're logged off from the server!");
                _clients.Remove(xClient.Name);
                xClient.StopClient();
                break;

            default:
                message = CommandHandler.GetErrorCommandNotRecognized();
                xClient.WriteMessage(message);
                break;
            }
        }