/// <summary> /// Read a command from the control connection. /// </summary> /// <returns> /// True if a command was read. /// </returns> /// <param name='verb'> /// Will receive the verb of the command. /// </param> /// <param name='args'> /// Will receive the arguments of the command, or null. /// </param> private bool ReadCommand(out string verb, out string args) { verb = null; args = null; int endPos = -1; // can there already be a command in the buffer? if (cmdRcvBytes > 0) { Array.IndexOf(cmdRcvBuffer, (byte)'\n', 0, cmdRcvBytes); } try { // read data until a newline is found do { int freeBytes = cmdRcvBuffer.Length - cmdRcvBytes; int bytes = controlSocket.Receive(cmdRcvBuffer, cmdRcvBytes, freeBytes, SocketFlags.None); if (bytes <= 0) { break; } cmdRcvBytes += bytes; // search \r\n endPos = Array.IndexOf(cmdRcvBuffer, (byte)'\r', 0, cmdRcvBytes); if (endPos != -1 && (cmdRcvBytes <= endPos + 1 || cmdRcvBuffer[endPos + 1] != (byte)'\n')) { endPos = -1; } } while (endPos == -1 && cmdRcvBytes < cmdRcvBuffer.Length); } catch (SocketException) { // in case the socket is closed or has some other error while reading return(false); } if (endPos == -1) { return(false); } string command = DecodeString(cmdRcvBuffer, endPos); // remove the command from the buffer cmdRcvBytes -= (endPos + 2); Array.Copy(cmdRcvBuffer, endPos + 2, cmdRcvBuffer, 0, cmdRcvBytes); // CF is missing a limited String.Split string[] tokens = command.Split(' '); verb = tokens[0].ToUpper(); // commands are case insensitive args = (tokens.Length > 1 ? String.Join(" ", tokens, 1, tokens.Length - 1) : null); if (logHandler != null) { logHandler.ReceivedCommand(verb, args); } return(true); }