Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/network_game_manager.cc @ 7767

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

hover registers sync vars. started with connection monitor

File size: 23.8 KB
RevLine 
[6067]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: Benjamin Wuest
13   co-programmer: ...
14*/
15
16
17/* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module
18   For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput
19*/
20#define DEBUG_MODULE_NETWORK
21
[7193]22#include "util/loading/factory.h"
[7349]23#include "state.h"
24#include "class_list.h"
25
[6341]26#include "network_stream.h"
[7349]27#include "shared_network_data.h"
[6341]28#include "converter.h"
[7693]29#include "message_manager.h"
[6341]30
[6498]31#include "playable.h"
32#include "player.h"
[6424]33
[7349]34#include "game_world.h"
[6424]35
[6116]36#include "network_game_manager.h"
[6067]37
38
39/* using namespace std is default, this needs to be here */
40using namespace std;
41
[6341]42NetworkGameManager* NetworkGameManager::singletonRef = NULL;
43
[6067]44/*!
45 * Standard constructor
46 */
[6116]47NetworkGameManager::NetworkGameManager()
[6695]48  : Synchronizeable()
[6067]49{
[6341]50  PRINTF(0)("START\n");
51
[6067]52  /* set the class id for the base object */
[6341]53  this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager");
54
[6695]55  this->setSynchronized(true);
[7697]56 
57  MessageManager::getInstance()->registerMessageHandler( MSGID_YOU_ARE, youAreHandler, NULL );
[6067]58}
59
60/*!
61 * Standard destructor
62 */
[6116]63NetworkGameManager::~NetworkGameManager()
[6067]64{
[7567]65#if 0
[6341]66  for ( int i = 0; i<outBuffer.size(); i++)
67  {
68    if ( outBuffer[i].buffer )
69      delete outBuffer[i].buffer;
70  }
[7567]71#endif
[6341]72
[6067]73}
74
[7567]75#if 0
[6341]76int NetworkGameManager::writeBytes(const byte* data, int length, int sender)
[6067]77{
[6341]78  int i = 0;
79  byte b;
80
81  while ( i<length )
82  {
83    b = data[i++];
84
[6695]85    /**************** Commands only processed by servers ****************/
[6341]86    if ( isServer() )
87    {
[6695]88      if ( b == NET_REQUEST_CREATE )
[6341]89      {
90        if ( !handleRequestCreate( i, data, length, sender ) )
91          return i;
92        continue;
93      }
[6695]94      else if ( b == NET_REQUEST_REMOVE )
[6341]95      {
96        if ( !handleRequestRemove( i, data, length, sender ) )
97          return i;
98        continue;
99      }
[6695]100      else if ( b == NET_REQUEST_PNODE_PATH )
101      {
102        if ( !handleRequestPNodePath( i, data, length, sender ) )
103          return i;
104        continue;
105      }
[6341]106    }
107    else
108    {
[6695]109      /**************** Commands only processed by clients ****************/
110      if ( b == NET_CREATE_ENTITY )
[6341]111      {
[6634]112        PRINTF(0)("CREATE_ENTITY\n");
[6341]113        if ( !handleCreateEntity( i, data, length, sender ) )
114          return i;
115        continue;
116      }
[6695]117      else if ( b == NET_REMOVE_ENTITY )
[6341]118      {
119        if ( !handleRemoveEntity( i, data, length, sender ) )
120          return i;
121        continue;
122      }
[6695]123      else if ( b == NET_CREATE_ENTITY_LIST )
[6341]124      {
125        if ( !handleCreateEntityList( i, data, length, sender ) )
126          return i;
127        continue;
128      }
[6695]129      else if ( b == NET_REMOVE_ENTITY_LIST )
[6341]130      {
131        if ( !handleRemoveEntityList( i, data, length, sender ) )
132          return i;
133        continue;
134      }
[6695]135      else if ( b == NET_YOU_ARE_ENTITY )
[6341]136      {
137        if ( !handleYouAreEntity( i, data, length, sender ) )
138          return i;
139        continue;
140      }
141    }
142
[6695]143    /**************** Commands processed by servers and clients ****************/
144    if ( b == NET_REQUEST_ENTITY_LIST )
[6341]145    {
[6634]146      sendEntityList( sender );
[6341]147      continue;
148    }
149
[6695]150
151    PRINTF(1)("Network is asynchronous: couldn't decode the command sent by %i\n", sender);
152    PRINTF(1)("Probably this is because the network protocol has different \n");
153    PRINTF(1)("versions or there occured an error in the sending algorithm\n");
[6341]154    PRINTF(1)("Data is not in the right format! i=%d\n", i);
155    return i;
156  }
157
158  return i;
[6067]159}
[7567]160#endif
[6067]161
[7567]162#if 0
[6116]163int NetworkGameManager::readBytes(byte* data, int maxLength, int * reciever)
[6067]164{
[6341]165  if ( !isServer() && !hasRequestedWorld )
166  {
[6815]167    assert( maxLength >= 1 );
168    data[0] = NET_REQUEST_ENTITY_LIST;
[6341]169    hasRequestedWorld = true;
[6815]170    return 1;
[6341]171  }
[6498]172
[6341]173  for ( int i = 0; i<outBuffer.size(); i++ )
174  {
175    *reciever = i;
176    if ( outBuffer[i].length>0 )
177    {
178      int nbytes = outBuffer[i].length;
179      outBuffer[i].length = 0;
180
181      if ( nbytes > maxLength )
182      {
183        PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.maxLength\n", nbytes, maxLength);
184        return 0;
185      }
186
187      memcpy(data, outBuffer[i].buffer, nbytes);
188      return nbytes;
189    }
190  }
191
[6634]192  return 0;
[6067]193}
[7567]194#endif
[6067]195
[7567]196#if 0
[6116]197void NetworkGameManager::writeDebug() const
[6067]198{
199}
200
[6116]201void NetworkGameManager::readDebug() const
[6067]202{
203}
[7567]204#endif
[6067]205
206
207/*!
208 * Checks whether this is connected to a server or a client
[6341]209 * and afterwards creates the needed entity
[6067]210 * @param classID: The ID of the class of which an entity should be created
211 */
[6498]212int NetworkGameManager::createEntity( ClassID classID, int owner )
[6067]213{
[6695]214  if ( this->isServer())
[6341]215  {
[7693]216    int res = this->executeCreateEntity( classID, SharedNetworkData::getInstance()->getNewUniqueID(), owner );
217   
218    if ( res < 0 )
[6341]219    {
220      PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");
[6498]221      return -1;
[6341]222    }
[7693]223   
224    return res;
[6341]225  }
226  else
227  {
[7567]228#if 0
[6341]229    this->requestCreateEntity( classID );
[7567]230#endif
[6498]231    return -1;
[6341]232  }
[6067]233}
234
[6341]235
[6067]236/*!
237 * Checks whether this is connected to a server or a client
[6341]238 * and afterwards creates the needed entity
239 * @param classID: The ID of the class of which an entity should be created
240 */
[6424]241BaseObject* NetworkGameManager::createEntity(const TiXmlElement* element)
[6341]242{
243  if ( this->isServer() )
244  {
[6695]245    if ( SharedNetworkData::getInstance()->getNewUniqueID() < 0 )
[6341]246    {
247      PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");
248      return NULL;
249    }
250
251    BaseObject * b = Factory::fabricate( element );
252
253    if ( !b )
254    {
[6424]255      PRINTF(1)("Could not fabricate Object with className %s\n", element->Value() );
[6341]256      return NULL;
257    }
258
259
260    if ( b->isA(CL_SYNCHRONIZEABLE) )
261    {
262      Synchronizeable * s = dynamic_cast<Synchronizeable*>(b);
[6695]263      s->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() );
[6341]264      s->setOwner( 0 );
[6695]265      // all entities created via this function are added automaticaly to the synchronizeable list
266      s->setSynchronized(true);
[6341]267      return b;
268    }
269    else
270    {
271      PRINTF(1)("Class %s is not a synchronizeable!\n", b->getClassName() );
272      delete b;
273    }
[6498]274
[6341]275  }
276  else
[6695]277
[6341]278  {
279    PRINTF(1)("This node is not a server and cannot create id %x\n", element->Value());
280  }
281  return NULL;
282}
283
284
285/*!
286 * Checks whether this is connected to a server or a client
[6067]287 * and afterwards removes the specified entity
288 * @param uniqueID: The ID of the entity object which should be removed
289 */
[6116]290void NetworkGameManager::removeEntity(int uniqueID)
[6067]291{
[6341]292  if ( this->isServer() )
293  {
294    this->executeRemoveEntity( uniqueID );
295  }
296  else
297  {
[7567]298#if 0
[6341]299    this->requestRemoveEntity( uniqueID );
[7567]300#endif
[6341]301  }
[6067]302}
303
304
[7567]305#if 0
[6067]306/*!
307 * Creates the needed entity on the server if possible
308 * @param classID: The ID of the class of which an entity should be created
309 */
[6341]310void NetworkGameManager::requestCreateEntity(ClassID classID)
[6067]311{
[6634]312  for ( int i = 0; i<outBuffer.size(); i++)
313  {
314    if ( !this->networkStream->isUserIdActive( i ) )
315      continue;
316
[6695]317    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_CREATE ) )
[6634]318      return;
319    if ( !writeToClientBuffer( outBuffer[i], (int)classID ) )
320      return;
321  }
[6067]322}
[7567]323#endif
[6067]324
[7567]325#if 0
[6067]326/*!
327 * Removes the specified entity on the server
328 * @param uniqueID: The ID of the entity object which should be removed
329 */
[6116]330void NetworkGameManager::requestRemoveEntity(int uniqueID)
[6067]331{
[6634]332  for ( int i = 0; i<outBuffer.size(); i++)
333  {
334    if ( !this->networkStream->isUserIdActive( i ) )
335      continue;
336
[6695]337    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_REMOVE ) )
[6634]338      return;
339    if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
340      return;
341  }
[6067]342}
[7567]343#endif
[6067]344
345/*!
346 * Creates the needed entity if possible
347 * This function is called if this is a server
348 * @param classID: The ID of the class of which an entity should be created
349 */
[6498]350int NetworkGameManager::executeCreateEntity(ClassID classID, int uniqueID, int owner)
[6067]351{
[7567]352#if 0
[6634]353  for ( int i = 0; i<outBuffer.size(); i++)
354  {
355    if ( !this->networkStream->isUserIdActive( i ) )
356      continue;
[6341]357
[6695]358    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_CREATE_ENTITY ) )
[6634]359      return -1;
360    if ( !writeToClientBuffer( outBuffer[i], (int)classID ) )
361      return -1;
362    if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
363      return -1;
364    if ( !writeToClientBuffer( outBuffer[i], owner ) )
365      return -1;
366  }
[7567]367#endif
[6695]368  PRINTF(0)("ExecuteCreateEntity: server side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
[6341]369  doCreateEntity( classID, uniqueID, owner );
[6498]370
371  return uniqueID;
[6067]372}
373
374/*!
375 * Removes the specified entity
376 * This function is called if this is a server
377 * @param uniqueID: The ID of the entity object which should be removed
378 */
[6116]379void NetworkGameManager::executeRemoveEntity(int uniqueID)
[6067]380{
[7567]381#if 0
[6634]382  for ( int i = 0; i<outBuffer.size(); i++)
383  {
384    if ( !this->networkStream->isUserIdActive( i ) )
385      continue;
[6341]386
[6695]387    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REMOVE_ENTITY ) )
[6634]388      return;
389    if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
390      return;
391  }
[7567]392#endif
[6634]393
[6341]394  doRemoveEntity(uniqueID);
[6067]395}
396
397/*!
398 * Checks whether it is possible to create an entity of a given class
399 * @return: true if the entity can be created, false otherwise
400 */
[6341]401bool NetworkGameManager::canCreateEntity(ClassID classID)
[6067]402{
[6341]403  return true;
[6067]404}
[6341]405
[7567]406#if 0
[6341]407/*!
408 * Sends the Entities to the new connected client
409 * @param userID: The ID of the user
410 */
411void NetworkGameManager::sendEntityList( int userID )
412{
413  if ( !isServer() )
414    return;
415
416  if ( userID >= outBuffer.size() )
417    resizeBufferVector( userID );
418
419  SynchronizeableList::const_iterator it, e;
420
421  it = this->networkStream->getSyncBegin();
422  e = this->networkStream->getSyncEnd();
423
[6695]424  // send the packet header
425  if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_CREATE_ENTITY_LIST ) )
[6341]426    return;
427
[6695]428  // send the number of entities: -2 because you must not send network_game_manager and handshake
[6341]429  if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount() ) )
430    return;
431
432  //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 );
433
[6695]434  // first send the NullParent
435  if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getLeafClassID()) )
436    return;
437  if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getUniqueID()) )
438    return;
439  if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getOwner()) )
440    return;
[6424]441
[6695]442  // now send the rest of the entities
[6341]443  while ( it != e )
444  {
[6695]445    if( (*it)->beSynchronized() && (*it) != PNode::getNullParent())
446    {
447      PRINTF(0)("SENDING ENTITY %s classid: %x, uniqueid %d\n", (*it)->getClassName(), (*it)->getLeafClassID(), (*it)->getUniqueID() );
448      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) )
449        return;
[6341]450
[6695]451      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getUniqueID()) ) )
452        return;
[6341]453
[6695]454      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getOwner()) ) )
455        return;
456    }
[6498]457    it++;
458  }
[6424]459
[6498]460  signalNewPlayer( userID );
461}
[7567]462#endif
[6424]463
464
465
[6498]466bool NetworkGameManager::signalNewPlayer(int userId)
467{
468  /* create new playable for Player*/
469  PRINTF(0)("Request for creation: %i\n", userId);
470  int uniqueId = this->createEntity(CL_SPACE_SHIP, userId);
471  PRINTF(0)("Request for creation: userid: %i, uniqueid: %i\n", userId, uniqueId);
472  this->sendYouAre(uniqueId, userId);
[6341]473}
474
[6498]475
[6737]476
477bool NetworkGameManager::signalLeftPlayer(int userID)
478{
479  const std::list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE);
[7767]480 
481  if ( !playableList )
482    return false;
483 
[6737]484  std::list<BaseObject*>::const_iterator it = playableList->begin();
485
486  for(; it != playableList->end(); it++)
487  {
488    if( dynamic_cast<Synchronizeable*>(*it)->getOwner() == userID )
489    {
490      PRINTF(0)("remove playable from %i\n", userID);
491      this->removeEntity(dynamic_cast<Synchronizeable*>(*it)->getUniqueID());
492      return true;
493    }
494  }
495  return false;
496}
497
[7567]498#if 0
[6341]499/**
500 * Creates a buffer for user n
501 * @param n The ID of the user
502 */
503void NetworkGameManager::resizeBufferVector( int n )
504{
505  for ( int i = outBuffer.size(); i<=n; i++)
506  {
507    clientBuffer outBuf;
508
509    outBuf.length = 0;
510
511    outBuf.maxLength = 5*1024;
512
513    outBuf.buffer = new byte[5*1014];
514
515    outBuffer.push_back(outBuf);
516  }
517}
[7567]518#endif
[6341]519
520/**
521 * Creates the entity on this host
522 * @param classID: ClassID of the entity to create
523 * @param uniqueID: Unique ID to assign to the synchronizeable
524 * @param owner: owner of this synchronizealbe
525 */
[6424]526BaseObject* NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner )
[6341]527{
[6695]528  PRINTF(0)("Creating Entity via Factory: classid: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
[6341]529
[6695]530  BaseObject * b;
531  /* These are some small exeptions in creation: Not all objects can/should be created via Factory */
532  /* Exception 1: NullParent */
533  if( classID == CL_NULL_PARENT)
534  {
535    b = (BaseObject*)PNode::getNullParent();
536  }
537  else
538    b = Factory::fabricate( classID );
539
[6341]540  if ( !b )
541  {
542    PRINTF(1)("Could not fabricate Object with classID %x\n", classID);
[6424]543    return NULL;
[6341]544  }
545
546  if ( b->isA(CL_SYNCHRONIZEABLE) )
547  {
548    Synchronizeable * s = dynamic_cast<Synchronizeable*>(b);
549    s->setUniqueID( uniqueID );
550    s->setOwner( owner );
[6695]551    s->setSynchronized(true);
552    //this->networkStream->connectSynchronizeable( *s );
[7444]553
[6341]554    PRINTF(0)("Fabricated %s with id %d\n", s->getClassName(), s->getUniqueID());
[6498]555
[6994]556    //TODO HACK: hack to prevent collision
[6634]557    if ( b->isA(CL_WORLD_ENTITY) && !b->isA(CL_PLAYABLE) )
[6498]558    {
[6959]559      if ( SharedNetworkData::getInstance()->getHostID()!=0 )
[6498]560      {
561        static Vector pos = Vector(1000.0, 1000.0, 1000.0);
562        PNode *p = dynamic_cast<PNode*>(b);
[6634]563        p->setAbsCoor(pos);
[6737]564        p->updateNode(0);
[6498]565        pos += Vector(1000.0, 1000.0, 1000.0);
566      }
567    }
[6994]568    ///TODO HACK this is only for network multiplayer games.
[7354]569    if( b->isA(CL_PLAYABLE))
[6959]570    {
[7349]571      Playable* ss = dynamic_cast<Playable*>(b);
[6959]572      if( owner%2 == 0)
573      {
[6498]574
[7071]575        ss->loadModel("models/ships/reap_0.obj");
[6959]576        ss->toList(OM_GROUP_00);
[7078]577        ss->setAbsCoor(213.37, 57.71, -47.98);
578        ss->setAbsDir(Quaternion(0.16, 0.98, -0.10));
[6959]579      }
580      else
581      {
582        ss->loadModel( "models/ships/fighter.obj" );
583        ss->toList(OM_GROUP_01);
[7078]584        ss->setAbsCoor(-314.450, 40.701, 83.554);
585      }
[6959]586    }
587
[6424]588    return b;
[6341]589  }
590  else
591  {
592    PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)classID);
593    delete b;
594  }
[6424]595  return NULL;
[6341]596}
597
598/**
599 * Removes a entity on this host
600 * @param uniqueID: unique ID assigned with the entity to remove
601 */
602void NetworkGameManager::doRemoveEntity( int uniqueID )
603{
604  SynchronizeableList::const_iterator it,e;
605  it = this->networkStream->getSyncBegin();
606  e = this->networkStream->getSyncEnd();
607
608  while ( it != e )
609  {
610    if ( (*it)->getUniqueID() == uniqueID )
611    {
[6868]612      assert((*it)->isA(CL_WORLD_ENTITY));
[6959]613      dynamic_cast<WorldEntity*>(*it)->leaveWorld();
[6868]614      dynamic_cast<WorldEntity*>(*it)->toList(OM_DEAD);
[6341]615      break;
616    }
617    it++;
618  }
619}
620
[7567]621#if 0
[6341]622/**
623 * Copies length bytes to the clientBuffer with error checking
624 * @param clientBuffer: the clientBuffer to write to
625 * @param data: buffer to the data
626 * @param length: length of data
627 * @return false on error true else
628 */
629bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length )
630{
631  if ( length > cb.maxLength-cb.length )
632  {
633    PRINTF(1)("No space left in clientBuffer\n");
634    return false;
635  }
636
637  memcpy( cb.buffer+cb.length, data, length );
638  return true;
639}
[7567]640#endif
[6341]641
[7567]642#if 0
[6341]643/**
644 * Reads data from clientBuffer with error checking
645 * @param clientBuffer: the clientBuffer to read from
646 * @param data: pointer to the buffer
647 * @param length:
648 * @return
649 */
650bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length )
651{
652  if ( cb.length < length )
653  {
654    PRINTF(0)("There is not enough data in clientBuffer\n");
655    return 0;
656  }
657
658  memcpy( data, cb.buffer+cb.length-length, length );
659  return true;
660}
[7567]661#endif
[6341]662
663/**
664 * Tells this client that he has to control this entity
665 * @param uniqueID: the entity's uniqeID
666 */
667void NetworkGameManager::doYouAre( int uniqueID )
668{
[6498]669
670  SynchronizeableList::const_iterator it = this->networkStream->getSyncBegin();
671
672  Playable *p = NULL;
[6634]673  Synchronizeable *s = NULL;
[6498]674
675  for ( ; it !=networkStream->getSyncEnd(); it++ )
676  {
677    if ( (*it)->getUniqueID()==uniqueID )
678    {
[6634]679      if ( (*it)->isA( CL_SYNCHRONIZEABLE ) )
680      {
681        s = dynamic_cast<Synchronizeable*>(*it);
682      }
[6498]683      if ( (*it)->isA( CL_PLAYABLE ) )
684      {
685        p = dynamic_cast<Playable*>(*it);
686        break;
687      } else
688      {
[7697]689        PRINTF(1)("UniqueID %d is not a Playable\n", uniqueID);
[6498]690      }
691    }
692  }
693
694  Player* player = State::getPlayer();
695  assert(p != NULL);
[6634]696  assert(s != NULL);
[6498]697  assert(player != NULL);
698
[6634]699  PRINTF(0)("uniqueID = %d\n", s->getUniqueID());
700
[6985]701  player->setPlayable(p);
[6498]702
703
[6341]704}
705
706/**
707 * Tells a remote client that he has to control this entity
708 * @param uniqueID: the entity's uniqeID
709 * @param userID: the users ID
710 */
711void NetworkGameManager::sendYouAre( int uniqueID, int userID )
712{
713  if ( !isServer() )
714    return;
[7693]715 
716  byte buf[INTSIZE];
717 
718  Converter::intToByteArray( uniqueID, buf, INTSIZE );
719 
720  MessageManager::getInstance()->sendMessage( MSGID_YOU_ARE, buf, INTSIZE, RT_USER, userID, MP_HIGHBANDWIDTH);
[6341]721}
722
723bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender )
724{
725  if ( INTSIZE > length-i )
726  {
727    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
728    return false;
729  }
730  int classID;
731  i += Converter::byteArrayToInt( &data[i], &classID );
732
733  createEntity( (ClassID)classID );
734
735  return true;
736}
737
738bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender )
739{
740  if ( INTSIZE > length-i )
741  {
742    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
743    return false;
744  }
745  int uniqueID;
746  i += Converter::byteArrayToInt( &data[i], &uniqueID );
747
748  removeEntity( uniqueID );
749
750  return true;
751}
752
753bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender )
754{
755  if ( INTSIZE > length-i )
756  {
757    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
758    return false;
759  }
760  int classID;
761  i += Converter::byteArrayToInt( &data[i], &classID );
762
763  if ( INTSIZE > length-i )
764  {
765    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
766    return false;
767  }
768  int uniqueID;
769  i += Converter::byteArrayToInt( &data[i], &uniqueID );
770
771  if ( INTSIZE > length-i )
772  {
773    PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
774    return false;
775  }
776  int owner;
777  i += Converter::byteArrayToInt( &data[i], &owner );
778
[6695]779  PRINTF(0)("handleCreateEntity: client side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
[6341]780  doCreateEntity( (ClassID)classID, uniqueID, owner );
781
782  return true;
783}
784
785bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender )
786{
787  if ( INTSIZE > length-i )
788  {
789    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
790    return false;
791  }
792  int uniqueID;
793  i += Converter::byteArrayToInt( &data[i], &uniqueID );
794
795  doRemoveEntity( uniqueID );
796
797  return true;
798}
799
800bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender )
801{
802  if ( INTSIZE > length-i )
803  {
804    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
805    return false;
806  }
807
808  PRINTF(0)("HandleCreateEntityList:  data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);
809
810  int n;
811  i += Converter::byteArrayToInt( &data[i], &n );
812
813
814  PRINTF(0)("HandleCreateEntityList: n = %d\n", n);
815
816  int classID, uniqueID, owner;
817
818  for ( int j = 0; j<n; j++ )
819  {
820
821    if ( INTSIZE > length-i )
822    {
823      PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
824      return false;
825    }
826    i += Converter::byteArrayToInt( &data[i], &classID );
827
828    if ( INTSIZE > length-i )
829    {
830      PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
831      return false;
832    }
833    i += Converter::byteArrayToInt( &data[i], &uniqueID );
834
835    if ( INTSIZE > length-i )
836    {
837      PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
838      return false;
839    }
840    i += Converter::byteArrayToInt( &data[i], &owner );
841
842    if ( classID != CL_NETWORK_GAME_MANAGER && classID != CL_HANDSHAKE )
[6424]843    {
844      BaseObject* b = doCreateEntity( (ClassID)classID, uniqueID, owner );
845    }
846
[6341]847  }
[6424]848
[6341]849  return true;
850}
851
852bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender )
853{
854  if ( INTSIZE > length-i )
855  {
856    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
857    return false;
858  }
859  int n;
860  i += Converter::byteArrayToInt( &data[i], &n );
861
862  int uniqueID;
863
864  for ( int j = 0; j<n; j++ )
865  {
866
867    if ( INTSIZE > length-i )
868    {
869      PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
870      return false;
871    }
872    i += Converter::byteArrayToInt( &data[i], &uniqueID );
873
874    doRemoveEntity( uniqueID );
875  }
876
877  return true;
878}
879
880bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender )
881{
882  if ( INTSIZE > length-i )
883  {
884    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
885    return false;
886  }
887
888  int uniqueID;
889  i += Converter::byteArrayToInt( &data[i], &uniqueID );
890
891  doYouAre( uniqueID );
892
893  return true;
894}
895
[6695]896/**
897 *  handles the network signal NET_REQUEST_PNODE_PATH
898 * @param i byte offset in the buffer
899 * @param data data array
900 * @param length length of the data arary
901 * @param sender the sender id
902 * @return true if process terminated sucessfully
903 */
904bool NetworkGameManager::handleRequestPNodePath(int& i, const byte* data, int length, int sender)
905{
906  if( INTSIZE > length-i )
907  {
908    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
909    return false;
910  }
911  PRINTF(0)("HandleRequestPNodePath:  data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);
912
913  int uniqueID1, uniqueID2;
914  if( INTSIZE > length-i )
915  {
916    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
917    return false;
918  }
919  i += Converter::byteArrayToInt( &data[i], &uniqueID1 );
920
921  if( INTSIZE > length-i )
922  {
923    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
924    return false;
925  }
926  i += Converter::byteArrayToInt( &data[i], &uniqueID2 );
927
928
929  PRINTF(0)("HandleRequestPNodePath: got a request for path from uid %i to uid %i\n", uniqueID1, uniqueID2);
930
931  return true;
932}
933
[7693]934bool NetworkGameManager::youAreHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
935{
936  assert( dataLength == INTSIZE );
937  int uniqueId;
938 
939  Converter::byteArrayToInt( data, &uniqueId );
940 
[7694]941  SynchronizeableList::const_iterator it = NetworkGameManager::getInstance()->networkStream->getSyncBegin();
942
943  Playable *p = NULL;
944  Synchronizeable *s = NULL;
945
946  for ( ; it !=NetworkGameManager::getInstance()->networkStream->getSyncEnd(); it++ )
947  {
948    if ( (*it)->getUniqueID()==uniqueId )
949    {
[7697]950      break;
[7694]951    }
952  }
953 
[7697]954  if ( it == NetworkGameManager::getInstance()->networkStream->getSyncEnd() )
[7694]955    return false;
956 
[7693]957  NetworkGameManager::getInstance()->doYouAre( uniqueId );
[7694]958 
959  return true;
[7693]960}
961
[7567]962#if 0
[6341]963bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b )
964{
965  if ( cb.maxLength-cb.length < 1 )
966  {
967    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 byte\n");
968    return false;
969  }
970
971  cb.buffer[cb.length++] = b;
972
973  return true;
974}
975
[7567]976
[6341]977bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, int i )
978{
979  int n = Converter::intToByteArray( i, cb.buffer+cb.length, cb.maxLength-cb.length );
980  cb.length += n;
981
982  if ( n <= 0 )
983  {
984    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 int\n");
985    return false;
986  }
987
988  return true;
989}
[7567]990#endif
[6341]991
992
Note: See TracBrowser for help on using the repository browser.