IPCBroker.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. otherProcess_ = new IPCProcess(context_, pp_.clientRead(), pp_.clientWrite());
  51. transport_.OpenServer(pp_.serverRead(), pp_.serverWrite());
  52. // copy args
  53. for (unsigned i = 0; i < args.Size(); i++)
  54. pargs.Push(args[i]);
  55. #ifdef ATOMIC_PLATFORM_WINDOWS
  56. wchar_t pipe_num[10];
  57. _i64tow_s(reinterpret_cast<__int64>(pp_.clientWrite()), pipe_num, sizeof(pipe_num)/sizeof(pipe_num[0]), 10);
  58. String cpipe;
  59. cpipe.SetUTF8FromWChar(pipe_num);
  60. String ipc_client = "--ipc-client=" + cpipe;
  61. pargs.Push(ipc_client);
  62. _i64tow_s(reinterpret_cast<__int64>(pp_.clientRead()), pipe_num, sizeof(pipe_num)/sizeof(pipe_num[0]), 10);
  63. String spipe;
  64. spipe.SetUTF8FromWChar(pipe_num);
  65. String ipc_server = "--ipc-server=" + spipe;
  66. pargs.Push(ipc_server);
  67. #else
  68. pargs.Push(ToString("--ipc-server=%i", pp_.fd1()));
  69. pargs.Push(ToString("--ipc-client=%i", pp_.fd2()));
  70. #endif
  71. pargs.Push(ToString("--ipc-id=%i", id_));
  72. if (!otherProcess_->Launch(command, pargs, initialDirectory))
  73. return false;
  74. #ifndef ATOMIC_PLATFORM_WINDOWS
  75. close(pp_.fd2());
  76. #endif
  77. return Run();
  78. }
  79. }