VariableDeltaSerializer.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "VariableDeltaSerializer.h"
  11. using namespace RakNet;
  12. VariableDeltaSerializer::VariableDeltaSerializer() {didComparisonThisTick=false;}
  13. VariableDeltaSerializer::~VariableDeltaSerializer() {RemoveRemoteSystemVariableHistory();}
  14. VariableDeltaSerializer::SerializationContext::SerializationContext() {variableHistoryIdentical=0; variableHistoryUnique=0;}
  15. VariableDeltaSerializer::SerializationContext::~SerializationContext() {}
  16. void VariableDeltaSerializer::OnMessageReceipt(RakNetGUID guid, uint32_t receiptId, bool messageArrived)
  17. {
  18. // Module?
  19. if (messageArrived)
  20. FreeVarsAssociatedWithReceipt(guid, receiptId);
  21. else
  22. DirtyAndFreeVarsAssociatedWithReceipt(guid, receiptId);
  23. }
  24. void VariableDeltaSerializer::BeginUnreliableAckedSerialize(SerializationContext *context, RakNetGUID _guid, BitStream *_bitStream, uint32_t _sendReceipt)
  25. {
  26. RakAssert(_guid!=UNASSIGNED_RAKNET_GUID);
  27. context->anyVariablesWritten=false;
  28. context->guid=_guid;
  29. context->bitStream=_bitStream;
  30. if (context->variableHistoryUnique==0)
  31. context->variableHistoryUnique=StartVariableHistoryWrite(_guid);
  32. context->variableHistory=context->variableHistoryUnique;
  33. context->sendReceipt=_sendReceipt;
  34. context->changedVariables = AllocChangedVariablesList();
  35. context->newSystemSend=false;
  36. context->serializationMode=UNRELIABLE_WITH_ACK_RECEIPT;
  37. }
  38. void VariableDeltaSerializer::BeginUniqueSerialize(SerializationContext *context, RakNetGUID _guid, BitStream *_bitStream)
  39. {
  40. RakAssert(_guid!=UNASSIGNED_RAKNET_GUID);
  41. context->anyVariablesWritten=false;
  42. context->guid=_guid;
  43. context->bitStream=_bitStream;
  44. if (context->variableHistoryUnique==0)
  45. context->variableHistoryUnique=StartVariableHistoryWrite(_guid);
  46. context->variableHistory=context->variableHistoryUnique;
  47. context->newSystemSend=false;
  48. context->serializationMode=RELIABLE;
  49. }
  50. void VariableDeltaSerializer::BeginIdenticalSerialize(SerializationContext *context, bool _isFirstSendToRemoteSystem, BitStream *_bitStream)
  51. {
  52. context->anyVariablesWritten=false;
  53. context->guid=UNASSIGNED_RAKNET_GUID;
  54. context->bitStream=_bitStream;
  55. context->serializationMode=RELIABLE;
  56. if (context->variableHistoryIdentical==0)
  57. context->variableHistoryIdentical=StartVariableHistoryWrite(UNASSIGNED_RAKNET_GUID);
  58. context->variableHistory=context->variableHistoryIdentical;
  59. context->newSystemSend=_isFirstSendToRemoteSystem;
  60. }
  61. void VariableDeltaSerializer::EndSerialize(SerializationContext *context)
  62. {
  63. if (context->serializationMode==UNRELIABLE_WITH_ACK_RECEIPT)
  64. {
  65. if (context->anyVariablesWritten==false)
  66. {
  67. context->bitStream->Reset();
  68. FreeChangedVariablesList(context->changedVariables);
  69. return;
  70. }
  71. StoreChangedVariablesList(context->variableHistory, context->changedVariables, context->sendReceipt);
  72. }
  73. else
  74. {
  75. if (context->variableHistoryIdentical)
  76. {
  77. if (didComparisonThisTick==false)
  78. {
  79. didComparisonThisTick=true;
  80. identicalSerializationBs.Reset();
  81. if (context->anyVariablesWritten==false)
  82. {
  83. context->bitStream->Reset();
  84. return;
  85. }
  86. identicalSerializationBs.Write(context->bitStream);
  87. context->bitStream->ResetReadPointer();
  88. }
  89. else
  90. {
  91. context->bitStream->Write(&identicalSerializationBs);
  92. identicalSerializationBs.ResetReadPointer();
  93. }
  94. }
  95. else if (context->anyVariablesWritten==false)
  96. {
  97. context->bitStream->Reset();
  98. return;
  99. }
  100. }
  101. }
  102. void VariableDeltaSerializer::BeginDeserialize(DeserializationContext *context, BitStream *_bitStream)
  103. {
  104. context->bitStream=_bitStream;
  105. }
  106. void VariableDeltaSerializer::EndDeserialize(DeserializationContext *context)
  107. {
  108. (void) context;
  109. }
  110. void VariableDeltaSerializer::AddRemoteSystemVariableHistory(RakNetGUID guid)
  111. {
  112. (void) guid;
  113. }
  114. void VariableDeltaSerializer::RemoveRemoteSystemVariableHistory(RakNetGUID guid)
  115. {
  116. unsigned int idx,idx2;
  117. idx = GetVarsWrittenPerRemoteSystemListIndex(guid);
  118. if (idx==(unsigned int)-1)
  119. return;
  120. if (remoteSystemVariableHistoryList[idx]->guid==guid)
  121. {
  122. // Memory pool doesn't call destructor
  123. for (idx2=0; idx2 < remoteSystemVariableHistoryList[idx]->updatedVariablesHistory.Size(); idx2++)
  124. {
  125. FreeChangedVariablesList(remoteSystemVariableHistoryList[idx]->updatedVariablesHistory[idx2]);
  126. }
  127. delete remoteSystemVariableHistoryList[idx];
  128. remoteSystemVariableHistoryList.RemoveAtIndexFast(idx);
  129. return;
  130. }
  131. }
  132. int RakNet::VariableDeltaSerializer::UpdatedVariablesListPtrComp( const uint32_t &key, ChangedVariablesList* const &data )
  133. {
  134. if (key<data->sendReceipt)
  135. return -1;
  136. if (key==data->sendReceipt)
  137. return 0;
  138. return 1;
  139. }
  140. void VariableDeltaSerializer::FreeVarsAssociatedWithReceipt(RakNetGUID guid, uint32_t receiptId)
  141. {
  142. unsigned int idx, idx2;
  143. idx = GetVarsWrittenPerRemoteSystemListIndex(guid);
  144. if (idx==(unsigned int)-1)
  145. return;
  146. RemoteSystemVariableHistory* vprs = remoteSystemVariableHistoryList[idx];
  147. bool objectExists;
  148. idx2=vprs->updatedVariablesHistory.GetIndexFromKey(receiptId,&objectExists);
  149. if (objectExists)
  150. {
  151. // Free this history node
  152. FreeChangedVariablesList(vprs->updatedVariablesHistory[idx2]);
  153. vprs->updatedVariablesHistory.RemoveAtIndex(idx2);
  154. }
  155. }
  156. void VariableDeltaSerializer::DirtyAndFreeVarsAssociatedWithReceipt(RakNetGUID guid, uint32_t receiptId)
  157. {
  158. unsigned int idx, idx2;
  159. idx = GetVarsWrittenPerRemoteSystemListIndex(guid);
  160. if (idx==(unsigned int)-1)
  161. return;
  162. RemoteSystemVariableHistory* vprs = remoteSystemVariableHistoryList[idx];
  163. bool objectExists;
  164. idx2=vprs->updatedVariablesHistory.GetIndexFromKey(receiptId,&objectExists);
  165. if (objectExists)
  166. {
  167. // 'Dirty' all variables sent this update, meaning they will be resent the next time Serialize() is called
  168. vprs->variableListDeltaTracker.FlagDirtyFromBitArray(vprs->updatedVariablesHistory[idx2]->bitField);
  169. // Free this history node
  170. FreeChangedVariablesList(vprs->updatedVariablesHistory[idx2]);
  171. vprs->updatedVariablesHistory.RemoveAtIndex(idx2);
  172. }
  173. }
  174. unsigned int VariableDeltaSerializer::GetVarsWrittenPerRemoteSystemListIndex(RakNetGUID guid)
  175. {
  176. unsigned int idx;
  177. for (idx=0; idx < remoteSystemVariableHistoryList.Size(); idx++)
  178. {
  179. if (remoteSystemVariableHistoryList[idx]->guid==guid)
  180. return idx;
  181. }
  182. return (unsigned int) -1;
  183. }
  184. void VariableDeltaSerializer::RemoveRemoteSystemVariableHistory(void)
  185. {
  186. unsigned int idx,idx2;
  187. for (idx=0; idx < remoteSystemVariableHistoryList.Size(); idx++)
  188. {
  189. for (idx2=0; idx2 < remoteSystemVariableHistoryList[idx]->updatedVariablesHistory.Size(); idx2++)
  190. {
  191. FreeChangedVariablesList(remoteSystemVariableHistoryList[idx]->updatedVariablesHistory[idx2]);
  192. }
  193. delete remoteSystemVariableHistoryList[idx];
  194. }
  195. remoteSystemVariableHistoryList.Clear(false,_FILE_AND_LINE_);
  196. }
  197. VariableDeltaSerializer::RemoteSystemVariableHistory* VariableDeltaSerializer::GetRemoteSystemVariableHistory(RakNetGUID guid)
  198. {
  199. unsigned int rshli = GetRemoteSystemHistoryListIndex(guid);
  200. return remoteSystemVariableHistoryList[rshli];
  201. }
  202. VariableDeltaSerializer::ChangedVariablesList *VariableDeltaSerializer::AllocChangedVariablesList(void)
  203. {
  204. VariableDeltaSerializer::ChangedVariablesList *p = updatedVariablesMemoryPool.Allocate(_FILE_AND_LINE_);
  205. p->bitWriteIndex=0;
  206. p->bitField[0]=0;
  207. return p;
  208. }
  209. void VariableDeltaSerializer::FreeChangedVariablesList(ChangedVariablesList *changedVariables)
  210. {
  211. updatedVariablesMemoryPool.Release(changedVariables, _FILE_AND_LINE_);
  212. }
  213. void VariableDeltaSerializer::StoreChangedVariablesList(RemoteSystemVariableHistory *variableHistory, ChangedVariablesList *changedVariables, uint32_t sendReceipt)
  214. {
  215. changedVariables->sendReceipt=sendReceipt;
  216. variableHistory->updatedVariablesHistory.Insert(changedVariables->sendReceipt,changedVariables,true,_FILE_AND_LINE_);
  217. }
  218. VariableDeltaSerializer::RemoteSystemVariableHistory *VariableDeltaSerializer::StartVariableHistoryWrite(RakNetGUID guid)
  219. {
  220. RemoteSystemVariableHistory *variableHistory;
  221. unsigned int rshli = GetRemoteSystemHistoryListIndex(guid);
  222. if (rshli==(unsigned int) -1)
  223. {
  224. variableHistory = new RemoteSystemVariableHistory;
  225. variableHistory->guid=guid;
  226. remoteSystemVariableHistoryList.Push(variableHistory,_FILE_AND_LINE_);
  227. }
  228. else
  229. {
  230. variableHistory=remoteSystemVariableHistoryList[rshli];
  231. }
  232. variableHistory->variableListDeltaTracker.StartWrite();
  233. return variableHistory;
  234. }
  235. unsigned int VariableDeltaSerializer::GetRemoteSystemHistoryListIndex(RakNetGUID guid)
  236. {
  237. // Find the variable tracker for the target system
  238. unsigned int idx;
  239. for (idx=0; idx < remoteSystemVariableHistoryList.Size(); idx++)
  240. {
  241. if (remoteSystemVariableHistoryList[idx]->guid==guid)
  242. {
  243. return idx;
  244. }
  245. }
  246. return (unsigned int) -1;
  247. }
  248. void VariableDeltaSerializer::OnPreSerializeTick(void)
  249. {
  250. didComparisonThisTick=false;
  251. }
粤ICP备19079148号