RemoteToolsJoinThread.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "RemoteToolsJoinThread.h"
  9. #include <AzCore/IO/SystemFile.h>
  10. #include <AzCore/PlatformIncl.h>
  11. #include <AzCore/Utils/Utils.h>
  12. #include <AzCore/std/ranges/elements_view.h>
  13. #include <AzCore/std/string/conversions.h>
  14. #include <AzCore/Name/Name.h>
  15. #include <AzNetworking/Framework/INetworking.h>
  16. #include <RemoteToolsSystemComponent.h>
  17. #include <Source/AutoGen/RemoteTools.AutoPackets.h>
  18. namespace RemoteTools
  19. {
  20. RemoteToolsJoinThread::RemoteToolsJoinThread(int updateRate, RemoteToolsSystemComponent* component)
  21. : TimedThread("RemoteTools::RemoteToolsJoinThread", AZ::TimeMs(updateRate))
  22. {
  23. m_remoteToolsComponent = component;
  24. }
  25. RemoteToolsJoinThread::~RemoteToolsJoinThread()
  26. {
  27. Stop();
  28. Join();
  29. }
  30. AZStd::string GetPersistentName()
  31. {
  32. AZStd::string persistentName = "O3DE";
  33. char procPath[AZ::IO::MaxPathLength];
  34. AZ::Utils::GetExecutablePathReturnType ret = AZ::Utils::GetExecutablePath(AZStd::data(procPath), AZStd::size(procPath));
  35. if (ret.m_pathStored == AZ::Utils::ExecutablePathResult::Success && ret.m_pathIncludesFilename)
  36. {
  37. persistentName = AZ::IO::PathView(procPath).Filename().Native();
  38. }
  39. return persistentName;
  40. }
  41. void RemoteToolsJoinThread::OnUpdate([[maybe_unused]] AZ::TimeMs updateRateMs)
  42. {
  43. bool isRequestingConnection = false;
  44. auto& networkInterfaces = AZ::Interface<AzNetworking::INetworking>::Get()->GetNetworkInterfaces();
  45. for (const auto& [persistentId, toolsRegistryEntry] : m_remoteToolsComponent->m_entryRegistry)
  46. {
  47. AZ::Name serviceName = toolsRegistryEntry.m_name;
  48. if (!toolsRegistryEntry.m_isHost && networkInterfaces.contains(serviceName))
  49. {
  50. auto networkInterface = networkInterfaces.find(serviceName);
  51. if (networkInterface->second->GetConnectionSet().GetActiveConnectionCount() == 0)
  52. {
  53. AzNetworking::ConnectionId connId = networkInterface->second->Connect(toolsRegistryEntry.m_ip);
  54. if (connId != AzNetworking::InvalidConnectionId)
  55. {
  56. RemoteToolsPackets::RemoteToolsConnect initPacket;
  57. initPacket.SetPersistentId(persistentId);
  58. initPacket.SetDisplayName(GetPersistentName());
  59. networkInterface->second->SendReliablePacket(connId, initPacket);
  60. }
  61. else
  62. {
  63. isRequestingConnection = true;
  64. }
  65. }
  66. }
  67. }
  68. if (!isRequestingConnection)
  69. {
  70. Stop();
  71. }
  72. }
  73. } // namespace RemoteTools