Changeset 7684
- Timestamp:
- Dec 1, 2010, 1:51:15 PM (14 years ago)
- Location:
- code/branches/masterserver/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/masterserver/src/libraries/network/MasterServerComm.cc
r7672 r7684 46 46 return 1; 47 47 } 48 49 /* initialize the event holder */ 50 this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 ); 48 51 49 52 /* install atexit handler for enet */ … … 89 92 90 93 /* Wait up to 2 seconds for the connection attempt to succeed. */ 91 if (enet_host_service (this->client, &this->event, 2000) > 0 &&92 this->event .type == ENET_EVENT_TYPE_CONNECT )94 if (enet_host_service (this->client, this->event, 2000) > 0 && 95 this->event->type == ENET_EVENT_TYPE_CONNECT ) 93 96 fprintf( stdout, "Connection to server succeeded." ); 94 97 else … … 107 110 /* see whether anything happened */ 108 111 /* WORK MARK REMOVE THIS OUTPUT */ 109 COUT(2) << "MARK polling...\n";112 //COUT(2) << "MARK polling...\n"; 110 113 111 114 /* enet_host_service returns 0 if no event occured */ 112 115 /* just newly set below test to >0 from >= 0, to be tested */ 113 if( enet_host_service( this->client, &this->event, 1000 ) > 0 )116 if( enet_host_service( this->client, this->event, 1000 ) > 0 ) 114 117 { 115 118 /* address buffer */ … … 118 121 119 122 /* check what type of event it is and react accordingly */ 120 switch (this->event .type)123 switch (this->event->type) 121 124 { /* new connection, not supposed to happen. */ 122 125 case ENET_EVENT_TYPE_CONNECT: break; … … 128 131 case ENET_EVENT_TYPE_RECEIVE: 129 132 addrconv = (char *) calloc( 50, 1 ); 130 enet_address_get_host_ip( &(this->event.peer->address), addrconv, 49 ); 133 enet_address_get_host_ip( &(this->event->peer->address), 134 addrconv, 49 ); 131 135 132 136 /* DEBUG */ 133 137 printf( "A packet of length %u containing %s was " 134 138 "received from %s on channel %u.\n", 135 this->event .packet->dataLength,136 this->event .packet->data,139 this->event->packet->dataLength, 140 this->event->packet->data, 137 141 addrconv, 138 this->event .channelID );142 this->event->channelID ); 139 143 /* END DEBUG */ 140 144 141 145 /* call the supplied callback, if any. */ 142 146 if( (*callback) != NULL ) 143 retval = (*callback)( addrconv, &(this->event) );144 145 enet_packet_destroy( event .packet );147 retval = (*callback)( addrconv, (this->event) ); 148 149 enet_packet_destroy( event->packet ); 146 150 if( addrconv ) free( addrconv ); 147 151 break; -
code/branches/masterserver/src/libraries/network/MasterServerComm.h
r7650 r7684 85 85 86 86 /** event data holder */ 87 ENetEvent event;87 ENetEvent *event; 88 88 89 89 /** address holder */ -
code/branches/masterserver/src/modules/masterserver/MasterServer.cc
r7666 r7684 36 36 /* singleton stuff */ 37 37 //ManageScopedSingleton( MasterServer, ScopeID::Root, false ); 38 39 /* helpers */ 40 static void 41 helper_output_debug( ENetEvent *event, char *addrconv ) 42 { 43 COUT(4) << "A packet of length" 44 << event->packet->dataLength 45 << " containing " 46 << (const char*)event->packet->data 47 << " was received from " 48 << addrconv 49 << " on channel " 50 << event->channelID << "\n"; 51 } 52 53 void 54 MasterServer::helper_sendlist( ENetEvent *event ) 55 { 56 /* get an iterator */ 57 std::list<packet::ServerInformation>::iterator i; 58 59 /* packet holder */ 60 ENetPacket *reply; 61 62 /* loop through list elements */ 63 for( i = mainlist.serverlist.begin(); i 64 != mainlist.serverlist.end(); ++i ) 65 { 66 /* WORK MARK */ 67 /* send this particular server */ 68 /* build reply string */ 69 char *tosend = (char *)calloc( (*i).getServerIP().length() 70 + MSPROTO_SERVERLIST_ITEM_LEN + 2,1 ); 71 if( !tosend ) 72 { COUT(2) << "Masterserver.cc: Memory allocation failed.\n"; 73 continue; 74 } 75 sprintf( tosend, "%s %s", MSPROTO_SERVERLIST_ITEM, 76 (*i).getServerIP().c_str() ); 77 78 /* create packet from it */ 79 reply = enet_packet_create( tosend, 80 strlen( tosend ) + 1, 81 ENET_PACKET_FLAG_RELIABLE); 82 83 /* Send the reply to the peer over channel id 0. */ 84 enet_peer_send( event->peer, 0, reply ); 85 86 /* One could just use enet_host_service() instead. */ 87 enet_host_flush( this->server ); 88 89 /* free the tosend buffer */ 90 free( tosend ); 91 } 92 93 /* send end-of-list packet */ 94 reply = enet_packet_create( MSPROTO_SERVERLIST_END, 95 MSPROTO_SERVERLIST_END_LEN + 1, 96 ENET_PACKET_FLAG_RELIABLE ); 97 98 enet_peer_send( event->peer, 0, reply ); 99 100 /* One could just use enet_host_service() instead. */ 101 enet_host_flush( this->server ); 102 } 103 104 105 38 106 39 107 /***** EVENTS *****/ … … 105 173 /* DEBUG */ 106 174 /* output debug info about the data that has come, to be removed */ 107 COUT(4) << "A packet of length" 108 << event->packet->dataLength 109 << " containing " 110 << (const char*)event->packet->data 111 << " was received from " 112 << addrconv 113 << " on channel " 114 << event->channelID << "\n"; 115 116 /* 117 //[> send some packet back for testing <] 118 //[> TESTING <] 119 120 //[> Create a reliable reply of size 7 containing "reply\0" <] 121 //ENetPacket * reply = enet_packet_create ("reply", 122 //strlen ("reply") + 1, 123 //ENET_PACKET_FLAG_RELIABLE); 124 125 //[> Send the reply to the peer over channel id 0. <] 126 //enet_peer_send( event->peer, 0, reply ); 127 128 //[> One could just use enet_host_service() instead. <] 129 //enet_host_flush( this->server ); 130 131 //[> /TESTING <] 132 */ 175 helper_output_debug( event, addrconv ); 133 176 134 177 /* GAME SERVER OR CLIENT CONNECTION? */ … … 151 194 MSPROTO_CLIENT_LEN) ) 152 195 { /* client */ 153 154 196 if( !strncmp( (char *)event->packet->data + MSPROTO_CLIENT_LEN+1, 155 197 MSPROTO_REQ_LIST, MSPROTO_REQ_LIST_LEN ) ) 156 { /* send server list */ 157 158 /* get an iterator */ 159 std::list<packet::ServerInformation>::iterator i; 160 161 /* packet holder */ 162 ENetPacket *reply; 163 164 /* loop through list elements */ 165 for( i = mainlist.serverlist.begin(); i != mainlist.serverlist.end(); ++i ) 166 { 167 /* WORK MARK */ 168 /* send this particular server */ 169 /* build reply string */ 170 char *tosend = (char *)calloc( (*i).getServerIP().length() + MSPROTO_SERVERLIST_ITEM_LEN + 2,1 ); 171 sprintf( tosend, "%s %s", MSPROTO_SERVERLIST_ITEM, (*i).getServerIP().c_str() ); 172 173 /* create packet from it */ 174 reply = enet_packet_create( tosend, 175 strlen( tosend ) + 1, 176 ENET_PACKET_FLAG_RELIABLE); 177 178 /* Send the reply to the peer over channel id 0. */ 179 enet_peer_send( event->peer, 0, reply ); 180 181 /* One could just use enet_host_service() instead. */ 182 enet_host_flush( this->server ); 183 } 184 185 /* send end-of-list packet */ 186 reply = enet_packet_create( MSPROTO_SERVERLIST_END, 187 MSPROTO_SERVERLIST_END_LEN + 1, 188 ENET_PACKET_FLAG_RELIABLE ); 189 190 enet_peer_send( event->peer, 0, reply ); 191 192 /* One could just use enet_host_service() instead. */ 193 enet_host_flush( this->server ); 194 195 } 198 /* send server list */ 199 helper_sendlist( event ); 196 200 } 197 201 else -
code/branches/masterserver/src/modules/masterserver/MasterServer.h
r7657 r7684 70 70 int eventData( ENetEvent *event ); 71 71 72 /* helpers */ 73 void helper_sendlist( ENetEvent *event ); 74 72 75 /* members */ 73 76 ENetAddress address;
Note: See TracChangeset
for help on using the changeset viewer.