Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 10, 2010, 2:19:44 PM (14 years ago)
Author:
smerkli
Message:

initiated WANDiscovery code, now implementing…

Location:
code/branches/masterserver/src
Files:
2 edited
2 copied

Legend:

Unmodified
Added
Removed
  • code/branches/masterserver/src/libraries/network/WANDiscovery.cc

    r7629 r7630  
    2727 */
    2828
    29 #include "LANDiscovery.h"
     29#include "WANDiscovery.h"
    3030
    3131#include <enet/enet.h>
     
    3838namespace orxonox
    3939{
    40   ManageScopedSingleton(LANDiscovery, ScopeID::Root, true);
     40  ManageScopedSingleton(WANDiscovery, ScopeID::Root, true);
    4141
    42   LANDiscovery::LANDiscovery()
     42  WANDiscovery::WANDiscovery()
    4343  {
    44     this->host_ = enet_host_create(NULL, 10, 0, 0, 0 );
    45     if ( this->host_ == NULL )
    46         COUT(1) << "LANDiscovery: host_ == NULL" << std::endl;
    4744  }
    4845
    49   LANDiscovery::~LANDiscovery()
     46  WANDiscovery::~WANDiscovery()
    5047  {
    51     enet_host_destroy(this->host_);
    5248  }
    5349
    54   void LANDiscovery::discover()
     50  void WANDiscovery::discover()
    5551  {
    5652    this->servers_.clear();
    57     ENetPeer* peer;
    58     ENetAddress address;
    59     memset(&address, 0, sizeof(ENetAddress));
    60     address.port = LAN_DISCOVERY_PORT;
    61 
    62     /* TODO: check for availability of each protocol */
    63     /* IPv4 */
    64     address.host = ENET_HOST_BROADCAST;
    65     peer = enet_host_connect(this->host_, &address, 0, 0);
    66     if (peer == NULL)
    67         COUT(1) << "Error: Could not send LAN discovery to IPv4 Broadcast." << std::endl;
    68 
    69     /* IPv6 */
    70     enet_address_set_host(&address, "ff02::1"); // TODO: use a multicast group
    71     peer = enet_host_connect(this->host_, &address, 0, 0);
    72     if (peer == NULL)
    73         COUT(1) << "Error: Could not send LAN discovery to IPv6 Multicast." << std::endl;
    74 
    75     ENetEvent event;
    76     while( enet_host_service(this->host_, &event, 1000 ) )
    77     {
    78       switch( event.type )
    79       {
    80         case ENET_EVENT_TYPE_CONNECT:
    81         {
    82           COUT(4) << "Received LAN discovery connect from server " << event.peer->host->receivedAddress << std::endl;
    83           ENetPacket* packet = enet_packet_create(LAN_DISCOVERY_MESSAGE, strlen(LAN_DISCOVERY_MESSAGE)+1, ENET_PACKET_FLAG_RELIABLE);
    84           enet_peer_send(event.peer, 0, packet);
    85           break;
    86         }
    87         case ENET_EVENT_TYPE_RECEIVE:
    88           {
    89             packet::ServerInformation info(&event);
    90             COUT(3) << "Received LAN discovery server information; Name: " << info.getServerName() << ", Address: " << info.getServerIP() << ", RTT: " << info.getServerRTT() << endl;
    91             std::vector<packet::ServerInformation>::iterator it;
    92             for( it=this->servers_.begin(); it!=this->servers_.end(); ++it )
    93             {
    94               if( (*it).getServerIP() == info.getServerIP() )
    95                 break;
    96             }
    97             if( it==this->servers_.end() )
    98               this->servers_.push_back(info);
    99           }
    100 //           enet_address_get_host_ip(&event.peer->address, buffer, buflen );
    101 //           serverIPs.push_back(std::string(buffer));
    102           break;
    103         default:
    104           break;
    105       }
    106     }
    10753  }
    10854
    109   std::string LANDiscovery::getServerListItemName(unsigned int index)
     55  std::string WANDiscovery::getServerListItemName(unsigned int index)
    11056  {
    111     if( index >= this->servers_.size() )
    112       return BLANKSTRING;
    113     else
    114       return this->servers_[index].getServerName();
     57
    11558  }
    11659
    117   std::string LANDiscovery::getServerListItemIP(unsigned int index)
     60  std::string WANDiscovery::getServerListItemIP(unsigned int index)
    11861  {
    119     if( index >= this->servers_.size() )
    120       return BLANKSTRING;
    121     else
    122       return this->servers_[index].getServerIP();
     62
    12363  }
    12464
  • code/branches/masterserver/src/libraries/network/WANDiscovery.h

    r7629 r7630  
    2121 *
    2222 *   Author:
    23  *      Fabian 'x3n' Landau
     23 *      Fabian 'x3n' Landau (original)
    2424 *   Co-authors:
    25  *      ...
     25 *      Sandro 'smerkli' Merkli (copied and adapted to WAN)
    2626 *
    2727 */
    2828
    29 #ifndef LANDISCOVERY_H
    30 #define LANDISCOVERY_H
     29#ifndef WANDISCOVERY_H
     30#define WANDISCOVERY_H
    3131
    3232#include "NetworkPrereqs.h"
     
    4040{
    4141
    42   class _NetworkExport LANDiscovery
     42  class _NetworkExport WANDiscovery
    4343// tolua_end
    44     : public Singleton<LANDiscovery>
     44    : public Singleton<WANDiscovery>
    4545  { // tolua_export
    46     friend class Singleton<LANDiscovery>;
     46    friend class Singleton<WANDiscovery>;
    4747    public:
    48       LANDiscovery();
    49       ~LANDiscovery();
     48      WANDiscovery();
     49      ~WANDiscovery();
    5050      void discover(); // tolua_export
    5151      std::string getServerListItemName( unsigned int index ); // tolua_export
    5252      std::string getServerListItemIP( unsigned int index ); // tolua_export
    53       static LANDiscovery& getInstance(){ return Singleton<LANDiscovery>::getInstance(); } // tolua_export
     53      static WANDiscovery& getInstance(){ return Singleton<WANDiscovery>::getInstance(); } // tolua_export
    5454     
    5555    private:
    56       static LANDiscovery* singletonPtr_s;
     56      static WANDiscovery* singletonPtr_s;
    5757      ENetHost* host_;
    5858      std::vector<packet::ServerInformation> servers_;
     
    6161} // tolua_export
    6262
    63 #endif // LANDISCOVERY_H
     63#endif // WANDISCOVERY_H
  • code/branches/masterserver/src/modules/masterserver/MasterServer.cc

    r7611 r7630  
    116116      << event->channelID << "\n";
    117117
    118     /* send some packet back for testing */
    119     /* TESTING */
    120 
    121     /* Create a reliable reply of size 7 containing "reply\0" */
    122     ENetPacket * reply = enet_packet_create ("reply",
    123         strlen ("reply") + 1,
    124         ENET_PACKET_FLAG_RELIABLE);
    125 
    126     /* Send the reply to the peer over channel id 0. */
    127     enet_peer_send( event->peer, 0, reply );
    128 
    129     /* One could just use enet_host_service() instead. */
    130     enet_host_flush( this->server );
    131 
    132     /* /TESTING */
    133 
    134     /* game server or client connection? */
    135     /* game server */
     118    //[> send some packet back for testing <]
     119    //[> TESTING <]
     120
     121    //[> Create a reliable reply of size 7 containing "reply\0" <]
     122    //ENetPacket * reply = enet_packet_create ("reply",
     123        //strlen ("reply") + 1,
     124        //ENET_PACKET_FLAG_RELIABLE);
     125
     126    //[> Send the reply to the peer over channel id 0. <]
     127    //enet_peer_send( event->peer, 0, reply );
     128
     129    //[> One could just use enet_host_service() instead. <]
     130    //enet_host_flush( this->server );
     131
     132    //[> /TESTING <]
     133
     134    /* GAME SERVER OR CLIENT CONNECTION? */
     135
     136    /* Game server */
    136137    /* parse data */
    137138    /* start actions */
    138139    /* and send reply */
     140
    139141    /* client */
    140142    /* parse data */
  • code/branches/masterserver/src/modules/masterserver/MasterServerComm.cc

    r7611 r7630  
    7676  { fprintf( stderr,
    7777        "No available peers for initiating an ENet connection.\n");
    78     exit (EXIT_FAILURE);
     78    //exit (EXIT_FAILURE);
     79    return -1;
    7980  }
    8081
     
    8788    enet_peer_reset (this->peer);
    8889    fprintf( stdout, "Connection to %s failed.", address );
    89     exit(EXIT_FAILURE);
     90    //exit(EXIT_FAILURE);
     91    return -1;
    9092  }
    9193
     
    171173  /* setup object and connect */
    172174  MasterServerComm msc = MasterServerComm();
    173   msc.connect( argv[1], 1234 );
     175  if( msc.connect( argv[1], 1234 ) )
     176    exit(EXIT_FAILURE);
    174177 
    175178  /* send some data and poll for replies */
Note: See TracChangeset for help on using the changeset viewer.