Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6043 in orxonox.OLD for branches


Ignore:
Timestamp:
Dec 11, 2005, 2:07:08 PM (19 years ago)
Author:
rennerc
Message:

added handshake

Location:
branches/network/src
Files:
2 added
15 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/defs/class_id.h

    r5996 r6043  
    220220
    221221  // network stuff (range from 0x00000b00 to 0x00000bff)
    222   CL_DATA_STREAM                =    0x00b01000,
     222  CL_DATA_STREAM                =    0x00b00b00,
    223223  CL_NETWORK_STREAM             =    0x00000b01,
    224224  CL_NETWORK_PROTOCOL           =    0x00000b02,
     
    226226  CL_SERVER_SOCKET              =    0X00000b04,
    227227  CL_CONNECTION_MONITOR         =    0x00000b05,
     228  CL_HANDSHAKE                  =    0x00000b06,
    228229
    229230
  • branches/network/src/lib/network/Makefile.am

    r5996 r6043  
    1111                      data_stream.cc \
    1212                      network_protocol.cc \
    13                       server_socket.cc
     13                      server_socket.cc \
     14                      handshake.cc
    1415
    1516
     
    2223                 data_stream.h \
    2324                 network_protocol.h \
    24                  server_socket.h
     25                 server_socket.h \
     26                 handshake.h
    2527
  • branches/network/src/lib/network/network_protocol.cc

    r5822 r6043  
    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(0)("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)
     
    8281  data[1] = 0;
    8382  /* 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;
     83  data[2] = (byte)(source.getUniqueID());
    8784  /* data length*/
    88   data[4] = length;
     85  data[3] = length;
    8986
    9087
     
    104101  if (length < headerLength)
    105102  {
    106     PRINTF(0)("Received data is to short; it can't contain a header!");
     103    PRINTF(0)("Received data is to short; it can't contain a header!\n");
    107104    Header h;
    108105    h.protocol = 0;
     
    117114  /* version number */
    118115  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];
     116  /* unique ID */
     117  h.uniqueID = data[2];
    123118  /* data length*/
    124   h.length = data[4];
     119  h.length = data[3];
    125120
    126121
  • branches/network/src/lib/network/network_protocol.h

    r5822 r6043  
    1717  byte protocol;
    1818  byte version;
    19   byte senderID;
    20   byte receiverID;
     19  byte uniqueID;
    2120  byte length;
    2221};
     
    3332    ~NetworkProtocol();
    3433
    35     int createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source, unsigned int remoteID);
     34    int createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source);
    3635    Header extractHeader(byte* data, int length);
    3736
  • branches/network/src/lib/network/network_stream.cc

    r6039 r6043  
    2626#include "connection_monitor.h"
    2727#include "synchronizeable.h"
     28#include "network_manager.h"
    2829#include "list.h"
    2930#include "debug.h"
     
    5657  this->networkProtocol = new NetworkProtocol();
    5758  this->connectionMonitor = new ConnectionMonitor();
     59
     60  Handshake* hs = new Handshake(false);
     61  this->handshakes.push_back(hs);
     62  this->connectSynchronizeable(*hs);
     63  PRINTF(0)("NetworkStream: %s\n", hs->getName());
    5864}
    5965
     
    7783  this->bActive = false;
    7884  this->serverSocket = NULL;
     85  myHostId = 0;
    7986}
    8087
     
    94101      (*i)->disconnectServer();
    95102      (*i)->destroy();
     103    }
     104  }
     105
     106  for (HandshakeVector::iterator i = handshakes.begin(); i!=handshakes.end(); i++)
     107  {
     108    if ( *i )
     109    {
     110      delete (*i);
    96111    }
    97112  }
     
    116131  if ( this->type == NET_SERVER )
    117132    this->updateConnectionList();
     133
     134  for (HandshakeVector::iterator it = handshakes.begin(); it!=handshakes.end(); it++)
     135  {
     136    if ( *it )
     137    {
     138      if ( (*it)->completed() )
     139      {
     140        if ( (*it)->ok() )
     141        {
     142          NetworkManager::getInstance()->setHostID( (*it)->getHostId() );
     143          myHostId = NetworkManager::getInstance()->getHostID();
     144          delete *it;
     145          *it = NULL;
     146          PRINT(0)("handshake finished\n");
     147        }
     148        else
     149        {
     150          PRINT(1)("handshake failed!\n");
     151          delete *it;
     152          *it = NULL;
     153          //TODO: handle error
     154        }
     155      }
     156    }
     157  }
     158
     159
     160  /* DOWNSTREAM */
     161
     162  int dataLength;
     163  int reciever;
     164  Header header;
     165  for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
     166  {
     167    if ( (*it)->getOwner() == myHostId )
     168    {
     169      PRINTF(0)("sync: %s\n", (*it)->getName());
     170      reciever = 0;
     171      do {
     172        dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, reciever);
     173
     174        if ( dataLength<=0 ){
     175          reciever = 0;
     176          continue;
     177        }
     178
     179        PRINTF(0)("read %d bytes\n", dataLength);
     180
     181        dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it)));
     182
     183        if ( dataLength<=0 )
     184          continue;
     185
     186        if ( reciever!=0 )
     187        {
     188          if ( networkSockets[reciever] )
     189          {
     190            PRINTF(0)("write %d bytes to socket\n", dataLength);
     191            networkSockets[reciever]->writePacket(downBuffer, dataLength);
     192          }
     193          else
     194          {
     195            PRINTF(1)("networkSockets[reciever] == NULL\n");
     196          }
     197        }
     198        else
     199        {
     200          for ( int i = 1; i<networkSockets.size(); i++)
     201          {
     202            if ( networkSockets[i] )
     203              networkSockets[i]->writePacket(downBuffer, dataLength);
     204          }
     205        }
     206
     207      } while( reciever!=0 );
     208    }
     209  }
     210
     211  /* UPSTREAM */
     212
     213  for ( int i = 1; i<networkSockets.size(); i++)
     214  {
     215    if ( networkSockets[i] )
     216    {
     217      do {
     218        dataLength = networkSockets[i]->readPacket(upBuffer, DATA_STREAM_BUFFER_SIZE);
     219
     220        if ( dataLength<=0 )
     221          continue;
     222
     223        PRINTF(0)("read %d bytes from socket\n", dataLength);
     224        header = networkProtocol->extractHeader(upBuffer, dataLength);
     225        dataLength -= sizeof(header);
     226
     227        if ( dataLength != header.length )
     228        {
     229          PRINTF(1)("packetsize in header and real packetsize do not match! %d:%d\n", dataLength, header.length);
     230          continue;
     231        }
     232
     233        for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
     234        {
     235          if ( *it && (*it)->getUniqueID()==header.uniqueID )
     236            (*it)->writeBytes(upBuffer+sizeof(header), dataLength);
     237        }
     238
     239      } while ( dataLength>0 );
     240    }
     241  }
    118242
    119243#if 0
     
    199323      freeSocketSlots.pop_back();
    200324      networkSockets[clientId] = tempNetworkSocket;
     325      handshakes[clientId] = new Handshake(true, clientId);
    201326    } else
    202327    {
    203328      clientId = networkSockets.size();
    204329      networkSockets.push_back(tempNetworkSocket);
     330      handshakes.push_back(new Handshake(true, clientId));
    205331    }
    206332
    207333    PRINTF(0)("New Client: %d\n", clientId);
    208     //TODO: start handshake
    209     //new Handshake(true, clientId);
     334
     335    this->connectSynchronizeable(*handshakes[clientId]);
    210336  }
    211337
     
    223349      networkSockets[i]->destroy();
    224350      networkSockets[i] = NULL;
     351      delete handshakes[i];
     352      handshakes[i] = NULL;
    225353
    226354      if ( i == networkSockets.size()-1 )
    227355      {
    228356        networkSockets.pop_back();
     357        handshakes.pop_back();
    229358      }
    230359      else
     
    237366
    238367}
     368
     369
  • branches/network/src/lib/network/network_stream.h

    r6018 r6043  
    1313#include "network_protocol.h"
    1414#include "server_socket.h"
     15#include "handshake.h"
    1516
    1617class Synchronizeable;
     
    2122
    2223typedef std::list<Synchronizeable*>  SynchronizeableList;
    23 typedef std::vector<NetworkSocket*> NetworkSocketVector;
     24typedef std::vector<NetworkSocket*>  NetworkSocketVector;
     25typedef std::vector<Handshake*>      HandshakeVector;
    2426
    2527
     
    4749    SynchronizeableList    synchronizeables;
    4850    NetworkSocketVector    networkSockets;
     51    HandshakeVector        handshakes;
    4952    ServerSocket*          serverSocket;
    5053    int                    type;
     
    5356    std::list<int>         freeSocketSlots;
    5457
     58    int                    myHostId;
     59
    5560    void updateConnectionList();
    5661};
  • branches/network/src/lib/network/synchronizeable.cc

    r5997 r6043  
    1717#include "synchronizeable.h"
    1818#include "netdefs.h"
     19#include "network_manager.h"
    1920
    2021
     
    2627
    2728  //owner = ?;
    28   //hostID = ?;
     29  hostID = NetworkManager::getInstance()->getHostID();
    2930  //state = ?;
    3031
     
    5556 *  read data from NetworkStream
    5657 */
    57 int Synchronizeable::readBytes(byte* data)
     58int Synchronizeable::readBytes(byte* data, int maxLength, int& reciever)
    5859{}
    5960
     
    6566void Synchronizeable::readDebug() const
    6667{}
    67 
    68 
    69 
    7068
    7169
     
    111109  return this->state & STATE_OUTOFSYNC == STATE_OUTOFSYNC;
    112110}
     111
     112
     113
  • branches/network/src/lib/network/synchronizeable.h

    r5997 r6043  
    2929
    3030    virtual void      writeBytes(const byte* data, int length);
    31     virtual int       readBytes(byte* data);
     31    virtual int       readBytes(byte* data, int maxLength, int& reciever);
    3232    virtual void      writeDebug() const;
    3333    virtual void      readDebug() const;
    34    
    35    
    36    
    37    
    38    
    39     void setIsServer(bool isServer);
    40     void setIsOutOfSync(bool outOfSync);
     34
     35    void setIsServer( bool isServer );
     36    void setIsOutOfSync( bool outOfSync );
    4137    bool isServer();
    4238    bool isOutOfSync();
     39    void setUniqueId( int id ){ uniqueID = id; }
     40    int  getUniqueID() const { return uniqueID; };
     41    void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); }
     42
     43    inline int getOwner(){ return owner; }
     44    inline void setOwner(int owner){ this->owner = owner; }
    4345
    4446  private:
    4547
    4648    int               uniqueID;
    47    
    48    
    49    
     49
     50
     51
    5052    //static std::vector<Synchronizeable*> classList;
    5153    int owner;
  • branches/network/src/subprojects/network/network_unit_test.cc

    r6026 r6043  
    190190  Synchronizeable* ss = new SimpleSync("Server\0");
    191191
    192   netMan->createServer(*ss, port);
     192  netMan->createServer(/**ss, */port);
    193193  SDL_Delay(20);
    194194
     
    229229  Synchronizeable* ss = new SimpleSync("Client\0");
    230230
    231   netMan->establishConnection(ip, *ss);
     231  netMan->establishConnection((const char*)"localhost", port/*,ip, *ss*/);
    232232
    233233  for(;;)
  • branches/network/src/subprojects/network/read_sync.cc

    r5996 r6043  
    8080 *  read data from Synchronizeable
    8181 */
    82 int ReadSync::readBytes(byte* data)
     82int ReadSync::readBytes(byte* data, int maxLength, int& reciever)
    8383{
    8484  return 0;
  • branches/network/src/subprojects/network/read_sync.h

    r5996 r6043  
    1717
    1818    virtual void writeBytes(const byte* data, int length);
    19     virtual int readBytes(byte* data);
     19    virtual int readBytes(byte* data, int maxLength, int& reciever);
    2020
    2121
  • branches/network/src/subprojects/network/simple_sync.cc

    r5996 r6043  
    8686 *  read data from Synchronizeable
    8787 */
    88 int SimpleSync::readBytes(byte* data)
     88int SimpleSync::readBytes(byte* data, int maxLength, int& reciever)
    8989{
    9090  PRINTF(0)("SimpleSync: sent %i bytes of data\n", this->outLength);
  • branches/network/src/subprojects/network/simple_sync.h

    r5996 r6043  
    1717
    1818    virtual void writeBytes(const byte* data, int length);
    19     virtual int readBytes(byte* data);
     19    virtual int readBytes(byte* data, int maxLength, int& reciever);
    2020
    2121
  • branches/network/src/subprojects/network/write_sync.cc

    r5996 r6043  
    8686 *  read data from Synchronizeable
    8787 */
    88 int WriteSync::readBytes(byte* data)
     88int WriteSync::readBytes(byte* data, int maxLength, int& reciever)
    8989{
    9090  PRINTF(0)("WriteSync: sent %i bytes of data\n", this->outLength);
  • branches/network/src/subprojects/network/write_sync.h

    r5996 r6043  
    1717
    1818    virtual void writeBytes(const byte* data, int length);
    19     virtual int readBytes(byte* data);
     19    virtual int readBytes(byte* data, int maxLength, int& reciever);
    2020
    2121
Note: See TracChangeset for help on using the changeset viewer.