Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6678 was 6634, checked in by bensch, 19 years ago

orxonox/trunk: merged the network-branche back to the trunk

merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r6500:HEAD
minor conflicts in texture and one Makefile resolved to the trunk

also made a small patch to texture, so it Modulates with GL_REPEAT

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