Go to the documentation of this file.00001 #include "VariableDeltaSerializer.h"
00002
00003 using namespace RakNet;
00004
00005 VariableDeltaSerializer::VariableDeltaSerializer() {didComparisonThisTick=false;}
00006 VariableDeltaSerializer::~VariableDeltaSerializer() {RemoveRemoteSystemVariableHistory();}
00007
00008 VariableDeltaSerializer::SerializationContext::SerializationContext() {variableHistoryIdentical=0; variableHistoryUnique=0;}
00009 VariableDeltaSerializer::SerializationContext::~SerializationContext() {}
00010
00011 void VariableDeltaSerializer::OnMessageReceipt(RakNetGUID guid, uint32_t receiptId, bool messageArrived)
00012 {
00013
00014 if (messageArrived)
00015 FreeVarsAssociatedWithReceipt(guid, receiptId);
00016 else
00017 DirtyAndFreeVarsAssociatedWithReceipt(guid, receiptId);
00018
00019 }
00020
00021 void VariableDeltaSerializer::BeginUnreliableAckedSerialize(SerializationContext *context, RakNetGUID _guid, BitStream *_bitStream, uint32_t _sendReceipt)
00022 {
00023 RakAssert(_guid!=UNASSIGNED_RAKNET_GUID);
00024 context->anyVariablesWritten=false;
00025 context->guid=_guid;
00026 context->bitStream=_bitStream;
00027 if (context->variableHistoryUnique==0)
00028 context->variableHistoryUnique=StartVariableHistoryWrite(_guid);
00029 context->variableHistory=context->variableHistoryUnique;
00030 context->sendReceipt=_sendReceipt;
00031 context->changedVariables = AllocChangedVariablesList();
00032 context->newSystemSend=false;
00033 context->serializationMode=UNRELIABLE_WITH_ACK_RECEIPT;
00034 }
00035
00036 void VariableDeltaSerializer::BeginUniqueSerialize(SerializationContext *context, RakNetGUID _guid, BitStream *_bitStream)
00037 {
00038 RakAssert(_guid!=UNASSIGNED_RAKNET_GUID);
00039 context->anyVariablesWritten=false;
00040 context->guid=_guid;
00041 context->bitStream=_bitStream;
00042 if (context->variableHistoryUnique==0)
00043 context->variableHistoryUnique=StartVariableHistoryWrite(_guid);
00044 context->variableHistory=context->variableHistoryUnique;
00045 context->newSystemSend=false;
00046
00047 context->serializationMode=RELIABLE;
00048 }
00049
00050
00051 void VariableDeltaSerializer::BeginIdenticalSerialize(SerializationContext *context, bool _isFirstSendToRemoteSystem, BitStream *_bitStream)
00052 {
00053 context->anyVariablesWritten=false;
00054 context->guid=UNASSIGNED_RAKNET_GUID;
00055 context->bitStream=_bitStream;
00056 context->serializationMode=RELIABLE;
00057 if (context->variableHistoryIdentical==0)
00058 context->variableHistoryIdentical=StartVariableHistoryWrite(UNASSIGNED_RAKNET_GUID);
00059 context->variableHistory=context->variableHistoryIdentical;
00060 context->newSystemSend=_isFirstSendToRemoteSystem;
00061 }
00062
00063 void VariableDeltaSerializer::EndSerialize(SerializationContext *context)
00064 {
00065 if (context->serializationMode==UNRELIABLE_WITH_ACK_RECEIPT)
00066 {
00067 if (context->anyVariablesWritten==false)
00068 {
00069 context->bitStream->Reset();
00070 FreeChangedVariablesList(context->changedVariables);
00071 return;
00072 }
00073
00074 StoreChangedVariablesList(context->variableHistory, context->changedVariables, context->sendReceipt);
00075 }
00076 else
00077 {
00078 if (context->variableHistoryIdentical)
00079 {
00080 if (didComparisonThisTick==false)
00081 {
00082 didComparisonThisTick=true;
00083 identicalSerializationBs.Reset();
00084
00085 if (context->anyVariablesWritten==false)
00086 {
00087 context->bitStream->Reset();
00088 return;
00089 }
00090
00091 identicalSerializationBs.Write(context->bitStream);
00092 context->bitStream->ResetReadPointer();
00093 }
00094 else
00095 {
00096 context->bitStream->Write(&identicalSerializationBs);
00097 identicalSerializationBs.ResetReadPointer();
00098 }
00099 }
00100 else if (context->anyVariablesWritten==false)
00101 {
00102 context->bitStream->Reset();
00103 return;
00104 }
00105 }
00106 }
00107
00108 void VariableDeltaSerializer::BeginDeserialize(DeserializationContext *context, BitStream *_bitStream)
00109 {
00110 context->bitStream=_bitStream;
00111 }
00112
00113 void VariableDeltaSerializer::EndDeserialize(DeserializationContext *context)
00114 {
00115 (void) context;
00116 }
00117
00118 void VariableDeltaSerializer::AddRemoteSystemVariableHistory(RakNetGUID guid)
00119 {
00120 (void) guid;
00121 }
00122
00123 void VariableDeltaSerializer::RemoveRemoteSystemVariableHistory(RakNetGUID guid)
00124 {
00125 unsigned int idx,idx2;
00126 idx = GetVarsWrittenPerRemoteSystemListIndex(guid);
00127 if (idx==(unsigned int)-1)
00128 return;
00129
00130 if (remoteSystemVariableHistoryList[idx]->guid==guid)
00131 {
00132
00133 for (idx2=0; idx2 < remoteSystemVariableHistoryList[idx]->updatedVariablesHistory.Size(); idx2++)
00134 {
00135 FreeChangedVariablesList(remoteSystemVariableHistoryList[idx]->updatedVariablesHistory[idx2]);
00136 }
00137
00138 delete remoteSystemVariableHistoryList[idx];
00139 remoteSystemVariableHistoryList.RemoveAtIndexFast(idx);
00140 return;
00141 }
00142 }
00143
00144 int VariableDeltaSerializer::UpdatedVariablesListPtrComp( const uint32_t &key, ChangedVariablesList* const &data )
00145 {
00146 if (key<data->sendReceipt)
00147 return -1;
00148 if (key==data->sendReceipt)
00149 return 0;
00150 return 1;
00151 }
00152
00153 void VariableDeltaSerializer::FreeVarsAssociatedWithReceipt(RakNetGUID guid, uint32_t receiptId)
00154 {
00155 unsigned int idx, idx2;
00156 idx = GetVarsWrittenPerRemoteSystemListIndex(guid);
00157 if (idx==(unsigned int)-1)
00158 return;
00159
00160 RemoteSystemVariableHistory* vprs = remoteSystemVariableHistoryList[idx];
00161 bool objectExists;
00162 idx2=vprs->updatedVariablesHistory.GetIndexFromKey(receiptId,&objectExists);
00163 if (objectExists)
00164 {
00165
00166 FreeChangedVariablesList(vprs->updatedVariablesHistory[idx2]);
00167 vprs->updatedVariablesHistory.RemoveAtIndex(idx2);
00168 }
00169 }
00170
00171 void VariableDeltaSerializer::DirtyAndFreeVarsAssociatedWithReceipt(RakNetGUID guid, uint32_t receiptId)
00172 {
00173 unsigned int idx, idx2;
00174 idx = GetVarsWrittenPerRemoteSystemListIndex(guid);
00175 if (idx==(unsigned int)-1)
00176 return;
00177
00178 RemoteSystemVariableHistory* vprs = remoteSystemVariableHistoryList[idx];
00179 bool objectExists;
00180 idx2=vprs->updatedVariablesHistory.GetIndexFromKey(receiptId,&objectExists);
00181 if (objectExists)
00182 {
00183
00184 vprs->variableListDeltaTracker.FlagDirtyFromBitArray(vprs->updatedVariablesHistory[idx2]->bitField);
00185
00186
00187 FreeChangedVariablesList(vprs->updatedVariablesHistory[idx2]);
00188 vprs->updatedVariablesHistory.RemoveAtIndex(idx2);
00189 }
00190 }
00191 unsigned int VariableDeltaSerializer::GetVarsWrittenPerRemoteSystemListIndex(RakNetGUID guid)
00192 {
00193 unsigned int idx;
00194 for (idx=0; idx < remoteSystemVariableHistoryList.Size(); idx++)
00195 {
00196 if (remoteSystemVariableHistoryList[idx]->guid==guid)
00197 return idx;
00198 }
00199 return (unsigned int) -1;
00200 }
00201 void VariableDeltaSerializer::RemoveRemoteSystemVariableHistory(void)
00202 {
00203 unsigned int idx,idx2;
00204 for (idx=0; idx < remoteSystemVariableHistoryList.Size(); idx++)
00205 {
00206 for (idx2=0; idx2 < remoteSystemVariableHistoryList[idx]->updatedVariablesHistory.Size(); idx2++)
00207 {
00208 FreeChangedVariablesList(remoteSystemVariableHistoryList[idx]->updatedVariablesHistory[idx2]);
00209 }
00210
00211 delete remoteSystemVariableHistoryList[idx];
00212 }
00213 remoteSystemVariableHistoryList.Clear(false,__FILE__,__LINE__);
00214 }
00215
00216 VariableDeltaSerializer::RemoteSystemVariableHistory* VariableDeltaSerializer::GetRemoteSystemVariableHistory(RakNetGUID guid)
00217 {
00218 unsigned int rshli = GetRemoteSystemHistoryListIndex(guid);
00219 return remoteSystemVariableHistoryList[rshli];
00220 }
00221
00222 VariableDeltaSerializer::ChangedVariablesList *VariableDeltaSerializer::AllocChangedVariablesList(void)
00223 {
00224 VariableDeltaSerializer::ChangedVariablesList *p = updatedVariablesMemoryPool.Allocate(__FILE__,__LINE__);
00225 p->bitWriteIndex=0;
00226 p->bitField[0]=0;
00227 return p;
00228 }
00229 void VariableDeltaSerializer::FreeChangedVariablesList(ChangedVariablesList *changedVariables)
00230 {
00231 updatedVariablesMemoryPool.Release(changedVariables, __FILE__,__LINE__);
00232 }
00233 void VariableDeltaSerializer::StoreChangedVariablesList(RemoteSystemVariableHistory *variableHistory, ChangedVariablesList *changedVariables, uint32_t sendReceipt)
00234 {
00235 changedVariables->sendReceipt=sendReceipt;
00236 variableHistory->updatedVariablesHistory.Insert(changedVariables->sendReceipt,changedVariables,true,__FILE__,__LINE__);
00237 }
00238
00239 VariableDeltaSerializer::RemoteSystemVariableHistory *VariableDeltaSerializer::StartVariableHistoryWrite(RakNetGUID guid)
00240 {
00241 RemoteSystemVariableHistory *variableHistory;
00242
00243 unsigned int rshli = GetRemoteSystemHistoryListIndex(guid);
00244 if (rshli==(unsigned int) -1)
00245 {
00246 variableHistory = new RemoteSystemVariableHistory;
00247 variableHistory->guid=guid;
00248 remoteSystemVariableHistoryList.Push(variableHistory,__FILE__,__LINE__);
00249 }
00250 else
00251 {
00252 variableHistory=remoteSystemVariableHistoryList[rshli];
00253 }
00254
00255 variableHistory->variableListDeltaTracker.StartWrite();
00256 return variableHistory;
00257 }
00258 unsigned int VariableDeltaSerializer::GetRemoteSystemHistoryListIndex(RakNetGUID guid)
00259 {
00260
00261 unsigned int idx;
00262 for (idx=0; idx < remoteSystemVariableHistoryList.Size(); idx++)
00263 {
00264 if (remoteSystemVariableHistoryList[idx]->guid==guid)
00265 {
00266 return idx;
00267 }
00268 }
00269 return (unsigned int) -1;
00270 }
00271
00272 void VariableDeltaSerializer::OnPreSerializeTick(void)
00273 {
00274 didComparisonThisTick=false;
00275 }