[230] | 1 | // |
---|
| 2 | // C++ Implementation: synchronisable |
---|
| 3 | // |
---|
[285] | 4 | // Description: |
---|
[230] | 5 | // |
---|
| 6 | // |
---|
[247] | 7 | // Author: Dumeni, Oliver Scheuss, (C) 2007 |
---|
[230] | 8 | // |
---|
| 9 | // Copyright: See COPYING file that comes with this distribution |
---|
| 10 | // |
---|
[238] | 11 | |
---|
[285] | 12 | #include "Synchronisable.h" |
---|
[230] | 13 | |
---|
| 14 | namespace network { |
---|
| 15 | |
---|
[247] | 16 | /** |
---|
| 17 | * Constructor: |
---|
| 18 | * calls registarAllVariables, that has to be implemented by the inheriting classID |
---|
| 19 | */ |
---|
[230] | 20 | Synchronisable::Synchronisable() |
---|
| 21 | { |
---|
[247] | 22 | datasize=0; |
---|
[237] | 23 | registerAllVariables(); |
---|
[230] | 24 | } |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | Synchronisable::~Synchronisable() |
---|
| 28 | { |
---|
[285] | 29 | |
---|
[230] | 30 | } |
---|
| 31 | |
---|
[247] | 32 | /** |
---|
| 33 | * This function is used to register a variable to be synchronized |
---|
| 34 | * also counts the total datasize needed to save the variables |
---|
| 35 | * @param var pointer to the variable |
---|
| 36 | * @param size size of the datatype the variable consists of |
---|
| 37 | */ |
---|
[237] | 38 | void Synchronisable::registerVar(const void *var, int size){ |
---|
[247] | 39 | // create temporary synch.Var struct |
---|
[237] | 40 | synchronisableVariable temp={size, var}; |
---|
[247] | 41 | // increase datasize |
---|
| 42 | datasize+=sizeof(int)+size; |
---|
| 43 | // push temp to syncList (at the bottom) |
---|
[237] | 44 | syncList.push_back(temp); |
---|
| 45 | } |
---|
[247] | 46 | |
---|
| 47 | /** |
---|
| 48 | * note: only use this function for debug use, because it's inefficient (in order to produce a gamestate, you have to copy the whole data again to another memory location after this process) |
---|
| 49 | * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct |
---|
| 50 | * structure of the bitstream: |
---|
| 51 | * (var1_size,var1,var2_size,var2,...) |
---|
| 52 | * varx_size: size = sizeof(int) |
---|
| 53 | * varx: size = varx_size |
---|
| 54 | * @return data containing all variables and their sizes |
---|
| 55 | */ |
---|
[237] | 56 | syncData Synchronisable::getData(){ |
---|
| 57 | std::list<synchronisableVariable>::iterator i; |
---|
| 58 | int totalsize=0; |
---|
| 59 | //figure out size of data to be allocated |
---|
| 60 | for(i=syncList.begin(); i!=syncList.end(); i++){ |
---|
| 61 | // increase size (size of variable and size of size of variable ;) |
---|
| 62 | totalsize+=sizeof(int)+i->size; |
---|
| 63 | } |
---|
| 64 | syncData retVal; |
---|
| 65 | retVal.objectID=this->objectID; |
---|
| 66 | retVal.classID=this->classID; |
---|
| 67 | retVal.length=totalsize; |
---|
| 68 | // allocate memory |
---|
| 69 | retVal.data = (unsigned char *)malloc(totalsize); |
---|
| 70 | // copy to location |
---|
[238] | 71 | //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP |
---|
| 72 | int n=0; |
---|
| 73 | for(i=syncList.begin(); n<totalsize && i!=syncList.end(); i++){ |
---|
[285] | 74 | //CHANGED: i->size TO (const void*)(&(i->size)) memcpy WANTS A CONST VOID* SO CONVERT INT TO CONST VOID* |
---|
[245] | 75 | std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int)); |
---|
[237] | 76 | n+=sizeof(int); |
---|
[238] | 77 | //CHANGED: i->var TO (const void*)(&(i->var)) SINCE var IS A POINTER, NO & BEFORE i |
---|
| 78 | std::memcpy(retVal.data+n, (const void*)(i->var), i->size); |
---|
[237] | 79 | n+=i->size; |
---|
| 80 | } |
---|
| 81 | return retVal; |
---|
| 82 | } |
---|
[247] | 83 | /** |
---|
| 84 | * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct |
---|
| 85 | * Difference to the above function: |
---|
| 86 | * takes a pointer to already allocated memory (must have at least getSize bytes length) |
---|
| 87 | * structure of the bitstream: |
---|
| 88 | * (var1_size,var1,var2_size,var2,...) |
---|
| 89 | * varx_size: size = sizeof(int) |
---|
| 90 | * varx: size = varx_size |
---|
| 91 | * @return data containing all variables and their sizes |
---|
| 92 | */ |
---|
| 93 | syncData Synchronisable::getData(unsigned char *mem){ |
---|
| 94 | std::list<synchronisableVariable>::iterator i; |
---|
| 95 | syncData retVal; |
---|
| 96 | retVal.objectID=this->objectID; |
---|
| 97 | retVal.classID=this->classID; |
---|
| 98 | retVal.length=datasize; |
---|
| 99 | retVal.data=mem; |
---|
| 100 | // copy to location |
---|
| 101 | //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP |
---|
| 102 | int n=0; |
---|
| 103 | for(i=syncList.begin(); n<datasize && i!=syncList.end(); i++){ |
---|
[285] | 104 | //CHANGED: i->size TO (const void*)(&(i->size)) memcpy WANTS A CONST VOID* SO CONVERT INT TO CONST VOID* |
---|
[247] | 105 | std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int)); |
---|
| 106 | n+=sizeof(int); |
---|
| 107 | //CHANGED: i->var TO (const void*)(&(i->var)) SINCE var IS A POINTER, NO & BEFORE i |
---|
| 108 | std::memcpy(retVal.data+n, (const void*)(i->var), i->size); |
---|
| 109 | n+=i->size; |
---|
| 110 | } |
---|
| 111 | return retVal; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | * This function takes a syncData struct and takes it to update the variables |
---|
| 116 | * @param vars data of the variables |
---|
| 117 | * @return true/false |
---|
| 118 | */ |
---|
[237] | 119 | bool Synchronisable::updateData(syncData vars){ |
---|
| 120 | unsigned char *data=vars.data; |
---|
| 121 | std::list<synchronisableVariable>::iterator i; |
---|
| 122 | for(i=syncList.begin(); i!=syncList.end(); i++){ |
---|
| 123 | if((int)*data==i->size){ |
---|
| 124 | data+=sizeof(int); |
---|
[238] | 125 | //CHANGED: THIS FROM i->var TO (void*)i->var SINCE var IS A CONST VOID* AND memcpy NEEDS A VOID* AS FIRST ARGUMENT |
---|
| 126 | memcpy((void*)i->var, data, i->size); |
---|
[237] | 127 | data+=i->size; |
---|
| 128 | } else |
---|
| 129 | return false; //there was some problem with registerVar |
---|
| 130 | } |
---|
| 131 | } |
---|
[230] | 132 | |
---|
[247] | 133 | /** |
---|
| 134 | * This function returns the total amount of bytes needed by getData to save the whole content of the variables |
---|
| 135 | * @return amount of bytes |
---|
| 136 | */ |
---|
| 137 | int Synchronisable::getSize(){ |
---|
| 138 | return datasize; |
---|
[230] | 139 | } |
---|
[247] | 140 | |
---|
| 141 | } |
---|