Пример #1
0
 public static void quit(ProcessWrapper proc)
 {
     proc.sendLine("quit");
     proc.readNextLine();
     proc.stopAllReadingAndQuit();
 }
Пример #2
0
        //One or both of id and response may be empty
        public static void GetNextReponse(out int id, out string response, ProcessWrapper proc)
        {
            //Starts with an equal sign, if it has a non \n, then thats id,
            //after that is either space then response, or \n\n.
            //After response is \n\n

            //=id response\n\n
            //=id\n\n
            //= response\n\n
            //=\n\n

            string curLine = proc.readNextLine().Trim();

            while (!curLine.Contains("=") && !curLine.Contains("?"))
                curLine = proc.readNextLine().Trim();

            while (curLine.Length == 0)
            {
                throw new Exception("Recieved empty line? This may be a problem?");
                curLine = proc.readNextLine().Trim();
            }

            if (curLine[0] == '?')
            {
                string error = "";
                error += curLine;
                while (curLine.Length != 0) //Go until we find \n\n (so an empty line)
                {
                    curLine = proc.readNextLine();
                    error += " " + curLine;
                }
                throw new Exception(error);
            }
            else if (curLine[0] != '=')
                throw new Exception("Error expected = or ? on all responses!");

            if (curLine == "=") //No id or response
            {
                curLine = proc.readNextLine(); //read next line still though as we are expecting
                //another line break

                id = -1; //Should be ignored anyway, this is NOT A SENTINEL
                response = null;

                return;
            }
            //else
            if (curLine[1] == ' ') //Just response, no id
            {
                id = -1; //Should be ignored anyway, this is NOT A SENTINEL
                curLine = curLine.Substring(1);
            }
            else
            {
                string[] delimitter = { " " };

                string[] parts = curLine.Split(delimitter, StringSplitOptions.RemoveEmptyEntries);

                id = Convert.ToInt32(parts[0].Substring(0));

                if (parts.Length <= 1) //No response
                {
                    response = null;
                }

                curLine = curLine.Substring(parts[0].Length + 1);
            }

            //There is a response at this point for sure
            response = "";
            do
            {
                string[] delimitter = { " " };

                string[] parts = curLine.Split(delimitter, StringSplitOptions.RemoveEmptyEntries);

                if (response.Length > 0) //Sort of a hack to keep the line stucture
                    response += "\n";

                foreach (string part in parts)
                    response += part + " ";

            } while ((curLine = proc.readNextLine()) != "");
        }