[Orxonox-commit 669] r3201 - branches/netp5/src/network

scheusso at orxonox.net scheusso at orxonox.net
Sat Jun 20 19:21:46 CEST 2009


Author: scheusso
Date: 2009-06-20 19:21:46 +0200 (Sat, 20 Jun 2009)
New Revision: 3201

Added:
   branches/netp5/src/network/Connection.cc
   branches/netp5/src/network/Connection.h
   branches/netp5/src/network/ServerConnection.cc
   branches/netp5/src/network/ServerConnection.h
Removed:
   branches/netp5/src/network/ConnectionManager.cc
   branches/netp5/src/network/ConnectionManager.h
Modified:
   branches/netp5/src/network/CMakeLists.txt
   branches/netp5/src/network/NetworkPrereqs.h
   branches/netp5/src/network/Server.cc
   branches/netp5/src/network/Server.h
Log:
cleaned up server connection handling
client is yet to come


Modified: branches/netp5/src/network/CMakeLists.txt
===================================================================
--- branches/netp5/src/network/CMakeLists.txt	2009-06-20 14:29:04 UTC (rev 3200)
+++ branches/netp5/src/network/CMakeLists.txt	2009-06-20 17:21:46 UTC (rev 3201)
@@ -23,7 +23,7 @@
   ClientConnection.cc
   ClientInformation.cc
   ClientConnectionListener.cc
-  ConnectionManager.cc
+  Connection.cc
   FunctionCallManager.cc
   GamestateManager.cc
   GamestateClient.cc
@@ -32,6 +32,7 @@
   Host.cc
   PacketBuffer.cc
   Server.cc
+  ServerConnection.cc
   TrafficControl.cc
 )
 ADD_SUBDIRECTORY(packet)

Added: branches/netp5/src/network/Connection.cc
===================================================================
--- branches/netp5/src/network/Connection.cc	                        (rev 0)
+++ branches/netp5/src/network/Connection.cc	2009-06-20 17:21:46 UTC (rev 3201)
@@ -0,0 +1,94 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Oliver Scheuss
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "Connection.h"
+
+#include <iostream>
+#include <cassert>
+
+#include "util/Debug.h"
+#include "util/Math.h"
+#include "util/Sleep.h"
+#include "ClientInformation.h"
+#include "synchronisable/Synchronisable.h"
+#include "packet/ClassID.h"
+
+namespace orxonox
+{
+  Connection *Connection::instance_=0;
+
+  Connection::Connection():
+    host_(0)
+  {
+    assert(instance_==0);
+    Connection::instance_=this;
+  }
+
+  Connection::~Connection(){
+    Connection::instance_=0;
+  }
+
+  bool Connection::addPacket(ENetPacket *packet, ENetPeer *peer) {
+    if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0)
+      return false;
+    else
+      return true;
+  }
+
+  bool Connection::sendPackets() {
+    if ( !Connection::instance_ || this->host_==NULL )
+      return false;
+    enet_host_flush(this->host_);
+    return true;
+  }
+
+  void Connection::processQueue() {
+    ENetEvent event;
+    
+    assert(this->host_);
+
+    if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
+    {
+      switch(event.type){
+        // log handling ================
+        case ENET_EVENT_TYPE_CONNECT:
+          addClient( &event );
+          break;
+        case ENET_EVENT_TYPE_DISCONNECT:
+          disconnectClient( &event );
+          break;
+        case ENET_EVENT_TYPE_RECEIVE:
+          processPacket( &event );
+          break;
+        case ENET_EVENT_TYPE_NONE:
+          break;
+      }
+    }
+  }
+
+}


Property changes on: branches/netp5/src/network/Connection.cc
___________________________________________________________________
Added: svn:mergeinfo
   + 

Added: branches/netp5/src/network/Connection.h
===================================================================
--- branches/netp5/src/network/Connection.h	                        (rev 0)
+++ branches/netp5/src/network/Connection.h	2009-06-20 17:21:46 UTC (rev 3201)
@@ -0,0 +1,94 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Oliver Scheuss
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+//
+// C++ Interface: Connection
+//
+// Description:
+//
+//
+// Author:  Oliver Scheuss, (C) 2007
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+#ifndef _Connection_H__
+#define _Connection_H__
+
+#include "NetworkPrereqs.h"
+
+#include <string>
+#include <map>
+#include <enet/enet.h>
+
+#include "packet/Packet.h"
+
+namespace orxonox
+{
+    const int NETWORK_PORT = 55556;
+    const int NETWORK_MAX_CONNECTIONS = 50;
+    const int NETWORK_WAIT_TIMEOUT = 1;
+    const int NETWORK_DEFAULT_CHANNEL = 0;
+
+//   struct _NetworkExport ClientList{
+//     ENetEvent *event;
+//     int ID;
+//     ClientList *next;
+//   };
+
+  class _NetworkExport Connection{
+  public:
+    ~Connection();
+    
+    static bool addPacket(ENetPacket *packet, ENetPeer *peer);
+    bool sendPackets();
+    ENetHost* getHost(){ return this->host_; }
+
+  protected:
+    Connection();
+    static Connection* getInstance(){ return Connection::instance_; }
+    
+    int service(ENetEvent* event){ return enet_host_service( this->host_, event, NETWORK_WAIT_TIMEOUT ); }
+    void disconnectPeer(ENetPeer *peer){ enet_peer_disconnect(peer, 0); }
+    
+    void processQueue();
+    virtual void addClient(ENetEvent* event)=0;
+    virtual void disconnectClient(ENetEvent* event)=0;
+    virtual bool processPacket(ENetEvent* event){ packet::Packet *p = packet::Packet::createPacket(event->packet, event->peer); return p->process(); }
+    
+    ENetHost *host_;
+  private:
+    ENetAddress *bindAddress_;
+
+    static Connection *instance_;
+
+  };
+
+}
+
+#endif /* _Connection_H__ */


Property changes on: branches/netp5/src/network/Connection.h
___________________________________________________________________
Added: svn:mergeinfo
   + 

Deleted: branches/netp5/src/network/ConnectionManager.cc
===================================================================
--- branches/netp5/src/network/ConnectionManager.cc	2009-06-20 14:29:04 UTC (rev 3200)
+++ branches/netp5/src/network/ConnectionManager.cc	2009-06-20 17:21:46 UTC (rev 3201)
@@ -1,315 +0,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   License notice:
- *
- *   This program is free software; you can redistribute it and/or
- *   modify it under the terms of the GNU General Public License
- *   as published by the Free Software Foundation; either version 2
- *   of the License, or (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- *   Author:
- *      Oliver Scheuss, (C) 2007
- *   Co-authors:
- *      ...
- *
- */
-
-//
-// C++ Interface: ConnectionManager
-//
-// Description: The Class ConnectionManager manages the servers conenctions to the clients.
-// each connection is provided by a new process. communication between master process and
-// connection processes is provided by ...
-//
-//
-// Author:  Oliver Scheuss
-//
-
-#include "ConnectionManager.h"
-
-#include <enet/enet.h>
-#include <iostream>
-#include <cassert>
-// boost.thread library for multithreading support
-#include <boost/thread/thread.hpp>
-#include <boost/bind.hpp>
-#include <boost/thread/recursive_mutex.hpp>
-
-#include "util/Debug.h"
-#include "util/Math.h"
-#include "util/Sleep.h"
-#include "ClientInformation.h"
-#include "synchronisable/Synchronisable.h"
-#include "packet/ClassID.h"
-
-namespace std
-{
-  bool operator< (ENetAddress a, ENetAddress b) {
-    return a.host <= b.host;
-  }
-}
-
-namespace orxonox
-{
-  //boost::thread_group network_threads;
-  static boost::recursive_mutex enet_mutex_g;
-
-  ConnectionManager *ConnectionManager::instance_=0;
-
-  ConnectionManager::ConnectionManager():receiverThread_(0){
-    assert(instance_==0);
-    instance_=this;
-    quit_=false;
-    bindAddress = new ENetAddress();
-    bindAddress->host = ENET_HOST_ANY;
-    bindAddress->port = NETWORK_PORT;
-  }
-
-  ConnectionManager::ConnectionManager(int port){
-    assert(instance_==0);
-    instance_=this;
-    quit_=false;
-    bindAddress = new ENetAddress();
-    bindAddress->host = ENET_HOST_ANY;
-    bindAddress->port = port;
-  }
-
-  ConnectionManager::ConnectionManager(int port, const std::string& address) :receiverThread_(0) {
-    assert(instance_==0);
-    instance_=this;
-    quit_=false;
-    bindAddress = new ENetAddress();
-    enet_address_set_host (bindAddress, address.c_str());
-    bindAddress->port = NETWORK_PORT;
-  }
-
-  ConnectionManager::ConnectionManager(int port, const char *address) : receiverThread_(0) {
-    assert(instance_==0);
-    instance_=this;
-    quit_=false;
-    bindAddress = new ENetAddress();
-    enet_address_set_host (bindAddress, address);
-    bindAddress->port = NETWORK_PORT;
-  }
-
-  ConnectionManager::~ConnectionManager(){
-    if(!quit_)
-      quitListener();
-    instance_=0;
-    delete bindAddress;
-  }
-
-
-  ENetEvent *ConnectionManager::getEvent(){
-    if(!buffer.isEmpty())
-      return buffer.pop();
-    else
-      return NULL;
-  }
-
-  bool ConnectionManager::queueEmpty() {
-    return buffer.isEmpty();
-  }
-
-  void ConnectionManager::createListener() {
-    receiverThread_ = new boost::thread(boost::bind(&ConnectionManager::receiverThread, this));
-    return;
-  }
-
-  bool ConnectionManager::quitListener() {
-    quit_=true;
-    receiverThread_->join();
-    delete receiverThread_;
-    return true;
-  }
-
-
-  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) {
-    boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
-    if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0)
-      return false;
-    return true;
-  }
-
-  bool ConnectionManager::addPacket(ENetPacket *packet, unsigned int clientID) {
-    if ( clientID == CLIENTID_UNKNOWN )
-    {
-      return addPacketAll(packet);
-    }
-    else
-    {
-      ClientInformation *temp = ClientInformation::findClient(clientID);
-      if(!temp){
-        COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
-        return false;
-      }
-      return addPacket(packet, temp->getPeer());
-    }
-  }
-
-  bool ConnectionManager::addPacketAll(ENetPacket *packet) {
-    if(!instance_)
-      return false;
-    boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
-//    for(ClientInformation *i=ClientInformation::getBegin()->next(); i!=0; i=i->next()){
-//      COUT(3) << "adding broadcast packet for client: " << i->getID() << std::endl;
-//      if(enet_peer_send(i->getPeer(), 0, packet)!=0)
-//        return false;
-//    }
-    enet_host_broadcast( instance_->server, 0, packet);
-    return true;
-  }
-
-  // we actually dont need that function, because host_service does that for us
-  bool ConnectionManager::sendPackets() {
-    if(server==NULL || !instance_)
-      return false;
-    boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
-    enet_host_flush(server);
-    lock.unlock();
-    return true;
-  }
-
-  void ConnectionManager::receiverThread() {
-    // what about some error-handling here ?
-    ENetEvent *event;
-    atexit(enet_deinitialize);
-    { //scope of the mutex
-      boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
-      enet_initialize();
-      server = enet_host_create(bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
-      lock.unlock();
-    }
-    if(server==NULL){
-      // add some error handling here ==========================
-      quit_=true;
-      return;
-    }
-
-    event = new ENetEvent;
-    while(!quit_)
-    {
-      { //mutex scope
-        boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
-        if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
-          // we should never reach this point
-          printf("ConnectionManager: ENet returned with an error\n");
-//           quit_=true;
-//           printf("waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhh\n");
-          continue;
-          // add some error handling here ========================
-        }
-        lock.unlock();
-      }
-      switch(event->type){
-        // log handling ================
-        case ENET_EVENT_TYPE_CONNECT:
-//             printf("====================================================================");
-        case ENET_EVENT_TYPE_DISCONNECT:
-//           printf("====================================================================");
-        case ENET_EVENT_TYPE_RECEIVE:
-          processData(event);
-          event = new ENetEvent;
-          break;
-        case ENET_EVENT_TYPE_NONE:
-          //receiverThread_->yield();
-          msleep(1);
-          break;
-      }
-//       usleep(100);
-      //receiverThread_->yield(); //TODO: find apropriate
-    }
-    disconnectClients();
-    // if we're finishied, destroy server
-    {
-      boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
-      enet_host_destroy(server);
-      lock.unlock();
-    }
-  }
-
-  //### added some bugfixes here, but we cannot test them because
-  //### the server crashes everytime because of some gamestates
-  //### (trying to resolve that now)
-  void ConnectionManager::disconnectClients() {
-    ENetEvent event;
-    ClientInformation *temp = ClientInformation::getBegin()->next();
-    while(temp!=0){
-      {
-        boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
-        enet_peer_disconnect(temp->getPeer(), 0);
-        lock.unlock();
-      }
-      temp = temp->next();
-    }
-    //bugfix: might be the reason why server crashes when clients disconnects
-    temp = ClientInformation::getBegin()->next();
-    boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
-    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){
-      switch (event.type)
-      {
-      case ENET_EVENT_TYPE_NONE: break;
-      case ENET_EVENT_TYPE_CONNECT: break;
-      case ENET_EVENT_TYPE_RECEIVE:
-        enet_packet_destroy(event.packet);
-        break;
-      case ENET_EVENT_TYPE_DISCONNECT:
-        COUT(4) << "disconnecting all clients" << std::endl;
-        if(ClientInformation::findClient(&(event.peer->address)))
-          delete ClientInformation::findClient(&(event.peer->address));
-        //maybe needs bugfix: might also be a reason for the server to crash
-        temp = temp->next();
-        break;
-      }
-    }
-    return;
-  }
-
-
-  int ConnectionManager::getClientID(ENetPeer* peer) {
-    return getClientID(&(peer->address));
-  }
-
-  int ConnectionManager::getClientID(ENetAddress* address) {
-    return ClientInformation::findClient(address)->getID();
-  }
-
-  ENetPeer *ConnectionManager::getClientPeer(int clientID) {
-    return ClientInformation::findClient(clientID)->getPeer();
-  }
-
-
-  void ConnectionManager::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;
-  }
-  
-
-  void ConnectionManager::disconnectClient(ClientInformation *client){
-    {
-      boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
-      enet_peer_disconnect(client->getPeer(), 0);
-      lock.unlock();
-    }
-  }
-
-
-}

Deleted: branches/netp5/src/network/ConnectionManager.h
===================================================================
--- branches/netp5/src/network/ConnectionManager.h	2009-06-20 14:29:04 UTC (rev 3200)
+++ branches/netp5/src/network/ConnectionManager.h	2009-06-20 17:21:46 UTC (rev 3201)
@@ -1,107 +0,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   License notice:
- *
- *   This program is free software; you can redistribute it and/or
- *   modify it under the terms of the GNU General Public License
- *   as published by the Free Software Foundation; either version 2
- *   of the License, or (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- *   Author:
- *      Oliver Scheuss, (C) 2007
- *   Co-authors:
- *      ...
- *
- */
-
-//
-// C++ Interface: ConnectionManager
-//
-// Description:
-//
-//
-// Author:  Oliver Scheuss, (C) 2007
-//
-// Copyright: See COPYING file that comes with this distribution
-//
-//
-#ifndef _ConnectionManager_H__
-#define _ConnectionManager_H__
-
-#include "NetworkPrereqs.h"
-
-#include <string>
-#include <map>
-
-#include "PacketBuffer.h"
-#include "packet/Packet.h"
-
-namespace boost { class thread; }
-
-namespace orxonox
-{
-    const int NETWORK_PORT = 55556;
-    const int NETWORK_MAX_CONNECTIONS = 50;
-    const int NETWORK_WAIT_TIMEOUT = 1;
-    const int NETWORK_DEFAULT_CHANNEL = 0;
-
-  struct _NetworkExport ClientList{
-    ENetEvent *event;
-    int ID;
-    ClientList *next;
-  };
-
-  class _NetworkExport ConnectionManager{
-    public:
-    ConnectionManager();
-    ConnectionManager(int port);
-    ConnectionManager(int port, const char *address);
-    ConnectionManager(int port, const std::string& address);
-    ~ConnectionManager();
-    ENetEvent *getEvent();
-    bool queueEmpty();
-    void createListener();
-    bool quitListener();
-    static bool addPacket(ENetPacket *packet, ENetPeer *peer);
-    static bool addPacket(ENetPacket *packet, unsigned int ID);
-    static bool addPacketAll(ENetPacket *packet);
-    bool sendPackets();
-    void disconnectClient(ClientInformation *client);
-    void syncClassid(unsigned int clientID);
-    bool checkReceiverThread(){ return !quit_; }
-
-  private:
-    ConnectionManager(const ConnectionManager& copy); // not used
-    inline bool processData(ENetEvent *event){ return buffer.push(event); }
-    void receiverThread();
-    void disconnectClients();
-    int getClientID(ENetPeer* peer);
-    int getClientID(ENetAddress* address);
-    ENetPeer *getClientPeer(int clientID);
-    PacketBuffer buffer;
-
-    ENetHost *server;
-    ENetAddress *bindAddress;
-
-    bool quit_; // quit-variable (communication with threads)
-
-    boost::thread *receiverThread_;
-    static ConnectionManager *instance_;
-
-  };
-
-}
-
-#endif /* _ConnectionManager_H__ */

Modified: branches/netp5/src/network/NetworkPrereqs.h
===================================================================
--- branches/netp5/src/network/NetworkPrereqs.h	2009-06-20 14:29:04 UTC (rev 3200)
+++ branches/netp5/src/network/NetworkPrereqs.h	2009-06-20 17:21:46 UTC (rev 3201)
@@ -88,7 +88,7 @@
   class ClientConnectionListener;
   class ClientFrameListener;
   class ClientInformation;
-  class ConnectionManager;
+  class Connection;
   class FunctionCallManager;
   class GamestateClient;
   class GamestateManager;
@@ -103,6 +103,7 @@
   struct NetworkFunctionPointer;
   class PacketBuffer;
   class Server;
+  class ServerConnection;
   class ServerFrameListener;
   class Synchronisable;
   class SynchronisableVariableBase;

Modified: branches/netp5/src/network/Server.cc
===================================================================
--- branches/netp5/src/network/Server.cc	2009-06-20 14:29:04 UTC (rev 3200)
+++ branches/netp5/src/network/Server.cc	2009-06-20 17:21:46 UTC (rev 3201)
@@ -45,7 +45,7 @@
 #include <cassert>
 
 
-#include "ConnectionManager.h"
+// #include "ConnectionManager.h"
 #include "ClientConnectionListener.h"
 #include "GamestateManager.h"
 #include "ClientInformation.h"
@@ -57,6 +57,7 @@
 #include "packet/Packet.h"
 #include "packet/Welcome.h"
 #include "packet/DeleteObjects.h"
+#include "packet/ClassID.h"
 #include "util/Convert.h"
 #include "ChatListener.h"
 #include "FunctionCallManager.h"
@@ -73,13 +74,12 @@
   */
   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();
   }
 
@@ -89,28 +89,16 @@
   * @param bindAddress Address to listen on
   */
   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_;
   }
@@ -119,7 +107,8 @@
   * This function opens the server by creating the listener thread
   */
   void Server::open() {
-    connection->createListener();
+    COUT(4) << "opening server" << endl;
+    this->openListener();
     return;
   }
 
@@ -127,16 +116,9 @@
   * This function closes the server
   */
   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;
   }
 
@@ -161,7 +143,7 @@
   * @param time time since last tick
   */
   void Server::update(const Clock& time) {
-    processQueue();
+    ServerConnection::processQueue();
     gamestates_->processGamestates();
     //this steers our network frequency
     timeSinceLastUpdate_+=time.getDeltaTime();
@@ -173,7 +155,7 @@
   }
 
   bool Server::queuePacket(ENetPacket *packet, int clientID){
-    return connection->addPacket(packet, clientID);
+    return ServerConnection::addPacket(packet, clientID);
   }
   
   /**
@@ -191,39 +173,6 @@
     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
-    }
-  }
 
   /**
   * takes a new snapshot of the gamestate and sends it to the clients
@@ -317,20 +266,14 @@
   }
 
 
-  bool Server::addClient(ENetEvent *event){
+  void Server::addClient(ENetEvent *event){
     static unsigned int newid=1;
 
     COUT(2) << "Server: adding client" << std::endl;
     ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
     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);
 
@@ -341,10 +284,10 @@
       listener++;
     }
 
-    newid++;
+    ++newid;
 
     COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
-    return createClient(temp->getID());
+    createClient(temp->getID());
 }
 
   bool Server::createClient(int clientID){
@@ -356,7 +299,7 @@
     COUT(5) << "Con.Man: creating client id: " << temp->getID() << std::endl;
     
     // synchronise class ids
-    connection->syncClassid(temp->getID());
+    syncClassid(temp->getID());
     
     // now synchronise functionIDs
     packet::FunctionIDs *fIDs = new packet::FunctionIDs();
@@ -381,28 +324,9 @@
     assert(b);
     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
     ObjectList<ClientConnectionListener>::iterator listener = ObjectList<ClientConnectionListener>::begin();
@@ -438,4 +362,15 @@
     return true;
   }
 
+  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;
+  }
+
 }

Modified: branches/netp5/src/network/Server.h
===================================================================
--- branches/netp5/src/network/Server.h	2009-06-20 14:29:04 UTC (rev 3200)
+++ branches/netp5/src/network/Server.h	2009-06-20 17:21:46 UTC (rev 3201)
@@ -34,6 +34,7 @@
 #include <string>
 
 #include "Host.h"
+#include "ServerConnection.h"
 #include "GamestateManager.h"
 
 namespace orxonox
@@ -43,12 +44,11 @@
   * This class is the root class of the network module for a server.
   * It implements all functions necessary for a Server
   */
-  class _NetworkExport Server : public Host{
+  class _NetworkExport Server : public Host, public ServerConnection{
   public:
     Server();
     Server(int port);
     Server(int port, const std::string& bindAddress);
-    Server(int port, const char *bindAddress);
     ~Server();
 
     void open();
@@ -59,17 +59,14 @@
     unsigned int getPing(unsigned int clientID);
     double getPacketLoss(unsigned int clientID);
   protected:
-    void processQueue();
     void updateGamestate();
   private:
     virtual bool isServer_(){return true;}
     unsigned int shipID(){return 0;}
     unsigned int playerID(){return 0;}
 
-    bool addClient(ENetEvent *event);
+    void addClient(ENetEvent *event);
     bool createClient(int clientID);
-    bool disconnectClient(ENetEvent *event);
-    void disconnectClient(int clientID);
     void disconnectClient( ClientInformation *client);
     bool processPacket( ENetPacket *packet, ENetPeer *peer );
     bool sendGameState();
@@ -77,9 +74,8 @@
     virtual bool chat(const std::string& message);
     virtual bool broadcast(const std::string& message);
     bool sendChat(const std::string& message, unsigned int clientID);
+    void syncClassid(unsigned int clientID);
 
-    //void processChat( chat *data, int clientId);
-    ConnectionManager *connection;
     GamestateManager *gamestates_;
 
 

Added: branches/netp5/src/network/ServerConnection.cc
===================================================================
--- branches/netp5/src/network/ServerConnection.cc	                        (rev 0)
+++ branches/netp5/src/network/ServerConnection.cc	2009-06-20 17:21:46 UTC (rev 3201)
@@ -0,0 +1,156 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Oliver Scheuss
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "ServerConnection.h"
+
+#include <cassert>
+
+#include "ClientInformation.h"
+
+#include "core/CoreIncludes.h"
+
+namespace orxonox
+{
+
+  ServerConnection::ServerConnection():
+    bListening_(false)
+  {
+    this->bindAddress_ = new ENetAddress();
+    this->bindAddress_->host = ENET_HOST_ANY;
+    this->bindAddress_->port = NETWORK_PORT;
+  }
+
+  ServerConnection::~ServerConnection(){
+    if ( this->bListening_ )
+      closeListener();
+    delete this->bindAddress_;
+  }
+
+  bool ServerConnection::openListener() {
+    enet_initialize();
+    atexit(enet_deinitialize);
+    this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, 0, 0);
+    if ( this->host_ == NULL )
+      return false;
+  }
+
+  bool ServerConnection::closeListener() {
+    this->bListening_=false;
+    disconnectClients();
+    enet_host_destroy(this->host_);
+  }
+
+  bool ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID) {
+    if ( clientID == CLIENTID_UNKNOWN )
+    {
+      return addPacketAll(packet);
+    }
+    else
+    {
+      ClientInformation *temp = ClientInformation::findClient(clientID);
+      if(!temp){
+        COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
+        return false;
+      }
+      return Connection::addPacket(packet, temp->getPeer());
+    }
+  }
+
+  bool ServerConnection::addPacketAll(ENetPacket *packet) {
+    if ( !Connection::getInstance() )
+      return false;
+    enet_host_broadcast( Connection::getInstance()->getHost(), 0, packet);
+    return true;
+  }
+  
+  void ServerConnection::disconnectClient(ClientInformation *client)
+  {
+    disconnectPeer( client->getPeer() );
+    delete client;
+  }
+  
+  void ServerConnection::disconnectClient( ENetEvent* event )
+  {
+    COUT(4) << "removing client from list" << std::endl;
+    ClientInformation *client = ClientInformation::findClient(&event->peer->address);
+    if(!client)
+      return;
+    else
+      ServerConnection::disconnectClient( client );
+  }
+  
+  void ServerConnection::disconnectClient(int clientID){
+    ClientInformation *client = ClientInformation::findClient(clientID);
+    if(client)
+      disconnectClient(client);
+  }
+
+  void ServerConnection::disconnectClients() {
+    ENetEvent event;
+    ClientInformation *temp = ClientInformation::getBegin();
+    while(temp!=0){
+      disconnectPeer( temp->getPeer() );
+      temp = temp->next();
+    }
+    temp = ClientInformation::getBegin();
+    while( temp!=0 ){
+      if( service( &event ) )
+      {
+        switch (event.type)
+        {
+        case ENET_EVENT_TYPE_NONE: break;
+        case ENET_EVENT_TYPE_CONNECT: break;
+        case ENET_EVENT_TYPE_RECEIVE:
+          enet_packet_destroy(event.packet);
+          break;
+        case ENET_EVENT_TYPE_DISCONNECT:
+          if(ClientInformation::findClient(&(event.peer->address)))
+            delete ClientInformation::findClient(&(event.peer->address));
+          temp = ClientInformation::getBegin();
+          break;
+        }
+      }
+    }
+    return;
+  }
+
+
+  int ServerConnection::getClientID(ENetPeer* peer) {
+    return getClientID(&(peer->address));
+  }
+
+  int ServerConnection::getClientID(ENetAddress* address) {
+    return ClientInformation::findClient(address)->getID();
+  }
+
+  ENetPeer *ServerConnection::getClientPeer(int clientID) {
+    return ClientInformation::findClient(clientID)->getPeer();
+  }
+
+
+}

Added: branches/netp5/src/network/ServerConnection.h
===================================================================
--- branches/netp5/src/network/ServerConnection.h	                        (rev 0)
+++ branches/netp5/src/network/ServerConnection.h	2009-06-20 17:21:46 UTC (rev 3201)
@@ -0,0 +1,81 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Oliver Scheuss
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+//
+// C++ Interface: ServerConnection
+//
+// Description:
+//
+//
+// Author:  Oliver Scheuss, (C) 2007
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+#ifndef _ServerConnection_H__
+#define _ServerConnection_H__
+
+#include "NetworkPrereqs.h"
+
+#include "Connection.h"
+
+namespace orxonox
+{
+
+  class _NetworkExport ServerConnection : public Connection{
+  public:
+    ~ServerConnection();
+    
+    void setBindAddress( const std::string& bindAddress ){ enet_address_set_host (this->bindAddress_, bindAddress.c_str()); }
+    void setPort( unsigned int port ){ this->bindAddress_->port = port; }
+    
+    bool openListener();
+    bool closeListener();
+    static bool addPacket(ENetPacket *packet, unsigned int ID);
+    static bool addPacketAll(ENetPacket *packet);
+    virtual void disconnectClient(ClientInformation *client);
+    void disconnectClient( ENetEvent* event );
+    void disconnectClient(int clientID);
+  protected:
+    ServerConnection();
+    void disconnectClients();
+
+  private:
+    int getClientID(ENetPeer* peer);
+    int getClientID(ENetAddress* address);
+    ENetPeer* getClientPeer(int clientID);
+
+    ENetAddress* bindAddress_;
+
+    bool bListening_;
+
+  };
+
+}
+
+#endif /* _ServerConnection_H__ */




More information about the Orxonox-commit mailing list