Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 27, 2015, 10:45:56 AM (10 years ago)
Author:
maxima
Message:

multiplayer branch merged to presentation

Location:
code/branches/presentationFS15/src/libraries/network
Files:
20 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentationFS15/src/libraries/network/GamestateManager.h

    r8402 r10497  
    1 /*
     1  /*
    22 *   ORXONOX - the hottest 3D action shooter ever to exist
    33 *                    > www.orxonox.net <
     
    7878      std::map< uint32_t, packet::Gamestate* > gamestates;
    7979    };
    80    
     80
    8181  public:
    82    
     82
    8383    GamestateManager();
    8484    ~GamestateManager();
     
    8888    virtual uint32_t  getLastReceivedGamestateID( unsigned int peerID );
    8989    virtual uint32_t  getCurrentGamestateID(){ if( currentGamestate_) return currentGamestate_->getID(); else return GAMESTATEID_INITIAL; }
    90    
     90
    9191    bool processGamestates();
    9292    bool sendAck(unsigned int gamestateID, uint32_t peerID);
  • code/branches/presentationFS15/src/libraries/network/LANDiscoverable.cc

    r8858 r10497  
    3535#include "util/Output.h"
    3636#include "packet/ServerInformation.h"
     37#include "core/config/ConfigValueIncludes.h"
     38#include "core/CoreIncludes.h"
     39
    3740
    3841namespace orxonox
     
    4346  LANDiscoverable::LANDiscoverable()
    4447  {
     48    /* register object in orxonox */
     49    RegisterObject(LANDiscoverable);
     50
     51    this->setConfigValues();
     52    //     this->setActivity(true);
    4553    this->host_ = 0;
    4654    this->bActive_ = false;
    47 //     this->setActivity(true);
     55  }
     56
     57  void LANDiscoverable::setConfigValues()
     58  {
     59    /* update ownName string from orxonox.ini config file, if it
     60     * has changed.
     61     */
     62    SetConfigValueExternal(ownName, "Discovery", "ownName", "OrxServer");
    4863  }
    4964
     
    6176    if( bActive == this->bActive_ )        // no change
    6277      return;
    63    
     78
    6479    if( bActive )
    6580    {
     
    8499  {
    85100    ENetEvent event;
    86    
     101
    87102    if( this->bActive_==false )
    88103      return;
    89104    assert(this->host_);
    90    
     105
    91106    while( enet_host_service( this->host_, &event, 0 ) > 0 )
    92107    {
     
    104119            orxout(internal_info, context::network) << "Received LAN discovery message from client " << event.peer->host->receivedAddress << endl;
    105120            packet::ServerInformation info;
    106             info.setServerName("Orxonox Server");
     121            info.setServerName(this->ownName);
     122            info.setClientNumber(this->clientNumber);
    107123            info.send(event.peer);
    108124//             ENetPacket* packet = enet_packet_create( LAN_DISCOVERY_ACK, strlen(LAN_DISCOVERY_ACK)+1, ENET_PACKET_FLAG_RELIABLE );
  • code/branches/presentationFS15/src/libraries/network/LANDiscoverable.h

    r8351 r10497  
    3131
    3232#include "NetworkPrereqs.h"
     33#include "core/config/Configurable.h"
    3334
    3435namespace orxonox
    3536{
    3637
    37   class LANDiscoverable
     38  class LANDiscoverable: public Configurable
    3839  {
    3940    public:
     
    4243      void setActivity( bool bActive );
    4344      void update();
     45      void updateClientNumber(int clientNumber) {this->clientNumber = clientNumber;}
     46;
     47      /** Function used for the configuration file parameter update */
     48      void setConfigValues();
    4449
    4550    private:
    4651      bool            bActive_;
    4752      ENetHost*       host_;
     53      std::string     ownName;
     54      int             clientNumber;
    4855  };
    4956
  • code/branches/presentationFS15/src/libraries/network/LANDiscovery.cc

    r8858 r10497  
    8989          {
    9090            packet::ServerInformation info(&event);
    91             orxout(internal_info, context::network) << "Received LAN discovery server information; Name: " << info.getServerName() << ", Address: " << info.getServerIP() << ", RTT: " << info.getServerRTT() << endl;
     91            std::string payload = info.getServerName();
     92            info.setServerName(payload.substr(0,payload.length()-2));
     93            info.setClientNumber( Ogre::StringConverter::parseInt(payload.substr(payload.length()-1)));
     94            orxout(internal_info, context::network) << "Received LAN discovery server information; Name: " << info.getServerName() << ", Address: " << info.getServerIP() << ", Players: " << info.getClientNumber() << ", RTT: " << info.getServerRTT() << endl;
    9295            std::vector<packet::ServerInformation>::iterator it;
    9396            for( it=this->servers_.begin(); it!=this->servers_.end(); ++it )
     
    124127  }
    125128
     129  std::string LANDiscovery::getServerListItemRTT(unsigned int index)
     130  {
     131    if( index >= this->servers_.size() )
     132      return BLANKSTRING;
     133    else{
     134      uint32_t serverrtt = this->servers_[index].getServerRTT();
     135      return Ogre::StringConverter::toString(serverrtt);
     136    }
     137  }
    126138
     139  std::string LANDiscovery::getServerListItemPlayerNumber(unsigned int index)
     140  {
     141    if( index >= this->servers_.size() )
     142      return BLANKSTRING;
     143    else{
     144      int playerNumber = this->servers_[index].getClientNumber();
     145      return Ogre::StringConverter::toString(playerNumber);
     146    }
     147  }
    127148} // namespace orxonox
  • code/branches/presentationFS15/src/libraries/network/LANDiscovery.h

    r8858 r10497  
    3333#include "packet/ServerInformation.h"
    3434#include "util/Singleton.h"
     35#include <OgreStringConverter.h>
    3536
    3637#include <vector>
     
    5152      std::string getServerListItemName( unsigned int index ); // tolua_export
    5253      std::string getServerListItemIP( unsigned int index ); // tolua_export
     54      std::string getServerListItemRTT( unsigned int index ); // tolua_export
     55      std::string getServerListItemPlayerNumber( unsigned int index ); // tolua_export
    5356      static LANDiscovery& getInstance(){ return Singleton<LANDiscovery>::getInstance(); } // tolua_export
    54      
     57
    5558    private:
    5659      static LANDiscovery* singletonPtr_s;
  • code/branches/presentationFS15/src/libraries/network/MasterServer.cc

    r8952 r10497  
    3434#include "util/Output.h"
    3535
    36 namespace orxonox 
     36namespace orxonox
    3737{
    3838  /*** MACROS ***/
     
    4545  MasterServer *MasterServer::instance = NULL;
    4646
    47 
    48 
    49 
    5047  /* command: list servers */
    51   void 
     48  void
    5249  MasterServer::listServers( void )
    5350  {
     
    5956
    6057    /* loop through list elements */
    61     for( i = MasterServer::getInstance()->mainlist.serverlist.begin(); 
    62       i != MasterServer::getInstance()->mainlist.serverlist.end(); ++i ) 
     58    for( i = MasterServer::getInstance()->mainlist.serverlist.begin();
     59      i != MasterServer::getInstance()->mainlist.serverlist.end(); ++i )
    6360    {
    6461      orxout(user_info) << "  " << (*i).ServerInfo.getServerIP() << std::endl;
     
    7067  }
    7168
    72   void 
     69  void
    7370  MasterServer::delServer( std::string todeladdr )
    7471  {
    7572    /* tell the user we're now removing the entry from the server list */
    76     orxout(user_info) << "MS: Deleting server \"" << todeladdr << "\"..." 
     73    orxout(user_info) << "MS: Deleting server \"" << todeladdr << "\"..."
    7774      << std::endl;
    7875
    7976    /* see if we actually have that server on our list */
    80     ServerListSearchResult shandle = 
     77    ServerListSearchResult shandle =
    8178      MasterServer::getInstance()->mainlist.findServerByAddress(todeladdr);
    8279
     
    8683    }
    8784
    88     /* force-disconnect the server */ 
     85    /* force-disconnect the server */
    8986    enet_peer_disconnect( shandle.result.peer, 0 );
    9087
     
    9895
    9996  /* helpers */
    100   static void 
     97  static void
    10198  helper_output_debug( ENetEvent *event, char *addrconv )
    10299  {
    103100    orxout(verbose, context::master_server)
    104       << "A packet of length" 
     101      << "A packet of length"
    105102      << event->packet->dataLength
    106103      << " containing "
    107104      << (const char*)event->packet->data
    108105      << " was received from "
    109       << addrconv 
     106      << addrconv
    110107      << " on channel "
    111108      << event->channelID << endl;
     
    122119
    123120    /* loop through list elements */
    124     for( i = mainlist.serverlist.begin(); i 
    125         != mainlist.serverlist.end(); ++i ) 
     121    for( i = mainlist.serverlist.begin(); i
     122        != mainlist.serverlist.end(); ++i )
    126123    {
    127124      /* send this particular server */
    128125      /* build reply string */
    129       char *tosend = (char *)calloc( (*i).ServerInfo.getServerIP().length()
    130           + MSPROTO_SERVERLIST_ITEM_LEN + 2,1 );
    131       if( !tosend ) 
     126      int packetlen = MSPROTO_SERVERLIST_ITEM_LEN + 1 + (*i).ServerInfo.getServerIP().length() + 1 + (*i).ServerInfo.getServerName().length() + 1 + sizeof((*i).ServerInfo.getClientNumber()) + 1;
     127      char *tosend = (char *)calloc(packetlen ,1 );
     128      if( !tosend )
    132129      { orxout(internal_warning, context::master_server) << "Masterserver.cc: Memory allocation failed." << endl;
    133130        continue;
    134       } 
    135       sprintf( tosend, "%s %s", MSPROTO_SERVERLIST_ITEM,
    136           (*i).ServerInfo.getServerIP().c_str() );
     131      }
     132      sprintf( tosend, "%s %s %s %u", MSPROTO_SERVERLIST_ITEM,
     133          (*i).ServerInfo.getServerIP().c_str(), (*i).ServerInfo.getServerName().c_str(), (*i).ServerInfo.getClientNumber());
    137134
    138135      /* create packet from it */
    139136      reply = enet_packet_create( tosend,
    140           strlen( tosend ) + 1, 
     137          strlen( tosend ) + 1,
    141138          ENET_PACKET_FLAG_RELIABLE);
    142139
     
    149146      /* free the tosend buffer */
    150147      free( tosend );
    151     } 
     148    }
    152149
    153150    /* create end-of-list packet */
     
    163160  }
    164161
    165   /* maybe the two methods below can be merged into one and 
    166    * made to use ENet's RTT functionality to check for disconnected 
     162  /* maybe the two methods below can be merged into one and
     163   * made to use ENet's RTT functionality to check for disconnected
    167164   * servers.
    168165   */
    169   void 
     166  void
    170167  MasterServer::helper_cleanupServers( void )
    171168  {
    172169    /* get an iterator */
    173170    std::list<ServerListElem>::iterator i;
    174      
     171
    175172    if( mainlist.serverlist.size() == 0 )
    176173      return;
    177174
    178175    /* loop through list elements */
    179     for( i = mainlist.serverlist.begin(); i 
    180         != mainlist.serverlist.end(); ++i ) 
     176    for( i = mainlist.serverlist.begin(); i
     177        != mainlist.serverlist.end(); ++i )
    181178    { /* see if we have a disconnected peer */
    182       if( (*i).peer && 
     179      if( (*i).peer &&
    183180         ((*i).peer->state == ENET_PEER_STATE_DISCONNECTED ||
    184181          (*i).peer->state == ENET_PEER_STATE_ZOMBIE ))
    185       { 
     182      {
    186183        /* Remove it from the list */
    187184        orxout(internal_warning) << (char*)(*i).peer->data << " timed out.\n";
     
    190187        /* stop iterating, we manipulated the list */
    191188        /* TODO note: this only removes one dead server per loop
    192          * iteration. not beautiful, but one iteration is ~100ms, 
     189         * iteration. not beautiful, but one iteration is ~100ms,
    193190         * so not really relevant for the moment.
    194191         */
     
    196193      }
    197194    }
    198  
     195
    199196  }
    200197
     
    204201  /***** EVENTS *****/
    205202  /* connect event */
    206   int 
     203  int
    207204  MasterServer::eventConnect( ENetEvent *event )
    208205  { /* check for bad parameters */
     
    217214
    218215    /* output debug info */
    219     orxout(verbose, context::master_server) << "A new client connected from " 
    220       << addrconv 
    221       << " on port " 
     216    orxout(verbose, context::master_server) << "A new client connected from "
     217      << addrconv
     218      << " on port "
    222219      << event->peer->address.port << endl;
    223220
    224221    /* store string form of address here */
    225     event->peer->data = addrconv; 
     222    event->peer->data = addrconv;
    226223
    227224    /* all fine. */
     
    230227
    231228  /* disconnect event */
    232   int 
     229  int
    233230  MasterServer::eventDisconnect( ENetEvent *event )
    234231  { /* check for bad parameters */
     
    255252
    256253  /* data event */
    257   int 
     254  int
    258255  MasterServer::eventData( ENetEvent *event )
    259256  { /* validate packet */
     
    262259      return -1;
    263260    }
    264      
     261
    265262    /* generate address in readable form */
    266263    char *addrconv = (char *) calloc( 50, 1 );
    267264    enet_address_get_host_ip( &(event->peer->address), addrconv, 49 );
     265    /* convert to string */
     266    std::string ip = std::string( addrconv );
     267    /* delete addrconv */
     268    if( addrconv ) free( addrconv );
     269
     270    /* pointer to full packet data */
     271    char * packetdata = (char *)event->packet->data;
    268272
    269273    /* output debug info about the data that has come */
     
    271275
    272276    /* GAME SERVER OR CLIENT CONNECTION? */
    273     if( !strncmp( (char *)event->packet->data, MSPROTO_GAME_SERVER,
    274       MSPROTO_GAME_SERVER_LEN ) )
     277    if( !strncmp(packetdata, MSPROTO_GAME_SERVER, MSPROTO_GAME_SERVER_LEN ) )
    275278    { /* Game server */
    276279
    277       if( !strncmp( (char *)event->packet->data
    278         + MSPROTO_GAME_SERVER_LEN+1,
    279         MSPROTO_REGISTER_SERVER, MSPROTO_REGISTER_SERVER_LEN ) )
     280      if( !strncmp( packetdata + MSPROTO_GAME_SERVER_LEN+1, MSPROTO_REGISTER_SERVER, MSPROTO_REGISTER_SERVER_LEN ) )
    280281      { /* register new server */
    281         mainlist.addServer( packet::ServerInformation( event ),
    282           event->peer );
    283        
     282        mainlist.addServer( packet::ServerInformation( event ), event->peer );
     283
    284284        /* tell people we did so */
    285         orxout(internal_info, context::master_server) << "Added new server to list: " << 
     285        orxout(internal_info, context::master_server) << "Added new server to list: " <<
    286286          packet::ServerInformation( event ).getServerIP() << endl;
    287287      }
    288288
    289       else if( !strncmp( (char *)event->packet->data
    290         + MSPROTO_GAME_SERVER_LEN+1,
    291         MSPROTO_SERVERDC, MSPROTO_SERVERDC_LEN ) )
    292       {
     289      else if( !strncmp( packetdata + MSPROTO_GAME_SERVER_LEN+1, MSPROTO_SERVERDC, MSPROTO_SERVERDC_LEN ) )
     290      { /* disconnect server */
     291
     292        /* remove the server from the list it belongs to */
     293        this->mainlist.delServerByAddress( ip );
     294
     295        /* tell the user */
     296        orxout(internal_info, context::master_server) << "Removed server " << ip << " from list." << endl;
     297      }
     298      /* TODO add hook for disconnect here */
     299
     300      else if( !strncmp( packetdata + MSPROTO_GAME_SERVER_LEN+1, MSPROTO_SET_NAME, MSPROTO_SET_NAME_LEN ) )
     301      { /* save server name */
    293302        /* create string from peer data */
    294         std::string name = std::string( addrconv );
     303        std::string data (event->packet->data,event->packet->data + event->packet->dataLength );
     304        std::string name = data.substr(MSPROTO_GAME_SERVER_LEN+1 + MSPROTO_SET_NAME_LEN + 1);
    295305
    296306        /* remove the server from the list it belongs to */
    297         this->mainlist.delServerByAddress( name );
     307        this->mainlist.setNameByAddress( ip, name );
    298308
    299309        /* tell the user */
    300         orxout(internal_info, context::master_server) << "Removed server " << name << " from list." << endl;
    301       }
    302 
    303       /* TODO add hook for disconnect here */
    304     }
    305     else if( !strncmp( (char *)event->packet->data, MSPROTO_CLIENT,
    306       MSPROTO_CLIENT_LEN) )
     310        orxout(internal_info, context::master_server) << "Updated server " << ip << " with new name " << name << endl;
     311      }
     312
     313      else if( !strncmp( packetdata + MSPROTO_GAME_SERVER_LEN+1, MSPROTO_SET_CLIENTS, MSPROTO_SET_CLIENTS_LEN ) )
     314      { /* save client count from server */
     315        /* create string from peer data */
     316        std::string data (event->packet->data,event->packet->data + event->packet->dataLength );
     317        std::string textform= data.substr(MSPROTO_GAME_SERVER_LEN + 1 + MSPROTO_SET_CLIENTS_LEN + 1);
     318        int clientNumber = Ogre::StringConverter::parseInt(textform);
     319
     320        this->mainlist.setClientsByAddress( ip, clientNumber);
     321
     322        /* tell the user */
     323        orxout(internal_info, context::master_server) << "Updated server " << ip << " with new client number " << clientNumber << endl;
     324      }
     325    }
     326    else if( !strncmp( packetdata, MSPROTO_CLIENT, MSPROTO_CLIENT_LEN) )
    307327    { /* client */
    308       if( !strncmp( (char *)event->packet->data + MSPROTO_CLIENT_LEN+1,
    309         MSPROTO_REQ_LIST, MSPROTO_REQ_LIST_LEN ) )
     328      if( !strncmp( packetdata + MSPROTO_CLIENT_LEN+1, MSPROTO_REQ_LIST, MSPROTO_REQ_LIST_LEN ) )
    310329        /* send server list */
    311330        helper_sendlist( event );
    312331    }
    313332    else
    314     { /* bad message, don't do anything. */ }
    315 
    316     /* delete addrconv */
    317     if( addrconv ) free( addrconv );
     333    { /* bad message, don't do anything. */ }
    318334
    319335    /* Clean up the packet now that we're done using it. */
     
    324340
    325341  /**** MAIN ROUTINE *****/
    326   int 
     342  int
    327343  MasterServer::run()
    328344  {
     
    330346    ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
    331347    if( event == NULL )
    332     { 
     348    {
    333349      orxout(user_error, context::master_server) << "Could not create ENetEvent structure, exiting." << endl;
    334350      exit( EXIT_FAILURE );
     
    345361    switch (event->type)
    346362    { /* new connection */
    347       case ENET_EVENT_TYPE_CONNECT: 
     363      case ENET_EVENT_TYPE_CONNECT:
    348364        eventConnect( event ); break;
    349365
    350366        /* disconnect */
    351       case ENET_EVENT_TYPE_DISCONNECT: 
     367      case ENET_EVENT_TYPE_DISCONNECT:
    352368        eventDisconnect( event ); break;
    353369
     
    358374
    359375    /* done */
     376    free(event);
    360377    return 0;
    361   } 
     378  }
    362379
    363380  /* constructor */
     
    380397    this->address.port = ORX_MSERVER_PORT;
    381398
    382     /* create a host with the above settings (the last two 0 mean: accept 
     399    /* create a host with the above settings (the last two 0 mean: accept
    383400     * any input/output bandwidth */
    384     this->server = enet_host_create( &this->address, ORX_MSERVER_MAXCONNS, 
     401    this->server = enet_host_create( &this->address, ORX_MSERVER_MAXCONNS,
    385402        ORX_MSERVER_MAXCHANS, 0, 0 );
    386403    assert(this->server);
     
    388405    /* see if creation worked */
    389406    if( !this->server )
    390     { orxout(user_error, context::master_server) << 
     407    { orxout(user_error, context::master_server) <<
    391408        "An error occurred while trying to create an ENet server host." << endl;
    392409      exit( EXIT_FAILURE );
  • code/branches/presentationFS15/src/libraries/network/MasterServer.h

    r8937 r10497  
    4949#include <cstdio>
    5050
    51 namespace orxonox
     51#include <OgreStringConverter.h>
     52
     53
     54namespace orxonox
    5255{
    5356  /* singleton */
     
    6366      /* static pointer for commands */
    6467      static MasterServer *instance;
    65       static MasterServer *getInstance() 
     68      static MasterServer *getInstance()
    6669        { return instance; }
    67       static void setInstance( MasterServer *setto ) 
     70      static void setInstance( MasterServer *setto )
    6871        { instance = setto;  }
    69      
     72
    7073      /* functions for commands */
    7174      static void listServers( void );
  • code/branches/presentationFS15/src/libraries/network/MasterServerProtocol.h

    r8351 r10497  
    3737/* Client token (shows that the party sending data is a client */
    3838#define MSPROTO_CLIENT "CL"
    39 #define MSPROTO_CLIENT_LEN 2 
     39#define MSPROTO_CLIENT_LEN 2
    4040
    4141/* Request: Serverlist (requiest made from client to master server */
    4242#define MSPROTO_REQ_LIST "REQ:LIST"
    4343#define MSPROTO_REQ_LIST_LEN 8
    44 
    45 
    4644
    4745
     
    6765/* ping request from server */
    6866#define MSPROTO_PING_GAMESERVER "PING"
    69 #define MSPROTO_PING_GAMESERVER_LEN 4 
     67#define MSPROTO_PING_GAMESERVER_LEN 4
    7068
    7169/* server disconnect */
     
    7573/* ping reply */
    7674#define MSPROTO_ACK "ACK"
    77 #define MSPROTO_ACK_LEN 3 
     75#define MSPROTO_ACK_LEN 3
    7876
     77/* server name */
     78#define MSPROTO_SET_NAME "NAM"
     79#define MSPROTO_SET_NAME_LEN 3
    7980
     81/* server number of clients */
     82#define MSPROTO_SET_CLIENTS "CLI"
     83#define MSPROTO_SET_CLIENTS_LEN 3
     84
     85#define SERVER_NAME_MAXLEN 7
    8086
    8187/* default master server port */
  • code/branches/presentationFS15/src/libraries/network/PeerList.cc

    r8858 r10497  
    3838  PeerList::~PeerList() { }
    3939
    40   int 
     40  int
    4141  PeerList::addPeer( ENetPeer *toadd )
    4242  { /* error correction */
    43     if( toadd == NULL ) 
     43    if( toadd == NULL )
    4444    { orxout(internal_error, context::master_server) << "PeerList::addPeer: empty peer given." << endl;
    4545      return -1;
     
    5252
    5353  bool sub_compAddr( ENetAddress addr1, ENetAddress addr2 )
    54   { 
     54  {
    5555    for( int i = 0; i < 16; ++i )
    5656      if( addr1.host.addr[i] < addr2.host.addr[i] )
     
    6161    return 0;
    6262  }
    63    
     63
    6464
    6565  bool
     
    6969
    7070    /* loop through list elements */
    71     for( i = peerlist.begin(); i != peerlist.end(); ++i ) 
     71    for( i = peerlist.begin(); i != peerlist.end(); ++i )
    7272      if( !sub_compAddr((*i)->address, addr ) )
    7373      { /* found this name, remove and quit */
     
    8686
    8787    /* loop through list elements */
    88     for( i = peerlist.begin(); i != peerlist.end(); ++i ) 
     88    for( i = peerlist.begin(); i != peerlist.end(); ++i )
    8989      if( !sub_compAddr((*i)->address, addr ) )
    9090        /* found this name, remove and quit */
     
    9595  }
    9696
     97  int
     98  PeerList::count(){
     99    return this->peerlist.size();
     100  }
     101
    97102}
    98 
  • code/branches/presentationFS15/src/libraries/network/PeerList.h

    r8351 r10497  
    3535
    3636/* peer list */
    37 namespace orxonox 
     37namespace orxonox
    3838{
    39   /** This class keeps a list of open connections 
     39  /** This class keeps a list of open connections
    4040   * and some info about them.
    4141   */
    42   class PeerList 
     42  class PeerList
    4343  { public:
    4444      /** constructor */
     
    5050      /** \param toadd The peer to add
    5151       * \return 0 for success, -1 for error.
    52        * 
    53        * Add new peer to list 
     52       *
     53       * Add new peer to list
    5454       */
    5555      int addPeer( ENetPeer *toadd );
     
    5757      /** \param addr Address to look for
    5858       * \return if the peer was found or not
    59        * 
    60        * Remove peer from list by address 
     59       *
     60       * Remove peer from list by address
    6161       */
    6262      bool remPeerByAddr( ENetAddress addr );
    6363
    6464      /** \param addr The address to find by
    65        *
    66        * Find a connection by address */
     65       *
     66       * Find a connection by address
     67       */
    6768      ENetPeer *findPeerByAddr( ENetAddress addr );
    6869
    69       /* NOTE: making this list public so it can easily
     70      /**
     71       * Count current peers
     72       */
     73       int count();
     74
     75      /* NOTE: making this list public so it can easily
    7076       * be iterated. This makes sense since iterating it
    7177       * will happen all the time, and using getter methods
  • code/branches/presentationFS15/src/libraries/network/Server.cc

    r9667 r10497  
    7777    this->timeSinceLastUpdate_=0;
    7878  }
    79 
     79/*
     80  Server::Server(int port, const std::string name)
     81  {
     82    this->setPort( port );
     83    this->timeSinceLastUpdate_=0;
     84    this->serverName_=name;
     85  }*/
    8086  /**
    8187  * Constructor
     
    108114    /* make discoverable on LAN */
    109115    LANDiscoverable::setActivity(true);
     116    LANDiscoverable::updateClientNumber(0);
    110117
    111118    /* make discoverable on WAN */
    112119    WANDiscoverable::setActivity(true);
     120    WANDiscoverable::updateClientNumber(0);
    113121
    114122    /* done */
     
    283291    // inform all the listeners
    284292    this->clientIDs_.push_back(peerID);
     293    WANDiscoverable::updateClientNumber(this->clientIDs_.size());
     294    LANDiscoverable::updateClientNumber(this->clientIDs_.size());
     295
    285296    ClientConnectionListener::broadcastClientConnected(peerID);
    286297    GamestateManager::addPeer(peerID);
     
    289300
    290301    orxout(internal_info, context::network) << "Server: added client id: " << peerID << endl;
     302
    291303    createClient(peerID);
    292304}
     
    309321    }
    310322  }
     323  WANDiscoverable::updateClientNumber(this->clientIDs_.size());
     324  LANDiscoverable::updateClientNumber(this->clientIDs_.size());
     325
    311326  ClientConnectionListener::broadcastClientDisconnected(peerID);
    312327  GamestateManager::removePeer(peerID);
  • code/branches/presentationFS15/src/libraries/network/Server.h

    r8858 r10497  
    6767    virtual void printRTT();
    6868    float getPacketLoss(unsigned int clientID);
     69    int getClientCount() { return this->clientIDs_.size();}
     70    std::string getServerName() { return this->serverName_;}
     71
    6972  protected:
    7073    void updateGamestate();
     
    8992    std::deque<packet::Packet*> packetQueue_;
    9093    std::vector<uint32_t>       clientIDs_;
     94    std::string                 serverName_;
    9195  };
    9296
  • code/branches/presentationFS15/src/libraries/network/ServerList.cc

    r8937 r10497  
    3030
    3131namespace orxonox
    32 { 
     32{
    3333  ServerList::ServerList()
    3434  { /* create a new list */ }
     
    3939  }
    4040
    41   int 
     41  int
    4242  ServerList::addServer( packet::ServerInformation toadd,
    4343    ENetPeer *peer )
    44   { 
     44  {
    4545    ServerListElem toAdd;
    4646    toAdd.ServerInfo = toadd;
    4747    toAdd.peer = peer;
    4848
    49     this->serverlist.push_back( toAdd ); 
     49    this->serverlist.push_back( toAdd );
    5050    return 0;
    5151  }
    5252
    53   bool 
     53  bool
    5454  ServerList::delServerByName( std::string name )
    55   { 
     55  {
    5656    /* get an iterator */
    5757    std::list<ServerListElem>::iterator i;
    5858
    5959    /* loop through list elements */
    60     for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
     60    for( i = serverlist.begin(); i != serverlist.end(); ++i )
    6161      if( (*i).ServerInfo.getServerName() == name )
    6262      { /* found this name, remove and quit */
     
    6868
    6969  bool ServerList::delServerByAddress( std::string address )
    70   { 
     70  {
    7171    /* get an iterator */
    7272    std::list<ServerListElem>::iterator i;
    7373
    7474    /* loop through list elements */
    75     for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
     75    for( i = serverlist.begin(); i != serverlist.end(); ++i )
    7676      if( (*i).ServerInfo.getServerIP() == address )
    7777      { /* found this name, remove and quit */
     
    9090
    9191    /* loop through list elements */
    92     for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
     92    for( i = serverlist.begin(); i != serverlist.end(); ++i )
    9393      if( (*i).ServerInfo.getServerIP() == address )
    9494      { /* found the target, return it */
     
    110110    /* iterate, return when name found */
    111111    /* loop through list elements */
    112     for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
     112    for( i = serverlist.begin(); i != serverlist.end(); ++i )
    113113      if( (*i).ServerInfo.getServerName() == name )
    114       { 
     114      {
    115115        ServerListSearchResult res = { (*i), true };
    116116        return res;
     
    124124  /* SORTING */
    125125  /* sort by name */
    126   bool sub_compare_names( ServerListElem no1, 
     126  bool sub_compare_names( ServerListElem no1,
    127127    ServerListElem no2 )
    128128  { return no1.ServerInfo.getServerName() > no2.ServerInfo.getServerName(); }
    129129
    130130  void ServerList::sortByName()
    131   { 
    132     this->serverlist.sort( sub_compare_names ); 
     131  {
     132    this->serverlist.sort( sub_compare_names );
    133133  }
    134  
     134
    135135  /* sort by ping */
    136   bool sub_compare_pings( ServerListElem no1, 
     136  bool sub_compare_pings( ServerListElem no1,
    137137    ServerListElem no2 )
    138   { 
    139     return no1.ServerInfo.getServerName() > no2.ServerInfo.getServerName();
     138  {
     139    return no1.ServerInfo.getServerRTT() > no2.ServerInfo.getServerRTT();
    140140  }
    141141
    142142  void ServerList::sortByPing()
    143143  {
    144     this->serverlist.sort( sub_compare_pings ); 
     144    this->serverlist.sort( sub_compare_pings );
    145145  }
    146146
     147  bool ServerList::setNameByAddress( std::string address, std::string name  ){
     148    /* get an iterator */
     149    std::list<ServerListElem>::iterator i;
     150
     151    /* loop through list elements */
     152    for( i = serverlist.begin(); i != serverlist.end(); ++i )
     153      if( (*i).ServerInfo.getServerIP() == address )
     154      { /* found this adress, rename and quit */
     155        (*i).ServerInfo.setServerName( name );
     156        return true;
     157      }
     158    return false;
     159  };
     160
     161  bool ServerList::setClientsByAddress( std::string address, int clientNumber ){
     162    /* get an iterator */
     163    std::list<ServerListElem>::iterator i;
     164
     165    /* loop through list elements */
     166    for( i = serverlist.begin(); i != serverlist.end(); ++i )
     167      if( (*i).ServerInfo.getServerIP() == address )
     168      { /* found this adress, rename and quit */
     169        (*i).ServerInfo.setClientNumber( clientNumber );
     170        return true;
     171      }
     172    return false;
     173  };
     174
    147175}
  • code/branches/presentationFS15/src/libraries/network/ServerList.h

    r8937 r10497  
    3535
    3636/* methods necessary */
    37 namespace orxonox 
    38 { 
     37namespace orxonox
     38{
    3939  /* HELPER STRUCTURES */
    40   struct ServerListElem 
     40  struct ServerListElem
    4141  {
    4242    /* server information (name, IP, etc) */
     
    6363   * and some info about them.
    6464   */
    65   class ServerList 
     65  class ServerList
    6666  { public:
    6767      /** constructor */
     
    7474      /* BASIC MANIPULATION */
    7575      /** \param toadd the server to add.
    76        * 
     76       *
    7777       * Add server to the game server list
    7878       */
     
    8181
    8282      /** \param name Name of the server to remove
    83        * 
    84        * Remove server by name 
     83       *
     84       * Remove server by name
    8585       */
    8686      bool delServerByName( std::string name );
    8787
    8888      /** \param address IP address of the server to remove
    89        * 
     89       *
    9090       * Remove server by address
    9191       */
    9292      bool delServerByAddress( std::string address );
    9393
     94      bool setNameByAddress( std::string address, std::string name  );
    9495
    95 
     96      bool setClientsByAddress( std::string address, int clientNumber );
    9697
    9798      /* SEARCHING */
    98       /* \param address The address of the server that is to be 
     99      /* \param address The address of the server that is to be
    99100       *  found
    100101       * \return A struct containing a result of the search and a boolean
    101102       *  that is only true if the search was successful
    102        * 
     103       *
    103104       * Find and return the list handle of a given address.
    104105       */
     
    107108
    108109
    109       /* \param name The name of the server that is to be 
     110      /* \param name The name of the server that is to be
    110111       *  found
    111112       * \return The struct containing the list entry of the server
    112        * 
     113       *
    113114       * Find and return the list handle of a given name.
    114115       */
     
    120121      /** sort by name  */
    121122      void sortByName();
    122      
     123
    123124      /** sort by ping */
    124125      void sortByPing();
  • code/branches/presentationFS15/src/libraries/network/WANDiscoverable.cc

    r9667 r10497  
    4444    /* debugging output */
    4545    orxout(verbose, context::master_server) << "Creating WANDiscoverable." << endl;
    46  
     46
    4747    /* register object in orxonox */
    4848    RegisterObject(WANDiscoverable);
     
    5050    /* check for the masterserver address option in orxonox.ini */
    5151    this->setConfigValues();
    52    
     52
    5353  }
    5454
    5555  void WANDiscoverable::setConfigValues()
    5656  {
    57     /* update msaddress string from orxonox.ini config file, if it 
    58      * has changed. 
     57    /* update msaddress string from orxonox.ini config file, if it
     58     * has changed.
    5959     */
    60     SetConfigValueExternal(msaddress, "WANDiscovery", "msaddress", "orxonox.net");
    61 //     SetConfigValue( msaddress, "orxonox.net");
    62   } 
     60    SetConfigValueExternal(msaddress, "Discovery", "msaddress", "orxonox.net");
     61    SetConfigValueExternal(ownName, "Discovery", "ownName", "OrxServer");
     62  }
    6363
    6464  WANDiscoverable::~WANDiscoverable()
     
    6767      this->disconnect();
    6868  }
    69  
     69
    7070  void WANDiscoverable::setActivity(bool bActive)
    7171  {
    7272    if( bActive==this->bActive_ )
    7373      return;
    74    
     74
    7575    if( bActive )
    7676    {
     
    8484    }
    8585  }
    86  
     86
    8787  bool WANDiscoverable::connect()
    8888  {
     
    9393      return false;
    9494    }
    95    
     95
    9696    /* connect and see if it worked */
    9797    if( msc.connect( this->msaddress.c_str(), ORX_MSERVER_PORT ) )
    9898    {
    99       orxout(internal_error, context::master_server) << "Could not connect to master server at " 
     99      orxout(internal_error, context::master_server) << "Could not connect to master server at "
    100100                 << this->msaddress << endl;
    101101      return false;
    102102    }
    103                  
     103
    104104    /* debugging output */
    105105    orxout(verbose, context::master_server) << "Initialization of WANDiscoverable complete." << endl;
    106    
    107    
     106
    108107    // Now register the server at the master server
    109108    this->msc.sendRequest( MSPROTO_GAME_SERVER " " MSPROTO_REGISTER_SERVER );
    110    
     109
     110    std::string request = MSPROTO_GAME_SERVER " " MSPROTO_SET_NAME " ";
     111    request += this->ownName;
     112    this->msc.sendRequest( request );
     113
    111114    return true;
    112115  }
     
    118121  }
    119122
     123  void WANDiscoverable::updateClientNumber(int clientNumber)
     124  {
     125    orxout(verbose, context::master_server) << "Sending new number of clients: " << clientNumber << endl;
     126    std::string request = MSPROTO_GAME_SERVER " " MSPROTO_SET_CLIENTS " ";
     127    request += Ogre::StringConverter::toString(clientNumber);
     128
     129    this->msc.sendRequest( request );
     130  }
    120131
    121132
    122  
     133
     134
    123135} // namespace orxonox
  • code/branches/presentationFS15/src/libraries/network/WANDiscoverable.h

    r9667 r10497  
    3232#include "core/config/Configurable.h"
    3333#include "MasterServerComm.h"
     34#include <OgreStringConverter.h>
    3435
    3536namespace orxonox
     
    4647
    4748      /** \return Address of the master server
    48        * 
    49        * Get the master server address 
     49       *
     50       * Get the master server address
    5051       */
    5152      std::string getMSAddress()
     
    5455      /** Function used for the configuration file parameter update */
    5556      void setConfigValues();
    56      
     57
    5758      /** Function used to set the activity/discoverability */
    5859      void setActivity( bool bActive );
    5960
     61      void updateClientNumber(int clientNumber);
     62
    6063      /** Master server communications object */
    6164      MasterServerComm msc;
    62      
     65
    6366    private:
    6467      /** Function used to connect to the master server */
    6568      bool connect();
    66      
     69
    6770      /** Function used to disconnect from the master server */
    6871      void disconnect();
    69      
     72
    7073      /** master server address */
    7174      std::string msaddress;
     75      std::string ownName;
    7276      bool        bActive_;
    7377
  • code/branches/presentationFS15/src/libraries/network/WANDiscovery.cc

    r8858 r10497  
    4141    /* debugging output */
    4242    orxout(verbose, context::master_server) << "Creating WANDiscovery." << endl;
    43  
     43
    4444    /* register object in orxonox */
    4545    RegisterObject(WANDiscovery);
     
    5454    /* connect and see if it worked */
    5555    if( msc.connect( this->msaddress.c_str(), ORX_MSERVER_PORT ) )
    56       orxout(internal_error, context::master_server) << "Could not connect to master server at " 
     56      orxout(internal_error, context::master_server) << "Could not connect to master server at "
    5757        << this->msaddress << endl;
    5858
     
    6363  void WANDiscovery::setConfigValues()
    6464  {
    65     /* update msaddress string from orxonox.ini config file, if it 
    66      * has changed. 
     65    /* update msaddress string from orxonox.ini config file, if it
     66     * has changed.
    6767     */
    6868    SetConfigValue( msaddress, "master.orxonox.net");
    69   } 
     69  }
    7070
    7171  WANDiscovery::~WANDiscovery()
    72   { 
     72  {
    7373    /* clear server list */
    74     this->servers_.clear(); 
     74    this->servers_.clear();
    7575  }
    7676
    7777  /* callback for the network reply poller */
    7878  int WANDiscovery::rhandler( char *addr, ENetEvent *ev )
    79   { 
     79  {
    8080    /* error recognition */
    8181    if( !ev || !ev->packet || !ev->packet->data )
     
    8888    if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_ITEM,
    8989      MSPROTO_SERVERLIST_ITEM_LEN ) )
    90     { 
     90    {
    9191      /* create server structure from that item */
    9292      packet::ServerInformation toadd;
    9393
    9494      /* fill in data, -1 for the index: index should be length -1 */
    95       toadd.setServerName( std::string((char*)ev->packet->data +
    96         MSPROTO_SERVERLIST_ITEM_LEN+1) );
    97       toadd.setServerIP( std::string((char*)ev->packet->data +
    98         MSPROTO_SERVERLIST_ITEM_LEN+1) );
     95      std::string datastr = std::string((char*)ev->packet->data + MSPROTO_SERVERLIST_ITEM_LEN+1);
     96      int separator = datastr.find(" ");
     97      toadd.setServerIP(datastr.substr(0,separator));
     98      int secondsep = datastr.find(" ", separator + 1);
     99      toadd.setServerName(datastr.substr(separator + 1, secondsep - separator - 1));
     100      toadd.setClientNumber(Ogre::StringConverter::parseInt(datastr.substr(secondsep+1)));
     101
     102      orxout(internal_info, context::network) << "Received WAN discovery server information; Name: " << toadd.getServerName() << ", Address: " << toadd.getServerIP() << ", Players: " << toadd.getClientNumber() << ", RTT: " << toadd.getServerRTT() << endl;
    99103
    100104      /* add to list */
     
    103107    else if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_END,
    104108      MSPROTO_SERVERLIST_END_LEN ) )
    105     { 
     109    {
    106110      /* this is the only case where 2 should be returned,
    107111       * as 1 is used to signal that we're done receiving
    108112       * the list
    109113       */
    110       return 2; 
     114      return 2;
    111115    }
    112116
     
    114118    return 1;
    115119  }
    116  
     120
    117121  void WANDiscovery::discover()
    118122  {
     
    131135      { case 0: /* no event occured, decrease timeout */
    132136          --i; break;
    133         case 1: /* got a list element, continue */ 
     137        case 1: /* got a list element, continue */
    134138          break;
    135139        case 2: /* done. */
     
    159163  }
    160164
     165  std::string WANDiscovery::getServerListItemRTT(unsigned int index)
     166        {
     167    if( index >= this->servers_.size() )
     168      return BLANKSTRING;
     169    else{
     170      uint32_t serverrtt = this->servers_[index].getServerRTT();
     171      return Ogre::StringConverter::toString(serverrtt);
     172    }
     173
     174  }
     175  std::string WANDiscovery::getServerListItemPlayerNumber(unsigned int index)
     176  {
     177    if( index >= this->servers_.size() )
     178      return BLANKSTRING;
     179    else{
     180      int playerNumber = this->servers_[index].getClientNumber();
     181      return Ogre::StringConverter::toString(playerNumber);
     182    }
     183  }
    161184
    162185} // namespace orxonox
  • code/branches/presentationFS15/src/libraries/network/WANDiscovery.h

    r9667 r10497  
    3535#include "MasterServerComm.h"
    3636#include "MasterServerProtocol.h"
     37#include <OgreStringConverter.h>
    3738
    3839#include <vector>
     
    5657
    5758      /** \return Address of the master server
    58        * 
    59        * Get the master server address 
     59       *
     60       * Get the master server address
    6061       */
    6162      std::string getMSAddress()
     
    6566      void discover(); // tolua_export
    6667
    67       /** \param index Index to get the name of 
     68      /** \param index Index to get the name of
    6869       * \return The name of the server
    69        * 
    70        * Get the name of the server at index index. 
     70       *
     71       * Get the name of the server at index index.
    7172       */
    7273      std::string getServerListItemName( unsigned int index ); // tolua_export
    7374
    74       /** \param index Index to get the IP of 
     75      /** \param index Index to get the IP of
    7576       * \return The IP of the server
    76        * 
    77        * Get the IP of the server at index index. 
     77       *
     78       * Get the IP of the server at index index.
    7879       */
    7980      std::string getServerListItemIP( unsigned int index ); // tolua_export
    8081
     82      /** \param index Index to get the RTT of
     83       * \return The RTT of the server
     84       *
     85       * Get the RTT of the server at index index.
     86       */
     87      std::string getServerListItemRTT( unsigned int index ); // tolua_export
     88
     89      /** \param index Index to get the RTT of
     90       * \return The number of players on the server
     91       *
     92       * Get the number of players on the server
     93       */
     94      std::string getServerListItemPlayerNumber( unsigned int index ); // tolua_export
     95
    8196      /* todo: might make this private and use getter/setter methods
    82        * at some later time. 
     97       * at some later time.
    8398       */
    8499      /** game server list */
     
    92107
    93108      int rhandler( char *addr, ENetEvent *ev );
    94      
     109
    95110    private:
    96111      /** master server address */
  • code/branches/presentationFS15/src/libraries/network/packet/ServerInformation.cc

    r8351 r10497  
    3939  namespace packet
    4040  {
    41    
     41
    4242    ServerInformation::ServerInformation()
    4343    {
    44      
    4544    }
    46    
     45
    4746    ServerInformation::ServerInformation(ENetEvent* event)
    4847    {
     
    6968    ServerInformation::~ServerInformation()
    7069    {
    71      
     70
    7271    }
    7372
    7473    void ServerInformation::send(ENetPeer* peer)
    7574    {
    76       uint32_t size = returnSize((char*&)LAN_DISCOVERY_ACK) + returnSize(this->serverName_);
     75      std::string payload = this->serverName_ + Ogre::StringConverter::toString(this->clientNumber_);
     76      uint32_t size = returnSize((char*&)LAN_DISCOVERY_ACK) + returnSize(payload);
    7777      uint8_t* temp = new uint8_t[size];
    7878      uint8_t* temp2 = temp;
    7979      saveAndIncrease((char*&)LAN_DISCOVERY_ACK, temp2);
    80       saveAndIncrease(this->serverName_, temp2);
     80      saveAndIncrease(payload, temp2);
    8181      ENetPacket* packet = enet_packet_create( temp, size, 0 );
    8282      enet_peer_send(peer, 0, packet);
    83      
     83
    8484      delete[] temp;
    8585    }
    86  
     86
    8787  } // namespace packet
    8888
     
    9595  }
    9696} // namespace orxonox
    97 
  • code/branches/presentationFS15/src/libraries/network/packet/ServerInformation.h

    r8351 r10497  
    3030
    3131#include <string>
     32#include <OgreStringConverter.h>
     33
    3234
    3335#ifndef SERVERINFORMATION_H
     
    4547        ServerInformation(ENetEvent* event);
    4648        ~ServerInformation();
    47        
     49
    4850        void          send( ENetPeer* peer );
     51        void          setServerName(std::string name) { this->serverName_ = name; }
     52        std::string   getServerName() { return this->serverName_; }
     53        void          setServerIP( std::string IP ) { this->serverIP_ = IP; }
    4954        std::string   getServerIP() { return this->serverIP_; }
    50         std::string   getServerName() { return this->serverName_; }
    51         void          setServerName(std::string name) { this->serverName_ = name; }
    52         void          setServerIP( std::string IP ) { this->serverIP_ = IP; }
     55        void          setClientNumber( int clientNumber ) { this->clientNumber_ = clientNumber; }
     56        int           getClientNumber() { return this->clientNumber_; }
    5357        uint32_t      getServerRTT() { return this->serverRTT_; }
    54        
     58
    5559      private:
    5660        std::string   serverName_;
     61        int           clientNumber_;
    5762        std::string   serverIP_;
    5863        uint32_t      serverRTT_;
Note: See TracChangeset for help on using the changeset viewer.