1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: claudio |
---|
13 | co-programmer: |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
18 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
19 | */ |
---|
20 | #define DEBUG_MODULE_NETWORK |
---|
21 | |
---|
22 | |
---|
23 | #include "base_object.h" |
---|
24 | #include "network_protocol.h" |
---|
25 | #include "udp_socket.h" |
---|
26 | #include "udp_server_socket.h" |
---|
27 | #include "connection_monitor.h" |
---|
28 | #include "synchronizeable.h" |
---|
29 | #include "network_game_manager.h" |
---|
30 | #include "shared_network_data.h" |
---|
31 | #include "message_manager.h" |
---|
32 | #include "preferences.h" |
---|
33 | #include "zip.h" |
---|
34 | |
---|
35 | #include "src/lib/util/loading/resource_manager.h" |
---|
36 | |
---|
37 | #include "network_log.h" |
---|
38 | |
---|
39 | |
---|
40 | #include "lib/util/loading/factory.h" |
---|
41 | |
---|
42 | #include "debug.h" |
---|
43 | #include "class_list.h" |
---|
44 | #include <algorithm> |
---|
45 | |
---|
46 | /* include your own header */ |
---|
47 | #include "network_stream.h" |
---|
48 | |
---|
49 | /* probably unnecessary */ |
---|
50 | using namespace std; |
---|
51 | |
---|
52 | |
---|
53 | #define PACKAGE_SIZE 256 |
---|
54 | |
---|
55 | |
---|
56 | NetworkStream::NetworkStream() |
---|
57 | : DataStream() |
---|
58 | { |
---|
59 | this->init(); |
---|
60 | /* initialize the references */ |
---|
61 | this->type = NET_CLIENT; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | NetworkStream::NetworkStream( std::string host, int port ) |
---|
66 | { |
---|
67 | this->type = NET_CLIENT; |
---|
68 | this->init(); |
---|
69 | this->peers[0].socket = new UdpSocket( host, port ); |
---|
70 | this->peers[0].userId = 0; |
---|
71 | this->peers[0].isServer = true; |
---|
72 | this->peers[0].connectionMonitor = new ConnectionMonitor( 0 ); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | NetworkStream::NetworkStream( int port ) |
---|
77 | { |
---|
78 | this->type = NET_SERVER; |
---|
79 | this->init(); |
---|
80 | this->serverSocket = new UdpServerSocket(port); |
---|
81 | this->bActive = true; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | void NetworkStream::init() |
---|
86 | { |
---|
87 | /* set the class id for the base object */ |
---|
88 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
89 | this->bActive = false; |
---|
90 | this->serverSocket = NULL; |
---|
91 | this->networkGameManager = NULL; |
---|
92 | myHostId = 0; |
---|
93 | currentState = 0; |
---|
94 | |
---|
95 | remainingBytesToWriteToDict = Preferences::getInstance()->getInt( "compression", "writedict", 0 ); |
---|
96 | |
---|
97 | assert( Zip::getInstance()->loadDictionary( "testdict" ) ); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | NetworkStream::~NetworkStream() |
---|
102 | { |
---|
103 | if ( this->serverSocket ) |
---|
104 | { |
---|
105 | serverSocket->close(); |
---|
106 | delete serverSocket; |
---|
107 | serverSocket = NULL; |
---|
108 | } |
---|
109 | |
---|
110 | for ( PeerList::iterator i = peers.begin(); i!=peers.end(); i++) |
---|
111 | { |
---|
112 | if ( i->second.socket ) |
---|
113 | { |
---|
114 | i->second.socket->disconnectServer(); |
---|
115 | delete i->second.socket; |
---|
116 | i->second.socket = NULL; |
---|
117 | } |
---|
118 | |
---|
119 | if ( i->second.handshake ) |
---|
120 | { |
---|
121 | delete i->second.handshake; |
---|
122 | i->second.handshake = NULL; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | for ( SynchronizeableList::const_iterator it = getSyncBegin(); it != getSyncEnd(); it ++ ) |
---|
127 | (*it)->setNetworkStream( NULL ); |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | void NetworkStream::createNetworkGameManager() |
---|
132 | { |
---|
133 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
134 | // setUniqueID( maxCon+2 ) because we need one id for every handshake |
---|
135 | // and one for handshake to reject client maxCon+1 |
---|
136 | this->networkGameManager->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
137 | MessageManager::getInstance()->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | void NetworkStream::startHandshake() |
---|
142 | { |
---|
143 | Handshake* hs = new Handshake(false); |
---|
144 | hs->setUniqueID( 0 ); |
---|
145 | assert( peers[0].handshake == NULL ); |
---|
146 | peers[0].handshake = hs; |
---|
147 | // peers[0].handshake->setSynchronized( true ); |
---|
148 | //this->connectSynchronizeable(*hs); |
---|
149 | //this->connectSynchronizeable(*hs); |
---|
150 | PRINTF(0)("NetworkStream: Handshake created: %s\n", hs->getName()); |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
---|
155 | { |
---|
156 | this->synchronizeables.push_back(&sync); |
---|
157 | sync.setNetworkStream( this ); |
---|
158 | |
---|
159 | this->bActive = true; |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync) |
---|
164 | { |
---|
165 | // removing the Synchronizeable from the List. |
---|
166 | std::list<Synchronizeable*>::iterator disconnectSynchro = std::find(this->synchronizeables.begin(), this->synchronizeables.end(), &sync); |
---|
167 | if (disconnectSynchro != this->synchronizeables.end()) |
---|
168 | this->synchronizeables.erase(disconnectSynchro); |
---|
169 | |
---|
170 | oldSynchronizeables[sync.getUniqueID()] = SDL_GetTicks(); |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | void NetworkStream::processData() |
---|
175 | { |
---|
176 | int tick = SDL_GetTicks(); |
---|
177 | |
---|
178 | currentState++; |
---|
179 | |
---|
180 | if ( this->type == NET_SERVER ) |
---|
181 | { |
---|
182 | if ( serverSocket ) |
---|
183 | serverSocket->update(); |
---|
184 | |
---|
185 | this->updateConnectionList(); |
---|
186 | } |
---|
187 | else |
---|
188 | { |
---|
189 | if ( peers[0].socket && ( !peers[0].socket->isOk() || peers[0].connectionMonitor->hasTimedOut() ) ) |
---|
190 | { |
---|
191 | PRINTF(1)("lost connection to server\n"); |
---|
192 | |
---|
193 | peers[0].socket->disconnectServer(); |
---|
194 | delete peers[0].socket; |
---|
195 | peers[0].socket = NULL; |
---|
196 | |
---|
197 | if ( peers[0].handshake ) |
---|
198 | delete peers[0].handshake; |
---|
199 | peers[0].handshake = NULL; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | cleanUpOldSyncList(); |
---|
204 | handleHandshakes(); |
---|
205 | |
---|
206 | // order of up/downstream is important!!!! |
---|
207 | // don't change it |
---|
208 | handleDownstream( tick ); |
---|
209 | handleUpstream( tick ); |
---|
210 | |
---|
211 | } |
---|
212 | |
---|
213 | void NetworkStream::updateConnectionList( ) |
---|
214 | { |
---|
215 | //check for new connections |
---|
216 | |
---|
217 | NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket(); |
---|
218 | |
---|
219 | if ( tempNetworkSocket ) |
---|
220 | { |
---|
221 | int clientId; |
---|
222 | if ( freeSocketSlots.size() >0 ) |
---|
223 | { |
---|
224 | clientId = freeSocketSlots.back(); |
---|
225 | freeSocketSlots.pop_back(); |
---|
226 | peers[clientId].socket = tempNetworkSocket; |
---|
227 | peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() ); |
---|
228 | peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); |
---|
229 | peers[clientId].handshake->setUniqueID(clientId); |
---|
230 | peers[clientId].userId = clientId; |
---|
231 | peers[clientId].isServer = false; |
---|
232 | } else |
---|
233 | { |
---|
234 | clientId = 1; |
---|
235 | |
---|
236 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
237 | if ( it->first >= clientId ) |
---|
238 | clientId = it->first + 1; |
---|
239 | |
---|
240 | peers[clientId].socket = tempNetworkSocket; |
---|
241 | peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); |
---|
242 | peers[clientId].handshake->setUniqueID(clientId); |
---|
243 | peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); |
---|
244 | peers[clientId].userId = clientId; |
---|
245 | peers[clientId].isServer = false; |
---|
246 | |
---|
247 | PRINTF(0)("num sync: %d\n", synchronizeables.size()); |
---|
248 | } |
---|
249 | |
---|
250 | if ( clientId > MAX_CONNECTIONS ) |
---|
251 | { |
---|
252 | peers[clientId].handshake->doReject( "too many connections" ); |
---|
253 | PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); |
---|
254 | } |
---|
255 | else |
---|
256 | |
---|
257 | PRINTF(0)("New Client: %d\n", clientId); |
---|
258 | |
---|
259 | //this->connectSynchronizeable(*handshakes[clientId]); |
---|
260 | } |
---|
261 | |
---|
262 | //check if connections are ok else remove them |
---|
263 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); ) |
---|
264 | { |
---|
265 | if ( |
---|
266 | it->second.socket && |
---|
267 | ( |
---|
268 | !it->second.socket->isOk() || |
---|
269 | it->second.connectionMonitor->hasTimedOut() |
---|
270 | ) |
---|
271 | ) |
---|
272 | { |
---|
273 | std::string reason = "disconnected"; |
---|
274 | if ( it->second.connectionMonitor->hasTimedOut() ) |
---|
275 | reason = "timeout"; |
---|
276 | PRINTF(0)("Client is gone: %d (%s)\n", it->second.userId, reason.c_str()); |
---|
277 | |
---|
278 | //assert(false); |
---|
279 | |
---|
280 | it->second.socket->disconnectServer(); |
---|
281 | delete it->second.socket; |
---|
282 | it->second.socket = NULL; |
---|
283 | |
---|
284 | if ( it->second.handshake ) |
---|
285 | delete it->second.handshake; |
---|
286 | it->second.handshake = NULL; |
---|
287 | |
---|
288 | for ( SynchronizeableList::iterator it2 = synchronizeables.begin(); it2 != synchronizeables.end(); it2++ ) |
---|
289 | { |
---|
290 | (*it2)->cleanUpUser( it->second.userId ); |
---|
291 | } |
---|
292 | |
---|
293 | NetworkGameManager::getInstance()->signalLeftPlayer(it->second.userId); |
---|
294 | |
---|
295 | freeSocketSlots.push_back( it->second.userId ); |
---|
296 | |
---|
297 | PeerList::iterator delit = it; |
---|
298 | it++; |
---|
299 | |
---|
300 | peers.erase( delit ); |
---|
301 | |
---|
302 | continue; |
---|
303 | } |
---|
304 | |
---|
305 | it++; |
---|
306 | } |
---|
307 | |
---|
308 | |
---|
309 | } |
---|
310 | |
---|
311 | void NetworkStream::debug() |
---|
312 | { |
---|
313 | if( this->isServer()) |
---|
314 | PRINT(0)(" Host ist Server with ID: %i\n", this->myHostId); |
---|
315 | else |
---|
316 | PRINT(0)(" Host ist Client with ID: %i\n", this->myHostId); |
---|
317 | |
---|
318 | PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); |
---|
319 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
320 | { |
---|
321 | if( (*it)->beSynchronized() == true) |
---|
322 | PRINT(0)(" Synchronizeable of class: %s::%s, with unique ID: %i, Synchronize: %i\n", (*it)->getClassName(), (*it)->getName(), |
---|
323 | (*it)->getUniqueID(), (*it)->beSynchronized()); |
---|
324 | } |
---|
325 | PRINT(0)(" Maximal Connections: %i\n", MAX_CONNECTIONS ); |
---|
326 | |
---|
327 | } |
---|
328 | |
---|
329 | |
---|
330 | int NetworkStream::getSyncCount() |
---|
331 | { |
---|
332 | int n = 0; |
---|
333 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
334 | if( (*it)->beSynchronized() == true) |
---|
335 | ++n; |
---|
336 | |
---|
337 | //return synchronizeables.size(); |
---|
338 | return n; |
---|
339 | } |
---|
340 | |
---|
341 | /** |
---|
342 | * check if handshakes completed |
---|
343 | */ |
---|
344 | void NetworkStream::handleHandshakes( ) |
---|
345 | { |
---|
346 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
347 | { |
---|
348 | if ( it->second.handshake ) |
---|
349 | { |
---|
350 | if ( it->second.handshake->completed() ) |
---|
351 | { |
---|
352 | if ( it->second.handshake->ok() ) |
---|
353 | { |
---|
354 | if ( !it->second.handshake->allowDel() ) |
---|
355 | { |
---|
356 | if ( type != NET_SERVER ) |
---|
357 | { |
---|
358 | SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); |
---|
359 | myHostId = SharedNetworkData::getInstance()->getHostID(); |
---|
360 | |
---|
361 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
362 | this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() ); |
---|
363 | MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() ); |
---|
364 | } |
---|
365 | |
---|
366 | |
---|
367 | PRINT(0)("handshake finished id=%d\n", it->second.handshake->getNetworkGameManagerId()); |
---|
368 | |
---|
369 | it->second.handshake->del(); |
---|
370 | } |
---|
371 | else |
---|
372 | { |
---|
373 | if ( it->second.handshake->canDel() ) |
---|
374 | { |
---|
375 | if ( type == NET_SERVER ) |
---|
376 | { |
---|
377 | handleNewClient( it->second.userId ); |
---|
378 | } |
---|
379 | |
---|
380 | PRINT(0)("handshake finished delete it\n"); |
---|
381 | delete it->second.handshake; |
---|
382 | it->second.handshake = NULL; |
---|
383 | } |
---|
384 | } |
---|
385 | |
---|
386 | } |
---|
387 | else |
---|
388 | { |
---|
389 | PRINT(1)("handshake failed!\n"); |
---|
390 | it->second.socket->disconnectServer(); |
---|
391 | } |
---|
392 | } |
---|
393 | } |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | /** |
---|
398 | * handle upstream network traffic |
---|
399 | */ |
---|
400 | void NetworkStream::handleUpstream( int tick ) |
---|
401 | { |
---|
402 | int offset; |
---|
403 | int n; |
---|
404 | |
---|
405 | for ( PeerList::reverse_iterator peer = peers.rbegin(); peer != peers.rend(); peer++ ) |
---|
406 | { |
---|
407 | offset = INTSIZE; //make already space for length |
---|
408 | |
---|
409 | if ( !peer->second.socket ) |
---|
410 | continue; |
---|
411 | |
---|
412 | n = Converter::intToByteArray( currentState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
413 | assert( n == INTSIZE ); |
---|
414 | offset += n; |
---|
415 | |
---|
416 | n = Converter::intToByteArray( peer->second.lastAckedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
417 | assert( n == INTSIZE ); |
---|
418 | offset += n; |
---|
419 | |
---|
420 | n = Converter::intToByteArray( peer->second.lastRecvedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
421 | assert( n == INTSIZE ); |
---|
422 | offset += n; |
---|
423 | |
---|
424 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
425 | { |
---|
426 | int oldOffset = offset; |
---|
427 | Synchronizeable & sync = **it; |
---|
428 | |
---|
429 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
---|
430 | continue; |
---|
431 | |
---|
432 | //if handshake not finished only sync handshake |
---|
433 | if ( peer->second.handshake && sync.getLeafClassID() != CL_HANDSHAKE ) |
---|
434 | continue; |
---|
435 | |
---|
436 | if ( isServer() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId ) |
---|
437 | continue; |
---|
438 | |
---|
439 | //do not sync null parent |
---|
440 | if ( sync.getLeafClassID() == CL_NULL_PARENT ) |
---|
441 | continue; |
---|
442 | |
---|
443 | assert( offset + INTSIZE <= UDP_PACKET_SIZE ); |
---|
444 | |
---|
445 | //server fakes uniqueid=0 for handshake |
---|
446 | if ( this->isServer() && sync.getUniqueID() < MAX_CONNECTIONS - 1 ) |
---|
447 | n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
448 | else |
---|
449 | n = Converter::intToByteArray( sync.getUniqueID(), buf + offset, UDP_PACKET_SIZE - offset ); |
---|
450 | assert( n == INTSIZE ); |
---|
451 | offset += n; |
---|
452 | |
---|
453 | //make space for size |
---|
454 | offset += INTSIZE; |
---|
455 | |
---|
456 | n = sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, -1000 ); |
---|
457 | offset += n; |
---|
458 | //NETPRINTF(0)("GGGGGEEEEETTTTT: %s (%d) %d\n",sync.getClassName(), sync.getUniqueID(), n); |
---|
459 | |
---|
460 | assert( Converter::intToByteArray( n, buf + offset - n - INTSIZE, INTSIZE ) == INTSIZE ); |
---|
461 | |
---|
462 | //check if all bytes == 0 -> remove data |
---|
463 | //TODO not all synchronizeables like this maybe add Synchronizeable::canRemoveZeroDiff() |
---|
464 | bool allZero = true; |
---|
465 | for ( int i = 0; i < n; i++ ) |
---|
466 | { |
---|
467 | if ( buf[i+oldOffset+2*INTSIZE] != 0 ) |
---|
468 | allZero = false; |
---|
469 | } |
---|
470 | |
---|
471 | if ( allZero ) |
---|
472 | { |
---|
473 | //NETPRINTF(n)("REMOVE ZERO DIFF: %s (%d)\n", sync.getClassName(), sync.getUniqueID()); |
---|
474 | offset = oldOffset; |
---|
475 | } |
---|
476 | |
---|
477 | |
---|
478 | } |
---|
479 | |
---|
480 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
481 | { |
---|
482 | Synchronizeable & sync = **it; |
---|
483 | |
---|
484 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
---|
485 | continue; |
---|
486 | |
---|
487 | sync.handleSentState( peer->second.userId, currentState, peer->second.lastAckedState ); |
---|
488 | } |
---|
489 | |
---|
490 | assert( Converter::intToByteArray( offset, buf, INTSIZE ) == INTSIZE ); |
---|
491 | |
---|
492 | int compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE ); |
---|
493 | |
---|
494 | if ( compLength < 0 ) |
---|
495 | { |
---|
496 | PRINTF(1)("compression failed!\n"); |
---|
497 | continue; |
---|
498 | } |
---|
499 | |
---|
500 | assert( peer->second.socket->writePacket( compBuf, compLength ) ); |
---|
501 | |
---|
502 | if ( this->remainingBytesToWriteToDict > 0 ) |
---|
503 | writeToNewDict( buf, offset ); |
---|
504 | |
---|
505 | peer->second.connectionMonitor->processUnzippedOutgoingPacket( tick, buf, offset, currentState ); |
---|
506 | peer->second.connectionMonitor->processZippedOutgoingPacket( tick, compBuf, compLength, currentState ); |
---|
507 | |
---|
508 | //NETPRINTF(n)("send packet: %d userId = %d\n", offset, peer->second.userId); |
---|
509 | } |
---|
510 | } |
---|
511 | |
---|
512 | /** |
---|
513 | * handle downstream network traffic |
---|
514 | */ |
---|
515 | void NetworkStream::handleDownstream( int tick ) |
---|
516 | { |
---|
517 | int offset = 0; |
---|
518 | |
---|
519 | int length = 0; |
---|
520 | int packetLength = 0; |
---|
521 | int compLength = 0; |
---|
522 | int uniqueId = 0; |
---|
523 | int state = 0; |
---|
524 | int ackedState = 0; |
---|
525 | int fromState = 0; |
---|
526 | int syncDataLength = 0; |
---|
527 | |
---|
528 | for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ ) |
---|
529 | { |
---|
530 | |
---|
531 | if ( !peer->second.socket ) |
---|
532 | continue; |
---|
533 | |
---|
534 | while ( 0 < (compLength = peer->second.socket->readPacket( compBuf, UDP_PACKET_SIZE )) ) |
---|
535 | { |
---|
536 | peer->second.connectionMonitor->processZippedIncomingPacket( tick, compBuf, compLength ); |
---|
537 | |
---|
538 | //PRINTF(0)("GGGGGOOOOOOOOOOTTTTTTTT: %d\n", compLength); |
---|
539 | packetLength = Zip::getInstance()->unZip( compBuf, compLength, buf, UDP_PACKET_SIZE ); |
---|
540 | |
---|
541 | if ( packetLength < 4*INTSIZE ) |
---|
542 | { |
---|
543 | if ( packetLength != 0 ) |
---|
544 | PRINTF(1)("got too small packet: %d\n", packetLength); |
---|
545 | continue; |
---|
546 | } |
---|
547 | |
---|
548 | if ( this->remainingBytesToWriteToDict > 0 ) |
---|
549 | writeToNewDict( buf, packetLength ); |
---|
550 | |
---|
551 | assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE ); |
---|
552 | assert( Converter::byteArrayToInt( buf + INTSIZE, &state ) == INTSIZE ); |
---|
553 | assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE ); |
---|
554 | assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE ); |
---|
555 | //NETPRINTF(n)("ackedstate: %d\n", ackedState); |
---|
556 | offset = 4*INTSIZE; |
---|
557 | |
---|
558 | peer->second.connectionMonitor->processUnzippedIncomingPacket( tick, buf, offset, state, ackedState ); |
---|
559 | |
---|
560 | //NETPRINTF(n)("got packet: %d, %d\n", length, packetLength); |
---|
561 | |
---|
562 | //if this is an old state drop it |
---|
563 | if ( state <= peer->second.lastRecvedState ) |
---|
564 | continue; |
---|
565 | |
---|
566 | if ( packetLength != length ) |
---|
567 | { |
---|
568 | PRINTF(1)("real packet length (%d) and transmitted packet length (%d) do not match!\n", packetLength, length); |
---|
569 | peer->second.socket->disconnectServer(); |
---|
570 | continue; |
---|
571 | } |
---|
572 | |
---|
573 | while ( offset + 2*INTSIZE < length ) |
---|
574 | { |
---|
575 | assert( offset > 0 ); |
---|
576 | assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE ); |
---|
577 | offset += INTSIZE; |
---|
578 | |
---|
579 | assert( Converter::byteArrayToInt( buf + offset, &syncDataLength ) == INTSIZE ); |
---|
580 | offset += INTSIZE; |
---|
581 | |
---|
582 | assert( syncDataLength > 0 ); |
---|
583 | assert( syncDataLength < 10000 ); |
---|
584 | |
---|
585 | Synchronizeable * sync = NULL; |
---|
586 | |
---|
587 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
588 | { |
---|
589 | // client thinks his handshake has id 0!!!!! |
---|
590 | if ( (*it)->getUniqueID() == uniqueId || ( uniqueId == 0 && (*it)->getUniqueID() == peer->second.userId ) ) |
---|
591 | { |
---|
592 | sync = *it; |
---|
593 | break; |
---|
594 | } |
---|
595 | } |
---|
596 | |
---|
597 | if ( sync == NULL ) |
---|
598 | { |
---|
599 | PRINTF(0)("could not find sync with id %d. try to create it\n", uniqueId); |
---|
600 | if ( oldSynchronizeables.find( uniqueId ) != oldSynchronizeables.end() ) |
---|
601 | { |
---|
602 | offset += syncDataLength; |
---|
603 | continue; |
---|
604 | } |
---|
605 | |
---|
606 | if ( !peers[peer->second.userId].isServer ) |
---|
607 | { |
---|
608 | offset += syncDataLength; |
---|
609 | continue; |
---|
610 | } |
---|
611 | |
---|
612 | int leafClassId; |
---|
613 | if ( INTSIZE > length - offset ) |
---|
614 | { |
---|
615 | offset += syncDataLength; |
---|
616 | continue; |
---|
617 | } |
---|
618 | |
---|
619 | Converter::byteArrayToInt( buf + offset, &leafClassId ); |
---|
620 | |
---|
621 | assert( leafClassId != 0 ); |
---|
622 | |
---|
623 | BaseObject * b = NULL; |
---|
624 | /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ |
---|
625 | /* Exception 1: NullParent */ |
---|
626 | if( leafClassId == CL_NULL_PARENT || leafClassId == CL_SYNCHRONIZEABLE || leafClassId == CL_NETWORK_GAME_MANAGER ) |
---|
627 | { |
---|
628 | PRINTF(1)("Can not create Class with ID %x!\n", (int)leafClassId); |
---|
629 | offset += syncDataLength; |
---|
630 | continue; |
---|
631 | } |
---|
632 | else |
---|
633 | b = Factory::fabricate( (ClassID)leafClassId ); |
---|
634 | |
---|
635 | if ( !b ) |
---|
636 | { |
---|
637 | PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId); |
---|
638 | offset += syncDataLength; |
---|
639 | continue; |
---|
640 | } |
---|
641 | |
---|
642 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
643 | { |
---|
644 | sync = dynamic_cast<Synchronizeable*>(b); |
---|
645 | sync->setUniqueID( uniqueId ); |
---|
646 | sync->setSynchronized(true); |
---|
647 | |
---|
648 | PRINTF(0)("Fabricated %s with id %d\n", sync->getClassName(), sync->getUniqueID()); |
---|
649 | } |
---|
650 | else |
---|
651 | { |
---|
652 | PRINTF(1)("Class with ID %x is not a synchronizeable!\n", (int)leafClassId); |
---|
653 | delete b; |
---|
654 | offset += syncDataLength; |
---|
655 | continue; |
---|
656 | } |
---|
657 | } |
---|
658 | |
---|
659 | |
---|
660 | int n = sync->setStateDiff( peer->second.userId, buf+offset, syncDataLength, state, fromState ); |
---|
661 | offset += n; |
---|
662 | //NETPRINTF(0)("SSSSSEEEEETTTTT: %s %d\n",sync->getClassName(), n); |
---|
663 | |
---|
664 | } |
---|
665 | |
---|
666 | if ( offset != length ) |
---|
667 | { |
---|
668 | PRINTF(0)("offset (%d) != length (%d)\n", offset, length); |
---|
669 | peer->second.socket->disconnectServer(); |
---|
670 | } |
---|
671 | |
---|
672 | |
---|
673 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
674 | { |
---|
675 | Synchronizeable & sync = **it; |
---|
676 | |
---|
677 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
---|
678 | continue; |
---|
679 | |
---|
680 | sync.handleRecvState( peer->second.userId, state, fromState ); |
---|
681 | } |
---|
682 | |
---|
683 | assert( peer->second.lastAckedState <= ackedState ); |
---|
684 | peer->second.lastAckedState = ackedState; |
---|
685 | |
---|
686 | assert( peer->second.lastRecvedState < state ); |
---|
687 | peer->second.lastRecvedState = state; |
---|
688 | |
---|
689 | } |
---|
690 | |
---|
691 | } |
---|
692 | |
---|
693 | } |
---|
694 | |
---|
695 | /** |
---|
696 | * is executed when a handshake has finished |
---|
697 | * @todo create playable for new user |
---|
698 | */ |
---|
699 | void NetworkStream::handleNewClient( int userId ) |
---|
700 | { |
---|
701 | MessageManager::getInstance()->initUser( userId ); |
---|
702 | |
---|
703 | networkGameManager->signalNewPlayer( userId ); |
---|
704 | } |
---|
705 | |
---|
706 | /** |
---|
707 | * removes old items from oldSynchronizeables |
---|
708 | */ |
---|
709 | void NetworkStream::cleanUpOldSyncList( ) |
---|
710 | { |
---|
711 | int now = SDL_GetTicks(); |
---|
712 | |
---|
713 | for ( std::map<int,int>::iterator it = oldSynchronizeables.begin(); it != oldSynchronizeables.end(); ) |
---|
714 | { |
---|
715 | if ( it->second < now - 10*1000 ) |
---|
716 | { |
---|
717 | std::map<int,int>::iterator delIt = it; |
---|
718 | it++; |
---|
719 | oldSynchronizeables.erase( delIt ); |
---|
720 | continue; |
---|
721 | } |
---|
722 | it++; |
---|
723 | } |
---|
724 | } |
---|
725 | |
---|
726 | /** |
---|
727 | * writes data to DATA/dicts/newdict |
---|
728 | * @param data pointer to data |
---|
729 | * @param length length |
---|
730 | */ |
---|
731 | void NetworkStream::writeToNewDict( byte * data, int length ) |
---|
732 | { |
---|
733 | if ( remainingBytesToWriteToDict <= 0 ) |
---|
734 | return; |
---|
735 | |
---|
736 | if ( length > remainingBytesToWriteToDict ) |
---|
737 | length = remainingBytesToWriteToDict; |
---|
738 | |
---|
739 | std::string fileName = ResourceManager::getInstance()->getDataDir(); |
---|
740 | fileName += "/dicts/newdict"; |
---|
741 | |
---|
742 | FILE * f = fopen( fileName.c_str(), "a" ); |
---|
743 | |
---|
744 | if ( !f ) |
---|
745 | { |
---|
746 | PRINTF(2)("could not open %s\n", fileName.c_str()); |
---|
747 | remainingBytesToWriteToDict = 0; |
---|
748 | return; |
---|
749 | } |
---|
750 | |
---|
751 | if ( fwrite( data, 1, length, f ) != length ) |
---|
752 | { |
---|
753 | PRINTF(2)("could not write to file\n"); |
---|
754 | fclose( f ); |
---|
755 | return; |
---|
756 | } |
---|
757 | |
---|
758 | fclose( f ); |
---|
759 | |
---|
760 | remainingBytesToWriteToDict -= length; |
---|
761 | } |
---|
762 | |
---|
763 | |
---|
764 | |
---|
765 | |
---|
766 | |
---|
767 | |
---|