[1056] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Dumeni Manatschal, (C) 2007 |
---|
| 24 | * Oliver Scheuss, (C) 2007 |
---|
| 25 | * Co-authors: |
---|
| 26 | * ... |
---|
| 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
[1150] | 30 | // |
---|
| 31 | // C++ Implementation: synchronisable |
---|
| 32 | // |
---|
| 33 | // Description: |
---|
| 34 | // |
---|
| 35 | // |
---|
| 36 | // Author: Dumeni, Oliver Scheuss, (C) 2007 |
---|
| 37 | // |
---|
| 38 | // Copyright: See COPYING file that comes with this distribution |
---|
| 39 | // |
---|
| 40 | |
---|
| 41 | #include "Synchronisable.h" |
---|
| 42 | |
---|
| 43 | #include <string> |
---|
| 44 | #include <iostream> |
---|
| 45 | |
---|
| 46 | #include "core/CoreIncludes.h" |
---|
| 47 | |
---|
| 48 | namespace network |
---|
| 49 | { |
---|
| 50 | /** |
---|
| 51 | * Constructor: |
---|
| 52 | * calls registarAllVariables, that has to be implemented by the inheriting classID |
---|
| 53 | */ |
---|
| 54 | Synchronisable::Synchronisable(){ |
---|
| 55 | RegisterRootObject(Synchronisable); |
---|
| 56 | static int idCounter=0; |
---|
| 57 | datasize=0; |
---|
| 58 | objectID=idCounter++; |
---|
| 59 | syncList = new std::list<synchronisableVariable *>; |
---|
| 60 | //registerAllVariables(); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | Synchronisable::~Synchronisable(){ |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * This function is used to register a variable to be synchronized |
---|
| 68 | * also counts the total datasize needed to save the variables |
---|
| 69 | * @param var pointer to the variable |
---|
| 70 | * @param size size of the datatype the variable consists of |
---|
| 71 | */ |
---|
| 72 | void Synchronisable::registerVar(const void *var, int size, variableType t){ |
---|
| 73 | // create temporary synch.Var struct |
---|
| 74 | synchronisableVariable *temp = new synchronisableVariable; |
---|
| 75 | temp->size = size; |
---|
| 76 | temp->var = var; |
---|
| 77 | temp->type = t; |
---|
| 78 | COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl; |
---|
| 79 | // increase datasize |
---|
| 80 | datasize+=sizeof(int)+size; |
---|
| 81 | //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl; |
---|
| 82 | COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl; |
---|
| 83 | syncList->push_back(temp); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * 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) |
---|
| 88 | * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct |
---|
| 89 | * structure of the bitstream: |
---|
| 90 | * (var1_size,var1,var2_size,var2,...) |
---|
| 91 | * varx_size: size = sizeof(int) |
---|
| 92 | * varx: size = varx_size |
---|
| 93 | * @return data containing all variables and their sizes |
---|
| 94 | */ |
---|
| 95 | // syncData Synchronisable::getData(){ |
---|
| 96 | // std::list<synchronisableVariable>::iterator i; |
---|
| 97 | // int totalsize=0; |
---|
| 98 | // //figure out size of data to be allocated |
---|
| 99 | // for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
| 100 | // // increase size (size of variable and size of size of variable ;) |
---|
| 101 | // if(i->type == STRING) |
---|
| 102 | // totalsize+=sizeof(int)+((std::string *)i->var)->length()+1; |
---|
| 103 | // else |
---|
| 104 | // totalsize+=sizeof(int)+i->size; |
---|
| 105 | // } |
---|
| 106 | // syncData retVal; |
---|
| 107 | // retVal.objectID=this->objectID; |
---|
| 108 | // retVal.classID=this->classID; |
---|
| 109 | // retVal.length=totalsize; |
---|
| 110 | // // allocate memory |
---|
| 111 | // retVal.data = (unsigned char *)malloc(totalsize); |
---|
| 112 | // // copy to location |
---|
| 113 | // //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP |
---|
| 114 | // int n=0; |
---|
| 115 | // for(i=syncList->begin(); n<totalsize && i!=syncList->end(); i++){ |
---|
| 116 | // std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int)); |
---|
| 117 | // n+=sizeof(int); |
---|
| 118 | // switch(i->type){ |
---|
| 119 | // case STRING: |
---|
| 120 | // std::memcpy(retVal.data+n, (const void *)(((std::string *)i->var)->c_str()), ((std::string *)i->var)->length()+1); |
---|
| 121 | // n+=((std::string *)i->var)->length()+1; |
---|
| 122 | // break; |
---|
| 123 | // case DATA: |
---|
| 124 | // std::memcpy(retVal.data+n, ((const void*)i->var), i->size); |
---|
| 125 | // n+=i->size; |
---|
| 126 | // break; |
---|
| 127 | // } |
---|
| 128 | // } |
---|
| 129 | // return retVal; |
---|
| 130 | // } |
---|
| 131 | /** |
---|
| 132 | * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct |
---|
| 133 | * Difference to the above function: |
---|
| 134 | * takes a pointer to already allocated memory (must have at least getSize bytes length) |
---|
| 135 | * structure of the bitstream: |
---|
| 136 | * (var1_size,var1,var2_size,var2,...) |
---|
| 137 | * varx_size: size = sizeof(int) |
---|
| 138 | * varx: size = varx_size |
---|
| 139 | * @return data containing all variables and their sizes |
---|
| 140 | */ |
---|
| 141 | syncData Synchronisable::getData(unsigned char *mem){ |
---|
| 142 | //std::cout << "inside getData" << std::endl; |
---|
| 143 | std::list<synchronisableVariable *>::iterator i; |
---|
| 144 | syncData retVal; |
---|
| 145 | retVal.objectID=this->objectID; |
---|
| 146 | retVal.classID=this->classID; |
---|
| 147 | retVal.length=getSize(); |
---|
| 148 | retVal.data=mem; |
---|
| 149 | // copy to location |
---|
| 150 | int n=0; //offset |
---|
| 151 | for(i=syncList->begin(); n<datasize && i!=syncList->end(); ++i){ |
---|
| 152 | //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int)); |
---|
| 153 | memcpy( (void *)(retVal.data+n), (const void *)&((*i)->size), sizeof(int) ); |
---|
| 154 | n+=sizeof(int); |
---|
| 155 | switch((*i)->type){ |
---|
| 156 | case DATA: |
---|
| 157 | std::memcpy( (void *)(retVal.data+n), (const void*)((*i)->var), (*i)->size); |
---|
| 158 | n+=(*i)->size; |
---|
| 159 | break; |
---|
| 160 | case STRING: |
---|
| 161 | std::memcpy( retVal.data+n, (const void*)( ( (std::string *) (*i)->var)->c_str()), (*i)->size); |
---|
| 162 | n+=(*i)->size; |
---|
| 163 | break; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | return retVal; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * This function takes a syncData struct and takes it to update the variables |
---|
| 171 | * @param vars data of the variables |
---|
| 172 | * @return true/false |
---|
| 173 | */ |
---|
| 174 | bool Synchronisable::updateData(syncData vars){ |
---|
| 175 | unsigned char *data=vars.data; |
---|
| 176 | std::list<synchronisableVariable *>::iterator i; |
---|
| 177 | if(syncList->empty()){ |
---|
| 178 | COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl; |
---|
| 179 | return false; |
---|
| 180 | } |
---|
| 181 | COUT(5) << "Synchronisable: objectID " << objectID << ", classID " << classID << " synchronising data" << std::endl; |
---|
| 182 | for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
| 183 | COUT(5) << "element size: " << (*i)->size << " type: " << (*i)->type << std::endl; |
---|
| 184 | if(*(int *)data==(*i)->size || (*i)->type==STRING){ |
---|
| 185 | switch((*i)->type){ |
---|
| 186 | case DATA: |
---|
| 187 | data+=sizeof(int); |
---|
| 188 | memcpy((void*)(*i)->var, data, (*i)->size); |
---|
| 189 | data+=(*i)->size; |
---|
| 190 | break; |
---|
| 191 | case STRING: |
---|
| 192 | (*i)->size = *(int *)data; |
---|
| 193 | data+=sizeof(int); |
---|
| 194 | *((std::string *)((*i)->var)) = std::string((const char*)data); |
---|
| 195 | data += (*i)->size; |
---|
| 196 | break; |
---|
| 197 | } |
---|
| 198 | } else |
---|
| 199 | return false; //there was some problem with registerVar |
---|
| 200 | } |
---|
| 201 | return true; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | * This function returns the total amount of bytes needed by getData to save the whole content of the variables |
---|
| 206 | * @return amount of bytes |
---|
| 207 | */ |
---|
| 208 | int Synchronisable::getSize(){ |
---|
| 209 | int tsize=0; |
---|
| 210 | std::list<synchronisableVariable *>::iterator i; |
---|
| 211 | for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
| 212 | switch((*i)->type){ |
---|
| 213 | case DATA: |
---|
| 214 | tsize+=sizeof(int); |
---|
| 215 | tsize+=(*i)->size; |
---|
| 216 | break; |
---|
| 217 | case STRING: |
---|
| 218 | tsize+=sizeof(int); |
---|
| 219 | (*i)->size=((std::string *)(*i)->var)->length()+1; |
---|
| 220 | tsize+=(*i)->size; |
---|
| 221 | break; |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | return tsize; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | } |
---|