private void processStream(Stream stream) { MessageStream msgStream = new MessageStream(new StandardStream(stream)); Message msg = msgStream.readMessage(); if (msg == null) { return; } EMessageType mType = (EMessageType)msg.getType(); if (mType == EMessageType.eNumber) { // End a session MessageNumber response = new MessageNumber(msg); int sid = response.getNumber(); m_agent.signalToStopSession(sid); } else if (mType == EMessageType.ePidAndCompileRequest) { MessagePidAndCompileRequest request = new MessagePidAndCompileRequest(msg); TCompileResult cr = m_agent.compile(request.getPid(), request.getCmd(), request.getWorkingDir()); // The output file was already saved to local disk by the agent, now respond the result only msg = MessageCompileResponse.createMessage(cr.wasExec, cr.exitCode, cr.outputText, null, 0); msgStream.writeMessage(msg); } else { // WTF??? } }
private bool compile(int sid, string cmd, string workingDir) { if (!isAgentRunning()) { string processPath = SystemUtils.getCurrentProcessPath(); if (!ProcessHelper.justExecuteStatic(processPath + " --agent", SystemUtils.getWorkingDir())) { CConsole.writeError("error: could not start the mongcc agent.\n"); return(false); } Thread.Sleep(s_kWaitingForAgentStartTime); // note: is it enough? } // Check again to make sure the agent is running if (!isAgentRunning()) { CConsole.writeError("error: could not start the mongcc agent.\n"); return(false); } // Connect to the agent and send data NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", Config.s_kAgentName); try { pipeClient.Connect(s_kPipeConnectTimeout); } catch (Exception) { CConsole.writeError("error: could not connect to the mongcc agent.\n"); return(false); } if (!pipeClient.IsConnected) { CConsole.writeError("error: could not connect to the mongcc agent.\n"); return(false); } bool success = false; m_sid = sid == 0 ? SystemUtils.getParentProcessId() : sid; MessageStream stream = new MessageStream(new StandardStream(pipeClient)); Message msg = MessagePidAndCompileRequest.createMessage(m_sid, cmd, workingDir); if (!stream.writeMessage(msg)) { CConsole.writeError("error: could not send data to the mongcc agent.\n"); goto my_end; } setCompiling(true); msg = stream.readMessage(); //============= take long time here if (msg == null) { CConsole.writeError("error: could not receive data from the mongcc agent.\n"); goto my_end; } MessageCompileResponse res = new MessageCompileResponse(msg); success = res.getWasExec() && res.getExitCode() == 0; if (success) { CConsole.writeInfo(res.getOutputText() + "\n"); } else { CConsole.writeError(res.getOutputText() + "\n"); } my_end: pipeClient.Close(); #region temp //Console.WriteLine("OK"); //Console.ReadLine(); #endregion return(success); }