[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 | |
---|
| 40 | |
---|
[7631] | 41 | int MasterServerComm::initialize() |
---|
| 42 | { |
---|
| 43 | /* initialize Enet */ |
---|
| 44 | if (enet_initialize () != 0) |
---|
| 45 | { COUT(1) << "An error occurred while initializing ENet.\n"; |
---|
| 46 | return 1; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | /* install atexit handler for enet */ |
---|
| 50 | atexit( enet_deinitialize ); |
---|
[7589] | 51 | |
---|
[7631] | 52 | |
---|
| 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) |
---|
| 62 | { COUT(1) << "An error occurred while trying to create an ENet client host.\n"; |
---|
| 63 | return 1; |
---|
| 64 | } |
---|
[7589] | 65 | } |
---|
| 66 | |
---|
[7631] | 67 | MasterServerComm::~MasterServerComm() |
---|
| 68 | { |
---|
| 69 | /* destroy the enet facilities */ |
---|
| 70 | enet_host_destroy(this->client); |
---|
| 71 | } |
---|
[7589] | 72 | |
---|
[7631] | 73 | int MasterServerComm::connect( char *address, unsigned int port ) |
---|
| 74 | { |
---|
| 75 | /* Connect to address:port. */ |
---|
| 76 | enet_address_set_host( &this->address, address ); |
---|
| 77 | this->address.port = port; |
---|
[7589] | 78 | |
---|
[7631] | 79 | /* Initiate the connection, allocating the two channels 0 and 1. */ |
---|
| 80 | this->peer = enet_host_connect(this->client, &this->address, 2, 0); |
---|
[7589] | 81 | |
---|
[7631] | 82 | if (this->peer == NULL ) |
---|
| 83 | { fprintf( stderr, |
---|
[7589] | 84 | "No available peers for initiating an ENet connection.\n"); |
---|
[7630] | 85 | //exit (EXIT_FAILURE); |
---|
| 86 | return -1; |
---|
[7631] | 87 | } |
---|
[7589] | 88 | |
---|
[7631] | 89 | /* Wait up to 2 seconds for the connection attempt to succeed. */ |
---|
| 90 | if (enet_host_service (this->client, &this->event, 2000) > 0 && |
---|
| 91 | this->event.type == ENET_EVENT_TYPE_CONNECT ) |
---|
| 92 | fprintf( stdout, "Connection to server succeeded." ); |
---|
| 93 | else |
---|
| 94 | { |
---|
| 95 | enet_peer_reset (this->peer); |
---|
| 96 | fprintf( stdout, "Connection to %s failed.", address ); |
---|
| 97 | //exit(EXIT_FAILURE); |
---|
| 98 | return -1; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | return 0; |
---|
[7611] | 102 | } |
---|
[7589] | 103 | |
---|
[7631] | 104 | int MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ) ) |
---|
[7611] | 105 | { |
---|
[7631] | 106 | /* see whether anything happened */ |
---|
| 107 | if( enet_host_service( this->client, &this->event, 100 ) >= 0 ) |
---|
| 108 | { |
---|
| 109 | /* address buffer */ |
---|
| 110 | char *addrconv = NULL; |
---|
[7589] | 111 | |
---|
[7631] | 112 | /* check what type of event it is and react accordingly */ |
---|
| 113 | switch (this->event.type) |
---|
| 114 | { /* new connection, not supposed to happen. */ |
---|
| 115 | case ENET_EVENT_TYPE_CONNECT: break; |
---|
[7611] | 116 | |
---|
[7631] | 117 | /* disconnect */ |
---|
| 118 | case ENET_EVENT_TYPE_DISCONNECT: /* ?? */ break; |
---|
| 119 | |
---|
[7611] | 120 | /* incoming data */ |
---|
[7631] | 121 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 122 | addrconv = (char *) calloc( 50, 1 ); |
---|
| 123 | enet_address_get_host_ip( &(this->event.peer->address), addrconv, 49 ); |
---|
[7611] | 124 | |
---|
[7631] | 125 | /* DEBUG */ |
---|
| 126 | printf( "A packet of length %u containing %s was " |
---|
| 127 | "received from %s on channel %u.\n", |
---|
| 128 | this->event.packet->dataLength, |
---|
| 129 | this->event.packet->data, |
---|
| 130 | addrconv, |
---|
| 131 | this->event.channelID ); |
---|
| 132 | /* END DEBUG */ |
---|
[7611] | 133 | |
---|
[7631] | 134 | /* call the supplied callback, if any. */ |
---|
| 135 | if( (*callback) != NULL ) |
---|
| 136 | (*callback)( addrconv, &(this->event) ); |
---|
[7611] | 137 | |
---|
[7631] | 138 | enet_packet_destroy( event.packet ); |
---|
| 139 | if( addrconv ) free( addrconv ); |
---|
| 140 | break; |
---|
| 141 | default: break; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /* event handled, return 0 */ |
---|
| 145 | return 0; |
---|
[7589] | 146 | } |
---|
[7631] | 147 | |
---|
| 148 | /* show that no event occured */ |
---|
| 149 | return 1; |
---|
[7589] | 150 | } |
---|
[7611] | 151 | |
---|
[7631] | 152 | int MasterServerComm::sendRequest( char *data ) |
---|
| 153 | { |
---|
| 154 | /* send the data to the friend */ |
---|
| 155 | /* Create a reliable packet of size 7 containing "packet\0" */ |
---|
| 156 | ENetPacket * packet = enet_packet_create( data, |
---|
| 157 | strlen( data ) + 1, |
---|
| 158 | ENET_PACKET_FLAG_RELIABLE); |
---|
[7611] | 159 | |
---|
[7631] | 160 | /* Send the packet to the peer over channel id 0. */ |
---|
| 161 | enet_peer_send (this->peer, 0, packet); |
---|
[7611] | 162 | |
---|
[7631] | 163 | /* One could just use enet_host_service() instead. */ |
---|
| 164 | enet_host_flush( this->client ); |
---|
| 165 | if( packet ) free( packet ); |
---|
| 166 | } |
---|
[7611] | 167 | |
---|
| 168 | } |
---|
| 169 | |
---|
[7631] | 170 | |
---|
| 171 | /* DON'T DELETE THIS I MIGHT NEED IT AGAIN -smerkli */ |
---|
| 172 | /* not needed anymore, only here for testing purposes */ |
---|
| 173 | /* |
---|
| 174 | //[> sample callback to output debugging info. <] |
---|
| 175 | //int callb( char *addr, ENetEvent *ev ) |
---|
| 176 | //{ |
---|
| 177 | //printf( "A packet of length %u containing %s was " |
---|
| 178 | //"received from %s on channel %u.\n", |
---|
| 179 | //ev->packet->dataLength, |
---|
| 180 | //ev->packet->data, |
---|
| 181 | //addr, |
---|
| 182 | //ev->channelID ); |
---|
| 183 | //return 0; |
---|
| 184 | //} |
---|
| 185 | |
---|
| 186 | //[> small testing implementation <] |
---|
| 187 | //int |
---|
| 188 | //main( int argc, char *argv[] ) |
---|
| 189 | //{ |
---|
| 190 | //[> setup object and connect <] |
---|
| 191 | //MasterServerComm msc = MasterServerComm(); |
---|
| 192 | //if( msc.connect( argv[1], 1234 ) ) |
---|
| 193 | //exit(EXIT_FAILURE); |
---|
[7611] | 194 | |
---|
[7631] | 195 | //[> send some data and poll for replies <] |
---|
| 196 | //char *theinput = (char *)calloc( 100,1 ); |
---|
| 197 | //while( true ) |
---|
| 198 | //{ |
---|
| 199 | //fgets( theinput, 90, stdin ); |
---|
| 200 | //if( !strncmp( theinput, "quit", 4 ) ) |
---|
| 201 | //break; |
---|
[7611] | 202 | |
---|
[7631] | 203 | //msc.sendRequest( theinput ); |
---|
| 204 | //msc.pollForReply( &callb ); |
---|
| 205 | //} |
---|
[7589] | 206 | |
---|
[7631] | 207 | //} |
---|
| 208 | */ |
---|