Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6303 was 6303, checked in by rennerc, 18 years ago
File size: 18.5 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Wuest
13   co-programmer: ...
14*/
15
16
17/* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module
18   For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput
19*/
20#define DEBUG_MODULE_NETWORK
21
22#include "factory.h"
23#include "network_stream.h"
24#include "converter.h"
25
26/* include your own header */
27#include "network_game_manager.h"
28
29
30/* using namespace std is default, this needs to be here */
31using namespace std;
32
33NetworkGameManager* NetworkGameManager::singletonRef = NULL;
34
35/*!
36 * Standard constructor
37 */
38NetworkGameManager::NetworkGameManager()
39{
40  PRINTF(0)("START\n");
41
42  /* set the class id for the base object */
43  this->setClassID(CL_NETWORK_GAME_MANAGER, "EntityManager");
44
45  allOutBuffer.length = 0;
46
47  allOutBuffer.maxLength = 10*1024;
48
49  allOutBuffer.buffer = new byte[10*1024];
50
51  newUniqueID = MAX_CONNECTIONS + 2;
52
53  hasRequestedWorld = false;
54}
55
56/*!
57 * Standard destructor
58 */
59NetworkGameManager::~NetworkGameManager()
60{
61  for ( int i = 0; i<outBuffer.size(); i++)
62  {
63    if ( outBuffer[i].buffer )
64      delete outBuffer[i].buffer;
65  }
66
67  if ( allOutBuffer.buffer )
68    delete allOutBuffer.buffer;
69}
70
71
72void NetworkGameManager::writeBytes(const byte* data, int length, int sender)
73{
74  int i = 0;
75  byte b;
76
77  while ( i<length )
78  {
79    b = data[i++];
80
81    PRINTF(0)("WriteBytes: b = %d\n", b);
82
83    if ( isServer() )
84    {
85      if ( b == REQUEST_CREATE )
86      {
87        if ( !handleRequestCreate( i, data, length, sender ) )
88          return;
89        continue;
90      }
91      if ( b == REQUEST_REMOVE )
92      {
93        if ( !handleRequestRemove( i, data, length, sender ) )
94          return;
95        continue;
96      }
97    }
98    else
99    {
100      if ( b == CREATE_ENTITY )
101      {
102        if ( !handleCreateEntity( i, data, length, sender ) )
103          return;
104        continue;
105      }
106      if ( b == REMOVE_ENTITY )
107      {
108        if ( !handleRemoveEntity( i, data, length, sender ) )
109          return;
110        continue;
111      }
112      if ( b == CREATE_ENTITY_LIST )
113      {
114        if ( !handleCreateEntityList( i, data, length, sender ) )
115          return;
116        continue;
117      }
118      if ( b == REMOVE_ENTITY_LIST )
119      {
120        if ( !handleRemoveEntityList( i, data, length, sender ) )
121          return;
122        continue;
123      }
124      if ( b == YOU_ARE_ENTITY )
125      {
126        if ( !handleYouAreEntity( i, data, length, sender ) )
127          return;
128        continue;
129      }
130    }
131
132    if ( b == REQUEST_SYNC )
133    {
134      if ( !handleRequestSync( i, data, length, sender ) )
135        return;
136      continue;
137    }
138
139    if ( b == REQUEST_ENTITY_LIST )
140    {
141      PRINTF(0)("sending THE list\n");
142      sendEntityList( sender );
143      continue;
144    }
145
146    //if we get her something with data is wrong
147    PRINTF(1)("Data is not in the right format! i=%d\n", i);
148    return;
149  }
150}
151
152int NetworkGameManager::readBytes(byte* data, int maxLength, int * reciever)
153{
154  if ( !isServer() && !hasRequestedWorld )
155  {
156    SYNCHELP_WRITE_BEGIN();
157    byte b = REQUEST_ENTITY_LIST;
158    SYNCHELP_WRITE_BYTE( b );
159    hasRequestedWorld = true;
160    PRINTF(0)("the world is enough! id=%\n", this->getUniqueID());
161    return SYNCHELP_WRITE_N;
162  }
163  for ( int i = 0; i<outBuffer.size(); i++ )
164  {
165    *reciever = i;
166    if ( outBuffer[i].length>0 )
167    {
168      int nbytes = outBuffer[i].length;
169      outBuffer[i].length = 0;
170
171      if ( nbytes > maxLength )
172      {
173        PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.maxLength\n", nbytes, maxLength);
174        return 0;
175      }
176
177      memcpy(data, outBuffer[i].buffer, nbytes);
178      return nbytes;
179    }
180  }
181
182  *reciever = 0;
183  int nbytes = allOutBuffer.length;
184  allOutBuffer.length = 0;
185
186  if ( nbytes <=0 )
187    return 0;
188
189  if ( nbytes > maxLength )
190  {
191    PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.length\n", nbytes, maxLength);
192    return 0;
193  }
194
195  memcpy( data, allOutBuffer.buffer, nbytes );
196  return nbytes;
197}
198
199void NetworkGameManager::writeDebug() const
200{
201}
202
203void NetworkGameManager::readDebug() const
204{
205}
206
207
208/*!
209 * Checks whether this is connected to a server or a client
210 * and afterwards creates the needed entity
211 * @param classID: The ID of the class of which an entity should be created
212 */
213void NetworkGameManager::createEntity( ClassID classID, int owner )
214{
215  if ( this->isServer() )
216  {
217    if ( newUniqueID < 0 )
218    {
219      PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");
220      return;
221    }
222
223    this->executeCreateEntity( classID, newUniqueID++, owner );
224  }
225  else
226  {
227    this->requestCreateEntity( classID );
228  }
229}
230
231
232/*!
233 * Checks whether this is connected to a server or a client
234 * and afterwards creates the needed entity
235 * @param classID: The ID of the class of which an entity should be created
236 */
237BaseObject* NetworkGameManager::createEntity( TiXmlElement* element)
238{
239  if ( this->isServer() )
240  {
241    if ( newUniqueID < 0 )
242    {
243      PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");
244      return NULL;
245    }
246    newUniqueID++;
247
248    BaseObject * b = Factory::fabricate( element );
249
250    if ( !b )
251    {
252      PRINTF(1)("Could not fabricate Object with classID %x\n", element->Value() );
253      return NULL;
254    }
255
256
257    if ( b->isA(CL_SYNCHRONIZEABLE) )
258    {
259      Synchronizeable * s = dynamic_cast<Synchronizeable*>(b);
260      s->setUniqueID( newUniqueID );
261      s->setOwner( 0 );
262      this->networkStream->connectSynchronizeable( *s );
263      return b;
264    }
265    else
266    {
267      PRINTF(1)("Class %s is not a synchronizeable!\n", b->getClassName() );
268      delete b;
269    }
270  }
271  else
272  {
273    PRINTF(1)("This node is not a server and cannot create id %x\n", element->Value());
274  }
275  return NULL;
276}
277
278
279/*!
280 * Checks whether this is connected to a server or a client
281 * and afterwards removes the specified entity
282 * @param uniqueID: The ID of the entity object which should be removed
283 */
284void NetworkGameManager::removeEntity(int uniqueID)
285{
286  if ( this->isServer() )
287  {
288    this->executeRemoveEntity( uniqueID );
289  }
290  else
291  {
292    this->requestRemoveEntity( uniqueID );
293  }
294}
295
296
297
298/*!
299 * Creates the needed entity on the server if possible
300 * @param classID: The ID of the class of which an entity should be created
301 */
302void NetworkGameManager::requestCreateEntity(ClassID classID)
303{
304  if ( !writeToClientBuffer( allOutBuffer, (byte)REQUEST_CREATE ) )
305    return;
306  if ( !writeToClientBuffer( allOutBuffer, (int)classID ) )
307    return;
308}
309
310/*!
311 * Removes the specified entity on the server
312 * @param uniqueID: The ID of the entity object which should be removed
313 */
314void NetworkGameManager::requestRemoveEntity(int uniqueID)
315{
316  if ( !writeToClientBuffer( allOutBuffer, (byte)REQUEST_REMOVE ) )
317    return;
318  if ( !writeToClientBuffer( allOutBuffer, uniqueID ) )
319    return;
320}
321
322/*!
323 * Creates the needed entity if possible
324 * This function is called if this is a server
325 * @param classID: The ID of the class of which an entity should be created
326 */
327void NetworkGameManager::executeCreateEntity(ClassID classID, int uniqueID, int owner)
328{
329  if ( !writeToClientBuffer( allOutBuffer, (byte)CREATE_ENTITY ) )
330    return;
331  if ( !writeToClientBuffer( allOutBuffer, (int)classID ) )
332    return;
333  if ( !writeToClientBuffer( allOutBuffer, uniqueID ) )
334    return;
335  if ( !writeToClientBuffer( allOutBuffer, owner ) )
336    return;
337
338  doCreateEntity( classID, uniqueID, owner );
339}
340
341/*!
342 * Removes the specified entity
343 * This function is called if this is a server
344 * @param uniqueID: The ID of the entity object which should be removed
345 */
346void NetworkGameManager::executeRemoveEntity(int uniqueID)
347{
348  if ( !writeToClientBuffer( allOutBuffer, (byte)REMOVE_ENTITY ) )
349    return;
350  if ( !writeToClientBuffer( allOutBuffer, uniqueID ) )
351    return;
352
353  doRemoveEntity(uniqueID);
354}
355
356/*!
357 * Checks whether it is possible to create an entity of a given class
358 * @return: true if the entity can be created, false otherwise
359 */
360bool NetworkGameManager::canCreateEntity(ClassID classID)
361{
362  return true;
363}
364
365/*!
366 * Sends the Entities to the new connected client
367 * @param userID: The ID of the user
368 */
369void NetworkGameManager::sendEntityList( int userID )
370{
371  if ( !isServer() )
372    return;
373
374  if ( userID >= outBuffer.size() )
375    resizeBufferVector( userID );
376
377  SynchronizeableList::const_iterator it, e;
378
379  it = this->networkStream->getSyncBegin();
380  e = this->networkStream->getSyncEnd();
381
382  if ( !writeToClientBuffer( outBuffer[userID], (byte)CREATE_ENTITY_LIST ) )
383    return;
384
385  // -2 because you must not send network_game_manager and handshake
386  if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount()-2 ) )
387    return;
388
389  //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 );
390
391  while ( it != e )
392  {
393    if ( (*it)->getLeafID() != CL_NETWORK_GAME_MANAGER && (*it)->getLeafID() != CL_HANDSHAKE )
394    {
395
396      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafID()) ) )
397        return;
398
399      //PRINTF(0)("SendEntityList: ClassID = %x\n", (*it)->getRealClassID());
400
401      if ( !writeToClientBuffer( outBuffer[userID], (*it)->getUniqueID() ) )
402        return;
403
404      if ( !writeToClientBuffer( outBuffer[userID], (*it)->getOwner() ) )
405        return;
406    }
407
408    it++;
409  }
410}
411
412/**
413 * Creates a buffer for user n
414 * @param n The ID of the user
415 */
416void NetworkGameManager::resizeBufferVector( int n )
417{
418  for ( int i = outBuffer.size(); i<=n; i++)
419  {
420    clientBuffer outBuf;
421
422    outBuf.length = 0;
423
424    outBuf.maxLength = 5*1024;
425
426    outBuf.buffer = new byte[5*1014];
427
428    outBuffer.push_back(outBuf);
429  }
430}
431
432/**
433 * Creates the entity on this host
434 * @param classID: ClassID of the entity to create
435 * @param uniqueID: Unique ID to assign to the synchronizeable
436 * @param owner: owner of this synchronizealbe
437 */
438void NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner )
439{
440  BaseObject * b = Factory::fabricate( classID );
441
442  if ( !b )
443  {
444    PRINTF(1)("Could not fabricate Object with classID %x\n", classID);
445    return;
446  }
447
448  if ( b->isA(CL_SYNCHRONIZEABLE) )
449  {
450    Synchronizeable * s = dynamic_cast<Synchronizeable*>(b);
451    s->setUniqueID( uniqueID );
452    s->setOwner( owner );
453    this->networkStream->connectSynchronizeable( *s );
454    if ( !isServer() )
455      s->setIsOutOfSync( true );
456    PRINTF(0)("Fabricated %s with id %d\n", s->getClassName(), s->getUniqueID());
457  }
458  else
459  {
460    PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)classID);
461    delete b;
462  }
463}
464
465/**
466 * Removes a entity on this host
467 * @param uniqueID: unique ID assigned with the entity to remove
468 */
469void NetworkGameManager::doRemoveEntity( int uniqueID )
470{
471  SynchronizeableList::const_iterator it,e;
472  it = this->networkStream->getSyncBegin();
473  e = this->networkStream->getSyncEnd();
474
475  while ( it != e )
476  {
477    if ( (*it)->getUniqueID() == uniqueID )
478    {
479      delete *it;
480      break;
481    }
482    it++;
483  }
484}
485
486/**
487 * Tell the synchronizeable that a user's synchronizeable is out of sync
488 * @param uniqueID: unique ID assigned with the entity which is out of sync
489 * @param userID: user ID who's synchronizeable is out of sync
490 */
491void NetworkGameManager::doRequestSync( int uniqueID, int userID )
492{
493  SynchronizeableList::const_iterator it,e;
494  it = this->networkStream->getSyncBegin();
495  e = this->networkStream->getSyncEnd();
496
497  while ( it != e )
498  {
499    if ( (*it)->getUniqueID() == uniqueID )
500    {
501      (*it)->requestSync( userID );
502      break;
503    }
504    it++;
505  }
506}
507
508/**
509 * Copies length bytes to the clientBuffer with error checking
510 * @param clientBuffer: the clientBuffer to write to
511 * @param data: buffer to the data
512 * @param length: length of data
513 * @return false on error true else
514 */
515bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length )
516{
517  if ( length > cb.maxLength-cb.length )
518  {
519    PRINTF(1)("No space left in clientBuffer\n");
520    return false;
521  }
522
523  memcpy( cb.buffer+cb.length, data, length );
524  return true;
525}
526
527/**
528 * Reads data from clientBuffer with error checking
529 * @param clientBuffer: the clientBuffer to read from
530 * @param data: pointer to the buffer
531 * @param length:
532 * @return
533 */
534bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length )
535{
536  if ( cb.length < length )
537  {
538    PRINTF(0)("There is not enough data in clientBuffer\n");
539    return 0;
540  }
541
542  memcpy( data, cb.buffer+cb.length-length, length );
543  return true;
544}
545
546/**
547 * Tells this client that he has to control this entity
548 * @param uniqueID: the entity's uniqeID
549 */
550void NetworkGameManager::doYouAre( int uniqueID )
551{
552  //TODO: what has to be done
553}
554
555/**
556 * Tells a remote client that he has to control this entity
557 * @param uniqueID: the entity's uniqeID
558 * @param userID: the users ID
559 */
560void NetworkGameManager::sendYouAre( int uniqueID, int userID )
561{
562  if ( !isServer() )
563    return;
564
565  if ( userID != 0 )
566  {
567    if ( !writeToClientBuffer( outBuffer[userID], (byte)YOU_ARE_ENTITY ) )
568      return;
569
570    if ( !writeToClientBuffer( outBuffer[userID], uniqueID ) )
571      return;
572  }
573  else
574  {
575    doYouAre(uniqueID);
576  }
577}
578
579bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender )
580{
581  if ( INTSIZE > length-i )
582  {
583    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
584    return false;
585  }
586  int classID;
587  i += Converter::byteArrayToInt( &data[i], &classID );
588
589  createEntity( (ClassID)classID );
590
591  return true;
592}
593
594bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender )
595{
596  if ( INTSIZE > length-i )
597  {
598    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
599    return false;
600  }
601  int uniqueID;
602  i += Converter::byteArrayToInt( &data[i], &uniqueID );
603
604  removeEntity( uniqueID );
605
606  return true;
607}
608
609bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender )
610{
611  if ( INTSIZE > length-i )
612  {
613    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
614    return false;
615  }
616  int classID;
617  i += Converter::byteArrayToInt( &data[i], &classID );
618
619  if ( INTSIZE > length-i )
620  {
621    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
622    return false;
623  }
624  int uniqueID;
625  i += Converter::byteArrayToInt( &data[i], &uniqueID );
626
627  if ( INTSIZE > length-i )
628  {
629    PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
630    return false;
631  }
632  int owner;
633  i += Converter::byteArrayToInt( &data[i], &owner );
634
635  doCreateEntity( (ClassID)classID, uniqueID, owner );
636
637  return true;
638}
639
640bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender )
641{
642  if ( INTSIZE > length-i )
643  {
644    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
645    return false;
646  }
647  int uniqueID;
648  i += Converter::byteArrayToInt( &data[i], &uniqueID );
649
650  doRemoveEntity( uniqueID );
651
652  return true;
653}
654
655bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender )
656{
657  if ( INTSIZE > length-i )
658  {
659    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
660    return false;
661  }
662
663  PRINTF(0)("HandleCreateEntityList:  data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);
664
665  int n;
666  i += Converter::byteArrayToInt( &data[i], &n );
667
668
669  PRINTF(0)("HandleCreateEntityList: n = %d\n", n);
670
671  int classID, uniqueID, owner;
672
673  for ( int j = 0; j<n; j++ )
674  {
675
676    if ( INTSIZE > length-i )
677    {
678      PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
679      return false;
680    }
681    i += Converter::byteArrayToInt( &data[i], &classID );
682
683    if ( INTSIZE > length-i )
684    {
685      PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
686      return false;
687    }
688    i += Converter::byteArrayToInt( &data[i], &uniqueID );
689
690    if ( INTSIZE > length-i )
691    {
692      PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
693      return false;
694    }
695    i += Converter::byteArrayToInt( &data[i], &owner );
696
697    doCreateEntity( (ClassID)classID, uniqueID, owner );
698
699  }
700  return true;
701}
702
703bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender )
704{
705  if ( INTSIZE > length-i )
706  {
707    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
708    return false;
709  }
710  int n;
711  i += Converter::byteArrayToInt( &data[i], &n );
712
713  int uniqueID;
714
715  for ( int j = 0; j<n; j++ )
716  {
717
718    if ( INTSIZE > length-i )
719    {
720      PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
721      return false;
722    }
723    i += Converter::byteArrayToInt( &data[i], &uniqueID );
724
725    doRemoveEntity( uniqueID );
726  }
727
728  return true;
729}
730
731bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender )
732{
733  if ( INTSIZE > length-i )
734  {
735    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
736    return false;
737  }
738
739  int uniqueID;
740  i += Converter::byteArrayToInt( &data[i], &uniqueID );
741
742  doYouAre( uniqueID );
743
744  return true;
745}
746
747bool NetworkGameManager::handleRequestSync( int & i, const byte * data, int length, int sender )
748{
749  if ( INTSIZE > length-i )
750  {
751    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
752    return false;
753  }
754  int uniqueID;
755  i += Converter::byteArrayToInt( &data[i], &uniqueID );
756
757  doRequestSync( uniqueID, sender );
758
759  return true;
760}
761
762bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b )
763{
764  if ( cb.maxLength-cb.length < 1 )
765  {
766    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 byte\n");
767    return false;
768  }
769
770  cb.buffer[cb.length++] = b;
771
772  return true;
773}
774
775bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, int i )
776{
777  int n = Converter::intToByteArray( i, cb.buffer+cb.length, cb.maxLength-cb.length );
778  cb.length += n;
779
780  if ( n <= 0 )
781  {
782    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 int\n");
783    return false;
784  }
785
786  return true;
787}
788
789void NetworkGameManager::sync( int uniqueID, int owner )
790{
791  if ( owner==this->getHostID() )
792    return;
793
794  if ( !isServer() )
795    executeRequestSync( uniqueID, 0 );
796  else
797    executeRequestSync( uniqueID, owner );
798}
799
800void NetworkGameManager::executeRequestSync( int uniqueID, int user )
801{
802  if ( user >= outBuffer.size() )
803    resizeBufferVector( user );
804
805  if ( !writeToClientBuffer( outBuffer[user], (byte)REQUEST_SYNC ) )
806    return;
807  if ( !writeToClientBuffer( outBuffer[user], uniqueID ) )
808    return;
809}
810
Note: See TracBrowser for help on using the repository browser.