3
0

ServerToClientReplicationWindow.h 3.4 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. #pragma once
  9. #include <Multiplayer/IMultiplayer.h>
  10. #include <Multiplayer/NetworkEntity/NetworkEntityHandle.h>
  11. #include <Multiplayer/ReplicationWindows/IReplicationWindow.h>
  12. #include <AzNetworking/ConnectionLayer/IConnection.h>
  13. #include <AzCore/Component/EntityBus.h>
  14. #include <AzCore/EBus/ScheduledEvent.h>
  15. #include <AzCore/Console/IConsole.h>
  16. #include <AzCore/std/smart_ptr/unique_ptr.h>
  17. namespace Multiplayer
  18. {
  19. class NetSystemComponent;
  20. class NetworkHierarchyRootComponent;
  21. class ServerToClientReplicationWindow
  22. : public IReplicationWindow
  23. {
  24. public:
  25. struct PrioritizedReplicationCandidate
  26. {
  27. PrioritizedReplicationCandidate() = default;
  28. PrioritizedReplicationCandidate(const ConstNetworkEntityHandle& entityHandle, float priority);
  29. bool operator <(const PrioritizedReplicationCandidate& rhs) const;
  30. bool operator >(const PrioritizedReplicationCandidate& rhs) const;
  31. ConstNetworkEntityHandle m_entityHandle;
  32. float m_priority;
  33. };
  34. // we sort lowest priority first, so that we can easily keep the biggest N priorities
  35. using ReplicationCandidateQueue = AZStd::priority_queue<PrioritizedReplicationCandidate>;
  36. ServerToClientReplicationWindow(NetworkEntityHandle controlledEntity, AzNetworking::IConnection* connection);
  37. //! IReplicationWindow interface
  38. //! @{
  39. bool ReplicationSetUpdateReady() override;
  40. const ReplicationSet& GetReplicationSet() const override;
  41. uint32_t GetMaxProxyEntityReplicatorSendCount() const override;
  42. bool IsInWindow(const ConstNetworkEntityHandle& entityPtr, NetEntityRole& outNetworkRole) const override;
  43. bool AddEntity(AZ::Entity* entity) override;
  44. void RemoveEntity(AZ::Entity* entity) override;
  45. void UpdateWindow() override;
  46. AzNetworking::PacketId SendEntityUpdateMessages(NetworkEntityUpdateVector& entityUpdateVector) override;
  47. void SendEntityRpcs(NetworkEntityRpcVector& entityRpcVector, bool reliable) override;
  48. void SendEntityResets(const NetEntityIdSet& resetIds) override;
  49. void DebugDraw() const override;
  50. //! @}
  51. private:
  52. void UpdateHierarchyReplicationSet(ReplicationSet& replicationSet, NetworkHierarchyRootComponent& hierarchyComponent);
  53. void EvaluateConnection();
  54. void AddEntityToReplicationSet(ConstNetworkEntityHandle& entityHandle, float priority, float distanceSquared);
  55. ServerToClientReplicationWindow& operator=(const ServerToClientReplicationWindow&) = delete;
  56. // sorted in reverse, lowest priority is the top()
  57. ReplicationCandidateQueue m_candidateQueue;
  58. ReplicationSet m_replicationSet;
  59. NetworkEntityHandle m_controlledEntity;
  60. AZ::TransformInterface* m_controlledEntityTransform = nullptr;
  61. AZ::EntityActivatedEvent::Handler m_entityActivatedEventHandler;
  62. AZ::EntityDeactivatedEvent::Handler m_entityDeactivatedEventHandler;
  63. AzNetworking::IConnection* m_connection = nullptr;
  64. // Cached values to detect a poor network connection
  65. uint32_t m_lastCheckedSentPackets = 0;
  66. uint32_t m_lastCheckedLostPackets = 0;
  67. bool m_isPoorConnection = true;
  68. };
  69. }