Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 405 was 405, checked in by scheusso, 16 years ago

corrected errors

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