IPCServer.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  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. #include <Atomic/IO/Log.h>
  23. #include <Atomic/IO/FileSystem.h>
  24. #include <Atomic/IPC/IPC.h>
  25. #include <Atomic/IPC/IPCEvents.h>
  26. #include <Atomic/IPC/IPCBroker.h>
  27. #include <Atomic/Core/CoreEvents.h>
  28. #include "IPCServer.h"
  29. namespace Atomic
  30. {
  31. unsigned IPCServer::cmdID_ = 1;
  32. IPCResultHandler::IPCResultHandler(Context* context) :
  33. Object(context)
  34. {
  35. }
  36. IPCResultHandler::~IPCResultHandler()
  37. {
  38. }
  39. IPCServer::IPCServer(Context* context) :
  40. Object(context),
  41. brokerEnabled_(false)
  42. {
  43. }
  44. IPCServer::~IPCServer()
  45. {
  46. }
  47. void IPCServer::HandleIPCWorkerStarted(StringHash eventType, VariantMap& eventData)
  48. {
  49. VariantMap startupData;
  50. serverBroker_->PostMessage(E_IPCINITIALIZE, startupData);
  51. brokerEnabled_ = true;
  52. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(IPCServer, HandleUpdate));
  53. SubscribeToEvent(serverBroker_, E_IPCCMDRESULT, ATOMIC_HANDLER(IPCServer, HandleIPCCmdResult));
  54. OnIPCWorkerStarted();
  55. }
  56. void IPCServer::HandleIPCWorkerExit(StringHash eventType, VariantMap& eventData)
  57. {
  58. if (eventData[IPCWorkerExit::P_BROKER] == serverBroker_)
  59. {
  60. serverBroker_ = 0;
  61. brokerEnabled_ = false;
  62. OnIPCWorkerExited();
  63. }
  64. }
  65. void IPCServer::HandleIPCWorkerLog(StringHash eventType, VariantMap& eventData)
  66. {
  67. using namespace IPCWorkerLog;
  68. // convert to a server log
  69. VariantMap serverLogData;
  70. serverLogData["message"] = eventData[P_MESSAGE].GetString();
  71. serverLogData["level"] = eventData[P_LEVEL].GetInt();
  72. SendEvent("IPCServerLog", serverLogData);
  73. OnIPCWorkerLog(eventData[P_LEVEL].GetInt(), eventData[P_MESSAGE].GetString());
  74. }
  75. bool IPCServer::StartInternal(const String& exec, const Vector<String>& args)
  76. {
  77. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  78. clientExecutable_ = exec;
  79. if (!clientExecutable_.Length() || !fileSystem->FileExists(clientExecutable_))
  80. {
  81. ATOMIC_LOGERRORF("IPCServer::Start - Client Executable does not exist: %s", clientExecutable_.CString());
  82. return false;
  83. }
  84. String dump;
  85. dump.Join(args, " ");
  86. ATOMIC_LOGDEBUGF("Launching Broker %s %s", clientExecutable_.CString(), dump.CString());
  87. IPC* ipc = GetSubsystem<IPC>();
  88. serverBroker_ = ipc->SpawnWorker(clientExecutable_, args);
  89. if (serverBroker_)
  90. {
  91. SubscribeToEvent(serverBroker_, E_IPCWORKERSTART, ATOMIC_HANDLER(IPCServer, HandleIPCWorkerStarted));
  92. SubscribeToEvent(serverBroker_, E_IPCWORKEREXIT, ATOMIC_HANDLER(IPCServer, HandleIPCWorkerExit));
  93. SubscribeToEvent(serverBroker_, E_IPCWORKERLOG, ATOMIC_HANDLER(IPCServer, HandleIPCWorkerLog));
  94. }
  95. else
  96. {
  97. ATOMIC_LOGERRORF("Error Spawning Broker %s %s", clientExecutable_.CString(), dump.CString());
  98. }
  99. return serverBroker_.NotNull();
  100. }
  101. bool IPCServer::GetBrokerEnabled() const
  102. {
  103. return brokerEnabled_;
  104. }
  105. void IPCServer::HandleIPCCmdResult(StringHash eventType, VariantMap& eventData)
  106. {
  107. using namespace IPCCmdResult;
  108. unsigned id = eventData[P_ID].GetUInt();
  109. List<IPCCommand>::Iterator itr = cmdProcess_.Begin();
  110. bool found = false;
  111. while (itr != cmdProcess_.End())
  112. {
  113. if ((*itr).id_ == id)
  114. {
  115. IPCCommand cmd = *itr;
  116. cmdProcess_.Erase(itr);
  117. found = true;
  118. if (cmd.handler_.Expired())
  119. {
  120. ATOMIC_LOGERROR("IPCServer::HandleIPCNETCmdResult - IPCNETResult for expired client");
  121. break;
  122. }
  123. cmd.handler_->HandleResult(cmd.id_, eventData);
  124. break;
  125. }
  126. itr++;
  127. }
  128. if (!found)
  129. {
  130. ATOMIC_LOGERRORF("IPCServer::HandleIPCNETCmdResult - IPCNETResult command %u not found in process queue", id);
  131. }
  132. }
  133. unsigned IPCServer::QueueCommand(IPCResultHandler* handler, const VariantMap& cmdMap)
  134. {
  135. IPCCommand cmd;
  136. cmd.id_ = cmdID_++;
  137. cmd.handler_ = handler;
  138. cmd.cmdMap_ = cmdMap;
  139. cmd.cmdMap_[IPCCmd::P_ID] = cmd.id_;
  140. cmdQueue_.Push(cmd);
  141. return cmd.id_;
  142. }
  143. void IPCServer::HandleUpdate(StringHash eventType, VariantMap& eventData)
  144. {
  145. if (!cmdQueue_.Size())
  146. return;
  147. IPCCommand cmd = cmdQueue_.Front();
  148. cmdQueue_.PopFront();
  149. String cmdString = cmd.cmdMap_["command"].GetString();
  150. if (!serverBroker_)
  151. {
  152. ATOMIC_LOGERRORF("IPCServer::HandleUpdate - null player broker for command: %s", cmdString.CString());
  153. return;
  154. }
  155. cmdProcess_.Push(cmd);
  156. serverBroker_->PostMessage(E_IPCCMD, cmd.cmdMap_);
  157. }
  158. }