Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/network/Synchronisable.cc @ 882

Last change on this file since 882 was 871, checked in by landauf, 17 years ago

merged core branch to trunk

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