Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: sys better now, got less errors and removed the pnode link sync alg, replacing

File size: 23.1 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 ( SharedNetworkData::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, SharedNetworkData::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 ( SharedNetworkData::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( SharedNetworkData::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  PRINTF(0)("ExecuteCreateEntity: server side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
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 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/**
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  PRINTF(0)("Creating Entity via Factory: classid: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
485
486  BaseObject * b;
487  /* These are some small exeptions in creation: Not all objects can/should be created via Factory */
488  /* Exception 1: NullParent */
489  if( classID == CL_NULL_PARENT)
490  {
491    b = PNode::getNullParent();
492  }
493  else
494    b = Factory::fabricate( classID );
495
496  if ( !b )
497  {
498    PRINTF(1)("Could not fabricate Object with classID %x\n", classID);
499    return NULL;
500  }
501
502  if ( b->isA(CL_SYNCHRONIZEABLE) )
503  {
504    Synchronizeable * s = dynamic_cast<Synchronizeable*>(b);
505    s->setUniqueID( uniqueID );
506    s->setOwner( owner );
507    s->setSynchronized(true);
508    //this->networkStream->connectSynchronizeable( *s );
509    if ( !isServer() )
510      s->setIsOutOfSync( true );
511    PRINTF(0)("Fabricated %s with id %d\n", s->getClassName(), s->getUniqueID());
512
513    //HACK: hack to prevent collision
514    if ( b->isA(CL_WORLD_ENTITY) && !b->isA(CL_PLAYABLE) )
515    {
516      if ( NetworkManager::getInstance()->getHostID()!=0 )
517      {
518        static Vector pos = Vector(1000.0, 1000.0, 1000.0);
519        PNode *p = dynamic_cast<PNode*>(b);
520        p->setAbsCoor(pos);
521        //p->updateNode(0);
522        pos += Vector(1000.0, 1000.0, 1000.0);
523      }
524    }
525
526    return b;
527  }
528  else
529  {
530    PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)classID);
531    delete b;
532  }
533  return NULL;
534}
535
536/**
537 * Removes a entity on this host
538 * @param uniqueID: unique ID assigned with the entity to remove
539 */
540void NetworkGameManager::doRemoveEntity( int uniqueID )
541{
542  SynchronizeableList::const_iterator it,e;
543  it = this->networkStream->getSyncBegin();
544  e = this->networkStream->getSyncEnd();
545
546  while ( it != e )
547  {
548    if ( (*it)->getUniqueID() == uniqueID )
549    {
550      delete *it;
551      break;
552    }
553    it++;
554  }
555}
556
557/**
558 * Tell the synchronizeable that a user's synchronizeable is out of sync
559 * @param uniqueID: unique ID assigned with the entity which is out of sync
560 * @param userID: user ID who's synchronizeable is out of sync
561 */
562void NetworkGameManager::doRequestSync( int uniqueID, int userID )
563{
564  SynchronizeableList::const_iterator it,e;
565  it = this->networkStream->getSyncBegin();
566  e = this->networkStream->getSyncEnd();
567
568  while ( it != e )
569  {
570    if ( (*it)->getUniqueID() == uniqueID )
571    {
572      (*it)->requestSync( userID );
573      break;
574    }
575    it++;
576  }
577}
578
579/**
580 * Copies length bytes to the clientBuffer with error checking
581 * @param clientBuffer: the clientBuffer to write to
582 * @param data: buffer to the data
583 * @param length: length of data
584 * @return false on error true else
585 */
586bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length )
587{
588  if ( length > cb.maxLength-cb.length )
589  {
590    PRINTF(1)("No space left in clientBuffer\n");
591    return false;
592  }
593
594  memcpy( cb.buffer+cb.length, data, length );
595  return true;
596}
597
598/**
599 * Reads data from clientBuffer with error checking
600 * @param clientBuffer: the clientBuffer to read from
601 * @param data: pointer to the buffer
602 * @param length:
603 * @return
604 */
605bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length )
606{
607  if ( cb.length < length )
608  {
609    PRINTF(0)("There is not enough data in clientBuffer\n");
610    return 0;
611  }
612
613  memcpy( data, cb.buffer+cb.length-length, length );
614  return true;
615}
616
617/**
618 * Tells this client that he has to control this entity
619 * @param uniqueID: the entity's uniqeID
620 */
621void NetworkGameManager::doYouAre( int uniqueID )
622{
623
624  SynchronizeableList::const_iterator it = this->networkStream->getSyncBegin();
625
626  Playable *p = NULL;
627  Synchronizeable *s = NULL;
628
629  for ( ; it !=networkStream->getSyncEnd(); it++ )
630  {
631    if ( (*it)->getUniqueID()==uniqueID )
632    {
633      if ( (*it)->isA( CL_SYNCHRONIZEABLE ) )
634      {
635        s = dynamic_cast<Synchronizeable*>(*it);
636      }
637      if ( (*it)->isA( CL_PLAYABLE ) )
638      {
639        p = dynamic_cast<Playable*>(*it);
640        break;
641      } else
642      {
643        PRINTF(1)("UniqueID is not a Playable\n");
644      }
645    }
646  }
647
648  Player* player = State::getPlayer();
649  assert(p != NULL);
650  assert(s != NULL);
651  assert(player != NULL);
652
653  s->setIsOutOfSync( true );
654
655  PRINTF(0)("uniqueID = %d\n", s->getUniqueID());
656
657  player->setControllable(p);
658
659
660}
661
662/**
663 * Tells a remote client that he has to control this entity
664 * @param uniqueID: the entity's uniqeID
665 * @param userID: the users ID
666 */
667void NetworkGameManager::sendYouAre( int uniqueID, int userID )
668{
669  if ( !isServer() )
670    return;
671
672  if ( userID != 0 )
673  {
674    if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_YOU_ARE_ENTITY ) )
675      return;
676
677    if ( !writeToClientBuffer( outBuffer[userID], uniqueID ) )
678      return;
679  }
680  else
681  {
682    doYouAre(uniqueID);
683  }
684}
685
686bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender )
687{
688  if ( INTSIZE > length-i )
689  {
690    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
691    return false;
692  }
693  int classID;
694  i += Converter::byteArrayToInt( &data[i], &classID );
695
696  createEntity( (ClassID)classID );
697
698  return true;
699}
700
701bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender )
702{
703  if ( INTSIZE > length-i )
704  {
705    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
706    return false;
707  }
708  int uniqueID;
709  i += Converter::byteArrayToInt( &data[i], &uniqueID );
710
711  removeEntity( uniqueID );
712
713  return true;
714}
715
716bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender )
717{
718  if ( INTSIZE > length-i )
719  {
720    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
721    return false;
722  }
723  int classID;
724  i += Converter::byteArrayToInt( &data[i], &classID );
725
726  if ( INTSIZE > length-i )
727  {
728    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
729    return false;
730  }
731  int uniqueID;
732  i += Converter::byteArrayToInt( &data[i], &uniqueID );
733
734  if ( INTSIZE > length-i )
735  {
736    PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
737    return false;
738  }
739  int owner;
740  i += Converter::byteArrayToInt( &data[i], &owner );
741
742  PRINTF(0)("handleCreateEntity: client side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
743  doCreateEntity( (ClassID)classID, uniqueID, owner );
744
745  return true;
746}
747
748bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender )
749{
750  if ( INTSIZE > length-i )
751  {
752    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
753    return false;
754  }
755  int uniqueID;
756  i += Converter::byteArrayToInt( &data[i], &uniqueID );
757
758  doRemoveEntity( uniqueID );
759
760  return true;
761}
762
763bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender )
764{
765  if ( INTSIZE > length-i )
766  {
767    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
768    return false;
769  }
770
771  PRINTF(0)("HandleCreateEntityList:  data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);
772
773  int n;
774  i += Converter::byteArrayToInt( &data[i], &n );
775
776
777  PRINTF(0)("HandleCreateEntityList: n = %d\n", n);
778
779  int classID, uniqueID, owner;
780
781  for ( int j = 0; j<n; j++ )
782  {
783
784    if ( INTSIZE > length-i )
785    {
786      PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
787      return false;
788    }
789    i += Converter::byteArrayToInt( &data[i], &classID );
790
791    if ( INTSIZE > length-i )
792    {
793      PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
794      return false;
795    }
796    i += Converter::byteArrayToInt( &data[i], &uniqueID );
797
798    if ( INTSIZE > length-i )
799    {
800      PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
801      return false;
802    }
803    i += Converter::byteArrayToInt( &data[i], &owner );
804
805    if ( classID != CL_NETWORK_GAME_MANAGER && classID != CL_HANDSHAKE )
806    {
807      BaseObject* b = doCreateEntity( (ClassID)classID, uniqueID, owner );
808
809      /*if ( b != NULL )
810      {
811        if ( b->isA(CL_WORLD_ENTITY) )
812        {
813          int n = dynamic_cast<WorldEntity*>(b)->writeState( data, length, sender );
814
815          i += n;
816        }
817    }*/
818    }
819
820  }
821
822  return true;
823}
824
825bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender )
826{
827  if ( INTSIZE > length-i )
828  {
829    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
830    return false;
831  }
832  int n;
833  i += Converter::byteArrayToInt( &data[i], &n );
834
835  int uniqueID;
836
837  for ( int j = 0; j<n; j++ )
838  {
839
840    if ( INTSIZE > length-i )
841    {
842      PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
843      return false;
844    }
845    i += Converter::byteArrayToInt( &data[i], &uniqueID );
846
847    doRemoveEntity( uniqueID );
848  }
849
850  return true;
851}
852
853bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender )
854{
855  if ( INTSIZE > length-i )
856  {
857    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
858    return false;
859  }
860
861  int uniqueID;
862  i += Converter::byteArrayToInt( &data[i], &uniqueID );
863
864  doYouAre( uniqueID );
865
866  return true;
867}
868
869bool NetworkGameManager::handleRequestSync( int & i, const byte * data, int length, int sender )
870{
871  if ( INTSIZE > length-i )
872  {
873    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
874    return false;
875  }
876  int uniqueID;
877  i += Converter::byteArrayToInt( &data[i], &uniqueID );
878
879  doRequestSync( uniqueID, sender );
880
881  return true;
882}
883
884
885/**
886 *  handles the network signal NET_REQUEST_PNODE_PATH
887 * @param i byte offset in the buffer
888 * @param data data array
889 * @param length length of the data arary
890 * @param sender the sender id
891 * @return true if process terminated sucessfully
892 */
893bool NetworkGameManager::handleRequestPNodePath(int& i, const byte* data, int length, int sender)
894{
895  if( INTSIZE > length-i )
896  {
897    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
898    return false;
899  }
900  PRINTF(0)("HandleRequestPNodePath:  data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);
901
902  int uniqueID1, uniqueID2;
903  if( INTSIZE > length-i )
904  {
905    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
906    return false;
907  }
908  i += Converter::byteArrayToInt( &data[i], &uniqueID1 );
909
910  if( INTSIZE > length-i )
911  {
912    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
913    return false;
914  }
915  i += Converter::byteArrayToInt( &data[i], &uniqueID2 );
916
917
918  PRINTF(0)("HandleRequestPNodePath: got a request for path from uid %i to uid %i\n", uniqueID1, uniqueID2);
919
920  return true;
921}
922
923
924bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b )
925{
926  if ( cb.maxLength-cb.length < 1 )
927  {
928    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 byte\n");
929    return false;
930  }
931
932  cb.buffer[cb.length++] = b;
933
934  return true;
935}
936
937bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, int i )
938{
939  int n = Converter::intToByteArray( i, cb.buffer+cb.length, cb.maxLength-cb.length );
940  cb.length += n;
941
942  if ( n <= 0 )
943  {
944    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 int\n");
945    return false;
946  }
947
948  return true;
949}
950
951void NetworkGameManager::sync( int uniqueID, int owner )
952{
953  /*if ( owner==this->getHostID() )
954  return;*/
955
956  if ( !isServer() )
957    executeRequestSync( uniqueID, 0 );
958  else
959    executeRequestSync( uniqueID, owner );
960}
961
962void NetworkGameManager::executeRequestSync( int uniqueID, int user )
963{
964  if ( user >= outBuffer.size() )
965    resizeBufferVector( user );
966
967  if ( !writeToClientBuffer( outBuffer[user], (byte)NET_REQUEST_SYNC ) )
968    return;
969  if ( !writeToClientBuffer( outBuffer[user], uniqueID ) )
970    return;
971}
972
Note: See TracBrowser for help on using the repository browser.