Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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