Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/audio/src/network/Synchronisable.cc @ 335

Last change on this file since 335 was 285, checked in by nicolasc, 17 years ago

cleaned up network, builds with CML

File size: 4.4 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
14namespace network {
15
16/**
17 * Constructor:
18 * calls registarAllVariables, that has to be implemented by the inheriting classID
19 */
20Synchronisable::Synchronisable()
21{
22  datasize=0;
23  registerAllVariables();
24}
25
26
27Synchronisable::~Synchronisable()
28{
29
30}
31
32/**
33 * This function is used to register a variable to be synchronized
34 * also counts the total datasize needed to save the variables
35 * @param var pointer to the variable
36 * @param size size of the datatype the variable consists of
37 */
38void Synchronisable::registerVar(const void *var, int size){
39  // create temporary synch.Var struct
40  synchronisableVariable temp={size, var};
41  // increase datasize
42  datasize+=sizeof(int)+size;
43  // push temp to syncList (at the bottom)
44  syncList.push_back(temp);
45}
46
47/**
48 * 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)
49 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
50 * structure of the bitstream:
51 * (var1_size,var1,var2_size,var2,...)
52 * varx_size: size = sizeof(int)
53 * varx: size = varx_size
54 * @return data containing all variables and their sizes
55 */
56syncData Synchronisable::getData(){
57  std::list<synchronisableVariable>::iterator i;
58  int totalsize=0;
59  //figure out size of data to be allocated
60  for(i=syncList.begin(); i!=syncList.end(); i++){
61    // increase size (size of variable and size of size of variable ;)
62    totalsize+=sizeof(int)+i->size;
63  }
64  syncData retVal;
65  retVal.objectID=this->objectID;
66  retVal.classID=this->classID;
67  retVal.length=totalsize;
68  // allocate memory
69  retVal.data = (unsigned char *)malloc(totalsize);
70  // copy to location
71  //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP
72  int n=0;
73  for(i=syncList.begin(); n<totalsize && i!=syncList.end(); i++){
74        //CHANGED: i->size TO (const void*)(&(i->size)) memcpy WANTS A CONST VOID* SO CONVERT INT TO CONST VOID*
75    std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int));
76    n+=sizeof(int);
77    //CHANGED: i->var TO (const void*)(&(i->var)) SINCE var IS A POINTER, NO & BEFORE i
78    std::memcpy(retVal.data+n, (const void*)(i->var), i->size);
79    n+=i->size;
80  }
81  return retVal;
82}
83/**
84 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
85 * Difference to the above function:
86 * takes a pointer to already allocated memory (must have at least getSize bytes length)
87 * structure of the bitstream:
88 * (var1_size,var1,var2_size,var2,...)
89 * varx_size: size = sizeof(int)
90 * varx: size = varx_size
91 * @return data containing all variables and their sizes
92 */
93syncData Synchronisable::getData(unsigned char *mem){
94  std::list<synchronisableVariable>::iterator i;
95  syncData retVal;
96  retVal.objectID=this->objectID;
97  retVal.classID=this->classID;
98  retVal.length=datasize;
99  retVal.data=mem;
100  // copy to location
101  //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP
102  int n=0;
103  for(i=syncList.begin(); n<datasize && i!=syncList.end(); i++){
104        //CHANGED: i->size TO (const void*)(&(i->size)) memcpy WANTS A CONST VOID* SO CONVERT INT TO CONST VOID*
105    std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int));
106    n+=sizeof(int);
107    //CHANGED: i->var TO (const void*)(&(i->var)) SINCE var IS A POINTER, NO & BEFORE i
108    std::memcpy(retVal.data+n, (const void*)(i->var), i->size);
109    n+=i->size;
110  }
111  return retVal;
112}
113
114/**
115 * This function takes a syncData struct and takes it to update the variables
116 * @param vars data of the variables
117 * @return true/false
118 */
119bool Synchronisable::updateData(syncData vars){
120  unsigned char *data=vars.data;
121  std::list<synchronisableVariable>::iterator i;
122  for(i=syncList.begin(); i!=syncList.end(); i++){
123    if((int)*data==i->size){
124      data+=sizeof(int);
125      //CHANGED: THIS FROM i->var TO (void*)i->var SINCE var IS A CONST VOID* AND memcpy NEEDS A VOID* AS FIRST ARGUMENT
126      memcpy((void*)i->var, data, i->size);
127      data+=i->size;
128    } else
129      return false; //there was some problem with registerVar
130  }
131}
132
133/**
134 * This function returns the total amount of bytes needed by getData to save the whole content of the variables
135 * @return amount of bytes
136 */
137int Synchronisable::getSize(){
138  return datasize;
139}
140
141}
Note: See TracBrowser for help on using the repository browser.