Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

new network system implemented. yet with a lot of empty function bodys

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