IPCBroker.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #ifndef ATOMIC_PLATFORM_WINDOWS
  23. #include <unistd.h>
  24. #endif
  25. #include "../Core/StringUtils.h"
  26. #include "../Core/Timer.h"
  27. #include "../IO/Log.h"
  28. #include "IPCUnix.h"
  29. #include "IPCBroker.h"
  30. namespace Atomic
  31. {
  32. unsigned IPCBroker::idCounter_ = 1;
  33. IPCBroker::IPCBroker(Context* context) : IPCChannel(context, idCounter_)
  34. {
  35. idCounter_++;
  36. }
  37. IPCBroker::~IPCBroker()
  38. {
  39. }
  40. void IPCBroker::ThreadFunction()
  41. {
  42. while (shouldRun_)
  43. {
  44. if (!otherProcess_->IsRunning())
  45. {
  46. break;
  47. }
  48. if (!Receive())
  49. {
  50. break;
  51. }
  52. // sleep thread a bit so we don't gobble CPU
  53. Time::Sleep(10);
  54. }
  55. shouldRun_ = false;
  56. }
  57. bool IPCBroker::Update()
  58. {
  59. if (otherProcess_.Null())
  60. return false;
  61. if (!shouldRun_)
  62. {
  63. Stop();
  64. #ifndef ATOMIC_PLATFORM_WINDOWS
  65. close(pp_.fd1());
  66. #endif
  67. return false;
  68. }
  69. return true;
  70. }
  71. bool IPCBroker::SpawnWorker(const String& command, const Vector<String>& args, const String& initialDirectory)
  72. {
  73. Vector<String> pargs;
  74. #ifdef ATOMIC_PLATFORM_WINDOWS
  75. otherProcess_ = new IPCProcess(context_, pp_.clientRead(), pp_.clientWrite());
  76. transport_.OpenServer(pp_.serverRead(), pp_.serverWrite());
  77. #else
  78. otherProcess_ = new IPCProcess(context_, pp_.fd1(), pp_.fd2());
  79. transport_.OpenServer(pp_.fd1());
  80. #endif
  81. // copy args
  82. for (unsigned i = 0; i < args.Size(); i++)
  83. pargs.Push(args[i]);
  84. #ifdef ATOMIC_PLATFORM_WINDOWS
  85. wchar_t pipe_num[10];
  86. _i64tow_s(reinterpret_cast<__int64>(pp_.clientWrite()), pipe_num, sizeof(pipe_num)/sizeof(pipe_num[0]), 10);
  87. String cpipe;
  88. cpipe.SetUTF8FromWChar(pipe_num);
  89. String ipc_client = "--ipc-client=" + cpipe;
  90. pargs.Push(ipc_client);
  91. _i64tow_s(reinterpret_cast<__int64>(pp_.clientRead()), pipe_num, sizeof(pipe_num)/sizeof(pipe_num[0]), 10);
  92. String spipe;
  93. spipe.SetUTF8FromWChar(pipe_num);
  94. String ipc_server = "--ipc-server=" + spipe;
  95. pargs.Push(ipc_server);
  96. #else
  97. pargs.Push(ToString("--ipc-server=%i", pp_.fd1()));
  98. pargs.Push(ToString("--ipc-client=%i", pp_.fd2()));
  99. #endif
  100. pargs.Push(ToString("--ipc-id=%i", id_));
  101. if (!otherProcess_->Launch(command, pargs, initialDirectory))
  102. return false;
  103. #ifndef ATOMIC_PLATFORM_WINDOWS
  104. close(pp_.fd2());
  105. #endif
  106. return Run();
  107. }
  108. }