コード例 #1
0
        public Command Parse(string userText)
        {
            if (userText == "quit")
            {
                return(Command.Quit);
            }

            if (userText == "exit")
            {
                return(Command.Quit);
            }

            if (userText == "history")
            {
                return(Command.History);
            }

            if (userText == "help")
            {
                return(Command.Help);
            }

            if (userText == "about")
            {
                return(Command.About);
            }

            if (userText == "connect")
            {
                return(Command.IncompleteConnect);
            }

            if (userText == "disconnect")
            {
                return(Command.NotImplemented);
            }


            // ----- was this a valid 'connect' command ?? ------
            ConnectCommandParseResult temp = ValidConnectionCommand(userText);

            // if valid, then try to connect to server
            if (temp.Valid)
            {
                LastConnectCommandParseResult.IpAddress = temp.IpAddress;
                LastConnectCommandParseResult.Port      = temp.Port;

                return(Command.ConnectWithArguments);
            }

            // if get here, we don't recognise the command
            return(Command.NotRecognised);
        }
コード例 #2
0
        /// parse text from user
        /// IF
        ///     in the form: connect <ip> <port>
        /// THEN
        ///     return true
        ///
        /// else return false
        public ConnectCommandParseResult ValidConnectionCommand(string userText)
        {
            // defaults to Valid = false
            ConnectCommandParseResult result = new ConnectCommandParseResult();

            /*
             * -------------------------------------------------- BEGIN
             * ------- quick hack to save matt typing connection details lots of times
             * REMOVE from final version !!!!!!!
             */
            if (userText == "localhost")
            {
                result.Valid     = true;
                result.IpAddress = "127.0.0.1";
                result.Port      = 11000;
                return(result);
            }
            // ---------------------------------------------------- END

            // 3 words
            // 1st = "connect"
            // 2nd = valid ip address
            // 3rd = port number

            string[] words = userText.Split(' ');

            // must be 3 words
            if (words.Length != 3)
            {
                return(result);
            }

            // first word must be 'connect'
            if (words[0] != "connect")
            {
                return(result);
            }

            // 3rd words must evaluate to an integer
            int userPort;

            if (Int32.TryParse(words[2], out userPort))
            {
                // seems okay - so set values ready to attempt to connect
                result.Valid     = true;
                result.IpAddress = words[1];
                result.Port      = userPort;
            }

            return(result);
        }