Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

newtork: now only entities created on the server get a valiable 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    return this->executeCreateEntity( classID, NetworkManager::getInstance()->getNewUniqueID(), owner );
221  }
222  else
223  {
224    this->requestCreateEntity( classID );
225    return -1;
226  }
227}
228
229
230/*!
231 * Checks whether this is connected to a server or a client
232 * and afterwards creates the needed entity
233 * @param classID: The ID of the class of which an entity should be created
234 */
235BaseObject* NetworkGameManager::createEntity(const TiXmlElement* element)
236{
237  if ( this->isServer() )
238  {
239    if ( NetworkManager::getInstance()->getNewUniqueID() < 0 )
240    {
241      PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");
242      return NULL;
243    }
244
245    BaseObject * b = Factory::fabricate( element );
246
247    if ( !b )
248    {
249      PRINTF(1)("Could not fabricate Object with className %s\n", element->Value() );
250      return NULL;
251    }
252
253
254    if ( b->isA(CL_SYNCHRONIZEABLE) )
255    {
256      Synchronizeable * s = dynamic_cast<Synchronizeable*>(b);
257      s->setUniqueID( NetworkManager::getInstance()->getNewUniqueID() );
258      s->setOwner( 0 );
259      // all entities created via this function are added automaticaly to the synchronizeable list
260      s->setSynchronized(true);
261      return b;
262    }
263    else
264    {
265      PRINTF(1)("Class %s is not a synchronizeable!\n", b->getClassName() );
266      delete b;
267    }
268
269  }
270  else
271
272  {
273    PRINTF(1)("This node is not a server and cannot create id %x\n", element->Value());
274  }
275  return NULL;
276}
277
278
279/*!
280 * Checks whether this is connected to a server or a client
281 * and afterwards removes the specified entity
282 * @param uniqueID: The ID of the entity object which should be removed
283 */
284void NetworkGameManager::removeEntity(int uniqueID)
285{
286  if ( this->isServer() )
287  {
288    this->executeRemoveEntity( uniqueID );
289  }
290  else
291  {
292    this->requestRemoveEntity( uniqueID );
293  }
294}
295
296
297
298/*!
299 * Creates the needed entity on the server if possible
300 * @param classID: The ID of the class of which an entity should be created
301 */
302void NetworkGameManager::requestCreateEntity(ClassID classID)
303{
304  for ( int i = 0; i<outBuffer.size(); i++)
305  {
306    if ( !this->networkStream->isUserIdActive( i ) )
307      continue;
308
309    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_CREATE ) )
310      return;
311    if ( !writeToClientBuffer( outBuffer[i], (int)classID ) )
312      return;
313  }
314}
315
316/*!
317 * Removes the specified entity on the server
318 * @param uniqueID: The ID of the entity object which should be removed
319 */
320void NetworkGameManager::requestRemoveEntity(int uniqueID)
321{
322  for ( int i = 0; i<outBuffer.size(); i++)
323  {
324    if ( !this->networkStream->isUserIdActive( i ) )
325      continue;
326
327    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_REMOVE ) )
328      return;
329    if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
330      return;
331  }
332}
333
334/*!
335 * Creates the needed entity if possible
336 * This function is called if this is a server
337 * @param classID: The ID of the class of which an entity should be created
338 */
339int NetworkGameManager::executeCreateEntity(ClassID classID, int uniqueID, int owner)
340{
341  for ( int i = 0; i<outBuffer.size(); i++)
342  {
343    if ( !this->networkStream->isUserIdActive( i ) )
344      continue;
345
346    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_CREATE_ENTITY ) )
347      return -1;
348    if ( !writeToClientBuffer( outBuffer[i], (int)classID ) )
349      return -1;
350    if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
351      return -1;
352    if ( !writeToClientBuffer( outBuffer[i], owner ) )
353      return -1;
354  }
355
356  doCreateEntity( classID, uniqueID, owner );
357
358  return uniqueID;
359}
360
361/*!
362 * Removes the specified entity
363 * This function is called if this is a server
364 * @param uniqueID: The ID of the entity object which should be removed
365 */
366void NetworkGameManager::executeRemoveEntity(int uniqueID)
367{
368  for ( int i = 0; i<outBuffer.size(); i++)
369  {
370    if ( !this->networkStream->isUserIdActive( i ) )
371      continue;
372
373    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REMOVE_ENTITY ) )
374      return;
375    if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
376      return;
377  }
378
379  doRemoveEntity(uniqueID);
380}
381
382/*!
383 * Checks whether it is possible to create an entity of a given class
384 * @return: true if the entity can be created, false otherwise
385 */
386bool NetworkGameManager::canCreateEntity(ClassID classID)
387{
388  return true;
389}
390
391/*!
392 * Sends the Entities to the new connected client
393 * @param userID: The ID of the user
394 */
395void NetworkGameManager::sendEntityList( int userID )
396{
397  if ( !isServer() )
398    return;
399
400  if ( userID >= outBuffer.size() )
401    resizeBufferVector( userID );
402
403  SynchronizeableList::const_iterator it, e;
404
405  it = this->networkStream->getSyncBegin();
406  e = this->networkStream->getSyncEnd();
407
408  if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_CREATE_ENTITY_LIST ) )
409    return;
410
411  // -2 because you must not send network_game_manager and handshake
412  if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount() ) )
413    return;
414
415  //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 );
416
417  int n = 0;
418
419  while ( it != e )
420  {
421    if( (*it)->beSynchronized())
422    {
423      PRINTF(0)("SENDING ENTITY %s id %d\n", (*it)->getClassName(), (*it)->getUniqueID() );
424      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) )
425        return;
426
427      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getUniqueID()) ) )
428        return;
429
430      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getOwner()) ) )
431        return;
432    }
433    it++;
434  }
435
436  signalNewPlayer( userID );
437}
438
439
440
441bool NetworkGameManager::signalNewPlayer(int userId)
442{
443  if ( userId >= outBuffer.size() )
444    resizeBufferVector( userId );
445
446  /* create new playable for Player*/
447  PRINTF(0)("Request for creation: %i\n", userId);
448  int uniqueId = this->createEntity(CL_SPACE_SHIP, userId);
449  PRINTF(0)("Request for creation: userid: %i, uniqueid: %i\n", userId, uniqueId);
450  this->sendYouAre(uniqueId, userId);
451
452}
453
454
455/**
456 * Creates a buffer for user n
457 * @param n The ID of the user
458 */
459void NetworkGameManager::resizeBufferVector( int n )
460{
461  for ( int i = outBuffer.size(); i<=n; i++)
462  {
463    clientBuffer outBuf;
464
465    outBuf.length = 0;
466
467    outBuf.maxLength = 5*1024;
468
469    outBuf.buffer = new byte[5*1014];
470
471    outBuffer.push_back(outBuf);
472  }
473}
474
475/**
476 * Creates the entity on this host
477 * @param classID: ClassID of the entity to create
478 * @param uniqueID: Unique ID to assign to the synchronizeable
479 * @param owner: owner of this synchronizealbe
480 */
481BaseObject* NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner )
482{
483  BaseObject * b = Factory::fabricate( classID );
484
485  if ( !b )
486  {
487    PRINTF(1)("Could not fabricate Object with classID %x\n", classID);
488    return NULL;
489  }
490
491  if ( b->isA(CL_SYNCHRONIZEABLE) )
492  {
493    Synchronizeable * s = dynamic_cast<Synchronizeable*>(b);
494    s->setUniqueID( uniqueID );
495    s->setOwner( owner );
496    s->setSynchronized(true);
497    //this->networkStream->connectSynchronizeable( *s );
498    if ( !isServer() )
499      s->setIsOutOfSync( true );
500    PRINTF(0)("Fabricated %s with id %d\n", s->getClassName(), s->getUniqueID());
501
502    //HACK: hack to prevent collision
503    if ( b->isA(CL_WORLD_ENTITY) && !b->isA(CL_PLAYABLE) )
504    {
505      if ( NetworkManager::getInstance()->getHostID()!=0 )
506      {
507        static Vector pos = Vector(1000.0, 1000.0, 1000.0);
508        PNode *p = dynamic_cast<PNode*>(b);
509        p->setAbsCoor(pos);
510        //p->updateNode(0);
511        pos += Vector(1000.0, 1000.0, 1000.0);
512      }
513    }
514
515    return b;
516  }
517  else
518  {
519    PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)classID);
520    delete b;
521  }
522  return NULL;
523}
524
525/**
526 * Removes a entity on this host
527 * @param uniqueID: unique ID assigned with the entity to remove
528 */
529void NetworkGameManager::doRemoveEntity( int uniqueID )
530{
531  SynchronizeableList::const_iterator it,e;
532  it = this->networkStream->getSyncBegin();
533  e = this->networkStream->getSyncEnd();
534
535  while ( it != e )
536  {
537    if ( (*it)->getUniqueID() == uniqueID )
538    {
539      delete *it;
540      break;
541    }
542    it++;
543  }
544}
545
546/**
547 * Tell the synchronizeable that a user's synchronizeable is out of sync
548 * @param uniqueID: unique ID assigned with the entity which is out of sync
549 * @param userID: user ID who's synchronizeable is out of sync
550 */
551void NetworkGameManager::doRequestSync( int uniqueID, int userID )
552{
553  SynchronizeableList::const_iterator it,e;
554  it = this->networkStream->getSyncBegin();
555  e = this->networkStream->getSyncEnd();
556
557  while ( it != e )
558  {
559    if ( (*it)->getUniqueID() == uniqueID )
560    {
561      (*it)->requestSync( userID );
562      break;
563    }
564    it++;
565  }
566}
567
568/**
569 * Copies length bytes to the clientBuffer with error checking
570 * @param clientBuffer: the clientBuffer to write to
571 * @param data: buffer to the data
572 * @param length: length of data
573 * @return false on error true else
574 */
575bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length )
576{
577  if ( length > cb.maxLength-cb.length )
578  {
579    PRINTF(1)("No space left in clientBuffer\n");
580    return false;
581  }
582
583  memcpy( cb.buffer+cb.length, data, length );
584  return true;
585}
586
587/**
588 * Reads data from clientBuffer with error checking
589 * @param clientBuffer: the clientBuffer to read from
590 * @param data: pointer to the buffer
591 * @param length:
592 * @return
593 */
594bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length )
595{
596  if ( cb.length < length )
597  {
598    PRINTF(0)("There is not enough data in clientBuffer\n");
599    return 0;
600  }
601
602  memcpy( data, cb.buffer+cb.length-length, length );
603  return true;
604}
605
606/**
607 * Tells this client that he has to control this entity
608 * @param uniqueID: the entity's uniqeID
609 */
610void NetworkGameManager::doYouAre( int uniqueID )
611{
612
613  SynchronizeableList::const_iterator it = this->networkStream->getSyncBegin();
614
615  Playable *p = NULL;
616  Synchronizeable *s = NULL;
617
618  for ( ; it !=networkStream->getSyncEnd(); it++ )
619  {
620    if ( (*it)->getUniqueID()==uniqueID )
621    {
622      if ( (*it)->isA( CL_SYNCHRONIZEABLE ) )
623      {
624        s = dynamic_cast<Synchronizeable*>(*it);
625      }
626      if ( (*it)->isA( CL_PLAYABLE ) )
627      {
628        p = dynamic_cast<Playable*>(*it);
629        break;
630      } else
631      {
632        PRINTF(1)("UniqueID is not a Playable\n");
633      }
634    }
635  }
636
637  Player* player = State::getPlayer();
638  assert(p != NULL);
639  assert(s != NULL);
640  assert(player != NULL);
641
642  s->setIsOutOfSync( true );
643
644  PRINTF(0)("uniqueID = %d\n", s->getUniqueID());
645
646  player->setControllable(p);
647
648
649}
650
651/**
652 * Tells a remote client that he has to control this entity
653 * @param uniqueID: the entity's uniqeID
654 * @param userID: the users ID
655 */
656void NetworkGameManager::sendYouAre( int uniqueID, int userID )
657{
658  if ( !isServer() )
659    return;
660
661  if ( userID != 0 )
662  {
663    if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_YOU_ARE_ENTITY ) )
664      return;
665
666    if ( !writeToClientBuffer( outBuffer[userID], uniqueID ) )
667      return;
668  }
669  else
670  {
671    doYouAre(uniqueID);
672  }
673}
674
675bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender )
676{
677  if ( INTSIZE > length-i )
678  {
679    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
680    return false;
681  }
682  int classID;
683  i += Converter::byteArrayToInt( &data[i], &classID );
684
685  createEntity( (ClassID)classID );
686
687  return true;
688}
689
690bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender )
691{
692  if ( INTSIZE > length-i )
693  {
694    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
695    return false;
696  }
697  int uniqueID;
698  i += Converter::byteArrayToInt( &data[i], &uniqueID );
699
700  removeEntity( uniqueID );
701
702  return true;
703}
704
705bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender )
706{
707  if ( INTSIZE > length-i )
708  {
709    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
710    return false;
711  }
712  int classID;
713  i += Converter::byteArrayToInt( &data[i], &classID );
714
715  if ( INTSIZE > length-i )
716  {
717    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
718    return false;
719  }
720  int uniqueID;
721  i += Converter::byteArrayToInt( &data[i], &uniqueID );
722
723  if ( INTSIZE > length-i )
724  {
725    PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
726    return false;
727  }
728  int owner;
729  i += Converter::byteArrayToInt( &data[i], &owner );
730
731  doCreateEntity( (ClassID)classID, uniqueID, owner );
732
733  return true;
734}
735
736bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender )
737{
738  if ( INTSIZE > length-i )
739  {
740    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
741    return false;
742  }
743  int uniqueID;
744  i += Converter::byteArrayToInt( &data[i], &uniqueID );
745
746  doRemoveEntity( uniqueID );
747
748  return true;
749}
750
751bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender )
752{
753  if ( INTSIZE > length-i )
754  {
755    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
756    return false;
757  }
758
759  PRINTF(0)("HandleCreateEntityList:  data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);
760
761  int n;
762  i += Converter::byteArrayToInt( &data[i], &n );
763
764
765  PRINTF(0)("HandleCreateEntityList: n = %d\n", n);
766
767  int classID, uniqueID, owner;
768
769  for ( int j = 0; j<n; j++ )
770  {
771
772    if ( INTSIZE > length-i )
773    {
774      PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
775      return false;
776    }
777    i += Converter::byteArrayToInt( &data[i], &classID );
778
779    if ( INTSIZE > length-i )
780    {
781      PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
782      return false;
783    }
784    i += Converter::byteArrayToInt( &data[i], &uniqueID );
785
786    if ( INTSIZE > length-i )
787    {
788      PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
789      return false;
790    }
791    i += Converter::byteArrayToInt( &data[i], &owner );
792
793    if ( classID != CL_NETWORK_GAME_MANAGER && classID != CL_HANDSHAKE )
794    {
795      BaseObject* b = doCreateEntity( (ClassID)classID, uniqueID, owner );
796
797      /*if ( b != NULL )
798      {
799        if ( b->isA(CL_WORLD_ENTITY) )
800        {
801          int n = dynamic_cast<WorldEntity*>(b)->writeState( data, length, sender );
802
803          i += n;
804        }
805    }*/
806    }
807
808  }
809
810  return true;
811}
812
813bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender )
814{
815  if ( INTSIZE > length-i )
816  {
817    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
818    return false;
819  }
820  int n;
821  i += Converter::byteArrayToInt( &data[i], &n );
822
823  int uniqueID;
824
825  for ( int j = 0; j<n; j++ )
826  {
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    doRemoveEntity( uniqueID );
836  }
837
838  return true;
839}
840
841bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender )
842{
843  if ( INTSIZE > length-i )
844  {
845    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
846    return false;
847  }
848
849  int uniqueID;
850  i += Converter::byteArrayToInt( &data[i], &uniqueID );
851
852  doYouAre( uniqueID );
853
854  return true;
855}
856
857bool NetworkGameManager::handleRequestSync( int & i, const byte * data, int length, int sender )
858{
859  if ( INTSIZE > length-i )
860  {
861    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
862    return false;
863  }
864  int uniqueID;
865  i += Converter::byteArrayToInt( &data[i], &uniqueID );
866
867  doRequestSync( uniqueID, sender );
868
869  return true;
870}
871
872
873/**
874 *  handles the network signal NET_REQUEST_PNODE_PATH
875 * @param i byte offset in the buffer
876 * @param data data array
877 * @param length length of the data arary
878 * @param sender the sender id
879 * @return true if process terminated sucessfully
880 */
881bool NetworkGameManager::handleRequestPNodePath(int& i, const byte* data, int length, int sender)
882{
883  if( INTSIZE > length-i )
884  {
885    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
886    return false;
887  }
888  PRINTF(0)("HandleRequestPNodePath:  data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);
889
890  int uniqueID1, uniqueID2;
891  if( INTSIZE > length-i )
892  {
893    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
894    return false;
895  }
896  i += Converter::byteArrayToInt( &data[i], &uniqueID1 );
897
898  if( INTSIZE > length-i )
899  {
900    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
901    return false;
902  }
903  i += Converter::byteArrayToInt( &data[i], &uniqueID2 );
904
905
906  PRINTF(0)("HandleRequestPNodePath: got a request for path from uid %i to uid %i\n", uniqueID1, uniqueID2);
907
908  return true;
909}
910
911
912bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b )
913{
914  if ( cb.maxLength-cb.length < 1 )
915  {
916    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 byte\n");
917    return false;
918  }
919
920  cb.buffer[cb.length++] = b;
921
922  return true;
923}
924
925bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, int i )
926{
927  int n = Converter::intToByteArray( i, cb.buffer+cb.length, cb.maxLength-cb.length );
928  cb.length += n;
929
930  if ( n <= 0 )
931  {
932    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 int\n");
933    return false;
934  }
935
936  return true;
937}
938
939void NetworkGameManager::sync( int uniqueID, int owner )
940{
941  /*if ( owner==this->getHostID() )
942  return;*/
943
944  if ( !isServer() )
945    executeRequestSync( uniqueID, 0 );
946  else
947    executeRequestSync( uniqueID, owner );
948}
949
950void NetworkGameManager::executeRequestSync( int uniqueID, int user )
951{
952  if ( user >= outBuffer.size() )
953    resizeBufferVector( user );
954
955  if ( !writeToClientBuffer( outBuffer[user], (byte)NET_REQUEST_SYNC ) )
956    return;
957  if ( !writeToClientBuffer( outBuffer[user], uniqueID ) )
958    return;
959}
960
Note: See TracBrowser for help on using the repository browser.