SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VariableListDeltaTracker.h
Go to the documentation of this file.
1 /*
2 * Original work: 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 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
16 #include "NativeTypes.h"
17 #include "DS_List.h"
18 #include "memoryoverride.h"
19 #include "BitStream.h"
20
21 #ifndef __VARIABLE_LIST_DELTA_TRACKER
22 #define __VARIABLE_LIST_DELTA_TRACKER
23
24 namespace SLNet
25 {
28 class VariableListDeltaTracker
29 {
30 public:
31 VariableListDeltaTracker();
32 ~VariableListDeltaTracker();
33
34 // Call before using a series of WriteVar
35 void StartWrite(void);
36
37 bool IsPastEndOfList(void) const {return nextWriteIndex>=variableList.Size();}
38
42 template <class VarType>
43 bool WriteVar(const VarType &varData)
44 {
45 SLNet::BitStream temp;
46 temp.Write(varData);
47 if (nextWriteIndex>=variableList.Size())
48 {
49 variableList.Push(VariableLastValueNode(temp.GetData(),temp.GetNumberOfBytesUsed()),_FILE_AND_LINE_);
50 nextWriteIndex++;
51 return true; // Different because it's new
52 }
53
54 if (temp.GetNumberOfBytesUsed()!=variableList[nextWriteIndex].byteLength)
55 {
56 variableList[nextWriteIndex].lastData=(char*) rakRealloc_Ex(variableList[nextWriteIndex].lastData, temp.GetNumberOfBytesUsed(),_FILE_AND_LINE_);
57 variableList[nextWriteIndex].byteLength=temp.GetNumberOfBytesUsed();
58 memcpy(variableList[nextWriteIndex].lastData,temp.GetData(),temp.GetNumberOfBytesUsed());
59 nextWriteIndex++;
60 variableList[nextWriteIndex].isDirty=false;
61 return true; // Different because the serialized size is different
62 }
63 if (variableList[nextWriteIndex].isDirty==false && memcmp(temp.GetData(),variableList[nextWriteIndex].lastData, variableList[nextWriteIndex].byteLength)==0)
64 {
65 nextWriteIndex++;
66 return false; // Same because not dirty and memcmp is the same
67 }
68
69 variableList[nextWriteIndex].isDirty=false;
70 memcpy(variableList[nextWriteIndex].lastData,temp.GetData(),temp.GetNumberOfBytesUsed());
71 nextWriteIndex++;
72 return true; // Different because dirty or memcmp was different
73 }
75 template <class VarType>
76 bool WriteVarToBitstream(const VarType &varData, SLNet::BitStream *bitStream)
77 {
78 bool wasDifferent = WriteVar(varData);
79 bitStream->Write(wasDifferent);
80 if (wasDifferent)
81 {
82 bitStream->Write(varData);
83 return true;
84 }
85 return false;
86 }
88 template <class VarType>
89 bool WriteVarToBitstream(const VarType &varData, SLNet::BitStream *bitStream, unsigned char *bArray, unsigned short writeOffset)
90 {
91 if (WriteVarToBitstream(varData,bitStream)==true)
92 {
93 BitSize_t numberOfBitsMod8 = writeOffset & 7;
94
95 if ( numberOfBitsMod8 == 0 )
96 bArray[ writeOffset >> 3 ] = 0x80;
97 else
98 bArray[ writeOffset >> 3 ] |= 0x80 >> ( numberOfBitsMod8 ); // Set the bit to 1
99
100 return true;
101 }
102 else
103 {
104 if ( ( writeOffset & 7 ) == 0 )
105 bArray[ writeOffset >> 3 ] = 0;
106
107 return false;
108 }
109 }
110
112 template <class VarType>
113 static bool ReadVarFromBitstream(VarType &varData, SLNet::BitStream *bitStream)
114 {
115 bool wasWritten;
116 if (bitStream->Read(wasWritten)==false)
117 return false;
118 if (wasWritten)
119 {
120 if (bitStream->Read(varData)==false)
121 return false;
122 }
123 return wasWritten;
124 }
125
128 void FlagDirtyFromBitArray(unsigned char *bArray);
129
131 struct VariableLastValueNode
132 {
133 VariableLastValueNode();
134 VariableLastValueNode(const unsigned char *data, int _byteLength);
135 ~VariableLastValueNode();
136 char *lastData;
137 unsigned int byteLength;
138 bool isDirty;
139 };
140
141 protected:
143 DataStructures::List<VariableLastValueNode> variableList;
145 unsigned int nextWriteIndex;
146 };
147
148
149 }
150
151 #endif