[7589] | 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 | * Sandro 'smerkli' Merkli |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[7611] | 29 | #include "MasterServerComm.h" |
---|
[7589] | 30 | |
---|
[7631] | 31 | namespace orxonox |
---|
[7589] | 32 | { |
---|
[7631] | 33 | |
---|
| 34 | MasterServerComm::MasterServerComm() |
---|
| 35 | { /* nothing anymore, everything's been outsourced to |
---|
| 36 | * the initialize method to facilitate debugging |
---|
| 37 | */ |
---|
| 38 | } |
---|
[7589] | 39 | |
---|
[7631] | 40 | int MasterServerComm::initialize() |
---|
| 41 | { |
---|
| 42 | /* initialize Enet */ |
---|
| 43 | if (enet_initialize () != 0) |
---|
| 44 | { COUT(1) << "An error occurred while initializing ENet.\n"; |
---|
| 45 | return 1; |
---|
| 46 | } |
---|
[7684] | 47 | |
---|
| 48 | /* initialize the event holder */ |
---|
| 49 | this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 ); |
---|
[7631] | 50 | |
---|
| 51 | /* install atexit handler for enet */ |
---|
| 52 | atexit( enet_deinitialize ); |
---|
[7589] | 53 | |
---|
[7631] | 54 | /* initiate the client */ |
---|
| 55 | this->client = enet_host_create( NULL /* create a client host */, |
---|
| 56 | 1, |
---|
| 57 | 2, /* allow up 2 channels to be used, 0 and 1 */ |
---|
| 58 | 0, |
---|
| 59 | 0 ); |
---|
| 60 | |
---|
| 61 | /* see if it worked */ |
---|
| 62 | if (this->client == NULL) |
---|
| 63 | { COUT(1) << "An error occurred while trying to create an ENet client host.\n"; |
---|
| 64 | return 1; |
---|
| 65 | } |
---|
[7632] | 66 | |
---|
| 67 | return 0; |
---|
[7589] | 68 | } |
---|
| 69 | |
---|
[7631] | 70 | MasterServerComm::~MasterServerComm() |
---|
| 71 | { |
---|
| 72 | /* destroy the enet facilities */ |
---|
| 73 | enet_host_destroy(this->client); |
---|
| 74 | } |
---|
[7589] | 75 | |
---|
[7725] | 76 | int MasterServerComm::connect( const char *address, unsigned int port ) |
---|
[7631] | 77 | { |
---|
| 78 | /* Connect to address:port. */ |
---|
| 79 | enet_address_set_host( &this->address, address ); |
---|
| 80 | this->address.port = port; |
---|
[7589] | 81 | |
---|
[7631] | 82 | /* Initiate the connection, allocating the two channels 0 and 1. */ |
---|
| 83 | this->peer = enet_host_connect(this->client, &this->address, 2, 0); |
---|
[7589] | 84 | |
---|
[7631] | 85 | if (this->peer == NULL ) |
---|
[7662] | 86 | { COUT(2) << "ERROR: No available peers for initiating an ENet connection.\n"; |
---|
[7756] | 87 | return -1; |
---|
[7631] | 88 | } |
---|
[7589] | 89 | |
---|
[7631] | 90 | /* Wait up to 2 seconds for the connection attempt to succeed. */ |
---|
[7759] | 91 | if (enet_host_service (this->client, this->event, 500) > 0 && |
---|
[7684] | 92 | this->event->type == ENET_EVENT_TYPE_CONNECT ) |
---|
[7745] | 93 | COUT(3) << "Connection to master server succeeded.\n"; |
---|
[7631] | 94 | else |
---|
| 95 | { |
---|
| 96 | enet_peer_reset (this->peer); |
---|
| 97 | fprintf( stdout, "Connection to %s failed.", address ); |
---|
[7745] | 98 | COUT(2) << "ERROR: connection to " << address << " failed.\n"; |
---|
[7631] | 99 | return -1; |
---|
| 100 | } |
---|
| 101 | |
---|
[7756] | 102 | /* all fine */ |
---|
[7631] | 103 | return 0; |
---|
[7611] | 104 | } |
---|
[7589] | 105 | |
---|
[7756] | 106 | /* NOTE this is to be reimplemented soon to return |
---|
| 107 | * a structure containing |
---|
| 108 | * - addrconv |
---|
| 109 | * - the event |
---|
| 110 | * so we can also make callbacks from objects |
---|
| 111 | */ |
---|
| 112 | int MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ), |
---|
| 113 | int delayms ) |
---|
[7611] | 114 | { |
---|
[7631] | 115 | /* see whether anything happened */ |
---|
[7668] | 116 | /* WORK MARK REMOVE THIS OUTPUT */ |
---|
[7692] | 117 | COUT(2) << "polling masterserver...\n"; |
---|
[7668] | 118 | |
---|
[7692] | 119 | /* address buffer */ |
---|
| 120 | char *addrconv = NULL; |
---|
| 121 | int retval = 0; |
---|
| 122 | |
---|
[7672] | 123 | /* enet_host_service returns 0 if no event occured */ |
---|
| 124 | /* just newly set below test to >0 from >= 0, to be tested */ |
---|
[7756] | 125 | if( enet_host_service( this->client, this->event, delayms ) > 0 ) |
---|
[7631] | 126 | { |
---|
| 127 | /* check what type of event it is and react accordingly */ |
---|
[7684] | 128 | switch (this->event->type) |
---|
[7631] | 129 | { /* new connection, not supposed to happen. */ |
---|
| 130 | case ENET_EVENT_TYPE_CONNECT: break; |
---|
[7611] | 131 | |
---|
[7631] | 132 | /* disconnect */ |
---|
| 133 | case ENET_EVENT_TYPE_DISCONNECT: /* ?? */ break; |
---|
| 134 | |
---|
[7611] | 135 | /* incoming data */ |
---|
[7631] | 136 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 137 | addrconv = (char *) calloc( 50, 1 ); |
---|
[7692] | 138 | if( !addrconv ) |
---|
| 139 | { COUT(2) << "MasterServerComm.cc: Could not allocate memory!\n"; |
---|
| 140 | break; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | /* resolve IP */ |
---|
[7684] | 144 | enet_address_get_host_ip( &(this->event->peer->address), |
---|
| 145 | addrconv, 49 ); |
---|
[7611] | 146 | |
---|
[7631] | 147 | /* DEBUG */ |
---|
[7756] | 148 | COUT(3) << "MasterServer Debug: A packet of length " |
---|
| 149 | << this->event->packet->dataLength |
---|
[7725] | 150 | << " containing " << this->event->packet->data |
---|
| 151 | << " was received from " << addrconv |
---|
| 152 | << " on channel " << this->event->channelID; |
---|
[7631] | 153 | /* END DEBUG */ |
---|
[7611] | 154 | |
---|
[7631] | 155 | /* call the supplied callback, if any. */ |
---|
| 156 | if( (*callback) != NULL ) |
---|
[7684] | 157 | retval = (*callback)( addrconv, (this->event) ); |
---|
[7611] | 158 | |
---|
[7756] | 159 | /* clean up */ |
---|
[7684] | 160 | enet_packet_destroy( event->packet ); |
---|
[7756] | 161 | if( addrconv ) |
---|
| 162 | free( addrconv ); |
---|
| 163 | |
---|
[7631] | 164 | break; |
---|
| 165 | default: break; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /* event handled, return 0 */ |
---|
[7650] | 169 | return retval; |
---|
[7589] | 170 | } |
---|
[7631] | 171 | |
---|
| 172 | /* show that no event occured */ |
---|
[7668] | 173 | return 0; |
---|
[7589] | 174 | } |
---|
[7611] | 175 | |
---|
[7725] | 176 | int MasterServerComm::sendRequest( const char *data ) |
---|
[7631] | 177 | { |
---|
| 178 | /* send the data to the friend */ |
---|
| 179 | /* Create a reliable packet of size 7 containing "packet\0" */ |
---|
| 180 | ENetPacket * packet = enet_packet_create( data, |
---|
| 181 | strlen( data ) + 1, |
---|
| 182 | ENET_PACKET_FLAG_RELIABLE); |
---|
[7611] | 183 | |
---|
[7631] | 184 | /* Send the packet to the peer over channel id 0. */ |
---|
| 185 | enet_peer_send (this->peer, 0, packet); |
---|
[7611] | 186 | |
---|
[7631] | 187 | /* One could just use enet_host_service() instead. */ |
---|
| 188 | enet_host_flush( this->client ); |
---|
[7668] | 189 | |
---|
[7692] | 190 | /* free the packet */ |
---|
| 191 | enet_packet_destroy( packet ); |
---|
[7632] | 192 | |
---|
| 193 | /* all done. */ |
---|
| 194 | return 0; |
---|
[7631] | 195 | } |
---|
[7611] | 196 | |
---|
[7650] | 197 | int MasterServerComm::sendRequest( std::string data ) |
---|
| 198 | { |
---|
| 199 | /* send the data to the friend */ |
---|
| 200 | /* Create a reliable packet of size 7 containing "packet\0" */ |
---|
| 201 | ENetPacket * packet = enet_packet_create( data.c_str(), |
---|
| 202 | data.length() + 1, |
---|
| 203 | ENET_PACKET_FLAG_RELIABLE); |
---|
| 204 | |
---|
| 205 | /* Send the packet to the peer over channel id 0. */ |
---|
| 206 | enet_peer_send (this->peer, 0, packet); |
---|
| 207 | |
---|
| 208 | /* One could just use enet_host_service() instead. */ |
---|
| 209 | enet_host_flush( this->client ); |
---|
| 210 | if( packet ) free( packet ); |
---|
| 211 | |
---|
| 212 | /* all done. */ |
---|
| 213 | return 0; |
---|
| 214 | } |
---|
| 215 | |
---|
[7611] | 216 | } |
---|