[1168] | 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 | |
---|
| 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 | { |
---|
[1232] | 50 | |
---|
| 51 | |
---|
[1245] | 52 | int Synchronisable::state_=0x1; // detemines wheter we are server (default) or client |
---|
[1232] | 53 | |
---|
[1168] | 54 | /** |
---|
| 55 | * Constructor: |
---|
| 56 | * calls registarAllVariables, that has to be implemented by the inheriting classID |
---|
| 57 | */ |
---|
| 58 | Synchronisable::Synchronisable(){ |
---|
| 59 | RegisterRootObject(Synchronisable); |
---|
| 60 | static int idCounter=0; |
---|
| 61 | datasize=0; |
---|
| 62 | objectID=idCounter++; |
---|
| 63 | syncList = new std::list<synchronisableVariable *>; |
---|
| 64 | //registerAllVariables(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | Synchronisable::~Synchronisable(){ |
---|
| 68 | } |
---|
[1232] | 69 | |
---|
| 70 | void Synchronisable::setClient(bool b){ |
---|
| 71 | if(b) // client |
---|
| 72 | state_=0x2; |
---|
| 73 | else // server |
---|
| 74 | state_=0x1; |
---|
| 75 | } |
---|
[1168] | 76 | |
---|
| 77 | /** |
---|
| 78 | * This function is used to register a variable to be synchronized |
---|
| 79 | * also counts the total datasize needed to save the variables |
---|
| 80 | * @param var pointer to the variable |
---|
| 81 | * @param size size of the datatype the variable consists of |
---|
| 82 | */ |
---|
[1232] | 83 | void Synchronisable::registerVar(void *var, int size, variableType t, int mode){ |
---|
[1168] | 84 | // create temporary synch.Var struct |
---|
| 85 | synchronisableVariable *temp = new synchronisableVariable; |
---|
| 86 | temp->size = size; |
---|
| 87 | temp->var = var; |
---|
[1232] | 88 | temp->mode = mode; |
---|
[1168] | 89 | temp->type = t; |
---|
| 90 | COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl; |
---|
| 91 | // increase datasize |
---|
| 92 | datasize+=sizeof(int)+size; |
---|
| 93 | //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl; |
---|
| 94 | COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl; |
---|
| 95 | syncList->push_back(temp); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * 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) |
---|
| 100 | * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct |
---|
| 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(){ |
---|
| 108 | // std::list<synchronisableVariable>::iterator i; |
---|
| 109 | // int totalsize=0; |
---|
| 110 | // //figure out size of data to be allocated |
---|
| 111 | // for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
| 112 | // // increase size (size of variable and size of size of variable ;) |
---|
| 113 | // if(i->type == STRING) |
---|
| 114 | // totalsize+=sizeof(int)+((std::string *)i->var)->length()+1; |
---|
| 115 | // else |
---|
| 116 | // totalsize+=sizeof(int)+i->size; |
---|
| 117 | // } |
---|
| 118 | // syncData retVal; |
---|
| 119 | // retVal.objectID=this->objectID; |
---|
| 120 | // retVal.classID=this->classID; |
---|
| 121 | // retVal.length=totalsize; |
---|
| 122 | // // allocate memory |
---|
| 123 | // retVal.data = (unsigned char *)malloc(totalsize); |
---|
| 124 | // // copy to location |
---|
| 125 | // //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP |
---|
| 126 | // int n=0; |
---|
| 127 | // for(i=syncList->begin(); n<totalsize && i!=syncList->end(); i++){ |
---|
| 128 | // std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int)); |
---|
| 129 | // n+=sizeof(int); |
---|
| 130 | // switch(i->type){ |
---|
| 131 | // case STRING: |
---|
| 132 | // std::memcpy(retVal.data+n, (const void *)(((std::string *)i->var)->c_str()), ((std::string *)i->var)->length()+1); |
---|
| 133 | // n+=((std::string *)i->var)->length()+1; |
---|
| 134 | // break; |
---|
| 135 | // case DATA: |
---|
| 136 | // std::memcpy(retVal.data+n, ((const void*)i->var), i->size); |
---|
| 137 | // n+=i->size; |
---|
| 138 | // break; |
---|
| 139 | // } |
---|
| 140 | // } |
---|
| 141 | // return retVal; |
---|
| 142 | // } |
---|
| 143 | /** |
---|
| 144 | * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct |
---|
| 145 | * Difference to the above function: |
---|
| 146 | * takes a pointer to already allocated memory (must have at least getSize bytes length) |
---|
| 147 | * structure of the bitstream: |
---|
| 148 | * (var1_size,var1,var2_size,var2,...) |
---|
| 149 | * varx_size: size = sizeof(int) |
---|
| 150 | * varx: size = varx_size |
---|
| 151 | * @return data containing all variables and their sizes |
---|
| 152 | */ |
---|
| 153 | syncData Synchronisable::getData(unsigned char *mem){ |
---|
| 154 | //std::cout << "inside getData" << std::endl; |
---|
[1360] | 155 | if(classID==0) |
---|
| 156 | COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl; |
---|
| 157 | this->classID=this->getIdentifier()->getNetworkID(); |
---|
[1168] | 158 | std::list<synchronisableVariable *>::iterator i; |
---|
| 159 | syncData retVal; |
---|
| 160 | retVal.objectID=this->objectID; |
---|
| 161 | retVal.classID=this->classID; |
---|
| 162 | retVal.length=getSize(); |
---|
[1199] | 163 | COUT(5) << "Synchronisable getting data from objectID: " << retVal.objectID << " classID: " << retVal.classID << " length: " << retVal.length << std::endl; |
---|
[1168] | 164 | retVal.data=mem; |
---|
| 165 | // copy to location |
---|
| 166 | int n=0; //offset |
---|
| 167 | for(i=syncList->begin(); n<datasize && i!=syncList->end(); ++i){ |
---|
| 168 | //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int)); |
---|
[1245] | 169 | if( ((*i)->mode & state_) == 0 ){ |
---|
[1248] | 170 | COUT(5) << "not getting data: " << std::endl; |
---|
[1232] | 171 | continue; // this variable should only be received |
---|
[1245] | 172 | } |
---|
[1168] | 173 | switch((*i)->type){ |
---|
| 174 | case DATA: |
---|
[1180] | 175 | std::memcpy( (void *)(retVal.data+n), (void*)((*i)->var), (*i)->size); |
---|
[1168] | 176 | n+=(*i)->size; |
---|
| 177 | break; |
---|
| 178 | case STRING: |
---|
[1180] | 179 | memcpy( (void *)(retVal.data+n), (void *)&((*i)->size), sizeof(int) ); |
---|
[1170] | 180 | n+=sizeof(int); |
---|
[1180] | 181 | const char *data = ( ( *(std::string *) (*i)->var).c_str()); |
---|
| 182 | std::memcpy( retVal.data+n, (void*)data, (*i)->size); |
---|
[1199] | 183 | COUT(5) << "synchronisable: char: " << (const char *)(retVal.data+n) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl; |
---|
[1168] | 184 | n+=(*i)->size; |
---|
| 185 | break; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | return retVal; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | * This function takes a syncData struct and takes it to update the variables |
---|
| 193 | * @param vars data of the variables |
---|
| 194 | * @return true/false |
---|
| 195 | */ |
---|
| 196 | bool Synchronisable::updateData(syncData vars){ |
---|
| 197 | unsigned char *data=vars.data; |
---|
| 198 | std::list<synchronisableVariable *>::iterator i; |
---|
| 199 | if(syncList->empty()){ |
---|
| 200 | COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl; |
---|
| 201 | return false; |
---|
| 202 | } |
---|
[1176] | 203 | COUT(5) << "Synchronisable: objectID " << vars.objectID << ", classID " << vars.classID << " size: " << vars.length << " synchronising data" << std::endl; |
---|
[1168] | 204 | for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
[1245] | 205 | if( ((*i)->mode ^ state_) == 0 ){ |
---|
| 206 | COUT(5) << "synchronisable: not updating variable " << std::endl; |
---|
| 207 | continue; // this variable should only be updated |
---|
| 208 | } |
---|
[1168] | 209 | COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl; |
---|
[1170] | 210 | switch((*i)->type){ |
---|
| 211 | case DATA: |
---|
| 212 | memcpy((void*)(*i)->var, data, (*i)->size); |
---|
| 213 | data+=(*i)->size; |
---|
| 214 | break; |
---|
| 215 | case STRING: |
---|
| 216 | (*i)->size = *(int *)data; |
---|
[1199] | 217 | COUT(5) << "string size: " << (*i)->size << std::endl; |
---|
[1170] | 218 | data+=sizeof(int); |
---|
| 219 | *((std::string *)((*i)->var)) = std::string((const char*)data); |
---|
[1199] | 220 | COUT(5) << "synchronisable: char: " << (const char*)data << " string: " << std::string((const char*)data) << std::endl; |
---|
[1170] | 221 | data += (*i)->size; |
---|
| 222 | break; |
---|
| 223 | } |
---|
[1168] | 224 | } |
---|
| 225 | return true; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | * This function returns the total amount of bytes needed by getData to save the whole content of the variables |
---|
| 230 | * @return amount of bytes |
---|
| 231 | */ |
---|
| 232 | int Synchronisable::getSize(){ |
---|
| 233 | int tsize=0; |
---|
| 234 | std::list<synchronisableVariable *>::iterator i; |
---|
| 235 | for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
[1232] | 236 | if( ((*i)->mode & state_) == 0 ) |
---|
| 237 | continue; // this variable should only be received, so dont add its size to the send-size |
---|
[1168] | 238 | switch((*i)->type){ |
---|
| 239 | case DATA: |
---|
| 240 | tsize+=(*i)->size; |
---|
| 241 | break; |
---|
| 242 | case STRING: |
---|
| 243 | tsize+=sizeof(int); |
---|
| 244 | (*i)->size=((std::string *)(*i)->var)->length()+1; |
---|
[1199] | 245 | COUT(5) << "String size: " << (*i)->size << std::endl; |
---|
[1168] | 246 | tsize+=(*i)->size; |
---|
| 247 | break; |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | return tsize; |
---|
| 251 | } |
---|
[1232] | 252 | |
---|
| 253 | void Synchronisable::setBacksync(bool sync){ |
---|
| 254 | backsync_=sync; |
---|
| 255 | } |
---|
[1168] | 256 | |
---|
[1232] | 257 | bool Synchronisable::getBacksync(){ |
---|
| 258 | return backsync_; |
---|
| 259 | } |
---|
| 260 | |
---|
[1168] | 261 | } |
---|