- Timestamp:
- Nov 3, 2010, 4:20:02 PM (14 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
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.