IPCBroker.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <unistd.h>
  2. #include "../Core/StringUtils.h"
  3. #include "../IO/Log.h"
  4. #include "IPCUnix.h"
  5. #include "IPCBroker.h"
  6. namespace Atomic
  7. {
  8. IPCBroker::IPCBroker(Context* context) : IPCChannel(context)
  9. {
  10. }
  11. IPCBroker::~IPCBroker()
  12. {
  13. }
  14. void IPCBroker::ThreadFunction()
  15. {
  16. while (shouldRun_)
  17. {
  18. if (!otherProcess_->IsRunning())
  19. {
  20. break;
  21. }
  22. if (!Receive())
  23. {
  24. break;
  25. }
  26. }
  27. shouldRun_ = false;
  28. }
  29. bool IPCBroker::Update()
  30. {
  31. if (otherProcess_.Null())
  32. return false;
  33. if (!shouldRun_)
  34. {
  35. Stop();
  36. close(pp_.fd1());
  37. return false;
  38. }
  39. return true;
  40. }
  41. bool IPCBroker::SpawnWorker(const String& command, const Vector<String>& args, const String& initialDirectory)
  42. {
  43. Vector<String> pargs;
  44. otherProcess_ = new IPCProcess(context_, pp_.fd1(), pp_.fd2());
  45. transport_.OpenServer(otherProcess_->fd1());
  46. // copy args
  47. for (unsigned i = 0; i < args.Size(); i++)
  48. pargs.Push(args[i]);
  49. #ifdef ATOMIC_PLATFORM_WINDOWS
  50. // fd2 with be a HANDLE on windows
  51. /*
  52. wchar_t pipe_num[10];
  53. _i64tow_s(reinterpret_cast<__int64>(pp.fd2()), pipe_num, sizeof(pipe_num)/sizeof(pipe_num[0]), 10);
  54. writable_cmdline += kCmdLinePipeEq + std::wstring(pipe_num);
  55. */
  56. #else
  57. pargs.Push(ToString("--ipc-server=%i", pp_.fd1()));
  58. pargs.Push(ToString("--ipc-client=%i", pp_.fd2()));
  59. #endif
  60. if (!otherProcess_->Launch(command, pargs, initialDirectory))
  61. return false;
  62. close(pp_.fd2());
  63. return Run();
  64. }
  65. }