Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/network_stream.cc @ 7952

Last change on this file since 7952 was 7952, checked in by rennerc, 18 years ago

less output on telnet console

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