Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 1, 2008, 7:04:09 PM (16 years ago)
Author:
landauf
Message:

merged objecthierarchy branch back to trunk

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/network/Server.cc

    r1907 r2087  
    4646
    4747#include "ConnectionManager.h"
     48#include "ClientConnectionListener.h"
    4849#include "GamestateManager.h"
    4950#include "ClientInformation.h"
    5051#include "util/Sleep.h"
    51 #include "objects/SpaceShip.h"
    5252#include "core/ConsoleCommand.h"
    5353#include "core/CoreIncludes.h"
     
    5858#include "packet/DeleteObjects.h"
    5959#include <util/Convert.h>
     60#include "ChatListener.h"
    6061
    6162namespace network
    6263{
    63   const int MAX_FAILURES = 20;
    64   const int NETWORK_FREQUENCY = 30;
     64  const unsigned int MAX_FAILURES = 20;
     65  const unsigned int NETWORK_FREQUENCY = 25;
     66  const float NETWORK_PERIOD = (float)1/NETWORK_FREQUENCY;
    6567
    6668  /**
     
    8587  * @param bindAddress Address to listen on
    8688  */
    87   Server::Server(int port, std::string bindAddress) {
     89  Server::Server(int port, const std::string& bindAddress) {
    8890    timeSinceLastUpdate_=0;
    8991    connection = new ConnectionManager(port, bindAddress);
     
    101103    gamestates_ = new GamestateManager();
    102104  }
    103  
     105
    104106  /**
    105107  * @brief Destructor
     
    128130  }
    129131
    130   bool Server::processChat(std::string message, unsigned int playerID){
     132  bool Server::processChat(const std::string& message, unsigned int playerID){
    131133    ClientInformation *temp = ClientInformation::getBegin();
    132134    packet::Chat *chat;
     
    138140      temp = temp->next();
    139141    }
    140     COUT(1) << "Player " << playerID << ": " << message << std::endl;
     142//    COUT(1) << "Player " << playerID << ": " << message << std::endl;
    141143    return true;
    142144  }
     
    152154    //this steers our network frequency
    153155    timeSinceLastUpdate_+=time;
    154     if(timeSinceLastUpdate_>=(1./NETWORK_FREQUENCY)){
    155       timeSinceLastUpdate_=(float)((int)(timeSinceLastUpdate_*NETWORK_FREQUENCY))/timeSinceLastUpdate_;
     156    if(timeSinceLastUpdate_>=NETWORK_PERIOD){
     157      timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD;
    156158      gamestates_->processGamestates();
    157159      updateGamestate();
     
    162164    return connection->addPacket(packet, clientID);
    163165  }
    164 
     166 
     167  /**
     168   * @brief: returns ping time to client in milliseconds
     169   */
     170  unsigned int Server::getPing(unsigned int clientID){
     171    assert(ClientInformation::findClient(clientID));
     172    return ClientInformation::findClient(clientID)->getRTT();
     173  }
     174
     175  /**
     176   * @brief: return packet loss ratio to client (scales from 0 to 1)
     177   */
     178  double Server::getPacketLoss(unsigned int clientID){
     179    assert(ClientInformation::findClient(clientID));
     180    return ClientInformation::findClient(clientID)->getPacketLoss();
     181  }
     182 
    165183  /**
    166184  * processes all the packets waiting in the queue
     
    239257      if(gs==NULL){
    240258        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
     259        temp = temp->next();
    241260        continue;
    242261      }
     
    281300
    282301  bool Server::addClient(ENetEvent *event){
     302    static unsigned int newid=1;
     303
     304    COUT(2) << "Server: adding client" << std::endl;
    283305    ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
    284306    if(!temp){
     
    286308      return false;
    287309    }
    288     if(temp==ClientInformation::getBegin()) { //not good if you use anything else than insertBack
    289       temp->setID(1);
     310    /*if(temp==ClientInformation::getBegin()) { //not good if you use anything else than insertBack
     311      newid=1;
    290312    }
    291313    else
    292       temp->setID(temp->prev()->getID()+1);
     314      newid=temp->prev()->getID()+1;*/
     315    temp->setID(newid);
    293316    temp->setPeer(event->peer);
     317
     318    // inform all the listeners
     319    orxonox::ObjectList<ClientConnectionListener>::iterator listener = orxonox::ObjectList<ClientConnectionListener>::begin();
     320    while(listener){
     321      listener->clientConnected(newid);
     322      listener++;
     323    }
     324
     325    newid++;
     326
    294327    COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
    295328    return createClient(temp->getID());
    296   }
     329}
    297330
    298331  bool Server::createClient(int clientID){
     
    304337    COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
    305338    connection->syncClassid(temp->getID());
    306     COUT(5) << "creating spaceship for clientid: " << temp->getID() << std::endl;
    307     // TODO: this is only a hack, untill we have a possibility to define default player-join actions
    308     if(!createShip(temp))
    309       COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl;
    310     else
    311       COUT(3) << "created spaceship" << std::endl;
    312339    temp->setSynched(true);
    313340    COUT(3) << "sending welcome" << std::endl;
     
    319346    g->setClientID(temp->getID());
    320347    b = g->collectData(0);
    321     assert(b);
     348    if(!b)
     349      return false; //no data for the client
    322350    b = g->compressData();
    323351    assert(b);
     
    327355  }
    328356
    329   bool Server::createShip(ClientInformation *client){
    330     if(!client)
    331       return false;
    332     orxonox::Identifier* id = ClassByName("SpaceShip");
    333     if(!id){
    334       COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl;
    335       return false;
    336     }
    337     orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());
    338     no->classID = id->getNetworkID();
    339     client->setShipID(no->getObjectID());
    340     no->setPosition(orxonox::Vector3(0,0,80));
    341     no->setScale(10);
    342     //no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0));
    343     no->setMesh("assff.mesh");
    344     no->setMaxSpeed(500);
    345     no->setMaxSideAndBackSpeed(50);
    346     no->setMaxRotation(1.0);
    347     no->setTransAcc(200);
    348     no->setRotAcc(3.0);
    349     no->setTransDamp(75);
    350     no->setRotDamp(1.0);
    351     no->setCamera(std::string("cam_") + convertToString(client->getID()));
    352     no->create();
    353 
    354     return true;
    355   }
    356 
    357357  bool Server::disconnectClient(ENetEvent *event){
    358358    COUT(4) << "removing client from list" << std::endl;
     
    360360
    361361    //boost::recursive_mutex::scoped_lock lock(head_->mutex_);
    362     orxonox::ObjectList<orxonox::SpaceShip>::iterator it = orxonox::ObjectList<orxonox::SpaceShip>::begin();
    363362    ClientInformation *client = ClientInformation::findClient(&event->peer->address);
    364363    if(!client)
    365364      return false;
    366365    gamestates_->removeClient(client);
    367     while(it){
    368       if(it->getObjectID()!=client->getShipID()){
    369         ++it;
    370         continue;
    371       }
    372       orxonox::ObjectList<orxonox::SpaceShip>::iterator temp=it;
    373       ++it;
    374       delete  *temp;
    375       return ClientInformation::removeClient(event->peer);
    376     }
    377     return false;
     366
     367// inform all the listeners
     368    orxonox::ObjectList<ClientConnectionListener>::iterator listener = orxonox::ObjectList<ClientConnectionListener>::begin();
     369    while(listener){
     370      listener->clientDisconnected(client->getID());
     371      listener++;
     372    }
     373
     374    return ClientInformation::removeClient(event->peer);
    378375  }
    379376
     
    387384    gamestates_->removeClient(client);
    388385  }
    389  
    390   bool Server::chat(std::string message){
     386
     387  bool Server::chat(const std::string& message){
     388      return this->sendChat(message, Host::getPlayerID());
     389  }
     390
     391  bool Server::broadcast(const std::string& message){
     392      return this->sendChat(message, CLIENTID_UNKNOWN);
     393  }
     394
     395  bool Server::sendChat(const std::string& message, unsigned int clientID){
    391396    ClientInformation *temp = ClientInformation::getBegin();
    392397    packet::Chat *chat;
    393398    while(temp){
    394       chat = new packet::Chat(message, Host::getPlayerID());
     399      chat = new packet::Chat(message, clientID);
    395400      chat->setClientID(temp->getID());
    396401      if(!chat->send())
     
    398403      temp = temp->next();
    399404    }
    400     COUT(1) << "Player " << Host::getPlayerID() << ": " << message << std::endl;
     405//    COUT(1) << "Player " << Host::getPlayerID() << ": " << message << std::endl;
     406    for (orxonox::ObjectList<ChatListener>::iterator it = orxonox::ObjectList<ChatListener>::begin(); it != orxonox::ObjectList<ChatListener>::end(); ++it)
     407      it->incomingChat(message, clientID);
     408
    401409    return true;
    402410  }
Note: See TracChangeset for help on using the changeset viewer.