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: Christoph Renner rennerc@ee.ethz.ch |
---|
13 | co-programmer: Patrick Boenzli boenzlip@orxonox.ethz.ch |
---|
14 | |
---|
15 | June 2006: finishing work on the network stream for pps presentation (rennerc@ee.ethz.ch) |
---|
16 | July 2006: some code rearangement and integration of the proxy server mechanism (boenzlip@ee.ethz.ch) |
---|
17 | */ |
---|
18 | |
---|
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 "monitor/connection_monitor.h" |
---|
28 | #include "monitor/network_monitor.h" |
---|
29 | #include "synchronizeable.h" |
---|
30 | #include "network_game_manager.h" |
---|
31 | #include "shared_network_data.h" |
---|
32 | #include "message_manager.h" |
---|
33 | #include "preferences.h" |
---|
34 | #include "zip.h" |
---|
35 | |
---|
36 | #include "src/lib/util/loading/resource_manager.h" |
---|
37 | |
---|
38 | #include "network_log.h" |
---|
39 | |
---|
40 | #include "player_stats.h" |
---|
41 | |
---|
42 | #include "lib/util/loading/factory.h" |
---|
43 | |
---|
44 | #include "debug.h" |
---|
45 | #include "class_list.h" |
---|
46 | #include <algorithm> |
---|
47 | |
---|
48 | |
---|
49 | #include "network_stream.h" |
---|
50 | |
---|
51 | |
---|
52 | using namespace std; |
---|
53 | |
---|
54 | |
---|
55 | #define PACKAGE_SIZE 256 |
---|
56 | |
---|
57 | |
---|
58 | /** |
---|
59 | * empty constructor |
---|
60 | */ |
---|
61 | NetworkStream::NetworkStream() |
---|
62 | : DataStream() |
---|
63 | { |
---|
64 | this->init(); |
---|
65 | /* initialize the references */ |
---|
66 | this->pInfo->nodeType = NET_CLIENT; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | /** |
---|
71 | * start as a client, connect to a server |
---|
72 | * @param host: host name (address) |
---|
73 | * @param port: port number |
---|
74 | */ |
---|
75 | NetworkStream::NetworkStream( std::string host, int port ) |
---|
76 | { |
---|
77 | this->init(); |
---|
78 | // init the peers[0] the server to ceonnect to |
---|
79 | this->peers[0].socket = new UdpSocket( host, port ); |
---|
80 | this->peers[0].userId = 0; |
---|
81 | this->peers[0].nodeType = NET_MASTER_SERVER; |
---|
82 | this->peers[0].connectionMonitor = new ConnectionMonitor( 0 ); |
---|
83 | // and set also the localhost |
---|
84 | this->pInfo->nodeType = NET_CLIENT; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | /** |
---|
89 | * start as a server |
---|
90 | * @param port: at this port |
---|
91 | */ |
---|
92 | NetworkStream::NetworkStream( int port ) |
---|
93 | { |
---|
94 | this->init(); |
---|
95 | this->serverSocket = new UdpServerSocket(port); |
---|
96 | this->pInfo->nodeType = NET_MASTER_SERVER; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | /** |
---|
101 | * generic init functions |
---|
102 | */ |
---|
103 | void NetworkStream::init() |
---|
104 | { |
---|
105 | /* set the class id for the base object */ |
---|
106 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
107 | this->serverSocket = NULL; |
---|
108 | this->networkGameManager = NULL; |
---|
109 | this->networkMonitor = NULL; |
---|
110 | |
---|
111 | this->pInfo = new PeerInfo(); |
---|
112 | this->pInfo->userId = 0; |
---|
113 | this->pInfo->lastAckedState = 0; |
---|
114 | this->pInfo->lastRecvedState = 0; |
---|
115 | |
---|
116 | |
---|
117 | this->currentState = 0; |
---|
118 | |
---|
119 | remainingBytesToWriteToDict = Preferences::getInstance()->getInt( "compression", "writedict", 0 ); |
---|
120 | |
---|
121 | assert( Zip::getInstance()->loadDictionary( "testdict" ) >= 0 ); |
---|
122 | this->dictClient = Zip::getInstance()->loadDictionary( "dict2pl_client" ); |
---|
123 | assert( this->dictClient >= 0 ); |
---|
124 | this->dictServer = Zip::getInstance()->loadDictionary( "dict2p_server" ); |
---|
125 | assert( this->dictServer >= 0 ); |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | /** |
---|
130 | * deconstructor |
---|
131 | */ |
---|
132 | NetworkStream::~NetworkStream() |
---|
133 | { |
---|
134 | if ( this->serverSocket ) |
---|
135 | { |
---|
136 | serverSocket->close(); |
---|
137 | delete serverSocket; |
---|
138 | serverSocket = NULL; |
---|
139 | } |
---|
140 | for ( PeerList::iterator i = peers.begin(); i!=peers.end(); i++) |
---|
141 | { |
---|
142 | if ( i->second.socket ) |
---|
143 | { |
---|
144 | i->second.socket->disconnectServer(); |
---|
145 | delete i->second.socket; |
---|
146 | i->second.socket = NULL; |
---|
147 | } |
---|
148 | |
---|
149 | if ( i->second.handshake ) |
---|
150 | { |
---|
151 | delete i->second.handshake; |
---|
152 | i->second.handshake = NULL; |
---|
153 | } |
---|
154 | |
---|
155 | if ( i->second.connectionMonitor ) |
---|
156 | { |
---|
157 | delete i->second.connectionMonitor; |
---|
158 | i->second.connectionMonitor = NULL; |
---|
159 | } |
---|
160 | } |
---|
161 | for ( SynchronizeableList::const_iterator it = getSyncBegin(); it != getSyncEnd(); it ++ ) |
---|
162 | (*it)->setNetworkStream( NULL ); |
---|
163 | |
---|
164 | if( this->pInfo) |
---|
165 | delete this->pInfo; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | /** |
---|
170 | * creates a new instance of the network game manager |
---|
171 | */ |
---|
172 | void NetworkStream::createNetworkGameManager() |
---|
173 | { |
---|
174 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
175 | // setUniqueID( maxCon+2 ) because we need one id for every handshake |
---|
176 | // and one for handshake to reject client maxCon+1 |
---|
177 | this->networkGameManager->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
178 | MessageManager::getInstance()->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | /** |
---|
183 | * starts the network handshake |
---|
184 | */ |
---|
185 | void NetworkStream::startHandshake() |
---|
186 | { |
---|
187 | Handshake* hs = new Handshake(this->pInfo->nodeType); |
---|
188 | hs->setUniqueID( 0 ); |
---|
189 | assert( peers[0].handshake == NULL ); |
---|
190 | peers[0].handshake = hs; |
---|
191 | |
---|
192 | hs->setPreferedNickName( Preferences::getInstance()->getString( "multiplayer", "nickname", "Player" ) ); |
---|
193 | |
---|
194 | // peers[0].handshake->setSynchronized( true ); |
---|
195 | //this->connectSynchronizeable(*hs); |
---|
196 | //this->connectSynchronizeable(*hs); |
---|
197 | PRINTF(0)("NetworkStream: Handshake created: %s\n", hs->getName()); |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | /** |
---|
202 | * this functions connects a synchronizeable to the networkstream, therefore synchronizeing |
---|
203 | * it all over the network and creating it on the other platforms (if and only if it is a |
---|
204 | * server |
---|
205 | */ |
---|
206 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
---|
207 | { |
---|
208 | this->synchronizeables.push_back(&sync); |
---|
209 | sync.setNetworkStream( this ); |
---|
210 | |
---|
211 | // this->bActive = true; |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | /** |
---|
216 | * removes the synchronizeable from the list of synchronized entities |
---|
217 | */ |
---|
218 | void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync) |
---|
219 | { |
---|
220 | // removing the Synchronizeable from the List. |
---|
221 | std::list<Synchronizeable*>::iterator disconnectSynchro = std::find(this->synchronizeables.begin(), this->synchronizeables.end(), &sync); |
---|
222 | if (disconnectSynchro != this->synchronizeables.end()) |
---|
223 | this->synchronizeables.erase(disconnectSynchro); |
---|
224 | |
---|
225 | oldSynchronizeables[sync.getUniqueID()] = SDL_GetTicks(); |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | /** |
---|
230 | * this is called to process data from the network socket to the synchronizeable and vice versa |
---|
231 | */ |
---|
232 | void NetworkStream::processData() |
---|
233 | { |
---|
234 | // create the network monitor after all the init work and before there is any connection handlings |
---|
235 | if( this->networkMonitor == NULL) |
---|
236 | this->networkMonitor = new NetworkMonitor(this); |
---|
237 | |
---|
238 | |
---|
239 | int tick = SDL_GetTicks(); |
---|
240 | |
---|
241 | this->currentState++; |
---|
242 | // there was a wrap around |
---|
243 | if( this->currentState < 0) |
---|
244 | { |
---|
245 | PRINTF(1)("A wrap around in the state variable as occured. The server was running so long? Pls restart server or write a mail to the supporters!\n"); |
---|
246 | } |
---|
247 | |
---|
248 | if ( this->pInfo->nodeType == NET_MASTER_SERVER ) |
---|
249 | { |
---|
250 | // execute everytthing the master server shoudl do |
---|
251 | if ( serverSocket ) |
---|
252 | serverSocket->update(); |
---|
253 | |
---|
254 | this->updateConnectionList(); |
---|
255 | } |
---|
256 | else if( this->pInfo->nodeType == NET_PROXY_SERVER) |
---|
257 | { |
---|
258 | // execute everything the proxy server should do |
---|
259 | } |
---|
260 | else |
---|
261 | { |
---|
262 | // check if the connection is ok else terminate and remove |
---|
263 | if ( peers[0].socket && ( !peers[0].socket->isOk() || peers[0].connectionMonitor->hasTimedOut() ) ) |
---|
264 | { |
---|
265 | PRINTF(1)("lost connection to server\n"); |
---|
266 | |
---|
267 | peers[0].socket->disconnectServer(); |
---|
268 | delete peers[0].socket; |
---|
269 | peers[0].socket = NULL; |
---|
270 | |
---|
271 | if ( peers[0].handshake ) |
---|
272 | delete peers[0].handshake; |
---|
273 | peers[0].handshake = NULL; |
---|
274 | |
---|
275 | if ( peers[0].connectionMonitor ) |
---|
276 | delete peers[0].connectionMonitor; |
---|
277 | peers[0].connectionMonitor = NULL; |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|
281 | cleanUpOldSyncList(); |
---|
282 | handleHandshakes(); |
---|
283 | |
---|
284 | // update the network monitor |
---|
285 | this->networkMonitor->process(); |
---|
286 | |
---|
287 | // order of up/downstream is important!!!! |
---|
288 | // don't change it |
---|
289 | handleDownstream( tick ); |
---|
290 | handleUpstream( tick ); |
---|
291 | } |
---|
292 | |
---|
293 | |
---|
294 | /** |
---|
295 | * if we are a server update the connection list to accept new connections (clients) |
---|
296 | * also start the handsake for the new clients |
---|
297 | */ |
---|
298 | void NetworkStream::updateConnectionList( ) |
---|
299 | { |
---|
300 | //check for new connections |
---|
301 | |
---|
302 | NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket(); |
---|
303 | |
---|
304 | // we got new network node |
---|
305 | if ( tempNetworkSocket ) |
---|
306 | { |
---|
307 | int clientId; |
---|
308 | // if there is a list of free client id slots, take these |
---|
309 | if ( freeSocketSlots.size() > 0 ) |
---|
310 | { |
---|
311 | clientId = freeSocketSlots.back(); |
---|
312 | freeSocketSlots.pop_back(); |
---|
313 | peers[clientId].socket = tempNetworkSocket; |
---|
314 | peers[clientId].handshake = new Handshake(this->pInfo->nodeType, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() ); |
---|
315 | peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); |
---|
316 | peers[clientId].handshake->setUniqueID(clientId); |
---|
317 | peers[clientId].userId = clientId; |
---|
318 | peers[clientId].nodeType = NET_CLIENT; |
---|
319 | } |
---|
320 | else |
---|
321 | { |
---|
322 | clientId = 1; |
---|
323 | |
---|
324 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
325 | if ( it->first >= clientId ) |
---|
326 | clientId = it->first + 1; |
---|
327 | |
---|
328 | peers[clientId].socket = tempNetworkSocket; |
---|
329 | peers[clientId].handshake = new Handshake(this->pInfo->nodeType, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); |
---|
330 | peers[clientId].handshake->setUniqueID(clientId); |
---|
331 | peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); |
---|
332 | peers[clientId].userId = clientId; |
---|
333 | peers[clientId].nodeType = NET_CLIENT; |
---|
334 | |
---|
335 | PRINTF(0)("num sync: %d\n", synchronizeables.size()); |
---|
336 | } |
---|
337 | |
---|
338 | // check if there are too many clients connected |
---|
339 | if ( clientId > MAX_CONNECTIONS ) |
---|
340 | { |
---|
341 | peers[clientId].handshake->doReject( "too many connections" ); |
---|
342 | PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); |
---|
343 | } |
---|
344 | else |
---|
345 | { |
---|
346 | PRINTF(0)("New Client: %d\n", clientId); |
---|
347 | } |
---|
348 | |
---|
349 | //this->connectSynchronizeable(*handshakes[clientId]); |
---|
350 | } |
---|
351 | |
---|
352 | |
---|
353 | |
---|
354 | //check if connections are ok else remove them |
---|
355 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); ) |
---|
356 | { |
---|
357 | if ( |
---|
358 | it->second.socket && |
---|
359 | ( |
---|
360 | !it->second.socket->isOk() || |
---|
361 | it->second.connectionMonitor->hasTimedOut() |
---|
362 | ) |
---|
363 | ) |
---|
364 | { |
---|
365 | std::string reason = "disconnected"; |
---|
366 | if ( it->second.connectionMonitor->hasTimedOut() ) |
---|
367 | reason = "timeout"; |
---|
368 | PRINTF(0)("Client is gone: %d (%s)\n", it->second.userId, reason.c_str()); |
---|
369 | |
---|
370 | |
---|
371 | // clean up the network data |
---|
372 | it->second.socket->disconnectServer(); |
---|
373 | delete it->second.socket; |
---|
374 | it->second.socket = NULL; |
---|
375 | |
---|
376 | if ( it->second.connectionMonitor ) |
---|
377 | delete it->second.connectionMonitor; |
---|
378 | it->second.connectionMonitor = NULL; |
---|
379 | |
---|
380 | if ( it->second.handshake ) |
---|
381 | delete it->second.handshake; |
---|
382 | it->second.handshake = NULL; |
---|
383 | |
---|
384 | for ( SynchronizeableList::iterator it2 = synchronizeables.begin(); it2 != synchronizeables.end(); it2++ ) |
---|
385 | { |
---|
386 | (*it2)->cleanUpUser( it->second.userId ); |
---|
387 | } |
---|
388 | |
---|
389 | NetworkGameManager::getInstance()->signalLeftPlayer(it->second.userId); |
---|
390 | |
---|
391 | freeSocketSlots.push_back( it->second.userId ); |
---|
392 | |
---|
393 | PeerList::iterator delit = it; |
---|
394 | it++; |
---|
395 | |
---|
396 | peers.erase( delit ); |
---|
397 | |
---|
398 | continue; |
---|
399 | } |
---|
400 | |
---|
401 | it++; |
---|
402 | } |
---|
403 | |
---|
404 | |
---|
405 | } |
---|
406 | |
---|
407 | |
---|
408 | void NetworkStream::debug() |
---|
409 | { |
---|
410 | if( this->isMasterServer()) |
---|
411 | PRINT(0)(" Host ist Server with ID: %i\n", this->pInfo->userId); |
---|
412 | else |
---|
413 | PRINT(0)(" Host ist Client with ID: %i\n", this->pInfo->userId); |
---|
414 | |
---|
415 | PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); |
---|
416 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
417 | { |
---|
418 | if( (*it)->beSynchronized() == true) |
---|
419 | PRINT(0)(" Synchronizeable of class: %s::%s, with unique ID: %i, Synchronize: %i\n", (*it)->getClassName(), (*it)->getName(), |
---|
420 | (*it)->getUniqueID(), (*it)->beSynchronized()); |
---|
421 | } |
---|
422 | PRINT(0)(" Maximal Connections: %i\n", MAX_CONNECTIONS ); |
---|
423 | |
---|
424 | } |
---|
425 | |
---|
426 | |
---|
427 | /** |
---|
428 | * @returns the number of synchronizeables registered to this stream |
---|
429 | */ |
---|
430 | int NetworkStream::getSyncCount() |
---|
431 | { |
---|
432 | int n = 0; |
---|
433 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
434 | if( (*it)->beSynchronized() == true) |
---|
435 | ++n; |
---|
436 | |
---|
437 | //return synchronizeables.size(); |
---|
438 | return n; |
---|
439 | } |
---|
440 | |
---|
441 | |
---|
442 | /** |
---|
443 | * check if handshakes completed. if so create the network game manager else remove it again |
---|
444 | */ |
---|
445 | void NetworkStream::handleHandshakes( ) |
---|
446 | { |
---|
447 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
448 | { |
---|
449 | if ( it->second.handshake ) |
---|
450 | { |
---|
451 | // handshake finished |
---|
452 | if ( it->second.handshake->completed() ) |
---|
453 | { |
---|
454 | //handshake is correct |
---|
455 | if ( it->second.handshake->ok() ) |
---|
456 | { |
---|
457 | if ( !it->second.handshake->allowDel() ) |
---|
458 | { |
---|
459 | if ( this->pInfo->nodeType == NET_CLIENT ) |
---|
460 | { |
---|
461 | SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); |
---|
462 | this->pInfo->userId = SharedNetworkData::getInstance()->getHostID(); |
---|
463 | |
---|
464 | it->second.nodeType = it->second.handshake->getRemoteNodeType(); |
---|
465 | this->networkMonitor->addNode(&it->second); |
---|
466 | |
---|
467 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
468 | this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() ); |
---|
469 | MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() ); |
---|
470 | } |
---|
471 | |
---|
472 | |
---|
473 | PRINT(0)("handshake finished id=%d\n", it->second.handshake->getNetworkGameManagerId()); |
---|
474 | |
---|
475 | it->second.handshake->del(); |
---|
476 | } |
---|
477 | else |
---|
478 | { |
---|
479 | // handsheke finished registring new player |
---|
480 | if ( it->second.handshake->canDel() ) |
---|
481 | { |
---|
482 | if ( this->pInfo->nodeType == NET_MASTER_SERVER ) |
---|
483 | { |
---|
484 | it->second.nodeType = it->second.handshake->getRemoteNodeType(); |
---|
485 | this->networkMonitor->addNode(&it->second); |
---|
486 | |
---|
487 | handleNewClient( it->second.userId ); |
---|
488 | |
---|
489 | if ( PlayerStats::getStats( it->second.userId ) && it->second.handshake->getPreferedNickName() != "" ) |
---|
490 | { |
---|
491 | PlayerStats::getStats( it->second.userId )->setNickName( it->second.handshake->getPreferedNickName() ); |
---|
492 | } |
---|
493 | } |
---|
494 | |
---|
495 | PRINT(0)("handshake finished delete it\n"); |
---|
496 | delete it->second.handshake; |
---|
497 | it->second.handshake = NULL; |
---|
498 | } |
---|
499 | } |
---|
500 | |
---|
501 | } |
---|
502 | else |
---|
503 | { |
---|
504 | PRINT(1)("handshake failed!\n"); |
---|
505 | it->second.socket->disconnectServer(); |
---|
506 | } |
---|
507 | } |
---|
508 | } |
---|
509 | } |
---|
510 | } |
---|
511 | |
---|
512 | |
---|
513 | /** |
---|
514 | * handle upstream network traffic |
---|
515 | */ |
---|
516 | void NetworkStream::handleUpstream( int tick ) |
---|
517 | { |
---|
518 | int offset; |
---|
519 | int n; |
---|
520 | |
---|
521 | for ( PeerList::reverse_iterator peer = peers.rbegin(); peer != peers.rend(); peer++ ) |
---|
522 | { |
---|
523 | offset = INTSIZE; // reserve enough space for the packet length |
---|
524 | |
---|
525 | // continue with the next peer if this peer has no socket assigned (therefore no network) |
---|
526 | if ( !peer->second.socket ) |
---|
527 | continue; |
---|
528 | |
---|
529 | // header informations: current state |
---|
530 | n = Converter::intToByteArray( currentState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
531 | assert( n == INTSIZE ); |
---|
532 | offset += n; |
---|
533 | |
---|
534 | // header informations: last acked state |
---|
535 | n = Converter::intToByteArray( peer->second.lastAckedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
536 | assert( n == INTSIZE ); |
---|
537 | offset += n; |
---|
538 | |
---|
539 | // header informations: last recved state |
---|
540 | n = Converter::intToByteArray( peer->second.lastRecvedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
541 | assert( n == INTSIZE ); |
---|
542 | offset += n; |
---|
543 | |
---|
544 | // now write all synchronizeables in the packet |
---|
545 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
546 | { |
---|
547 | int oldOffset = offset; |
---|
548 | Synchronizeable & sync = **it; |
---|
549 | |
---|
550 | // do not include synchronizeables with uninit id and syncs that don't want to be synchronized |
---|
551 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
---|
552 | continue; |
---|
553 | |
---|
554 | // if handshake not finished only sync handshake |
---|
555 | if ( peer->second.handshake && sync.getLeafClassID() != CL_HANDSHAKE ) |
---|
556 | continue; |
---|
557 | |
---|
558 | // if we are a server and this is not our handshake |
---|
559 | if ( isMasterServer() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId ) |
---|
560 | continue; |
---|
561 | |
---|
562 | /* list of synchronizeables that will never be synchronized over the network: */ |
---|
563 | // do not sync null parent |
---|
564 | if ( sync.getLeafClassID() == CL_NULL_PARENT ) |
---|
565 | continue; |
---|
566 | |
---|
567 | assert( offset + INTSIZE <= UDP_PACKET_SIZE ); |
---|
568 | |
---|
569 | // server fakes uniqueid == 0 for handshake |
---|
570 | if ( this->isMasterServer() && sync.getUniqueID() < MAX_CONNECTIONS - 1 ) |
---|
571 | n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
572 | else |
---|
573 | n = Converter::intToByteArray( sync.getUniqueID(), buf + offset, UDP_PACKET_SIZE - offset ); |
---|
574 | |
---|
575 | assert( n == INTSIZE ); |
---|
576 | offset += n; |
---|
577 | |
---|
578 | // make space for size |
---|
579 | offset += INTSIZE; |
---|
580 | |
---|
581 | n = sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, -1000 ); |
---|
582 | offset += n; |
---|
583 | |
---|
584 | assert( Converter::intToByteArray( n, buf + offset - n - INTSIZE, INTSIZE ) == INTSIZE ); |
---|
585 | |
---|
586 | // check if all data bytes == 0 -> remove data and the synchronizeable from the sync process since there is no update |
---|
587 | // TODO not all synchronizeables like this maybe add Synchronizeable::canRemoveZeroDiff() |
---|
588 | bool allZero = true; |
---|
589 | for ( int i = 0; i < n; i++ ) |
---|
590 | { |
---|
591 | if ( buf[i+oldOffset+2*INTSIZE] != 0 ) |
---|
592 | allZero = false; |
---|
593 | } |
---|
594 | // if there is no new data in this synchronizeable reset the data offset to the last state -> dont synchronizes |
---|
595 | // data that hast not changed |
---|
596 | if ( allZero ) |
---|
597 | { |
---|
598 | offset = oldOffset; |
---|
599 | } |
---|
600 | } // all synchronizeables written |
---|
601 | |
---|
602 | |
---|
603 | |
---|
604 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
605 | { |
---|
606 | Synchronizeable & sync = **it; |
---|
607 | |
---|
608 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
---|
609 | continue; |
---|
610 | |
---|
611 | sync.handleSentState( peer->second.userId, currentState, peer->second.lastAckedState ); |
---|
612 | } |
---|
613 | |
---|
614 | |
---|
615 | assert( Converter::intToByteArray( offset, buf, INTSIZE ) == INTSIZE ); |
---|
616 | |
---|
617 | // now compress the data with the zip library |
---|
618 | int compLength = 0; |
---|
619 | if ( this->isMasterServer() ) |
---|
620 | compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictServer ); |
---|
621 | else |
---|
622 | compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictClient ); |
---|
623 | |
---|
624 | if ( compLength <= 0 ) |
---|
625 | { |
---|
626 | PRINTF(1)("compression failed!\n"); |
---|
627 | continue; |
---|
628 | } |
---|
629 | |
---|
630 | assert( peer->second.socket->writePacket( compBuf, compLength ) ); |
---|
631 | |
---|
632 | if ( this->remainingBytesToWriteToDict > 0 ) |
---|
633 | writeToNewDict( buf, offset, true ); |
---|
634 | |
---|
635 | peer->second.connectionMonitor->processUnzippedOutgoingPacket( tick, buf, offset, currentState ); |
---|
636 | peer->second.connectionMonitor->processZippedOutgoingPacket( tick, compBuf, compLength, currentState ); |
---|
637 | |
---|
638 | } |
---|
639 | } |
---|
640 | |
---|
641 | /** |
---|
642 | * handle downstream network traffic |
---|
643 | */ |
---|
644 | void NetworkStream::handleDownstream( int tick ) |
---|
645 | { |
---|
646 | int offset = 0; |
---|
647 | |
---|
648 | int length = 0; |
---|
649 | int packetLength = 0; |
---|
650 | int compLength = 0; |
---|
651 | int uniqueId = 0; |
---|
652 | int state = 0; |
---|
653 | int ackedState = 0; |
---|
654 | int fromState = 0; |
---|
655 | int syncDataLength = 0; |
---|
656 | |
---|
657 | for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ ) |
---|
658 | { |
---|
659 | |
---|
660 | if ( !peer->second.socket ) |
---|
661 | continue; |
---|
662 | |
---|
663 | while ( 0 < (compLength = peer->second.socket->readPacket( compBuf, UDP_PACKET_SIZE )) ) |
---|
664 | { |
---|
665 | peer->second.connectionMonitor->processZippedIncomingPacket( tick, compBuf, compLength ); |
---|
666 | |
---|
667 | packetLength = Zip::getInstance()->unZip( compBuf, compLength, buf, UDP_PACKET_SIZE ); |
---|
668 | |
---|
669 | if ( packetLength < 4*INTSIZE ) |
---|
670 | { |
---|
671 | if ( packetLength != 0 ) |
---|
672 | PRINTF(1)("got too small packet: %d\n", packetLength); |
---|
673 | continue; |
---|
674 | } |
---|
675 | |
---|
676 | if ( this->remainingBytesToWriteToDict > 0 ) |
---|
677 | writeToNewDict( buf, packetLength, false ); |
---|
678 | |
---|
679 | assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE ); |
---|
680 | assert( Converter::byteArrayToInt( buf + INTSIZE, &state ) == INTSIZE ); |
---|
681 | assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE ); |
---|
682 | assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE ); |
---|
683 | offset = 4*INTSIZE; |
---|
684 | |
---|
685 | peer->second.connectionMonitor->processUnzippedIncomingPacket( tick, buf, packetLength, state, ackedState ); |
---|
686 | |
---|
687 | |
---|
688 | //if this is an old state drop it |
---|
689 | if ( state <= peer->second.lastRecvedState ) |
---|
690 | continue; |
---|
691 | |
---|
692 | if ( packetLength != length ) |
---|
693 | { |
---|
694 | PRINTF(1)("real packet length (%d) and transmitted packet length (%d) do not match!\n", packetLength, length); |
---|
695 | peer->second.socket->disconnectServer(); |
---|
696 | continue; |
---|
697 | } |
---|
698 | |
---|
699 | while ( offset + 2*INTSIZE < length ) |
---|
700 | { |
---|
701 | assert( offset > 0 ); |
---|
702 | assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE ); |
---|
703 | offset += INTSIZE; |
---|
704 | |
---|
705 | assert( Converter::byteArrayToInt( buf + offset, &syncDataLength ) == INTSIZE ); |
---|
706 | offset += INTSIZE; |
---|
707 | |
---|
708 | assert( syncDataLength > 0 ); |
---|
709 | assert( syncDataLength < 10000 ); |
---|
710 | |
---|
711 | Synchronizeable * sync = NULL; |
---|
712 | |
---|
713 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
714 | { |
---|
715 | // client thinks his handshake has id 0!!!!! |
---|
716 | if ( (*it)->getUniqueID() == uniqueId || ( uniqueId == 0 && (*it)->getUniqueID() == peer->second.userId ) ) |
---|
717 | { |
---|
718 | sync = *it; |
---|
719 | break; |
---|
720 | } |
---|
721 | } |
---|
722 | |
---|
723 | if ( sync == NULL ) |
---|
724 | { |
---|
725 | PRINTF(0)("could not find sync with id %d. try to create it\n", uniqueId); |
---|
726 | if ( oldSynchronizeables.find( uniqueId ) != oldSynchronizeables.end() ) |
---|
727 | { |
---|
728 | offset += syncDataLength; |
---|
729 | continue; |
---|
730 | } |
---|
731 | |
---|
732 | if ( !peers[peer->second.userId].isMasterServer() ) |
---|
733 | { |
---|
734 | offset += syncDataLength; |
---|
735 | continue; |
---|
736 | } |
---|
737 | |
---|
738 | int leafClassId; |
---|
739 | if ( INTSIZE > length - offset ) |
---|
740 | { |
---|
741 | offset += syncDataLength; |
---|
742 | continue; |
---|
743 | } |
---|
744 | |
---|
745 | Converter::byteArrayToInt( buf + offset, &leafClassId ); |
---|
746 | |
---|
747 | assert( leafClassId != 0 ); |
---|
748 | |
---|
749 | BaseObject * b = NULL; |
---|
750 | /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ |
---|
751 | /* Exception 1: NullParent */ |
---|
752 | if( leafClassId == CL_NULL_PARENT || leafClassId == CL_SYNCHRONIZEABLE || leafClassId == CL_NETWORK_GAME_MANAGER ) |
---|
753 | { |
---|
754 | PRINTF(1)("Can not create Class with ID %x!\n", (int)leafClassId); |
---|
755 | offset += syncDataLength; |
---|
756 | continue; |
---|
757 | } |
---|
758 | else |
---|
759 | b = Factory::fabricate( (ClassID)leafClassId ); |
---|
760 | |
---|
761 | if ( !b ) |
---|
762 | { |
---|
763 | PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId); |
---|
764 | offset += syncDataLength; |
---|
765 | continue; |
---|
766 | } |
---|
767 | |
---|
768 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
769 | { |
---|
770 | sync = dynamic_cast<Synchronizeable*>(b); |
---|
771 | sync->setUniqueID( uniqueId ); |
---|
772 | sync->setSynchronized(true); |
---|
773 | |
---|
774 | PRINTF(0)("Fabricated %s with id %d\n", sync->getClassName(), sync->getUniqueID()); |
---|
775 | } |
---|
776 | else |
---|
777 | { |
---|
778 | PRINTF(1)("Class with ID %x is not a synchronizeable!\n", (int)leafClassId); |
---|
779 | delete b; |
---|
780 | offset += syncDataLength; |
---|
781 | continue; |
---|
782 | } |
---|
783 | } |
---|
784 | |
---|
785 | |
---|
786 | int n = sync->setStateDiff( peer->second.userId, buf+offset, syncDataLength, state, fromState ); |
---|
787 | offset += n; |
---|
788 | //NETPRINTF(0)("SSSSSEEEEETTTTT: %s %d\n",sync->getClassName(), n); |
---|
789 | |
---|
790 | } |
---|
791 | |
---|
792 | if ( offset != length ) |
---|
793 | { |
---|
794 | PRINTF(0)("offset (%d) != length (%d)\n", offset, length); |
---|
795 | peer->second.socket->disconnectServer(); |
---|
796 | } |
---|
797 | |
---|
798 | |
---|
799 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
800 | { |
---|
801 | Synchronizeable & sync = **it; |
---|
802 | |
---|
803 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
---|
804 | continue; |
---|
805 | |
---|
806 | sync.handleRecvState( peer->second.userId, state, fromState ); |
---|
807 | } |
---|
808 | |
---|
809 | assert( peer->second.lastAckedState <= ackedState ); |
---|
810 | peer->second.lastAckedState = ackedState; |
---|
811 | |
---|
812 | assert( peer->second.lastRecvedState < state ); |
---|
813 | peer->second.lastRecvedState = state; |
---|
814 | |
---|
815 | } |
---|
816 | |
---|
817 | } |
---|
818 | |
---|
819 | } |
---|
820 | |
---|
821 | /** |
---|
822 | * is executed when a handshake has finished |
---|
823 | */ |
---|
824 | void NetworkStream::handleNewClient( int userId ) |
---|
825 | { |
---|
826 | // init and assign the message manager |
---|
827 | MessageManager::getInstance()->initUser( userId ); |
---|
828 | // do all game relevant stuff here |
---|
829 | networkGameManager->signalNewPlayer( userId ); |
---|
830 | |
---|
831 | // register the new client at the network monitor |
---|
832 | // this->networkMonitor->addClient(); |
---|
833 | } |
---|
834 | |
---|
835 | |
---|
836 | /** |
---|
837 | * removes old items from oldSynchronizeables |
---|
838 | */ |
---|
839 | void NetworkStream::cleanUpOldSyncList( ) |
---|
840 | { |
---|
841 | int now = SDL_GetTicks(); |
---|
842 | |
---|
843 | for ( std::map<int,int>::iterator it = oldSynchronizeables.begin(); it != oldSynchronizeables.end(); ) |
---|
844 | { |
---|
845 | if ( it->second < now - 10*1000 ) |
---|
846 | { |
---|
847 | std::map<int,int>::iterator delIt = it; |
---|
848 | it++; |
---|
849 | oldSynchronizeables.erase( delIt ); |
---|
850 | continue; |
---|
851 | } |
---|
852 | it++; |
---|
853 | } |
---|
854 | } |
---|
855 | |
---|
856 | /** |
---|
857 | * writes data to DATA/dicts/newdict |
---|
858 | * @param data pointer to data |
---|
859 | * @param length length |
---|
860 | */ |
---|
861 | void NetworkStream::writeToNewDict( byte * data, int length, bool upstream ) |
---|
862 | { |
---|
863 | if ( remainingBytesToWriteToDict <= 0 ) |
---|
864 | return; |
---|
865 | |
---|
866 | if ( length > remainingBytesToWriteToDict ) |
---|
867 | length = remainingBytesToWriteToDict; |
---|
868 | |
---|
869 | std::string fileName = ResourceManager::getInstance()->getDataDir(); |
---|
870 | fileName += "/dicts/newdict"; |
---|
871 | |
---|
872 | if ( upstream ) |
---|
873 | fileName += "_upstream"; |
---|
874 | else |
---|
875 | fileName += "_downstream"; |
---|
876 | |
---|
877 | FILE * f = fopen( fileName.c_str(), "a" ); |
---|
878 | |
---|
879 | if ( !f ) |
---|
880 | { |
---|
881 | PRINTF(2)("could not open %s\n", fileName.c_str()); |
---|
882 | remainingBytesToWriteToDict = 0; |
---|
883 | return; |
---|
884 | } |
---|
885 | |
---|
886 | if ( fwrite( data, 1, length, f ) != length ) |
---|
887 | { |
---|
888 | PRINTF(2)("could not write to file\n"); |
---|
889 | fclose( f ); |
---|
890 | return; |
---|
891 | } |
---|
892 | |
---|
893 | fclose( f ); |
---|
894 | |
---|
895 | remainingBytesToWriteToDict -= length; |
---|
896 | } |
---|
897 | |
---|
898 | |
---|
899 | |
---|
900 | |
---|
901 | |
---|
902 | |
---|