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 | |
---|
14 | namespace network { |
---|
15 | |
---|
16 | /** |
---|
17 | * Constructor: |
---|
18 | * calls registarAllVariables, that has to be implemented by the inheriting classID |
---|
19 | */ |
---|
20 | Synchronisable::Synchronisable() |
---|
21 | { |
---|
22 | static int idCounter=0; |
---|
23 | datasize=0; |
---|
24 | objectID=idCounter++; |
---|
25 | //registerAllVariables(); |
---|
26 | } |
---|
27 | |
---|
28 | |
---|
29 | Synchronisable::~Synchronisable() |
---|
30 | { |
---|
31 | |
---|
32 | } |
---|
33 | |
---|
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 | */ |
---|
40 | void Synchronisable::registerVar(const void *var, int size){ |
---|
41 | // create temporary synch.Var struct |
---|
42 | synchronisableVariable temp={size, var}; |
---|
43 | // increase datasize |
---|
44 | datasize+=sizeof(int)+size; |
---|
45 | // push temp to syncList (at the bottom) |
---|
46 | syncList.push_back(temp); |
---|
47 | } |
---|
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 | */ |
---|
58 | syncData 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 |
---|
73 | //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP |
---|
74 | int n=0; |
---|
75 | for(i=syncList.begin(); n<totalsize && i!=syncList.end(); i++){ |
---|
76 | //CHANGED: i->size TO (const void*)(&(i->size)) memcpy WANTS A CONST VOID* SO CONVERT INT TO CONST VOID* |
---|
77 | std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int)); |
---|
78 | n+=sizeof(int); |
---|
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); |
---|
81 | n+=i->size; |
---|
82 | } |
---|
83 | return retVal; |
---|
84 | } |
---|
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 | */ |
---|
95 | syncData 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++){ |
---|
106 | //CHANGED: i->size TO (const void*)(&(i->size)) memcpy WANTS A CONST VOID* SO CONVERT INT TO CONST VOID* |
---|
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 | */ |
---|
121 | bool 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); |
---|
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); |
---|
129 | data+=i->size; |
---|
130 | } else |
---|
131 | return false; //there was some problem with registerVar |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * This function returns the total amount of bytes needed by getData to save the whole content of the variables |
---|
137 | * @return amount of bytes |
---|
138 | */ |
---|
139 | int Synchronisable::getSize(){ |
---|
140 | return datasize; |
---|
141 | } |
---|
142 | |
---|
143 | } |
---|