| [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: | 
|---|
| [8858] | 23 |  *      Oliver Scheuss | 
|---|
| [7161] | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
 | 29 | #include "LANDiscovery.h" | 
|---|
 | 30 |  | 
|---|
 | 31 | #include <enet/enet.h> | 
|---|
 | 32 | #include <cstring> | 
|---|
 | 33 |  | 
|---|
 | 34 | #include "core/CoreIncludes.h" | 
|---|
| [10624] | 35 | #include "core/singleton/ScopedSingletonIncludes.h" | 
|---|
| [7161] | 36 |  | 
|---|
 | 37 |  | 
|---|
 | 38 | namespace orxonox | 
|---|
| [7284] | 39 | { | 
|---|
| [10624] | 40 |   ManageScopedSingleton(LANDiscovery, ScopeID::GRAPHICS, false); | 
|---|
| [7284] | 41 |  | 
|---|
| [7161] | 42 |   LANDiscovery::LANDiscovery() | 
|---|
 | 43 |   { | 
|---|
| [7459] | 44 |     this->host_ = enet_host_create(NULL, 10, 0, 0, 0 ); | 
|---|
 | 45 |     if ( this->host_ == NULL ) | 
|---|
| [8858] | 46 |         orxout(internal_error, context::network) << "LANDiscovery: host_ == NULL" << endl; | 
|---|
| [7161] | 47 |   } | 
|---|
 | 48 |  | 
|---|
 | 49 |   LANDiscovery::~LANDiscovery() | 
|---|
 | 50 |   { | 
|---|
| [8706] | 51 |     if (this->host_ != NULL) | 
|---|
 | 52 |       enet_host_destroy(this->host_); | 
|---|
| [7161] | 53 |   } | 
|---|
| [7284] | 54 |  | 
|---|
| [7161] | 55 |   void LANDiscovery::discover() | 
|---|
 | 56 |   { | 
|---|
 | 57 |     this->servers_.clear(); | 
|---|
| [7459] | 58 |     ENetPeer* peer; | 
|---|
| [7161] | 59 |     ENetAddress address; | 
|---|
| [7459] | 60 |     memset(&address, 0, sizeof(ENetAddress)); | 
|---|
| [7161] | 61 |     address.port = LAN_DISCOVERY_PORT; | 
|---|
| [7284] | 62 |  | 
|---|
| [7459] | 63 |     /* TODO: check for availability of each protocol */ | 
|---|
 | 64 |     /* IPv4 */ | 
|---|
 | 65 |     address.host = ENET_HOST_BROADCAST; | 
|---|
 | 66 |     peer = enet_host_connect(this->host_, &address, 0, 0); | 
|---|
 | 67 |     if (peer == NULL) | 
|---|
| [8858] | 68 |         orxout(internal_error, context::network) << "Could not send LAN discovery to IPv4 Broadcast." << endl; | 
|---|
| [7284] | 69 |  | 
|---|
| [7459] | 70 |     /* IPv6 */ | 
|---|
 | 71 |     enet_address_set_host(&address, "ff02::1"); // TODO: use a multicast group | 
|---|
 | 72 |     peer = enet_host_connect(this->host_, &address, 0, 0); | 
|---|
 | 73 |     if (peer == NULL) | 
|---|
| [8858] | 74 |         orxout(internal_error, context::network) << "Could not send LAN discovery to IPv6 Multicast." << endl; | 
|---|
| [7459] | 75 |  | 
|---|
| [7161] | 76 |     ENetEvent event; | 
|---|
 | 77 |     while( enet_host_service(this->host_, &event, 1000 ) ) | 
|---|
 | 78 |     { | 
|---|
 | 79 |       switch( event.type ) | 
|---|
 | 80 |       { | 
|---|
 | 81 |         case ENET_EVENT_TYPE_CONNECT: | 
|---|
 | 82 |         { | 
|---|
| [8858] | 83 |           orxout(verbose, context::network) << "Received LAN discovery connect from server " << event.peer->host->receivedAddress << endl; | 
|---|
| [7161] | 84 |           ENetPacket* packet = enet_packet_create(LAN_DISCOVERY_MESSAGE, strlen(LAN_DISCOVERY_MESSAGE)+1, ENET_PACKET_FLAG_RELIABLE); | 
|---|
 | 85 |           enet_peer_send(event.peer, 0, packet); | 
|---|
 | 86 |           break; | 
|---|
 | 87 |         } | 
|---|
 | 88 |         case ENET_EVENT_TYPE_RECEIVE: | 
|---|
 | 89 |           { | 
|---|
 | 90 |             packet::ServerInformation info(&event); | 
|---|
| [10622] | 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; | 
|---|
| [7161] | 95 |             std::vector<packet::ServerInformation>::iterator it; | 
|---|
 | 96 |             for( it=this->servers_.begin(); it!=this->servers_.end(); ++it ) | 
|---|
 | 97 |             { | 
|---|
 | 98 |               if( (*it).getServerIP() == info.getServerIP() ) | 
|---|
 | 99 |                 break; | 
|---|
 | 100 |             } | 
|---|
 | 101 |             if( it==this->servers_.end() ) | 
|---|
 | 102 |               this->servers_.push_back(info); | 
|---|
 | 103 |           } | 
|---|
 | 104 | //           enet_address_get_host_ip(&event.peer->address, buffer, buflen ); | 
|---|
 | 105 | //           serverIPs.push_back(std::string(buffer)); | 
|---|
 | 106 |           break; | 
|---|
 | 107 |         default: | 
|---|
 | 108 |           break; | 
|---|
 | 109 |       } | 
|---|
 | 110 |     } | 
|---|
 | 111 |   } | 
|---|
| [7284] | 112 |  | 
|---|
| [7161] | 113 |   std::string LANDiscovery::getServerListItemName(unsigned int index) | 
|---|
 | 114 |   { | 
|---|
 | 115 |     if( index >= this->servers_.size() ) | 
|---|
 | 116 |       return BLANKSTRING; | 
|---|
 | 117 |     else | 
|---|
 | 118 |       return this->servers_[index].getServerName(); | 
|---|
 | 119 |   } | 
|---|
| [7284] | 120 |  | 
|---|
| [7161] | 121 |   std::string LANDiscovery::getServerListItemIP(unsigned int index) | 
|---|
 | 122 |   { | 
|---|
 | 123 |     if( index >= this->servers_.size() ) | 
|---|
 | 124 |       return BLANKSTRING; | 
|---|
 | 125 |     else | 
|---|
 | 126 |       return this->servers_[index].getServerIP(); | 
|---|
 | 127 |   } | 
|---|
| [7284] | 128 |  | 
|---|
| [10622] | 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 |   } | 
|---|
| [7284] | 138 |  | 
|---|
| [10622] | 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 |   } | 
|---|
| [7161] | 148 | } // namespace orxonox | 
|---|