Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6139 in orxonox.OLD for trunk/src/lib


Ignore:
Timestamp:
Dec 16, 2005, 6:45:32 PM (18 years ago)
Author:
patrick
Message:

trunk: merged branche network with trunk using command: svn merge -r5999:HEAD, conflicts resolved in favor of the trunk bla

Location:
trunk/src/lib
Files:
16 edited
6 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/importer/texture.cc

    r5863 r6139  
    1919
    2020#include "debug.h"
     21#include "compiler.h"
     22#include <math.h>
    2123
    2224// INCLUDING SDL_Image
     
    3739  this->texture = 0;
    3840  this->image = NULL;
     41  this->priority = 0.5;
    3942
    4043  if (imageName != NULL)
     
    177180 * @returns a !!new!! Surface, that is loadable by openGL.
    178181 */
    179 SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha)
     182SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) const
    180183{
    181184  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
     
    237240 * @returns The ID of the texture.
    238241 */
    239 GLuint Texture::loadTexToGL (const SDL_Surface* surface)
     242GLuint Texture::loadTexToGL (const SDL_Surface* surface) const
    240243{
    241244//   if (this->texture != 0 && glIsTexture(this->texture))
     
    243246//   this->texture = 0;
    244247
    245   GLuint texture;
     248  int      errorCode = 0;           //!< the error code for the texture loading functions
     249  GLuint   texture;                 //!< the OpenGL texture handle
     250  int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
     251  int      mipmapWidth = 0;         //!< the width of the mipmap
     252  int      mipmapHight = 0;         //!< the height of the mipmap
     253
    246254
    247255  if (surface == NULL)
     
    251259  glGenTextures(1, &texture);
    252260  glBindTexture(GL_TEXTURE_2D, texture);
    253   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    254   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    255   // build the Texture
     261
     262  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, /*GL_LINEAR*/ GL_LINEAR_MIPMAP_LINEAR);
     263  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, /*GL_LINEAR*/ GL_LINEAR);
     264  /* control the mipmap levels */
     265  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, -100);
     266  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
     267  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
     268
     269  /* build the Texture  OpenGL V >= 1.1 */
    256270  glTexImage2D(GL_TEXTURE_2D,
    257271               0,
     
    262276               GL_UNSIGNED_BYTE,
    263277               surface->pixels);
    264   // build the MipMaps
    265   gluBuild2DMipmaps(GL_TEXTURE_2D,
    266                     GL_RGBA,
    267                     surface->w,
    268                     surface->h,
    269                     GL_RGBA,
    270                     GL_UNSIGNED_BYTE,
    271                     surface->pixels);
     278
     279  // build the MipMaps automaticaly
     280  errorCode = gluBuild2DMipmaps(GL_TEXTURE_2D,
     281                                GL_RGBA,
     282                                surface->w,
     283                                surface->h,
     284                                GL_RGBA,
     285                                GL_UNSIGNED_BYTE,
     286                                surface->pixels
     287                               );
     288  if(unlikely(errorCode != 0))
     289    PRINTF(1)("Error while loading texture, gluBuild2DMipmaps returned %i\n", errorCode);
     290
     291#define max(a,b) (a>b)?a:b
     292  mipmapLevel = (int)(log2f(max(surface->w, surface->h)));
     293#undef max
     294  PRINTF(5)("Built the texture mipmpaps, got: %i levels of detail\n", mipmapLevel);
     295
     296  /* now actualy load the mipmaps into the graphics memory */
     297  glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 1, GL_TEXTURE_WIDTH, &mipmapWidth);
     298  glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 1, GL_TEXTURE_WIDTH, &mipmapHight);
     299  PRINTF(5)(" Mipmap at level %i has a %ix%i resolution\n", 1, mipmapWidth, mipmapHight);
     300  glTexImage2D(GL_PROXY_TEXTURE_2D,
     301               1,
     302               GL_RGBA,
     303               mipmapWidth, mipmapHight,
     304               0,
     305               GL_RGBA,
     306               GL_UNSIGNED_BYTE,
     307               NULL
     308               );
     309
    272310  glBindTexture(GL_TEXTURE_2D, 0);
    273311  return texture;
    274312}
     313
  • trunk/src/lib/graphics/importer/texture.h

    r5859 r6139  
    3939
    4040      // Utility functionality:
    41       static SDL_Surface* prepareSurface(SDL_Surface* input, bool& hasAlpha);
    42       static GLuint loadTexToGL (const SDL_Surface* surface);
     41      SDL_Surface* prepareSurface(SDL_Surface* input, bool& hasAlpha) const;
     42      GLuint loadTexToGL (const SDL_Surface* surface) const;
    4343
    4444    protected:
     
    5353      bool             bAlpha;             //!< if the texture has an alpha channel.
    5454      SDL_Surface*     image;              //!< The SDL_Surfce that stores the Texture on it.
     55      GLclampf         priority;           //!< the priority of the current texture (used for garphics cards with limited mem)
    5556
    5657      static bool      texturesEnabled;    //!< If the Textures are enabled.
  • trunk/src/lib/network/Makefile.am

    r5996 r6139  
    1111                      data_stream.cc \
    1212                      network_protocol.cc \
    13                       server_socket.cc
     13                      server_socket.cc \
     14                      handshake.cc \
     15                      network_game_manager.cc \
     16                      converter.cc
     17
    1418
    1519
     
    2226                 data_stream.h \
    2327                 network_protocol.h \
    24                  server_socket.h
     28                 server_socket.h \
     29                 handshake.h \
     30                 network_game_manager.h \
     31                 converter.h
    2532
     33
  • trunk/src/lib/network/network_manager.cc

    r5997 r6139  
    5050  this->tmpStream = NULL;
    5151  this->hostID = -1;
     52  this->bGameServer = false;
    5253
    5354  PRINTF(0)("NetworkManager created\n");
     
    9596  }
    9697
    97   this->tmpStream = new NetworkStream(ipAddress, NET_CLIENT);
     98  this->tmpStream = new NetworkStream(ipAddress);
    9899  return 1;
    99100}
     
    106107int NetworkManager::createServer(unsigned int port)
    107108{
    108   this->tmpStream = new NetworkStream(port, NET_SERVER);
     109  this->tmpStream = new NetworkStream(port);
     110  this->bGameServer = true;
    109111  SDL_Delay(20);
    110112  return 1;
     
    120122{
    121123  /* creating a new network stream, it will register itself automaticaly to the class list */
    122   this->tmpStream = new NetworkStream(address, sync, NET_CLIENT);
     124  this->tmpStream = new NetworkStream(address);
     125  this->tmpStream->connectSynchronizeable(sync);
    123126}
    124127
     
    132135  PRINTF(0)("Create a new server socket\n");
    133136  /* creating a new network stream, it will register itself automaticaly to the class list */
    134   this->tmpStream = new NetworkStream(port, sync, NET_SERVER);
     137  this->tmpStream = new NetworkStream(port);
     138  this->tmpStream->connectSynchronizeable(sync);
     139  this->bGameServer = true;
    135140}
    136141
  • trunk/src/lib/network/network_manager.h

    r5997 r6139  
    4545    void setHostID(int id);
    4646    /** Returns the hostID @return The hostID of the object */
    47     inline int getHostID() { return this->hostID; };
     47    inline int getHostID() { return this->hostID; }
     48    inline bool isGameServer() { return this->bGameServer; }
    4849
    49   private:
     50
    5051    void connectSynchronizeable(Synchronizeable& sync);
    5152    void synchronize();
    5253
     54  private:
    5355    NetworkManager();
    5456
    5557
    5658  private:
    57     const std::list<BaseObject*>*    netStreamList;            // list with refs to all network streams
    58     const std::list<BaseObject*>*    syncList;                 // list of synchronizeables
    59     static NetworkManager* singletonRef;           //!< Pointer to the only instance of this Class
    60     NetworkStream*         tmpStream;              //!< FIXME: this is only for testing purposes
    61     int hostID;                                    //!< The Host-ID of the Manager
     59    const std::list<BaseObject*>*    netStreamList;           // list with refs to all network streams
     60    const std::list<BaseObject*>*    syncList;                // list of synchronizeables
     61    static NetworkManager*           singletonRef;            //!< Pointer to the only instance of this Class
     62    NetworkStream*                   tmpStream;               //!< FIXME: this is only for testing purposes
     63    int                              hostID;                  //!< The Host-ID of the Manager
     64    bool                             bGameServer;             //!< true if it is a server
    6265
    6366};
  • trunk/src/lib/network/network_protocol.cc

    r5822 r6139  
    5757    * @arg bufferLength: the length of the internal buffer
    5858    * @arg source: reference to the source Synchronizeable object
    59     * @arg remoteID: id number of the remote Synchronizeable object
    6059    * @return: the new data length with header (the header data is included into byte* data)
    6160    *          -1 if there isn't enough space in the buffer for the header
    6261*/
    63 int NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source, unsigned int remoteID)
     62int NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source)
    6463{
    65   printf("NetworkProtocol: create header length = %i, bufferLength = %i\n", length, bufferLength);
     64  PRINTF(5)("create header length = %i, bufferLength = %i\n", length, bufferLength);
    6665  //If there isn't enough space for the header return -1
    6766  if (length + this->headerLength > bufferLength)
    6867    return -1;
    6968
    70 //   for(int i = 0; i < length; i++)
    71 //     printf("send byte[%i]=%u\n", i, data[i]);
    7269
    73 
    74   //Create space for the header
     70  // FIXME: without move Create space for the header
    7571  for( int i = length - 1; i >= 0; i--)
    7672    data[i + this->headerLength] = data[i];
    7773
    7874  //Now create the header
    79   /* protocol identifier */
    80   data[0] = 255;
    81   /* version number */
    82   data[1] = 0;
    8375  /* sender ID: FIXME: there will be a better ID (for example unique:D)*/
    84   data[2] = (byte)source.getClassID();
    85   /* receiver ID */
    86   data[3] = remoteID;
     76  data[0] = (byte)(source.getUniqueID());
    8777  /* data length*/
    88   data[4] = length;
     78  data[1] = length;
    8979
    9080
     
    10090Header NetworkProtocol::extractHeader(byte* data, int length)
    10191{
    102   PRINTF(0)("extract Header\n");
     92  PRINTF(5)("extract Header\n");
    10393  //Test if received data can contain a header
    10494  if (length < headerLength)
    10595  {
    106     PRINTF(0)("Received data is to short; it can't contain a header!");
     96    PRINTF(1)("Received data is to short; it can't contain a header!\n");
    10797    Header h;
    108     h.protocol = 0;
     98    h.length = 0;
    10999    return h;
    110100  }
     
    114104  //&h = data;
    115105
    116   h.protocol = data[0];
    117   /* version number */
    118   h.version = data[1];
    119   /* sender ID: FIXME: there will be a better ID (for example unique:D)*/
    120   h.senderID = data[2];
    121   /* receiver ID */
    122   h.receiverID = data[3];
     106  /* unique ID */
     107  h.synchronizeableID = data[0];
    123108  /* data length*/
    124   h.length = data[4];
     109  h.length = data[1];
    125110
    126 
    127 //   for(int i = 0; i < length; i++)
    128 //     printf("recS byte[%i]=%u\n", i, data[i]);
    129 
    130   //Remove header
    131 //   for (int i = headerLength; i < length; i++)
    132 //     data[i - headerLength] = data[i];
    133111
    134112  return h;
  • trunk/src/lib/network/network_protocol.h

    r5822 r6139  
    66#define _NETWORK_PROTOCOL_H
    77
     8/* include base_object.h since all classes are derived from this one */
    89#include "base_object.h"
    910
     
    1112#include "netdefs.h"
    1213
    13 #define HEADER_LENGTH 1
    14 
    1514typedef struct Header
    1615{
    17   byte protocol;
    18   byte version;
    19   byte senderID;
    20   byte receiverID;
     16  byte synchronizeableID;
    2117  byte length;
    2218};
     
    3329    ~NetworkProtocol();
    3430
    35     int createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source, unsigned int remoteID);
     31    int createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source);
    3632    Header extractHeader(byte* data, int length);
    3733
  • trunk/src/lib/network/network_socket.cc

    r5996 r6139  
    2828#include "debug.h"
    2929
    30 
    3130/**
    3231 * Default constructor
     
    5251  this->tcpSocket = sock;
    5352
    54   SDL_CreateThread(thread_read, (void*)this);
    55   SDL_CreateThread(thread_write, (void*)this);
     53  readThread = SDL_CreateThread(thread_read, (void*)this);
     54  writeThread = SDL_CreateThread(thread_write, (void*)this);
    5655}
    5756
     
    6564  outgoingBufferLength = 0;
    6665
     66  readThread = NULL;
     67  writeThread = NULL;
     68
     69
     70  thread_write_running = false;
     71  thread_read_running = false;
     72
    6773  incomingBufferMutex = SDL_CreateMutex();
    6874  outgoingBufferMutex = SDL_CreateMutex();
     75
     76
    6977  socketMutex = SDL_CreateMutex();
    7078  terminateThread = false;
     
    8997 * Default destructor
    9098 */
    91 NetworkSocket::~ NetworkSocket( )
    92 {
     99NetworkSocket::~NetworkSocket( )
     100{
     101  this->terminateThread = true;
    93102  /* Quit SDL_net */
    94103  // NOTE: what if other instances of NetworkSocket running?
     
    99108  SDL_DestroyMutex(outgoingBufferMutex);
    100109  SDL_DestroyMutex(socketMutex);
     110  SDL_DestroyMutex(threadTerminationMutex);
    101111}
    102112
     
    122132  }
    123133
    124   SDL_CreateThread(thread_read, (void*)this);
    125   SDL_CreateThread(thread_write, (void*)this);
     134  readThread = SDL_CreateThread(thread_read, (void*)this);
     135  writeThread = SDL_CreateThread(thread_write, (void*)this);
    126136}
    127137
     
    152162int NetworkSocket::writeBytes(byte * data, int length)
    153163{
    154   PRINTF(0)("NetworkSocket::writeBytes()\n");
     164  PRINTF(5)("NetworkSocket::writeBytes()\n");
    155165#ifdef _USE_OUTGOING_BUFFER
    156166
     
    200210int NetworkSocket::readBytes(byte * data, int length)
    201211{
    202   PRINTF(0)("NetworkSocket::readBytes()\n");
     212  PRINTF(5)("NetworkSocket::readBytes()\n");
    203213  if (data==NULL)
    204214    return 0;
     
    256266  NetworkSocket * self = (NetworkSocket*)data;
    257267
     268  self->thread_read_running = true;
     269
    258270  while (!self->terminateThread)
    259271  {
     
    263275
    264276    //if buffer is full
    265     if (nbytestoread<=0)
     277    if (nbytestoread<=0 || !self->tcpSocket)
    266278    {
    267279      SDL_Delay(_MSECONDS_SLEEP_FULL_BUFFER);
     
    275287    if (nbytesread<=0)
    276288    {
    277       printf("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
     289      if (nbytesread<0)
     290        printf("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
    278291
    279292      SDL_mutexP(self->socketMutex);
     
    284297      SDL_mutexV(self->socketMutex);
    285298      SDL_mutexV(self->incomingBufferMutex);
    286       return -1;
     299      continue;
    287300    }
    288301
     
    295308  }
    296309
     310  SDL_mutexP(self->threadTerminationMutex);
     311  self->thread_read_running = false;
     312
     313  if ( !self->thread_write_running )
     314  {
     315    //delete self;
     316    SDL_mutexV(self->threadTerminationMutex);
     317  }
     318  else
     319  {
     320    SDL_mutexV(self->threadTerminationMutex);
     321  }
     322
     323
     324  PRINTF(0)("QUIT READ THREAD\n");
    297325  return 0;
    298326}
     
    305333  NetworkSocket * self = (NetworkSocket*)data;
    306334
     335  self->thread_write_running = true;
     336
    307337  while (!self->terminateThread)
    308338  {
     
    314344
    315345    //if buffer is full
    316     if (nbytestowrite<=0)
     346    if (nbytestowrite<=0 || !self->tcpSocket)
    317347    {
    318348      SDL_Delay(_MSECONDS_SLEEP_EMPTY_BUFFER);
     
    342372
    343373      SDL_mutexV(self->socketMutex);
    344       return -1;
     374      continue;
    345375    }
    346376
    347377  }
    348378
    349   printf("QUIT WRITE THREAD\n");
     379  SDL_mutexP(self->threadTerminationMutex);
     380  self->thread_write_running = false;
     381
     382  if ( !self->thread_read_running )
     383  {
     384    //delete self;
     385    SDL_mutexV(self->threadTerminationMutex);
     386  }
     387  else
     388  {
     389    SDL_mutexV(self->threadTerminationMutex);
     390  }
     391
     392
     393  PRINTF(0)("QUIT WRITE THREAD\n");
    350394  return 0;
    351395}
     
    353397bool NetworkSocket::writePacket( byte * data, int length )
    354398{
    355   PRINTF(0)("NetworkSocket::writePacket()\n");
     399  PRINTF(5)("NetworkSocket::writePacket()\n");
    356400  if (length>255)
    357401  {
     
    368412int NetworkSocket::readPacket( byte * data, int maxLength )
    369413{
    370   PRINTF(0)("NetworkSocket::readPacket()\n");
     414  PRINTF(5)("NetworkSocket::readPacket()\n");
    371415  if (incomingBufferLength<1)
    372416  {
  • trunk/src/lib/network/network_socket.h

    r5996 r6139  
    6363  bool terminateThread;
    6464
     65  SDL_mutex* threadTerminationMutex;
    6566  static int thread_read(void * data);
     67  bool thread_read_running;
     68  bool thread_write_running;
     69
     70  SDL_Thread*            readThread;
     71  SDL_Thread*            writeThread;
     72
    6673#ifdef _USE_OUTGOING_BUFFER
    6774  static int thread_write(void * data);
     
    7481  void init();
    7582
     83  //dont make this public use destroy() instead
     84  ~NetworkSocket();
     85
    7686public:
    7787
     
    7989  NetworkSocket(IPaddress ip);
    8090  NetworkSocket(TCPsocket sock);
    81   ~NetworkSocket();
     91  void destroy() { terminateThread = true; };
     92
    8293
    8394  void connectToServer(IPaddress ip);
     
    8798  int readPacket(byte * data, int maxLength);
    8899
     100  inline bool isOk() { return tcpSocket!=NULL; }
     101
    89102};
    90103
  • trunk/src/lib/network/network_stream.cc

    r5996 r6139  
    2626#include "connection_monitor.h"
    2727#include "synchronizeable.h"
     28#include "network_manager.h"
    2829#include "list.h"
    2930#include "debug.h"
     31#include "class_list.h"
    3032
    3133/* include your own header */
     
    4547  /* initialize the references */
    4648  this->type = NET_CLIENT;
    47   this->networkSocket = new NetworkSocket();
    4849  this->networkProtocol = new NetworkProtocol();
    49   this->synchronizeables = NULL;
    5050  this->connectionMonitor = new ConnectionMonitor();
    5151}
    5252
    53 NetworkStream::NetworkStream(IPaddress& address, NodeType type)
    54 {
    55   this->type = type;
     53NetworkStream::NetworkStream(IPaddress& address)
     54{
     55  this->type = NET_CLIENT;
    5656  this->init();
    57   this->networkSocket = new NetworkSocket(address);
     57  this->networkSockets.push_back(new NetworkSocket(address));
    5858  this->networkProtocol = new NetworkProtocol();
    59   this->synchronizeables = NULL;
    6059  this->connectionMonitor = new ConnectionMonitor();
    61 }
    62 
    63 
    64 NetworkStream::NetworkStream(unsigned int port, NodeType type)
    65 {
    66   this->type = type;
     60
     61  Handshake* hs = new Handshake(false);
     62  hs->setUniqueID( 0 );
     63  this->handshakes.push_back(hs);
     64  this->connectSynchronizeable(*hs);
     65  PRINTF(0)("NetworkStream: %s\n", hs->getName());
     66}
     67
     68
     69NetworkStream::NetworkStream(unsigned int port)
     70{
     71  this->type = NET_SERVER;
    6772  this->init();
    68   this->networkSocket = new NetworkSocket();
    69 //  this->networkSocket->listen(port);
     73  this->serverSocket = new ServerSocket(port);
    7074  this->networkProtocol = new NetworkProtocol();
    71   this->synchronizeables = NULL;
    7275  this->connectionMonitor = new ConnectionMonitor();
    73 }
    74 
    75 
    76 NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type)
    77     : DataStream()
    78 {
    79   this->type = type;
    80   this->init();
    81   this->networkSocket = new NetworkSocket(address);
    82   this->networkProtocol = new NetworkProtocol();
    83   this->synchronizeables = &sync;
    84   this->connectionMonitor = new ConnectionMonitor();
     76  this->networkSockets.push_back( NULL );
     77  this->handshakes.push_back( NULL );
    8578  this->bActive = true;
    86 }
    87 
    88 
    89 NetworkStream::NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type)
    90     : DataStream()
    91 {
    92   this->type = type;
    93   this->init();
    94   this->networkSocket = new NetworkSocket();
    95 //  this->networkSocket->listen(port);
    96   this->networkProtocol = new NetworkProtocol();
    97   this->synchronizeables = &sync;
    98   this->connectionMonitor = new ConnectionMonitor();
    99   this->bActive = true;
     79
     80  this->setMaxConnections( 10 );
    10081}
    10182
     
    10586  /* set the class id for the base object */
    10687  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
    107   this->state = NET_REC_HEADER;
    10888  this->bActive = false;
     89  this->serverSocket = NULL;
     90  myHostId = 0;
    10991}
    11092
     
    11294NetworkStream::~NetworkStream()
    11395{
    114 
    115   networkSocket->disconnectServer();
    116 
    117   if( this->networkSocket)
    118     delete networkSocket;
     96  if ( this->serverSocket )
     97  {
     98    serverSocket->close();
     99    delete serverSocket;
     100  }
     101
     102  for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++)
     103  {
     104    if ( *i )
     105    {
     106      (*i)->disconnectServer();
     107      (*i)->destroy();
     108    }
     109  }
     110
     111  for (HandshakeVector::iterator i = handshakes.begin(); i!=handshakes.end(); i++)
     112  {
     113    if ( *i )
     114    {
     115      delete (*i);
     116    }
     117  }
    119118
    120119  delete connectionMonitor;
     
    125124void NetworkStream::connectSynchronizeable(Synchronizeable& sync)
    126125{
    127   this->synchronizeables = &sync;
    128   if( this->networkSocket != NULL)
     126  this->synchronizeables.push_back(&sync);
     127  sync.setNetworkStream( this );
     128
     129  if( this->networkSockets.size()>0 )
    129130    this->bActive = true;
    130131}
    131132
     133void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync)
     134{
     135  this->synchronizeables.remove(&sync);
     136
     137  if( this->networkSockets.size()<=0 )
     138    this->bActive = false;
     139}
     140
    132141
    133142void NetworkStream::processData()
    134143{
    135   int dataLength = 0;
     144  if ( this->type == NET_SERVER )
     145    this->updateConnectionList();
     146  else
     147  {
     148    if ( networkSockets[0] && !networkSockets[0]->isOk() )
     149    {
     150      PRINTF(1)("lost connection to server\n");
     151
     152      //delete networkSockets[i];
     153      networkSockets[0]->disconnectServer();
     154      networkSockets[0]->destroy();
     155      networkSockets[0] = NULL;
     156      //TODO: delete handshake from synchronizeable list so i can delete it
     157      if ( handshakes[0] )
     158        delete handshakes[0];
     159      handshakes[0] = NULL;
     160    }
     161  }
     162
     163  for (int i = 0; i<handshakes.size(); i++)
     164  {
     165    if ( handshakes[i] )
     166    {
     167      if ( handshakes[i]->completed() )
     168      {
     169        if ( handshakes[i]->ok() )
     170        {
     171          if ( type != NET_SERVER )
     172          {
     173            NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() );
     174            myHostId = NetworkManager::getInstance()->getHostID();
     175          }
     176          PRINT(0)("handshake finished\n");
     177          delete handshakes[i];
     178          handshakes[i] = NULL;
     179          //TODO: replace handshake by entitymanager
     180        }
     181        else
     182        {
     183          PRINT(1)("handshake failed!\n");
     184          networkSockets[i]->disconnectServer();
     185          delete handshakes[i];
     186          handshakes[i] = NULL;
     187          //TODO: handle error
     188        }
     189      }
     190    }
     191  }
     192
    136193
    137194  /* DOWNSTREAM */
    138   printf("\nSynchronizeable: %s\n", this->synchronizeables->getName());
    139   PRINT(0)("============= DOWNSTREAM:===============\n");
    140   /* first of all read the synchronizeable's data: */
    141   if( this->isServer())
    142     dataLength = this->synchronizeables->readBytes((byte*)downBuffer);
    143 
    144   if( dataLength > 0)
    145   {
    146     /* send the received data to connectionMonitor */
    147     this->connectionMonitor->processPacket((byte*)downBuffer, dataLength);
    148 
    149     dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,
    150                  *(this->synchronizeables), 12/* some random number (no real id)*/);
    151 
    152     /* pass the data to the network socket */
    153  //   dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
    154     /* check if there was an error */
    155     if( dataLength == -1)
    156     {
    157       PRINTF(0)("Error in writing data to the NetworkSocket\n");
    158     }
    159   }
    160 
     195
     196  int dataLength;
     197  int reciever;
     198  Header header;
     199  for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
     200  {
     201    //TODO: remove items from synchronizeables if they dont exist
     202    if ( (*it)!=NULL && (*it)->getOwner() == myHostId )
     203    {
     204      do {
     205        reciever = 0;
     206        dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, &reciever);
     207
     208
     209        if ( dataLength<=0 ){
     210          reciever = 0;
     211          continue;
     212        }
     213
     214        dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it)));
     215
     216        //FIXME: this is a hack, find a better way
     217        Header* header = (Header*)downBuffer;
     218        if ( header->synchronizeableID<100 )
     219          header->synchronizeableID = 0;
     220
     221        if ( dataLength<=0 )
     222          continue;
     223
     224        if ( reciever!=0 )
     225        {
     226          if ( networkSockets[reciever] != NULL )
     227          {
     228            PRINTF(5)("write %d bytes to socket %d\n", dataLength, reciever);
     229            networkSockets[reciever]->writePacket(downBuffer, dataLength);
     230          }
     231          else
     232          {
     233            PRINTF(1)("networkSockets[reciever] == NULL\n");
     234          }
     235        }
     236        else
     237        {
     238          for ( int i = 0; i<networkSockets.size(); i++)
     239          {
     240            if ( networkSockets[i] != NULL )
     241            {
     242              PRINTF(5)("write %d bytes to socket %d\n", dataLength, reciever);
     243              networkSockets[i]->writePacket(downBuffer, dataLength);
     244            }
     245          }
     246        }
     247
     248      } while( reciever!=0 );
     249    }
     250    else
     251    {
     252      PRINTF(0)("synchronizeables == NULL");
     253    }
     254  }
    161255
    162256  /* UPSTREAM */
    163   dataLength = 0;
    164   PRINT(0)("============== UPSTREAM:================\n");
    165   /* first of all read the next Orxonox Network Header */
    166 
    167   if( this->state == NET_REC_HEADER)
    168   {
    169 //    dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
    170     if( dataLength == sizeof(Header))
    171     {
    172       this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength);
    173       printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n",
    174              this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID,
    175              this->packetHeader.receiverID, this->packetHeader.length);
    176       /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/
    177 
    178       this->state = NET_REC_DATA;
    179     }
    180   }
    181   if( this->state == NET_REC_DATA)
    182   {
    183     /* now read the data */
    184 //    dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
    185     /* check if the data is available and process it if so */
    186     if( dataLength == this->packetHeader.length)
    187     {
    188       printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength);
    189       /* send the received data to connectionMonitor */
    190       this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length);
    191       /* now pass the data to the sync object */
    192       if( !this->isServer())
    193         this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length);
    194 
    195       this->state = NET_REC_HEADER;
    196     }
    197   }
    198 
    199 
    200 }
     257
     258  for ( int i = 0; i<networkSockets.size(); i++)
     259  {
     260    if ( networkSockets[i] )
     261    {
     262      do {
     263        dataLength = networkSockets[i]->readPacket(upBuffer, DATA_STREAM_BUFFER_SIZE);
     264
     265        if ( dataLength<=0 )
     266          continue;
     267
     268        PRINTF(5)("read %d bytes from socket\n", dataLength);
     269        header = networkProtocol->extractHeader(upBuffer, dataLength);
     270        dataLength -= sizeof(header);
     271
     272        if ( dataLength != header.length )
     273        {
     274          PRINTF(1)("packetsize in header and real packetsize do not match! %d:%d\n", dataLength, header.length);
     275          continue;
     276        }
     277
     278        if ( header.synchronizeableID == 0 )
     279        {
     280          header.synchronizeableID = i;
     281        }
     282
     283        for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
     284        {
     285          if ( *it && (*it)->getUniqueID()==header.synchronizeableID )
     286            (*it)->writeBytes(upBuffer+sizeof(header), dataLength);
     287        }
     288
     289      } while ( dataLength>0 );
     290    }
     291  }
     292}
     293
     294void NetworkStream::updateConnectionList( )
     295{
     296  //check for new connections
     297
     298  NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket();
     299
     300  if ( tempNetworkSocket )
     301  {
     302    int clientId;
     303    if ( freeSocketSlots.size() >0 )
     304    {
     305      clientId = freeSocketSlots.back();
     306      freeSocketSlots.pop_back();
     307      networkSockets[clientId] = tempNetworkSocket;
     308      handshakes[clientId] = new Handshake(true, clientId);
     309      handshakes[clientId]->setUniqueID(clientId);
     310    } else
     311    {
     312      clientId = networkSockets.size();
     313      networkSockets.push_back(tempNetworkSocket);
     314      Handshake* tHs = new Handshake(true, clientId);
     315      tHs->setUniqueID(clientId);
     316      handshakes.push_back(tHs);
     317    }
     318
     319    if ( clientId > this->maxConnections )
     320    {
     321      handshakes[clientId]->doReject();
     322      PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId);
     323    }
     324    else
     325
     326    PRINTF(0)("New Client: %d\n", clientId);
     327
     328    this->connectSynchronizeable(*handshakes[clientId]);
     329  }
     330
     331
     332  //check if connections are ok else remove them
     333  for ( int i = 1; i<networkSockets.size(); i++)
     334  {
     335    if ( networkSockets[i] && !networkSockets[i]->isOk() )
     336    {
     337      //TODO: tell EntityManager that this player left the game
     338      PRINTF(0)("Client is gone: %d\n", i);
     339
     340      //delete networkSockets[i];
     341      networkSockets[i]->disconnectServer();
     342      networkSockets[i]->destroy();
     343      networkSockets[i] = NULL;
     344      //TODO: delete handshake from synchronizeable list so i can delete it
     345      if ( handshakes[i] )
     346        delete handshakes[i];
     347      handshakes[i] = NULL;
     348
     349      if ( i == networkSockets.size()-1 )
     350      {
     351        networkSockets.pop_back();
     352        handshakes.pop_back();
     353      }
     354      else
     355      {
     356        freeSocketSlots.push_back(i);
     357      }
     358    }
     359  }
     360
     361
     362}
     363
     364void NetworkStream::setMaxConnections( int n )
     365{
     366  if ( !this->isServer() )
     367  {
     368    PRINTF(1)("Cannot set maxConnections because I am no server.\n");
     369  }
     370  if ( this->networkSockets.size() > 1 )
     371  {
     372    PRINTF(1)("Cannot set maxConnections because there are already %d connections.\n", this->networkSockets.size());
     373    return;
     374  }
     375  this->maxConnections = n;
     376}
     377
     378
  • trunk/src/lib/network/network_stream.h

    r5996 r6139  
    77#define _NETWORK_STREAM
    88
     9#include <vector>
     10#include <list>
     11
    912#include "data_stream.h"
    1013#include "network_protocol.h"
     14#include "server_socket.h"
     15#include "handshake.h"
    1116
    1217class Synchronizeable;
    1318class NetworkSocket;
     19class ServerSocket;
    1420class ConnectionMonitor;
    1521class NetworkProtocol;
    1622
    17 
    18 //<! The state of the NetworkStream
    19 typedef enum NetStat {
    20   NET_REC_HEADER = 0,                          //!< Waiting for header
    21   NET_REC_DATA,                                //!< Waiting for data
    22 
    23   NUM_STATES                                   //!< Number of states
    24 };
     23typedef std::list<Synchronizeable*>  SynchronizeableList;
     24typedef std::vector<NetworkSocket*>  NetworkSocketVector;
     25typedef std::vector<Handshake*>      HandshakeVector;
    2526
    2627
     
    3031  public:
    3132    NetworkStream();
    32     NetworkStream(IPaddress& address, NodeType type);
    33     NetworkStream(unsigned int port, NodeType type);
     33    NetworkStream(IPaddress& address);
     34    NetworkStream(unsigned int port);
    3435
    35     NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type);
    36     NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type);
    3736    ~NetworkStream();
    3837    void init();
    3938
    4039    void connectSynchronizeable(Synchronizeable& sync);
     40    void disconnectSynchronizeable(Synchronizeable& sync);
    4141
    4242    inline bool isServer() const { return (this->type == NET_SERVER)? true:false; }
    4343    inline bool isActive() const { return this->bActive; }
     44
     45    inline int getMaxConnections(){ return maxConnections; }
     46    void setMaxConnections( int n );
    4447
    4548    virtual void processData();
     
    4851    NetworkProtocol*       networkProtocol;
    4952    ConnectionMonitor*     connectionMonitor;
    50     Synchronizeable*       synchronizeables;
    51     NetworkSocket*         networkSocket;
     53    SynchronizeableList    synchronizeables;
     54    NetworkSocketVector    networkSockets;
     55    HandshakeVector        handshakes;
     56    ServerSocket*          serverSocket;
    5257    int                    type;
    53     int                    state;
    5458    Header                 packetHeader;
    5559    bool                   bActive;
     60    std::list<int>         freeSocketSlots;
     61
     62    int                    myHostId;
     63    int                    maxConnections;
     64
     65    void updateConnectionList();
    5666};
    5767#endif /* _NETWORK_STREAM */
  • trunk/src/lib/network/server_socket.cc

    r5996 r6139  
    110110
    111111
    112 NetworkSocket ServerSocket::getNewSocket( )
     112NetworkSocket* ServerSocket::getNewSocket( )
    113113{
     114  if ( !listenSocket )
     115  {
     116    PRINTF(1)("listenSocket == NULL! Maybe you forgot to call listen()\n");
     117    close();
     118    return NULL;
     119  }
     120
    114121  TCPsocket sock = SDLNet_TCP_Accept(listenSocket);
    115122
     
    120127  else
    121128  {
    122     return NetworkSocket(sock);
     129    return new NetworkSocket(sock);
    123130  }
    124131}
    125132
     133void ServerSocket::close( )
     134{
     135  if ( listenSocket )
     136  {
     137    SDLNet_TCP_Close( listenSocket );
     138    listenSocket = NULL;
     139  }
     140
     141  _isListening = false;
     142}
     143
  • trunk/src/lib/network/server_socket.h

    r5996 r6139  
    3939    ~ServerSocket();
    4040    bool listen( unsigned int port );
    41     NetworkSocket getNewSocket( void );
     41    NetworkSocket* getNewSocket( void );
     42    void close();
     43    inline bool isOk(){ return listenSocket!=NULL; }
    4244};
    4345
  • trunk/src/lib/network/synchronizeable.cc

    r5997 r6139  
    1515*/
    1616
     17#define DEBUG_MODULE_NETWORK
     18
    1719#include "synchronizeable.h"
    1820#include "netdefs.h"
     21#include "network_manager.h"
     22#include "network_stream.h"
    1923
    2024
     
    2529{
    2630
    27   //owner = ?;
    28   //hostID = ?;
     31  owner = 0;
     32  hostID = NetworkManager::getInstance()->getHostID();
     33  uniqueID = -1;
    2934  //state = ?;
    3035
     
    4449 */
    4550Synchronizeable::~Synchronizeable()
    46 {}
     51{
     52  if ( this->networkStream )
     53    this->networkStream->disconnectSynchronizeable(*this);
     54}
    4755
    4856/**
     
    5058 */
    5159void Synchronizeable::writeBytes(const byte* data, int length)
    52 {}
     60{
     61  PRINTF(1)("Synchronizeable::writeBytes was called\n");
     62}
    5363
    5464/**
    5565 *  read data from NetworkStream
    5666 */
    57 int Synchronizeable::readBytes(byte* data)
    58 {}
     67int Synchronizeable::readBytes(byte* data, int maxLength, int * reciever)
     68{
     69  PRINTF(1)("Synchronizeable::readBytes was called\n");
     70}
    5971
    6072
     
    6577void Synchronizeable::readDebug() const
    6678{}
    67 
    68 
    69 
    7079
    7180
     
    111120  return this->state & STATE_OUTOFSYNC == STATE_OUTOFSYNC;
    112121}
     122
     123
     124
  • trunk/src/lib/network/synchronizeable.h

    r5997 r6139  
    1919#define STATE_OUTOFSYNC 2
    2020
     21class NetworkStream;
     22
    2123
    2224class Synchronizeable : virtual public BaseObject
     
    2931
    3032    virtual void      writeBytes(const byte* data, int length);
    31     virtual int       readBytes(byte* data);
     33    virtual int       readBytes(byte* data, int maxLength, int * reciever);
    3234    virtual void      writeDebug() const;
    3335    virtual void      readDebug() const;
    34    
    35    
    36    
    37    
    38    
    39     void setIsServer(bool isServer);
    40     void setIsOutOfSync(bool outOfSync);
     36
     37    void setIsServer( bool isServer );
     38    void setIsOutOfSync( bool outOfSync );
    4139    bool isServer();
    4240    bool isOutOfSync();
     41    void setUniqueID( int id ){ uniqueID = id; }
     42    int  getUniqueID() const { return uniqueID; };
     43    void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); }
     44
     45    inline int getOwner(){ return owner; }
     46    inline void setOwner(int owner){ this->owner = owner; }
     47
     48    inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; }
    4349
    4450  private:
    4551
    4652    int               uniqueID;
    47    
    48    
    49    
     53
     54
     55
    5056    //static std::vector<Synchronizeable*> classList;
    5157    int owner;
     
    5460    std::list<int> synchronizeRequests;
    5561
     62    NetworkStream* networkStream;
     63
    5664  };
    5765#endif /* _SYNCHRONIZEABLE_H */
  • trunk/src/lib/sound/sound_source.cc

    r5930 r6139  
    6969    SoundEngine::getInstance()->popALSource(this->sourceID);
    7070
    71   printf("%d\n",sourceID);
     71  //printf("%d\n",sourceID);
    7272  alSourceStop(this->sourceID);
    7373  alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
Note: See TracChangeset for help on using the changeset viewer.