IPCBroker.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef ATOMIC_PLATFORM_WINDOWS
  2. #include <unistd.h>
  3. #endif
  4. #include "../Core/StringUtils.h"
  5. #include "../IO/Log.h"
  6. #include "IPCUnix.h"
  7. #include "IPCBroker.h"
  8. namespace Atomic
  9. {
  10. unsigned IPCBroker::idCounter_ = 1;
  11. IPCBroker::IPCBroker(Context* context) : IPCChannel(context, idCounter_)
  12. {
  13. idCounter_++;
  14. }
  15. IPCBroker::~IPCBroker()
  16. {
  17. }
  18. void IPCBroker::ThreadFunction()
  19. {
  20. while (shouldRun_)
  21. {
  22. if (!otherProcess_->IsRunning())
  23. {
  24. break;
  25. }
  26. if (!Receive())
  27. {
  28. break;
  29. }
  30. }
  31. shouldRun_ = false;
  32. }
  33. bool IPCBroker::Update()
  34. {
  35. if (otherProcess_.Null())
  36. return false;
  37. if (!shouldRun_)
  38. {
  39. Stop();
  40. #ifndef ATOMIC_PLATFORM_WINDOWS
  41. close(pp_.fd1());
  42. #endif
  43. return false;
  44. }
  45. return true;
  46. }
  47. bool IPCBroker::SpawnWorker(const String& command, const Vector<String>& args, const String& initialDirectory)
  48. {
  49. Vector<String> pargs;
  50. #ifdef ATOMIC_PLATFORM_WINDOWS
  51. otherProcess_ = new IPCProcess(context_, pp_.clientRead(), pp_.clientWrite());
  52. transport_.OpenServer(pp_.serverRead(), pp_.serverWrite());
  53. #else
  54. otherProcess_ = new IPCProcess(context_, pp_.fd1(), pp_.fd2());
  55. transport_.OpenServer(pp_.fd1());
  56. #endif
  57. // copy args
  58. for (unsigned i = 0; i < args.Size(); i++)
  59. pargs.Push(args[i]);
  60. #ifdef ATOMIC_PLATFORM_WINDOWS
  61. wchar_t pipe_num[10];
  62. _i64tow_s(reinterpret_cast<__int64>(pp_.clientWrite()), pipe_num, sizeof(pipe_num)/sizeof(pipe_num[0]), 10);
  63. String cpipe;
  64. cpipe.SetUTF8FromWChar(pipe_num);
  65. String ipc_client = "--ipc-client=" + cpipe;
  66. pargs.Push(ipc_client);
  67. _i64tow_s(reinterpret_cast<__int64>(pp_.clientRead()), pipe_num, sizeof(pipe_num)/sizeof(pipe_num[0]), 10);
  68. String spipe;
  69. spipe.SetUTF8FromWChar(pipe_num);
  70. String ipc_server = "--ipc-server=" + spipe;
  71. pargs.Push(ipc_server);
  72. #else
  73. pargs.Push(ToString("--ipc-server=%i", pp_.fd1()));
  74. pargs.Push(ToString("--ipc-client=%i", pp_.fd2()));
  75. #endif
  76. pargs.Push(ToString("--ipc-id=%i", id_));
  77. if (!otherProcess_->Launch(command, pargs, initialDirectory))
  78. return false;
  79. #ifndef ATOMIC_PLATFORM_WINDOWS
  80. close(pp_.fd2());
  81. #endif
  82. return Run();
  83. }
  84. }