Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/network/Synchronisable.cc @ 1151

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