RemoteConsole_impl.inl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <RemoteConsoleCore.h>
  9. AZ_CVAR(AZ::CVarFixedString, log_RemoteConsoleAllowedAddresses, "127.0.0.1",
  10. nullptr, AZ::ConsoleFunctorFlags::Null,
  11. "COMMA separated list of allowed hosts or IP addresses which can connect");
  12. AZ_CVAR(bool, log_EnableRemoteConsole, true,
  13. nullptr, AZ::ConsoleFunctorFlags::Null,
  14. "enables/disables the remote console");
  15. AZ_CVAR(uint16_t, log_RemoteConsolePort, static_cast<uint16_t>(defaultRemoteConsolePort),
  16. nullptr, AZ::ConsoleFunctorFlags::Null,
  17. "Base port (4600 for example) for remote console to listen on. It will start there and continue upwards until an unused one is found.");
  18. /////////////////////////////////////////////////////////////////////////////////////////////
  19. /////////////////////////////////////////////////////////////////////////////////////////////
  20. /////////////////////////////////////////////////////////////////////////////////////////////
  21. CRemoteConsole::CRemoteConsole()
  22. : m_listener(1)
  23. , m_lastPortValue(0)
  24. , m_running(false)
  25. , m_pServer(nullptr)
  26. {
  27. }
  28. /////////////////////////////////////////////////////////////////////////////////////////////
  29. CRemoteConsole::~CRemoteConsole()
  30. {
  31. Stop();
  32. }
  33. /////////////////////////////////////////////////////////////////////////////////////////////
  34. void CRemoteConsole::RegisterConsoleVariables()
  35. {
  36. m_lastPortValue = 0;
  37. }
  38. /////////////////////////////////////////////////////////////////////////////////////////////
  39. void CRemoteConsole::UnregisterConsoleVariables()
  40. {
  41. }
  42. /////////////////////////////////////////////////////////////////////////////////////////////
  43. void CRemoteConsole::Start()
  44. {
  45. if (!IsStarted())
  46. {
  47. m_pServer = new SRemoteServer;
  48. m_pServer->StartServer();
  49. m_running = true;
  50. }
  51. }
  52. /////////////////////////////////////////////////////////////////////////////////////////////
  53. void CRemoteConsole::Stop()
  54. {
  55. // make sure we don't stop if we never started the remote console in the first place
  56. if (IsStarted())
  57. {
  58. m_running = false;
  59. m_pServer->StopServer();
  60. m_pServer->WaitForThread();
  61. delete m_pServer;
  62. m_pServer = nullptr;
  63. }
  64. }
  65. /////////////////////////////////////////////////////////////////////////////////////////////
  66. void CRemoteConsole::AddLogMessage(AZStd::string_view log)
  67. {
  68. if (!IsStarted())
  69. {
  70. return;
  71. }
  72. IRemoteEvent* pEvent = new SStringEvent<eCET_LogMessage>(log);
  73. m_pServer->AddEvent(pEvent);
  74. }
  75. /////////////////////////////////////////////////////////////////////////////////////////////
  76. void CRemoteConsole::AddLogWarning(AZStd::string_view log)
  77. {
  78. if (!IsStarted())
  79. {
  80. return;
  81. }
  82. IRemoteEvent* pEvent = new SStringEvent<eCET_LogWarning>(log);
  83. m_pServer->AddEvent(pEvent);
  84. }
  85. /////////////////////////////////////////////////////////////////////////////////////////////
  86. void CRemoteConsole::AddLogError(AZStd::string_view log)
  87. {
  88. if (!IsStarted())
  89. {
  90. return;
  91. }
  92. IRemoteEvent* pEvent = new SStringEvent<eCET_LogError>(log);
  93. m_pServer->AddEvent(pEvent);
  94. }
  95. /////////////////////////////////////////////////////////////////////////////////////////////
  96. void CRemoteConsole::Update()
  97. {
  98. auto console = AZ::Interface<AZ::IConsole>::Get();
  99. if (bool enableRemoteConsole; console != nullptr
  100. && console->GetCvarValue("log_EnableRemoteConsole", enableRemoteConsole) == AZ::GetValueResult::Success)
  101. {
  102. // we disable the remote console in the editor, since there is no reason to remote into it and we don't want it eating up that port
  103. // number anyway and preventing the game from using it.
  104. bool isEditor = gEnv && gEnv->IsEditor();
  105. bool isEnabled = !isEditor && enableRemoteConsole;
  106. bool isStarted = IsStarted();
  107. uint16_t newPortValue = static_cast<uint16_t>(defaultRemoteConsolePort);
  108. console->GetCvarValue("log_RemoteConsolePort", newPortValue);
  109. // the editor never allows remote control.
  110. if ((isEnabled) && (!isStarted))
  111. {
  112. Start();
  113. }
  114. else if (isStarted)
  115. {
  116. if ((!isEnabled) || (newPortValue != m_lastPortValue))
  117. {
  118. Stop();
  119. }
  120. }
  121. m_lastPortValue = newPortValue;
  122. }
  123. if (m_pServer)
  124. {
  125. TEventBuffer events;
  126. m_pServer->GetEvents(events);
  127. for (TEventBuffer::iterator it = events.begin(), end = events.end(); it != end; ++it)
  128. {
  129. IRemoteEvent* pEvent = *it;
  130. switch (pEvent->GetType())
  131. {
  132. case eCET_ConsoleCommand:
  133. for (TListener::Notifier notifier(m_listener); notifier.IsValid(); notifier.Next())
  134. {
  135. notifier->OnConsoleCommand(((SStringEvent<eCET_ConsoleCommand>*)pEvent)->GetData());
  136. }
  137. break;
  138. case eCET_GameplayEvent:
  139. for (TListener::Notifier notifier(m_listener); notifier.IsValid(); notifier.Next())
  140. {
  141. notifier->OnGameplayCommand(((SStringEvent<eCET_GameplayEvent>*)pEvent)->GetData());
  142. }
  143. break;
  144. }
  145. delete *it;
  146. }
  147. }
  148. }
  149. /////////////////////////////////////////////////////////////////////////////////////////////
  150. void CRemoteConsole::RegisterListener(IRemoteConsoleListener* pListener, const char* name)
  151. {
  152. m_listener.Add(pListener, name);
  153. }
  154. /////////////////////////////////////////////////////////////////////////////////////////////
  155. void CRemoteConsole::UnregisterListener(IRemoteConsoleListener* pListener)
  156. {
  157. m_listener.Remove(pListener);
  158. }