/// <summary> /// Send a command to the Folding@Home client server. /// </summary> /// <param name="command">Command text. Null, empty, or whitespace strings will be ignored.</param> /// <exception cref="InvalidOperationException">Connection is not connected.</exception> /// <remarks>Callers should make sure the Connection is connected by checking the value of the Connected property.</remarks> public void SendCommand(string command) { // check connection status, callers should make sure they're connected first if (!Connected) { throw new InvalidOperationException("Connection is not connected."); } if (command == null || command.Trim().Length == 0) { OnStatusMessage(new StatusMessageEventArgs("No command text given.", TraceLevel.Warning)); return; } if (!command.EndsWith("\n", StringComparison.Ordinal)) { command += "\n"; } byte[] buffer = Encoding.ASCII.GetBytes(command); #if SEND_ASYNC _stream.BeginWrite(buffer, 0, buffer.Length, WriteCallback, new AsyncData(command, buffer)); #else try { _stream.Write(buffer, 0, buffer.Length); // send data sent event OnDataLengthSent(new DataLengthEventArgs(buffer.Length)); // send status message OnStatusMessage(new StatusMessageEventArgs(String.Format(CultureInfo.CurrentCulture, "Sent command: {0} ({1} bytes)", CleanUpCommandText(command), buffer.Length), TraceLevel.Info)); } catch (Exception ex) { OnStatusMessage(new StatusMessageEventArgs(String.Format(CultureInfo.CurrentCulture, "Write failed: {0}", ex.Message), TraceLevel.Error)); Close(); } #endif }