Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: working on helper funciton

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