Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 23, 2009, 6:02:25 PM (15 years ago)
Author:
scheusso
Message:

merged netp5 back to trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/network/Server.cc

    r3198 r3214  
    4242
    4343#include <enet/enet.h>
    44 #include <iostream>
    4544#include <cassert>
    46 
    47 
    48 #include "ConnectionManager.h"
    49 #include "ClientConnectionListener.h"
     45#include <string>
     46
     47#include "util/Debug.h"
     48#include "core/Clock.h"
     49#include "core/ObjectList.h"
     50#include "packet/Chat.h"
     51#include "packet/ClassID.h"
     52#include "packet/DeleteObjects.h"
     53#include "packet/FunctionIDs.h"
     54#include "packet/Gamestate.h"
     55#include "packet/Welcome.h"
     56#include "ChatListener.h"
     57#include "ClientInformation.h"
     58#include "FunctionCallManager.h"
    5059#include "GamestateManager.h"
    51 #include "ClientInformation.h"
    52 #include "util/Sleep.h"
    53 #include "core/Clock.h"
    54 #include "core/ConsoleCommand.h"
    55 #include "core/CoreIncludes.h"
    56 #include "packet/Chat.h"
    57 #include "packet/Packet.h"
    58 #include "packet/Welcome.h"
    59 #include "packet/DeleteObjects.h"
    60 #include "util/Convert.h"
    61 #include "ChatListener.h"
    62 #include "FunctionCallManager.h"
    63 #include "packet/FunctionIDs.h"
    64 
    6560
    6661namespace orxonox
     
    7469  Server::Server() {
    7570    timeSinceLastUpdate_=0;
    76     connection = new ConnectionManager();
    7771    gamestates_ = new GamestateManager();
    7872  }
    7973
    8074  Server::Server(int port){
     75    this->setPort( port );
    8176    timeSinceLastUpdate_=0;
    82     connection = new ConnectionManager(port);
    8377    gamestates_ = new GamestateManager();
    8478  }
     
    9084  */
    9185  Server::Server(int port, const std::string& bindAddress) {
     86    this->setPort( port );
     87    this->setBindAddress( bindAddress );
    9288    timeSinceLastUpdate_=0;
    93     connection = new ConnectionManager(port, bindAddress);
    9489    gamestates_ = new GamestateManager();
    9590  }
    9691
    9792  /**
    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   /**
    10993  * @brief Destructor
    11094  */
    11195  Server::~Server(){
    112     if(connection)
    113       delete connection;
    11496    if(gamestates_)
    11597      delete gamestates_;
     
    120102  */
    121103  void Server::open() {
    122     connection->createListener();
     104    COUT(4) << "opening server" << endl;
     105    this->openListener();
    123106    return;
    124107  }
     
    128111  */
    129112  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();
     113    COUT(4) << "closing server" << endl;
     114    this->disconnectClients();
     115    this->closeListener();
    140116    return;
    141117  }
     
    162138  */
    163139  void Server::update(const Clock& time) {
    164     processQueue();
     140    Connection::processQueue();
    165141    gamestates_->processGamestates();
    166142    //this steers our network frequency
    167143    timeSinceLastUpdate_+=time.getDeltaTime();
    168     if(timeSinceLastUpdate_>=NETWORK_PERIOD){
     144    if(timeSinceLastUpdate_>=NETWORK_PERIOD)
     145    {
    169146      timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD;
    170147      updateGamestate();
    171148      FunctionCallManager::sendCalls();
    172149    }
     150    sendPackets(); // flush the enet queue
    173151  }
    174152
    175153  bool Server::queuePacket(ENetPacket *packet, int clientID){
    176     return connection->addPacket(packet, clientID);
     154    return ServerConnection::addPacket(packet, clientID);
    177155  }
    178156 
     
    191169    assert(ClientInformation::findClient(clientID));
    192170    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     }
    226171  }
    227172
     
    318263
    319264
    320   bool Server::addClient(ENetEvent *event){
     265  void Server::addClient(ENetEvent *event){
    321266    static unsigned int newid=1;
    322267
     
    325270    if(!temp){
    326271      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;*/
     272    }
    334273    temp->setID(newid);
    335274    temp->setPeer(event->peer);
     
    342281    }
    343282
    344     newid++;
     283    ++newid;
    345284
    346285    COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
    347     return createClient(temp->getID());
     286    createClient(temp->getID());
    348287}
    349288
     
    357296   
    358297    // synchronise class ids
    359     connection->syncClassid(temp->getID());
     298    syncClassid(temp->getID());
    360299   
    361300    // now synchronise functionIDs
     
    382321    return true;
    383322  }
    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   }
    403323 
    404   void Server::disconnectClient( ClientInformation *client){
    405     connection->disconnectClient(client);
     324  void Server::disconnectClient( ClientInformation *client ){
     325    ServerConnection::disconnectClient( client );
    406326    gamestates_->removeClient(client);
    407327// inform all the listeners
     
    439359  }
    440360
     361  void Server::syncClassid(unsigned int clientID) {
     362    int failures=0;
     363    packet::ClassID *classid = new packet::ClassID();
     364    classid->setClientID(clientID);
     365    while(!classid->send() && failures < 10){
     366      failures++;
     367    }
     368    assert(failures<10);
     369    COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl;
     370  }
     371
    441372}
Note: See TracChangeset for help on using the changeset viewer.