[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" |
---|
[531] | 13 | #include "orxonox/core/CoreIncludes.h" |
---|
[230] | 14 | |
---|
[531] | 15 | |
---|
[230] | 16 | namespace network { |
---|
| 17 | |
---|
[247] | 18 | /** |
---|
| 19 | * Constructor: |
---|
| 20 | * calls registarAllVariables, that has to be implemented by the inheriting classID |
---|
| 21 | */ |
---|
[230] | 22 | Synchronisable::Synchronisable() |
---|
| 23 | { |
---|
[531] | 24 | RegisterRootObject(Synchronisable); |
---|
[401] | 25 | static int idCounter=0; |
---|
[247] | 26 | datasize=0; |
---|
[401] | 27 | objectID=idCounter++; |
---|
[565] | 28 | // registerAllVariables(); |
---|
[230] | 29 | } |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | Synchronisable::~Synchronisable() |
---|
| 33 | { |
---|
[285] | 34 | |
---|
[230] | 35 | } |
---|
| 36 | |
---|
[247] | 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 | */ |
---|
[565] | 43 | void Synchronisable::registerVar(const void *var, int size, variableType t){ |
---|
[247] | 44 | // create temporary synch.Var struct |
---|
[565] | 45 | synchronisableVariable temp={size, var, t}; |
---|
[247] | 46 | // increase datasize |
---|
| 47 | datasize+=sizeof(int)+size; |
---|
| 48 | // push temp to syncList (at the bottom) |
---|
[237] | 49 | syncList.push_back(temp); |
---|
| 50 | } |
---|
[247] | 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 | */ |
---|
[571] | 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 | // } |
---|
[247] | 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=datasize; |
---|
| 113 | retVal.data=mem; |
---|
| 114 | // copy to location |
---|
| 115 | int n=0; |
---|
[571] | 116 | for(i=syncList.begin(); n<datasize && i!=syncList.end(); ++i){ |
---|
[569] | 117 | //COUT(2) << "size of variable: " << i->size << std::endl; |
---|
| 118 | //COUT(2) << "size of variable: " << i->size << std::endl; |
---|
[571] | 119 | //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int)); |
---|
| 120 | *((int *)(retVal.data+n)) = i->size; |
---|
[247] | 121 | n+=sizeof(int); |
---|
[565] | 122 | switch(i->type){ |
---|
| 123 | case DATA: |
---|
| 124 | std::memcpy(retVal.data+n, (const void*)(i->var), i->size); |
---|
| 125 | n+=i->size; |
---|
| 126 | break; |
---|
| 127 | case STRING: |
---|
[571] | 128 | std::memcpy( retVal.data+n, (const void*)( ( (std::string *) i->var)->c_str()), ( (std::string *)i->var )->length()+1); |
---|
[565] | 129 | n+=((std::string *) i->var)->length()+1; |
---|
| 130 | break; |
---|
| 131 | } |
---|
[247] | 132 | } |
---|
| 133 | return retVal; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * This function takes a syncData struct and takes it to update the variables |
---|
| 138 | * @param vars data of the variables |
---|
| 139 | * @return true/false |
---|
| 140 | */ |
---|
[237] | 141 | bool Synchronisable::updateData(syncData vars){ |
---|
| 142 | unsigned char *data=vars.data; |
---|
| 143 | std::list<synchronisableVariable>::iterator i; |
---|
| 144 | for(i=syncList.begin(); i!=syncList.end(); i++){ |
---|
[565] | 145 | if((int)*data==i->size || i->type==STRING){ |
---|
| 146 | switch(i->type){ |
---|
| 147 | case DATA: |
---|
| 148 | data+=sizeof(int); |
---|
| 149 | memcpy((void*)i->var, data, i->size); |
---|
| 150 | data+=i->size; |
---|
| 151 | break; |
---|
| 152 | case STRING: |
---|
| 153 | i->size = (int)*data; |
---|
| 154 | data+=sizeof(int); |
---|
| 155 | *((std::string *)i->var) = std::string((const char*)data); |
---|
| 156 | data += i->size; |
---|
| 157 | break; |
---|
| 158 | } |
---|
[237] | 159 | } else |
---|
| 160 | return false; //there was some problem with registerVar |
---|
| 161 | } |
---|
[405] | 162 | return true; |
---|
[237] | 163 | } |
---|
[230] | 164 | |
---|
[247] | 165 | /** |
---|
| 166 | * This function returns the total amount of bytes needed by getData to save the whole content of the variables |
---|
| 167 | * @return amount of bytes |
---|
| 168 | */ |
---|
| 169 | int Synchronisable::getSize(){ |
---|
[565] | 170 | int tsize=0; |
---|
| 171 | std::list<synchronisableVariable>::iterator i; |
---|
| 172 | for(i=syncList.begin(); i!=syncList.end(); i++){ |
---|
| 173 | switch(i->type){ |
---|
| 174 | case DATA: |
---|
| 175 | tsize+=i->size; |
---|
| 176 | break; |
---|
| 177 | case STRING: |
---|
| 178 | tsize+=((std::string *)i->var)->length()+1; |
---|
| 179 | break; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | return tsize; |
---|
[230] | 183 | } |
---|
[247] | 184 | |
---|
| 185 | } |
---|