- Timestamp:
- Mar 22, 2018, 4:12:23 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/Masterserver_FS18/src/libraries/network/Connection.cc
r11071 r11829 45 45 const unsigned int NETWORK_DISCONNECT_TIMEOUT = 500; 46 46 47 /** 48 * Constructor 49 * @param firstPeerId The initial value of nextPeerID_ 50 */ 47 51 Connection::Connection(uint32_t firstPeerID): 48 52 host_(nullptr), bCommunicationThreadRunning_(false), nextPeerID_(firstPeerID) 49 53 { 54 // Global initialization of ENet 50 55 enet_initialize(); 56 57 // Register enet_deinitialize to be executed when the program exits normally 51 58 atexit(enet_deinitialize); 59 60 // Create mutexes for incoming and outgoing events 52 61 this->incomingEventsMutex_ = new boost::mutex; 53 62 this->outgoingEventsMutex_ = new boost::mutex; 54 // this->overallMutex_ = new boost::mutex; 55 } 56 63 } 64 65 /** 66 * Destructor 67 */ 57 68 Connection::~Connection() 58 69 { 70 // Delete the mutexes 59 71 delete this->incomingEventsMutex_; 60 72 delete this->outgoingEventsMutex_; 61 } 62 73 74 // TODO: Why is enet_deinitialize() not called here? 75 // Would make sense, since its counterpart, enet_initialize(), is called in the constructor. 76 } 77 78 /** 79 * Start the main communication thread. 80 */ 63 81 void Connection::startCommunicationThread() 64 82 { … … 67 85 } 68 86 87 /** 88 * Stop the main communication thread. 89 */ 69 90 void Connection::stopCommunicationThread() 70 91 { 71 92 this->bCommunicationThreadRunning_ = false; 72 if( !this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME) ) 73 { 74 // force thread to stop 93 // Wait for peaceful termination 94 if(!this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME)) 95 { 96 // Force thread to stop if the waiting time runs out. 75 97 this->communicationThread_->interrupt(); 76 98 } … … 78 100 } 79 101 102 /** 103 * Send an outgoing event of type 'disconnectPeer'. 104 * @param peerID The peer to which the event is sent 105 */ 80 106 void Connection::disconnectPeer(uint32_t peerID) 81 107 { 82 // this->overallMutex_->lock();83 108 outgoingEvent outEvent = { peerID, OutgoingEventType::disconnectPeer, nullptr, 0 }; 84 109 … … 86 111 this->outgoingEvents_.push_back(outEvent); 87 112 this->outgoingEventsMutex_->unlock(); 88 // this->overallMutex_->unlock(); 89 } 90 113 } 114 115 /** 116 * Send an outgoing event of type 'disconnectPeers'. 117 */ 91 118 void Connection::disconnectPeers() 92 119 { … … 98 125 } 99 126 127 /** 128 * Send a packet. 129 * @param packet Pointer to the packet to send 130 * @param peerID The peer to which the event is sent 131 * @param channelId The channel ID 132 */ 100 133 void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID) 101 134 { 102 // this->overallMutex_->lock();103 135 outgoingEvent outEvent = { peerID, OutgoingEventType::sendPacket, packet, channelID }; 104 136 … … 106 138 this->outgoingEvents_.push_back(outEvent); 107 139 this->outgoingEventsMutex_->unlock(); 108 // this->overallMutex_->unlock(); 109 } 110 140 } 141 142 /** 143 * Send a broadcast packet. 144 * @param packet Pointer to the packet to send 145 * @param channelId The channel ID 146 */ 111 147 void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID) 112 148 { 113 // this->overallMutex_->lock();114 149 outgoingEvent outEvent = { 0, OutgoingEventType::broadcastPacket, packet, channelID }; 115 150 … … 117 152 this->outgoingEvents_.push_back(outEvent); 118 153 this->outgoingEventsMutex_->unlock(); 119 // this->overallMutex_->unlock(); 120 } 121 122 154 } 155 156 157 /** 158 * Main communication thread 159 */ 123 160 void Connection::communicationThread() 124 161 { 125 162 ENetEvent event; 126 163 127 // this->overallMutex_->lock(); 128 while( bCommunicationThreadRunning_ ) 164 while(this->bCommunicationThreadRunning_) 129 165 { 130 166 // Receive all pending incoming Events (such as packets, connects and disconnects) 131 while( enet_host_check_events( this->host_, &event ) > 0)167 while(enet_host_check_events(this->host_, &event ) > 0) 132 168 { 133 processIncomingEvent(event);169 this->processIncomingEvent(event); 134 170 } 135 171 136 // this->overallMutex_->unlock(); 172 // Sleep for 1ms 173 // TODO: Why? 137 174 msleep(1); 138 // this->overallMutex_->lock();139 175 140 176 // Send all waiting outgoing packets 177 // TODO: Why do we need a mutex to read a single variable? 141 178 this->outgoingEventsMutex_->lock(); 142 179 uint32_t outgoingEventsCount = this->outgoingEvents_.size(); 143 180 this->outgoingEventsMutex_->unlock(); 144 while( outgoingEventsCount > 0 ) 181 182 // TODO: Not convinced about how mutexes are used here, seems kinda pointless 183 while(outgoingEventsCount > 0) 145 184 { 146 // orxout(verbose, context::network) << "outgoing event" << endl;147 185 this->outgoingEventsMutex_->lock(); 148 186 outgoingEvent outEvent = this->outgoingEvents_.front(); … … 150 188 this->outgoingEventsMutex_->unlock(); 151 189 152 processOutgoingEvent(outEvent);190 this->processOutgoingEvent(outEvent); 153 191 154 192 this->outgoingEventsMutex_->lock(); … … 158 196 159 197 // Wait for incoming events (at most NETWORK_WAIT_TIMEOUT ms) 160 if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0)198 if(enet_host_service(this->host_, &event, NETWORK_WAIT_TIMEOUT) > 0) 161 199 { 162 processIncomingEvent(event);200 this->processIncomingEvent(event); 163 201 } 164 202 } 165 // this->overallMutex_->unlock(); 166 } 167 203 } 204 205 /** 206 * Handle an incoming event. 207 * @param event The incoming event 208 */ 168 209 void Connection::processIncomingEvent(ENetEvent& event) 169 210 { 170 211 incomingEvent inEvent; 171 212 // preprocess event 172 switch( event.type)213 switch(event.type) 173 214 { 174 215 case ENET_EVENT_TYPE_CONNECT: … … 192 233 } 193 234 235 /** 236 * Send an event. 237 * @param event The event to send 238 */ 194 239 void Connection::processOutgoingEvent(outgoingEvent& event) 195 240 { 196 241 ENetPeer* peer; 197 switch( event.type)242 switch(event.type) 198 243 { 199 244 case OutgoingEventType::sendPacket: 200 245 // check whether the peer is still/already in the peer list 201 if( this->peerMap_.find(event.peerID) != this->peerMap_.end())246 if(this->peerMap_.find(event.peerID) != this->peerMap_.end()) 202 247 { 203 248 peer = this->peerMap_[event.peerID]; 204 enet_peer_send( peer, event.channelID, event.packet);249 enet_peer_send(peer, event.channelID, event.packet); 205 250 } 206 251 else 207 252 { 208 253 // peer probably already disconnected so just discard packet 209 assert(event.peerID <this->nextPeerID_);254 assert(event.peerID < this->nextPeerID_); 210 255 enet_packet_destroy(event.packet); 211 256 } 212 257 break; 213 258 case OutgoingEventType::disconnectPeer: 214 if( this->peerMap_.find(event.peerID) != this->peerMap_.end())259 if(this->peerMap_.find(event.peerID) != this->peerMap_.end()) 215 260 { 216 261 peer = this->peerMap_[event.peerID]; … … 220 265 { 221 266 // peer probably already disconnected so just discard disconnect event 222 assert(event.peerID <this->nextPeerID_);267 assert(event.peerID < this->nextPeerID_); 223 268 } 224 269 break; 225 270 case OutgoingEventType::disconnectPeers: 226 disconnectPeersInternal();271 this->disconnectPeersInternal(); 227 272 break; 228 273 case OutgoingEventType::broadcastPacket: … … 234 279 } 235 280 236 237 281 void Connection::disconnectPeersInternal() 238 282 { … … 241 285 enet_peer_disconnect(mapEntry.second, 0); 242 286 } 243 uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT /NETWORK_WAIT_TIMEOUT;287 uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT / NETWORK_WAIT_TIMEOUT; 244 288 uint32_t i = 0; 245 289 while( this->peerMap_.size() && i++ < iterations )
Note: See TracChangeset
for help on using the changeset viewer.