- Timestamp:
- Dec 28, 2010, 4:46:42 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network6/src/libraries/network/Connection.cc
r7801 r7823 44 44 45 45 Connection::Connection(): 46 host_(0), bCommunicationThreadRunning_(false) 46 host_(0), bCommunicationThreadRunning_(false), nextPeerID_(NETWORK_PEER_ID_SERVER+1) 47 47 { 48 48 enet_initialize(); … … 75 75 } 76 76 77 78 // int Connection::service(ENetEvent* event) { 79 // return enet_host_service( this->host_, event, NETWORK_WAIT_TIMEOUT ); 80 // } 81 82 void Connection::disconnectPeer(ENetPeer *peer) 83 { 84 assert(peer); 85 outgoingEvent outEvent = { peer, outgoingEventType::disconnectPeer, (ENetPacket*)10, 15 }; 77 void Connection::disconnectPeer(uint32_t peerID) 78 { 79 outgoingEvent outEvent = { peerID, outgoingEventType::disconnectPeer, 0, 0 }; 86 80 87 81 this->outgoingEventsMutex_->lock(); … … 89 83 this->outgoingEventsMutex_->unlock(); 90 84 } 91 92 void Connection::addPacket(ENetPacket *packet, ENetPeer *peer, uint8_t channelID) 93 { 94 assert(peer); 95 outgoingEvent outEvent = { peer, outgoingEventType::sendPacket, packet, channelID }; 85 86 void Connection::disconnectPeers() 87 { 88 outgoingEvent outEvent = { 0, outgoingEventType::disconnectPeers, 0, 0 }; 96 89 97 90 this->outgoingEventsMutex_->lock(); … … 99 92 this->outgoingEventsMutex_->unlock(); 100 93 } 101 102 void Connection:: broadcastPacket(ENetPacket* packet, uint8_t channelID)103 { 104 outgoingEvent outEvent = { (ENetPeer*)15, outgoingEventType::broadcastPacket, packet, channelID };94 95 void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID) 96 { 97 outgoingEvent outEvent = { peerID, outgoingEventType::sendPacket, packet, channelID }; 105 98 106 99 this->outgoingEventsMutex_->lock(); … … 108 101 this->outgoingEventsMutex_->unlock(); 109 102 } 103 104 void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID) 105 { 106 outgoingEvent outEvent = { 0, outgoingEventType::broadcastPacket, packet, channelID }; 107 108 this->outgoingEventsMutex_->lock(); 109 this->outgoingEvents_.push_back(outEvent); 110 this->outgoingEventsMutex_->unlock(); 111 } 110 112 111 113 … … 119 121 while( enet_host_check_events( this->host_, &event ) > 0 ) 120 122 { 121 // COUT(0) << "incoming event" << endl; 122 // received an event 123 this->incomingEventsMutex_->lock(); 124 this->incomingEvents_.push_back(event); 125 this->incomingEventsMutex_->unlock(); 123 processIncomingEvent(event); 126 124 } 127 125 … … 138 136 this->outgoingEventsMutex_->unlock(); 139 137 140 switch( outEvent.type ) 141 { 142 case outgoingEventType::sendPacket: 143 enet_peer_send( outEvent.peer, outEvent.channelID, outEvent.packet ); 144 break; 145 case outgoingEventType::disconnectPeer: 146 enet_peer_disconnect(outEvent.peer, 0); 147 break; 148 case outgoingEventType::broadcastPacket: 149 enet_host_broadcast( this->host_, outEvent.channelID, outEvent.packet ); 150 break; 151 default: 152 assert(0); 153 } 138 processOutgoingEvent(outEvent); 139 154 140 this->outgoingEventsMutex_->lock(); 155 141 outgoingEventsCount = this->outgoingEvents_.size(); … … 160 146 if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 ) 161 147 { 162 // COUT(0) << "incoming event after wait" << endl; 163 //received an event 164 this->incomingEventsMutex_->lock(); 165 this->incomingEvents_.push_back(event); 166 this->incomingEventsMutex_->unlock(); 148 processIncomingEvent(event); 167 149 } 168 150 } 169 151 } 152 153 void Connection::processIncomingEvent(ENetEvent& event) 154 { 155 incomingEvent inEvent; 156 // preprocess event 157 switch( event.type ) 158 { 159 case ENET_EVENT_TYPE_CONNECT: 160 inEvent = preprocessConnectEvent(event); 161 break; 162 case ENET_EVENT_TYPE_RECEIVE: 163 inEvent = preprocessReceiveEvent(event); 164 break; 165 case ENET_EVENT_TYPE_DISCONNECT: 166 inEvent = preprocessDisconnectEvent(event); 167 break; 168 case ENET_EVENT_TYPE_NONE: 169 default: 170 return; 171 } 172 173 // pushing event to queue 174 this->incomingEventsMutex_->lock(); 175 this->incomingEvents_.push_back(inEvent); 176 this->incomingEventsMutex_->unlock(); 177 } 178 179 void Connection::processOutgoingEvent(outgoingEvent& event) 180 { 181 ENetPeer* peer; 182 switch( event.type ) 183 { 184 case outgoingEventType::sendPacket: 185 assert(this->peerMap_.find(event.peerID) != this->peerMap_.end()); 186 peer = this->peerMap_[event.peerID]; 187 enet_peer_send( peer, event.channelID, event.packet ); 188 break; 189 case outgoingEventType::disconnectPeer: 190 assert(this->peerMap_.find(event.peerID) != this->peerMap_.end()); 191 peer = this->peerMap_[event.peerID]; 192 enet_peer_disconnect(peer, 0); 193 break; 194 case outgoingEventType::disconnectPeers: 195 while( this->peerMap_.size()!=0 ) 196 { 197 peer = this->peerMap_.begin()->second; 198 enet_peer_disconnect(peer, 0); 199 } 200 break; 201 case outgoingEventType::broadcastPacket: 202 enet_host_broadcast( this->host_, event.channelID, event.packet ); 203 break; 204 default: 205 assert(0); 206 } 207 } 208 170 209 171 210 void Connection::processQueue() 172 211 { 173 ENetEvent event;212 incomingEvent inEvent; 174 213 175 214 this->incomingEventsMutex_->lock(); … … 178 217 while( incomingEventsCount > 0 ) 179 218 { 180 packet::Packet* p;219 // pop event from queue 181 220 this->incomingEventsMutex_->lock(); 182 event = this->incomingEvents_.front();221 inEvent = this->incomingEvents_.front(); 183 222 this->incomingEvents_.pop_front(); 184 223 this->incomingEventsMutex_->unlock(); 185 224 186 switch(event.type) 225 // process event 226 switch( inEvent.type ) 187 227 { 188 // log handling ================ 189 case ENET_EVENT_TYPE_CONNECT: 190 addPeer( &event ); 228 case incomingEventType::peerConnect: 229 addPeer(inEvent.peerID); 191 230 break; 192 case ENET_EVENT_TYPE_DISCONNECT:193 removePeer( &event);231 case incomingEventType::peerDisconnect: 232 removePeer(inEvent.peerID); 194 233 break; 195 case ENET_EVENT_TYPE_RECEIVE: 196 // COUT(0) << "ENET_EVENT_TYPE_RECEIVE" << endl; 197 p = createPacket( &event ); 198 processPacket(p); 234 case incomingEventType::receivePacket: 235 processPacket(inEvent.packet); 199 236 break; 200 case ENET_EVENT_TYPE_NONE:237 default: 201 238 break; 202 239 } 203 240 241 // check whether there are still events in the queue 204 242 this->incomingEventsMutex_->lock(); 205 243 incomingEventsCount = this->incomingEvents_.size(); … … 208 246 } 209 247 210 packet::Packet* Connection::createPacket(ENetEvent* event) 211 { 212 packet::Packet *p = packet::Packet::createPacket(event->packet, event->peer); 213 return p; 214 // return p->process(); 215 } 248 incomingEvent Connection::preprocessConnectEvent(ENetEvent& event) 249 { 250 // make sure this peer doesn't exist 251 assert( this->peerMap_.find(this->nextPeerID_) == this->peerMap_.end() ); 252 assert( this->peerIDMap_.find(event.peer) == this->peerIDMap_.end() ); 253 254 // give peer a new id and increase peerID for next peer 255 uint32_t peerID = this->nextPeerID_; 256 ++this->nextPeerID_; 257 258 // add peer/peerID into peerMap_ and peerIDMap_ 259 this->peerMap_[peerID] = event.peer; 260 this->peerIDMap_[event.peer] = peerID; 261 262 // create new peerEvent and return it 263 incomingEvent inEvent = { peerID, incomingEventType::peerConnect, 0 }; 264 return inEvent; 265 } 266 267 incomingEvent Connection::preprocessDisconnectEvent(ENetEvent& event) 268 { 269 // assert that the peer exists and get peerID 270 assert( this->peerIDMap_.find(event.peer) != this->peerIDMap_.end() ); 271 uint32_t peerID = this->peerIDMap_[event.peer]; 272 273 // remove peer/peerID from maps 274 this->peerIDMap_.erase(this->peerIDMap_.find(event.peer)); 275 this->peerMap_.erase(this->peerMap_.find(peerID)); 276 277 // create new peerEvent and return it 278 incomingEvent inEvent = { peerID, incomingEventType::peerDisconnect, 0 }; 279 return inEvent; 280 } 281 282 incomingEvent Connection::preprocessReceiveEvent(ENetEvent& event) 283 { 284 // assert that the peer exists and get peerID 285 assert( this->peerIDMap_.find(event.peer) != this->peerIDMap_.end() ); 286 uint32_t peerID = this->peerIDMap_[event.peer]; 287 288 // create new Packet from ENetPacket 289 packet::Packet* p = packet::Packet::createPacket(event.packet, peerID); 290 291 // create new peerEvent and return it 292 incomingEvent inEvent = { peerID, incomingEventType::receivePacket, p }; 293 return inEvent; 294 } 295 216 296 217 297 void Connection::enableCompression()
Note: See TracChangeset
for help on using the changeset viewer.