Changeset 777 for code/branches/FICN/src/network/Synchronisable.cc
- Timestamp:
- Dec 31, 2007, 7:40:23 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/network/Synchronisable.cc
r774 r777 10 10 // 11 11 12 #include <string> 13 #include <iostream> 14 12 15 #include "Synchronisable.h" 13 #include "core/CoreIncludes.h"14 16 17 namespace network 18 { 19 /** 20 * Constructor: 21 * calls registarAllVariables, that has to be implemented by the inheriting classID 22 */ 23 Synchronisable::Synchronisable(){ 24 RegisterRootObject(Synchronisable); 25 static int idCounter=0; 26 datasize=0; 27 objectID=idCounter++; 28 //registerAllVariables(); 29 } 15 30 16 namespace network { 31 Synchronisable::~Synchronisable(){ 32 } 17 33 18 /** 19 * Constructor: 20 * calls registarAllVariables, that has to be implemented by the inheriting classID 21 */ 22 Synchronisable::Synchronisable() 23 { 24 RegisterRootObject(Synchronisable); 25 static int idCounter=0; 26 datasize=0; 27 objectID=idCounter++; 28 //registerAllVariables(); 29 } 34 /** 35 * This function is used to register a variable to be synchronized 36 * also counts the total datasize needed to save the variables 37 * @param var pointer to the variable 38 * @param size size of the datatype the variable consists of 39 */ 40 void Synchronisable::registerVar(const void *var, int size, variableType t){ 41 // create temporary synch.Var struct 42 synchronisableVariable temp={size, var, t}; 43 // increase datasize 44 datasize+=sizeof(int)+size; 45 // push temp to syncList (at the bottom) 46 syncList.push_back(temp); 47 } 30 48 31 32 Synchronisable::~Synchronisable() 33 { 34 35 } 36 37 /** 38 * This function is used to register a variable to be synchronized 39 * also counts the total datasize needed to save the variables 40 * @param var pointer to the variable 41 * @param size size of the datatype the variable consists of 42 */ 43 void Synchronisable::registerVar(const void *var, int size, variableType t){ 44 // create temporary synch.Var struct 45 synchronisableVariable temp={size, var, t}; 46 // increase datasize 47 datasize+=sizeof(int)+size; 48 // push temp to syncList (at the bottom) 49 syncList.push_back(temp); 50 } 51 52 /** 53 * 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) 54 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct 55 * structure of the bitstream: 56 * (var1_size,var1,var2_size,var2,...) 57 * varx_size: size = sizeof(int) 58 * varx: size = varx_size 59 * @return data containing all variables and their sizes 60 */ 61 // syncData Synchronisable::getData(){ 62 // std::list<synchronisableVariable>::iterator i; 63 // int totalsize=0; 64 // //figure out size of data to be allocated 65 // for(i=syncList.begin(); i!=syncList.end(); i++){ 66 // // increase size (size of variable and size of size of variable ;) 67 // if(i->type == STRING) 68 // totalsize+=sizeof(int)+((std::string *)i->var)->length()+1; 69 // else 70 // totalsize+=sizeof(int)+i->size; 71 // } 72 // syncData retVal; 73 // retVal.objectID=this->objectID; 74 // retVal.classID=this->classID; 75 // retVal.length=totalsize; 76 // // allocate memory 77 // retVal.data = (unsigned char *)malloc(totalsize); 78 // // copy to location 79 // //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP 80 // int n=0; 81 // for(i=syncList.begin(); n<totalsize && i!=syncList.end(); i++){ 82 // std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int)); 83 // n+=sizeof(int); 84 // switch(i->type){ 85 // case STRING: 86 // std::memcpy(retVal.data+n, (const void *)(((std::string *)i->var)->c_str()), ((std::string *)i->var)->length()+1); 87 // n+=((std::string *)i->var)->length()+1; 88 // break; 89 // case DATA: 90 // std::memcpy(retVal.data+n, ((const void*)i->var), i->size); 91 // n+=i->size; 92 // break; 93 // } 94 // } 95 // return retVal; 96 // } 97 /** 98 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct 99 * Difference to the above function: 100 * takes a pointer to already allocated memory (must have at least getSize bytes length) 101 * structure of the bitstream: 102 * (var1_size,var1,var2_size,var2,...) 103 * varx_size: size = sizeof(int) 104 * varx: size = varx_size 105 * @return data containing all variables and their sizes 106 */ 107 syncData Synchronisable::getData(unsigned char *mem){ 108 std::list<synchronisableVariable>::iterator i; 109 syncData retVal; 110 retVal.objectID=this->objectID; 111 retVal.classID=this->classID; 112 retVal.length=getSize(); 113 retVal.data=mem; 114 // copy to location 115 int n=0; 116 for(i=syncList.begin(); n<datasize && i!=syncList.end(); ++i){ 117 //COUT(2) << "size of variable: " << i->size << std::endl; 118 //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int)); 119 memcpy( (void *)(retVal.data+n), (const void*)&(i->size), sizeof(int) ); 120 n+=sizeof(int); 121 switch(i->type){ 49 /** 50 * 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) 51 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct 52 * structure of the bitstream: 53 * (var1_size,var1,var2_size,var2,...) 54 * varx_size: size = sizeof(int) 55 * varx: size = varx_size 56 * @return data containing all variables and their sizes 57 */ 58 // syncData Synchronisable::getData(){ 59 // std::list<synchronisableVariable>::iterator i; 60 // int totalsize=0; 61 // //figure out size of data to be allocated 62 // for(i=syncList.begin(); i!=syncList.end(); i++){ 63 // // increase size (size of variable and size of size of variable ;) 64 // if(i->type == STRING) 65 // totalsize+=sizeof(int)+((std::string *)i->var)->length()+1; 66 // else 67 // totalsize+=sizeof(int)+i->size; 68 // } 69 // syncData retVal; 70 // retVal.objectID=this->objectID; 71 // retVal.classID=this->classID; 72 // retVal.length=totalsize; 73 // // allocate memory 74 // retVal.data = (unsigned char *)malloc(totalsize); 75 // // copy to location 76 // //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP 77 // int n=0; 78 // for(i=syncList.begin(); n<totalsize && i!=syncList.end(); i++){ 79 // std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int)); 80 // n+=sizeof(int); 81 // switch(i->type){ 82 // case STRING: 83 // std::memcpy(retVal.data+n, (const void *)(((std::string *)i->var)->c_str()), ((std::string *)i->var)->length()+1); 84 // n+=((std::string *)i->var)->length()+1; 85 // break; 86 // case DATA: 87 // std::memcpy(retVal.data+n, ((const void*)i->var), i->size); 88 // n+=i->size; 89 // break; 90 // } 91 // } 92 // return retVal; 93 // } 94 /** 95 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct 96 * Difference to the above function: 97 * takes a pointer to already allocated memory (must have at least getSize bytes length) 98 * structure of the bitstream: 99 * (var1_size,var1,var2_size,var2,...) 100 * varx_size: size = sizeof(int) 101 * varx: size = varx_size 102 * @return data containing all variables and their sizes 103 */ 104 syncData Synchronisable::getData(unsigned char *mem){ 105 std::list<synchronisableVariable>::iterator i; 106 syncData retVal; 107 retVal.objectID=this->objectID; 108 retVal.classID=this->classID; 109 retVal.length=getSize(); 110 retVal.data=mem; 111 // copy to location 112 int n=0; 113 for(i=syncList.begin(); n<datasize && i!=syncList.end(); ++i){ 114 //COUT(2) << "size of variable: " << i->size << std::endl; 115 //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int)); 116 memcpy( (void *)(retVal.data+n), (const void*)&(i->size), sizeof(int) ); 117 n+=sizeof(int); 118 switch(i->type){ 122 119 case DATA: 123 120 std::memcpy( (void *)(retVal.data+n), (const void*)(i->var), i->size); … … 128 125 n+=((std::string *) i->var)->length()+1; 129 126 break; 127 } 130 128 } 129 return retVal; 131 130 } 132 return retVal;133 }134 131 135 /**136 * This function takes a syncData struct and takes it to update the variables137 * @param vars data of the variables138 * @return true/false139 */140 bool Synchronisable::updateData(syncData vars){141 unsigned char *data=vars.data;142 std::list<synchronisableVariable>::iterator i;143 for(i=syncList.begin(); i!=syncList.end(); i++){144 if((int)*data==i->size || i->type==STRING){145 switch(i->type){132 /** 133 * This function takes a syncData struct and takes it to update the variables 134 * @param vars data of the variables 135 * @return true/false 136 */ 137 bool Synchronisable::updateData(syncData vars){ 138 unsigned char *data=vars.data; 139 std::list<synchronisableVariable>::iterator i; 140 for(i=syncList.begin(); i!=syncList.end(); i++){ 141 if((int)*data==i->size || i->type==STRING){ 142 switch(i->type){ 146 143 case DATA: 147 144 data+=sizeof(int); … … 155 152 data += i->size; 156 153 break; 157 } 158 } else 159 return false; //there was some problem with registerVar 154 } 155 } else 156 return false; //there was some problem with registerVar 157 } 158 return true; 160 159 } 161 return true;162 }163 160 164 /**165 * This function returns the total amount of bytes needed by getData to save the whole content of the variables166 * @return amount of bytes167 */168 int Synchronisable::getSize(){169 int tsize=0;170 std::list<synchronisableVariable>::iterator i;171 for(i=syncList.begin(); i!=syncList.end(); i++){172 switch(i->type){161 /** 162 * This function returns the total amount of bytes needed by getData to save the whole content of the variables 163 * @return amount of bytes 164 */ 165 int Synchronisable::getSize(){ 166 int tsize=0; 167 std::list<synchronisableVariable>::iterator i; 168 for(i=syncList.begin(); i!=syncList.end(); i++){ 169 switch(i->type){ 173 170 case DATA: 174 171 tsize+=sizeof(int); … … 179 176 tsize+=((std::string *)i->var)->length()+1; 180 177 break; 178 } 181 179 } 180 return tsize; 182 181 } 183 return tsize;184 }185 182 186 183 }
Note: See TracChangeset
for help on using the changeset viewer.