Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: branche compiles again

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