[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 | { |
---|
[7768] | 33 | |
---|
[7631] | 34 | MasterServerComm::MasterServerComm() |
---|
| 35 | { /* nothing anymore, everything's been outsourced to |
---|
| 36 | * the initialize method to facilitate debugging |
---|
| 37 | */ |
---|
[7768] | 38 | /* register object in orxonox */ |
---|
[7631] | 39 | } |
---|
[7589] | 40 | |
---|
[7631] | 41 | int MasterServerComm::initialize() |
---|
| 42 | { |
---|
| 43 | /* initialize Enet */ |
---|
[7763] | 44 | if( enet_initialize () != 0 ) |
---|
[7631] | 45 | { COUT(1) << "An error occurred while initializing ENet.\n"; |
---|
| 46 | return 1; |
---|
| 47 | } |
---|
[7684] | 48 | |
---|
| 49 | /* initialize the event holder */ |
---|
| 50 | this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 ); |
---|
[7631] | 51 | |
---|
[7589] | 52 | |
---|
[7631] | 53 | /* initiate the client */ |
---|
| 54 | this->client = enet_host_create( NULL /* create a client host */, |
---|
| 55 | 1, |
---|
| 56 | 2, /* allow up 2 channels to be used, 0 and 1 */ |
---|
| 57 | 0, |
---|
| 58 | 0 ); |
---|
| 59 | |
---|
| 60 | /* see if it worked */ |
---|
| 61 | if (this->client == NULL) |
---|
[7763] | 62 | { COUT(1) << "An error occurred while trying to create an " |
---|
| 63 | << "ENet client host.\n"; |
---|
[7631] | 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 | |
---|
[7763] | 85 | if( this->peer == NULL ) |
---|
| 86 | { COUT(2) << "ERROR: No available peers for initiating an ENet" |
---|
| 87 | << " connection.\n"; |
---|
[7756] | 88 | return -1; |
---|
[7631] | 89 | } |
---|
[7589] | 90 | |
---|
[7631] | 91 | /* Wait up to 2 seconds for the connection attempt to succeed. */ |
---|
[7684] | 92 | if (enet_host_service (this->client, this->event, 2000) > 0 && |
---|
| 93 | this->event->type == ENET_EVENT_TYPE_CONNECT ) |
---|
[7745] | 94 | COUT(3) << "Connection to master server succeeded.\n"; |
---|
[7631] | 95 | else |
---|
| 96 | { |
---|
| 97 | enet_peer_reset (this->peer); |
---|
[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 | |
---|
[7761] | 106 | int MasterServerComm::disconnect( void ) |
---|
| 107 | { |
---|
| 108 | enet_peer_disconnect( this->peer, 0 ); |
---|
| 109 | |
---|
| 110 | /* Allow up to 1 second for the disconnect to succeed |
---|
| 111 | * and drop any packets received packets. |
---|
| 112 | */ |
---|
| 113 | while (enet_host_service (this->client, this->event, 1000) > 0) |
---|
| 114 | { |
---|
| 115 | switch (this->event->type) |
---|
| 116 | { |
---|
| 117 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 118 | enet_packet_destroy (event->packet); |
---|
| 119 | break; |
---|
| 120 | |
---|
| 121 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 122 | COUT(4) << "Disconnect from master server successful.\n"; |
---|
| 123 | return 0; |
---|
| 124 | default: break; |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /* We've arrived here, so the disconnect attempt didn't |
---|
| 129 | * succeed yet, hence: force the connection down. |
---|
| 130 | */ |
---|
| 131 | enet_peer_reset( this->peer ); |
---|
| 132 | |
---|
| 133 | /* done */ |
---|
| 134 | return 0; |
---|
| 135 | } |
---|
| 136 | |
---|
[7756] | 137 | /* NOTE this is to be reimplemented soon to return |
---|
| 138 | * a structure containing |
---|
| 139 | * - addrconv |
---|
| 140 | * - the event |
---|
| 141 | * so we can also make callbacks from objects |
---|
| 142 | */ |
---|
| 143 | int MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ), |
---|
| 144 | int delayms ) |
---|
[7611] | 145 | { |
---|
[7631] | 146 | /* see whether anything happened */ |
---|
[7668] | 147 | /* WORK MARK REMOVE THIS OUTPUT */ |
---|
[7692] | 148 | COUT(2) << "polling masterserver...\n"; |
---|
[7668] | 149 | |
---|
[7692] | 150 | /* address buffer */ |
---|
| 151 | char *addrconv = NULL; |
---|
| 152 | int retval = 0; |
---|
| 153 | |
---|
[7672] | 154 | /* enet_host_service returns 0 if no event occured */ |
---|
| 155 | /* just newly set below test to >0 from >= 0, to be tested */ |
---|
[7756] | 156 | if( enet_host_service( this->client, this->event, delayms ) > 0 ) |
---|
[7631] | 157 | { |
---|
| 158 | /* check what type of event it is and react accordingly */ |
---|
[7684] | 159 | switch (this->event->type) |
---|
[7631] | 160 | { /* new connection, not supposed to happen. */ |
---|
| 161 | case ENET_EVENT_TYPE_CONNECT: break; |
---|
[7611] | 162 | |
---|
[7631] | 163 | /* disconnect */ |
---|
| 164 | case ENET_EVENT_TYPE_DISCONNECT: /* ?? */ break; |
---|
| 165 | |
---|
[7611] | 166 | /* incoming data */ |
---|
[7631] | 167 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 168 | addrconv = (char *) calloc( 50, 1 ); |
---|
[7692] | 169 | if( !addrconv ) |
---|
| 170 | { COUT(2) << "MasterServerComm.cc: Could not allocate memory!\n"; |
---|
| 171 | break; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /* resolve IP */ |
---|
[7684] | 175 | enet_address_get_host_ip( &(this->event->peer->address), |
---|
| 176 | addrconv, 49 ); |
---|
[7611] | 177 | |
---|
[7631] | 178 | /* DEBUG */ |
---|
[7756] | 179 | COUT(3) << "MasterServer Debug: A packet of length " |
---|
| 180 | << this->event->packet->dataLength |
---|
[7725] | 181 | << " containing " << this->event->packet->data |
---|
| 182 | << " was received from " << addrconv |
---|
| 183 | << " on channel " << this->event->channelID; |
---|
[7631] | 184 | /* END DEBUG */ |
---|
[7611] | 185 | |
---|
[7631] | 186 | /* call the supplied callback, if any. */ |
---|
| 187 | if( (*callback) != NULL ) |
---|
[7684] | 188 | retval = (*callback)( addrconv, (this->event) ); |
---|
[7611] | 189 | |
---|
[7756] | 190 | /* clean up */ |
---|
[7684] | 191 | enet_packet_destroy( event->packet ); |
---|
[7756] | 192 | if( addrconv ) |
---|
| 193 | free( addrconv ); |
---|
| 194 | |
---|
[7631] | 195 | break; |
---|
| 196 | default: break; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | /* event handled, return 0 */ |
---|
[7650] | 200 | return retval; |
---|
[7589] | 201 | } |
---|
[7631] | 202 | |
---|
| 203 | /* show that no event occured */ |
---|
[7668] | 204 | return 0; |
---|
[7589] | 205 | } |
---|
[7611] | 206 | |
---|
[7725] | 207 | int MasterServerComm::sendRequest( const char *data ) |
---|
[7631] | 208 | { |
---|
| 209 | /* send the data to the friend */ |
---|
| 210 | /* Create a reliable packet of size 7 containing "packet\0" */ |
---|
| 211 | ENetPacket * packet = enet_packet_create( data, |
---|
| 212 | strlen( data ) + 1, |
---|
| 213 | ENET_PACKET_FLAG_RELIABLE); |
---|
[7611] | 214 | |
---|
[7631] | 215 | /* Send the packet to the peer over channel id 0. */ |
---|
| 216 | enet_peer_send (this->peer, 0, packet); |
---|
[7611] | 217 | |
---|
[7631] | 218 | /* One could just use enet_host_service() instead. */ |
---|
| 219 | enet_host_flush( this->client ); |
---|
[7668] | 220 | |
---|
[7692] | 221 | /* free the packet */ |
---|
| 222 | enet_packet_destroy( packet ); |
---|
[7632] | 223 | |
---|
| 224 | /* all done. */ |
---|
| 225 | return 0; |
---|
[7631] | 226 | } |
---|
[7611] | 227 | |
---|
[7650] | 228 | int MasterServerComm::sendRequest( std::string data ) |
---|
| 229 | { |
---|
| 230 | /* send the data to the friend */ |
---|
| 231 | /* Create a reliable packet of size 7 containing "packet\0" */ |
---|
| 232 | ENetPacket * packet = enet_packet_create( data.c_str(), |
---|
| 233 | data.length() + 1, |
---|
| 234 | ENET_PACKET_FLAG_RELIABLE); |
---|
| 235 | |
---|
| 236 | /* Send the packet to the peer over channel id 0. */ |
---|
| 237 | enet_peer_send (this->peer, 0, packet); |
---|
| 238 | |
---|
| 239 | /* One could just use enet_host_service() instead. */ |
---|
| 240 | enet_host_flush( this->client ); |
---|
[7763] | 241 | enet_packet_destroy( packet ); |
---|
[7650] | 242 | |
---|
| 243 | /* all done. */ |
---|
| 244 | return 0; |
---|
| 245 | } |
---|
| 246 | |
---|
[7611] | 247 | } |
---|