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