Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/Synchronisable.cc @ 1056

Last change on this file since 1056 was 1056, checked in by landauf, 16 years ago

don't panic, no codechanges!
added a link to www.orxonox.net

File size: 6.8 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 <string>
42#include <iostream>
43
44#include "Synchronisable.h"
45#include "core/CoreIncludes.h"
46
47namespace network
48{
49  /**
50  * Constructor:
51  * calls registarAllVariables, that has to be implemented by the inheriting classID
52  */
53  Synchronisable::Synchronisable(){
54    RegisterRootObject(Synchronisable);
55    static int idCounter=0;
56    datasize=0;
57    objectID=idCounter++;
58    //registerAllVariables();
59  }
60
61  Synchronisable::~Synchronisable(){
62  }
63
64  /**
65  * This function is used to register a variable to be synchronized
66  * also counts the total datasize needed to save the variables
67  * @param var pointer to the variable
68  * @param size size of the datatype the variable consists of
69  */
70  void Synchronisable::registerVar(const void *var, int size, variableType t){
71    // create temporary synch.Var struct
72    synchronisableVariable temp={size, var, t};
73    // increase datasize
74    datasize+=sizeof(int)+size;
75    // push temp to syncList (at the bottom)
76    syncList.push_back(temp);
77  }
78
79  /**
80  * 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)
81  * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
82  * structure of the bitstream:
83  * (var1_size,var1,var2_size,var2,...)
84  * varx_size: size = sizeof(int)
85  * varx: size = varx_size
86  * @return data containing all variables and their sizes
87  */
88  // syncData Synchronisable::getData(){
89  //   std::list<synchronisableVariable>::iterator i;
90  //   int totalsize=0;
91  //   //figure out size of data to be allocated
92  //   for(i=syncList.begin(); i!=syncList.end(); i++){
93  //     // increase size (size of variable and size of size of variable ;)
94  //     if(i->type == STRING)
95  //       totalsize+=sizeof(int)+((std::string *)i->var)->length()+1;
96  //     else
97  //       totalsize+=sizeof(int)+i->size;
98  //   }
99  //   syncData retVal;
100  //   retVal.objectID=this->objectID;
101  //   retVal.classID=this->classID;
102  //   retVal.length=totalsize;
103  //   // allocate memory
104  //   retVal.data = (unsigned char *)malloc(totalsize);
105  //   // copy to location
106  //   //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP
107  //   int n=0;
108  //   for(i=syncList.begin(); n<totalsize && i!=syncList.end(); i++){
109  //     std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int));
110  //     n+=sizeof(int);
111  //     switch(i->type){
112  //     case STRING:
113  //       std::memcpy(retVal.data+n, (const void *)(((std::string *)i->var)->c_str()), ((std::string *)i->var)->length()+1);
114  //       n+=((std::string *)i->var)->length()+1;
115  //       break;
116  //     case DATA:
117  //       std::memcpy(retVal.data+n, ((const void*)i->var), i->size);
118  //       n+=i->size;
119  //       break;
120  //     }
121  //   }
122  //   return retVal;
123  // }
124  /**
125  * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
126  * Difference to the above function:
127  * takes a pointer to already allocated memory (must have at least getSize bytes length)
128  * structure of the bitstream:
129  * (var1_size,var1,var2_size,var2,...)
130  * varx_size: size = sizeof(int)
131  * varx: size = varx_size
132  * @return data containing all variables and their sizes
133  */
134  syncData Synchronisable::getData(unsigned char *mem){
135    std::list<synchronisableVariable>::iterator i;
136    syncData retVal;
137    retVal.objectID=this->objectID;
138    retVal.classID=this->classID;
139    retVal.length=getSize();
140    retVal.data=mem;
141    // copy to location
142    int n=0;
143    for(i=syncList.begin(); n<datasize && i!=syncList.end(); ++i){
144      //COUT(2) << "size of variable: " << i->size << std::endl;
145      //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int));
146      memcpy( (void *)(retVal.data+n), (const void*)&(i->size), sizeof(int) );
147      n+=sizeof(int);
148      switch(i->type){
149      case DATA:
150        std::memcpy( (void *)(retVal.data+n), (const void*)(i->var), i->size);
151        n+=i->size;
152        break;
153      case STRING:
154        std::memcpy( retVal.data+n, (const void*)( ( (std::string *) i->var)->c_str()), ( (std::string *)i->var )->length()+1);
155        n+=((std::string *) i->var)->length()+1;
156        break;
157      }
158    }
159    return retVal;
160  }
161
162  /**
163  * This function takes a syncData struct and takes it to update the variables
164  * @param vars data of the variables
165  * @return true/false
166  */
167  bool Synchronisable::updateData(syncData vars){
168    unsigned char *data=vars.data;
169    std::list<synchronisableVariable>::iterator i;
170    for(i=syncList.begin(); i!=syncList.end(); i++){
171      if((int)*data==i->size || i->type==STRING){
172        switch(i->type){
173      case DATA:
174        data+=sizeof(int);
175        memcpy((void*)i->var, data, i->size);
176        data+=i->size;
177        break;
178      case STRING:
179        i->size = (int)*data;
180        data+=sizeof(int);
181        *((std::string *)i->var) = std::string((const char*)data);
182        data += i->size;
183        break;
184        }
185      } else
186        return false; //there was some problem with registerVar
187    }
188    return true;
189  }
190
191  /**
192  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
193  * @return amount of bytes
194  */
195  int Synchronisable::getSize(){
196    int tsize=0;
197    std::list<synchronisableVariable>::iterator i;
198    for(i=syncList.begin(); i!=syncList.end(); i++){
199      switch(i->type){
200    case DATA:
201      tsize+=sizeof(int);
202      tsize+=i->size;
203      break;
204    case STRING:
205      tsize+=sizeof(int);
206      tsize+=((std::string *)i->var)->length()+1;
207      break;
208      }
209    }
210    return tsize;
211  }
212
213}
Note: See TracBrowser for help on using the repository browser.