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 patrick@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 (patrick@orxonox.ethz.ch) |
---|
17 | */ |
---|
18 | |
---|
19 | |
---|
20 | #define DEBUG_MODULE_NETWORK |
---|
21 | |
---|
22 | #include "proxy/proxy_control.h" |
---|
23 | |
---|
24 | #include "base_object.h" |
---|
25 | #include "network_protocol.h" |
---|
26 | #include "udp_socket.h" |
---|
27 | #include "udp_server_socket.h" |
---|
28 | #include "monitor/connection_monitor.h" |
---|
29 | #include "monitor/network_monitor.h" |
---|
30 | #include "synchronizeable.h" |
---|
31 | #include "ip.h" |
---|
32 | #include "network_game_manager.h" |
---|
33 | #include "shared_network_data.h" |
---|
34 | #include "message_manager.h" |
---|
35 | #include "preferences.h" |
---|
36 | #include "zip.h" |
---|
37 | |
---|
38 | #include "src/lib/util/loading/resource_manager.h" |
---|
39 | |
---|
40 | #include "network_log.h" |
---|
41 | |
---|
42 | #include "player_stats.h" |
---|
43 | |
---|
44 | #include "lib/util/loading/factory.h" |
---|
45 | |
---|
46 | #include "debug.h" |
---|
47 | #include "class_list.h" |
---|
48 | #include <algorithm> |
---|
49 | |
---|
50 | |
---|
51 | #include "network_stream.h" |
---|
52 | |
---|
53 | |
---|
54 | #include "converter.h" |
---|
55 | |
---|
56 | |
---|
57 | #define PACKAGE_SIZE 256 |
---|
58 | |
---|
59 | |
---|
60 | /** |
---|
61 | * empty constructor |
---|
62 | */ |
---|
63 | NetworkStream::NetworkStream() |
---|
64 | : DataStream() |
---|
65 | { |
---|
66 | this->init(); |
---|
67 | /* initialize the references */ |
---|
68 | this->pInfo->nodeType = NET_UNASSIGNED; |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | NetworkStream::NetworkStream( int nodeType) |
---|
73 | { |
---|
74 | this->init(); |
---|
75 | |
---|
76 | this->pInfo->nodeType = nodeType; |
---|
77 | |
---|
78 | switch( nodeType) |
---|
79 | { |
---|
80 | case NET_MASTER_SERVER: |
---|
81 | // init the shared network data |
---|
82 | SharedNetworkData::getInstance()->setHostID(NET_ID_MASTER_SERVER); |
---|
83 | break; |
---|
84 | case NET_PROXY_SERVER_ACTIVE: |
---|
85 | // init the shared network data |
---|
86 | SharedNetworkData::getInstance()->setHostID(NET_ID_PROXY_SERVER_01); |
---|
87 | break; |
---|
88 | case NET_PROXY_SERVER_PASSIVE: |
---|
89 | // init the shared network data |
---|
90 | SharedNetworkData::getInstance()->setHostID(NET_ID_PROXY_SERVER_01); |
---|
91 | break; |
---|
92 | case NET_CLIENT: |
---|
93 | SharedNetworkData::getInstance()->setHostID(NET_ID_UNASSIGNED); |
---|
94 | break; |
---|
95 | } |
---|
96 | |
---|
97 | SharedNetworkData::getInstance()->setDefaultSyncStream(this); |
---|
98 | |
---|
99 | // get the local ip address |
---|
100 | IPaddress ip; |
---|
101 | SDLNet_ResolveHost( &ip, NULL, 0); |
---|
102 | this->pInfo->ip = ip; |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | /** |
---|
108 | * generic init functions |
---|
109 | */ |
---|
110 | void NetworkStream::init() |
---|
111 | { |
---|
112 | /* set the class id for the base object */ |
---|
113 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
114 | this->clientSocket = NULL; |
---|
115 | this->proxySocket = NULL; |
---|
116 | this->networkGameManager = NULL; |
---|
117 | this->networkMonitor = NULL; |
---|
118 | |
---|
119 | this->pInfo = new PeerInfo(); |
---|
120 | this->pInfo->userId = NET_UID_UNASSIGNED; |
---|
121 | this->pInfo->lastAckedState = 0; |
---|
122 | this->pInfo->lastRecvedState = 0; |
---|
123 | this->pInfo->bLocal = true; |
---|
124 | |
---|
125 | this->bRedirect = false; |
---|
126 | |
---|
127 | this->currentState = 0; |
---|
128 | |
---|
129 | remainingBytesToWriteToDict = Preferences::getInstance()->getInt( "compression", "writedict", 0 ); |
---|
130 | |
---|
131 | assert( Zip::getInstance()->loadDictionary( "testdict" ) >= 0 ); |
---|
132 | this->dictClient = Zip::getInstance()->loadDictionary( "dict2pl_client" ); |
---|
133 | assert( this->dictClient >= 0 ); |
---|
134 | this->dictServer = Zip::getInstance()->loadDictionary( "dict2p_server" ); |
---|
135 | assert( this->dictServer >= 0 ); |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | /** |
---|
140 | * deconstructor |
---|
141 | */ |
---|
142 | NetworkStream::~NetworkStream() |
---|
143 | { |
---|
144 | if ( this->clientSocket ) |
---|
145 | { |
---|
146 | clientSocket->close(); |
---|
147 | delete clientSocket; |
---|
148 | clientSocket = NULL; |
---|
149 | } |
---|
150 | if ( this->proxySocket) |
---|
151 | { |
---|
152 | proxySocket->close(); |
---|
153 | delete proxySocket; |
---|
154 | proxySocket = NULL; |
---|
155 | } |
---|
156 | for ( PeerList::iterator i = peers.begin(); i!=peers.end(); i++) |
---|
157 | { |
---|
158 | if ( i->second.socket ) |
---|
159 | { |
---|
160 | i->second.socket->disconnectServer(); |
---|
161 | delete i->second.socket; |
---|
162 | i->second.socket = NULL; |
---|
163 | } |
---|
164 | |
---|
165 | if ( i->second.handshake ) |
---|
166 | { |
---|
167 | delete i->second.handshake; |
---|
168 | i->second.handshake = NULL; |
---|
169 | } |
---|
170 | |
---|
171 | if ( i->second.connectionMonitor ) |
---|
172 | { |
---|
173 | delete i->second.connectionMonitor; |
---|
174 | i->second.connectionMonitor = NULL; |
---|
175 | } |
---|
176 | } |
---|
177 | for ( SynchronizeableList::const_iterator it = getSyncBegin(); it != getSyncEnd(); it ++ ) |
---|
178 | (*it)->setNetworkStream( NULL ); |
---|
179 | |
---|
180 | if( this->pInfo) |
---|
181 | delete this->pInfo; |
---|
182 | |
---|
183 | if( this->networkMonitor) |
---|
184 | delete this->networkMonitor; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | /** |
---|
189 | * establish a connection to a remote master server |
---|
190 | * @param host: host name |
---|
191 | * @param port: the port number |
---|
192 | */ |
---|
193 | void NetworkStream::connectToMasterServer(std::string host, int port) |
---|
194 | { |
---|
195 | int node = NET_ID_MASTER_SERVER; |
---|
196 | // this create the new node in the peers map |
---|
197 | this->peers[node].socket = new UdpSocket( host, port ); |
---|
198 | this->peers[node].userId = NET_ID_MASTER_SERVER; |
---|
199 | |
---|
200 | this->peers[node].nodeType = NET_MASTER_SERVER; |
---|
201 | this->peers[node].connectionMonitor = new ConnectionMonitor( NET_ID_MASTER_SERVER ); |
---|
202 | this->peers[node].ip = this->peers[node].socket->getRemoteAddress(); |
---|
203 | this->peers[node].bLocal = true; |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | /** |
---|
208 | * establish a connection to a remote proxy server |
---|
209 | * @param host: host name |
---|
210 | * @param port: the port number |
---|
211 | */ |
---|
212 | void NetworkStream::connectToProxyServer(int proxyId, std::string host, int port) |
---|
213 | { |
---|
214 | PRINTF(0)("connect to proxy %s, this is proxyId %i\n", host.c_str(), proxyId); |
---|
215 | |
---|
216 | // this creates the new proxyId in the peers map |
---|
217 | this->peers[proxyId].socket = new UdpSocket( host, port ); |
---|
218 | this->peers[proxyId].userId = proxyId; |
---|
219 | |
---|
220 | this->peers[proxyId].nodeType = NET_PROXY_SERVER_ACTIVE; |
---|
221 | this->peers[proxyId].connectionMonitor = new ConnectionMonitor( proxyId ); |
---|
222 | this->peers[proxyId].ip = this->peers[proxyId].socket->getRemoteAddress(); |
---|
223 | this->peers[proxyId].bLocal = true; |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | /** |
---|
228 | * create a server |
---|
229 | * @param port: interface port for all clients |
---|
230 | */ |
---|
231 | void NetworkStream::createServer(int clientPort, int proxyPort) |
---|
232 | { |
---|
233 | PRINTF(0)(" Creating new Server: listening for clients on port %i and for proxies on port %i", clientPort, proxyPort); |
---|
234 | this->clientSocket= new UdpServerSocket(clientPort); |
---|
235 | this->proxySocket = new UdpServerSocket(proxyPort); |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | /** |
---|
240 | * creates a new instance of the network game manager |
---|
241 | */ |
---|
242 | void NetworkStream::createNetworkGameManager() |
---|
243 | { |
---|
244 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
245 | |
---|
246 | this->networkGameManager->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
247 | MessageManager::getInstance()->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | /** |
---|
252 | * starts the network handshake |
---|
253 | * handsakes are always initialized from the client side first. this starts the handshake and therefore is only |
---|
254 | * executed as client |
---|
255 | * @param userId: start handshake for this user id (optional, default == 0) |
---|
256 | */ |
---|
257 | void NetworkStream::startHandshake(int userId) |
---|
258 | { |
---|
259 | Handshake* hs = new Handshake(this->pInfo->nodeType); |
---|
260 | // fake the unique id |
---|
261 | hs->setUniqueID( userId); |
---|
262 | assert( this->peers[userId].handshake == NULL ); |
---|
263 | this->peers[userId].handshake = hs; |
---|
264 | this->peers[userId].bLocal = true; |
---|
265 | |
---|
266 | // set the preferred nick name |
---|
267 | hs->setPreferedNickName( Preferences::getInstance()->getString( "multiplayer", "nickname", "Player" ) ); |
---|
268 | |
---|
269 | PRINTF(0)("NetworkStream: Handshake created: %s\n", hs->getCName()); |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | /** |
---|
274 | * this functions connects a synchronizeable to the networkstream, therefore synchronizeing |
---|
275 | * it all over the network and creating it on the other platforms (if and only if it is a |
---|
276 | * server |
---|
277 | * @param sync: the synchronizeable to add |
---|
278 | */ |
---|
279 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
---|
280 | { |
---|
281 | this->synchronizeables.push_back(&sync); |
---|
282 | sync.setNetworkStream( this ); |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | /** |
---|
287 | * removes the synchronizeable from the list of synchronized entities |
---|
288 | * @param sync: the syncronizeable to remove |
---|
289 | */ |
---|
290 | void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync) |
---|
291 | { |
---|
292 | // removing the Synchronizeable from the List. |
---|
293 | std::list<Synchronizeable*>::iterator disconnectSynchro = std::find(this->synchronizeables.begin(), this->synchronizeables.end(), &sync); |
---|
294 | if (disconnectSynchro != this->synchronizeables.end()) |
---|
295 | this->synchronizeables.erase(disconnectSynchro); |
---|
296 | |
---|
297 | oldSynchronizeables[sync.getUniqueID()] = SDL_GetTicks(); |
---|
298 | } |
---|
299 | |
---|
300 | |
---|
301 | /** |
---|
302 | * this is called to process data from the network socket to the synchronizeable and vice versa |
---|
303 | */ |
---|
304 | void NetworkStream::processData() |
---|
305 | { |
---|
306 | // create the network monitor after all the init work and before there is any connection handlings |
---|
307 | if( this->networkMonitor == NULL) |
---|
308 | { |
---|
309 | this->networkMonitor = new NetworkMonitor(this); |
---|
310 | SharedNetworkData::getInstance()->setNetworkMonitor( this->networkMonitor); |
---|
311 | } |
---|
312 | |
---|
313 | |
---|
314 | int tick = SDL_GetTicks(); |
---|
315 | |
---|
316 | this->currentState++; |
---|
317 | // there was a wrap around |
---|
318 | if( this->currentState < 0) |
---|
319 | { |
---|
320 | 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"); |
---|
321 | } |
---|
322 | |
---|
323 | if ( SharedNetworkData::getInstance()->isMasterServer()) |
---|
324 | { |
---|
325 | // execute everytthing the master server shoudl do |
---|
326 | if ( this->clientSocket ) |
---|
327 | this->clientSocket->update(); |
---|
328 | if( this->proxySocket) |
---|
329 | this->proxySocket->update(); |
---|
330 | |
---|
331 | this->updateConnectionList(); |
---|
332 | } |
---|
333 | else if( SharedNetworkData::getInstance()->isProxyServerActive()) |
---|
334 | { |
---|
335 | //execute everything the proxy server should do |
---|
336 | if ( this->clientSocket ) |
---|
337 | this->clientSocket->update(); |
---|
338 | if( this->proxySocket) |
---|
339 | this->proxySocket->update(); |
---|
340 | |
---|
341 | this->updateConnectionList(); |
---|
342 | } |
---|
343 | |
---|
344 | #warning make this more modular: every proxy/master server connection should be watched for termination |
---|
345 | if( !SharedNetworkData::getInstance()->isMasterServer()) |
---|
346 | { |
---|
347 | // check if the connection is ok else terminate and remove |
---|
348 | if ( !peers.empty() && peers[NET_ID_MASTER_SERVER].socket && |
---|
349 | ( !peers[NET_ID_MASTER_SERVER].socket->isOk() || |
---|
350 | peers[NET_ID_MASTER_SERVER].connectionMonitor->hasTimedOut() ) ) |
---|
351 | { |
---|
352 | this->handleDisconnect( NET_ID_MASTER_SERVER); |
---|
353 | PRINTF(1)("lost connection to server\n"); |
---|
354 | } |
---|
355 | // check if there is a redirection command |
---|
356 | if( this->bRedirect) |
---|
357 | { |
---|
358 | this->handleReconnect( NET_ID_MASTER_SERVER); |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | this->cleanUpOldSyncList(); |
---|
363 | this->handleHandshakes(); |
---|
364 | |
---|
365 | // update the network monitor |
---|
366 | this->networkMonitor->process(); |
---|
367 | |
---|
368 | // order of up/downstream is important!!!! |
---|
369 | // don't change it |
---|
370 | this->handleDownstream( tick ); |
---|
371 | this->handleUpstream( tick ); |
---|
372 | } |
---|
373 | |
---|
374 | |
---|
375 | /** |
---|
376 | * @brief handles incoming connections |
---|
377 | * |
---|
378 | * if we are a NET_MASTER_SERVER or NET_PROXY_SERVER_ACTIVE update the connection list to accept new connections (clients) |
---|
379 | * start and initialize the handsake for the new clients |
---|
380 | */ |
---|
381 | void NetworkStream::updateConnectionList( ) |
---|
382 | { |
---|
383 | //check for new connections |
---|
384 | |
---|
385 | NetworkSocket* tempNetworkSocket = NULL; |
---|
386 | int userId; |
---|
387 | |
---|
388 | if( this->clientSocket != NULL) |
---|
389 | { |
---|
390 | tempNetworkSocket = this->clientSocket->getNewSocket(); |
---|
391 | |
---|
392 | // we got new NET_CLIENT connecting |
---|
393 | if ( tempNetworkSocket ) |
---|
394 | { |
---|
395 | // get a userId |
---|
396 | // if ( freeSocketSlots.size() > 0 ) |
---|
397 | // { |
---|
398 | // // this should never be called |
---|
399 | // userId = freeSocketSlots.back(); |
---|
400 | // freeSocketSlots.pop_back(); |
---|
401 | // } |
---|
402 | // else |
---|
403 | { |
---|
404 | // each server (proxy and master) have an address space for new network nodes of 1000 nodes |
---|
405 | // the first NET_ID_PROXY_MAX are always reserved for proxy servers |
---|
406 | userId = SharedNetworkData::getInstance()->getHostID() * 1000 + NET_ID_PROXY_MAX + 1; |
---|
407 | |
---|
408 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
409 | if ( it->first >= userId ) |
---|
410 | userId = it->first + 1; |
---|
411 | |
---|
412 | // make sure that this server only uses an address space of 1000 |
---|
413 | assert( userId < (SharedNetworkData::getInstance()->getHostID() + 1) * 1000); |
---|
414 | } |
---|
415 | // this creates a new entry in the peers list |
---|
416 | peers[userId].socket = tempNetworkSocket; |
---|
417 | peers[userId].nodeType = NET_CLIENT; |
---|
418 | |
---|
419 | // handle the newly connected client |
---|
420 | this->handleConnect(userId); |
---|
421 | |
---|
422 | PRINTF(0)("New Client: %d\n", userId); |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | |
---|
427 | if( this->proxySocket != NULL) |
---|
428 | { |
---|
429 | tempNetworkSocket = this->proxySocket->getNewSocket(); |
---|
430 | |
---|
431 | // we got new NET_PROXY_SERVER_ACTIVE connecting |
---|
432 | if ( tempNetworkSocket ) |
---|
433 | { |
---|
434 | // determine the network node id |
---|
435 | // if ( freeSocketSlots.size() > 0 ) |
---|
436 | // { |
---|
437 | // userId = freeSocketSlots.back(); |
---|
438 | // freeSocketSlots.pop_back(); |
---|
439 | // } |
---|
440 | // else |
---|
441 | { |
---|
442 | userId = 1; |
---|
443 | |
---|
444 | for( int i = 0; i < NET_ID_PROXY_MAX; i++) |
---|
445 | { |
---|
446 | if( this->peers.find( i) != this->peers.end()) |
---|
447 | userId = i; |
---|
448 | break; |
---|
449 | } |
---|
450 | |
---|
451 | // for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
452 | // if ( it->first >= userId ) |
---|
453 | // userId = it->first + 1; |
---|
454 | } |
---|
455 | |
---|
456 | // this creates a new entry in the peers list |
---|
457 | peers[userId].socket = tempNetworkSocket; |
---|
458 | peers[userId].nodeType = NET_PROXY_SERVER_ACTIVE; |
---|
459 | |
---|
460 | // handle the newly connected proxy server |
---|
461 | this->handleConnect(userId); |
---|
462 | |
---|
463 | PRINTF(0)("New proxy connected: %d\n", userId); |
---|
464 | } |
---|
465 | } |
---|
466 | |
---|
467 | |
---|
468 | |
---|
469 | //check if connections are ok else remove them |
---|
470 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); ) |
---|
471 | { |
---|
472 | if ( |
---|
473 | it->second.socket && |
---|
474 | ( |
---|
475 | !it->second.socket->isOk() || |
---|
476 | it->second.connectionMonitor->hasTimedOut() |
---|
477 | ) |
---|
478 | ) |
---|
479 | { |
---|
480 | std::string reason = "disconnected"; |
---|
481 | if ( it->second.connectionMonitor->hasTimedOut() ) |
---|
482 | reason = "timeout"; |
---|
483 | PRINTF(0)("Client is gone: %d (%s)\n", it->second.userId, reason.c_str()); |
---|
484 | |
---|
485 | |
---|
486 | this->handleDisconnect( it->second.userId); |
---|
487 | |
---|
488 | if( SharedNetworkData::getInstance()->isProxyServerActive()) |
---|
489 | ProxyControl::getInstance()->signalLeaveClient(it->second.userId); |
---|
490 | |
---|
491 | it++; |
---|
492 | continue; |
---|
493 | } |
---|
494 | |
---|
495 | it++; |
---|
496 | } |
---|
497 | |
---|
498 | |
---|
499 | } |
---|
500 | |
---|
501 | |
---|
502 | /** |
---|
503 | * this handles new connections |
---|
504 | * @param userId: the id of the new user node |
---|
505 | */ |
---|
506 | void NetworkStream::handleConnect( int userId) |
---|
507 | { |
---|
508 | // create new handshake and init its variables |
---|
509 | this->peers[userId].handshake = new Handshake(this->pInfo->nodeType, userId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); |
---|
510 | this->peers[userId].handshake->setUniqueID(userId); |
---|
511 | |
---|
512 | this->peers[userId].connectionMonitor = new ConnectionMonitor( userId ); |
---|
513 | this->peers[userId].userId = userId; |
---|
514 | this->peers[userId].bLocal = true; |
---|
515 | |
---|
516 | PRINTF(0)("num sync: %d\n", synchronizeables.size()); |
---|
517 | |
---|
518 | // get the proxy server informations and write them to the handshake, if any (proxy) |
---|
519 | assert( this->networkMonitor != NULL); |
---|
520 | PeerInfo* pi = this->networkMonitor->getFirstChoiceProxy(); |
---|
521 | if( pi != NULL) |
---|
522 | { |
---|
523 | this->peers[userId].handshake->setProxy1Address( pi->ip); |
---|
524 | } |
---|
525 | pi = this->networkMonitor->getSecondChoiceProxy(); |
---|
526 | if( pi != NULL) |
---|
527 | this->peers[userId].handshake->setProxy2Address( pi->ip); |
---|
528 | |
---|
529 | // check if the connecting client should reconnect to a proxy server |
---|
530 | if( SharedNetworkData::getInstance()->isMasterServer()) |
---|
531 | { |
---|
532 | if( this->networkMonitor->isReconnectNextClient()) |
---|
533 | { |
---|
534 | this->peers[userId].handshake->setRedirect(true); |
---|
535 | PRINTF(0)("forwarding client to proxy server because this server is saturated\n"); |
---|
536 | } |
---|
537 | } |
---|
538 | |
---|
539 | // the connecting node of course is a client |
---|
540 | this->peers[userId].ip = this->peers[userId].socket->getRemoteAddress(); |
---|
541 | } |
---|
542 | |
---|
543 | |
---|
544 | |
---|
545 | /** |
---|
546 | * some debug output |
---|
547 | */ |
---|
548 | void NetworkStream::debug() |
---|
549 | { |
---|
550 | if( SharedNetworkData::getInstance()->isMasterServer()) { |
---|
551 | PRINT(0)(" Host ist Master Server with ID: %i\n", this->pInfo->userId); |
---|
552 | } |
---|
553 | else if( SharedNetworkData::getInstance()->isProxyServerActive()) { |
---|
554 | PRINT(0)(" Host ist Proxy Server with ID: %i\n", this->pInfo->userId); |
---|
555 | } |
---|
556 | else { |
---|
557 | PRINT(0)(" Host ist Client with ID: %i\n", this->pInfo->userId); |
---|
558 | } |
---|
559 | |
---|
560 | PRINT(0)(" Current number of connections is: %i\n", this->peers.size()); |
---|
561 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
562 | { |
---|
563 | PRINT(0)(" peers[%i] with uniqueId %i and address: %s\n", it->first, it->second.userId, it->second.ip.ipString().c_str()); |
---|
564 | } |
---|
565 | PRINT(0)("\n\n"); |
---|
566 | |
---|
567 | |
---|
568 | PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); |
---|
569 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
570 | { |
---|
571 | if( (*it)->beSynchronized() == true) |
---|
572 | PRINT(0)(" Synchronizeable of class: %s::%s, with unique ID: %i, Synchronize: %i\n", (*it)->getClassCName(), (*it)->getCName(), |
---|
573 | (*it)->getUniqueID(), (*it)->beSynchronized()); |
---|
574 | } |
---|
575 | PRINT(0)(" Maximal Connections: %i\n", SharedNetworkData::getInstance()->getMaxPlayer() ); |
---|
576 | |
---|
577 | } |
---|
578 | |
---|
579 | |
---|
580 | /** |
---|
581 | * @returns the number of synchronizeables registered to this stream |
---|
582 | */ |
---|
583 | int NetworkStream::getSyncCount() |
---|
584 | { |
---|
585 | int n = 0; |
---|
586 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
587 | if( (*it)->beSynchronized() == true) |
---|
588 | ++n; |
---|
589 | |
---|
590 | //return synchronizeables.size(); |
---|
591 | return n; |
---|
592 | } |
---|
593 | |
---|
594 | |
---|
595 | /** |
---|
596 | * check if handshakes completed. if so create the network game manager else remove it again |
---|
597 | */ |
---|
598 | void NetworkStream::handleHandshakes( ) |
---|
599 | { |
---|
600 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
601 | { |
---|
602 | if ( it->second.handshake ) |
---|
603 | { |
---|
604 | // handshake finished |
---|
605 | if ( it->second.handshake->completed() ) |
---|
606 | { |
---|
607 | //handshake is correct |
---|
608 | if ( it->second.handshake->ok() ) |
---|
609 | { |
---|
610 | // write the first informations into the node so they can be read from there for case differentiation |
---|
611 | it->second.nodeType = it->second.handshake->getRemoteNodeType(); |
---|
612 | |
---|
613 | // the counter part didn't mark it free for deletion yet |
---|
614 | if ( !it->second.handshake->allowDel() ) |
---|
615 | { |
---|
616 | // make sure this is a connection: |
---|
617 | // - client <==> master server |
---|
618 | // - proxy server <==> master server |
---|
619 | if( SharedNetworkData::getInstance()->isClient() || |
---|
620 | SharedNetworkData::getInstance()->isProxyServerActive() && |
---|
621 | SharedNetworkData::getInstance()->isUserMasterServer(it->second.userId)) |
---|
622 | { |
---|
623 | PRINTF(4)("Handshake: i am in client role\n"); |
---|
624 | |
---|
625 | SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); |
---|
626 | this->pInfo->userId = SharedNetworkData::getInstance()->getHostID(); |
---|
627 | |
---|
628 | // it->second.ip = it->second.socket->getRemoteAddress(); |
---|
629 | |
---|
630 | // it->second.nodeType = it->second.handshake->getRemoteNodeType(); |
---|
631 | // it->second.ip = it->second.socket->getRemoteAddress(); |
---|
632 | // add the new server to the nodes list (it can be a NET_MASTER_SERVER or NET_PROXY_SERVER) |
---|
633 | this->networkMonitor->addNode(&it->second); |
---|
634 | // get proxy 1 address and add it |
---|
635 | this->networkMonitor->addNode(it->second.handshake->getProxy1Address(), NET_PROXY_SERVER_ACTIVE); |
---|
636 | // get proxy 2 address and add it |
---|
637 | this->networkMonitor->addNode(it->second.handshake->getProxy2Address(), NET_PROXY_SERVER_ACTIVE); |
---|
638 | |
---|
639 | // now check if the server accepted the connection |
---|
640 | if( SharedNetworkData::getInstance()->isClient() && it->second.handshake->redirect() ) |
---|
641 | { |
---|
642 | this->bRedirect = true; |
---|
643 | } |
---|
644 | |
---|
645 | // create the new network game manager and init it |
---|
646 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
647 | this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() ); |
---|
648 | // init the new message manager |
---|
649 | MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() ); |
---|
650 | } |
---|
651 | |
---|
652 | PRINT(0)("handshake finished id=%d\n", it->second.handshake->getNetworkGameManagerId()); |
---|
653 | it->second.handshake->del(); |
---|
654 | |
---|
655 | } |
---|
656 | else |
---|
657 | { |
---|
658 | // handshake finished registring new player |
---|
659 | if ( it->second.handshake->canDel() ) |
---|
660 | { |
---|
661 | |
---|
662 | if ( SharedNetworkData::getInstance()->isMasterServer() ) |
---|
663 | { |
---|
664 | it->second.ip = it->second.socket->getRemoteAddress(); |
---|
665 | |
---|
666 | this->networkMonitor->addNode(&it->second); |
---|
667 | |
---|
668 | this->handleNewClient( it->second.userId ); |
---|
669 | |
---|
670 | if ( PlayerStats::getStats( it->second.userId ) && it->second.handshake->getPreferedNickName() != "" ) |
---|
671 | { |
---|
672 | PlayerStats::getStats( it->second.userId )->setNickName( it->second.handshake->getPreferedNickName() ); |
---|
673 | } |
---|
674 | } |
---|
675 | else if ( SharedNetworkData::getInstance()->isProxyServerActive() && it->second.isClient() ) |
---|
676 | { |
---|
677 | PRINTF(4)("Handshake: Proxy in server role: connecting %i\n", it->second.userId); |
---|
678 | |
---|
679 | it->second.ip = it->second.socket->getRemoteAddress(); |
---|
680 | |
---|
681 | this->networkMonitor->addNode(&it->second); |
---|
682 | |
---|
683 | // work with the ProxyControl to init the new client |
---|
684 | ProxyControl::getInstance()->signalNewClient( it->second.userId); |
---|
685 | |
---|
686 | if ( PlayerStats::getStats( it->second.userId ) && it->second.handshake->getPreferedNickName() != "" ) |
---|
687 | { |
---|
688 | PlayerStats::getStats( it->second.userId )->setNickName( it->second.handshake->getPreferedNickName() ); |
---|
689 | } |
---|
690 | } |
---|
691 | |
---|
692 | PRINT(0)("handshake finished delete it\n"); |
---|
693 | delete it->second.handshake; |
---|
694 | it->second.handshake = NULL; |
---|
695 | } |
---|
696 | } |
---|
697 | |
---|
698 | } |
---|
699 | else |
---|
700 | { |
---|
701 | PRINT(1)("handshake failed!\n"); |
---|
702 | it->second.socket->disconnectServer(); |
---|
703 | } |
---|
704 | } |
---|
705 | } |
---|
706 | } |
---|
707 | } |
---|
708 | |
---|
709 | |
---|
710 | /** |
---|
711 | * this functions handles a reconnect event received from the a NET_MASTER_SERVER or NET_PROXY_SERVER |
---|
712 | */ |
---|
713 | void NetworkStream::handleReconnect(int userId) |
---|
714 | { |
---|
715 | this->bRedirect = false; |
---|
716 | #warning this peer will be created if it does not yet exist: dangerous |
---|
717 | PeerInfo* pInfo = &this->peers[userId]; |
---|
718 | |
---|
719 | IP proxyIP; |
---|
720 | if( this->networkMonitor->isForcedReconnection()) |
---|
721 | proxyIP = this->networkMonitor->getForcedReconnectionIP(); |
---|
722 | else |
---|
723 | proxyIP = pInfo->handshake->getProxy1Address(); |
---|
724 | |
---|
725 | PRINTF(0)("===============================================\n"); |
---|
726 | PRINTF(0)("Client is redirected to the other proxy servers\n"); |
---|
727 | PRINTF(0)(" user id: %i\n", userId); |
---|
728 | PRINTF(0)(" connecting to: %s\n", proxyIP.ipString().c_str()); |
---|
729 | PRINTF(0)("===============================================\n"); |
---|
730 | |
---|
731 | // flush the old synchronization states, since the numbering could be completely different |
---|
732 | pInfo->lastAckedState = 0; |
---|
733 | pInfo->lastRecvedState = 0; |
---|
734 | |
---|
735 | // disconnect from the current server and reconnect to proxy server |
---|
736 | this->handleDisconnect( userId); |
---|
737 | // this->connectToProxyServer(NET_ID_PROXY_SERVER_01, proxyIP.ipString(), 9999); |
---|
738 | this->connectToMasterServer(proxyIP.ipString(), 9999); |
---|
739 | #warning the ports are not yet integrated correctly in the ip class |
---|
740 | |
---|
741 | // and restart the handshake |
---|
742 | this->startHandshake( userId); |
---|
743 | } |
---|
744 | |
---|
745 | |
---|
746 | |
---|
747 | /** |
---|
748 | * reconnects to another server, with full handshake |
---|
749 | * @param address of the new server |
---|
750 | */ |
---|
751 | void NetworkStream::reconnectToServer(IP address) |
---|
752 | { |
---|
753 | this->networkMonitor->setForcedReconnection(address); |
---|
754 | this->handleReconnect( NET_ID_MASTER_SERVER); |
---|
755 | } |
---|
756 | |
---|
757 | |
---|
758 | |
---|
759 | /** |
---|
760 | * softly reconnecting to another server |
---|
761 | * @param address of the new server |
---|
762 | */ |
---|
763 | void NetworkStream::softReconnectToServer(IP address) |
---|
764 | { |
---|
765 | this->networkMonitor->setForcedReconnection(address); |
---|
766 | this->handleReconnect( NET_ID_MASTER_SERVER); |
---|
767 | } |
---|
768 | |
---|
769 | |
---|
770 | /** |
---|
771 | * handles the disconnect event |
---|
772 | * @param userId id of the user to remove |
---|
773 | */ |
---|
774 | void NetworkStream::handleDisconnect( int userId ) |
---|
775 | { |
---|
776 | this->peers[userId].socket->disconnectServer(); |
---|
777 | delete this->peers[userId].socket; |
---|
778 | this->peers[userId].socket = NULL; |
---|
779 | |
---|
780 | if ( this->peers[userId].handshake ) |
---|
781 | delete this->peers[userId].handshake; |
---|
782 | this->peers[userId].handshake = NULL; |
---|
783 | |
---|
784 | if ( this->peers[userId].connectionMonitor ) |
---|
785 | delete this->peers[userId].connectionMonitor; |
---|
786 | this->peers[userId].connectionMonitor = NULL; |
---|
787 | |
---|
788 | |
---|
789 | for ( SynchronizeableList::iterator it2 = synchronizeables.begin(); it2 != synchronizeables.end(); it2++ ) { |
---|
790 | (*it2)->cleanUpUser( userId ); |
---|
791 | } |
---|
792 | |
---|
793 | if( SharedNetworkData::getInstance()->isMasterServer()) |
---|
794 | NetworkGameManager::getInstance()->signalLeftPlayer(userId); |
---|
795 | |
---|
796 | this->freeSocketSlots.push_back( userId ); |
---|
797 | |
---|
798 | this->networkMonitor->removeNode(&this->peers[userId]); |
---|
799 | this->peers.erase( userId); |
---|
800 | } |
---|
801 | |
---|
802 | |
---|
803 | |
---|
804 | /** |
---|
805 | * handle upstream network traffic |
---|
806 | * @param tick: seconds elapsed since last update |
---|
807 | */ |
---|
808 | void NetworkStream::handleUpstream( int tick ) |
---|
809 | { |
---|
810 | int offset; |
---|
811 | int n; |
---|
812 | |
---|
813 | for ( PeerList::reverse_iterator peer = peers.rbegin(); peer != peers.rend(); peer++ ) |
---|
814 | { |
---|
815 | offset = INTSIZE; // reserve enough space for the packet length |
---|
816 | |
---|
817 | // continue with the next peer if this peer has no socket assigned (therefore no network) |
---|
818 | if ( !peer->second.socket ) |
---|
819 | continue; |
---|
820 | |
---|
821 | // header informations: current state |
---|
822 | n = Converter::intToByteArray( currentState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
823 | assert( n == INTSIZE ); |
---|
824 | offset += n; |
---|
825 | |
---|
826 | // header informations: last acked state |
---|
827 | n = Converter::intToByteArray( peer->second.lastAckedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
828 | assert( n == INTSIZE ); |
---|
829 | offset += n; |
---|
830 | |
---|
831 | // header informations: last recved state |
---|
832 | n = Converter::intToByteArray( peer->second.lastRecvedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
833 | assert( n == INTSIZE ); |
---|
834 | offset += n; |
---|
835 | |
---|
836 | // now write all synchronizeables in the packet |
---|
837 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
838 | { |
---|
839 | |
---|
840 | int oldOffset = offset; |
---|
841 | Synchronizeable & sync = **it; |
---|
842 | |
---|
843 | |
---|
844 | // do not include synchronizeables with uninit id and syncs that don't want to be synchronized |
---|
845 | if ( !sync.beSynchronized() || sync.getUniqueID() <= NET_UID_UNASSIGNED ) |
---|
846 | continue; |
---|
847 | |
---|
848 | // if handshake not finished only sync handshake |
---|
849 | if ( peer->second.handshake && sync.getLeafClassID() != CL_HANDSHAKE ) |
---|
850 | continue; |
---|
851 | |
---|
852 | // if we are a server (both master and proxy servers) and this is not our handshake |
---|
853 | if ( ( SharedNetworkData::getInstance()->isMasterServer() || |
---|
854 | SharedNetworkData::getInstance()->isProxyServerActive() && peer->second.isClient()) |
---|
855 | && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId ) |
---|
856 | continue; |
---|
857 | |
---|
858 | /* list of synchronizeables that will never be synchronized over the network: */ |
---|
859 | // do not sync null parent |
---|
860 | if ( sync.getLeafClassID() == CL_NULL_PARENT ) |
---|
861 | continue; |
---|
862 | |
---|
863 | |
---|
864 | assert( sync.getLeafClassID() != 0); |
---|
865 | |
---|
866 | assert( offset + INTSIZE <= UDP_PACKET_SIZE ); |
---|
867 | |
---|
868 | // server fakes uniqueid == 0 for handshake synchronizeable |
---|
869 | if ( ( SharedNetworkData::getInstance()->isMasterServer() || |
---|
870 | SharedNetworkData::getInstance()->isProxyServerActive() && peer->second.isClient() ) && |
---|
871 | ( sync.getUniqueID() >= 1000 || sync.getUniqueID() <= SharedNetworkData::getInstance()->getMaxPlayer() + 1 + NET_ID_PROXY_MAX)) |
---|
872 | n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
873 | else |
---|
874 | n = Converter::intToByteArray( sync.getUniqueID(), buf + offset, UDP_PACKET_SIZE - offset ); |
---|
875 | |
---|
876 | |
---|
877 | assert( n == INTSIZE ); |
---|
878 | offset += n; |
---|
879 | |
---|
880 | // make space for packet size |
---|
881 | offset += INTSIZE; |
---|
882 | |
---|
883 | n = sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, -1000 ); |
---|
884 | offset += n; |
---|
885 | |
---|
886 | assert( Converter::intToByteArray( n, buf + offset - n - INTSIZE, INTSIZE ) == INTSIZE ); |
---|
887 | |
---|
888 | // check if all data bytes == 0 -> remove data and the synchronizeable from the sync process since there is no update |
---|
889 | // TODO not all synchronizeables like this maybe add Synchronizeable::canRemoveZeroDiff() |
---|
890 | bool allZero = true; |
---|
891 | for ( int i = 0; i < n; i++ ) |
---|
892 | { |
---|
893 | if ( buf[i+oldOffset+2*INTSIZE] != 0 ) |
---|
894 | allZero = false; |
---|
895 | } |
---|
896 | // if there is no new data in this synchronizeable reset the data offset to the last state -> dont synchronizes |
---|
897 | // data that hast not changed |
---|
898 | if ( allZero ) |
---|
899 | { |
---|
900 | offset = oldOffset; |
---|
901 | } |
---|
902 | } // all synchronizeables written |
---|
903 | |
---|
904 | |
---|
905 | |
---|
906 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
907 | { |
---|
908 | Synchronizeable & sync = **it; |
---|
909 | |
---|
910 | // again exclude all unwanted syncs |
---|
911 | if ( !sync.beSynchronized() || sync.getUniqueID() <= NET_UID_UNASSIGNED) |
---|
912 | continue; |
---|
913 | |
---|
914 | sync.handleSentState( peer->second.userId, currentState, peer->second.lastAckedState ); |
---|
915 | } |
---|
916 | |
---|
917 | |
---|
918 | assert( Converter::intToByteArray( offset, buf, INTSIZE ) == INTSIZE ); |
---|
919 | |
---|
920 | // now compress the data with the zip library |
---|
921 | int compLength = 0; |
---|
922 | if ( SharedNetworkData::getInstance()->isMasterServer() || |
---|
923 | SharedNetworkData::getInstance()->isProxyServerActive()) |
---|
924 | compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictServer ); |
---|
925 | else |
---|
926 | compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictClient ); |
---|
927 | |
---|
928 | if ( compLength <= 0 ) |
---|
929 | { |
---|
930 | PRINTF(1)("compression failed!\n"); |
---|
931 | continue; |
---|
932 | } |
---|
933 | |
---|
934 | assert( peer->second.socket->writePacket( compBuf, compLength ) ); |
---|
935 | |
---|
936 | if ( this->remainingBytesToWriteToDict > 0 ) |
---|
937 | writeToNewDict( buf, offset, true ); |
---|
938 | |
---|
939 | peer->second.connectionMonitor->processUnzippedOutgoingPacket( tick, buf, offset, currentState ); |
---|
940 | peer->second.connectionMonitor->processZippedOutgoingPacket( tick, compBuf, compLength, currentState ); |
---|
941 | |
---|
942 | } |
---|
943 | } |
---|
944 | |
---|
945 | /** |
---|
946 | * handle downstream network traffic |
---|
947 | */ |
---|
948 | void NetworkStream::handleDownstream( int tick ) |
---|
949 | { |
---|
950 | int offset = 0; |
---|
951 | |
---|
952 | int length = 0; |
---|
953 | int packetLength = 0; |
---|
954 | int compLength = 0; |
---|
955 | int uniqueId = 0; |
---|
956 | int state = 0; |
---|
957 | int ackedState = 0; |
---|
958 | int fromState = 0; |
---|
959 | int syncDataLength = 0; |
---|
960 | |
---|
961 | for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ ) |
---|
962 | { |
---|
963 | |
---|
964 | if ( !peer->second.socket ) |
---|
965 | continue; |
---|
966 | |
---|
967 | while ( 0 < (compLength = peer->second.socket->readPacket( compBuf, UDP_PACKET_SIZE )) ) |
---|
968 | { |
---|
969 | peer->second.connectionMonitor->processZippedIncomingPacket( tick, compBuf, compLength ); |
---|
970 | |
---|
971 | packetLength = Zip::getInstance()->unZip( compBuf, compLength, buf, UDP_PACKET_SIZE ); |
---|
972 | |
---|
973 | if ( packetLength < 4*INTSIZE ) |
---|
974 | { |
---|
975 | if ( packetLength != 0 ) |
---|
976 | PRINTF(1)("got too small packet: %d\n", packetLength); |
---|
977 | continue; |
---|
978 | } |
---|
979 | |
---|
980 | if ( this->remainingBytesToWriteToDict > 0 ) |
---|
981 | writeToNewDict( buf, packetLength, false ); |
---|
982 | |
---|
983 | assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE ); |
---|
984 | assert( Converter::byteArrayToInt( buf + INTSIZE, &state ) == INTSIZE ); |
---|
985 | assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE ); |
---|
986 | assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE ); |
---|
987 | offset = 4*INTSIZE; |
---|
988 | |
---|
989 | peer->second.connectionMonitor->processUnzippedIncomingPacket( tick, buf, packetLength, state, ackedState ); |
---|
990 | |
---|
991 | |
---|
992 | //if this is an old state drop it |
---|
993 | if ( state <= peer->second.lastRecvedState ) |
---|
994 | continue; |
---|
995 | |
---|
996 | if ( packetLength != length ) |
---|
997 | { |
---|
998 | PRINTF(1)("real packet length (%d) and transmitted packet length (%d) do not match!\n", packetLength, length); |
---|
999 | peer->second.socket->disconnectServer(); |
---|
1000 | continue; |
---|
1001 | } |
---|
1002 | |
---|
1003 | while ( offset + 2 * INTSIZE < length ) |
---|
1004 | { |
---|
1005 | // read the unique id of the sync |
---|
1006 | assert( offset > 0 ); |
---|
1007 | assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE ); |
---|
1008 | offset += INTSIZE; |
---|
1009 | |
---|
1010 | // read the data length |
---|
1011 | assert( Converter::byteArrayToInt( buf + offset, &syncDataLength ) == INTSIZE ); |
---|
1012 | offset += INTSIZE; |
---|
1013 | |
---|
1014 | assert( syncDataLength > 0 ); |
---|
1015 | assert( syncDataLength < 10000 ); |
---|
1016 | |
---|
1017 | Synchronizeable * sync = NULL; |
---|
1018 | |
---|
1019 | // look for the synchronizeable in question |
---|
1020 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
1021 | { |
---|
1022 | // client thinks his handshake has a special id: hostId * 1000 (host id of this server) |
---|
1023 | if ( (*it)->getUniqueID() == uniqueId || // so this client exists already go to sync work |
---|
1024 | ( uniqueId == 0 && (*it)->getUniqueID() == peer->second.userId ) ) // so this is a Handshake! |
---|
1025 | { |
---|
1026 | sync = *it; |
---|
1027 | break; |
---|
1028 | } |
---|
1029 | } |
---|
1030 | |
---|
1031 | // this synchronizeable does not yet exist! create it |
---|
1032 | if ( sync == NULL ) |
---|
1033 | { |
---|
1034 | PRINTF(0)("could not find sync with id %d. try to create it\n", uniqueId); |
---|
1035 | |
---|
1036 | // if it is an old synchronizeable already removed, ignore it |
---|
1037 | if ( oldSynchronizeables.find( uniqueId ) != oldSynchronizeables.end() ) |
---|
1038 | { |
---|
1039 | offset += syncDataLength; |
---|
1040 | continue; |
---|
1041 | } |
---|
1042 | |
---|
1043 | // if the node we got this unknown sync we ignore it if: |
---|
1044 | // - the remote host is a client |
---|
1045 | // - the remote host is a proxy server and we are master server |
---|
1046 | // (since it has no rights to create a new sync) |
---|
1047 | if ( peers[peer->second.userId].isClient() || |
---|
1048 | (peers[peer->second.userId].isProxyServerActive() && SharedNetworkData::getInstance()->isMasterServer())) |
---|
1049 | { |
---|
1050 | offset += syncDataLength; |
---|
1051 | continue; |
---|
1052 | } |
---|
1053 | |
---|
1054 | int leafClassId; |
---|
1055 | if ( INTSIZE > length - offset ) |
---|
1056 | { |
---|
1057 | offset += syncDataLength; |
---|
1058 | continue; |
---|
1059 | } |
---|
1060 | |
---|
1061 | Converter::byteArrayToInt( buf + offset, &leafClassId ); |
---|
1062 | |
---|
1063 | assert( leafClassId != 0 ); |
---|
1064 | |
---|
1065 | |
---|
1066 | BaseObject * b = NULL; |
---|
1067 | /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ |
---|
1068 | /* Exception 1: NullParent */ |
---|
1069 | if( leafClassId == CL_NULL_PARENT || leafClassId == CL_SYNCHRONIZEABLE || leafClassId == CL_NETWORK_GAME_MANAGER ) |
---|
1070 | { |
---|
1071 | PRINTF(1)("Don't create Object with ID %x, ignored!\n", (int)leafClassId); |
---|
1072 | offset += syncDataLength; |
---|
1073 | continue; |
---|
1074 | } |
---|
1075 | else |
---|
1076 | b = Factory::fabricate( (ClassID)leafClassId ); |
---|
1077 | |
---|
1078 | if ( !b ) |
---|
1079 | { |
---|
1080 | PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId); |
---|
1081 | offset += syncDataLength; |
---|
1082 | continue; |
---|
1083 | } |
---|
1084 | |
---|
1085 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
1086 | { |
---|
1087 | sync = dynamic_cast<Synchronizeable*>(b); |
---|
1088 | sync->setUniqueID( uniqueId ); |
---|
1089 | sync->setSynchronized(true); |
---|
1090 | |
---|
1091 | PRINTF(0)("Fabricated %s with id %d\n", sync->getClassCName(), sync->getUniqueID()); |
---|
1092 | } |
---|
1093 | else |
---|
1094 | { |
---|
1095 | PRINTF(1)("Class with ID %x is not a synchronizeable!\n", (int)leafClassId); |
---|
1096 | delete b; |
---|
1097 | offset += syncDataLength; |
---|
1098 | continue; |
---|
1099 | } |
---|
1100 | } |
---|
1101 | |
---|
1102 | |
---|
1103 | int n = sync->setStateDiff( peer->second.userId, buf+offset, syncDataLength, state, fromState ); |
---|
1104 | offset += n; |
---|
1105 | |
---|
1106 | } |
---|
1107 | |
---|
1108 | if ( offset != length ) |
---|
1109 | { |
---|
1110 | PRINTF(0)("offset (%d) != length (%d)\n", offset, length); |
---|
1111 | peer->second.socket->disconnectServer(); |
---|
1112 | } |
---|
1113 | |
---|
1114 | |
---|
1115 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
1116 | { |
---|
1117 | Synchronizeable & sync = **it; |
---|
1118 | |
---|
1119 | if ( !sync.beSynchronized() || sync.getUniqueID() <= NET_UID_UNASSIGNED ) |
---|
1120 | continue; |
---|
1121 | |
---|
1122 | sync.handleRecvState( peer->second.userId, state, fromState ); |
---|
1123 | } |
---|
1124 | |
---|
1125 | assert( peer->second.lastAckedState <= ackedState ); |
---|
1126 | peer->second.lastAckedState = ackedState; |
---|
1127 | |
---|
1128 | assert( peer->second.lastRecvedState < state ); |
---|
1129 | peer->second.lastRecvedState = state; |
---|
1130 | |
---|
1131 | } |
---|
1132 | |
---|
1133 | } |
---|
1134 | |
---|
1135 | } |
---|
1136 | |
---|
1137 | /** |
---|
1138 | * is executed when a handshake has finished |
---|
1139 | */ |
---|
1140 | void NetworkStream::handleNewClient( int userId ) |
---|
1141 | { |
---|
1142 | // init and assign the message manager |
---|
1143 | MessageManager::getInstance()->initUser( userId ); |
---|
1144 | // do all game relevant stuff here |
---|
1145 | networkGameManager->signalNewPlayer( userId ); |
---|
1146 | } |
---|
1147 | |
---|
1148 | |
---|
1149 | /** |
---|
1150 | * removes old items from oldSynchronizeables |
---|
1151 | */ |
---|
1152 | void NetworkStream::cleanUpOldSyncList( ) |
---|
1153 | { |
---|
1154 | int now = SDL_GetTicks(); |
---|
1155 | |
---|
1156 | for ( std::map<int,int>::iterator it = oldSynchronizeables.begin(); it != oldSynchronizeables.end(); ) |
---|
1157 | { |
---|
1158 | if ( it->second < now - 10*1000 ) |
---|
1159 | { |
---|
1160 | std::map<int,int>::iterator delIt = it; |
---|
1161 | it++; |
---|
1162 | oldSynchronizeables.erase( delIt ); |
---|
1163 | continue; |
---|
1164 | } |
---|
1165 | it++; |
---|
1166 | } |
---|
1167 | } |
---|
1168 | |
---|
1169 | /** |
---|
1170 | * writes data to DATA/dicts/newdict |
---|
1171 | * @param data pointer to data |
---|
1172 | * @param length length |
---|
1173 | */ |
---|
1174 | void NetworkStream::writeToNewDict( byte * data, int length, bool upstream ) |
---|
1175 | { |
---|
1176 | if ( remainingBytesToWriteToDict <= 0 ) |
---|
1177 | return; |
---|
1178 | |
---|
1179 | if ( length > remainingBytesToWriteToDict ) |
---|
1180 | length = remainingBytesToWriteToDict; |
---|
1181 | |
---|
1182 | std::string fileName = ResourceManager::getInstance()->getDataDir(); |
---|
1183 | fileName += "/dicts/newdict"; |
---|
1184 | |
---|
1185 | if ( upstream ) |
---|
1186 | fileName += "_upstream"; |
---|
1187 | else |
---|
1188 | fileName += "_downstream"; |
---|
1189 | |
---|
1190 | FILE * f = fopen( fileName.c_str(), "a" ); |
---|
1191 | |
---|
1192 | if ( !f ) |
---|
1193 | { |
---|
1194 | PRINTF(2)("could not open %s\n", fileName.c_str()); |
---|
1195 | remainingBytesToWriteToDict = 0; |
---|
1196 | return; |
---|
1197 | } |
---|
1198 | |
---|
1199 | if ( fwrite( data, 1, length, f ) != length ) |
---|
1200 | { |
---|
1201 | PRINTF(2)("could not write to file\n"); |
---|
1202 | fclose( f ); |
---|
1203 | return; |
---|
1204 | } |
---|
1205 | |
---|
1206 | fclose( f ); |
---|
1207 | |
---|
1208 | remainingBytesToWriteToDict -= length; |
---|
1209 | } |
---|
1210 | |
---|
1211 | |
---|
1212 | |
---|
1213 | |
---|
1214 | |
---|
1215 | |
---|