IPC.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #if defined(ATOMIC_PLATFORM_OSX) || defined(ATOMIC_PLATFORM_LINUX)
  23. #include <unistd.h>
  24. #endif
  25. #include "../Core/CoreEvents.h"
  26. #include "../Engine/Engine.h"
  27. #include "../IO/Log.h"
  28. #include "IPCBroker.h"
  29. #include "IPCWorker.h"
  30. #include "IPC.h"
  31. #include "IPCEvents.h"
  32. #if defined(ATOMIC_PLATFORM_WINDOWS)
  33. #include <windows.h>
  34. #undef PostMessage
  35. #endif
  36. namespace Atomic
  37. {
  38. IPC::IPC(Context* context) : Object(context),
  39. workerChannelID_(0)
  40. {
  41. SubscribeToEvent(E_BEGINFRAME, HANDLER(IPC, HandleBeginFrame));
  42. #ifdef ATOMIC_PLATFORM_WINDOWS
  43. jobHandle_ = CreateJobObject(NULL, NULL);
  44. if (!jobHandle_)
  45. {
  46. LOGERROR("IPC::IPC - Unable to create IPC job");
  47. }
  48. else
  49. {
  50. JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 };
  51. // Configure all child processes associated with the job to terminate when main process is closed
  52. jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
  53. if (0 == SetInformationJobObject(jobHandle_, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli)))
  54. {
  55. LOGERROR("IPC::IPC - Unable set job information");
  56. jobHandle_ = 0;
  57. }
  58. }
  59. #endif
  60. }
  61. IPC::~IPC()
  62. {
  63. for (unsigned i = 0; i < brokers_.Size(); i++)
  64. brokers_[i]->Stop();
  65. brokers_.Clear();
  66. if (worker_.NotNull())
  67. worker_->Stop();
  68. worker_ = 0;
  69. }
  70. bool IPC::InitWorker(unsigned id, IPCHandle fd1, IPCHandle fd2)
  71. {
  72. workerChannelID_ = id;
  73. #ifndef ATOMIC_PLATFORM_WINDOWS
  74. // close server fd
  75. close(fd1);
  76. worker_ = new IPCWorker(context_, fd2, id);
  77. #else
  78. worker_ = new IPCWorker(context_, fd1, fd2, id);
  79. #endif
  80. worker_->Run();
  81. SendEventToBroker(E_IPCWORKERSTART);
  82. return true;
  83. }
  84. bool IPC::ProcessArguments(const Vector<String>& arguments, int& id, IPCHandle& fd0, IPCHandle& fd1)
  85. {
  86. id = -1;
  87. fd0 = INVALID_IPCHANDLE_VALUE;
  88. fd1 = INVALID_IPCHANDLE_VALUE;
  89. for (unsigned i = 0; i < arguments.Size(); ++i)
  90. {
  91. if (arguments[i].Length() > 1)
  92. {
  93. String argument = arguments[i].ToLower();
  94. if (argument.StartsWith("--ipc-id="))
  95. {
  96. Vector<String> idc = argument.Split(argument.CString(), '=');
  97. if (idc.Size() == 2)
  98. id = ToInt(idc[1].CString());
  99. }
  100. else if (argument.StartsWith("--ipc-server=") || argument.StartsWith("--ipc-client="))
  101. {
  102. LOGINFOF("Starting IPCWorker %s", argument.CString());
  103. Vector<String> ipc = argument.Split(argument.CString(), '=');
  104. if (ipc.Size() == 2)
  105. {
  106. if (argument.StartsWith("--ipc-server="))
  107. {
  108. #ifdef ATOMIC_PLATFORM_WINDOWS
  109. // clientRead
  110. WString wipc(ipc[1]);
  111. HANDLE pipe = reinterpret_cast<HANDLE>(_wtoi64(wipc.CString()));
  112. fd0 = pipe;
  113. #else
  114. int fd = ToInt(ipc[1].CString());
  115. fd0 = fd;
  116. #endif
  117. }
  118. else
  119. {
  120. #ifdef ATOMIC_PLATFORM_WINDOWS
  121. // clientWrite
  122. WString wipc(ipc[1]);
  123. HANDLE pipe = reinterpret_cast<HANDLE>(_wtoi64(wipc.CString()));
  124. fd1 = pipe;
  125. #else
  126. int fd = ToInt(ipc[1].CString());
  127. fd1 = fd;
  128. #endif
  129. }
  130. }
  131. }
  132. }
  133. }
  134. if (id > 0 && fd0 != INVALID_IPCHANDLE_VALUE && fd1 != INVALID_IPCHANDLE_VALUE)
  135. {
  136. return true;
  137. }
  138. return false;
  139. }
  140. IPCBroker* IPC::SpawnWorker(const String& command, const Vector<String>& args, const String& initialDirectory)
  141. {
  142. SharedPtr<IPCBroker> broker(new IPCBroker(context_));
  143. if (broker->SpawnWorker(command, args, initialDirectory))
  144. {
  145. brokers_.Push(broker);
  146. return broker;
  147. }
  148. return 0;
  149. }
  150. void IPC::SendEventToBroker(StringHash eventType)
  151. {
  152. SendEventToBroker(eventType, GetEventDataMap());
  153. }
  154. void IPC::SendEventToBroker(StringHash eventType, VariantMap& eventData)
  155. {
  156. if (worker_.NotNull())
  157. {
  158. worker_->PostMessage(eventType, eventData);
  159. }
  160. }
  161. void IPC::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  162. {
  163. // If we're a worker, if update fails, time to exit
  164. if (worker_.NotNull())
  165. {
  166. if (!worker_->Update())
  167. {
  168. worker_ = 0;
  169. GetSubsystem<Engine>()->Exit();
  170. return;
  171. }
  172. }
  173. // Update brokers
  174. Vector<IPCBroker*> remove;
  175. for (unsigned i = 0; i < brokers_.Size(); i++)
  176. {
  177. SharedPtr<IPCBroker>& broker = brokers_[i];
  178. if (!broker->Update())
  179. {
  180. VariantMap brokerData;
  181. brokerData[IPCWorkerExit::P_BROKER] = broker;
  182. brokerData[IPCWorkerExit::P_EXITCODE] = 0;
  183. broker->SendEvent(E_IPCWORKEREXIT, brokerData);
  184. remove.Push(broker);
  185. }
  186. }
  187. for (unsigned i = 0; i < remove.Size(); i++)
  188. {
  189. brokers_.Remove(SharedPtr<IPCBroker>(remove[i]));
  190. }
  191. eventMutex_.Acquire();
  192. for (List<QueuedEvent>::Iterator itr = queuedEvents_.Begin(); itr != queuedEvents_.End(); itr++)
  193. {
  194. unsigned channelID = (*itr).channelID_;
  195. StringHash qeventType = (*itr).eventType_;
  196. VariantMap& qeventData = (*itr).eventData_;
  197. bool sent = false;
  198. for (unsigned i = 0; i < brokers_.Size(); i++)
  199. {
  200. SharedPtr<IPCBroker>& broker = brokers_[i];
  201. if (broker->GetID() == channelID) {
  202. broker->SendEvent(qeventType, qeventData);
  203. sent = true;
  204. break;
  205. }
  206. }
  207. if (!sent)
  208. SendEvent(qeventType, qeventData);
  209. }
  210. queuedEvents_.Clear();
  211. eventMutex_.Release();
  212. }
  213. void IPC::QueueEvent(unsigned id, StringHash eventType, VariantMap& eventData)
  214. {
  215. eventMutex_.Acquire();
  216. QueuedEvent event;
  217. event.channelID_ = id;
  218. event.eventType_ = eventType;
  219. event.eventData_ = eventData;
  220. queuedEvents_.Push(event);
  221. eventMutex_.Release();
  222. }
  223. }