Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some changes got lost(camera)

File size: 9.2 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    classID=this->getIdentifier()->getNetworkID();
157    std::list<synchronisableVariable *>::iterator i;
158    syncData retVal;
159    retVal.objectID=this->objectID;
160    retVal.classID=this->classID;
161    retVal.length=getSize();
162    COUT(5) << "Synchronisable getting data from objectID: " << retVal.objectID << " classID: " << retVal.classID << " length: " << retVal.length << std::endl;
163    retVal.data=mem;
164    // copy to location
165    int n=0; //offset
166    for(i=syncList->begin(); n<datasize && i!=syncList->end(); ++i){
167      //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int));
168      if( ((*i)->mode & state_) == 0 ){
169        COUT(5) << "not getting data: " << std::endl;
170        continue;  // this variable should only be received
171      }
172      switch((*i)->type){
173      case DATA:
174        std::memcpy( (void *)(retVal.data+n), (void*)((*i)->var), (*i)->size);
175        n+=(*i)->size;
176        break;
177      case STRING:
178        memcpy( (void *)(retVal.data+n), (void *)&((*i)->size), sizeof(int) );
179        n+=sizeof(int);
180        const char *data = ( ( *(std::string *) (*i)->var).c_str());
181        std::memcpy( retVal.data+n, (void*)data, (*i)->size);
182        COUT(5) << "synchronisable: char: " << (const char *)(retVal.data+n) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl;
183        n+=(*i)->size;
184        break;
185      }
186    }
187    return retVal;
188  }
189
190  /**
191  * This function takes a syncData struct and takes it to update the variables
192  * @param vars data of the variables
193  * @return true/false
194  */
195  bool Synchronisable::updateData(syncData vars){
196    unsigned char *data=vars.data;
197    std::list<synchronisableVariable *>::iterator i;
198    if(syncList->empty()){
199      COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl;
200      return false;
201    }
202    COUT(5) << "Synchronisable: objectID " << vars.objectID << ", classID " << vars.classID << " size: " << vars.length << " synchronising data" << std::endl;
203    for(i=syncList->begin(); i!=syncList->end(); i++){
204      if( ((*i)->mode ^ state_) == 0 ){
205        COUT(5) << "synchronisable: not updating variable " << std::endl;
206        continue;  // this variable should only be updated
207      }
208      COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl;
209      switch((*i)->type){
210      case DATA:
211        memcpy((void*)(*i)->var, data, (*i)->size);
212        data+=(*i)->size;
213        break;
214      case STRING:
215        (*i)->size = *(int *)data;
216        COUT(5) << "string size: " << (*i)->size << std::endl;
217        data+=sizeof(int);
218        *((std::string *)((*i)->var)) = std::string((const char*)data);
219        COUT(5) << "synchronisable: char: " << (const char*)data << " string: " << std::string((const char*)data) << std::endl;
220        data += (*i)->size;
221        break;
222      }
223    }
224    return true;
225  }
226
227  /**
228  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
229  * @return amount of bytes
230  */
231  int Synchronisable::getSize(){
232    int tsize=0;
233    std::list<synchronisableVariable *>::iterator i;
234    for(i=syncList->begin(); i!=syncList->end(); i++){
235      if( ((*i)->mode & state_) == 0 )
236        continue;  // this variable should only be received, so dont add its size to the send-size
237      switch((*i)->type){
238      case DATA:
239        tsize+=(*i)->size;
240        break;
241      case STRING:
242        tsize+=sizeof(int);
243        (*i)->size=((std::string *)(*i)->var)->length()+1;
244        COUT(5) << "String size: " << (*i)->size << std::endl;
245        tsize+=(*i)->size;
246        break;
247      }
248    }
249    return tsize;
250  }
251 
252  void Synchronisable::setBacksync(bool sync){
253    backsync_=sync;
254  }
255
256  bool Synchronisable::getBacksync(){
257    return backsync_;
258  }
259 
260}
Note: See TracBrowser for help on using the repository browser.