Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 1, 2008, 3:54:20 PM (16 years ago)
Author:
rgrieder
Message:
  • @everyone: Do not create a branch until I've added the svn:eol-style property correctly. Otherwise this would cost me another 4 hours or so when we want to merge back.
  • merged network branch back to trunk
  • I had to omit the changes from last evening concerning the line endings
  • might not work yet because of the line endings
  • @beni: script branch is the only branch still open. you probably will have to apply a patch because of inconsistent new lines
File:
1 edited

Legend:

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

    r1360 r1502  
    4242
    4343#include <iostream>
     44
    4445
    4546#include "ConnectionManager.h"
     
    4950//#include "NetworkFrameListener.h"
    5051#include "util/Sleep.h"
     52#include "objects/SpaceShip.h"
    5153
    5254
     
    5658 
    5759#define MAX_FAILURES 20;
    58  
     60#define NETWORK_FREQUENCY 30
    5961 
    6062  /**
     
    6365  */
    6466  Server::Server() {
     67    timeSinceLastUpdate_=0;
    6568    packet_gen = PacketGenerator();
    6669    clients = new ClientInformation(true);
     
    6871    gamestates = new GameStateManager(clients);
    6972  }
     73 
     74  Server::Server(int port){
     75    timeSinceLastUpdate_=0;
     76    packet_gen = PacketGenerator();
     77    clients = new ClientInformation(true);
     78    connection = new ConnectionManager(clients, port);
     79    gamestates = new GameStateManager(clients);
     80  }
    7081
    7182  /**
     
    7586  */
    7687  Server::Server(int port, std::string bindAddress) {
     88    timeSinceLastUpdate_=0;
    7789    packet_gen = PacketGenerator();
    7890    clients = new ClientInformation();
     
    8799  */
    88100  Server::Server(int port, const char *bindAddress) {
     101    timeSinceLastUpdate_=0;
    89102    packet_gen = PacketGenerator();
    90103    clients = new ClientInformation();
     
    117130    ENetPacket *packet = packet_gen.chatMessage(msg.c_str());
    118131    //std::cout <<"adding packets" << std::endl;
    119     if(connection->addPacketAll(packet))
    120     //std::cout <<"added packets" << std::endl;
    121       return connection->sendPackets();
    122     else
    123       return false;
     132    return connection->addPacketAll(packet);
    124133  }
    125134
     
    132141    ENetPacket *packet = packet_gen.chatMessage(msg);
    133142    COUT(4) <<"Server: adding Packets" << std::endl;
    134     connection->addPacketAll(packet);
    135     //std::cout <<"added packets" << std::endl;
    136     if (connection->sendPackets()){
    137       COUT(4) << "Server: Sucessfully" << std::endl;
    138       return true;
    139     }
    140     return false;
     143    return connection->addPacketAll(packet);
    141144  }
    142145
     
    148151  void Server::tick(float time) {
    149152    processQueue();
    150     updateGamestate();
    151 //     usleep(500000); // TODO remove
     153    //this steers our network frequency
     154    timeSinceLastUpdate_+=time;
     155    if(timeSinceLastUpdate_>=(1./NETWORK_FREQUENCY)){
     156      timeSinceLastUpdate_-=(1./NETWORK_FREQUENCY);
     157      gamestates->processGameStates();
     158      updateGamestate();
     159    }
     160//     usleep(5000); // TODO remove
    152161    return;
    153162  }
     
    157166  */
    158167  void Server::processQueue() {
    159     ENetPacket *packet;
     168    ENetEvent *event;
    160169    int clientID=-1;
    161170    while(!connection->queueEmpty()){
    162171      //std::cout << "Client " << clientID << " sent: " << std::endl;
    163172      //clientID here is a reference to grab clientID from ClientInformation
    164       packet = connection->getPacket(clientID);
    165       if(!packet)
    166         continue;
     173      event = connection->getEvent();
     174      if(!event)
     175        continue;
     176      assert(event->type != ENET_EVENT_TYPE_NONE);
     177      switch( event->type ) {
     178      case ENET_EVENT_TYPE_CONNECT:
     179        COUT(3) << "processing event_Type_connect" << std::endl;
     180        addClient(event);
     181        break;
     182      case ENET_EVENT_TYPE_DISCONNECT:
     183        if(clients->findClient(&event->peer->address))
     184          disconnectClient(event);
     185        break;
     186      case ENET_EVENT_TYPE_RECEIVE:
     187        if(clients->findClient(&event->peer->address)){
     188          clientID = clients->findClient(&event->peer->address)->getID();
     189          if( !elaborate(event->packet, clientID) )
     190            COUT(3) << "Server: could not elaborate" << std::endl;
     191        }
     192        break;
     193      }
     194      delete event;
    167195      //if statement to catch case that packetbuffer is empty
    168       if( !elaborate(packet, clientID) )
    169         COUT(3) << "Server: could not elaborate" << std::endl;
    170196    }
    171197  }
     
    210236      if(gs==NULL){
    211237        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
    212         return false;
     238        continue;
    213239      }
    214240      //std::cout << "adding gamestate" << std::endl;
    215       if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) ){
     241      ENetPacket *packet = packet_gen.gstate(gs);
     242      if(!packet)
     243        continue;
     244      if ( !(connection->addPacket(packet, cid)) ){
    216245        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
    217246        temp->addFailure();
    218         if(temp->getFailures() > 20 )
    219           disconnectClient(temp);
     247        /*if(temp->getFailures() > 0 )
     248          disconnectClient(temp);*/
    220249      //std::cout << "added gamestate" << std::endl;
    221       }
     250      }else
     251        temp->resetFailures();
    222252      added=true;
    223253      temp=temp->next();
     
    226256      delete gs;
    227257    }
    228     if(added) {
     258    /*if(added) {
    229259      //std::cout << "send gamestates from server.cc in sendGameState" << std::endl;
    230260      return connection->sendPackets();
    231     }
    232     COUT(5) << "Server: had no gamestates to send" << std::endl;
    233     return false;
     261    }*/
     262    //COUT(5) << "Server: had no gamestates to send" << std::endl;
     263    return true;
    234264  }
    235265
    236266  void Server::processAck( ack *data, int clientID) {
    237     COUT(4) << "\b\b\b\n\n\n\n\nServer: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
     267    COUT(4) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
    238268    gamestates->ackGameState(clientID, data->a);
    239269    delete data;
     
    241271 
    242272  bool Server::processConnectRequest( connectRequest *con, int clientID ){
    243     COUT(3) << "processing connectRequest " << std::endl;
     273    //(COUT(3) << "processing connectRequest " << std::endl;
    244274    //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID);
    245     connection->createClient(clientID);
     275    //createClient(clientID);
    246276    delete con;
    247277    return true;
     
    250280  void Server::processGamestate( GameStateCompressed *data, int clientID){
    251281    COUT(4) << "processing partial gamestate from client " << clientID << std::endl;
    252     if(!gamestates->pushGameState(data, clientID))
    253         COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
     282    gamestates->addGameState(data, clientID);
     283        /*COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
    254284    else
    255285      if(clients->findClient(clientID))
    256         clients->findClient(clientID)->resetFailures();
     286        clients->findClient(clientID)->resetFailures();*/
     287  }
     288 
     289  bool Server::addClient(ENetEvent *event){
     290    ClientInformation *temp = clients->insertBack(new ClientInformation);
     291    if(!temp){
     292      COUT(2) << "Server: could not add client" << std::endl;
     293      return false;
     294    }
     295    if(temp->prev()->getHead()) { //not good if you use anything else than insertBack
     296      temp->prev()->setID(0); //bugfix: not necessary but usefull
     297      temp->setID(1);
     298    }
     299    else
     300      temp->setID(temp->prev()->getID()+1);
     301    temp->setPeer(event->peer);
     302    COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
     303    return createClient(temp->getID());
     304  }
     305 
     306  bool Server::createClient(int clientID){
     307    ClientInformation *temp = clients->findClient(clientID);
     308    if(!temp){
     309      COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
     310      return false;
     311    }
     312    COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
     313    connection->syncClassid(temp->getID());
     314    COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl;
     315    // TODO: this is only a hack, untill we have a possibility to define default player-join actions
     316    if(!createShip(temp))
     317      COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl;
     318    else
     319      COUT(3) << "created spaceship" << std::endl;
     320    temp->setSynched(true);
     321    COUT(3) << "sending welcome" << std::endl;
     322    connection->sendWelcome(temp->getID(), temp->getShipID(), true);
     323    return true;
     324  }
     325 
     326  bool Server::createShip(ClientInformation *client){
     327    if(!client)
     328      return false;
     329    orxonox::Identifier* id = ID("SpaceShip");
     330    if(!id){
     331      COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl;
     332      return false;
     333    }
     334    orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());
     335    no->setPosition(orxonox::Vector3(0,0,80));
     336    no->setScale(10);
     337    //no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0));
     338    no->setMesh("assff.mesh");
     339    no->setMaxSpeed(500);
     340    no->setMaxSideAndBackSpeed(50);
     341    no->setMaxRotation(1.0);
     342    no->setTransAcc(200);
     343    no->setRotAcc(3.0);
     344    no->setTransDamp(75);
     345    no->setRotDamp(1.0);
     346    no->setCamera("cam_"+client->getID());
     347    no->classID = id->getNetworkID();
     348    no->create();
     349   
     350    client->setShipID(no->objectID);
     351    return true;
     352  }
     353 
     354  bool Server::disconnectClient(ENetEvent *event){
     355    COUT(4) << "removing client from list" << std::endl;
     356    //return removeClient(head_->findClient(&(peer->address))->getID());
     357   
     358    //boost::recursive_mutex::scoped_lock lock(head_->mutex_);
     359    orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start();
     360    ClientInformation *client = clients->findClient(&event->peer->address);
     361    if(!client)
     362      return false;
     363    while(it){
     364      if(it->objectID!=client->getShipID()){
     365        ++it;
     366        continue;
     367      }
     368      orxonox::Iterator<orxonox::SpaceShip> temp=it;
     369      ++it;
     370      delete  *temp;
     371      return clients->removeClient(event->peer);
     372    }
     373    return false;
    257374  }
    258375
Note: See TracChangeset for help on using the changeset viewer.