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 | * Oliver Scheuss, (C) 2008 |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "Gamestate.h" |
---|
30 | #include "network/ClientInformation.h" |
---|
31 | #include "network/GamestateHandler.h" |
---|
32 | |
---|
33 | #include <zlib.h> |
---|
34 | #include <assert.h> |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | namespace network { |
---|
39 | |
---|
40 | namespace packet { |
---|
41 | |
---|
42 | |
---|
43 | #define GAMESTATE_START(data) data + sizeof(GamestateHeader) |
---|
44 | #define GAMESTATE_HEADER(data) ((GamestateHeader *)data) |
---|
45 | #define HEADER GAMESTATE_HEADER(data_) |
---|
46 | |
---|
47 | Gamestate::Gamestate() |
---|
48 | { |
---|
49 | } |
---|
50 | |
---|
51 | Gamestate::Gamestate(unsigned char *data, int clientID): |
---|
52 | Packet(data, clientID) |
---|
53 | { |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | Gamestate::~Gamestate() |
---|
58 | { |
---|
59 | } |
---|
60 | |
---|
61 | bool Gamestate::collectData(int id, int mode) |
---|
62 | { |
---|
63 | int tempsize=0, currentsize=0; |
---|
64 | assert(data_==0 /*&& bs_==0*/); |
---|
65 | int size = calcGamestateSize(mode); |
---|
66 | |
---|
67 | COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl; |
---|
68 | //retval->data = (unsigned char*)malloc(size); |
---|
69 | if(size==0) |
---|
70 | return false; |
---|
71 | data_ = new unsigned char[size + sizeof(GamestateHeader)]; |
---|
72 | //bs_ = new Bytestream(data_+sizeof(GamestateHeader), size); |
---|
73 | if(!data_){ |
---|
74 | COUT(2) << "GameStateManager: could not allocate memory" << std::endl; |
---|
75 | return false; |
---|
76 | } |
---|
77 | |
---|
78 | //start collect data synchronisable by synchronisable |
---|
79 | unsigned char *mem=data_; |
---|
80 | mem+=sizeof(GamestateHeader); |
---|
81 | orxonox::Iterator<Synchronisable> it; |
---|
82 | for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){ |
---|
83 | tempsize=it->getSize2(mode); |
---|
84 | |
---|
85 | if(currentsize+tempsize > size){ |
---|
86 | // start allocate additional memory |
---|
87 | COUT(3) << "G.St.Man: need additional memory" << std::endl; |
---|
88 | orxonox::Iterator<Synchronisable> temp = it; |
---|
89 | int addsize=tempsize; |
---|
90 | while(++temp) |
---|
91 | addsize+=temp->getSize2(mode); |
---|
92 | data_ = (unsigned char *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize); |
---|
93 | if(!data_) |
---|
94 | return false; |
---|
95 | size = currentsize+addsize; |
---|
96 | }// stop allocate additional memory |
---|
97 | |
---|
98 | if(!it->getData2(mem, mode)) |
---|
99 | return false; // mem pointer gets automatically increased because of call by reference |
---|
100 | // increase size counter by size of current synchronisable |
---|
101 | currentsize+=tempsize; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | //start write gamestate header |
---|
106 | HEADER->packetType = ENUM::Gamestate; |
---|
107 | assert( *(ENUM::Type *)(data_) == ENUM::Gamestate); |
---|
108 | HEADER->normsize = currentsize; |
---|
109 | HEADER->id = id; |
---|
110 | HEADER->diffed = false; |
---|
111 | HEADER->complete = true; |
---|
112 | HEADER->compressed = false; |
---|
113 | //stop write gamestate header |
---|
114 | |
---|
115 | COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl; |
---|
116 | COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl; |
---|
117 | return true; |
---|
118 | } |
---|
119 | |
---|
120 | bool Gamestate::spreadData(int mode) |
---|
121 | { |
---|
122 | assert(data_ && !HEADER->compressed && !HEADER->diffed); |
---|
123 | unsigned int size, objectID, classID; |
---|
124 | unsigned char *mem=data_+sizeof(GamestateHeader); |
---|
125 | // get the start of the Synchronisable list |
---|
126 | orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start(); |
---|
127 | |
---|
128 | while(mem < data_+sizeof(GamestateHeader)+HEADER->normsize){ |
---|
129 | // extract synchronisable header |
---|
130 | size = *(unsigned int *)mem; |
---|
131 | objectID = *(unsigned int*)(mem+sizeof(unsigned int)); |
---|
132 | classID = *(unsigned int*)(mem+2*sizeof(unsigned int)); |
---|
133 | |
---|
134 | if(!it || it->objectID!=objectID || it->classID!=classID){ |
---|
135 | // bad luck ;) |
---|
136 | // delete the synchronisable (obviously seems to be deleted on the server) |
---|
137 | while(it && it->objectID!=objectID) |
---|
138 | removeObject(it); |
---|
139 | |
---|
140 | if(!it){ |
---|
141 | //fabricate the new synchronisable |
---|
142 | if(!Synchronisable::fabricate(mem, mode)) |
---|
143 | /*return false*/; |
---|
144 | it=orxonox::ObjectList<Synchronisable>::end(); |
---|
145 | } |
---|
146 | } else |
---|
147 | { |
---|
148 | // we have our object |
---|
149 | if(! it->updateData(mem, mode)) |
---|
150 | { |
---|
151 | COUT(1) << "We couldn't update objectID: " \ |
---|
152 | << objectID << "; classID: " << classID << std::endl; |
---|
153 | } |
---|
154 | } |
---|
155 | ++it; |
---|
156 | } |
---|
157 | |
---|
158 | return true; |
---|
159 | } |
---|
160 | |
---|
161 | int Gamestate::getID(){ |
---|
162 | return HEADER->id; |
---|
163 | } |
---|
164 | |
---|
165 | unsigned int Gamestate::getSize() const |
---|
166 | { |
---|
167 | assert(data_); |
---|
168 | if(HEADER->compressed) |
---|
169 | return HEADER->compsize+sizeof(GamestateHeader); |
---|
170 | else |
---|
171 | { |
---|
172 | return HEADER->normsize+sizeof(GamestateHeader); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | bool Gamestate::process() |
---|
177 | { |
---|
178 | return GamestateHandler::addGamestate(this, getClientID()); |
---|
179 | } |
---|
180 | |
---|
181 | bool Gamestate::compressData() |
---|
182 | { |
---|
183 | assert(HEADER); |
---|
184 | uLongf buffer = (uLongf)((HEADER->normsize + 12)*1.01)+1; |
---|
185 | if(buffer==0) |
---|
186 | return false; |
---|
187 | |
---|
188 | unsigned char *ndata = new unsigned char[buffer+sizeof(GamestateHeader)]; |
---|
189 | unsigned char *dest = GAMESTATE_START(ndata); |
---|
190 | int retval; |
---|
191 | retval = compress( dest, &buffer, GAMESTATE_START(data_), (uLong)(HEADER->normsize) ); |
---|
192 | switch ( retval ) { |
---|
193 | case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break; |
---|
194 | case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; |
---|
195 | return false; |
---|
196 | case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; |
---|
197 | return false; |
---|
198 | case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; |
---|
199 | return false; |
---|
200 | } |
---|
201 | |
---|
202 | //copy and modify header |
---|
203 | HEADER->compsize = buffer; |
---|
204 | HEADER->compressed = true; |
---|
205 | *GAMESTATE_HEADER(ndata) = *HEADER; |
---|
206 | //delete old data |
---|
207 | delete[] data_; |
---|
208 | //save new data |
---|
209 | data_ = ndata; |
---|
210 | assert(HEADER->compressed); |
---|
211 | COUT(3) << "gamestate compress normsize: " << HEADER->normsize << " compsize: " << HEADER->compsize << std::endl; |
---|
212 | return true; |
---|
213 | } |
---|
214 | bool Gamestate::decompressData() |
---|
215 | { |
---|
216 | assert(HEADER->compressed); |
---|
217 | //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl; |
---|
218 | int normsize = HEADER->normsize; |
---|
219 | int compsize = HEADER->compsize; |
---|
220 | int bufsize; |
---|
221 | if(normsize < compsize) |
---|
222 | bufsize = compsize; |
---|
223 | else |
---|
224 | bufsize = normsize; |
---|
225 | if(bufsize==0) |
---|
226 | return NULL; |
---|
227 | unsigned char *ndata = new unsigned char[bufsize + sizeof(GamestateHeader)]; |
---|
228 | unsigned char *dest = ndata + sizeof(GamestateHeader); |
---|
229 | int retval; |
---|
230 | uLongf length=normsize; |
---|
231 | retval = uncompress( dest, &length, data_+sizeof(GamestateHeader), (uLong)compsize ); |
---|
232 | switch ( retval ) { |
---|
233 | case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break; |
---|
234 | case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false; |
---|
235 | case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false; |
---|
236 | case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false; |
---|
237 | } |
---|
238 | |
---|
239 | HEADER->compressed = false; |
---|
240 | //copy over the header |
---|
241 | *GAMESTATE_HEADER(ndata) = *HEADER; |
---|
242 | //delete old (compressed data) |
---|
243 | delete[] data_; |
---|
244 | //set new pointers and create bytestream |
---|
245 | data_ = ndata; |
---|
246 | //bs_ = new Bytestream(getGs(), GAMESTATE_HEADER->normsize); |
---|
247 | |
---|
248 | return true; |
---|
249 | } |
---|
250 | |
---|
251 | Gamestate *Gamestate::diff(Gamestate *base) |
---|
252 | { |
---|
253 | //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; |
---|
254 | unsigned char *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_); |
---|
255 | unsigned int of=0; // pointers offset |
---|
256 | unsigned int dest_length=0; |
---|
257 | dest_length=HEADER->normsize; |
---|
258 | if(dest_length==0) |
---|
259 | return NULL; |
---|
260 | unsigned char *ndata = new unsigned char[dest_length*sizeof(unsigned char)+sizeof(GamestateHeader)]; |
---|
261 | unsigned char *dest = ndata + sizeof(GamestateHeader); |
---|
262 | while(of < GAMESTATE_HEADER(base->data_)->normsize && of < HEADER->normsize){ |
---|
263 | *(dest+of)=*(basep+of)^*(gs+of); // do the xor |
---|
264 | ++of; |
---|
265 | } |
---|
266 | if(GAMESTATE_HEADER(base->data_)->normsize!=HEADER->normsize){ |
---|
267 | unsigned char n=0; |
---|
268 | if(GAMESTATE_HEADER(base->data_)->normsize < HEADER->normsize){ |
---|
269 | while(of<dest_length){ |
---|
270 | *(dest+of)=n^*(gs+of); |
---|
271 | of++; |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | *GAMESTATE_HEADER(ndata) = *HEADER; |
---|
277 | GAMESTATE_HEADER(ndata)->diffed = true; |
---|
278 | Gamestate *g = new Gamestate(ndata, 0); |
---|
279 | return g; |
---|
280 | } |
---|
281 | |
---|
282 | Gamestate *Gamestate::undiff(Gamestate *base) |
---|
283 | { |
---|
284 | assert(this && base); |
---|
285 | assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed); |
---|
286 | //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; |
---|
287 | unsigned char *basep = GAMESTATE_START(base->data_); |
---|
288 | unsigned char *gs = GAMESTATE_START(this->data_); |
---|
289 | unsigned int of=0; // pointers offset |
---|
290 | unsigned int dest_length=0; |
---|
291 | dest_length=HEADER->normsize; |
---|
292 | if(dest_length==0) |
---|
293 | return NULL; |
---|
294 | unsigned char *ndata = new unsigned char[dest_length*sizeof(unsigned char)+sizeof(GamestateHeader)]; |
---|
295 | unsigned char *dest = ndata + sizeof(GamestateHeader); |
---|
296 | while(of < GAMESTATE_HEADER(base->data_)->normsize && of < HEADER->normsize){ |
---|
297 | *(dest+of)=*(basep+of)^*(gs+of); // do the xor |
---|
298 | ++of; |
---|
299 | } |
---|
300 | if(GAMESTATE_HEADER(base->data_)->normsize!=HEADER->normsize){ |
---|
301 | unsigned char n=0; |
---|
302 | if(GAMESTATE_HEADER(base->data_)->normsize < HEADER->normsize){ |
---|
303 | while(of < dest_length){ |
---|
304 | *(dest+of)=n^*(gs+of); |
---|
305 | of++; |
---|
306 | } |
---|
307 | } |
---|
308 | } |
---|
309 | *GAMESTATE_HEADER(ndata) = *HEADER; |
---|
310 | GAMESTATE_HEADER(ndata)->diffed = false; |
---|
311 | Gamestate *g = new Gamestate(ndata, 0); |
---|
312 | return g; |
---|
313 | } |
---|
314 | |
---|
315 | |
---|
316 | unsigned int Gamestate::calcGamestateSize(int mode) |
---|
317 | { |
---|
318 | int size=0; |
---|
319 | // get the start of the Synchronisable list |
---|
320 | orxonox::Iterator<Synchronisable> it; |
---|
321 | // get total size of gamestate |
---|
322 | for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it) |
---|
323 | size+=it->getSize2(mode); // size of the actual data of the synchronisable |
---|
324 | // size+=sizeof(GamestateHeader); |
---|
325 | return size; |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * This function removes a Synchronisable out of the universe |
---|
330 | * @param it iterator of the list pointing to the object |
---|
331 | * @return iterator pointing to the next object in the list |
---|
332 | */ |
---|
333 | void Gamestate::removeObject(orxonox::Iterator<Synchronisable> &it) { |
---|
334 | orxonox::Iterator<Synchronisable> temp=it; |
---|
335 | ++it; |
---|
336 | delete *temp; |
---|
337 | } |
---|
338 | |
---|
339 | bool Gamestate::isDiffed(){ |
---|
340 | return HEADER->diffed; |
---|
341 | } |
---|
342 | |
---|
343 | int Gamestate::getBaseID(){ |
---|
344 | return HEADER->base_id; |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | } |
---|