Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7671 was 7567, checked in by rennerc, 18 years ago

removed some output of heightMap. commented out old code in networkGameManager

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