Shadowrun: Awakened 29 September 2011 - Build 871
Classes | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes
RakNet::VariableDeltaSerializer Class Reference

Class to compare memory values of variables in a current state to a prior state Results of the comparisons will be written to a bitStream, such that only changed variables get written
Can be used with ReplicaManager3 to Serialize a Replica3 per-variable, rather than comparing the entire object against itself
Usage:
BR> 1. Call BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize(). In the case of Replica3, this would be in the Serialize() call
2. For each variable of the type in step 1, call Serialize(). The same variables must be serialized every tick()
3. Call EndSerialize()
4. Repeat step 1 for each of the other categories of how to send varaibles. More...

#include <VariableDeltaSerializer.h>

List of all members.

Classes

struct  ChangedVariablesList
struct  DeserializationContext
struct  RemoteSystemVariableHistory
struct  SerializationContext

Public Member Functions

void AddRemoteSystemVariableHistory (RakNetGUID guid)
void BeginDeserialize (DeserializationContext *context, BitStream *_bitStream)
 Call when you receive the BitStream written by SerializeVariable(), before calling DeserializeVariable()
void BeginIdenticalSerialize (SerializationContext *context, bool _isFirstSerializeToThisSystem, BitStream *_bitStream)
 Call before doing one or more SerializeVariable calls for data that is sent with the same value to every remote system (such as health, position, etc.) This is the most common type of serialization, and also the most efficient Disadvantages: A copy of every variable still needs to be held, although only once Advantages: After the first serialization, the last serialized bitStream will be used for subsequent sends.
void BeginUniqueSerialize (SerializationContext *context, RakNetGUID _guid, BitStream *_bitStream)
 Call before doing one or more SerializeVariable calls for data that may be sent differently to every remote system (such as an invisibility flag that only teammates can see) The last value of each variable will be saved per remote system. Unlike BeginUnreliableAckedSerialize(), send receipts are not necessary Disadvantages: Every variable for every remote system is copied internally. Very memory and CPU intensive for multiple connections. Advantages: When data is sent differently depending on the recipient, this system can make things easier to use and is as efficient as it can be.
void BeginUnreliableAckedSerialize (SerializationContext *context, RakNetGUID _guid, BitStream *_bitStream, uint32_t _sendReceipt)
 Call before doing one or more SerializeVariable calls when the data will be sent UNRELIABLE_WITH_ACK_RECEIPT The last value of each variable will be saved per remote system. Additionally, a history of _sendReceipts is stored to determine what to resend on packetloss. When variables are lost, they will be flagged dirty and always resent to the system that lost it Disadvantages: Every variable for every remote system is copied internally, in addition to a history list of what variables changed for which _sendReceipt. Very memory and CPU intensive for multiple connections. Advantages: When data needs to be resent by RakNet, RakNet can only resend the value it currently has. This allows the application to control the resend, sending the most recent value of the variable. The end result is that bandwidth is used more efficiently because old data is never sent.
template<class VarType >
bool DeserializeVariable (DeserializationContext *context, VarType &variable)
void EndDeserialize (DeserializationContext *context)
void EndSerialize (SerializationContext *context)
 Call after BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize(), then after calling SerializeVariable() one or more times.
void OnMessageReceipt (RakNetGUID guid, uint32_t receiptId, bool messageArrived)
void OnPreSerializeTick (void)
void RemoveRemoteSystemVariableHistory (RakNetGUID guid)
template<class VarType >
void SerializeVariable (SerializationContext *context, const VarType &variable)
 VariableDeltaSerializer ()
 ~VariableDeltaSerializer ()

Protected Member Functions

ChangedVariablesListAllocChangedVariablesList (void)
void DirtyAndFreeVarsAssociatedWithReceipt (RakNetGUID guid, uint32_t receiptId)
void FreeChangedVariablesList (ChangedVariablesList *changedVariables)
void FreeVarsAssociatedWithReceipt (RakNetGUID guid, uint32_t receiptId)
unsigned int GetRemoteSystemHistoryListIndex (RakNetGUID guid)
RemoteSystemVariableHistoryGetRemoteSystemVariableHistory (RakNetGUID guid)
unsigned int GetVarsWrittenPerRemoteSystemListIndex (RakNetGUID guid)
void RemoveRemoteSystemVariableHistory (void)
RemoteSystemVariableHistoryStartVariableHistoryWrite (RakNetGUID guid)
void StoreChangedVariablesList (RemoteSystemVariableHistory *variableHistory, ChangedVariablesList *changedVariables, uint32_t sendReceipt)

Static Protected Member Functions

static int UpdatedVariablesListPtrComp (const uint32_t &key, ChangedVariablesList *const &data)

Protected Attributes

bool didComparisonThisTick
RakNet::BitStream identicalSerializationBs
DataStructures::List
< RemoteSystemVariableHistory * > 
remoteSystemVariableHistoryList
DataStructures::MemoryPool
< ChangedVariablesList
updatedVariablesMemoryPool

Detailed Description

On the receiver:

1. Call BeginDeserialize(). In the case of Replica3, this would be in the Deserialize() call 2. Call DeserializeVariable() for each variable, in the same order as was Serialized() 3. Call EndSerialize()

See also:
The ReplicaManager3 sample

Definition at line 30 of file VariableDeltaSerializer.h.


Constructor & Destructor Documentation

RakNet::VariableDeltaSerializer::VariableDeltaSerializer ( )
RakNet::VariableDeltaSerializer::~VariableDeltaSerializer ( )

Member Function Documentation

void RakNet::VariableDeltaSerializer::AddRemoteSystemVariableHistory ( RakNetGUID  guid)

BeginUnreliableAckedSerialize() and BeginUniqueSerialize() require knowledge of when connections are added and dropped Call AddRemoteSystemVariableHistory() and RemoveRemoteSystemVariableHistory() to notify the system of these events

Parameters:
[in]_guidWhich system we are sending to
ChangedVariablesList* RakNet::VariableDeltaSerializer::AllocChangedVariablesList ( void  ) [protected]
void RakNet::VariableDeltaSerializer::BeginDeserialize ( DeserializationContext context,
BitStream _bitStream 
)
Parameters:
[in]contextHolds the context of this group of deserialize calls. This can be a stack object just passed to the function.
[in]_bitStreamPass the bitStream originally passed to and written to by serialize calls
void RakNet::VariableDeltaSerializer::BeginIdenticalSerialize ( SerializationContext context,
bool  _isFirstSerializeToThisSystem,
BitStream _bitStream 
)
Precondition:
Call OnPreSerializeTick() before doing any calls to BeginIdenticalSerialize() for each of your objects, once per game tick
Parameters:
[in]contextHolds the context of this group of serialize calls. This can be a stack object just passed to the function.
[in]_isFirstSerializeToThisSystemPass true if this is the first time ever serializing to this system (the initial download). This way all variables will be written, rather than checking against prior sent values.
[in]_bitSteamWhich bitStream to write to
void RakNet::VariableDeltaSerializer::BeginUniqueSerialize ( SerializationContext context,
RakNetGUID  _guid,
BitStream _bitStream 
)
Precondition:
AddRemoteSystemVariableHistory() and RemoveRemoteSystemVariableHistory() must be called for new and lost connections
Parameters:
[in]contextHolds the context of this group of serialize calls. This can be a stack object just passed to the function.
[in]_guidWhich system we are sending to
[in]_bitSteamWhich bitStream to write to
void RakNet::VariableDeltaSerializer::BeginUnreliableAckedSerialize ( SerializationContext context,
RakNetGUID  _guid,
BitStream _bitStream,
uint32_t  _sendReceipt 
)
Precondition:
Upon getting ID_SND_RECEIPT_LOSS or ID_SND_RECEIPT_ACKED call OnMessageReceipt()
AddRemoteSystemVariableHistory() and RemoveRemoteSystemVariableHistory() must be called for new and lost connections
Parameters:
[in]contextHolds the context of this group of serialize calls. This can be a stack object just passed to the function.
[in]_guidWhich system we are sending to
[in]_bitSteamWhich bitStream to write to
[in]_sendReceiptReturned from RakPeer::IncrementNextSendReceipt() and passed to the Send() or SendLists() function. Identifies this update for ID_SND_RECEIPT_LOSS and ID_SND_RECEIPT_ACKED
template<class VarType >
bool RakNet::VariableDeltaSerializer::DeserializeVariable ( DeserializationContext context,
VarType &  variable 
) [inline]

Call to deserialize into a variable

Precondition:
You have called BeginDeserialize()
Note:
Be sure to call EndDeserialize() after finishing all deserializations
Parameters:
[in]contextSame context pointer passed to BeginDeserialize()
[in]variableA variable to write to the bitStream passed to context

Definition at line 198 of file VariableDeltaSerializer.h.

References RakNet::VariableDeltaSerializer::DeserializationContext::bitStream, and RakNet::VariableListDeltaTracker::ReadVarFromBitstream().

    {
        return VariableListDeltaTracker::ReadVarFromBitstream(variable, context->bitStream);
    }
void RakNet::VariableDeltaSerializer::DirtyAndFreeVarsAssociatedWithReceipt ( RakNetGUID  guid,
uint32_t  receiptId 
) [protected]
void RakNet::VariableDeltaSerializer::EndDeserialize ( DeserializationContext context)
Parameters:
[in]contextSame context pointer passed to BeginDeserialize()
void RakNet::VariableDeltaSerializer::EndSerialize ( SerializationContext context)
Parameters:
[in]contextSame context pointer passed to BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize()
void RakNet::VariableDeltaSerializer::FreeChangedVariablesList ( ChangedVariablesList changedVariables) [protected]
void RakNet::VariableDeltaSerializer::FreeVarsAssociatedWithReceipt ( RakNetGUID  guid,
uint32_t  receiptId 
) [protected]
unsigned int RakNet::VariableDeltaSerializer::GetRemoteSystemHistoryListIndex ( RakNetGUID  guid) [protected]
RemoteSystemVariableHistory* RakNet::VariableDeltaSerializer::GetRemoteSystemVariableHistory ( RakNetGUID  guid) [protected]
unsigned int RakNet::VariableDeltaSerializer::GetVarsWrittenPerRemoteSystemListIndex ( RakNetGUID  guid) [protected]
void RakNet::VariableDeltaSerializer::OnMessageReceipt ( RakNetGUID  guid,
uint32_t  receiptId,
bool  messageArrived 
)

Call when getting ID_SND_RECEIPT_LOSS or ID_SND_RECEIPT_ACKED for a particular system Example:

uint32_t msgNumber; memcpy(&msgNumber, packet->data+1, 4); DataStructures::Multilist<ML_STACK, Replica3*> replicaListOut; replicaManager.GetReplicasCreatedByMe(replicaListOut); DataStructures::DefaultIndexType idx; for (idx=0; idx < replicaListOut.GetSize(); idx++) { ((SampleReplica*)replicaListOut[idx])->NotifyReplicaOfMessageDeliveryStatus(packet->guid,msgNumber, packet->data[0]==ID_SND_RECEIPT_ACKED); }

Parameters:
[in]guidWhich system we are sending to
[in]receiptIdEncoded in bytes 1-4 inclusive of ID_SND_RECEIPT_LOSS and ID_SND_RECEIPT_ACKED
[in]messageArrivedTrue for ID_SND_RECEIPT_ACKED, false otherwise
void RakNet::VariableDeltaSerializer::OnPreSerializeTick ( void  )

BeginIdenticalSerialize() requires knowledge of when serialization has started for an object across multiple systems This way it can setup the flag to do new comparisons against the last sent values, rather than just resending the last sent bitStream For Replica3, overload and call this from Replica3::OnUserReplicaPreSerializeTick()

void RakNet::VariableDeltaSerializer::RemoveRemoteSystemVariableHistory ( RakNetGUID  guid)

BeginUnreliableAckedSerialize() and BeginUniqueSerialize() require knowledge of when connections are added and dropped Call AddRemoteSystemVariableHistory() and RemoveRemoteSystemVariableHistory() to notify the system of these events

Parameters:
[in]_guidWhich system we are sending to
void RakNet::VariableDeltaSerializer::RemoveRemoteSystemVariableHistory ( void  ) [protected]
template<class VarType >
void RakNet::VariableDeltaSerializer::SerializeVariable ( SerializationContext context,
const VarType &  variable 
) [inline]

Call to Serialize a variable Will write to the bitSteam passed to context true, variableValue if the variable has changed or has never been written. Otherwise will write false.

Precondition:
You have called BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize()
Will also require calling OnPreSerializeTick() if using BeginIdenticalSerialize()
Note:
Be sure to call EndSerialize() after finishing all serializations
Parameters:
[in]contextSame context pointer passed to BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize()
[in]variableA variable to write to the bitStream passed to context

Definition at line 150 of file VariableDeltaSerializer.h.

References RakNet::VariableDeltaSerializer::SerializationContext::anyVariablesWritten, RakNet::VariableDeltaSerializer::ChangedVariablesList::bitField, RakNet::VariableDeltaSerializer::SerializationContext::bitStream, RakNet::VariableDeltaSerializer::ChangedVariablesList::bitWriteIndex, RakNet::VariableDeltaSerializer::SerializationContext::changedVariables, didComparisonThisTick, RakNet::VariableListDeltaTracker::IsPastEndOfList(), RakNet::VariableDeltaSerializer::SerializationContext::newSystemSend, RakNet::VariableDeltaSerializer::SerializationContext::serializationMode, UNRELIABLE_WITH_ACK_RECEIPT, RakNet::VariableDeltaSerializer::SerializationContext::variableHistory, RakNet::VariableDeltaSerializer::SerializationContext::variableHistoryIdentical, RakNet::VariableDeltaSerializer::RemoteSystemVariableHistory::variableListDeltaTracker, RakNet::BitStream::Write(), and RakNet::VariableListDeltaTracker::WriteVarToBitstream().

    {
        if (context->newSystemSend)
        {
            if (context->variableHistory->variableListDeltaTracker.IsPastEndOfList()==false)
            {
                // previously sent data to another system
                context->bitStream->Write(true);
                context->bitStream->Write(variable);
                context->anyVariablesWritten=true;
            }
            else
            {
                // never sent data to another system
                context->variableHistory->variableListDeltaTracker.WriteVarToBitstream(variable, context->bitStream);
                context->anyVariablesWritten=true;
            }
        }
        else if (context->serializationMode==UNRELIABLE_WITH_ACK_RECEIPT)
        {
            context->anyVariablesWritten|=
            context->variableHistory->variableListDeltaTracker.WriteVarToBitstream(variable, context->bitStream, context->changedVariables->bitField, context->changedVariables->bitWriteIndex++);
        }
        else
        {
            if (context->variableHistoryIdentical)
            {
                // Identical serialization to a number of systems
                if (didComparisonThisTick==false)
                    context->anyVariablesWritten|=
                    context->variableHistory->variableListDeltaTracker.WriteVarToBitstream(variable, context->bitStream);
                // Else bitstream is written to at the end
            }
            else
            {
                // Per-system serialization
                context->anyVariablesWritten|=
                    context->variableHistory->variableListDeltaTracker.WriteVarToBitstream(variable, context->bitStream);
            }
        }
    }
RemoteSystemVariableHistory* RakNet::VariableDeltaSerializer::StartVariableHistoryWrite ( RakNetGUID  guid) [protected]
void RakNet::VariableDeltaSerializer::StoreChangedVariablesList ( RemoteSystemVariableHistory variableHistory,
ChangedVariablesList changedVariables,
uint32_t  sendReceipt 
) [protected]
static int RakNet::VariableDeltaSerializer::UpdatedVariablesListPtrComp ( const uint32_t key,
ChangedVariablesList *const &  data 
) [static, protected]

Member Data Documentation

Definition at line 236 of file VariableDeltaSerializer.h.

Referenced by SerializeVariable().

Definition at line 237 of file VariableDeltaSerializer.h.

A list of RemoteSystemVariableHistory indexed by guid, one per connection that we serialize to List is added to when SerializeConstruction is called, and removed from when SerializeDestruction is called, or when a given connection is dropped

Definition at line 231 of file VariableDeltaSerializer.h.

Definition at line 234 of file VariableDeltaSerializer.h.


The documentation for this class was generated from the following file:

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net