Subprocess.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/IO/Log.h>
  8. #include <Atomic/Core/Timer.h>
  9. #include "Subprocess.h"
  10. #include "SubprocessSystem.h"
  11. namespace ToolCore
  12. {
  13. Subprocess::Subprocess(Context* context) :
  14. Object(context),
  15. processHandle_(NULL),
  16. inStream_(pipeOut_),
  17. errorStream_(pipeError_),
  18. returnCode_(-1)
  19. {
  20. }
  21. Subprocess::~Subprocess()
  22. {
  23. if (processHandle_)
  24. delete processHandle_;
  25. }
  26. void Subprocess::ThreadFunction()
  27. {
  28. while (shouldRun_)
  29. {
  30. int read;
  31. char data[129];
  32. if (!Poco::Process::isRunning(*processHandle_))
  33. {
  34. shouldRun_ = false;
  35. break;
  36. }
  37. inStream_.read(data, 128);
  38. read = inStream_.gcount();
  39. data[read] = 0;
  40. if (read)
  41. {
  42. mutex_.Acquire();
  43. output_ += (const char*) data;
  44. mutex_.Release();
  45. }
  46. if (inStream_.eof())
  47. {
  48. returnCode_ = processHandle_->wait();
  49. if (returnCode_)
  50. {
  51. while (true)
  52. {
  53. errorStream_.read(data, 128);
  54. read = errorStream_.gcount();
  55. data[read] = 0;
  56. if (read)
  57. {
  58. mutex_.Acquire();
  59. errorOutput_ += (const char*) data;
  60. mutex_.Release();
  61. }
  62. else
  63. {
  64. break;
  65. }
  66. }
  67. }
  68. shouldRun_ = false;
  69. break;
  70. }
  71. }
  72. }
  73. void Subprocess::ProcessOutput(SubprocessSystem* system)
  74. {
  75. String output, errorOutput;
  76. GetOutput(output, errorOutput);
  77. if (output.Length())
  78. {
  79. VariantMap eventData;
  80. eventData[SubprocessOutput::P_TEXT] = output;
  81. SendEvent(E_SUBPROCESSOUTPUT, eventData);
  82. }
  83. if (errorOutput.Length())
  84. {
  85. VariantMap eventData;
  86. eventData[SubprocessOutput::P_TEXT] = errorOutput;
  87. SendEvent(E_SUBPROCESSOUTPUT, eventData);
  88. }
  89. }
  90. bool Subprocess::Update(SubprocessSystem* system)
  91. {
  92. ProcessOutput(system);
  93. if (!shouldRun_) {
  94. ProcessOutput(system);
  95. VariantMap eventData;
  96. eventData[SubprocessComplete::P_RETCODE] = returnCode_;
  97. SendEvent(E_SUBPROCESSCOMPLETE, eventData);
  98. return false;
  99. }
  100. return true;
  101. }
  102. bool Subprocess::Launch(const String& command, const Vector<String>& args, const String& initialDirectory)
  103. {
  104. Poco::Process::Env env;
  105. return Launch(command, args, initialDirectory, env);
  106. }
  107. bool Subprocess::Launch(const String& command, const Vector<String>& args, const String& initialDirectory, const Poco::Process::Env& env)
  108. {
  109. Poco::Process::Args pargs;
  110. for (unsigned i = 0; i < args.Size(); i++)
  111. pargs.push_back(args[i].CString());
  112. std::string pcommand = command.CString();
  113. std::string pinitialDirectory = initialDirectory.CString();
  114. // this can take an ENV as well, may come in useful
  115. processHandle_ = new Poco::ProcessHandle(Poco::Process::launch(pcommand, pargs, pinitialDirectory, &pipeIn_, &pipeOut_, &pipeError_, env));
  116. if (!Poco::Process::isRunning(*processHandle_))
  117. return false;
  118. return Run();
  119. }
  120. }