IPC.cpp 7.6 KB

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