Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 24, 2008, 2:57:43 PM (16 years ago)
Author:
scheusso
Message:

changed concept of threading, had to change packetbuffer (using events now)

File:
1 edited

Legend:

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

    r1389 r1409  
    5050//#include "NetworkFrameListener.h"
    5151#include "util/Sleep.h"
     52#include "objects/SpaceShip.h"
    5253
    5354
     
    165166  */
    166167  void Server::processQueue() {
    167     ENetPacket *packet;
     168    ENetEvent *event;
    168169    int clientID=-1;
    169170    while(!connection->queueEmpty()){
    170171      //std::cout << "Client " << clientID << " sent: " << std::endl;
    171172      //clientID here is a reference to grab clientID from ClientInformation
    172       packet = connection->getPacket(clientID);
    173       if(!packet)
     173      event = connection->getEvent();
     174      if(!event)
    174175        continue;
     176      switch( event->type ) {
     177      case ENET_EVENT_TYPE_CONNECT:
     178        COUT(3) << "processing event_Type_connect" << std::endl;
     179        addClient(event);
     180        break;
     181      case ENET_EVENT_TYPE_DISCONNECT:
     182        if(clients->findClient(&event->peer->address))
     183          disconnectClient(event);
     184        break;
     185      case ENET_EVENT_TYPE_RECEIVE:
     186        if(clients->findClient(&event->peer->address)){
     187          clientID = clients->findClient(&event->peer->address)->getID();
     188          if( !elaborate(event->packet, clientID) )
     189            COUT(3) << "Server: could not elaborate" << std::endl;
     190        }
     191        break;
     192      }
     193      delete event;
    175194      //if statement to catch case that packetbuffer is empty
    176       if( !elaborate(packet, clientID) )
    177         COUT(3) << "Server: could not elaborate" << std::endl;
    178195    }
    179196  }
     
    250267 
    251268  bool Server::processConnectRequest( connectRequest *con, int clientID ){
    252     COUT(3) << "processing connectRequest " << std::endl;
     269    //(COUT(3) << "processing connectRequest " << std::endl;
    253270    //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID);
    254     connection->createClient(clientID);
     271    //createClient(clientID);
    255272    delete con;
    256273    return true;
     
    265282        clients->findClient(clientID)->resetFailures();
    266283  }
     284 
     285  bool Server::addClient(ENetEvent *event){
     286    ClientInformation *temp = clients->insertBack(new ClientInformation);
     287    if(!temp){
     288      COUT(2) << "Server: could not add client" << std::endl;
     289      return false;
     290    }
     291    if(temp->prev()->getHead()) { //not good if you use anything else than insertBack
     292      temp->prev()->setID(0); //bugfix: not necessary but usefull
     293      temp->setID(1);
     294    }
     295    else
     296      temp->setID(temp->prev()->getID()+1);
     297    temp->setPeer(event->peer);
     298    COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
     299    return createClient(temp->getID());
     300  }
     301 
     302  bool Server::createClient(int clientID){
     303    ClientInformation *temp = clients->findClient(clientID);
     304    if(!temp){
     305      COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
     306      return false;
     307    }
     308    COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
     309    connection->syncClassid(temp->getID());
     310    COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl;
     311    // TODO: this is only a hack, untill we have a possibility to define default player-join actions
     312    if(!createShip(temp))
     313      COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl;
     314    else
     315      COUT(3) << "created spaceship" << std::endl;
     316    temp->setSynched(true);
     317    COUT(3) << "sending welcome" << std::endl;
     318    connection->sendWelcome(temp->getID(), temp->getShipID(), true);
     319    return true;
     320  }
     321 
     322  bool Server::createShip(ClientInformation *client){
     323    if(!client)
     324      return false;
     325    orxonox::Identifier* id = ID("SpaceShip");
     326    if(!id){
     327      COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl;
     328      return false;
     329    }
     330    orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());
     331    no->setPosition(orxonox::Vector3(0,0,80));
     332    no->setScale(10);
     333    //no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0));
     334    no->setMesh("assff.mesh");
     335    no->setMaxSpeed(500);
     336    no->setMaxSideAndBackSpeed(50);
     337    no->setMaxRotation(1.0);
     338    no->setTransAcc(200);
     339    no->setRotAcc(3.0);
     340    no->setTransDamp(75);
     341    no->setRotDamp(1.0);
     342    no->setCamera("cam_"+client->getID());
     343    no->classID = id->getNetworkID();
     344    no->create();
     345   
     346    client->setShipID(no->objectID);
     347    return true;
     348  }
     349 
     350  bool Server::disconnectClient(ENetEvent *event){
     351    COUT(4) << "removing client from list" << std::endl;
     352    //return removeClient(head_->findClient(&(peer->address))->getID());
     353   
     354    //boost::recursive_mutex::scoped_lock lock(head_->mutex_);
     355    orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start();
     356    ClientInformation *client = clients->findClient(&event->peer->address);
     357    if(!client)
     358      return false;
     359    while(it){
     360      if(it->objectID!=client->getShipID()){
     361        ++it;
     362        continue;
     363      }
     364      orxonox::Iterator<orxonox::SpaceShip> temp=it;
     365      ++it;
     366      delete  *temp;
     367      return clients->removeClient(event->peer);
     368    }
     369    return false;
     370  }
    267371
    268372  void Server::disconnectClient(int clientID){
Note: See TracChangeset for help on using the changeset viewer.