RemoteToolsSystemComponent.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #pragma once
  9. #include <AzCore/Component/Component.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <AzFramework/Network/IRemoteTools.h>
  12. #include <AzNetworking/ConnectionLayer/IConnectionListener.h>
  13. #include <AzNetworking/DataStructures/ByteBuffer.h>
  14. #include <AzNetworking/Utilities/TimedThread.h>
  15. #include "Utilities/RemoteToolsJoinThread.h"
  16. namespace RemoteToolsPackets
  17. {
  18. class RemoteToolsConnect;
  19. class RemoteToolsMessage;
  20. } // namespace RemoteToolsPackets
  21. namespace RemoteTools
  22. {
  23. // Slightly below AzNetworking's TCP max packet size for maximum message space with room for packet headers
  24. static constexpr uint32_t RemoteToolsBufferSize = AzNetworking::MaxPacketSize - 384;
  25. using RemoteToolsMessageBuffer = AzNetworking::ByteBuffer<RemoteToolsBufferSize>;
  26. struct RemoteToolsRegistryEntry
  27. {
  28. bool m_isHost;
  29. AZ::Name m_name;
  30. AzNetworking::IpAddress m_ip;
  31. AzFramework::RemoteToolsEndpointContainer m_availableTargets;
  32. AzFramework::RemoteToolsEndpointInfo m_lastTarget;
  33. AZStd::vector<AZStd::byte> m_tmpInboundBuffer;
  34. uint32_t m_tmpInboundBufferPos;
  35. AzFramework::RemoteToolsEndpointStatusEvent m_endpointJoinedEvent;
  36. AzFramework::RemoteToolsEndpointStatusEvent m_endpointLeftEvent;
  37. AzFramework::RemoteToolsEndpointConnectedEvent m_endpointConnectedEvent;
  38. AzFramework::RemoteToolsEndpointChangedEvent m_endpointChangedEvent;
  39. };
  40. class RemoteToolsSystemComponent
  41. : public AZ::Component
  42. , public AZ::SystemTickBus::Handler
  43. , public AzFramework::IRemoteTools
  44. , public AzNetworking::IConnectionListener
  45. {
  46. friend class RemoteToolsJoinThread;
  47. public:
  48. AZ_COMPONENT(RemoteToolsSystemComponent, "{ca110b7c-795e-4fa5-baa9-a115d7e3d86e}");
  49. static void Reflect(AZ::ReflectContext* context);
  50. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  51. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  52. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  53. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  54. RemoteToolsSystemComponent();
  55. ~RemoteToolsSystemComponent();
  56. bool HandleRequest(
  57. AzNetworking::IConnection* connection,
  58. const AzNetworking::IPacketHeader& packetHeader,
  59. const RemoteToolsPackets::RemoteToolsConnect& packet);
  60. bool HandleRequest(
  61. AzNetworking::IConnection* connection,
  62. const AzNetworking::IPacketHeader& packetHeader,
  63. const RemoteToolsPackets::RemoteToolsMessage& packet);
  64. protected:
  65. ////////////////////////////////////////////////////////////////////////
  66. // IConnectionListener interface
  67. AzNetworking::ConnectResult ValidateConnect(
  68. const AzNetworking::IpAddress& remoteAddress,
  69. const AzNetworking::IPacketHeader& packetHeader,
  70. AzNetworking::ISerializer& serializer) override;
  71. void OnConnect(AzNetworking::IConnection* connection) override;
  72. AzNetworking::PacketDispatchResult OnPacketReceived(
  73. AzNetworking::IConnection* connection,
  74. const AzNetworking::IPacketHeader& packetHeader,
  75. AzNetworking::ISerializer& serializer) override;
  76. void OnPacketLost(AzNetworking::IConnection* connection, AzNetworking::PacketId packetId) override;
  77. void OnDisconnect(
  78. AzNetworking::IConnection* connection,
  79. AzNetworking::DisconnectReason reason,
  80. AzNetworking::TerminationEndpoint endpoint) override;
  81. ////////////////////////////////////////////////////////////////////////
  82. //////////////////////////////////////////////////////////////////////////
  83. // ObjectStream Load Callback
  84. void OnMessageParsed(AzFramework::RemoteToolsMessage** ppMsg, void* classPtr, const AZ::Uuid& classId, const AZ::SerializeContext* sc);
  85. //////////////////////////////////////////////////////////////////////////
  86. ////////////////////////////////////////////////////////////////////////
  87. // AZ::Component interface implementation
  88. void Init() override;
  89. void Activate() override;
  90. void Deactivate() override;
  91. ////////////////////////////////////////////////////////////////////////
  92. ////////////////////////////////////////////////////////////////////////
  93. // AZ::SystemTickBus::Handler overrides.
  94. void OnSystemTick() override;
  95. ////////////////////////////////////////////////////////////////////////
  96. ////////////////////////////////////////////////////////////////////////
  97. // AzFramework::IRemoteTools interface implementation
  98. void RegisterToolingServiceClient(AZ::Crc32 key, AZ::Name name, uint16_t port) override;
  99. void RegisterToolingServiceHost(AZ::Crc32 key, AZ::Name name, uint16_t port) override;
  100. const AzFramework::ReceivedRemoteToolsMessages* GetReceivedMessages(AZ::Crc32 key) const override;
  101. void ClearReceivedMessages(AZ::Crc32 key) override;
  102. void ClearReceivedMessagesForNextTick(AZ::Crc32 key) override;
  103. void RegisterRemoteToolsEndpointJoinedHandler(AZ::Crc32 key, AzFramework::RemoteToolsEndpointStatusEvent::Handler& handler) override;
  104. void RegisterRemoteToolsEndpointLeftHandler(AZ::Crc32 key, AzFramework::RemoteToolsEndpointStatusEvent::Handler& handler) override;
  105. void RegisterRemoteToolsEndpointConnectedHandler(AZ::Crc32 key, AzFramework::RemoteToolsEndpointConnectedEvent::Handler& handler) override;
  106. void RegisterRemoteToolsEndpointChangedHandler(AZ::Crc32 key, AzFramework::RemoteToolsEndpointChangedEvent::Handler& handler) override;
  107. void EnumTargetInfos(AZ::Crc32 key, AzFramework::RemoteToolsEndpointContainer& infos) override;
  108. void SetDesiredEndpoint(AZ::Crc32 key, AZ::u32 desiredTargetID) override;
  109. void SetDesiredEndpointInfo(AZ::Crc32 key, const AzFramework::RemoteToolsEndpointInfo& targetInfo) override;
  110. AzFramework::RemoteToolsEndpointInfo GetDesiredEndpoint(AZ::Crc32 key) override;
  111. AzFramework::RemoteToolsEndpointInfo GetEndpointInfo(AZ::Crc32 key, AZ::u32 desiredTargetID) override;
  112. bool IsEndpointOnline(AZ::Crc32 key, AZ::u32 desiredTargetID) override;
  113. void SendRemoteToolsMessage(const AzFramework::RemoteToolsEndpointInfo& target, const AzFramework::RemoteToolsMessage& msg) override;
  114. ////////////////////////////////////////////////////////////////////////
  115. AZStd::unique_ptr<RemoteToolsJoinThread> m_joinThread;
  116. AZStd::unordered_map<AZ::Crc32, RemoteToolsRegistryEntry> m_entryRegistry;
  117. AZStd::unordered_map<AZ::Crc32, AzFramework::ReceivedRemoteToolsMessages> m_inbox;
  118. AZStd::mutex m_inboxMutex;
  119. AZStd::set<AZ::Crc32> m_messageTypesToClearForNextTick;
  120. private:
  121. AzFramework::RemoteToolsMessage* DeserializeMessage(const AZ::Crc32& key, AZStd::vector<AZStd::byte>& buffer, const uint32_t& totalBufferSize);
  122. };
  123. } // namespace RemoteTools