Add RewindableArray and cleanup a bit more of vector

This commit is contained in:
puvvadar
2021-05-25 16:40:58 -07:00
parent 50b9233552
commit d2797c0d15
7 changed files with 127 additions and 8 deletions
@@ -0,0 +1,46 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <Multiplayer/NetworkTime/INetworkTime.h>
#include <Multiplayer/NetworkTime/RewindableObject.h>
#include <AzNetworking/Serialization/ISerializer.h>
#include <AzNetworking/ConnectionLayer/IConnection.h>
#include <AzNetworking/Utilities/NetworkCommon.h>
#include <AzCore/std/containers/array.h>
#include <AzCore/std/string/string.h>
#include <AzCore/Console/ILogger.h>
namespace Multiplayer
{
//! @class RewindableArray
//! @brief Data structure that has a compile-time upper bound, provides array semantics and supports network serialization
template <typename TYPE, uint32_t SIZE>
class RewindableArray
: public AZStd::array<RewindableObject<TYPE, Multiplayer::RewindHistorySize>, SIZE>
{
public:
//! Serialization method for array contained rewindable objects
//! @param serializer ISerializer instance to use for serialization
//! @return bool true for success, false for serialization failure
bool Serialize(AzNetworking::ISerializer& serializer);
//! Serialization method for array contained rewindable objects
//! @param serializer ISerializer instance to use for serialization
//! @param deltaRecord Bitset delta record used to detect state change during reconciliation
//! @return bool true for success, false for serialization failure
bool Serialize(AzNetworking::ISerializer& serializer, AzNetworking::IBitset &deltaRecord);
};
}
#include <Multiplayer/NetworkTime/RewindableArray.inl>
@@ -0,0 +1,53 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
namespace Multiplayer
{
template <typename TYPE, uint32_t SIZE>
bool RewindableArray<TYPE, SIZE>::Serialize(AzNetworking::ISerializer& serializer)
{
for (uint32_t i = 0; i < size(); ++i)
{
if(!this[i].Serialize(serializer))
{
return false;
}
}
return serializer.IsValid();
}
template <typename TYPE, uint32_t SIZE>
bool RewindableArray<TYPE, SIZE>::Serialize(AzNetworking::ISerializer& serializer, AzNetworking::IBitset& deltaRecord)
{
for (uint32_t i = 0; i < size(); ++i)
{
if (deltaRecord.GetBit(i))
{
serializer.ClearTrackedChangesFlag();
if(!this[i].Serialize(serializer))
{
return false;
}
if ((serializer.GetSerializerMode() == AzNetworking::SerializerMode::WriteToObject) && !serializer.GetTrackedChangesFlag())
{
deltaRecord.SetBit(i, false);
}
}
}
return serializer.IsValid();
}
}
@@ -43,12 +43,13 @@ namespace Multiplayer
//! Serialization method for fixed vector contained rewindable objects
//! @param serializer ISerializer instance to use for serialization
//! @return bool true for success, false for serialization failure
constexpr bool Serialize(AzNetworking::ISerializer& serializer);
bool Serialize(AzNetworking::ISerializer& serializer);
//! Serialization method for fixed vector contained rewindable objects
//! @param serializer ISerializer instance to use for serialization
//! @param deltaRecord Bitset delta record used to detect state change during reconciliation
//! @return bool true for success, false for serialization failure
constexpr bool Serialize(AzNetworking::ISerializer& serializer, AzNetworking::IBitset &deltaRecord);
bool Serialize(AzNetworking::ISerializer& serializer, AzNetworking::IBitset &deltaRecord);
//! Copies elements from the buffer pointed to by Buffer to this FixedSizeVector instance, vector size will be set to BufferSize
//! @param buffer pointer to the buffer to copy
@@ -28,7 +28,7 @@ namespace Multiplayer
}
template <typename TYPE, uint32_t SIZE>
constexpr bool RewindableFixedVector<TYPE, SIZE>::Serialize(AzNetworking::ISerializer& serializer)
bool RewindableFixedVector<TYPE, SIZE>::Serialize(AzNetworking::ISerializer& serializer)
{
m_rewindableSize = m_container.size();
if(!m_rewindableSize.Serialize(serializer) && !resize(m_rewindableSize))
@@ -48,7 +48,7 @@ namespace Multiplayer
}
template <typename TYPE, uint32_t SIZE>
constexpr bool RewindableFixedVector<TYPE, SIZE>::Serialize(AzNetworking::ISerializer& serializer, AzNetworking::IBitset& deltaRecord)
bool RewindableFixedVector<TYPE, SIZE>::Serialize(AzNetworking::ISerializer& serializer, AzNetworking::IBitset& deltaRecord)
{
if (deltaRecord.GetBit(SIZE))
{