NetworkEntityTests.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <CommonNetworkEntitySetup.h>
  8. #include <MockInterfaces.h>
  9. #include <TestMultiplayerComponent.h>
  10. #include <Source/NetworkEntity/NetworkEntityManager.h>
  11. #include <Source/NetworkEntity/EntityReplication/PropertyPublisher.h>
  12. #include <Source/EntityDomains/FullOwnershipEntityDomain.h>
  13. #include <Source/EntityDomains/NullEntityDomain.h>
  14. #include <Source/ReplicationWindows/NullReplicationWindow.h>
  15. #include <AzCore/Component/Entity.h>
  16. #include <AzCore/Console/Console.h>
  17. #include <AzCore/Name/Name.h>
  18. #include <AzCore/UnitTest/TestTypes.h>
  19. #include <AzCore/UnitTest/UnitTest.h>
  20. #include <AzFramework/Components/TransformComponent.h>
  21. #include <AzNetworking/Serialization/StringifySerializer.h>
  22. #include <AzNetworking/UdpTransport/UdpPacketHeader.h>
  23. #include <AzTest/AzTest.h>
  24. #include <Multiplayer/Components/NetBindComponent.h>
  25. #include <Multiplayer/NetworkEntity/EntityReplication/EntityReplicator.h>
  26. #include <Multiplayer/NetworkInput/NetworkInput.h>
  27. #include <Multiplayer/NetworkInput/NetworkInputArray.h>
  28. #include <Multiplayer/NetworkInput/NetworkInputHistory.h>
  29. #include <Multiplayer/NetworkInput/NetworkInputMigrationVector.h>
  30. namespace Multiplayer
  31. {
  32. class MultiplayerNetworkEntityTests : public NetworkEntityTests
  33. {
  34. public:
  35. void SetUp() override
  36. {
  37. NetworkEntityTests::SetUp();
  38. m_root = AZStd::make_unique<EntityInfo>(1, "root", NetEntityId{ 1 }, EntityInfo::Role::Root);
  39. PopulateNetworkEntity(*m_root);
  40. SetupEntity(m_root->m_entity, m_root->m_netId, NetEntityRole::Authority);
  41. // Create an entity replicator for the root entity
  42. const NetworkEntityHandle rootHandle(m_root->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  43. m_root->m_replicator = AZStd::make_unique<EntityReplicator>(
  44. *m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, rootHandle);
  45. m_root->m_replicator->Initialize(rootHandle);
  46. m_root->m_replicator->ActivateNetworkEntity();
  47. }
  48. void TearDown() override
  49. {
  50. m_root.reset();
  51. NetworkEntityTests::TearDown();
  52. }
  53. void PopulateNetworkEntity(const EntityInfo& entityInfo)
  54. {
  55. entityInfo.m_entity->CreateComponent<AzFramework::TransformComponent>();
  56. entityInfo.m_entity->CreateComponent<NetBindComponent>();
  57. entityInfo.m_entity->CreateComponent<NetworkTransformComponent>();
  58. }
  59. AZStd::unique_ptr<EntityInfo> m_root;
  60. };
  61. TEST_F(MultiplayerNetworkEntityTests, ConstNetworkEntityHandleTest)
  62. {
  63. ConstNetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  64. EXPECT_TRUE(handle.Exists());
  65. EXPECT_NE(handle.GetEntity(), nullptr);
  66. EXPECT_NE(handle.GetNetBindComponent(), nullptr);
  67. EXPECT_NE(handle.GetNetEntityId(), InvalidNetEntityId);
  68. EXPECT_TRUE(handle == handle.GetEntity());
  69. EXPECT_TRUE(handle.GetEntity() == handle);
  70. EXPECT_FALSE(handle != handle.GetEntity());
  71. EXPECT_FALSE(handle.GetEntity() != handle);
  72. EXPECT_FALSE(handle == nullptr);
  73. EXPECT_FALSE(nullptr == handle);
  74. EXPECT_TRUE(handle != nullptr);
  75. EXPECT_TRUE(nullptr != handle);
  76. EXPECT_TRUE(handle == handle);
  77. EXPECT_FALSE(handle != handle);
  78. EXPECT_FALSE(handle < handle);
  79. EXPECT_NE(handle.FindComponent<NetworkTransformComponent>(), nullptr);
  80. EXPECT_NE(handle.FindComponent(NetworkTransformComponent::RTTI_Type()), nullptr);
  81. EXPECT_EQ(handle.FindComponent<NetworkHierarchyChildComponent>(), nullptr);
  82. EXPECT_EQ(handle.FindComponent(NetworkHierarchyChildComponent::RTTI_Type()), nullptr);
  83. handle.Reset(handle);
  84. EXPECT_NE(handle, nullptr);
  85. handle.Reset();
  86. EXPECT_EQ(handle, nullptr);
  87. EXPECT_EQ(handle.GetNetBindComponent(), nullptr);
  88. }
  89. TEST_F(MultiplayerNetworkEntityTests, NetworkEntityHandleTest)
  90. {
  91. NetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  92. EXPECT_NE(handle.FindComponent(NetworkTransformComponent::RTTI_Type()), nullptr);
  93. EXPECT_NE(handle.FindController(NetworkTransformComponent::RTTI_Type()), nullptr);
  94. }
  95. TEST_F(MultiplayerNetworkEntityTests, TestEntityAuthorityTracker)
  96. {
  97. ConstNetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  98. const HostId localhost = HostId("127.0.0.1", 6777, ProtocolType::Udp);
  99. m_networkEntityManager->Initialize(localhost, AZStd::make_unique<NullEntityDomain>());
  100. NetworkEntityAuthorityTracker* netEntityAuthorityTracker = m_networkEntityManager->GetNetworkEntityAuthorityTracker();
  101. netEntityAuthorityTracker->AddEntityAuthorityManager(handle, localhost);
  102. EXPECT_TRUE(netEntityAuthorityTracker->DoesEntityHaveOwner(handle));
  103. netEntityAuthorityTracker->RemoveEntityAuthorityManager(handle, localhost);
  104. // Succeeds on authority role
  105. EXPECT_TRUE(netEntityAuthorityTracker->DoesEntityHaveOwner(handle));
  106. netEntityAuthorityTracker->SetTimeoutTimeMs(AZ::TimeMs(33));
  107. }
  108. TEST_F(MultiplayerNetworkEntityTests, TestNullDomain)
  109. {
  110. ConstNetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  111. const HostId localhost = HostId("127.0.0.1", 6777, ProtocolType::Udp);
  112. m_networkEntityManager->Initialize(localhost, AZStd::make_unique<NullEntityDomain>());
  113. EXPECT_TRUE(m_networkEntityManager->IsInitialized());
  114. IEntityDomain* domain = m_networkEntityManager->GetEntityDomain();
  115. EXPECT_NE(domain, nullptr);
  116. EXPECT_EQ(domain->GetAabb(), AZ::Aabb::CreateNull());
  117. EXPECT_FALSE(domain->IsInDomain(handle));
  118. domain->SetAabb(AZ::Aabb::CreateFromMinMax(AZ::Vector3(1, 1, 1), AZ::Vector3(2, 2, 2)));
  119. EXPECT_EQ(domain->GetAabb(), AZ::Aabb::CreateNull());
  120. domain->HandleLossOfAuthoritativeReplicator(handle);
  121. EXPECT_TRUE(m_networkEntityManager->IsMarkedForRemoval(handle));
  122. domain->DebugDraw();
  123. }
  124. TEST_F(MultiplayerNetworkEntityTests, TestFullOwnershipDomain)
  125. {
  126. ConstNetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  127. const HostId localhost = HostId("127.0.0.1", 6777, ProtocolType::Udp);
  128. m_networkEntityManager->Initialize(localhost, AZStd::make_unique<FullOwnershipEntityDomain>());
  129. EXPECT_TRUE(m_networkEntityManager->IsInitialized());
  130. IEntityDomain* domain = m_networkEntityManager->GetEntityDomain();
  131. EXPECT_NE(domain, nullptr);
  132. EXPECT_EQ(domain->GetAabb(), AZ::Aabb::CreateNull());
  133. EXPECT_TRUE(domain->IsInDomain(handle));
  134. domain->SetAabb(AZ::Aabb::CreateFromMinMax(AZ::Vector3(1, 1, 1), AZ::Vector3(2, 2, 2)));
  135. EXPECT_EQ(domain->GetAabb(), AZ::Aabb::CreateNull());
  136. AZ_TEST_START_TRACE_SUPPRESSION;
  137. domain->HandleLossOfAuthoritativeReplicator(handle);
  138. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  139. domain->DebugDraw();
  140. }
  141. TEST_F(MultiplayerNetworkEntityTests, TestNetworkEntityTracker)
  142. {
  143. const NetworkEntityTracker* constNetEntityTracker = m_networkEntityManager->GetNetworkEntityTracker();
  144. EXPECT_EQ(constNetEntityTracker->GetNetBindComponent(nullptr), nullptr);
  145. uint8_t entityCount = 0;
  146. for (NetworkEntityTracker::const_iterator it = constNetEntityTracker->begin(); it != constNetEntityTracker->end(); ++it)
  147. {
  148. ++entityCount;
  149. }
  150. EXPECT_EQ(entityCount, constNetEntityTracker->size());
  151. NetworkEntityTracker* netEntityTracker = m_networkEntityManager->GetNetworkEntityTracker();
  152. Multiplayer::NetEntityId netId = netEntityTracker->Get(m_root->m_entity->GetId());
  153. NetworkEntityHandle handle = netEntityTracker->Get(netId);
  154. EXPECT_NE(handle.GetEntity(), nullptr);
  155. AZ::EntityId InvalidId = AZ::EntityId();
  156. EXPECT_EQ(netEntityTracker->Get(InvalidId), InvalidNetEntityId);
  157. ConstNetworkEntityHandle constHandle = constNetEntityTracker->Get(netId);
  158. EXPECT_EQ(handle, constHandle);
  159. EXPECT_TRUE(netEntityTracker->Exists(netId));
  160. NetworkEntityTracker::iterator it = netEntityTracker->begin();
  161. AZ::Entity* entity = netEntityTracker->Move(it);
  162. EXPECT_NE(entity, nullptr);
  163. netEntityTracker->Add(it->first, entity);
  164. netEntityTracker->erase(it->first);
  165. EXPECT_FALSE(netEntityTracker->Exists(netId));
  166. }
  167. TEST_F(MultiplayerNetworkEntityTests, TestReplicatorPendingDeletion)
  168. {
  169. m_root->m_replicator->SetPendingRemoval(AZ::TimeMs(100));
  170. EXPECT_TRUE(m_root->m_replicator->IsPendingRemoval());
  171. m_root->m_replicator->ClearPendingRemoval();
  172. EXPECT_FALSE(m_root->m_replicator->IsPendingRemoval());
  173. EXPECT_FALSE(m_root->m_replicator->IsDeletionAcknowledged());
  174. }
  175. struct TestRpcStruct : public Multiplayer::IRpcParamStruct
  176. {
  177. TestRpcStruct()
  178. {
  179. }
  180. TestRpcStruct(const AZ::Vector3& impulse, const AZ::Vector3& worldPoint)
  181. : m_impulse(impulse)
  182. , m_worldPoint(worldPoint)
  183. {
  184. ;
  185. }
  186. bool Serialize(AzNetworking::ISerializer& serializer) override
  187. {
  188. bool ret(true);
  189. ret &= serializer.Serialize(m_impulse, "impulse");
  190. ret &= serializer.Serialize(m_worldPoint, "worldPoint");
  191. return ret;
  192. };
  193. AZ::Vector3 m_impulse;
  194. AZ::Vector3 m_worldPoint;
  195. };
  196. TEST_F(MultiplayerNetworkEntityTests, TestNetworkEntityRpcMessage)
  197. {
  198. ConstNetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  199. NetworkEntityRpcMessage message = NetworkEntityRpcMessage(
  200. RpcDeliveryType::AuthorityToClient,
  201. handle.GetNetEntityId(),
  202. handle.FindComponent<NetworkTransformComponent>()->GetNetComponentId(),
  203. RpcIndex(0),
  204. ReliabilityType::Unreliable);
  205. // Test getters
  206. TestRpcStruct params;
  207. EXPECT_FALSE(message.GetRpcParams(params));
  208. message.SetRpcParams(params);
  209. EXPECT_EQ(message.GetComponentId(), handle.FindComponent<NetworkTransformComponent>()->GetNetComponentId());
  210. EXPECT_EQ(message.GetEntityId(), handle.GetNetEntityId());
  211. EXPECT_EQ(message.GetReliability(), ReliabilityType::Unreliable);
  212. EXPECT_EQ(message.GetRpcDeliveryType(), RpcDeliveryType::AuthorityToClient);
  213. EXPECT_EQ(message.GetRpcIndex(), RpcIndex(0));
  214. EXPECT_TRUE(message.GetRpcParams(params));
  215. // Test setters
  216. message.SetReliability(ReliabilityType::Reliable);
  217. EXPECT_EQ(message.GetReliability(), ReliabilityType::Reliable);
  218. message.SetRpcDeliveryType(RpcDeliveryType::AuthorityToAutonomous);
  219. EXPECT_EQ(message.GetRpcDeliveryType(), RpcDeliveryType::AuthorityToAutonomous);
  220. const NetworkEntityRpcMessage constMessage = NetworkEntityRpcMessage(
  221. RpcDeliveryType::AuthorityToClient,
  222. handle.GetNetEntityId(),
  223. handle.FindComponent<NetworkTransformComponent>()->GetNetComponentId(),
  224. RpcIndex(1),
  225. ReliabilityType::Unreliable);
  226. // Test ctors, assignment and comparisons (const and non const versions)
  227. NetworkEntityRpcMessage message2(AZStd::move(message));
  228. EXPECT_EQ(message, message2);
  229. EXPECT_TRUE(message != constMessage);
  230. message = constMessage;
  231. EXPECT_EQ(message, constMessage);
  232. message = NetworkEntityRpcMessage(constMessage);
  233. EXPECT_EQ(message, constMessage);
  234. NetworkEntityRpcMessage message3 = constMessage;
  235. message3 = message;
  236. EXPECT_EQ(message, message3);
  237. const NetworkEntityRpcMessage constMessage2(message2);
  238. NetworkEntityRpcMessage message4 = constMessage;
  239. message4 = constMessage2;
  240. AzNetworking::StringifySerializer serializer;
  241. EXPECT_TRUE(message.Serialize(serializer));
  242. EXPECT_EQ(message.GetEstimatedSerializeSize(), 15);
  243. m_entityReplicationManager->AddDeferredRpcMessage(message);
  244. m_entityReplicationManager->AddDeferredRpcMessage(message2);
  245. AZ_TEST_START_TRACE_SUPPRESSION;
  246. m_entityReplicationManager->SendUpdates();
  247. AZ_TEST_STOP_TRACE_SUPPRESSION(3);
  248. m_entityReplicationManager->SetReplicationWindow(AZStd::make_unique<NullReplicationWindow>(m_mockConnection.get()));
  249. m_entityReplicationManager->AddDeferredRpcMessage(message);
  250. m_entityReplicationManager->AddDeferredRpcMessage(message2);
  251. AZ_TEST_START_TRACE_SUPPRESSION;
  252. m_entityReplicationManager->SendUpdates();
  253. AZ_TEST_STOP_TRACE_SUPPRESSION(0);
  254. NetworkEntityRpcVector rpcVector;
  255. rpcVector.push_back(message);
  256. m_entityReplicationManager->HandleEntityRpcMessages(m_mockConnection.get(), rpcVector);
  257. }
  258. TEST_F(MultiplayerNetworkEntityTests, TestNetworkEntityUpdateMessage)
  259. {
  260. ConstNetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  261. NetworkEntityUpdateMessage message = NetworkEntityUpdateMessage();
  262. // Test getters
  263. EXPECT_EQ(message.GetNetworkRole(), NetEntityRole::InvalidRole);
  264. EXPECT_EQ(message.GetData(), nullptr);
  265. EXPECT_EQ(message.GetEntityId(), InvalidNetEntityId);
  266. EXPECT_FALSE(message.GetHasValidPrefabId());
  267. EXPECT_FALSE(message.GetIsDelete());
  268. EXPECT_TRUE(message.GetPrefabEntityId().m_prefabName.IsEmpty());
  269. EXPECT_FALSE(message.GetWasMigrated());
  270. // Test setters
  271. PrefabEntityId prefabId;
  272. prefabId.m_entityOffset = 0;
  273. prefabId.m_prefabName = AZ::Name("Test");
  274. message.SetPrefabEntityId(prefabId);
  275. EXPECT_NE(message.GetPrefabEntityId().m_prefabName.GetCStr(), "");
  276. message.SetData(message.ModifyData());
  277. EXPECT_NE(message.GetData(), nullptr);
  278. AzNetworking::StringifySerializer serializer;
  279. EXPECT_TRUE(message.Serialize(serializer));
  280. EXPECT_EQ(message.GetEstimatedSerializeSize(), 17);
  281. // Test ctors, assignment and comparisons (const and non const versions)
  282. message = NetworkEntityUpdateMessage(NetEntityRole::Authority, m_root->m_netId, true, false);
  283. EXPECT_EQ(m_root->m_netId, message.GetEntityId());
  284. EXPECT_EQ(message.GetNetworkRole(), NetEntityRole::Authority);
  285. message = NetworkEntityUpdateMessage(NetEntityRole::Authority, m_root->m_netId, false, false);
  286. AzNetworking::PacketEncodingBuffer buffer;
  287. message.SetPrefabEntityId(prefabId);
  288. EXPECT_NE(message.GetPrefabEntityId().m_prefabName.GetCStr(), "");
  289. message.SetData(buffer);
  290. NetworkEntityUpdateMessage message2(AZStd::move(message));
  291. EXPECT_EQ(message, message2);
  292. EXPECT_TRUE(m_root->m_replicator->PrepareToGenerateUpdatePacket());
  293. const NetworkEntityUpdateMessage constMessage = m_root->m_replicator->GenerateUpdatePacket();
  294. m_root->m_replicator->RecordSentPacketId(AzNetworking::PacketId{ 1 });
  295. EXPECT_TRUE(message != constMessage);
  296. message = constMessage;
  297. EXPECT_EQ(message, constMessage);
  298. message = NetworkEntityUpdateMessage(constMessage);
  299. EXPECT_EQ(message, constMessage);
  300. NetworkEntityUpdateMessage message3 = constMessage;
  301. message3 = message;
  302. EXPECT_EQ(message, message3);
  303. const NetworkEntityUpdateMessage constMessage2(message2);
  304. NetworkEntityUpdateMessage message4 = constMessage;
  305. message4 = constMessage2;
  306. UdpPacketHeader header(PacketType{ 11111 }, InvalidSequenceId, SequenceId{ 1 }, InvalidSequenceId, 0xF8000FFF, SequenceRolloverCount{ 0 });
  307. AZ_TEST_START_TRACE_SUPPRESSION;
  308. EXPECT_FALSE(m_entityReplicationManager->HandleEntityUpdateMessage(m_mockConnection.get(), header, message));
  309. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  310. EXPECT_FALSE(m_entityReplicationManager->HandleEntityDeleteMessage(m_root->m_replicator.get(), header, message));
  311. EXPECT_TRUE(m_entityReplicationManager->HandleEntityUpdateMessage(m_mockConnection.get(), header, constMessage));
  312. }
  313. TEST_F(MultiplayerNetworkEntityTests, EntityReplicatorNoDeleteSentIfCreateWasNotSent)
  314. {
  315. // Don't send an entity delete message if no create messages have been sent yet either.
  316. m_root->m_replicator->MarkForRemoval();
  317. EXPECT_FALSE(m_root->m_replicator->HasChangesToPublish());
  318. }
  319. TEST_F(MultiplayerNetworkEntityTests, EntityReplicationManagerNoDeleteHandledIfNoCreateReceived)
  320. {
  321. // Don't process an entity delete message if no create message has been received yet.
  322. // If the message is processed, the entity will get created then immediately destroyed, which is wasted processing.
  323. // "Send" a creation message.
  324. EXPECT_TRUE(m_root->m_replicator->HasChangesToPublish());
  325. EXPECT_TRUE(m_root->m_replicator->PrepareToGenerateUpdatePacket());
  326. const NetworkEntityUpdateMessage createMessage = m_root->m_replicator->GenerateUpdatePacket();
  327. m_root->m_replicator->RecordSentPacketId(AzNetworking::PacketId{ 1 });
  328. // Mark the entity as deleted and "send" a delete message.
  329. m_root->m_replicator->MarkForRemoval();
  330. EXPECT_TRUE(m_root->m_replicator->PrepareToGenerateUpdatePacket());
  331. const NetworkEntityUpdateMessage deleteMessage = m_root->m_replicator->GenerateUpdatePacket();
  332. m_root->m_replicator->RecordSentPacketId(AzNetworking::PacketId{ 2 });
  333. // When processing the message, nothing should happen, and the message should be marked as handled because it is dropped.
  334. // If the message were attempted to be processed, HandleEntityUpdateMessage() would return false from
  335. // a deserialize failure due to the wrong network role, which would then cause this test to fail.
  336. UdpPacketHeader header(
  337. PacketType{ 11111 }, InvalidSequenceId, SequenceId{ 1 }, InvalidSequenceId, 0xF8000FFF, SequenceRolloverCount{ 0 });
  338. EXPECT_TRUE(m_entityReplicationManager->HandleEntityUpdateMessage(m_mockConnection.get(), header, deleteMessage));
  339. }
  340. TEST_F(MultiplayerNetworkEntityTests, EntityReplicatorDeleteMessageIncludesUpdatedProperties)
  341. {
  342. // When sending a delete message, the message should also contain any properties that were changed
  343. // since the previous replication.
  344. // Always claim that every packet sent was acknowledged.
  345. ON_CALL(*m_mockConnection, WasPacketAcked).WillByDefault(::testing::Return(true));
  346. // First "send" a creation message.
  347. EXPECT_TRUE(m_root->m_replicator->HasChangesToPublish());
  348. EXPECT_TRUE(m_root->m_replicator->PrepareToGenerateUpdatePacket());
  349. const NetworkEntityUpdateMessage createMessage = m_root->m_replicator->GenerateUpdatePacket();
  350. m_root->m_replicator->RecordSentPacketId(AzNetworking::PacketId{ 1 });
  351. EXPECT_FALSE(createMessage.GetIsDelete());
  352. // It should be seen as sent, so nothing more to publish.
  353. EXPECT_FALSE(m_root->m_replicator->HasChangesToPublish());
  354. // Change the translation on the entity and notify that it has been dirtied.
  355. // Make sure the replicator sees it as a new change.
  356. AZ::TransformBus::Event(m_root->m_entity->GetId(), &AZ::TransformBus::Events::SetWorldTranslation, AZ::Vector3(1.0f, 2.0f, 3.0f));
  357. m_networkEntityManager->NotifyEntitiesDirtied();
  358. EXPECT_TRUE(m_root->m_replicator->HasChangesToPublish());
  359. // Next, mark the entity as deleted.
  360. m_root->m_replicator->MarkForRemoval();
  361. // The delete should be seen as a change that needs to be sent.
  362. EXPECT_TRUE(m_root->m_replicator->HasChangesToPublish());
  363. // Generate the delete packet.
  364. EXPECT_TRUE(m_root->m_replicator->PrepareToGenerateUpdatePacket());
  365. const NetworkEntityUpdateMessage deleteMessage = m_root->m_replicator->GenerateUpdatePacket();
  366. m_root->m_replicator->RecordSentPacketId(AzNetworking::PacketId{ 2 });
  367. // The message should be a delete message that has a payload larger than 3 bytes.
  368. // A 3-byte payload is just a header, more than 3 bytes includes property data changes.
  369. EXPECT_TRUE(deleteMessage.GetIsDelete());
  370. EXPECT_GT(deleteMessage.GetData()->GetSize(), 3);
  371. // The delete should now be seen as sent too.
  372. EXPECT_FALSE(m_root->m_replicator->HasChangesToPublish());
  373. }
  374. TEST_F(MultiplayerNetworkEntityTests, EntityReplicatorDeleteMessageResentUntilAcknowledged)
  375. {
  376. // When sending a delete message, the message should keep getting resent until it has been acknowledged.
  377. // Start by mocking that no packets were acknowledged.
  378. ON_CALL(*m_mockConnection, WasPacketAcked).WillByDefault(::testing::Return(false));
  379. // First "send" a creation message.
  380. EXPECT_TRUE(m_root->m_replicator->PrepareToGenerateUpdatePacket());
  381. const NetworkEntityUpdateMessage createMessage = m_root->m_replicator->GenerateUpdatePacket();
  382. m_root->m_replicator->RecordSentPacketId(AzNetworking::PacketId{ 1 });
  383. // Next, mark the entity as deleted.
  384. m_root->m_replicator->MarkForRemoval();
  385. // Generate the delete packet.
  386. EXPECT_TRUE(m_root->m_replicator->PrepareToGenerateUpdatePacket());
  387. const NetworkEntityUpdateMessage deleteMessage = m_root->m_replicator->GenerateUpdatePacket();
  388. m_root->m_replicator->RecordSentPacketId(AzNetworking::PacketId{ 2 });
  389. EXPECT_TRUE(deleteMessage.GetIsDelete());
  390. // The delete shouldn't be seen as acknowledged yet, so we should still need to publish changes.
  391. EXPECT_FALSE(m_root->m_replicator->IsDeletionAcknowledged());
  392. EXPECT_TRUE(m_root->m_replicator->HasChangesToPublish());
  393. // Generate another delete packet. It should still not be acknowledged, so we should still need to publish changes.
  394. EXPECT_TRUE(m_root->m_replicator->PrepareToGenerateUpdatePacket());
  395. const NetworkEntityUpdateMessage deleteMessage2 = m_root->m_replicator->GenerateUpdatePacket();
  396. m_root->m_replicator->RecordSentPacketId(AzNetworking::PacketId{ 3 });
  397. EXPECT_TRUE(deleteMessage2.GetIsDelete());
  398. EXPECT_FALSE(m_root->m_replicator->IsDeletionAcknowledged());
  399. EXPECT_TRUE(m_root->m_replicator->HasChangesToPublish());
  400. // Set the messages to acknowledged and make sure there are no longer any changes to publish.
  401. ON_CALL(*m_mockConnection, WasPacketAcked).WillByDefault(::testing::Return(true));
  402. EXPECT_TRUE(m_root->m_replicator->IsDeletionAcknowledged());
  403. EXPECT_FALSE(m_root->m_replicator->HasChangesToPublish());
  404. }
  405. TEST_F(MultiplayerNetworkEntityTests, TestNetworkEntityManagerRelevancy)
  406. {
  407. ConstNetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  408. EXPECT_TRUE(m_networkEntityManager->GetAlwaysRelevantToClientsSet().empty());
  409. EXPECT_TRUE(m_networkEntityManager->GetAlwaysRelevantToServersSet().empty());
  410. m_networkEntityManager->MarkAlwaysRelevantToClients(handle, true);
  411. m_networkEntityManager->MarkAlwaysRelevantToServers(handle, true);
  412. EXPECT_FALSE(m_networkEntityManager->GetAlwaysRelevantToClientsSet().empty());
  413. EXPECT_FALSE(m_networkEntityManager->GetAlwaysRelevantToServersSet().empty());
  414. m_networkEntityManager->MarkAlwaysRelevantToClients(handle, false);
  415. m_networkEntityManager->MarkAlwaysRelevantToServers(handle, false);
  416. EXPECT_TRUE(m_networkEntityManager->GetAlwaysRelevantToClientsSet().empty());
  417. EXPECT_TRUE(m_networkEntityManager->GetAlwaysRelevantToServersSet().empty());
  418. m_networkEntityManager->SetMigrateTimeoutTimeMs(AZ::TimeMs(0));
  419. }
  420. TEST_F(MultiplayerNetworkEntityTests, TestNetworkEntityManagerHandleExit)
  421. {
  422. ConstNetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  423. Multiplayer::NetEntityIdSet idSet({ m_root->m_netId });
  424. m_networkEntityManager->HandleEntitiesExitDomain(idSet);
  425. }
  426. TEST_F(MultiplayerNetworkEntityTests, TestNetworkEntityManagerForceAssumeAuth)
  427. {
  428. ConstNetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  429. AZ_TEST_START_TRACE_SUPPRESSION;
  430. m_networkEntityManager->ForceAssumeAuthority(handle);
  431. AZ_TEST_STOP_TRACE_SUPPRESSION(2);
  432. }
  433. TEST_F(MultiplayerNetworkEntityTests, TestNetworkEntityManagerDebugDraw)
  434. {
  435. m_networkEntityManager->DebugDraw();
  436. }
  437. TEST_F(MultiplayerNetworkEntityTests, TestNetBindGetSet)
  438. {
  439. ConstNetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  440. NetBindComponent* netBindComponent = handle.GetNetBindComponent();
  441. EXPECT_FALSE(netBindComponent->IsProcessingInput());
  442. EXPECT_FALSE(netBindComponent->IsReprocessingInput());
  443. EXPECT_FALSE(netBindComponent->IsNetEntityRoleServer());
  444. EXPECT_FALSE(netBindComponent->IsNetEntityRoleClient());
  445. EXPECT_EQ(netBindComponent->GetAllowEntityMigration(), EntityMigration::Enabled);
  446. netBindComponent->SetAllowEntityMigration(EntityMigration::Disabled);
  447. EXPECT_EQ(netBindComponent->GetAllowEntityMigration(), EntityMigration::Disabled);
  448. ConstNetworkEntityHandle handle2 = netBindComponent->GetEntityHandle();
  449. EXPECT_EQ(handle, handle2);
  450. EXPECT_TRUE(netBindComponent->GetPredictableRecord().HasChanges());
  451. netBindComponent->NotifyLocalChanges();
  452. AZ::Data::AssetId prefabAssetId(AZ::Uuid("Test"), 1);
  453. EXPECT_NE(prefabAssetId, netBindComponent->GetPrefabAssetId());
  454. netBindComponent->SetPrefabAssetId(prefabAssetId);
  455. EXPECT_EQ(prefabAssetId, netBindComponent->GetPrefabAssetId());
  456. }
  457. TEST_F(MultiplayerNetworkEntityTests, TestNetBindDirty)
  458. {
  459. ConstNetworkEntityHandle handle(m_root->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  460. NetBindComponent* netBindComponent = handle.GetNetBindComponent();
  461. netBindComponent->MarkDirty();
  462. m_networkEntityManager->NotifyEntitiesDirtied();
  463. }
  464. TEST_F(MultiplayerNetworkEntityTests, TestNetBindPropertyValidateAuthority)
  465. {
  466. AZStd::unique_ptr<EntityInfo> testEntity = AZStd::make_unique<EntityInfo>(3, "root", NetEntityId{ 3 }, EntityInfo::Role::None);
  467. PopulateNetworkEntity(*testEntity);
  468. SetupEntity(testEntity->m_entity, testEntity->m_netId, NetEntityRole::Authority);
  469. testEntity->m_entity->Activate();
  470. ConstNetworkEntityHandle handle(testEntity->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  471. NetBindComponent* netBindComponent = handle.GetNetBindComponent();
  472. EXPECT_TRUE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Authority, NetEntityRole::Server));
  473. EXPECT_TRUE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Authority, NetEntityRole::Autonomous));
  474. EXPECT_TRUE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Authority, NetEntityRole::Client));
  475. EXPECT_TRUE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Autonomous, NetEntityRole::Authority));
  476. const bool predictable(true);
  477. EXPECT_TRUE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Server, predictable));
  478. EXPECT_TRUE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Autonomous, predictable));
  479. EXPECT_TRUE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Client, predictable));
  480. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Autonomous, NetEntityRole::Authority, predictable));
  481. const bool notPredictable(false);
  482. EXPECT_TRUE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Server, notPredictable));
  483. EXPECT_TRUE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Autonomous, notPredictable));
  484. EXPECT_TRUE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Client, notPredictable));
  485. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Autonomous, NetEntityRole::Authority, notPredictable));
  486. }
  487. TEST_F(MultiplayerNetworkEntityTests, TestNetBindPropertyValidateServer)
  488. {
  489. AZStd::unique_ptr<EntityInfo> testEntity = AZStd::make_unique<EntityInfo>(4, "root", NetEntityId{ 4 }, EntityInfo::Role::None);
  490. PopulateNetworkEntity(*testEntity);
  491. SetupEntity(testEntity->m_entity, testEntity->m_netId, NetEntityRole::Server);
  492. testEntity->m_entity->Activate();
  493. ConstNetworkEntityHandle handle(testEntity->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  494. NetBindComponent* netBindComponent = handle.GetNetBindComponent();
  495. EXPECT_TRUE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Authority, NetEntityRole::Server));
  496. EXPECT_TRUE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Authority, NetEntityRole::Autonomous));
  497. EXPECT_TRUE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Authority, NetEntityRole::Client));
  498. EXPECT_FALSE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Autonomous, NetEntityRole::Authority));
  499. const bool predictable(true);
  500. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Server, predictable));
  501. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Autonomous, predictable));
  502. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Client, predictable));
  503. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Autonomous, NetEntityRole::Authority, predictable));
  504. const bool notPredictable(false);
  505. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Server, notPredictable));
  506. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Autonomous, notPredictable));
  507. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Client, notPredictable));
  508. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Autonomous, NetEntityRole::Authority, notPredictable));
  509. }
  510. TEST_F(MultiplayerNetworkEntityTests, TestNetBindPropertyValidateAutonomous)
  511. {
  512. AZStd::unique_ptr<EntityInfo> testEntity = AZStd::make_unique<EntityInfo>(5, "root", NetEntityId{ 5 }, EntityInfo::Role::None);
  513. PopulateNetworkEntity(*testEntity);
  514. SetupEntity(testEntity->m_entity, testEntity->m_netId, NetEntityRole::Autonomous);
  515. testEntity->m_entity->Activate();
  516. ConstNetworkEntityHandle handle(testEntity->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  517. NetBindComponent* netBindComponent = handle.GetNetBindComponent();
  518. EXPECT_FALSE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Authority, NetEntityRole::Server));
  519. EXPECT_TRUE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Authority, NetEntityRole::Autonomous));
  520. EXPECT_TRUE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Authority, NetEntityRole::Client));
  521. EXPECT_TRUE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Autonomous, NetEntityRole::Authority));
  522. const bool predictable(true);
  523. EXPECT_TRUE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Server, predictable));
  524. EXPECT_TRUE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Autonomous, predictable));
  525. EXPECT_TRUE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Client, predictable));
  526. EXPECT_TRUE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Autonomous, NetEntityRole::Authority, predictable));
  527. const bool notPredictable(false);
  528. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Server, notPredictable));
  529. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Autonomous, notPredictable));
  530. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Client, notPredictable));
  531. EXPECT_TRUE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Autonomous, NetEntityRole::Authority, notPredictable));
  532. }
  533. TEST_F(MultiplayerNetworkEntityTests, TestNetBindPropertyValidateClient)
  534. {
  535. AZStd::unique_ptr<EntityInfo> testEntity = AZStd::make_unique<EntityInfo>(6, "root", NetEntityId{ 6 }, EntityInfo::Role::None);
  536. PopulateNetworkEntity(*testEntity);
  537. SetupEntity(testEntity->m_entity, testEntity->m_netId, NetEntityRole::Client);
  538. testEntity->m_entity->Activate();
  539. ConstNetworkEntityHandle handle(testEntity->m_entity.get(), m_networkEntityManager->GetNetworkEntityTracker());
  540. NetBindComponent* netBindComponent = handle.GetNetBindComponent();
  541. EXPECT_FALSE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Authority, NetEntityRole::Server));
  542. EXPECT_FALSE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Authority, NetEntityRole::Autonomous));
  543. EXPECT_TRUE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Authority, NetEntityRole::Client));
  544. EXPECT_FALSE(netBindComponent->ValidatePropertyRead("TestProperty", NetEntityRole::Autonomous, NetEntityRole::Authority));
  545. const bool predictable(true);
  546. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Server, predictable));
  547. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Autonomous, predictable));
  548. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Client, predictable));
  549. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Autonomous, NetEntityRole::Authority, predictable));
  550. const bool notPredictable(false);
  551. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Server, notPredictable));
  552. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Autonomous, notPredictable));
  553. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Authority, NetEntityRole::Client, notPredictable));
  554. EXPECT_FALSE(netBindComponent->ValidatePropertyWrite("TestProperty", NetEntityRole::Autonomous, NetEntityRole::Authority, notPredictable));
  555. }
  556. } // namespace Multiplayer