[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 | * Oliver Scheuss |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "LANDiscoverable.h" |
---|
| 30 | |
---|
| 31 | #include <enet/enet.h> |
---|
| 32 | #include <cassert> |
---|
| 33 | #include <cstring> |
---|
| 34 | |
---|
[8858] | 35 | #include "util/Output.h" |
---|
[8351] | 36 | #include "packet/ServerInformation.h" |
---|
[10622] | 37 | #include "core/config/ConfigValueIncludes.h" |
---|
| 38 | #include "core/CoreIncludes.h" |
---|
[8351] | 39 | |
---|
[10622] | 40 | |
---|
[7161] | 41 | namespace orxonox |
---|
| 42 | { |
---|
| 43 | const char* LAN_DISCOVERY_MESSAGE = "Orxonox Client"; |
---|
| 44 | const char* LAN_DISCOVERY_ACK = "Orxonox Server Ack"; |
---|
| 45 | |
---|
[11009] | 46 | RegisterClassNoArgs(LANDiscoverable); |
---|
| 47 | |
---|
[11071] | 48 | LANDiscoverable::LANDiscoverable() : bActive_(false), host_(nullptr) |
---|
[7161] | 49 | { |
---|
[10622] | 50 | /* register object in orxonox */ |
---|
| 51 | RegisterObject(LANDiscoverable); |
---|
| 52 | |
---|
| 53 | this->setConfigValues(); |
---|
[7161] | 54 | } |
---|
| 55 | |
---|
[10622] | 56 | void LANDiscoverable::setConfigValues() |
---|
| 57 | { |
---|
| 58 | /* update ownName string from orxonox.ini config file, if it |
---|
| 59 | * has changed. |
---|
| 60 | */ |
---|
| 61 | SetConfigValueExternal(ownName, "Discovery", "ownName", "OrxServer"); |
---|
| 62 | } |
---|
| 63 | |
---|
[7161] | 64 | LANDiscoverable::~LANDiscoverable() |
---|
| 65 | { |
---|
| 66 | if( this->host_ ) |
---|
| 67 | { |
---|
| 68 | enet_host_destroy( this->host_ ); |
---|
| 69 | assert( this->bActive_ ); |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void LANDiscoverable::setActivity(bool bActive) |
---|
| 74 | { |
---|
| 75 | if( bActive == this->bActive_ ) // no change |
---|
| 76 | return; |
---|
[10622] | 77 | |
---|
[7161] | 78 | if( bActive ) |
---|
| 79 | { |
---|
[7459] | 80 | ENetAddress bindAddress; |
---|
| 81 | memset(& bindAddress, 0, sizeof(ENetAddress)); |
---|
| 82 | bindAddress.host = ENET_HOST_ANY; |
---|
| 83 | bindAddress.port = LAN_DISCOVERY_PORT; |
---|
[11071] | 84 | assert( this->host_ == nullptr ); |
---|
[7459] | 85 | this->host_ = enet_host_create( &bindAddress, 10, 0, 0, 0 ); |
---|
[11071] | 86 | if ( this->host_ == nullptr ) |
---|
| 87 | orxout(internal_error, context::network) << "LANDiscoverable: host_ == nullptr" << endl; |
---|
[7161] | 88 | } |
---|
| 89 | else |
---|
| 90 | { |
---|
| 91 | enet_host_destroy( this->host_ ); |
---|
[11071] | 92 | this->host_ = nullptr; |
---|
[7161] | 93 | } |
---|
| 94 | this->bActive_ = bActive; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void LANDiscoverable::update() |
---|
| 98 | { |
---|
| 99 | ENetEvent event; |
---|
[10622] | 100 | |
---|
[7161] | 101 | if( this->bActive_==false ) |
---|
| 102 | return; |
---|
| 103 | assert(this->host_); |
---|
[10622] | 104 | |
---|
[7161] | 105 | while( enet_host_service( this->host_, &event, 0 ) > 0 ) |
---|
| 106 | { |
---|
| 107 | switch(event.type) |
---|
| 108 | { |
---|
| 109 | case ENET_EVENT_TYPE_CONNECT: |
---|
[8858] | 110 | orxout(verbose, context::network) << "Received LAN discovery connect from client " << event.peer->host->receivedAddress << endl; |
---|
[7459] | 111 | break; |
---|
[7161] | 112 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 113 | case ENET_EVENT_TYPE_NONE: |
---|
| 114 | break; |
---|
| 115 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 116 | if( strcmp( LAN_DISCOVERY_MESSAGE, (char*)event.packet->data ) == 0 ) // check for a suitable orxonox client |
---|
| 117 | { |
---|
[8858] | 118 | orxout(internal_info, context::network) << "Received LAN discovery message from client " << event.peer->host->receivedAddress << endl; |
---|
[7161] | 119 | packet::ServerInformation info; |
---|
[10622] | 120 | info.setServerName(this->ownName); |
---|
| 121 | info.setClientNumber(this->clientNumber); |
---|
[7161] | 122 | info.send(event.peer); |
---|
| 123 | // ENetPacket* packet = enet_packet_create( LAN_DISCOVERY_ACK, strlen(LAN_DISCOVERY_ACK)+1, ENET_PACKET_FLAG_RELIABLE ); |
---|
| 124 | // enet_peer_send(event.peer, 0, packet ); |
---|
| 125 | enet_host_flush(this->host_); |
---|
| 126 | } |
---|
| 127 | break; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | } |
---|