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 | |
---|
16 | namespace network { |
---|
17 | |
---|
18 | /** |
---|
19 | * Constructor: |
---|
20 | * calls registarAllVariables, that has to be implemented by the inheriting classID |
---|
21 | */ |
---|
22 | Synchronisable::Synchronisable() |
---|
23 | { |
---|
24 | RegisterRootObject(Synchronisable); |
---|
25 | static int idCounter=0; |
---|
26 | datasize=0; |
---|
27 | objectID=idCounter++; |
---|
28 | //registerAllVariables(); |
---|
29 | } |
---|
30 | |
---|
31 | |
---|
32 | Synchronisable::~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 | */ |
---|
43 | void Synchronisable::registerVar(const void *var, int size){ |
---|
44 | // create temporary synch.Var struct |
---|
45 | synchronisableVariable temp={size, var}; |
---|
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 | */ |
---|
61 | syncData 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 |
---|
76 | //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP |
---|
77 | int n=0; |
---|
78 | for(i=syncList.begin(); n<totalsize && i!=syncList.end(); i++){ |
---|
79 | //CHANGED: i->size TO (const void*)(&(i->size)) memcpy WANTS A CONST VOID* SO CONVERT INT TO CONST VOID* |
---|
80 | std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int)); |
---|
81 | n+=sizeof(int); |
---|
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); |
---|
84 | n+=i->size; |
---|
85 | } |
---|
86 | return retVal; |
---|
87 | } |
---|
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 | */ |
---|
98 | syncData 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++){ |
---|
109 | //CHANGED: i->size TO (const void*)(&(i->size)) memcpy WANTS A CONST VOID* SO CONVERT INT TO CONST VOID* |
---|
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 | */ |
---|
124 | bool 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); |
---|
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); |
---|
132 | data+=i->size; |
---|
133 | } else |
---|
134 | return false; //there was some problem with registerVar |
---|
135 | } |
---|
136 | return true; |
---|
137 | } |
---|
138 | |
---|
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 | */ |
---|
143 | int Synchronisable::getSize(){ |
---|
144 | return datasize; |
---|
145 | } |
---|
146 | |
---|
147 | } |
---|