Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: some more nodes connected to the NetworkStream no sync yet

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