1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Dumeni Manatschal, (C) 2007 |
---|
24 | * Oliver Scheuss, (C) 2007 |
---|
25 | * Co-authors: |
---|
26 | * ... |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | // |
---|
31 | // C++ Implementation: synchronisable |
---|
32 | // |
---|
33 | // Description: |
---|
34 | // |
---|
35 | // |
---|
36 | // Author: Dumeni, Oliver Scheuss, (C) 2007 |
---|
37 | // |
---|
38 | // Copyright: See COPYING file that comes with this distribution |
---|
39 | // |
---|
40 | |
---|
41 | #include "Synchronisable.h" |
---|
42 | |
---|
43 | #include <string> |
---|
44 | #include <iostream> |
---|
45 | |
---|
46 | #include "core/CoreIncludes.h" |
---|
47 | |
---|
48 | namespace network |
---|
49 | { |
---|
50 | |
---|
51 | |
---|
52 | int Synchronisable::state_=1; // detemines wheter we are server (default) or client |
---|
53 | |
---|
54 | /** |
---|
55 | * Constructor: |
---|
56 | * calls registarAllVariables, that has to be implemented by the inheriting classID |
---|
57 | */ |
---|
58 | Synchronisable::Synchronisable(){ |
---|
59 | RegisterRootObject(Synchronisable); |
---|
60 | static int idCounter=0; |
---|
61 | datasize=0; |
---|
62 | objectID=idCounter++; |
---|
63 | syncList = new std::list<synchronisableVariable *>; |
---|
64 | //registerAllVariables(); |
---|
65 | } |
---|
66 | |
---|
67 | Synchronisable::~Synchronisable(){ |
---|
68 | } |
---|
69 | |
---|
70 | void Synchronisable::setClient(bool b){ |
---|
71 | if(b) // client |
---|
72 | state_=0x2; |
---|
73 | else // server |
---|
74 | state_=0x1; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * This function is used to register a variable to be synchronized |
---|
79 | * also counts the total datasize needed to save the variables |
---|
80 | * @param var pointer to the variable |
---|
81 | * @param size size of the datatype the variable consists of |
---|
82 | */ |
---|
83 | void Synchronisable::registerVar(void *var, int size, variableType t, int mode){ |
---|
84 | // create temporary synch.Var struct |
---|
85 | synchronisableVariable *temp = new synchronisableVariable; |
---|
86 | temp->size = size; |
---|
87 | temp->var = var; |
---|
88 | temp->mode = mode; |
---|
89 | temp->type = t; |
---|
90 | COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl; |
---|
91 | // increase datasize |
---|
92 | datasize+=sizeof(int)+size; |
---|
93 | //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl; |
---|
94 | COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl; |
---|
95 | syncList->push_back(temp); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * 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) |
---|
100 | * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct |
---|
101 | * structure of the bitstream: |
---|
102 | * (var1_size,var1,var2_size,var2,...) |
---|
103 | * varx_size: size = sizeof(int) |
---|
104 | * varx: size = varx_size |
---|
105 | * @return data containing all variables and their sizes |
---|
106 | */ |
---|
107 | // syncData Synchronisable::getData(){ |
---|
108 | // std::list<synchronisableVariable>::iterator i; |
---|
109 | // int totalsize=0; |
---|
110 | // //figure out size of data to be allocated |
---|
111 | // for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
112 | // // increase size (size of variable and size of size of variable ;) |
---|
113 | // if(i->type == STRING) |
---|
114 | // totalsize+=sizeof(int)+((std::string *)i->var)->length()+1; |
---|
115 | // else |
---|
116 | // totalsize+=sizeof(int)+i->size; |
---|
117 | // } |
---|
118 | // syncData retVal; |
---|
119 | // retVal.objectID=this->objectID; |
---|
120 | // retVal.classID=this->classID; |
---|
121 | // retVal.length=totalsize; |
---|
122 | // // allocate memory |
---|
123 | // retVal.data = (unsigned char *)malloc(totalsize); |
---|
124 | // // copy to location |
---|
125 | // //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP |
---|
126 | // int n=0; |
---|
127 | // for(i=syncList->begin(); n<totalsize && i!=syncList->end(); i++){ |
---|
128 | // std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int)); |
---|
129 | // n+=sizeof(int); |
---|
130 | // switch(i->type){ |
---|
131 | // case STRING: |
---|
132 | // std::memcpy(retVal.data+n, (const void *)(((std::string *)i->var)->c_str()), ((std::string *)i->var)->length()+1); |
---|
133 | // n+=((std::string *)i->var)->length()+1; |
---|
134 | // break; |
---|
135 | // case DATA: |
---|
136 | // std::memcpy(retVal.data+n, ((const void*)i->var), i->size); |
---|
137 | // n+=i->size; |
---|
138 | // break; |
---|
139 | // } |
---|
140 | // } |
---|
141 | // return retVal; |
---|
142 | // } |
---|
143 | /** |
---|
144 | * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct |
---|
145 | * Difference to the above function: |
---|
146 | * takes a pointer to already allocated memory (must have at least getSize bytes length) |
---|
147 | * structure of the bitstream: |
---|
148 | * (var1_size,var1,var2_size,var2,...) |
---|
149 | * varx_size: size = sizeof(int) |
---|
150 | * varx: size = varx_size |
---|
151 | * @return data containing all variables and their sizes |
---|
152 | */ |
---|
153 | syncData Synchronisable::getData(unsigned char *mem){ |
---|
154 | //std::cout << "inside getData" << std::endl; |
---|
155 | classID=this->getIdentifier()->getNetworkID(); |
---|
156 | std::list<synchronisableVariable *>::iterator i; |
---|
157 | syncData retVal; |
---|
158 | retVal.objectID=this->objectID; |
---|
159 | retVal.classID=this->classID; |
---|
160 | retVal.length=getSize(); |
---|
161 | COUT(5) << "Synchronisable getting data from objectID: " << retVal.objectID << " classID: " << retVal.classID << " length: " << retVal.length << std::endl; |
---|
162 | retVal.data=mem; |
---|
163 | // copy to location |
---|
164 | int n=0; //offset |
---|
165 | for(i=syncList->begin(); n<datasize && i!=syncList->end(); ++i){ |
---|
166 | //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int)); |
---|
167 | if( ((*i)->mode & state_) == 0 ) |
---|
168 | continue; // this variable should only be received |
---|
169 | switch((*i)->type){ |
---|
170 | case DATA: |
---|
171 | std::memcpy( (void *)(retVal.data+n), (void*)((*i)->var), (*i)->size); |
---|
172 | n+=(*i)->size; |
---|
173 | break; |
---|
174 | case STRING: |
---|
175 | memcpy( (void *)(retVal.data+n), (void *)&((*i)->size), sizeof(int) ); |
---|
176 | n+=sizeof(int); |
---|
177 | const char *data = ( ( *(std::string *) (*i)->var).c_str()); |
---|
178 | std::memcpy( retVal.data+n, (void*)data, (*i)->size); |
---|
179 | COUT(5) << "synchronisable: char: " << (const char *)(retVal.data+n) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl; |
---|
180 | n+=(*i)->size; |
---|
181 | break; |
---|
182 | } |
---|
183 | } |
---|
184 | return retVal; |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * This function takes a syncData struct and takes it to update the variables |
---|
189 | * @param vars data of the variables |
---|
190 | * @return true/false |
---|
191 | */ |
---|
192 | bool Synchronisable::updateData(syncData vars){ |
---|
193 | unsigned char *data=vars.data; |
---|
194 | std::list<synchronisableVariable *>::iterator i; |
---|
195 | if(syncList->empty()){ |
---|
196 | COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl; |
---|
197 | return false; |
---|
198 | } |
---|
199 | COUT(5) << "Synchronisable: objectID " << vars.objectID << ", classID " << vars.classID << " size: " << vars.length << " synchronising data" << std::endl; |
---|
200 | for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
201 | if( ((*i)->mode ^ state_) == 0 ) |
---|
202 | continue; // this variable should only be sent |
---|
203 | COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl; |
---|
204 | switch((*i)->type){ |
---|
205 | case DATA: |
---|
206 | memcpy((void*)(*i)->var, data, (*i)->size); |
---|
207 | data+=(*i)->size; |
---|
208 | break; |
---|
209 | case STRING: |
---|
210 | (*i)->size = *(int *)data; |
---|
211 | COUT(5) << "string size: " << (*i)->size << std::endl; |
---|
212 | data+=sizeof(int); |
---|
213 | *((std::string *)((*i)->var)) = std::string((const char*)data); |
---|
214 | COUT(5) << "synchronisable: char: " << (const char*)data << " string: " << std::string((const char*)data) << std::endl; |
---|
215 | data += (*i)->size; |
---|
216 | break; |
---|
217 | } |
---|
218 | } |
---|
219 | return true; |
---|
220 | } |
---|
221 | |
---|
222 | /** |
---|
223 | * This function returns the total amount of bytes needed by getData to save the whole content of the variables |
---|
224 | * @return amount of bytes |
---|
225 | */ |
---|
226 | int Synchronisable::getSize(){ |
---|
227 | int tsize=0; |
---|
228 | std::list<synchronisableVariable *>::iterator i; |
---|
229 | for(i=syncList->begin(); i!=syncList->end(); i++){ |
---|
230 | if( ((*i)->mode & state_) == 0 ) |
---|
231 | continue; // this variable should only be received, so dont add its size to the send-size |
---|
232 | switch((*i)->type){ |
---|
233 | case DATA: |
---|
234 | tsize+=(*i)->size; |
---|
235 | break; |
---|
236 | case STRING: |
---|
237 | tsize+=sizeof(int); |
---|
238 | (*i)->size=((std::string *)(*i)->var)->length()+1; |
---|
239 | COUT(5) << "String size: " << (*i)->size << std::endl; |
---|
240 | tsize+=(*i)->size; |
---|
241 | break; |
---|
242 | } |
---|
243 | } |
---|
244 | return tsize; |
---|
245 | } |
---|
246 | |
---|
247 | void Synchronisable::setBacksync(bool sync){ |
---|
248 | backsync_=sync; |
---|
249 | } |
---|
250 | |
---|
251 | bool Synchronisable::getBacksync(){ |
---|
252 | return backsync_; |
---|
253 | } |
---|
254 | |
---|
255 | } |
---|