Changeset 7611 for code/branches/masterserver/src/modules
- Timestamp:
- Nov 3, 2010, 4:20:02 PM (14 years ago)
- Location:
- code/branches/masterserver/src/modules/masterserver
- Files:
-
- 1 added
- 1 edited
- 4 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 -
code/branches/masterserver/src/modules/masterserver/MasterServer.h
r7590 r7611 68 68 69 69 private: 70 /* methods */ 71 int eventConnect( ENetEvent *event ); 72 int eventDisconnect( ENetEvent *event ); 73 int eventData( ENetEvent *event ); 74 75 /* members */ 70 76 ENetAddress address; 71 77 ENetHost *server; 72 78 ServerList *mainlist; 73 79 PeerList *peers; 80 81 /* main routine */ 74 82 int run(); 75 83 -
code/branches/masterserver/src/modules/masterserver/MasterServerComm.cc
r7610 r7611 31 31 #include <cstring> 32 32 #include <enet/enet.h> 33 #include "MasterServerComm.h" 33 34 34 int main( int argc, char *argv[])35 MasterServerComm::MasterServerComm() 35 36 { 36 37 /* initialize Enet */ 37 38 if (enet_initialize () != 0) 38 39 { fprintf (stderr, "An error occurred while initializing ENet.\n"); 39 return EXIT_FAILURE;40 exit(EXIT_FAILURE); 40 41 } 41 42 atexit (enet_deinitialize); 42 43 43 /* client handle */44 ENetHost *client;45 44 46 45 /* initiate the client */ 47 client = enet_host_create( NULL /* create a client host */,46 this->client = enet_host_create( NULL /* create a client host */, 48 47 1, 49 48 2, /* allow up 2 channels to be used, 0 and 1 */ … … 52 51 53 52 /* see if it worked */ 54 if ( client == NULL)53 if (this->client == NULL) 55 54 { fprintf (stderr, 56 55 "An error occurred while trying to create an ENet client host.\n"); 57 56 exit (EXIT_FAILURE); 58 57 } 58 } 59 59 60 ENetAddress address; 61 ENetEvent event; 62 ENetPeer *peer; 60 MasterServerComm::~MasterServerComm() 61 { 62 enet_host_destroy(this->client); 63 } 63 64 64 /* Connect to some.server.net:1234. */ 65 enet_address_set_host (& address, argv[1] ); 66 address.port = 1234; 65 int 66 MasterServerComm::connect( char *address, unsigned int port ) 67 { 68 /* Connect to address:port. */ 69 enet_address_set_host( &this->address, address ); 70 this->address.port = port; 67 71 68 72 /* Initiate the connection, allocating the two channels 0 and 1. */ 69 peer = enet_host_connect(client, &address, 2, 0);73 this->peer = enet_host_connect(this->client, &this->address, 2, 0); 70 74 71 if ( peer == NULL )75 if (this->peer == NULL ) 72 76 { fprintf( stderr, 73 77 "No available peers for initiating an ENet connection.\n"); … … 75 79 } 76 80 77 /* Wait up to 5 seconds for the connection attempt to succeed. */ 78 if (enet_host_service (client, & event, 5000) > 0 && 79 event.type == ENET_EVENT_TYPE_CONNECT) 80 { fprintf( stdout, "Connection to some.server.net:1234 succeeded." ); 81 char *theinput = (char *)calloc( 100,1 ); 82 while( true ) 83 { 84 fgets( theinput, 90, stdin ); 85 if( !strncmp( theinput, "quit", 4 ) ) 86 break; 87 88 /* send the data to the friend */ 89 /* Create a reliable packet of size 7 containing "packet\0" */ 90 ENetPacket * packet = enet_packet_create( theinput, 91 strlen( theinput ) + 1, 92 ENET_PACKET_FLAG_RELIABLE); 93 94 /* Send the packet to the peer over channel id 0. */ 95 enet_peer_send (peer, 0, packet); 96 97 /* One could just use enet_host_service() instead. */ 98 enet_host_flush( client ); 99 if( packet ) free( packet ); 100 } 101 } 81 /* Wait up to 2 seconds for the connection attempt to succeed. */ 82 if (enet_host_service (this->client, &this->event, 2000) > 0 && 83 this->event.type == ENET_EVENT_TYPE_CONNECT ) 84 fprintf( stdout, "Connection to server succeeded." ); 102 85 else 103 86 { 104 /* Either the 5 seconds are up or a disconnect event was */ 105 /* received. Reset the peer in the event the 5 seconds */ 106 /* had run out without any significant event. */ 107 enet_peer_reset (peer); 108 fprintf( stdout, "Connection to some.server.net:1234 failed." ); 87 enet_peer_reset (this->peer); 88 fprintf( stdout, "Connection to %s failed.", address ); 109 89 exit(EXIT_FAILURE); 110 90 } 111 91 112 enet_host_destroy(client);92 return 0; 113 93 } 94 95 int 96 MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ) ) 97 { 98 if( enet_host_service( this->client, &this->event, 100 ) >= 0 ) 99 { 100 char *addrconv = NULL; 101 /* check what type of event it is and react accordingly */ 102 switch (this->event.type) 103 { /* new connection, not supposed to happen. */ 104 case ENET_EVENT_TYPE_CONNECT: break; 105 106 /* disconnect */ 107 case ENET_EVENT_TYPE_DISCONNECT: 108 /* ?? */ break; 109 110 /* incoming data */ 111 case ENET_EVENT_TYPE_RECEIVE: 112 addrconv = (char *) calloc( 50, 1 ); 113 enet_address_get_host_ip( &(this->event.peer->address), addrconv, 49 ); 114 115 /* DEBUG */ 116 printf( "A packet of length %u containing %s was " 117 "received from %s on channel %u.\n", 118 this->event.packet->dataLength, 119 this->event.packet->data, 120 addrconv, 121 this->event.channelID ); 122 /* END DEBUG */ 123 124 /* call the supplied callback, if any. */ 125 if( (*callback) != NULL ) 126 (*callback)( addrconv, &(this->event) ); 127 128 enet_packet_destroy( event.packet ); 129 if( addrconv ) free( addrconv ); 130 break; 131 default: break; 132 } 133 } 134 135 return 0; 136 } 137 138 int 139 MasterServerComm::sendRequest( char *data ) 140 { 141 /* send the data to the friend */ 142 /* Create a reliable packet of size 7 containing "packet\0" */ 143 ENetPacket * packet = enet_packet_create( data, 144 strlen( data ) + 1, 145 ENET_PACKET_FLAG_RELIABLE); 146 147 /* Send the packet to the peer over channel id 0. */ 148 enet_peer_send (this->peer, 0, packet); 149 150 /* One could just use enet_host_service() instead. */ 151 enet_host_flush( this->client ); 152 if( packet ) free( packet ); 153 } 154 155 /* sample callback to output debugging info. */ 156 int callb( char *addr, ENetEvent *ev ) 157 { 158 printf( "A packet of length %u containing %s was " 159 "received from %s on channel %u.\n", 160 ev->packet->dataLength, 161 ev->packet->data, 162 addr, 163 ev->channelID ); 164 return 0; 165 } 166 167 /* small testing implementation */ 168 int 169 main( int argc, char *argv[] ) 170 { 171 /* setup object and connect */ 172 MasterServerComm msc = MasterServerComm(); 173 msc.connect( argv[1], 1234 ); 174 175 /* send some data and poll for replies */ 176 char *theinput = (char *)calloc( 100,1 ); 177 while( true ) 178 { 179 fgets( theinput, 90, stdin ); 180 if( !strncmp( theinput, "quit", 4 ) ) 181 break; 182 183 msc.sendRequest( theinput ); 184 msc.pollForReply( &callb ); 185 } 186 187 }
Note: See TracChangeset
for help on using the changeset viewer.