Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6672 was 6672, checked in by patrick, 18 years ago

network: now every entity gets a network uniqueID

File size: 22.5 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
35/* include your own header */
36#include "network_game_manager.h"
37
38
39/* using namespace std is default, this needs to be here */
40using namespace std;
41
42NetworkGameManager* NetworkGameManager::singletonRef = NULL;
43
44/*!
45 * Standard constructor
46 */
47NetworkGameManager::NetworkGameManager()
48  : Synchronizeable()
49{
50  PRINTF(0)("START\n");
51
52  /* set the class id for the base object */
53  this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager");
54
55  hasRequestedWorld = false;
56  this->setSynchronized(true);
57}
58
59/*!
60 * Standard destructor
61 */
62NetworkGameManager::~NetworkGameManager()
63{
64  for ( int i = 0; i<outBuffer.size(); i++)
65  {
66    if ( outBuffer[i].buffer )
67      delete outBuffer[i].buffer;
68  }
69
70}
71
72
73int NetworkGameManager::writeBytes(const byte* data, int length, int sender)
74{
75  int i = 0;
76  byte b;
77
78  while ( i<length )
79  {
80    b = data[i++];
81
82    /**************** Commands only processed by servers ****************/
83    if ( isServer() )
84    {
85      if ( b == NET_REQUEST_CREATE )
86      {
87        if ( !handleRequestCreate( i, data, length, sender ) )
88          return i;
89        continue;
90      }
91      else if ( b == NET_REQUEST_REMOVE )
92      {
93        if ( !handleRequestRemove( i, data, length, sender ) )
94          return i;
95        continue;
96      }
97      else if ( b == NET_REQUEST_PNODE_PATH )
98      {
99        if ( !handleRequestPNodePath( i, data, length, sender ) )
100          return i;
101        continue;
102      }
103    }
104    else
105    {
106      /**************** Commands only processed by clients ****************/
107      if ( b == NET_CREATE_ENTITY )
108      {
109        PRINTF(0)("CREATE_ENTITY\n");
110        if ( !handleCreateEntity( i, data, length, sender ) )
111          return i;
112        continue;
113      }
114      else if ( b == NET_REMOVE_ENTITY )
115      {
116        if ( !handleRemoveEntity( i, data, length, sender ) )
117          return i;
118        continue;
119      }
120      else if ( b == NET_CREATE_ENTITY_LIST )
121      {
122        if ( !handleCreateEntityList( i, data, length, sender ) )
123          return i;
124        continue;
125      }
126      else if ( b == NET_REMOVE_ENTITY_LIST )
127      {
128        if ( !handleRemoveEntityList( i, data, length, sender ) )
129          return i;
130        continue;
131      }
132      else if ( b == NET_YOU_ARE_ENTITY )
133      {
134        if ( !handleYouAreEntity( i, data, length, sender ) )
135          return i;
136        continue;
137      }
138    }
139
140    /**************** Commands processed by servers and clients ****************/
141    if ( b == NET_REQUEST_ENTITY_LIST )
142    {
143      sendEntityList( sender );
144      continue;
145    }
146    else if ( b == NET_REQUEST_SYNC )
147    {
148      if ( !handleRequestSync( i, data, length, sender ) )
149        return i;
150      continue;
151    }
152
153
154    PRINTF(1)("Network is asynchronous: couldn't decode the command sent by %i\n", sender);
155    PRINTF(1)("Probably this is because the network protocol has different \n");
156    PRINTF(1)("versions or there occured an error in the sending algorithm\n");
157    PRINTF(1)("Data is not in the right format! i=%d\n", i);
158    return i;
159  }
160
161  return i;
162}
163
164int NetworkGameManager::readBytes(byte* data, int maxLength, int * reciever)
165{
166  if ( !isServer() && !hasRequestedWorld )
167  {
168    SYNCHELP_WRITE_BEGIN();
169    byte b = NET_REQUEST_ENTITY_LIST;
170    SYNCHELP_WRITE_BYTE( b );
171    hasRequestedWorld = true;
172    return SYNCHELP_WRITE_N;
173  }
174
175  for ( int i = 0; i<outBuffer.size(); i++ )
176  {
177    *reciever = i;
178    if ( outBuffer[i].length>0 )
179    {
180      int nbytes = outBuffer[i].length;
181      outBuffer[i].length = 0;
182
183      if ( nbytes > maxLength )
184      {
185        PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.maxLength\n", nbytes, maxLength);
186        return 0;
187      }
188
189      memcpy(data, outBuffer[i].buffer, nbytes);
190      return nbytes;
191    }
192  }
193
194  return 0;
195}
196
197void NetworkGameManager::writeDebug() const
198{
199}
200
201void NetworkGameManager::readDebug() const
202{
203}
204
205
206/*!
207 * Checks whether this is connected to a server or a client
208 * and afterwards creates the needed entity
209 * @param classID: The ID of the class of which an entity should be created
210 */
211int NetworkGameManager::createEntity( ClassID classID, int owner )
212{
213  if ( this->isServer() )
214  {
215    if ( NetworkManager::getInstance()->getNewUniqueID() < 0 )
216    {
217      PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");
218      return -1;
219    }
220
221    return this->executeCreateEntity( classID, NetworkManager::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 ( NetworkManager::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( NetworkManager::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  doCreateEntity( classID, uniqueID, owner );
358
359  return uniqueID;
360}
361
362/*!
363 * Removes the specified entity
364 * This function is called if this is a server
365 * @param uniqueID: The ID of the entity object which should be removed
366 */
367void NetworkGameManager::executeRemoveEntity(int uniqueID)
368{
369  for ( int i = 0; i<outBuffer.size(); i++)
370  {
371    if ( !this->networkStream->isUserIdActive( i ) )
372      continue;
373
374    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REMOVE_ENTITY ) )
375      return;
376    if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
377      return;
378  }
379
380  doRemoveEntity(uniqueID);
381}
382
383/*!
384 * Checks whether it is possible to create an entity of a given class
385 * @return: true if the entity can be created, false otherwise
386 */
387bool NetworkGameManager::canCreateEntity(ClassID classID)
388{
389  return true;
390}
391
392/*!
393 * Sends the Entities to the new connected client
394 * @param userID: The ID of the user
395 */
396void NetworkGameManager::sendEntityList( int userID )
397{
398  if ( !isServer() )
399    return;
400
401  if ( userID >= outBuffer.size() )
402    resizeBufferVector( userID );
403
404  SynchronizeableList::const_iterator it, e;
405
406  it = this->networkStream->getSyncBegin();
407  e = this->networkStream->getSyncEnd();
408
409  if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_CREATE_ENTITY_LIST ) )
410    return;
411
412  // -2 because you must not send network_game_manager and handshake
413  if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount() ) )
414    return;
415
416  //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 );
417
418  int n = 0;
419
420  while ( it != e )
421  {
422    if( (*it)->beSynchronized())
423    {
424      PRINTF(0)("SENDING ENTITY %s id %d\n", (*it)->getClassName(), (*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/**
457 * Creates a buffer for user n
458 * @param n The ID of the user
459 */
460void NetworkGameManager::resizeBufferVector( int n )
461{
462  for ( int i = outBuffer.size(); i<=n; i++)
463  {
464    clientBuffer outBuf;
465
466    outBuf.length = 0;
467
468    outBuf.maxLength = 5*1024;
469
470    outBuf.buffer = new byte[5*1014];
471
472    outBuffer.push_back(outBuf);
473  }
474}
475
476/**
477 * Creates the entity on this host
478 * @param classID: ClassID of the entity to create
479 * @param uniqueID: Unique ID to assign to the synchronizeable
480 * @param owner: owner of this synchronizealbe
481 */
482BaseObject* NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner )
483{
484  BaseObject * b = Factory::fabricate( classID );
485
486  if ( !b )
487  {
488    PRINTF(1)("Could not fabricate Object with classID %x\n", classID);
489    return NULL;
490  }
491
492  if ( b->isA(CL_SYNCHRONIZEABLE) )
493  {
494    Synchronizeable * s = dynamic_cast<Synchronizeable*>(b);
495    s->setUniqueID( uniqueID );
496    s->setOwner( owner );
497    s->setSynchronized(true);
498    //this->networkStream->connectSynchronizeable( *s );
499    if ( !isServer() )
500      s->setIsOutOfSync( true );
501    PRINTF(0)("Fabricated %s with id %d\n", s->getClassName(), s->getUniqueID());
502
503    //HACK: hack to prevent collision
504    if ( b->isA(CL_WORLD_ENTITY) && !b->isA(CL_PLAYABLE) )
505    {
506      if ( NetworkManager::getInstance()->getHostID()!=0 )
507      {
508        static Vector pos = Vector(1000.0, 1000.0, 1000.0);
509        PNode *p = dynamic_cast<PNode*>(b);
510        p->setAbsCoor(pos);
511        //p->updateNode(0);
512        pos += Vector(1000.0, 1000.0, 1000.0);
513      }
514    }
515
516    return b;
517  }
518  else
519  {
520    PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)classID);
521    delete b;
522  }
523  return NULL;
524}
525
526/**
527 * Removes a entity on this host
528 * @param uniqueID: unique ID assigned with the entity to remove
529 */
530void NetworkGameManager::doRemoveEntity( int uniqueID )
531{
532  SynchronizeableList::const_iterator it,e;
533  it = this->networkStream->getSyncBegin();
534  e = this->networkStream->getSyncEnd();
535
536  while ( it != e )
537  {
538    if ( (*it)->getUniqueID() == uniqueID )
539    {
540      delete *it;
541      break;
542    }
543    it++;
544  }
545}
546
547/**
548 * Tell the synchronizeable that a user's synchronizeable is out of sync
549 * @param uniqueID: unique ID assigned with the entity which is out of sync
550 * @param userID: user ID who's synchronizeable is out of sync
551 */
552void NetworkGameManager::doRequestSync( int uniqueID, int userID )
553{
554  SynchronizeableList::const_iterator it,e;
555  it = this->networkStream->getSyncBegin();
556  e = this->networkStream->getSyncEnd();
557
558  while ( it != e )
559  {
560    if ( (*it)->getUniqueID() == uniqueID )
561    {
562      (*it)->requestSync( userID );
563      break;
564    }
565    it++;
566  }
567}
568
569/**
570 * Copies length bytes to the clientBuffer with error checking
571 * @param clientBuffer: the clientBuffer to write to
572 * @param data: buffer to the data
573 * @param length: length of data
574 * @return false on error true else
575 */
576bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length )
577{
578  if ( length > cb.maxLength-cb.length )
579  {
580    PRINTF(1)("No space left in clientBuffer\n");
581    return false;
582  }
583
584  memcpy( cb.buffer+cb.length, data, length );
585  return true;
586}
587
588/**
589 * Reads data from clientBuffer with error checking
590 * @param clientBuffer: the clientBuffer to read from
591 * @param data: pointer to the buffer
592 * @param length:
593 * @return
594 */
595bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length )
596{
597  if ( cb.length < length )
598  {
599    PRINTF(0)("There is not enough data in clientBuffer\n");
600    return 0;
601  }
602
603  memcpy( data, cb.buffer+cb.length-length, length );
604  return true;
605}
606
607/**
608 * Tells this client that he has to control this entity
609 * @param uniqueID: the entity's uniqeID
610 */
611void NetworkGameManager::doYouAre( int uniqueID )
612{
613
614  SynchronizeableList::const_iterator it = this->networkStream->getSyncBegin();
615
616  Playable *p = NULL;
617  Synchronizeable *s = NULL;
618
619  for ( ; it !=networkStream->getSyncEnd(); it++ )
620  {
621    if ( (*it)->getUniqueID()==uniqueID )
622    {
623      if ( (*it)->isA( CL_SYNCHRONIZEABLE ) )
624      {
625        s = dynamic_cast<Synchronizeable*>(*it);
626      }
627      if ( (*it)->isA( CL_PLAYABLE ) )
628      {
629        p = dynamic_cast<Playable*>(*it);
630        break;
631      } else
632      {
633        PRINTF(1)("UniqueID is not a Playable\n");
634      }
635    }
636  }
637
638  Player* player = State::getPlayer();
639  assert(p != NULL);
640  assert(s != NULL);
641  assert(player != NULL);
642
643  s->setIsOutOfSync( true );
644
645  PRINTF(0)("uniqueID = %d\n", s->getUniqueID());
646
647  player->setControllable(p);
648
649
650}
651
652/**
653 * Tells a remote client that he has to control this entity
654 * @param uniqueID: the entity's uniqeID
655 * @param userID: the users ID
656 */
657void NetworkGameManager::sendYouAre( int uniqueID, int userID )
658{
659  if ( !isServer() )
660    return;
661
662  if ( userID != 0 )
663  {
664    if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_YOU_ARE_ENTITY ) )
665      return;
666
667    if ( !writeToClientBuffer( outBuffer[userID], uniqueID ) )
668      return;
669  }
670  else
671  {
672    doYouAre(uniqueID);
673  }
674}
675
676bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender )
677{
678  if ( INTSIZE > length-i )
679  {
680    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
681    return false;
682  }
683  int classID;
684  i += Converter::byteArrayToInt( &data[i], &classID );
685
686  createEntity( (ClassID)classID );
687
688  return true;
689}
690
691bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender )
692{
693  if ( INTSIZE > length-i )
694  {
695    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
696    return false;
697  }
698  int uniqueID;
699  i += Converter::byteArrayToInt( &data[i], &uniqueID );
700
701  removeEntity( uniqueID );
702
703  return true;
704}
705
706bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender )
707{
708  if ( INTSIZE > length-i )
709  {
710    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
711    return false;
712  }
713  int classID;
714  i += Converter::byteArrayToInt( &data[i], &classID );
715
716  if ( INTSIZE > length-i )
717  {
718    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
719    return false;
720  }
721  int uniqueID;
722  i += Converter::byteArrayToInt( &data[i], &uniqueID );
723
724  if ( INTSIZE > length-i )
725  {
726    PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
727    return false;
728  }
729  int owner;
730  i += Converter::byteArrayToInt( &data[i], &owner );
731
732  doCreateEntity( (ClassID)classID, uniqueID, owner );
733
734  return true;
735}
736
737bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender )
738{
739  if ( INTSIZE > length-i )
740  {
741    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
742    return false;
743  }
744  int uniqueID;
745  i += Converter::byteArrayToInt( &data[i], &uniqueID );
746
747  doRemoveEntity( uniqueID );
748
749  return true;
750}
751
752bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender )
753{
754  if ( INTSIZE > length-i )
755  {
756    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
757    return false;
758  }
759
760  PRINTF(0)("HandleCreateEntityList:  data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);
761
762  int n;
763  i += Converter::byteArrayToInt( &data[i], &n );
764
765
766  PRINTF(0)("HandleCreateEntityList: n = %d\n", n);
767
768  int classID, uniqueID, owner;
769
770  for ( int j = 0; j<n; j++ )
771  {
772
773    if ( INTSIZE > length-i )
774    {
775      PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
776      return false;
777    }
778    i += Converter::byteArrayToInt( &data[i], &classID );
779
780    if ( INTSIZE > length-i )
781    {
782      PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
783      return false;
784    }
785    i += Converter::byteArrayToInt( &data[i], &uniqueID );
786
787    if ( INTSIZE > length-i )
788    {
789      PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
790      return false;
791    }
792    i += Converter::byteArrayToInt( &data[i], &owner );
793
794    if ( classID != CL_NETWORK_GAME_MANAGER && classID != CL_HANDSHAKE )
795    {
796      BaseObject* b = doCreateEntity( (ClassID)classID, uniqueID, owner );
797
798      /*if ( b != NULL )
799      {
800        if ( b->isA(CL_WORLD_ENTITY) )
801        {
802          int n = dynamic_cast<WorldEntity*>(b)->writeState( data, length, sender );
803
804          i += n;
805        }
806    }*/
807    }
808
809  }
810
811  return true;
812}
813
814bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender )
815{
816  if ( INTSIZE > length-i )
817  {
818    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
819    return false;
820  }
821  int n;
822  i += Converter::byteArrayToInt( &data[i], &n );
823
824  int uniqueID;
825
826  for ( int j = 0; j<n; j++ )
827  {
828
829    if ( INTSIZE > length-i )
830    {
831      PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
832      return false;
833    }
834    i += Converter::byteArrayToInt( &data[i], &uniqueID );
835
836    doRemoveEntity( uniqueID );
837  }
838
839  return true;
840}
841
842bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender )
843{
844  if ( INTSIZE > length-i )
845  {
846    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
847    return false;
848  }
849
850  int uniqueID;
851  i += Converter::byteArrayToInt( &data[i], &uniqueID );
852
853  doYouAre( uniqueID );
854
855  return true;
856}
857
858bool NetworkGameManager::handleRequestSync( int & i, const byte * data, int length, int sender )
859{
860  if ( INTSIZE > length-i )
861  {
862    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
863    return false;
864  }
865  int uniqueID;
866  i += Converter::byteArrayToInt( &data[i], &uniqueID );
867
868  doRequestSync( uniqueID, sender );
869
870  return true;
871}
872
873
874/**
875 *  handles the network signal NET_REQUEST_PNODE_PATH
876 * @param i byte offset in the buffer
877 * @param data data array
878 * @param length length of the data arary
879 * @param sender the sender id
880 * @return true if process terminated sucessfully
881 */
882bool NetworkGameManager::handleRequestPNodePath(int& i, const byte* data, int length, int sender)
883{
884  if( INTSIZE > length-i )
885  {
886    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
887    return false;
888  }
889  PRINTF(0)("HandleRequestPNodePath:  data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);
890
891  int uniqueID1, uniqueID2;
892  if( INTSIZE > length-i )
893  {
894    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
895    return false;
896  }
897  i += Converter::byteArrayToInt( &data[i], &uniqueID1 );
898
899  if( INTSIZE > length-i )
900  {
901    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
902    return false;
903  }
904  i += Converter::byteArrayToInt( &data[i], &uniqueID2 );
905
906
907  PRINTF(0)("HandleRequestPNodePath: got a request for path from uid %i to uid %i\n", uniqueID1, uniqueID2);
908
909  return true;
910}
911
912
913bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b )
914{
915  if ( cb.maxLength-cb.length < 1 )
916  {
917    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 byte\n");
918    return false;
919  }
920
921  cb.buffer[cb.length++] = b;
922
923  return true;
924}
925
926bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, int i )
927{
928  int n = Converter::intToByteArray( i, cb.buffer+cb.length, cb.maxLength-cb.length );
929  cb.length += n;
930
931  if ( n <= 0 )
932  {
933    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 int\n");
934    return false;
935  }
936
937  return true;
938}
939
940void NetworkGameManager::sync( int uniqueID, int owner )
941{
942  /*if ( owner==this->getHostID() )
943  return;*/
944
945  if ( !isServer() )
946    executeRequestSync( uniqueID, 0 );
947  else
948    executeRequestSync( uniqueID, owner );
949}
950
951void NetworkGameManager::executeRequestSync( int uniqueID, int user )
952{
953  if ( user >= outBuffer.size() )
954    resizeBufferVector( user );
955
956  if ( !writeToClientBuffer( outBuffer[user], (byte)NET_REQUEST_SYNC ) )
957    return;
958  if ( !writeToClientBuffer( outBuffer[user], uniqueID ) )
959    return;
960}
961
Note: See TracBrowser for help on using the repository browser.