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 <string> |
---|
13 | #include <iostream> |
---|
14 | |
---|
15 | #include "Synchronisable.h" |
---|
16 | #include "core/CoreIncludes.h" |
---|
17 | |
---|
18 | namespace network |
---|
19 | { |
---|
20 | /** |
---|
21 | * Constructor: |
---|
22 | * calls registarAllVariables, that has to be implemented by the inheriting classID |
---|
23 | */ |
---|
24 | Synchronisable::Synchronisable(){ |
---|
25 | RegisterRootObject(Synchronisable); |
---|
26 | static int idCounter=0; |
---|
27 | datasize=0; |
---|
28 | objectID=idCounter++; |
---|
29 | //registerAllVariables(); |
---|
30 | } |
---|
31 | |
---|
32 | Synchronisable::~Synchronisable(){ |
---|
33 | } |
---|
34 | |
---|
35 | /** |
---|
36 | * This function is used to register a variable to be synchronized |
---|
37 | * also counts the total datasize needed to save the variables |
---|
38 | * @param var pointer to the variable |
---|
39 | * @param size size of the datatype the variable consists of |
---|
40 | */ |
---|
41 | void Synchronisable::registerVar(const void *var, int size, variableType t){ |
---|
42 | // create temporary synch.Var struct |
---|
43 | synchronisableVariable *temp = new synchronisableVariable; |
---|
44 | temp->size = size; |
---|
45 | temp->var = var; |
---|
46 | temp->type = t; |
---|
47 | COUT(5) << "registering var with size: " << temp->size << " and type: " << temp->type << std::endl; |
---|
48 | // increase datasize |
---|
49 | datasize+=sizeof(int)+size; |
---|
50 | //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl; |
---|
51 | syncList.push_back(temp); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * 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) |
---|
56 | * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct |
---|
57 | * structure of the bitstream: |
---|
58 | * (var1_size,var1,var2_size,var2,...) |
---|
59 | * varx_size: size = sizeof(int) |
---|
60 | * varx: size = varx_size |
---|
61 | * @return data containing all variables and their sizes |
---|
62 | */ |
---|
63 | // syncData Synchronisable::getData(){ |
---|
64 | // std::list<synchronisableVariable>::iterator i; |
---|
65 | // int totalsize=0; |
---|
66 | // //figure out size of data to be allocated |
---|
67 | // for(i=syncList.begin(); i!=syncList.end(); i++){ |
---|
68 | // // increase size (size of variable and size of size of variable ;) |
---|
69 | // if(i->type == STRING) |
---|
70 | // totalsize+=sizeof(int)+((std::string *)i->var)->length()+1; |
---|
71 | // else |
---|
72 | // totalsize+=sizeof(int)+i->size; |
---|
73 | // } |
---|
74 | // syncData retVal; |
---|
75 | // retVal.objectID=this->objectID; |
---|
76 | // retVal.classID=this->classID; |
---|
77 | // retVal.length=totalsize; |
---|
78 | // // allocate memory |
---|
79 | // retVal.data = (unsigned char *)malloc(totalsize); |
---|
80 | // // copy to location |
---|
81 | // //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP |
---|
82 | // int n=0; |
---|
83 | // for(i=syncList.begin(); n<totalsize && i!=syncList.end(); i++){ |
---|
84 | // std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int)); |
---|
85 | // n+=sizeof(int); |
---|
86 | // switch(i->type){ |
---|
87 | // case STRING: |
---|
88 | // std::memcpy(retVal.data+n, (const void *)(((std::string *)i->var)->c_str()), ((std::string *)i->var)->length()+1); |
---|
89 | // n+=((std::string *)i->var)->length()+1; |
---|
90 | // break; |
---|
91 | // case DATA: |
---|
92 | // std::memcpy(retVal.data+n, ((const void*)i->var), i->size); |
---|
93 | // n+=i->size; |
---|
94 | // break; |
---|
95 | // } |
---|
96 | // } |
---|
97 | // return retVal; |
---|
98 | // } |
---|
99 | /** |
---|
100 | * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct |
---|
101 | * Difference to the above function: |
---|
102 | * takes a pointer to already allocated memory (must have at least getSize bytes length) |
---|
103 | * structure of the bitstream: |
---|
104 | * (var1_size,var1,var2_size,var2,...) |
---|
105 | * varx_size: size = sizeof(int) |
---|
106 | * varx: size = varx_size |
---|
107 | * @return data containing all variables and their sizes |
---|
108 | */ |
---|
109 | syncData Synchronisable::getData(unsigned char *mem){ |
---|
110 | //std::cout << "inside getData" << std::endl; |
---|
111 | std::list<synchronisableVariable *>::iterator i; |
---|
112 | syncData retVal; |
---|
113 | retVal.objectID=this->objectID; |
---|
114 | retVal.classID=this->classID; |
---|
115 | retVal.length=getSize(); |
---|
116 | retVal.data=mem; |
---|
117 | // copy to location |
---|
118 | int n=0; |
---|
119 | for(i=syncList.begin(); n<datasize && i!=syncList.end(); ++i){ |
---|
120 | //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int)); |
---|
121 | memcpy( (void *)(retVal.data+n), (const void *)&((*i)->size), sizeof(int) ); |
---|
122 | n+=sizeof(int); |
---|
123 | switch((*i)->type){ |
---|
124 | case DATA: |
---|
125 | std::memcpy( (void *)(retVal.data+n), (const void*)((*i)->var), (*i)->size); |
---|
126 | n+=(*i)->size; |
---|
127 | break; |
---|
128 | case STRING: |
---|
129 | std::memcpy( retVal.data+n, (const void*)( ( (std::string *) (*i)->var)->c_str()), ( (std::string *)(*i)->var )->length()+1); |
---|
130 | n+=((std::string *) (*i)->var)->length()+1; |
---|
131 | break; |
---|
132 | } |
---|
133 | } |
---|
134 | return retVal; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * This function takes a syncData struct and takes it to update the variables |
---|
139 | * @param vars data of the variables |
---|
140 | * @return true/false |
---|
141 | */ |
---|
142 | bool Synchronisable::updateData(syncData vars){ |
---|
143 | unsigned char *data=vars.data; |
---|
144 | std::list<synchronisableVariable *>::iterator i; |
---|
145 | if(syncList.empty()){ |
---|
146 | COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl; |
---|
147 | return false; |
---|
148 | } |
---|
149 | COUT(5) << "Synchronisable: synchronising data" << std::endl; |
---|
150 | i=syncList.begin(); |
---|
151 | COUT(5) << "element size: " << (*syncList.begin())->size << std::endl; |
---|
152 | COUT(5) << "*i.size" << (*i)->size << std::endl; |
---|
153 | for(; i!=syncList.end(); i++){ |
---|
154 | if(*(int *)data==(*i)->size || (*i)->type==STRING){ |
---|
155 | switch((*i)->type){ |
---|
156 | case DATA: |
---|
157 | data+=sizeof(int); |
---|
158 | memcpy((void*)(*i)->var, data, (*i)->size); |
---|
159 | data+=(*i)->size; |
---|
160 | break; |
---|
161 | case STRING: |
---|
162 | (*i)->size = *(int *)data; |
---|
163 | data+=sizeof(int); |
---|
164 | *((std::string *)((*i)->var)) = std::string((const char*)data); |
---|
165 | data += (*i)->size; |
---|
166 | break; |
---|
167 | } |
---|
168 | } else |
---|
169 | return false; //there was some problem with registerVar |
---|
170 | } |
---|
171 | return true; |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * This function returns the total amount of bytes needed by getData to save the whole content of the variables |
---|
176 | * @return amount of bytes |
---|
177 | */ |
---|
178 | int Synchronisable::getSize(){ |
---|
179 | int tsize=0; |
---|
180 | std::list<synchronisableVariable *>::iterator i; |
---|
181 | for(i=syncList.begin(); i!=syncList.end(); i++){ |
---|
182 | switch((*i)->type){ |
---|
183 | case DATA: |
---|
184 | tsize+=sizeof(int); |
---|
185 | tsize+=(*i)->size; |
---|
186 | break; |
---|
187 | case STRING: |
---|
188 | tsize+=sizeof(int); |
---|
189 | tsize+=((std::string *)(*i)->var)->length()+1; |
---|
190 | break; |
---|
191 | } |
---|
192 | } |
---|
193 | return tsize; |
---|
194 | } |
---|
195 | |
---|
196 | } |
---|