Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ipv6/src/libraries/network/LANDiscovery.cc @ 7411

Last change on this file since 7411 was 7411, checked in by adrfried, 14 years ago

more debug output added

File size: 3.8 KB
RevLine 
[7161]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "LANDiscovery.h"
30
31#include <enet/enet.h>
32#include <cstring>
33
[7284]34#include "util/ScopedSingletonManager.h"
[7161]35#include "core/CoreIncludes.h"
36
37
38namespace orxonox
[7284]39{
[7161]40  ManageScopedSingleton(LANDiscovery, ScopeID::Root, true);
[7284]41
[7161]42  LANDiscovery::LANDiscovery()
43  {
[7294]44    this->host_ = enet_host_create(NULL, 10, 0, 0, 0 );
[7402]45    if ( this->host_ == NULL )
46        COUT(1) << "LANDiscovery: host_ == NULL" << std::endl;
[7161]47  }
48
49  LANDiscovery::~LANDiscovery()
50  {
51    enet_host_destroy(this->host_);
52  }
[7284]53
[7161]54  void LANDiscovery::discover()
55  {
56    this->servers_.clear();
[7322]57    ENetPeer* peer;
[7161]58    ENetAddress address;
[7322]59    memset(&address, 0, sizeof(ENetAddress));
[7161]60    address.port = LAN_DISCOVERY_PORT;
[7284]61
[7322]62    /* TODO: check for availability of each protocol */
63    /* IPv4 */
64    address.host = ENET_HOST_BROADCAST;
[7294]65    peer = enet_host_connect(this->host_, &address, 0, 0);
[7402]66    if (peer == NULL)
67        COUT(1) << "Error: Could not send LAN discovery to IPv4 Broadcast." << std::endl;
[7284]68
[7322]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);
[7402]72    if (peer == NULL)
73        COUT(1) << "Error: Could not send LAN discovery to IPv6 Multicast." << std::endl;
[7322]74
[7161]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        {
[7411]82          COUT(4) << "connect from server: " << event.peer->address << endl;
[7161]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);
[7411]90            COUT(3) << "received server information; Name: " << info.getServerName() << ", Address: " << info.getServerIP() << ", RTT: " << info.getServerRTT() << endl;
[7161]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    }
107  }
[7284]108
[7161]109  std::string LANDiscovery::getServerListItemName(unsigned int index)
110  {
111    if( index >= this->servers_.size() )
112      return BLANKSTRING;
113    else
114      return this->servers_[index].getServerName();
115  }
[7284]116
[7161]117  std::string LANDiscovery::getServerListItemIP(unsigned int index)
118  {
119    if( index >= this->servers_.size() )
120      return BLANKSTRING;
121    else
122      return this->servers_[index].getServerIP();
123  }
[7284]124
125
[7161]126} // namespace orxonox
Note: See TracBrowser for help on using the repository browser.