Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/Synchronisable.cc @ 568

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

added class BaseEntity:

  • !!! please inherit from this class in future (instead of Entity/WorldEntity/BaseObject):
  • this class is able to be synchronised over the network (or at least created so far)
File size: 5.3 KB
Line 
1//
2// C++ Implementation: synchronisable
3//
4// Description:
5//
6//
7// Author:  Dumeni, Oliver Scheuss, (C) 2007
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11
12#include "Synchronisable.h"
13#include "orxonox/core/CoreIncludes.h"
14
15
16namespace network {
17
18/**
19 * Constructor:
20 * calls registarAllVariables, that has to be implemented by the inheriting classID
21 */
22Synchronisable::Synchronisable()
23{
24  RegisterRootObject(Synchronisable);
25  static int idCounter=0;
26  datasize=0;
27  objectID=idCounter++;
28//   registerAllVariables();
29}
30
31
32Synchronisable::~Synchronisable()
33{
34
35}
36
37/**
38 * This function is used to register a variable to be synchronized
39 * also counts the total datasize needed to save the variables
40 * @param var pointer to the variable
41 * @param size size of the datatype the variable consists of
42 */
43void Synchronisable::registerVar(const void *var, int size, variableType t){
44  // create temporary synch.Var struct
45  synchronisableVariable temp={size, var, t};
46  // increase datasize
47  datasize+=sizeof(int)+size;
48  // push temp to syncList (at the bottom)
49  syncList.push_back(temp);
50}
51
52/**
53 * 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)
54 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
55 * structure of the bitstream:
56 * (var1_size,var1,var2_size,var2,...)
57 * varx_size: size = sizeof(int)
58 * varx: size = varx_size
59 * @return data containing all variables and their sizes
60 */
61syncData Synchronisable::getData(){
62  std::list<synchronisableVariable>::iterator i;
63  int totalsize=0;
64  //figure out size of data to be allocated
65  for(i=syncList.begin(); i!=syncList.end(); i++){
66    // increase size (size of variable and size of size of variable ;)
67    if(i->type == STRING)
68      totalsize+=sizeof(int)+((std::string *)i->var)->length()+1;
69    else
70      totalsize+=sizeof(int)+i->size;
71  }
72  syncData retVal;
73  retVal.objectID=this->objectID;
74  retVal.classID=this->classID;
75  retVal.length=totalsize;
76  // allocate memory
77  retVal.data = (unsigned char *)malloc(totalsize);
78  // copy to location
79  //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP
80  int n=0;
81  for(i=syncList.begin(); n<totalsize && i!=syncList.end(); i++){
82    std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int));
83    n+=sizeof(int);
84    switch(i->type){
85    case STRING:
86      std::memcpy(retVal.data+n, (const void *)(((std::string *)i->var)->c_str()), ((std::string *)i->var)->length()+1);
87      n+=((std::string *)i->var)->length()+1;
88      break;
89    case DATA:
90      std::memcpy(retVal.data+n, ((const void*)i->var), i->size);
91      n+=i->size;
92      break;
93    }
94  }
95  return retVal;
96}
97/**
98 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
99 * Difference to the above function:
100 * takes a pointer to already allocated memory (must have at least getSize bytes length)
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 */
107syncData Synchronisable::getData(unsigned char *mem){
108  std::list<synchronisableVariable>::iterator i;
109  syncData retVal;
110  retVal.objectID=this->objectID;
111  retVal.classID=this->classID;
112  retVal.length=datasize;
113  retVal.data=mem;
114  // copy to location
115  //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP
116  int n=0;
117  for(i=syncList.begin(); n<datasize && i!=syncList.end(); i++){
118    std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int));
119    n+=sizeof(int);
120    switch(i->type){
121      case DATA:
122        std::memcpy(retVal.data+n, (const void*)(i->var), i->size);
123        n+=i->size;
124        break;
125      case STRING:
126        std::memcpy(retVal.data+n, (const void*)(((std::string *)i->var)->c_str()), ((std::string *)i->var)->length()+1);
127        n+=((std::string *) i->var)->length()+1;
128        break;
129    }
130  }
131  return retVal;
132}
133
134/**
135 * This function takes a syncData struct and takes it to update the variables
136 * @param vars data of the variables
137 * @return true/false
138 */
139bool Synchronisable::updateData(syncData vars){
140  unsigned char *data=vars.data;
141  std::list<synchronisableVariable>::iterator i;
142  for(i=syncList.begin(); i!=syncList.end(); i++){
143    if((int)*data==i->size || i->type==STRING){
144      switch(i->type){
145      case DATA:
146        data+=sizeof(int);
147        memcpy((void*)i->var, data, i->size);
148        data+=i->size;
149        break;
150      case STRING:
151        i->size = (int)*data;
152        data+=sizeof(int);
153        *((std::string *)i->var) = std::string((const char*)data);
154        data += i->size;
155        break;
156      }
157    } else
158      return false; //there was some problem with registerVar
159  }
160  return true;
161}
162
163/**
164 * This function returns the total amount of bytes needed by getData to save the whole content of the variables
165 * @return amount of bytes
166 */
167int Synchronisable::getSize(){
168  int tsize=0;
169  std::list<synchronisableVariable>::iterator i;
170  for(i=syncList.begin(); i!=syncList.end(); i++){
171    switch(i->type){
172    case DATA:
173      tsize+=i->size;
174      break;
175    case STRING:
176      tsize+=((std::string *)i->var)->length()+1;
177      break;
178    }
179  }
180  return tsize;
181}
182
183}
Note: See TracBrowser for help on using the repository browser.