IPCBroker.cpp 1.9 KB

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