Index: code/branches/network6/src/libraries/network/Connection.cc
===================================================================
--- code/branches/network6/src/libraries/network/Connection.cc	(revision 7822)
+++ code/branches/network6/src/libraries/network/Connection.cc	(revision 7823)
@@ -44,5 +44,5 @@
 
   Connection::Connection():
-    host_(0), bCommunicationThreadRunning_(false)
+    host_(0), bCommunicationThreadRunning_(false), nextPeerID_(NETWORK_PEER_ID_SERVER+1)
   {
     enet_initialize();
@@ -75,13 +75,7 @@
   }
 
-
-//   int Connection::service(ENetEvent* event) {
-//     return enet_host_service( this->host_, event, NETWORK_WAIT_TIMEOUT );
-//   }
-
-  void Connection::disconnectPeer(ENetPeer *peer)
-  {
-    assert(peer);
-    outgoingEvent outEvent = { peer, outgoingEventType::disconnectPeer, (ENetPacket*)10, 15 };
+  void Connection::disconnectPeer(uint32_t peerID)
+  {
+    outgoingEvent outEvent = { peerID, outgoingEventType::disconnectPeer, 0, 0 };
     
     this->outgoingEventsMutex_->lock();
@@ -89,9 +83,8 @@
     this->outgoingEventsMutex_->unlock();
   }
-
-  void Connection::addPacket(ENetPacket *packet, ENetPeer *peer, uint8_t channelID)
-  {
-    assert(peer);
-    outgoingEvent outEvent = { peer, outgoingEventType::sendPacket, packet, channelID };
+  
+  void Connection::disconnectPeers()
+  {
+    outgoingEvent outEvent = { 0, outgoingEventType::disconnectPeers, 0, 0 };
     
     this->outgoingEventsMutex_->lock();
@@ -99,8 +92,8 @@
     this->outgoingEventsMutex_->unlock();
   }
-  
-  void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID)
-  {
-    outgoingEvent outEvent = { (ENetPeer*)15, outgoingEventType::broadcastPacket, packet, channelID };
+
+  void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID)
+  {
+    outgoingEvent outEvent = { peerID, outgoingEventType::sendPacket, packet, channelID };
     
     this->outgoingEventsMutex_->lock();
@@ -108,4 +101,13 @@
     this->outgoingEventsMutex_->unlock();
   }
+  
+  void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID)
+  {
+    outgoingEvent outEvent = { 0, outgoingEventType::broadcastPacket, packet, channelID };
+    
+    this->outgoingEventsMutex_->lock();
+    this->outgoingEvents_.push_back(outEvent);
+    this->outgoingEventsMutex_->unlock();
+  }
 
   
@@ -119,9 +121,5 @@
       while( enet_host_check_events( this->host_, &event ) > 0 )
       {
-//         COUT(0) << "incoming event" << endl;
-        // received an event
-        this->incomingEventsMutex_->lock();
-        this->incomingEvents_.push_back(event);
-        this->incomingEventsMutex_->unlock();
+        processIncomingEvent(event);
       }
       
@@ -138,18 +136,6 @@
         this->outgoingEventsMutex_->unlock();
         
-        switch( outEvent.type )
-        {
-          case outgoingEventType::sendPacket:
-            enet_peer_send( outEvent.peer, outEvent.channelID, outEvent.packet );
-            break;
-          case outgoingEventType::disconnectPeer:
-            enet_peer_disconnect(outEvent.peer, 0);
-            break;
-          case outgoingEventType::broadcastPacket:
-            enet_host_broadcast( this->host_, outEvent.channelID, outEvent.packet );
-            break;
-          default:
-            assert(0);
-        }
+        processOutgoingEvent(outEvent);
+        
         this->outgoingEventsMutex_->lock();
         outgoingEventsCount = this->outgoingEvents_.size();
@@ -160,16 +146,69 @@
       if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
       {
-//         COUT(0) << "incoming event after wait" << endl;
-        //received an event
-        this->incomingEventsMutex_->lock();
-        this->incomingEvents_.push_back(event);
-        this->incomingEventsMutex_->unlock();
+        processIncomingEvent(event);
       }
     }
   }
+  
+  void Connection::processIncomingEvent(ENetEvent& event)
+  {
+    incomingEvent inEvent;
+    // preprocess event
+    switch( event.type )
+    {
+      case ENET_EVENT_TYPE_CONNECT:
+        inEvent = preprocessConnectEvent(event);
+        break;
+      case ENET_EVENT_TYPE_RECEIVE:
+        inEvent = preprocessReceiveEvent(event);
+        break;
+      case ENET_EVENT_TYPE_DISCONNECT:
+        inEvent = preprocessDisconnectEvent(event);
+        break;
+      case ENET_EVENT_TYPE_NONE:
+      default:
+        return;
+    }
+    
+    // pushing event to queue
+    this->incomingEventsMutex_->lock();
+    this->incomingEvents_.push_back(inEvent);
+    this->incomingEventsMutex_->unlock();
+  }
+  
+  void Connection::processOutgoingEvent(outgoingEvent& event)
+  {
+    ENetPeer* peer;
+    switch( event.type )
+    {
+      case outgoingEventType::sendPacket:
+        assert(this->peerMap_.find(event.peerID) != this->peerMap_.end());
+        peer = this->peerMap_[event.peerID];
+        enet_peer_send( peer, event.channelID, event.packet );
+        break;
+      case outgoingEventType::disconnectPeer:
+        assert(this->peerMap_.find(event.peerID) != this->peerMap_.end());
+        peer = this->peerMap_[event.peerID];
+        enet_peer_disconnect(peer, 0);
+        break;
+      case outgoingEventType::disconnectPeers:
+        while( this->peerMap_.size()!=0 )
+        {
+          peer = this->peerMap_.begin()->second;
+          enet_peer_disconnect(peer, 0);
+        }
+        break;
+      case outgoingEventType::broadcastPacket:
+        enet_host_broadcast( this->host_, event.channelID, event.packet );
+        break;
+      default:
+        assert(0);
+    }
+  }
+
 
   void Connection::processQueue()
   {
-    ENetEvent event;
+    incomingEvent inEvent;
 
     this->incomingEventsMutex_->lock();
@@ -178,28 +217,27 @@
     while( incomingEventsCount > 0 )
     {
-      packet::Packet* p;
+      // pop event from queue
       this->incomingEventsMutex_->lock();
-      event = this->incomingEvents_.front();
+      inEvent = this->incomingEvents_.front();
       this->incomingEvents_.pop_front();
       this->incomingEventsMutex_->unlock();
       
-      switch(event.type)
+      // process event
+      switch( inEvent.type )
       {
-        // log handling ================
-        case ENET_EVENT_TYPE_CONNECT:
-          addPeer( &event );
+        case incomingEventType::peerConnect:
+          addPeer(inEvent.peerID);
           break;
-        case ENET_EVENT_TYPE_DISCONNECT:
-          removePeer( &event );
+        case incomingEventType::peerDisconnect:
+          removePeer(inEvent.peerID);
           break;
-        case ENET_EVENT_TYPE_RECEIVE:
-//           COUT(0) << "ENET_EVENT_TYPE_RECEIVE" << endl;
-          p = createPacket( &event );
-          processPacket(p);
+        case incomingEventType::receivePacket:
+          processPacket(inEvent.packet);
           break;
-        case ENET_EVENT_TYPE_NONE:
+        default:
           break;
       }
       
+      // check whether there are still events in the queue
       this->incomingEventsMutex_->lock();
       incomingEventsCount = this->incomingEvents_.size();
@@ -208,10 +246,52 @@
   }
 
-  packet::Packet* Connection::createPacket(ENetEvent* event)
-  {
-    packet::Packet *p = packet::Packet::createPacket(event->packet, event->peer);
-    return p;
-//     return p->process();
-  }
+  incomingEvent Connection::preprocessConnectEvent(ENetEvent& event)
+  {
+    // make sure this peer doesn't exist
+    assert( this->peerMap_.find(this->nextPeerID_) == this->peerMap_.end() );
+    assert( this->peerIDMap_.find(event.peer) == this->peerIDMap_.end() );
+    
+    // give peer a new id and increase peerID for next peer
+    uint32_t peerID = this->nextPeerID_;
+    ++this->nextPeerID_;
+    
+    // add peer/peerID into peerMap_ and peerIDMap_
+    this->peerMap_[peerID] = event.peer;
+    this->peerIDMap_[event.peer] = peerID;
+    
+    // create new peerEvent and return it
+    incomingEvent inEvent = { peerID, incomingEventType::peerConnect, 0 };
+    return inEvent;
+  }
+  
+  incomingEvent Connection::preprocessDisconnectEvent(ENetEvent& event)
+  {
+    // assert that the peer exists and get peerID
+    assert( this->peerIDMap_.find(event.peer) != this->peerIDMap_.end() );
+    uint32_t peerID = this->peerIDMap_[event.peer];
+    
+    // remove peer/peerID from maps
+    this->peerIDMap_.erase(this->peerIDMap_.find(event.peer));
+    this->peerMap_.erase(this->peerMap_.find(peerID));
+    
+    // create new peerEvent and return it
+    incomingEvent inEvent = { peerID, incomingEventType::peerDisconnect, 0 };
+    return inEvent;
+  }
+  
+  incomingEvent Connection::preprocessReceiveEvent(ENetEvent& event)
+  {
+    // assert that the peer exists and get peerID
+    assert( this->peerIDMap_.find(event.peer) != this->peerIDMap_.end() );
+    uint32_t peerID = this->peerIDMap_[event.peer];
+    
+    // create new Packet from ENetPacket
+    packet::Packet* p = packet::Packet::createPacket(event.packet, peerID);
+    
+    // create new peerEvent and return it
+    incomingEvent inEvent = { peerID, incomingEventType::receivePacket, p };
+    return inEvent;
+  }
+
   
   void Connection::enableCompression()
