Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 555 was 531, checked in by scheusso, 17 years ago

started implementing presentation ;)

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