Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merge/src/network/Synchronisable.cc @ 1306

Last change on this file since 1306 was 1306, checked in by scheusso, 17 years ago

found a 'bug' in projectile - we set the networkid now everytime in constructor of projectile

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