Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5996 in orxonox.OLD for trunk/src/lib/network


Ignore:
Timestamp:
Dec 9, 2005, 12:31:01 PM (19 years ago)
Author:
patrick
Message:

orxonox/trunk: merged network branche into trunk with command svn merge -r 5824:HEAD

Location:
trunk/src/lib/network
Files:
9 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/network/Makefile.am

    r5822 r5996  
    1010                      network_stream.cc \
    1111                      data_stream.cc \
    12                       network_protocol.cc
     12                      network_protocol.cc \
     13                      server_socket.cc
    1314
    1415
     
    2021                 network_stream.h \
    2122                 data_stream.h \
    22                  network_protocol.h
     23                 network_protocol.h \
     24                 server_socket.h
    2325
  • trunk/src/lib/network/network_manager.cc

    r5885 r5996  
    3535
    3636
    37 /************************************
    38   What you will see here are the function definitions from the header file (network_manager.h) with doxygen documentation. Here is an example:
    39 
    40 
    41  In file network_manager.h
    42 
    43  class NetworkManager
    44  {
    45    int doSomeStuff(float argument, float* pointer);
    46  }
    47 
    48  will be implemented in the source file as follows:
    49 
    50  In file network_manager.cc
    51 
    52  / **
    53   *  this is the short description for this function: it just does some stuff
    54   * @param argument: this is the first argument, stuff...
    55   * @param pointer:  this is the pointer to nowhereland
    56   * return: whatever the function returns: for example an index, number, etc.
    57   * /
    58  int NetworkManager::doSomeStuff(float argument, float* pointer)
    59  {
    60    // whaterver you want to do
    61  }
    62 
    63 
    64  if you want to make automake compile your files: you will want to add the file names to the local Makefile.am
    65 
    66  ************************************/
    67 
    68 
     37NetworkManager* NetworkManager::singletonRef = NULL;
    6938
    7039/**
     
    7948  this->netStreamList = NULL;
    8049  this->syncList = NULL;
     50  this->tmpStream = NULL;
    8151
    8252  PRINTF(0)("NetworkManager created\n");
     
    11484 *  creates a connection from one object to a host
    11585 * @param hostName: the name of the destination host
    116  * @param synchronizeable: reference to the sync object
    11786 */
    118 NetworkStream& establishConnection(const char& hostName, const Synchronizeable& sync)
    119 {}
     87int NetworkManager::establishConnection(const char* name, unsigned int port)
     88{
     89  IPaddress ipAddress;
     90  int error = SDLNet_ResolveHost(&ipAddress, name, port);
     91  if( error == -1) {
     92    printf("\n\nerror on address resolution, program inconsistency\n\n");
     93    return -1;
     94  }
     95
     96  this->tmpStream = new NetworkStream(ipAddress, NET_CLIENT);
     97  return 1;
     98}
     99
     100
     101/**
     102 *  creates a new NetworkStream of server type
     103 * @param port: number of the TCP port
     104 */
     105int NetworkManager::createServer(unsigned int port)
     106{
     107  this->tmpStream = new NetworkStream(port, NET_SERVER);
     108  SDL_Delay(20);
     109  return 1;
     110}
    120111
    121112
     
    127118NetworkStream& NetworkManager::establishConnection(IPaddress& address, Synchronizeable& sync)
    128119{
    129   printf("Establish connection to server %s, on port %u\n", SDLNet_ResolveIP(&address), address.port);
    130120  /* creating a new network stream, it will register itself automaticaly to the class list */
    131   NetworkStream* netStream = new NetworkStream(address, sync, NET_CLIENT);
     121  this->tmpStream = new NetworkStream(address, sync, NET_CLIENT);
    132122}
     123
    133124
    134125/**
     
    140131  PRINTF(0)("Create a new server socket\n");
    141132  /* creating a new network stream, it will register itself automaticaly to the class list */
    142   NetworkStream* netStream = new NetworkStream(port, sync, NET_SERVER);
     133  this->tmpStream = new NetworkStream(port, sync, NET_SERVER);
    143134}
    144135
     
    153144
    154145
     146void NetworkManager::connectSynchronizeable(Synchronizeable& sync)
     147{
     148  this->tmpStream->connectSynchronizeable(sync);
     149}
     150
    155151
    156152/**
     
    163159    std::list<BaseObject*>::const_iterator stream;
    164160    for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream)
    165       static_cast<NetworkStream*>(*stream)->processData();
     161      if( static_cast<NetworkStream*>(*stream)->isActive())
     162        static_cast<NetworkStream*>(*stream)->processData();
     163
    166164  }
    167165
  • trunk/src/lib/network/network_manager.h

    r5885 r5996  
    1919class NetworkStream;
    2020class Synchronizeable;
    21 template<typename> class tList;
     21template<typename>
     22class tList;
    2223
    2324/* and here is the class itsself*/
     
    2526{
    2627
    27 public:
     28  public:
    2829
    29   NetworkManager();
    30   ~NetworkManager();
     30    inline static NetworkManager* getInstance() { if (!NetworkManager::singletonRef) NetworkManager::singletonRef = new NetworkManager();
     31      return NetworkManager::singletonRef; }
     32    ~NetworkManager();
    3133
    32   void initialize();
    33   void shutdown();
     34    void initialize();
     35    void shutdown();
    3436
    35   NetworkStream& establishConnection(IPaddress& address, Synchronizeable& sync);
    36   NetworkStream& establishConnection(const char& hostName, const Synchronizeable& sync);
    37   NetworkStream& createServer(Synchronizeable& sync, unsigned int port);
    38   void shutdownConnection();
     37    int establishConnection(const char* name, unsigned int port);
     38    int createServer(unsigned int port);
    3939
    40   void synchronize();
     40    NetworkStream& establishConnection(IPaddress& address, Synchronizeable& sync);
     41    NetworkStream& createServer(Synchronizeable& sync, unsigned int port);
     42    void shutdownConnection();
     43
    4144
    4245private:
    43   const std::list<BaseObject*>*    netStreamList;            // list with refs to all network streams
    44   const std::list<BaseObject*>*    syncList;                 // list of synchronizeables
     46    void connectSynchronizeable(Synchronizeable& sync);
     47    void synchronize();
     48
     49
     50  private:
     51    NetworkManager();
     52
     53
     54  private:
     55    const std::list<BaseObject*>*    netStreamList;            // list with refs to all network streams
     56    const std::list<BaseObject*>*    syncList;                 // list of synchronizeables
     57    static NetworkManager* singletonRef;           //!< Pointer to the only instance of this Class
     58    NetworkStream*         tmpStream;              //!< FIXME: this is only for testing purposes
    4559
    4660};
  • trunk/src/lib/network/network_socket.cc

    r5822 r5996  
    4747
    4848
     49NetworkSocket::NetworkSocket( TCPsocket sock )
     50{
     51  this->init();
     52  this->tcpSocket = sock;
     53
     54  SDL_CreateThread(thread_read, (void*)this);
     55  SDL_CreateThread(thread_write, (void*)this);
     56}
     57
    4958void NetworkSocket::init()
    5059{
     
    7584}
    7685
     86
     87
    7788/**
    7889 * Default destructor
     
    8596  PRINTF(5)("SDL_net shutdown\n");
    8697
    87   _isListening = false;
    88 
    8998  SDL_DestroyMutex(incomingBufferMutex);
    9099  SDL_DestroyMutex(outgoingBufferMutex);
     
    113122  }
    114123
    115   _isListening = false;
    116 
    117124  SDL_CreateThread(thread_read, (void*)this);
    118125  SDL_CreateThread(thread_write, (void*)this);
    119126}
    120127
    121 /**
    122  * Tells the NetworkSocket to listen on a specific port for incoming connections.
    123  * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection.
    124  * @param port
    125  */
    126 void NetworkSocket::listen(unsigned int port)
    127 {
    128   _isListening = true;
    129   //check if not already connected or listening
    130   if (tcpSocket)
    131   {
    132     PRINTF(1)("NetworkSocket::listen: tcpSocket!=NULL! maybe you already called listen or connectToServer or did not call disconnectServer()!\n");
    133     _isListening = false;
    134     return;
    135   }
    136 
    137   IPaddress ip;
    138 
    139   if (SDLNet_ResolveHost(&ip, NULL, port)==-1)
    140   {
    141     PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
    142     _isListening = false;
    143     return;
    144   }
    145 
    146   tcpSocket = SDLNet_TCP_Open(&ip);
    147 
    148   if (!tcpSocket)
    149   {
    150     PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
    151     _isListening = false;
    152     return;
    153   }
    154 
    155   SDL_CreateThread(thread_listen, (void*)this);
    156   SDL_CreateThread(thread_write, (void*)this);
    157 }
    158128
    159129/**
     
    184154  PRINTF(0)("NetworkSocket::writeBytes()\n");
    185155#ifdef _USE_OUTGOING_BUFFER
    186 
    187   //printf("length=%d, bufsize=%d\n", length, _OUTGOING_BUFFER_SIZE);
    188 //   if (length>_OUTGOING_BUFFER_SIZE)
    189 //   {
    190 //     int res = 0;
    191 //     int n = length / _OUTGOING_BUFFER_SIZE;
    192 //     if (length % _OUTGOING_BUFFER_SIZE != 0)
    193 //       n++;
    194 // //     printf("n=%d\n", n);
    195 //     SDL_Delay(500);
    196 //     for (int i = 0; i<n; i++)
    197 //     {
    198 // //       printf("i=%d\n", i);
    199 //       if (i==n-1)
    200 //       {
    201 //         res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, length-i*_OUTGOING_BUFFER_SIZE);
    202 // //         printf("res = %d\n", res);
    203 //       }
    204 //       else
    205 //       {
    206 //         res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, _OUTGOING_BUFFER_SIZE);
    207 // //         printf("res = %d\n", res);
    208 //       }
    209 //     }
    210 //     return res;
    211 //   }
    212156
    213157#define min(a,b) (a<b)?a:b
     
    256200int NetworkSocket::readBytes(byte * data, int length)
    257201{
     202  PRINTF(0)("NetworkSocket::readBytes()\n");
    258203  if (data==NULL)
    259204    return 0;
     
    299244}
    300245
    301 /**
    302  * used to create a thread to listen
    303  * will call thrad_read when established connection
    304  * @param data: pointer to NetwortSocket
    305  */
    306 int NetworkSocket::thread_listen( void * data )
    307 {
    308   NetworkSocket * self = (NetworkSocket*)data;
    309   self->_isListening = true;
    310   TCPsocket tempsocket;
    311 
    312   tempsocket = SDLNet_TCP_Accept(self->tcpSocket);
    313 
    314   while (!tempsocket && !self->terminateThread)
    315     tempsocket = SDLNet_TCP_Accept(self->tcpSocket);
    316 
    317   SDL_mutexP(self->socketMutex);
    318   SDLNet_TCP_Close(self->tcpSocket);
    319   self->tcpSocket = NULL;
    320 
    321   if (!tempsocket)
    322   {
    323     printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
    324     //printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
    325     SDL_mutexV(self->socketMutex);
    326     self->_isListening = false;
    327     return -1;
    328   }
    329 
    330   self->tcpSocket = tempsocket;
    331 
    332   SDL_mutexV(self->socketMutex);
    333 
    334   self->_isListening = false;
    335   return thread_read(data);
    336 }
    337246
    338247/**
     
    354263
    355264    //if buffer is full
    356     if (nbytestoread<=0 || self->_isListening)
     265    if (nbytestoread<=0)
    357266    {
    358267      SDL_Delay(_MSECONDS_SLEEP_FULL_BUFFER);
     
    405314
    406315    //if buffer is full
    407     if (nbytestowrite<=0 || self->_isListening)
     316    if (nbytestowrite<=0)
    408317    {
    409318      SDL_Delay(_MSECONDS_SLEEP_EMPTY_BUFFER);
     
    442351}
    443352
     353bool NetworkSocket::writePacket( byte * data, int length )
     354{
     355  PRINTF(0)("NetworkSocket::writePacket()\n");
     356  if (length>255)
     357  {
     358    PRINTF(1)("Packet length > 255!\n");
     359    return false;
     360  }
     361
     362  byte blen = length;
     363
     364  writeBytes(&blen, 1);
     365  writeBytes(data, length);
     366}
     367
     368int NetworkSocket::readPacket( byte * data, int maxLength )
     369{
     370  PRINTF(0)("NetworkSocket::readPacket()\n");
     371  if (incomingBufferLength<1)
     372  {
     373    return 0;
     374  }
     375
     376  byte blen = incomingBuffer[0];
     377
     378  if (blen>maxLength)
     379  {
     380    PRINTF(1)("Buffersize is too small (%d) for packet (%d)\n", maxLength, blen);
     381    return 0;
     382  }
     383
     384  if (blen>incomingBufferLength)
     385  {
     386    return 0;
     387  }
     388
     389  readBytes(&blen, 1);
     390  int res = readBytes(data, blen);
     391
     392  if (res!=blen)
     393    return -1;
     394  else
     395    return blen;
     396
     397}
     398
     399
  • trunk/src/lib/network/network_socket.h

    r5822 r5996  
    1818//sleep if outgoing buffer is empty
    1919#define _MSECONDS_SLEEP_EMPTY_BUFFER 10
     20
    2021
    2122/* contains memmove and memcpy */
     
    6263  bool terminateThread;
    6364
    64   static int thread_listen(void * data);
    6565  static int thread_read(void * data);
    6666#ifdef _USE_OUTGOING_BUFFER
     
    6868#endif
    6969
    70   bool _isListening;
     70  int writeBytes(byte * data, int length);
     71  int readBytes(byte * data, int length);
     72  int readBlock(byte * data, int length);
     73
     74  void init();
    7175
    7276public:
     
    7478  NetworkSocket();
    7579  NetworkSocket(IPaddress ip);
     80  NetworkSocket(TCPsocket sock);
    7681  ~NetworkSocket();
    7782
    7883  void connectToServer(IPaddress ip);
    79   void listen(unsigned int port);
    8084  void disconnectServer();
    81   int writeBytes(byte * data, int length);
    82   int readBytes(byte * data, int length);
    83   int readBlock(byte * data, int length);
    8485
    85 private:
    86   void init();
     86  bool writePacket(byte * data, int length);
     87  int readPacket(byte * data, int maxLength);
    8788
    8889};
  • trunk/src/lib/network/network_stream.cc

    r5822 r5996  
    4040
    4141NetworkStream::NetworkStream()
    42   : DataStream()
     42    : DataStream()
    4343{
    4444  this->init();
    4545  /* initialize the references */
     46  this->type = NET_CLIENT;
    4647  this->networkSocket = new NetworkSocket();
    4748  this->networkProtocol = new NetworkProtocol();
     
    5051}
    5152
     53NetworkStream::NetworkStream(IPaddress& address, NodeType type)
     54{
     55  this->type = type;
     56  this->init();
     57  this->networkSocket = new NetworkSocket(address);
     58  this->networkProtocol = new NetworkProtocol();
     59  this->synchronizeables = NULL;
     60  this->connectionMonitor = new ConnectionMonitor();
     61}
     62
     63
     64NetworkStream::NetworkStream(unsigned int port, NodeType type)
     65{
     66  this->type = type;
     67  this->init();
     68  this->networkSocket = new NetworkSocket();
     69//  this->networkSocket->listen(port);
     70  this->networkProtocol = new NetworkProtocol();
     71  this->synchronizeables = NULL;
     72  this->connectionMonitor = new ConnectionMonitor();
     73}
     74
    5275
    5376NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type)
    54   : DataStream()
    55 {
     77    : DataStream()
     78{
     79  this->type = type;
    5680  this->init();
    5781  this->networkSocket = new NetworkSocket(address);
     
    5983  this->synchronizeables = &sync;
    6084  this->connectionMonitor = new ConnectionMonitor();
     85  this->bActive = true;
    6186}
    6287
    6388
    6489NetworkStream::NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type)
    65   : DataStream()
    66 {
     90    : DataStream()
     91{
     92  this->type = type;
    6793  this->init();
    6894  this->networkSocket = new NetworkSocket();
    69   this->networkSocket->listen(port);
     95//  this->networkSocket->listen(port);
    7096  this->networkProtocol = new NetworkProtocol();
    7197  this->synchronizeables = &sync;
    7298  this->connectionMonitor = new ConnectionMonitor();
     99  this->bActive = true;
    73100}
    74101
     
    79106  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
    80107  this->state = NET_REC_HEADER;
     108  this->bActive = false;
    81109}
    82110
     
    85113{
    86114
    87  networkSocket->disconnectServer();
    88 
    89  if( this->networkSocket)
    90    delete networkSocket;
    91 
    92  delete connectionMonitor;
    93  delete networkProtocol;
    94 }
     115  networkSocket->disconnectServer();
     116
     117  if( this->networkSocket)
     118    delete networkSocket;
     119
     120  delete connectionMonitor;
     121  delete networkProtocol;
     122}
     123
     124
     125void NetworkStream::connectSynchronizeable(Synchronizeable& sync)
     126{
     127  this->synchronizeables = &sync;
     128  if( this->networkSocket != NULL)
     129    this->bActive = true;
     130}
     131
    95132
    96133void NetworkStream::processData()
     
    102139  PRINT(0)("============= DOWNSTREAM:===============\n");
    103140  /* first of all read the synchronizeable's data: */
    104   dataLength = this->synchronizeables->readBytes((byte*)downBuffer);
    105 
    106   /* send the received data to connectionMonitor */
    107   this->connectionMonitor->processPacket((byte*)downBuffer, dataLength);
    108 
    109   dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,
    110       *(this->synchronizeables), 12/* some random number (no real id)*/);
    111 
    112   /* pass the data to the network socket */
    113   dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
    114   /* check if there was an error */
    115   if( dataLength == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");}
    116 
     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  }
    117160
    118161
     
    124167  if( this->state == NET_REC_HEADER)
    125168  {
    126     dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
     169//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
    127170    if( dataLength == sizeof(Header))
    128171    {
     
    139182  {
    140183    /* now read the data */
    141     dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
     184//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
    142185    /* check if the data is available and process it if so */
    143186    if( dataLength == this->packetHeader.length)
    144187    {
    145       printf("NetworkStream::processData() - Got Data: \n");
     188      printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength);
    146189      /* send the received data to connectionMonitor */
    147190      this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length);
    148191      /* now pass the data to the sync object */
    149       this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length);
     192      if( !this->isServer())
     193        this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length);
    150194
    151195      this->state = NET_REC_HEADER;
  • trunk/src/lib/network/network_stream.h

    r5822 r5996  
    2828{
    2929
    30 public:
    31   NetworkStream();
    32   NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type);
    33   NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type);
    34   ~NetworkStream();
     30  public:
     31    NetworkStream();
     32    NetworkStream(IPaddress& address, NodeType type);
     33    NetworkStream(unsigned int port, NodeType type);
    3534
    36   void init();
     35    NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type);
     36    NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type);
     37    ~NetworkStream();
     38    void init();
    3739
    38   inline bool isServer() { return (this->type == NET_SERVER)? true:false; }
    39   virtual void processData();
     40    void connectSynchronizeable(Synchronizeable& sync);
    4041
    41 private:
    42    NetworkProtocol* networkProtocol;
    43    //NetworkSocket*       networkSockets;
    44    ConnectionMonitor*      connectionMonitor;
    45    // tList<Synchronizeable>* synchronizeables;
    46    Synchronizeable*         synchronizeables;
    47    NetworkSocket* networkSocket;
    48    int                    type;
    49    int                    state;
    50    Header                 packetHeader;
     42    inline bool isServer() const { return (this->type == NET_SERVER)? true:false; }
     43    inline bool isActive() const { return this->bActive; }
     44
     45    virtual void processData();
     46
     47  private:
     48    NetworkProtocol*       networkProtocol;
     49    ConnectionMonitor*     connectionMonitor;
     50    Synchronizeable*       synchronizeables;
     51    NetworkSocket*         networkSocket;
     52    int                    type;
     53    int                    state;
     54    Header                 packetHeader;
     55    bool                   bActive;
    5156};
    5257#endif /* _NETWORK_STREAM */
    53 
  • trunk/src/lib/network/synchronizeable.cc

    r5822 r5996  
    1818#include "netdefs.h"
    1919
     20
     21/**
     22 *  default constructor
     23 */
     24Synchronizeable::Synchronizeable()
     25{}
     26
    2027/**
    2128 *  default constructor
     
    2532  this->setName(name);
    2633}
     34
    2735
    2836/**
     
    4149 *  read data from NetworkStream
    4250 */
    43 int Synchronizeable::readBytes(byte* data) const
     51int Synchronizeable::readBytes(byte* data)
    4452{}
    4553
  • trunk/src/lib/network/synchronizeable.h

    r5822 r5996  
    1515
    1616    Synchronizeable(const char* name);
     17    Synchronizeable();
    1718    ~Synchronizeable();
    1819
    1920    virtual void      writeBytes(const byte* data, int length);
    20     virtual int       readBytes(byte* data) const;
     21    virtual int       readBytes(byte* data);
    2122    virtual void      writeDebug() const;
    2223    virtual void      readDebug() const;
Note: See TracChangeset for help on using the changeset viewer.