[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 | |
---|
| 38 | namespace orxonox |
---|
[7284] | 39 | { |
---|
[7161] | 40 | ManageScopedSingleton(LANDiscovery, ScopeID::Root, true); |
---|
[7284] | 41 | |
---|
[7161] | 42 | LANDiscovery::LANDiscovery() |
---|
| 43 | { |
---|
| 44 | this->host_ = enet_host_create(NULL, 10, 0, 0 ); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | LANDiscovery::~LANDiscovery() |
---|
| 48 | { |
---|
| 49 | enet_host_destroy(this->host_); |
---|
| 50 | } |
---|
[7284] | 51 | |
---|
[7161] | 52 | void LANDiscovery::discover() |
---|
| 53 | { |
---|
| 54 | this->servers_.clear(); |
---|
| 55 | ENetAddress address; |
---|
| 56 | enet_address_set_host(&address, "255.255.255.255"); |
---|
| 57 | address.port = LAN_DISCOVERY_PORT; |
---|
[7284] | 58 | |
---|
[7161] | 59 | ENetPeer* peer; |
---|
| 60 | peer = enet_host_connect(this->host_, &address, 0); |
---|
[7284] | 61 | |
---|
[7161] | 62 | ENetEvent event; |
---|
| 63 | while( enet_host_service(this->host_, &event, 1000 ) ) |
---|
| 64 | { |
---|
| 65 | switch( event.type ) |
---|
| 66 | { |
---|
| 67 | case ENET_EVENT_TYPE_CONNECT: |
---|
| 68 | { |
---|
| 69 | COUT(0) << "connect from server: " << event.peer->address.host << endl; |
---|
| 70 | ENetPacket* packet = enet_packet_create(LAN_DISCOVERY_MESSAGE, strlen(LAN_DISCOVERY_MESSAGE)+1, ENET_PACKET_FLAG_RELIABLE); |
---|
| 71 | enet_peer_send(event.peer, 0, packet); |
---|
| 72 | break; |
---|
| 73 | } |
---|
| 74 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 75 | { |
---|
| 76 | packet::ServerInformation info(&event); |
---|
| 77 | COUT(0) << "received server information; name: " << info.getServerName() << ", IP: " << info.getServerIP() << ", RTT: " << info.getServerRTT() << endl; |
---|
| 78 | std::vector<packet::ServerInformation>::iterator it; |
---|
| 79 | for( it=this->servers_.begin(); it!=this->servers_.end(); ++it ) |
---|
| 80 | { |
---|
| 81 | if( (*it).getServerIP() == info.getServerIP() ) |
---|
| 82 | break; |
---|
| 83 | } |
---|
| 84 | if( it==this->servers_.end() ) |
---|
| 85 | this->servers_.push_back(info); |
---|
| 86 | } |
---|
| 87 | // enet_address_get_host_ip(&event.peer->address, buffer, buflen ); |
---|
| 88 | // serverIPs.push_back(std::string(buffer)); |
---|
| 89 | break; |
---|
| 90 | default: |
---|
| 91 | break; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | } |
---|
[7284] | 95 | |
---|
[7161] | 96 | std::string LANDiscovery::getServerListItemName(unsigned int index) |
---|
| 97 | { |
---|
| 98 | if( index >= this->servers_.size() ) |
---|
| 99 | return BLANKSTRING; |
---|
| 100 | else |
---|
| 101 | return this->servers_[index].getServerName(); |
---|
| 102 | } |
---|
[7284] | 103 | |
---|
[7161] | 104 | std::string LANDiscovery::getServerListItemIP(unsigned int index) |
---|
| 105 | { |
---|
| 106 | if( index >= this->servers_.size() ) |
---|
| 107 | return BLANKSTRING; |
---|
| 108 | else |
---|
| 109 | return this->servers_[index].getServerIP(); |
---|
| 110 | } |
---|
[7284] | 111 | |
---|
| 112 | |
---|
[7161] | 113 | } // namespace orxonox |
---|