Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5829 in orxonox.OLD for branches/network/src/lib


Ignore:
Timestamp:
Nov 30, 2005, 9:43:05 AM (19 years ago)
Author:
patrick
Message:

network: much work on multiplayability, does not yet work

Location:
branches/network/src/lib/network
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/lib/network/network_manager.cc

    r5822 r5829  
    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/**
     
    161157  if (this->netStreamList != NULL || (this->netStreamList = ClassList::getList(CL_NETWORK_STREAM)) != NULL)
    162158  {
     159//     tIterator<BaseObject>* iterator = this->netStreamList->getIterator();
     160//     NetworkStream* stream = (NetworkStream*)(iterator->firstElement());
     161//     while( stream)
     162//     {
     163//       if(stream->isActive())
     164//         stream->processData();
     165//       stream = (NetworkStream*)(iterator->nextElement());
     166//     }
     167//     delete iterator;
    163168    std::list<BaseObject*>::iterator stream;
    164169    for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream)
    165       static_cast<NetworkStream*>(*stream)->processData();
     170      if( static_cast<NetworkStream*>(*stream)->isActive())
     171        static_cast<NetworkStream*>(*stream)->processData();
     172
    166173  }
    167174
  • branches/network/src/lib/network/network_manager.h

    r5822 r5829  
    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();
    4143
    42 private:
    43   std::list<BaseObject*>*    netStreamList;            // list with refs to all network streams
    44   std::list<BaseObject*>*    syncList;                 // list of synchronizeables
     44    void connectSynchronizeable(Synchronizeable& sync);
     45
     46    void synchronize();
     47
     48  private:
     49    NetworkManager();
     50
     51
     52  private:
     53    std::list<BaseObject*>*    netStreamList;            // list with refs to all network streams
     54    std::list<BaseObject*>*    syncList;                 // list of synchronizeables
     55    static NetworkManager* singletonRef;           //!< Pointer to the only instance of this Class
     56    NetworkStream*         tmpStream;              //!< FIXME: this is only for testing purposes
    4557
    4658};
  • branches/network/src/lib/network/network_socket.cc

    r5822 r5829  
    313313
    314314  while (!tempsocket && !self->terminateThread)
     315  {
    315316    tempsocket = SDLNet_TCP_Accept(self->tcpSocket);
     317    SDL_Delay(_MSECONDS_SLEEP_LISTEN);
     318  }
    316319
    317320  SDL_mutexP(self->socketMutex);
  • branches/network/src/lib/network/network_socket.h

    r5822 r5829  
    1818//sleep if outgoing buffer is empty
    1919#define _MSECONDS_SLEEP_EMPTY_BUFFER 10
     20//sleep when waiting for connections
     21#define _MSECONDS_SLEEP_LISTEN 100
    2022
    2123/* contains memmove and memcpy */
  • branches/network/src/lib/network/network_stream.cc

    r5822 r5829  
    4040
    4141NetworkStream::NetworkStream()
    42   : DataStream()
     42    : DataStream()
    4343{
    4444  this->init();
     
    5050}
    5151
     52NetworkStream::NetworkStream(IPaddress& address, NodeType type)
     53{
     54  this->init();
     55  this->networkSocket = new NetworkSocket(address);
     56  this->networkProtocol = new NetworkProtocol();
     57  this->synchronizeables = NULL;
     58  this->connectionMonitor = new ConnectionMonitor();
     59}
     60
     61
     62NetworkStream::NetworkStream(unsigned int port, NodeType type)
     63{
     64  this->init();
     65  this->networkSocket = new NetworkSocket();
     66  this->networkSocket->listen(port);
     67  this->networkProtocol = new NetworkProtocol();
     68  this->synchronizeables = NULL;
     69  this->connectionMonitor = new ConnectionMonitor();
     70}
     71
    5272
    5373NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type)
    54   : DataStream()
     74    : DataStream()
    5575{
    5676  this->init();
     
    5979  this->synchronizeables = &sync;
    6080  this->connectionMonitor = new ConnectionMonitor();
     81  this->bActive = true;
    6182}
    6283
    6384
    6485NetworkStream::NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type)
    65   : DataStream()
     86    : DataStream()
    6687{
    6788  this->init();
     
    7192  this->synchronizeables = &sync;
    7293  this->connectionMonitor = new ConnectionMonitor();
     94  this->bActive = true;
    7395}
    7496
     
    79101  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
    80102  this->state = NET_REC_HEADER;
     103  this->bActive = false;
    81104}
    82105
     
    85108{
    86109
    87  networkSocket->disconnectServer();
     110  networkSocket->disconnectServer();
    88111
    89  if( this->networkSocket)
    90    delete networkSocket;
     112  if( this->networkSocket)
     113    delete networkSocket;
    91114
    92  delete connectionMonitor;
    93  delete networkProtocol;
     115  delete connectionMonitor;
     116  delete networkProtocol;
    94117}
     118
     119
     120void NetworkStream::connectSynchronizeable(Synchronizeable& sync)
     121{
     122  this->synchronizeables = &sync;
     123  if( this->networkSocket != NULL)
     124    this->bActive = true;
     125}
     126
    95127
    96128void NetworkStream::processData()
     
    102134  PRINT(0)("============= DOWNSTREAM:===============\n");
    103135  /* first of all read the synchronizeable's data: */
     136  //if(this->isServer())
    104137  dataLength = this->synchronizeables->readBytes((byte*)downBuffer);
    105138
    106   /* send the received data to connectionMonitor */
    107   this->connectionMonitor->processPacket((byte*)downBuffer, dataLength);
     139  if( dataLength > 0)
     140  {
     141    /* send the received data to connectionMonitor */
     142    this->connectionMonitor->processPacket((byte*)downBuffer, dataLength);
    108143
    109   dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,
    110       *(this->synchronizeables), 12/* some random number (no real id)*/);
     144    dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,
     145                 *(this->synchronizeables), 12/* some random number (no real id)*/);
    111146
    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 
     147    /* pass the data to the network socket */
     148    dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
     149    /* check if there was an error */
     150    if( dataLength == -1)
     151    {
     152      PRINTF(0)("Error in writing data to the NetworkSocket\n");
     153    }
     154  }
    117155
    118156
     
    147185      this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length);
    148186      /* now pass the data to the sync object */
     187      //if(!this->isServer())
    149188      this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length);
    150189
  • branches/network/src/lib/network/network_stream.h

    r5822 r5829  
    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 
  • branches/network/src/lib/network/synchronizeable.cc

    r5822 r5829  
    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
  • branches/network/src/lib/network/synchronizeable.h

    r5822 r5829  
    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.