- Timestamp:
- Nov 3, 2010, 4:20:02 PM (14 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
code/branches/masterserver/src/modules/masterserver/MasterServer.cc
r7610 r7611 40 40 /* connect event */ 41 41 int 42 eventConnect( ENetEvent *event )42 MasterServer::eventConnect( ENetEvent *event ) 43 43 { /* check for bad parameters */ 44 44 if( !event ) 45 { fprintf( stderr, "No event given.\n" );45 { COUT(2) << "MasterServer::eventConnect: No event given.\n" ; 46 46 return -1; 47 47 } 48 48 49 /* convert address to string. */ 50 char *addrconv = (char *) calloc( 50, 1 ); 51 enet_address_get_host_ip( &(event->peer->address), addrconv, 49 ); 52 49 53 /* output debug info */ 50 printf( "A new client connected from %x:%u.\n", 51 (event->peer->address.host), 52 event->peer->address.port ); 54 COUT(4) << "A new client connected from " 55 << addrconv 56 << " on port " 57 << event->peer->address.port << "\n"; 53 58 54 59 /* game server or client connection? */ … … 61 66 /* add to client list */ 62 67 63 /* NOTE this seems to be some weird snipped from the tutorial as peer.data 64 * is a uint_32 of some kind and hence shouldn't be assigned a c string? :S 65 */ 66 /* Store any relevant client information here. */ 67 /* event->peer->data = "Client information"; */ 68 /* /NOTE */ 68 /* store string form of address here */ 69 event->peer->data = addrconv; 70 71 /* all fine. */ 69 72 return 0; 70 73 } … … 72 75 /* disconnect event */ 73 76 int 74 eventDisconnect( ENetEvent *event )77 MasterServer::eventDisconnect( ENetEvent *event ) 75 78 { /* check for bad parameters */ 76 79 if( !event ) 77 { fprintf( stderr, "No event given.\n" );80 { COUT(2) << "No event given.\n"; 78 81 return -1; 79 82 } 80 83 81 84 /* output that the disconnect happened, to be removed at a later time. */ 82 printf( "%s disconnected.\n", (char*)event->peer->data );85 COUT(4) << (char*)event->peer->data << " disconnected.\n"; 83 86 84 87 /* remove the server from the list it belongs to */ 85 88 86 89 /* Reset the peer's client information. */ 87 event->peer->data = NULL;90 if( event->peer->data ) free( event->peer->data ); 88 91 return 0; 89 92 } … … 91 94 /* data event */ 92 95 int 93 eventData( ENetEvent *event )96 MasterServer::eventData( ENetEvent *event ) 94 97 { /* output what's in the packet (to be removed later) */ 95 if( !event || !(event->packet) || !(event->peer) || !(event->channelID))96 { fprintf( stderr, "No complete event given.\n" );98 if( !event || !(event->packet) || !(event->peer) ) 99 { COUT(2) << "No complete event given.\n"; 97 100 return -1; 98 101 } 99 102 103 /* generate address in readable form */ 104 char *addrconv = (char *) calloc( 50, 1 ); 105 enet_address_get_host_ip( &(event->peer->address), addrconv, 49 ); 106 107 /* DEBUG */ 100 108 /* output debug info about the data that has come, to be removed */ 101 //printf( "A packet of length %u containing %s was received from %s on channel %u.\n", 102 //event->packet->dataLength, 103 //event->packet->data, 104 //event->peer->data, 105 //event->channelID ); 109 COUT(4) << "A packet of length" 110 << event->packet->dataLength 111 << " containing " 112 << event->packet->data 113 << " was received from " 114 << addrconv 115 << " on channel " 116 << event->channelID << "\n"; 117 118 /* send some packet back for testing */ 119 /* TESTING */ 120 121 /* Create a reliable reply of size 7 containing "reply\0" */ 122 ENetPacket * reply = enet_packet_create ("reply", 123 strlen ("reply") + 1, 124 ENET_PACKET_FLAG_RELIABLE); 125 126 /* Send the reply to the peer over channel id 0. */ 127 enet_peer_send( event->peer, 0, reply ); 128 129 /* One could just use enet_host_service() instead. */ 130 enet_host_flush( this->server ); 131 132 /* /TESTING */ 106 133 107 134 /* game server or client connection? */ … … 115 142 /* and send reply */ 116 143 144 /* delete addrconv */ 145 if( addrconv ) free( addrconv ); 146 117 147 /* Clean up the packet now that we're done using it. */ 118 148 enet_packet_destroy( event->packet ); … … 120 150 } 121 151 152 122 153 /**** MAIN ROUTINE *****/ 123 154 int 124 155 MasterServer::run() 125 156 { 126 COUT(0) << "omg, i got baschtl'd!\n";127 128 157 /***** ENTER MAIN LOOP *****/ 129 158 ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char)); 130 159 if( event == NULL ) 131 { fprintf( stderr, "Could not create ENetEvent structure, exiting.\n" ); 132 exit( EXIT_FAILURE ); 133 } 160 { 161 COUT(1) << "Could not create ENetEvent structure, exiting.\n"; 162 exit( EXIT_FAILURE ); 163 } 164 165 /* tell people we're now initialized and blocking. */ 166 COUT(0) << "MasterServer initialized, waiting for connections.\n"; 134 167 135 168 /* create an iterator for the loop */ 136 while( enet_host_service( this->server, event, 1000 ) > 0 )169 while( enet_host_service( this->server, event, 1000 ) >= 0 ) 137 170 { /* check what type of event it is and react accordingly */ 138 171 switch (event->type) … … 163 196 /***** INITIALIZE NETWORKING *****/ 164 197 if( enet_initialize () != 0) 165 { fprintf( stderr, "An error occurred while initializing ENet.\n");198 { COUT(1) << "An error occurred while initializing ENet.\n"; 166 199 exit( EXIT_FAILURE ); 167 200 } … … 181 214 /* see if creation worked */ 182 215 if( !this->server ) 183 { fprintf( stderr,184 "An error occurred while trying to create an ENet server host.\n" );185 exit( EXIT_FAILURE );186 } 187 188 /***** INITIALIZE GAME SERVER LIST*****/216 { COUT(1) << 217 "An error occurred while trying to create an ENet server host.\n"; 218 exit( EXIT_FAILURE ); 219 } 220 221 /***** INITIALIZE GAME SERVER AND PEER LISTS *****/ 189 222 this->mainlist = new ServerList(); 190 if( this->mainlist == NULL )191 { fprintf( stderr, "Error creating server list.\n" );192 exit( EXIT_FAILURE );193 }194 195 /***** INITIALIZE PEER LIST *****/196 223 this->peers = new PeerList(); 224 if( this->mainlist == NULL || this->peers == NULL ) 225 { COUT(1) << "Error creating server or peer list.\n"; 226 exit( EXIT_FAILURE ); 227 } 197 228 198 229 /* run the main method */ … … 207 238 enet_host_destroy( this->server ); 208 239 240 /* free all used memory */ 209 241 /* clear the list of connected game servers */ 210 242 /* clear the list of connected game clients */ 211 243 212 /* free all used memory */213 214 244 } 215 245
Note: See TracChangeset
for help on using the changeset viewer.