- Timestamp:
- Feb 13, 2011, 9:34:22 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network6/src/libraries/network/Connection.cc
r7825 r7878 38 38 39 39 #include "packet/Packet.h" 40 #include <util/Sleep.h> 40 41 41 42 namespace orxonox 42 43 { 43 44 const boost::posix_time::millisec NETWORK_COMMUNICATION_THREAD_WAIT_TIME(20); 45 const unsigned int NETWORK_DISCONNECT_TIMEOUT = 500; 44 46 45 47 Connection::Connection(uint32_t firstPeerID): … … 50 52 this->incomingEventsMutex_ = new boost::mutex; 51 53 this->outgoingEventsMutex_ = new boost::mutex; 54 this->overallMutex_ = new boost::mutex; 52 55 } 53 56 … … 77 80 void Connection::disconnectPeer(uint32_t peerID) 78 81 { 82 this->overallMutex_->lock(); 79 83 outgoingEvent outEvent = { peerID, outgoingEventType::disconnectPeer, 0, 0 }; 80 84 … … 82 86 this->outgoingEvents_.push_back(outEvent); 83 87 this->outgoingEventsMutex_->unlock(); 88 this->overallMutex_->unlock(); 84 89 } 85 90 … … 95 100 void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID) 96 101 { 102 this->overallMutex_->lock(); 97 103 outgoingEvent outEvent = { peerID, outgoingEventType::sendPacket, packet, channelID }; 98 104 … … 100 106 this->outgoingEvents_.push_back(outEvent); 101 107 this->outgoingEventsMutex_->unlock(); 108 this->overallMutex_->unlock(); 102 109 } 103 110 104 111 void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID) 105 112 { 113 this->overallMutex_->lock(); 106 114 outgoingEvent outEvent = { 0, outgoingEventType::broadcastPacket, packet, channelID }; 107 115 … … 109 117 this->outgoingEvents_.push_back(outEvent); 110 118 this->outgoingEventsMutex_->unlock(); 119 this->overallMutex_->unlock(); 111 120 } 112 121 … … 116 125 ENetEvent event; 117 126 127 this->overallMutex_->lock(); 118 128 while( bCommunicationThreadRunning_ ) 119 129 { … … 123 133 processIncomingEvent(event); 124 134 } 135 136 this->overallMutex_->unlock(); 137 msleep(10); 138 this->overallMutex_->lock(); 125 139 126 140 // Send all waiting outgoing packets … … 149 163 } 150 164 } 165 this->overallMutex_->unlock(); 151 166 } 152 167 … … 209 224 break; 210 225 case outgoingEventType::disconnectPeers: 211 while( this->peerMap_.size()!=0 ) 212 { 213 peer = this->peerMap_.begin()->second; 214 enet_peer_disconnect(peer, 0); 215 } 226 disconnectPeersInternal(); 216 227 break; 217 228 case outgoingEventType::broadcastPacket: … … 223 234 } 224 235 236 237 void Connection::disconnectPeersInternal() 238 { 239 std::map<uint32_t, ENetPeer*>::iterator it; 240 for( it=this->peerMap_.begin(); it!=this->peerMap_.end(); ++it ) 241 { 242 enet_peer_disconnect(it->second, 0); 243 } 244 uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT/NETWORK_WAIT_TIMEOUT; 245 uint32_t i = 0; 246 while( this->peerMap_.size() && i++ < iterations ) 247 { 248 ENetEvent event; 249 if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 ) 250 { 251 processIncomingEvent(event); 252 } 253 } 254 } 225 255 226 256 void Connection::processQueue() … … 261 291 } 262 292 } 293 294 void Connection::waitOutgoingQueue() 295 { 296 uint32_t outgoingEventsCount; 297 this->outgoingEventsMutex_->lock(); 298 outgoingEventsCount = this->outgoingEvents_.size(); 299 this->outgoingEventsMutex_->unlock(); 300 while( outgoingEventsCount ) 301 { 302 msleep(1); 303 this->outgoingEventsMutex_->lock(); 304 outgoingEventsCount = this->outgoingEvents_.size(); 305 this->outgoingEventsMutex_->unlock(); 306 } 307 } 308 263 309 264 310 incomingEvent Connection::preprocessConnectEvent(ENetEvent& event)
Note: See TracChangeset
for help on using the changeset viewer.