Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/network_game_manager.cc @ 6784

Last change on this file since 6784 was 6737, checked in by patrick, 19 years ago

trunk: merged network back to trunk

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