Index: code/trunk/src/network/Server.cc
===================================================================
--- code/trunk/src/network/Server.cc	(revision 3198)
+++ code/trunk/src/network/Server.cc	(revision 3214)
@@ -42,25 +42,20 @@
 
 #include <enet/enet.h>
-#include <iostream>
 #include <cassert>
-
-
-#include "ConnectionManager.h"
-#include "ClientConnectionListener.h"
+#include <string>
+
+#include "util/Debug.h"
+#include "core/Clock.h"
+#include "core/ObjectList.h"
+#include "packet/Chat.h"
+#include "packet/ClassID.h"
+#include "packet/DeleteObjects.h"
+#include "packet/FunctionIDs.h"
+#include "packet/Gamestate.h"
+#include "packet/Welcome.h"
+#include "ChatListener.h"
+#include "ClientInformation.h"
+#include "FunctionCallManager.h"
 #include "GamestateManager.h"
-#include "ClientInformation.h"
-#include "util/Sleep.h"
-#include "core/Clock.h"
-#include "core/ConsoleCommand.h"
-#include "core/CoreIncludes.h"
-#include "packet/Chat.h"
-#include "packet/Packet.h"
-#include "packet/Welcome.h"
-#include "packet/DeleteObjects.h"
-#include "util/Convert.h"
-#include "ChatListener.h"
-#include "FunctionCallManager.h"
-#include "packet/FunctionIDs.h"
-
 
 namespace orxonox
@@ -74,11 +69,10 @@
   Server::Server() {
     timeSinceLastUpdate_=0;
-    connection = new ConnectionManager();
     gamestates_ = new GamestateManager();
   }
 
   Server::Server(int port){
+    this->setPort( port );
     timeSinceLastUpdate_=0;
-    connection = new ConnectionManager(port);
     gamestates_ = new GamestateManager();
   }
@@ -90,26 +84,14 @@
   */
   Server::Server(int port, const std::string& bindAddress) {
+    this->setPort( port );
+    this->setBindAddress( bindAddress );
     timeSinceLastUpdate_=0;
-    connection = new ConnectionManager(port, bindAddress);
     gamestates_ = new GamestateManager();
   }
 
   /**
-  * Constructor
-  * @param port Port to listen on
-  * @param bindAddress Address to listen on
-  */
-  Server::Server(int port, const char *bindAddress) {
-    timeSinceLastUpdate_=0;
-    connection = new ConnectionManager(port, bindAddress);
-    gamestates_ = new GamestateManager();
-  }
-
-  /**
   * @brief Destructor
   */
   Server::~Server(){
-    if(connection)
-      delete connection;
     if(gamestates_)
       delete gamestates_;
@@ -120,5 +102,6 @@
   */
   void Server::open() {
-    connection->createListener();
+    COUT(4) << "opening server" << endl;
+    this->openListener();
     return;
   }
@@ -128,14 +111,7 @@
   */
   void Server::close() {
-    ClientInformation *temp = ClientInformation::getBegin();
-    ClientInformation *temp2;
-    // disconnect all connected clients
-    while( temp )
-    {
-      temp2 = temp;
-      temp = temp->next();
-      disconnectClient( temp2 );
-    }
-    connection->quitListener();
+    COUT(4) << "closing server" << endl;
+    this->disconnectClients();
+    this->closeListener();
     return;
   }
@@ -162,17 +138,19 @@
   */
   void Server::update(const Clock& time) {
-    processQueue();
+    Connection::processQueue();
     gamestates_->processGamestates();
     //this steers our network frequency
     timeSinceLastUpdate_+=time.getDeltaTime();
-    if(timeSinceLastUpdate_>=NETWORK_PERIOD){
+    if(timeSinceLastUpdate_>=NETWORK_PERIOD)
+    {
       timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD;
       updateGamestate();
       FunctionCallManager::sendCalls();
     }
+    sendPackets(); // flush the enet queue
   }
 
   bool Server::queuePacket(ENetPacket *packet, int clientID){
-    return connection->addPacket(packet, clientID);
+    return ServerConnection::addPacket(packet, clientID);
   }
   
@@ -191,37 +169,4 @@
     assert(ClientInformation::findClient(clientID));
     return ClientInformation::findClient(clientID)->getPacketLoss();
-  }
-  
-  /**
-  * processes all the packets waiting in the queue
-  */
-  void Server::processQueue() {
-    ENetEvent *event;
-    while(!connection->queueEmpty()){
-      //std::cout << "Client " << clientID << " sent: " << std::endl;
-      //clientID here is a reference to grab clientID from ClientInformation
-      event = connection->getEvent();
-      if(!event)
-        continue;
-      assert(event->type != ENET_EVENT_TYPE_NONE);
-      switch( event->type ) {
-      case ENET_EVENT_TYPE_CONNECT:
-        COUT(4) << "processing event_Type_connect" << std::endl;
-        addClient(event);
-        break;
-      case ENET_EVENT_TYPE_DISCONNECT:
-        if(ClientInformation::findClient(&event->peer->address))
-          disconnectClient(event);
-        break;
-      case ENET_EVENT_TYPE_RECEIVE:
-        if(!processPacket(event->packet, event->peer))
-          COUT(3) << "processing incoming packet failed" << std::endl;
-        break;
-      default:
-        break;
-      }
-      delete event;
-      //if statement to catch case that packetbuffer is empty
-    }
   }
 
@@ -318,5 +263,5 @@
 
 
-  bool Server::addClient(ENetEvent *event){
+  void Server::addClient(ENetEvent *event){
     static unsigned int newid=1;
 
@@ -325,11 +270,5 @@
     if(!temp){
       COUT(2) << "Server: could not add client" << std::endl;
-      return false;
-    }
-    /*if(temp==ClientInformation::getBegin()) { //not good if you use anything else than insertBack
-      newid=1;
-    }
-    else
-      newid=temp->prev()->getID()+1;*/
+    }
     temp->setID(newid);
     temp->setPeer(event->peer);
@@ -342,8 +281,8 @@
     }
 
-    newid++;
+    ++newid;
 
     COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
-    return createClient(temp->getID());
+    createClient(temp->getID());
 }
 
@@ -357,5 +296,5 @@
     
     // synchronise class ids
-    connection->syncClassid(temp->getID());
+    syncClassid(temp->getID());
     
     // now synchronise functionIDs
@@ -382,26 +321,7 @@
     return true;
   }
-
-  bool Server::disconnectClient(ENetEvent *event){
-    COUT(4) << "removing client from list" << std::endl;
-    //return removeClient(head_->findClient(&(peer->address))->getID());
-
-    //boost::recursive_mutex::scoped_lock lock(head_->mutex_);
-    ClientInformation *client = ClientInformation::findClient(&event->peer->address);
-    if(!client)
-      return false;
-    else
-      disconnectClient( client );
-    return true;
-  }
-
-  void Server::disconnectClient(int clientID){
-    ClientInformation *client = ClientInformation::findClient(clientID);
-    if(client)
-      disconnectClient(client);
-  }
   
-  void Server::disconnectClient( ClientInformation *client){
-    connection->disconnectClient(client);
+  void Server::disconnectClient( ClientInformation *client ){
+    ServerConnection::disconnectClient( client );
     gamestates_->removeClient(client);
 // inform all the listeners
@@ -439,3 +359,14 @@
   }
 
+  void Server::syncClassid(unsigned int clientID) {
+    int failures=0;
+    packet::ClassID *classid = new packet::ClassID();
+    classid->setClientID(clientID);
+    while(!classid->send() && failures < 10){
+      failures++;
+    }
+    assert(failures<10);
+    COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl;
+  }
+
 }
