Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3201


Ignore:
Timestamp:
Jun 20, 2009, 7:21:46 PM (15 years ago)
Author:
scheusso
Message:

cleaned up server connection handling
client is yet to come

Location:
code/branches/netp5/src/network
Files:
4 added
2 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/netp5/src/network/CMakeLists.txt

    r3196 r3201  
    2424  ClientInformation.cc
    2525  ClientConnectionListener.cc
    26   ConnectionManager.cc
     26  Connection.cc
    2727  FunctionCallManager.cc
    2828  GamestateManager.cc
     
    3333  PacketBuffer.cc
    3434  Server.cc
     35  ServerConnection.cc
    3536  TrafficControl.cc
    3637)
  • code/branches/netp5/src/network/NetworkPrereqs.h

    r3084 r3201  
    8989  class ClientFrameListener;
    9090  class ClientInformation;
    91   class ConnectionManager;
     91  class Connection;
    9292  class FunctionCallManager;
    9393  class GamestateClient;
     
    104104  class PacketBuffer;
    105105  class Server;
     106  class ServerConnection;
    106107  class ServerFrameListener;
    107108  class Synchronisable;
  • code/branches/netp5/src/network/Server.cc

    r3198 r3201  
    4646
    4747
    48 #include "ConnectionManager.h"
     48// #include "ConnectionManager.h"
    4949#include "ClientConnectionListener.h"
    5050#include "GamestateManager.h"
     
    5858#include "packet/Welcome.h"
    5959#include "packet/DeleteObjects.h"
     60#include "packet/ClassID.h"
    6061#include "util/Convert.h"
    6162#include "ChatListener.h"
     
    7475  Server::Server() {
    7576    timeSinceLastUpdate_=0;
    76     connection = new ConnectionManager();
    7777    gamestates_ = new GamestateManager();
    7878  }
    7979
    8080  Server::Server(int port){
     81    this->setPort( port );
    8182    timeSinceLastUpdate_=0;
    82     connection = new ConnectionManager(port);
    8383    gamestates_ = new GamestateManager();
    8484  }
     
    9090  */
    9191  Server::Server(int port, const std::string& bindAddress) {
     92    this->setPort( port );
     93    this->setBindAddress( bindAddress );
    9294    timeSinceLastUpdate_=0;
    93     connection = new ConnectionManager(port, bindAddress);
    9495    gamestates_ = new GamestateManager();
    9596  }
    9697
    9798  /**
    98   * Constructor
    99   * @param port Port to listen on
    100   * @param bindAddress Address to listen on
    101   */
    102   Server::Server(int port, const char *bindAddress) {
    103     timeSinceLastUpdate_=0;
    104     connection = new ConnectionManager(port, bindAddress);
    105     gamestates_ = new GamestateManager();
    106   }
    107 
    108   /**
    10999  * @brief Destructor
    110100  */
    111101  Server::~Server(){
    112     if(connection)
    113       delete connection;
    114102    if(gamestates_)
    115103      delete gamestates_;
     
    120108  */
    121109  void Server::open() {
    122     connection->createListener();
     110    COUT(4) << "opening server" << endl;
     111    this->openListener();
    123112    return;
    124113  }
     
    128117  */
    129118  void Server::close() {
    130     ClientInformation *temp = ClientInformation::getBegin();
    131     ClientInformation *temp2;
    132     // disconnect all connected clients
    133     while( temp )
    134     {
    135       temp2 = temp;
    136       temp = temp->next();
    137       disconnectClient( temp2 );
    138     }
    139     connection->quitListener();
     119    COUT(4) << "closing server" << endl;
     120    this->disconnectClients();
     121    this->closeListener();
    140122    return;
    141123  }
     
    162144  */
    163145  void Server::update(const Clock& time) {
    164     processQueue();
     146    ServerConnection::processQueue();
    165147    gamestates_->processGamestates();
    166148    //this steers our network frequency
     
    174156
    175157  bool Server::queuePacket(ENetPacket *packet, int clientID){
    176     return connection->addPacket(packet, clientID);
     158    return ServerConnection::addPacket(packet, clientID);
    177159  }
    178160 
     
    191173    assert(ClientInformation::findClient(clientID));
    192174    return ClientInformation::findClient(clientID)->getPacketLoss();
    193   }
    194  
    195   /**
    196   * processes all the packets waiting in the queue
    197   */
    198   void Server::processQueue() {
    199     ENetEvent *event;
    200     while(!connection->queueEmpty()){
    201       //std::cout << "Client " << clientID << " sent: " << std::endl;
    202       //clientID here is a reference to grab clientID from ClientInformation
    203       event = connection->getEvent();
    204       if(!event)
    205         continue;
    206       assert(event->type != ENET_EVENT_TYPE_NONE);
    207       switch( event->type ) {
    208       case ENET_EVENT_TYPE_CONNECT:
    209         COUT(4) << "processing event_Type_connect" << std::endl;
    210         addClient(event);
    211         break;
    212       case ENET_EVENT_TYPE_DISCONNECT:
    213         if(ClientInformation::findClient(&event->peer->address))
    214           disconnectClient(event);
    215         break;
    216       case ENET_EVENT_TYPE_RECEIVE:
    217         if(!processPacket(event->packet, event->peer))
    218           COUT(3) << "processing incoming packet failed" << std::endl;
    219         break;
    220       default:
    221         break;
    222       }
    223       delete event;
    224       //if statement to catch case that packetbuffer is empty
    225     }
    226175  }
    227176
     
    318267
    319268
    320   bool Server::addClient(ENetEvent *event){
     269  void Server::addClient(ENetEvent *event){
    321270    static unsigned int newid=1;
    322271
     
    325274    if(!temp){
    326275      COUT(2) << "Server: could not add client" << std::endl;
    327       return false;
    328     }
    329     /*if(temp==ClientInformation::getBegin()) { //not good if you use anything else than insertBack
    330       newid=1;
    331     }
    332     else
    333       newid=temp->prev()->getID()+1;*/
     276    }
    334277    temp->setID(newid);
    335278    temp->setPeer(event->peer);
     
    342285    }
    343286
    344     newid++;
     287    ++newid;
    345288
    346289    COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
    347     return createClient(temp->getID());
     290    createClient(temp->getID());
    348291}
    349292
     
    357300   
    358301    // synchronise class ids
    359     connection->syncClassid(temp->getID());
     302    syncClassid(temp->getID());
    360303   
    361304    // now synchronise functionIDs
     
    382325    return true;
    383326  }
    384 
    385   bool Server::disconnectClient(ENetEvent *event){
    386     COUT(4) << "removing client from list" << std::endl;
    387     //return removeClient(head_->findClient(&(peer->address))->getID());
    388 
    389     //boost::recursive_mutex::scoped_lock lock(head_->mutex_);
    390     ClientInformation *client = ClientInformation::findClient(&event->peer->address);
    391     if(!client)
    392       return false;
    393     else
    394       disconnectClient( client );
    395     return true;
    396   }
    397 
    398   void Server::disconnectClient(int clientID){
    399     ClientInformation *client = ClientInformation::findClient(clientID);
    400     if(client)
    401       disconnectClient(client);
    402   }
    403327 
    404   void Server::disconnectClient( ClientInformation *client){
    405     connection->disconnectClient(client);
     328  void Server::disconnectClient( ClientInformation *client ){
     329    ServerConnection::disconnectClient( client );
    406330    gamestates_->removeClient(client);
    407331// inform all the listeners
     
    439363  }
    440364
     365  void Server::syncClassid(unsigned int clientID) {
     366    int failures=0;
     367    packet::ClassID *classid = new packet::ClassID();
     368    classid->setClientID(clientID);
     369    while(!classid->send() && failures < 10){
     370      failures++;
     371    }
     372    assert(failures<10);
     373    COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl;
     374  }
     375
    441376}
  • code/branches/netp5/src/network/Server.h

    r3084 r3201  
    3535
    3636#include "Host.h"
     37#include "ServerConnection.h"
    3738#include "GamestateManager.h"
    3839
     
    4445  * It implements all functions necessary for a Server
    4546  */
    46   class _NetworkExport Server : public Host{
     47  class _NetworkExport Server : public Host, public ServerConnection{
    4748  public:
    4849    Server();
    4950    Server(int port);
    5051    Server(int port, const std::string& bindAddress);
    51     Server(int port, const char *bindAddress);
    5252    ~Server();
    5353
     
    6060    double getPacketLoss(unsigned int clientID);
    6161  protected:
    62     void processQueue();
    6362    void updateGamestate();
    6463  private:
     
    6766    unsigned int playerID(){return 0;}
    6867
    69     bool addClient(ENetEvent *event);
     68    void addClient(ENetEvent *event);
    7069    bool createClient(int clientID);
    71     bool disconnectClient(ENetEvent *event);
    72     void disconnectClient(int clientID);
    7370    void disconnectClient( ClientInformation *client);
    7471    bool processPacket( ENetPacket *packet, ENetPeer *peer );
     
    7875    virtual bool broadcast(const std::string& message);
    7976    bool sendChat(const std::string& message, unsigned int clientID);
     77    void syncClassid(unsigned int clientID);
    8078
    81     //void processChat( chat *data, int clientId);
    82     ConnectionManager *connection;
    8379    GamestateManager *gamestates_;
    8480
Note: See TracChangeset for help on using the changeset viewer.