- Timestamp:
- May 30, 2018, 2:56:24 PM (6 years ago)
- Location:
- code/branches/PresentationFS18
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/PresentationFS18
- Property svn:mergeinfo changed
/code/branches/Masterserver_FS18 (added) merged: 11816,11829,11842,11856,11858,11880,11889,11905,11907,11910,11929-11930,11937,11962-11963,11973,11983,11990
- Property svn:mergeinfo changed
-
code/branches/PresentationFS18/src/libraries/network/Connection.cc
r11071 r12020 38 38 39 39 #include "packet/Packet.h" 40 #include "util/Output.h" 40 41 #include <util/Sleep.h> 41 42 … … 45 46 const unsigned int NETWORK_DISCONNECT_TIMEOUT = 500; 46 47 48 /** 49 * Constructor 50 * @param firstPeerId The initial value of nextPeerID_ 51 */ 47 52 Connection::Connection(uint32_t firstPeerID): 48 53 host_(nullptr), bCommunicationThreadRunning_(false), nextPeerID_(firstPeerID) 49 54 { 55 // Global initialization of ENet 50 56 enet_initialize(); 57 58 // Register enet_deinitialize to be executed when the program exits normally 51 59 atexit(enet_deinitialize); 60 61 // Create mutexes for incoming and outgoing events 52 62 this->incomingEventsMutex_ = new boost::mutex; 53 63 this->outgoingEventsMutex_ = new boost::mutex; 54 // this->overallMutex_ = new boost::mutex; 55 } 56 64 } 65 66 /** 67 * Destructor 68 */ 57 69 Connection::~Connection() 58 70 { 71 // Delete the mutexes 59 72 delete this->incomingEventsMutex_; 60 73 delete this->outgoingEventsMutex_; 61 74 } 62 75 76 /** 77 * Start the main communication thread. 78 */ 63 79 void Connection::startCommunicationThread() 64 80 { … … 67 83 } 68 84 85 /** 86 * Stop the main communication thread. 87 */ 69 88 void Connection::stopCommunicationThread() 70 89 { 71 90 this->bCommunicationThreadRunning_ = false; 72 if( !this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME) ) 73 { 74 // force thread to stop 91 // Wait for peaceful termination 92 if(!this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME)) 93 { 94 // Force thread to stop if the waiting time runs out. 75 95 this->communicationThread_->interrupt(); 76 96 } … … 78 98 } 79 99 100 /** 101 * Send an outgoing event of type 'disconnectPeer'. 102 * @param peerID The peer to which the event is sent 103 */ 80 104 void Connection::disconnectPeer(uint32_t peerID) 81 105 { 82 // this->overallMutex_->lock();83 106 outgoingEvent outEvent = { peerID, OutgoingEventType::disconnectPeer, nullptr, 0 }; 84 107 … … 86 109 this->outgoingEvents_.push_back(outEvent); 87 110 this->outgoingEventsMutex_->unlock(); 88 // this->overallMutex_->unlock(); 89 } 90 111 } 112 113 /** 114 * Send an outgoing event of type 'disconnectPeers'. 115 */ 91 116 void Connection::disconnectPeers() 92 117 { … … 98 123 } 99 124 125 /** 126 * Send a packet. 127 * @param packet Pointer to the packet to send 128 * @param peerID The peer to which the event is sent 129 * @param channelId The channel ID 130 */ 100 131 void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID) 101 132 { 102 // this->overallMutex_->lock();103 133 outgoingEvent outEvent = { peerID, OutgoingEventType::sendPacket, packet, channelID }; 104 134 … … 106 136 this->outgoingEvents_.push_back(outEvent); 107 137 this->outgoingEventsMutex_->unlock(); 108 // this->overallMutex_->unlock(); 109 } 110 138 } 139 140 /** 141 * Send a broadcast packet. 142 * @param packet Pointer to the packet to send 143 * @param channelId The channel ID 144 */ 111 145 void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID) 112 146 { 113 // this->overallMutex_->lock();114 147 outgoingEvent outEvent = { 0, OutgoingEventType::broadcastPacket, packet, channelID }; 115 148 … … 117 150 this->outgoingEvents_.push_back(outEvent); 118 151 this->outgoingEventsMutex_->unlock(); 119 // this->overallMutex_->unlock(); 120 } 121 122 152 } 153 154 155 /** 156 * Main communication thread 157 */ 123 158 void Connection::communicationThread() 124 159 { 125 160 ENetEvent event; 126 161 127 // this->overallMutex_->lock(); 128 while( bCommunicationThreadRunning_ ) 162 while(this->bCommunicationThreadRunning_) 129 163 { 130 164 // Receive all pending incoming Events (such as packets, connects and disconnects) 131 while( enet_host_check_events( this->host_, &event ) > 0)165 while(enet_host_check_events(this->host_, &event ) > 0) 132 166 { 133 processIncomingEvent(event);167 this->processIncomingEvent(event); 134 168 } 135 169 136 // this->overallMutex_->unlock(); 170 // Sleep for 1ms 137 171 msleep(1); 138 // this->overallMutex_->lock();139 172 140 173 // Send all waiting outgoing packets … … 142 175 uint32_t outgoingEventsCount = this->outgoingEvents_.size(); 143 176 this->outgoingEventsMutex_->unlock(); 144 while( outgoingEventsCount > 0 ) 177 178 while(outgoingEventsCount > 0) 145 179 { 146 // orxout(verbose, context::network) << "outgoing event" << endl;147 180 this->outgoingEventsMutex_->lock(); 148 181 outgoingEvent outEvent = this->outgoingEvents_.front(); … … 150 183 this->outgoingEventsMutex_->unlock(); 151 184 152 processOutgoingEvent(outEvent);185 this->processOutgoingEvent(outEvent); 153 186 154 187 this->outgoingEventsMutex_->lock(); … … 158 191 159 192 // Wait for incoming events (at most NETWORK_WAIT_TIMEOUT ms) 160 if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0)193 if(enet_host_service(this->host_, &event, NETWORK_WAIT_TIMEOUT) > 0) 161 194 { 162 processIncomingEvent(event);195 this->processIncomingEvent(event); 163 196 } 164 197 } 165 // this->overallMutex_->unlock(); 166 } 167 198 } 199 200 /** 201 * Handle an incoming event. 202 * @param event The incoming event 203 */ 168 204 void Connection::processIncomingEvent(ENetEvent& event) 169 205 { 170 206 incomingEvent inEvent; 171 207 // preprocess event 172 switch( event.type)208 switch(event.type) 173 209 { 174 210 case ENET_EVENT_TYPE_CONNECT: … … 192 228 } 193 229 230 /** 231 * Send an event. 232 * @param event The event to send 233 */ 194 234 void Connection::processOutgoingEvent(outgoingEvent& event) 195 235 { 196 236 ENetPeer* peer; 197 switch( event.type)237 switch(event.type) 198 238 { 199 239 case OutgoingEventType::sendPacket: 200 240 // check whether the peer is still/already in the peer list 201 if( this->peerMap_.find(event.peerID) != this->peerMap_.end())241 if(this->peerMap_.find(event.peerID) != this->peerMap_.end()) 202 242 { 203 243 peer = this->peerMap_[event.peerID]; 204 enet_peer_send( peer, event.channelID, event.packet);244 enet_peer_send(peer, event.channelID, event.packet); 205 245 } 206 246 else 207 247 { 208 248 // peer probably already disconnected so just discard packet 209 assert(event.peerID<this->nextPeerID_);249 orxout(message) << "Trying to send packet to peer that is not in peer list. Ignoring packet." << endl; 210 250 enet_packet_destroy(event.packet); 211 251 } 212 252 break; 213 253 case OutgoingEventType::disconnectPeer: 214 if( this->peerMap_.find(event.peerID) != this->peerMap_.end())254 if(this->peerMap_.find(event.peerID) != this->peerMap_.end()) 215 255 { 216 256 peer = this->peerMap_[event.peerID]; … … 220 260 { 221 261 // peer probably already disconnected so just discard disconnect event 222 assert(event.peerID <this->nextPeerID_);262 assert(event.peerID < this->nextPeerID_); 223 263 } 224 264 break; 225 265 case OutgoingEventType::disconnectPeers: 226 disconnectPeersInternal();266 this->disconnectPeersInternal(); 227 267 break; 228 268 case OutgoingEventType::broadcastPacket: … … 234 274 } 235 275 236 237 276 void Connection::disconnectPeersInternal() 238 277 { … … 241 280 enet_peer_disconnect(mapEntry.second, 0); 242 281 } 243 uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT /NETWORK_WAIT_TIMEOUT;282 uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT / NETWORK_WAIT_TIMEOUT; 244 283 uint32_t i = 0; 245 284 while( this->peerMap_.size() && i++ < iterations ) … … 361 400 } 362 401 363 364 402 }
Note: See TracChangeset
for help on using the changeset viewer.