Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network_socket: can now send more than 255 bytes
network_game_manager: fixed some bugs

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