/// <summary> /// Send a JSON message to the command. First 2 bytes is size of message. /// </summary> /// <remarks> /// This is used by both asynchronous and no result commands. /// </remarks> /// <param name="json">The JSON string to send.</param> internal void SendJsonCommand(IJson json) { #if DEBUG if (Log.IsDebugEnabled) { Log.Debug("Send: " + json.ToJson()); } #endif Stream output = _process.StandardInput.BaseStream; byte[] msg = JsonMsgUtil.WrapJsonOutput(json); output.Write(msg, 0, msg.Length); output.Flush(); }
/// <summary> /// Send a JSON message to the command. First 2 bytes is size of message. /// </summary> /// <param name="json">The JSON string to send.</param> internal void SendJsonForAsynchronousResults(IJson json) { #if DEBUG if (Log.IsDebugEnabled) { Log.Debug("Send: " + json); } #endif _runningAsynchronously = true; Stream output = _process.StandardInput.BaseStream; byte[] msg = JsonMsgUtil.WrapJsonOutput(json); output.Write(msg, 0, msg.Length); output.Flush(); }
void ReadStandardOutput() { #if DEBUG if (Log.IsDebugEnabled) { Log.Debug("Started reading from standard output."); } #endif try { // Read JSON messages. int retries = 0; const int maxRetries = 12; // 1/4 sec * 4 * 3 = 3 seconds. while (_running) { Stream input = _process.StandardOutput.BaseStream; string response = JsonMsgUtil.ExtractJsonInput(input); if (response == null) { if (retries++ > maxRetries) { #if DEBUG if (Log.IsErrorEnabled) { Log.Error("Null received from input stream. Maximum number of retries reached."); } #endif _running = false; Close(); } #if DEBUG if (Log.IsWarnEnabled) { Log.Warn("Null received from input stream. Waiting 250ms."); } #endif Thread.Sleep(250); } else { retries = 0; // reset _msgQueue.Enqueue(response); } _completed.Set(); } } catch (ThreadAbortException) { #if DEBUG if (Log.IsWarnEnabled) { Log.Warn("StandardOutput: Thread aborted."); } #endif } #if DEBUG if (Log.IsDebugEnabled) { Log.Debug("Finished reading from standard output."); } #endif }