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 | syncList = new std::list<synchronisableVariable *>; |
---|
30 | //registerAllVariables(); |
---|
31 | } |
---|
32 | |
---|
33 | Synchronisable::~Synchronisable(){ |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | * This function is used to register a variable to be synchronized |
---|
38 | * also counts the total datasize needed to save the variables |
---|
39 | * @param var pointer to the variable |
---|
40 | * @param size size of the datatype the variable consists of |
---|
41 | */ |
---|
42 | void Synchronisable::registerVar(const void *var, int size, variableType t){ |
---|
43 | // create temporary synch.Var struct |
---|
44 | synchronisableVariable *temp = new synchronisableVariable; |
---|
45 | temp->size = size; |
---|
46 | temp->var = var; |
---|
47 | temp->type = t; |
---|
48 | COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl; |
---|
49 | // increase datasize |
---|
50 | datasize+=sizeof(int)+size; |
---|
51 | //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl; |
---|
52 | COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl; |
---|
53 | syncList->push_back(temp); |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * 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) |
---|
58 | * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct |
---|
59 | * structure of the bitstream: |
---|
60 | * (var1_size,var1,var2_size,var2,...) |
---|
61 | * varx_size: size = sizeof(int) |
---|
62 | * varx: size = varx_size |
---|
63 | * @return data containing all variables and their sizes |
---|
64 | */ |
---|
65 | // syncData Synchronisable::getData(){ |
---|
66 | // std::list<synchronisableVariable>::iterator i; |
---|
67 | // int totalsize=0; |
---|
68 | // //figure out size of data to be allocated |
---|
69 | // for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
70 | // // increase size (size of variable and size of size of variable ;) |
---|
71 | // if(i->type == STRING) |
---|
72 | // totalsize+=sizeof(int)+((std::string *)i->var)->length()+1; |
---|
73 | // else |
---|
74 | // totalsize+=sizeof(int)+i->size; |
---|
75 | // } |
---|
76 | // syncData retVal; |
---|
77 | // retVal.objectID=this->objectID; |
---|
78 | // retVal.classID=this->classID; |
---|
79 | // retVal.length=totalsize; |
---|
80 | // // allocate memory |
---|
81 | // retVal.data = (unsigned char *)malloc(totalsize); |
---|
82 | // // copy to location |
---|
83 | // //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP |
---|
84 | // int n=0; |
---|
85 | // for(i=syncList->begin(); n<totalsize && i!=syncList->end(); i++){ |
---|
86 | // std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int)); |
---|
87 | // n+=sizeof(int); |
---|
88 | // switch(i->type){ |
---|
89 | // case STRING: |
---|
90 | // std::memcpy(retVal.data+n, (const void *)(((std::string *)i->var)->c_str()), ((std::string *)i->var)->length()+1); |
---|
91 | // n+=((std::string *)i->var)->length()+1; |
---|
92 | // break; |
---|
93 | // case DATA: |
---|
94 | // std::memcpy(retVal.data+n, ((const void*)i->var), i->size); |
---|
95 | // n+=i->size; |
---|
96 | // break; |
---|
97 | // } |
---|
98 | // } |
---|
99 | // return retVal; |
---|
100 | // } |
---|
101 | /** |
---|
102 | * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct |
---|
103 | * Difference to the above function: |
---|
104 | * takes a pointer to already allocated memory (must have at least getSize bytes length) |
---|
105 | * structure of the bitstream: |
---|
106 | * (var1_size,var1,var2_size,var2,...) |
---|
107 | * varx_size: size = sizeof(int) |
---|
108 | * varx: size = varx_size |
---|
109 | * @return data containing all variables and their sizes |
---|
110 | */ |
---|
111 | syncData Synchronisable::getData(unsigned char *mem){ |
---|
112 | //std::cout << "inside getData" << std::endl; |
---|
113 | std::list<synchronisableVariable *>::iterator i; |
---|
114 | syncData retVal; |
---|
115 | retVal.objectID=this->objectID; |
---|
116 | retVal.classID=this->classID; |
---|
117 | retVal.length=getSize(); |
---|
118 | retVal.data=mem; |
---|
119 | // copy to location |
---|
120 | int n=0; //offset |
---|
121 | for(i=syncList->begin(); n<datasize && i!=syncList->end(); ++i){ |
---|
122 | //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int)); |
---|
123 | memcpy( (void *)(retVal.data+n), (const void *)&((*i)->size), sizeof(int) ); |
---|
124 | n+=sizeof(int); |
---|
125 | switch((*i)->type){ |
---|
126 | case DATA: |
---|
127 | std::memcpy( (void *)(retVal.data+n), (const void*)((*i)->var), (*i)->size); |
---|
128 | n+=(*i)->size; |
---|
129 | break; |
---|
130 | case STRING: |
---|
131 | std::memcpy( retVal.data+n, (const void*)( ( (std::string *) (*i)->var)->c_str()), (*i)->size); |
---|
132 | n+=(*i)->size; |
---|
133 | break; |
---|
134 | } |
---|
135 | } |
---|
136 | return retVal; |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * This function takes a syncData struct and takes it to update the variables |
---|
141 | * @param vars data of the variables |
---|
142 | * @return true/false |
---|
143 | */ |
---|
144 | bool Synchronisable::updateData(syncData vars){ |
---|
145 | unsigned char *data=vars.data; |
---|
146 | std::list<synchronisableVariable *>::iterator i; |
---|
147 | if(syncList->empty()){ |
---|
148 | COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl; |
---|
149 | return false; |
---|
150 | } |
---|
151 | COUT(5) << "Synchronisable: objectID " << objectID << ", classID " << classID << " synchronising data" << std::endl; |
---|
152 | for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
153 | COUT(5) << "element size: " << (*i)->size << " type: " << (*i)->type << std::endl; |
---|
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 | (*i)->size=((std::string *)(*i)->var)->length()+1; |
---|
190 | tsize+=(*i)->size; |
---|
191 | break; |
---|
192 | } |
---|
193 | } |
---|
194 | return tsize; |
---|
195 | } |
---|
196 | |
---|
197 | } |
---|