Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7775 was 7772, checked in by rennerc, 18 years ago

fixed bug

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