IPC.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. IPCBroker* IPC::SpawnWorker(const String& command, const Vector<String>& args, const String& initialDirectory)
  85. {
  86. SharedPtr<IPCBroker> broker(new IPCBroker(context_));
  87. if (broker->SpawnWorker(command, args, initialDirectory))
  88. {
  89. brokers_.Push(broker);
  90. return broker;
  91. }
  92. return 0;
  93. }
  94. void IPC::SendEventToBroker(StringHash eventType)
  95. {
  96. SendEventToBroker(eventType, GetEventDataMap());
  97. }
  98. void IPC::SendEventToBroker(StringHash eventType, VariantMap& eventData)
  99. {
  100. if (worker_.NotNull())
  101. {
  102. worker_->PostMessage(eventType, eventData);
  103. }
  104. }
  105. void IPC::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  106. {
  107. // If we're a worker, if update fails, time to exit
  108. if (worker_.NotNull())
  109. {
  110. if (!worker_->Update())
  111. {
  112. worker_ = 0;
  113. GetSubsystem<Engine>()->Exit();
  114. return;
  115. }
  116. }
  117. // Update brokers
  118. Vector<IPCBroker*> remove;
  119. for (unsigned i = 0; i < brokers_.Size(); i++)
  120. {
  121. SharedPtr<IPCBroker>& broker = brokers_[i];
  122. if (!broker->Update())
  123. {
  124. VariantMap brokerData;
  125. brokerData[IPCWorkerExit::P_BROKER] = broker;
  126. brokerData[IPCWorkerExit::P_EXITCODE] = 0;
  127. broker->SendEvent(E_IPCWORKEREXIT, brokerData);
  128. remove.Push(broker);
  129. }
  130. }
  131. for (unsigned i = 0; i < remove.Size(); i++)
  132. {
  133. brokers_.Remove(SharedPtr<IPCBroker>(remove[i]));
  134. }
  135. eventMutex_.Acquire();
  136. for (List<QueuedEvent>::Iterator itr = queuedEvents_.Begin(); itr != queuedEvents_.End(); itr++)
  137. {
  138. unsigned channelID = (*itr).channelID_;
  139. StringHash qeventType = (*itr).eventType_;
  140. VariantMap& qeventData = (*itr).eventData_;
  141. bool sent = false;
  142. for (unsigned i = 0; i < brokers_.Size(); i++)
  143. {
  144. SharedPtr<IPCBroker>& broker = brokers_[i];
  145. if (broker->GetID() == channelID) {
  146. broker->SendEvent(qeventType, qeventData);
  147. sent = true;
  148. break;
  149. }
  150. }
  151. if (!sent)
  152. SendEvent(qeventType, qeventData);
  153. }
  154. queuedEvents_.Clear();
  155. eventMutex_.Release();
  156. }
  157. void IPC::QueueEvent(unsigned id, StringHash eventType, VariantMap& eventData)
  158. {
  159. eventMutex_.Acquire();
  160. QueuedEvent event;
  161. event.channelID_ = id;
  162. event.eventType_ = eventType;
  163. event.eventData_ = eventData;
  164. queuedEvents_.Push(event);
  165. eventMutex_.Release();
  166. }
  167. }