Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: dont get the sync process to run again.

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