IPCBroker.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "../IO/Log.h"
  27. #include "IPCUnix.h"
  28. #include "IPCBroker.h"
  29. namespace Atomic
  30. {
  31. unsigned IPCBroker::idCounter_ = 1;
  32. IPCBroker::IPCBroker(Context* context) : IPCChannel(context, idCounter_)
  33. {
  34. idCounter_++;
  35. }
  36. IPCBroker::~IPCBroker()
  37. {
  38. }
  39. void IPCBroker::ThreadFunction()
  40. {
  41. while (shouldRun_)
  42. {
  43. if (!otherProcess_->IsRunning())
  44. {
  45. break;
  46. }
  47. if (!Receive())
  48. {
  49. break;
  50. }
  51. }
  52. shouldRun_ = false;
  53. }
  54. bool IPCBroker::Update()
  55. {
  56. if (otherProcess_.Null())
  57. return false;
  58. if (!shouldRun_)
  59. {
  60. Stop();
  61. #ifndef ATOMIC_PLATFORM_WINDOWS
  62. close(pp_.fd1());
  63. #endif
  64. return false;
  65. }
  66. return true;
  67. }
  68. bool IPCBroker::SpawnWorker(const String& command, const Vector<String>& args, const String& initialDirectory)
  69. {
  70. Vector<String> pargs;
  71. #ifdef ATOMIC_PLATFORM_WINDOWS
  72. otherProcess_ = new IPCProcess(context_, pp_.clientRead(), pp_.clientWrite());
  73. transport_.OpenServer(pp_.serverRead(), pp_.serverWrite());
  74. #else
  75. otherProcess_ = new IPCProcess(context_, pp_.fd1(), pp_.fd2());
  76. transport_.OpenServer(pp_.fd1());
  77. #endif
  78. // copy args
  79. for (unsigned i = 0; i < args.Size(); i++)
  80. pargs.Push(args[i]);
  81. #ifdef ATOMIC_PLATFORM_WINDOWS
  82. wchar_t pipe_num[10];
  83. _i64tow_s(reinterpret_cast<__int64>(pp_.clientWrite()), pipe_num, sizeof(pipe_num)/sizeof(pipe_num[0]), 10);
  84. String cpipe;
  85. cpipe.SetUTF8FromWChar(pipe_num);
  86. String ipc_client = "--ipc-client=" + cpipe;
  87. pargs.Push(ipc_client);
  88. _i64tow_s(reinterpret_cast<__int64>(pp_.clientRead()), pipe_num, sizeof(pipe_num)/sizeof(pipe_num[0]), 10);
  89. String spipe;
  90. spipe.SetUTF8FromWChar(pipe_num);
  91. String ipc_server = "--ipc-server=" + spipe;
  92. pargs.Push(ipc_server);
  93. #else
  94. pargs.Push(ToString("--ipc-server=%i", pp_.fd1()));
  95. pargs.Push(ToString("--ipc-client=%i", pp_.fd2()));
  96. #endif
  97. pargs.Push(ToString("--ipc-id=%i", id_));
  98. if (!otherProcess_->Launch(command, pargs, initialDirectory))
  99. return false;
  100. #ifndef ATOMIC_PLATFORM_WINDOWS
  101. close(pp_.fd2());
  102. #endif
  103. return Run();
  104. }
  105. }