Changeset 1502 for code/trunk/src/network/ClientConnection.cc
- Timestamp:
- Jun 1, 2008, 3:54:20 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/network/ClientConnection.cc
r1293 r1502 50 50 { 51 51 //static boost::thread_group network_threads; 52 53 boost::recursive_mutex ClientConnection::enet_mutex_; 52 54 53 55 ClientConnection::ClientConnection(int port, std::string address) { … … 75 77 76 78 77 ENetPacket *ClientConnection::getPacket(ENetAddress &address) {79 /*ENetPacket *ClientConnection::getPacket(ENetAddress &address) { 78 80 if(!buffer.isEmpty()) { 79 81 //std::cout << "###BUFFER IS NOT EMPTY###" << std::endl; … … 88 90 ENetAddress address; //sems that address is not needed 89 91 return getPacket(address); 92 }*/ 93 94 ENetEvent *ClientConnection::getEvent(){ 95 if(!buffer.isEmpty()) 96 return buffer.pop(); 97 else 98 return NULL; 90 99 } 91 100 … … 98 107 //network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this)); 99 108 // wait 10 seconds for the connection to be established 100 return waitEstablished( 10000);109 return waitEstablished(3000); 101 110 } 102 111 … … 117 126 return false; 118 127 } 128 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 119 129 if(enet_peer_send(server, 0, packet)<0) 120 130 return false; … … 123 133 } 124 134 125 bool ClientConnection::sendPackets( ENetEvent *event) {135 bool ClientConnection::sendPackets() { 126 136 if(server==NULL) 127 137 return false; 128 if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0){ 129 return true;} 130 else 131 return false; 132 } 133 134 bool ClientConnection::sendPackets() { 135 ENetEvent event; 136 if(server==NULL) 137 return false; 138 if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0){ 139 return true;} 140 else 141 return false; 138 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 139 enet_host_flush(client); 140 lock.unlock(); 141 return true; 142 142 } 143 143 144 144 void ClientConnection::receiverThread() { 145 145 // what about some error-handling here ? 146 enet_initialize();147 146 atexit(enet_deinitialize); 148 ENetEvent event; 149 client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0); 150 if(client==NULL) 147 ENetEvent *event; 148 { 149 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 150 enet_initialize(); 151 client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0); 152 lock.unlock(); 153 } 154 if(client==NULL) { 155 COUT(2) << "ClientConnection: could not create client host" << std::endl; 151 156 // add some error handling here ========================== 152 157 quit=true; 158 } 153 159 //connect to the server 154 160 if(!establishConnection()){ 161 COUT(2) << "clientConn: receiver thread: could not establishConnection" << std::endl; 155 162 quit=true; 156 163 return; 157 164 } 165 event = new ENetEvent; 158 166 //main loop 159 167 while(!quit){ 160 168 //std::cout << "connection loop" << std::endl; 161 if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)<0){ 162 // we should never reach this point 163 quit=true; 164 // add some error handling here ======================== 169 { 170 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 171 if(enet_host_service(client, event, NETWORK_CLIENT_TIMEOUT)<0){ 172 // we should never reach this point 173 quit=true; 174 continue; 175 // add some error handling here ======================== 176 } 177 lock.unlock(); 165 178 } 166 switch(event .type){179 switch(event->type){ 167 180 // log handling ================ 168 181 case ENET_EVENT_TYPE_CONNECT: 182 break; 169 183 case ENET_EVENT_TYPE_RECEIVE: 170 184 COUT(5) << "Cl.Con: receiver-Thread while loop: got new packet" << std::endl; 171 if ( !processData( &event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl;185 if ( !processData(event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl; 172 186 COUT(5) << "Cl.Con: processed Data in receiver-thread while loop" << std::endl; 187 event = new ENetEvent; 173 188 break; 174 189 case ENET_EVENT_TYPE_DISCONNECT: … … 178 193 break; 179 194 case ENET_EVENT_TYPE_NONE: 180 continue; 195 //receiverThread_->yield(); 196 usleep(1000); 197 break; 181 198 } 182 //receiverThread_->yield();183 199 } 184 200 // now disconnect … … 186 202 if(!disconnectConnection()) 187 203 // if disconnecting failed destroy conn. 204 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 188 205 enet_peer_reset(server); 189 206 return; … … 192 209 bool ClientConnection::disconnectConnection() { 193 210 ENetEvent event; 211 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 194 212 enet_peer_disconnect(server, 0); 195 213 while(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT) > 0){ … … 212 230 ENetEvent event; 213 231 // connect to peer (server is type ENetPeer*) 232 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 214 233 server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS); 215 if(server==NULL) 234 if(server==NULL) { 235 COUT(2) << "ClientConnection: server == NULL" << std::endl; 216 236 // error handling 217 237 return false; 238 } 218 239 // handshake 219 if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)> 0 && event.type == ENET_EVENT_TYPE_CONNECT){240 if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>=0 && event.type == ENET_EVENT_TYPE_CONNECT){ 220 241 established=true; 221 242 return true; 222 243 } 223 else 224 return false; 244 else { 245 COUT(2) << "ClientConnection: enet_host_service < 0 or event.type != ENET_EVENT_TYPE_CONNECT # EVENT:" << event.type << std::endl; 246 return false; 247 } 225 248 } 226 249
Note: See TracChangeset
for help on using the changeset viewer.