• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

VariableListDeltaTracker.h

Go to the documentation of this file.
00001 #include "NativeTypes.h"
00002 #include "DS_List.h"
00003 #include "RakMemoryOverride.h"
00004 #include "BitStream.h"
00005 
00006 namespace RakNet
00007 {
00010 class VariableListDeltaTracker
00011 {
00012 public:
00013         VariableListDeltaTracker();
00014         ~VariableListDeltaTracker();
00015 
00016         // Call before using a series of WriteVar
00017         void StartWrite(void);
00018 
00019         bool IsPastEndOfList(void) const {return nextWriteIndex>=variableList.Size();}
00020 
00024         template <class VarType>
00025         bool WriteVar(const VarType &varData)
00026         {
00027                 RakNet::BitStream temp;
00028                 temp.Write(varData);
00029                 if (nextWriteIndex>=variableList.Size())
00030                 {
00031                         variableList.Push(VariableLastValueNode(temp.GetData(),temp.GetNumberOfBytesUsed()),__FILE__,__LINE__);
00032                         nextWriteIndex++;
00033                         return true; // Different because it's new
00034                 }
00035 
00036                 if (temp.GetNumberOfBytesUsed()!=variableList[nextWriteIndex].byteLength)
00037                 {
00038                         variableList[nextWriteIndex].lastData=(char*) rakRealloc_Ex(variableList[nextWriteIndex].lastData, temp.GetNumberOfBytesUsed(),__FILE__,__LINE__);
00039                         variableList[nextWriteIndex].byteLength=temp.GetNumberOfBytesUsed();
00040                         memcpy(variableList[nextWriteIndex].lastData,temp.GetData(),temp.GetNumberOfBytesUsed());
00041                         nextWriteIndex++;
00042                         variableList[nextWriteIndex].isDirty=false;
00043                         return true; // Different because the serialized size is different
00044                 }
00045                 if (variableList[nextWriteIndex].isDirty==false && memcmp(temp.GetData(),variableList[nextWriteIndex].lastData, variableList[nextWriteIndex].byteLength)==0)
00046                 {
00047                         nextWriteIndex++;
00048                         return false; // Same because not dirty and memcmp is the same
00049                 }
00050 
00051                 variableList[nextWriteIndex].isDirty=false;
00052                 memcpy(variableList[nextWriteIndex].lastData,temp.GetData(),temp.GetNumberOfBytesUsed());
00053                 nextWriteIndex++;
00054                 return true; // Different because dirty or memcmp was different
00055         }
00057         template <class VarType>
00058         bool WriteVarToBitstream(const VarType &varData, RakNet::BitStream *bitStream)
00059         {
00060                 bool wasDifferent = WriteVar(varData);
00061                 bitStream->Write(wasDifferent);
00062                 if (wasDifferent)
00063                 {
00064                         bitStream->Write(varData);
00065                         return true;
00066                 }
00067                 return false;
00068         }
00070         template <class VarType>
00071         bool WriteVarToBitstream(const VarType &varData, RakNet::BitStream *bitStream, unsigned char *bArray, unsigned short writeOffset)
00072         {
00073                 if (WriteVarToBitstream(varData,bitStream)==true)
00074                 {
00075                         BitSize_t numberOfBitsMod8 = writeOffset & 7;
00076 
00077                         if ( numberOfBitsMod8 == 0 )
00078                                 bArray[ writeOffset >> 3 ] = 0x80;
00079                         else
00080                                 bArray[ writeOffset >> 3 ] |= 0x80 >> ( numberOfBitsMod8 ); // Set the bit to 1
00081 
00082                         return true;
00083                 }
00084                 else
00085                 {
00086                         if ( ( writeOffset & 7 ) == 0 )
00087                                 bArray[ writeOffset >> 3 ] = 0;
00088 
00089                         return false;
00090                 }
00091         }
00092 
00094         template <class VarType>
00095         static bool ReadVarFromBitstream(const VarType &varData, RakNet::BitStream *bitStream)
00096         {
00097                 bool wasWritten;
00098                 if (bitStream->Read(wasWritten)==false)
00099                         return false;
00100                 if (wasWritten)
00101                 {
00102                         if (bitStream->Read(varData)==false)
00103                                 return false;
00104                 }
00105                 return wasWritten;
00106         }
00107 
00110         void FlagDirtyFromBitArray(unsigned char *bArray);
00111 
00113         struct VariableLastValueNode
00114         {
00115                 VariableLastValueNode();
00116                 VariableLastValueNode(const unsigned char *data, int _byteLength);
00117                 ~VariableLastValueNode();
00118                 char *lastData;
00119                 int byteLength;
00120                 bool isDirty;
00121         };
00122 
00123 protected:
00125         DataStructures::List<VariableLastValueNode> variableList;
00127         unsigned int nextWriteIndex;
00128 };
00129 
00130 
00131 }

Generated on Thu Sep 30 2010 01:27:29 for RakNet by  doxygen 1.7.1