Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1409


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)

Location:
code/branches/network/src
Files:
10 edited

Legend:

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

    r1377 r1409  
    120120    isConnected=client_connection.createConnection();
    121121    if(isConnected){
    122       COUT(3) << "sending connectrequest" << std::endl;
    123       if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets())
    124         COUT(1) << "could not send connection request !!!!!!!!!" << std::endl;
     122//       COUT(3) << "sending connectrequest" << std::endl;
     123//       if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets())
     124//         COUT(1) << "could not send connection request !!!!!!!!!" << std::endl;
    125125    }else
    126126      COUT(1) << "could not create connection laber" << std::endl;
     
    239239      }
    240240    }
    241     ENetPacket *packet;
     241    ENetEvent *event;
    242242    // stop if the packet queue is empty
    243243    while(!(client_connection.queueEmpty())){
    244       packet = client_connection.getPacket();
    245       COUT(5) << "tick packet size " << packet->dataLength << std::endl;
    246       elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
     244      event = client_connection.getEvent();
     245      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
     246      elaborate(event->packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
    247247    }
    248248    if(!client_connection.sendPackets())
  • code/branches/network/src/network/ClientConnection.cc

    r1377 r1409  
    7575
    7676
    77   ENetPacket *ClientConnection::getPacket(ENetAddress &address) {
     77  /*ENetPacket *ClientConnection::getPacket(ENetAddress &address) {
    7878    if(!buffer.isEmpty()) {
    7979      //std::cout << "###BUFFER IS NOT EMPTY###" << std::endl;
     
    8888    ENetAddress address; //sems that address is not needed
    8989    return getPacket(address);
     90  }*/
     91 
     92  ENetEvent *ClientConnection::getEvent(){
     93    if(!buffer.isEmpty())
     94      return buffer.pop();
     95    else
     96      return NULL;
    9097  }
    9198
     
    144151    enet_initialize();
    145152    atexit(enet_deinitialize);
    146     ENetEvent event;
     153    ENetEvent *event;
    147154    client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
    148155    if(client==NULL) {
     
    159166    //main loop
    160167    while(!quit){
     168      event = new ENetEvent;
    161169      //std::cout << "connection loop" << std::endl;
    162       if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)<0){
     170      if(enet_host_service(client, event, NETWORK_CLIENT_TIMEOUT)<0){
    163171        // we should never reach this point
    164172        quit=true;
    165173        // add some error handling here ========================
    166174      }
    167       switch(event.type){
     175      switch(event->type){
    168176        // log handling ================
    169177      case ENET_EVENT_TYPE_CONNECT:
     
    171179      case ENET_EVENT_TYPE_RECEIVE:
    172180        COUT(5) << "Cl.Con: receiver-Thread while loop: got new packet" << std::endl;
    173         if ( !processData(&event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl;
     181        if ( !processData(event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl;
    174182        COUT(5) << "Cl.Con: processed Data in receiver-thread while loop" << std::endl;
    175183        break;
  • code/branches/network/src/network/ClientConnection.h

    r1377 r1409  
    6363    ClientConnection(int port, std::string address);
    6464    ClientConnection(int port, const char* address);
    65     ENetPacket *getPacket(ENetAddress &address); // thread1
    66     ENetPacket *getPacket(); // thread1
     65    //ENetPacket *getPacket(ENetAddress &address); // thread1
     66    //ENetPacket *getPacket(); // thread1
     67    ENetEvent *getEvent();
    6768    // check wheter the packet queue is empty
    6869    bool queueEmpty();
  • code/branches/network/src/network/ConnectionManager.cc

    r1389 r1409  
    4444#include "core/CoreIncludes.h"
    4545#include "core/BaseObject.h"
     46#include "objects/SpaceShip.h"
    4647#include "util/Math.h"
    47 #include "objects/SpaceShip.h"
    4848#include "ClientInformation.h"
    4949#include "ConnectionManager.h"
     
    9494  }
    9595
    96   ENetPacket *ConnectionManager::getPacket(ENetAddress &address) {
     96  /*ENetPacket *ConnectionManager::getPacket(ENetAddress &address) {
    9797    if(!buffer.isEmpty())
    9898      return buffer.pop(address);
    9999    else
    100100      return NULL;
    101   }
     101  }*/
    102102/**
    103103This function only pops the first element in PacketBuffer (first in first out)
    104104used by processQueue in Server.cc
    105105*/
    106   ENetPacket *ConnectionManager::getPacket(int &clientID) {
     106  /*ENetPacket *ConnectionManager::getPacket(int &clientID) {
    107107    ENetAddress address;
    108108    ENetPacket *packet=getPacket(address);
     
    112112    clientID=temp->getID();
    113113    return packet;
     114  }*/
     115 
     116  ENetEvent *ConnectionManager::getEvent(){
     117    if(!buffer.isEmpty())
     118      return buffer.pop();
     119    else
     120      return NULL;
    114121  }
    115122
     
    176183  void ConnectionManager::receiverThread() {
    177184    // what about some error-handling here ?
     185    ENetEvent *event;
    178186    enet_initialize();
    179187    atexit(enet_deinitialize);
    180     ENetEvent *event = new ENetEvent;
    181188    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
    182189    if(server==NULL){
     
    187194
    188195    while(!quit){
     196      event = new ENetEvent;
    189197      if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
    190198        // we should never reach this point
     
    195203        // log handling ================
    196204        case ENET_EVENT_TYPE_CONNECT:
    197           addClient(event);
     205          COUT(3) << "adding event_type_connect to queue" << std::endl;
     206        case ENET_EVENT_TYPE_DISCONNECT:
     207          //addClient(event);
    198208          //this is a workaround to ensure thread safety
    199           COUT(5) << "Con.Man: connection event has occured" << std::endl;
    200           break;
     209          //COUT(5) << "Con.Man: connection event has occured" << std::endl;
     210          //break;
    201211        case ENET_EVENT_TYPE_RECEIVE:
    202212          //std::cout << "received data" << std::endl;
    203213          COUT(5) << "Con.Man: receive event has occured" << std::endl;
    204214          // only add, if client has connected yet and not been disconnected
    205           if(head_->findClient(&event->peer->address))
     215          //if(head_->findClient(&event->peer->address))
    206216            processData(event);
    207           else
    208             COUT(3) << "received a packet from a client we don't know" << std::endl;
     217//           else
     218//             COUT(3) << "received a packet from a client we don't know" << std::endl;
    209219          break;
    210         case ENET_EVENT_TYPE_DISCONNECT:
    211           clientDisconnect(event->peer);
    212           break;
     220        //case ENET_EVENT_TYPE_DISCONNECT:
     221          //clientDisconnect(event->peer);
     222          //break;
    213223        case ENET_EVENT_TYPE_NONE:
     224          delete event;
    214225          //receiverThread_->yield();
    215226          break;
     
    262273  }
    263274
    264   bool ConnectionManager::clientDisconnect(ENetPeer *peer) {
     275  /*bool ConnectionManager::clientDisconnect(ENetPeer *peer) {
    265276    COUT(4) << "removing client from list" << std::endl;
    266277    return removeClient(head_->findClient(&(peer->address))->getID());
    267   }
     278  }*/
    268279/**
    269280This function adds a client that connects to the clientlist of the server
     
    271282addClientTest in diffTest.cc since addClient is not good for testing because of syncClassid
    272283*/
    273   bool ConnectionManager::addClient(ENetEvent *event) {
     284  /*bool ConnectionManager::addClient(ENetEvent *event) {
    274285    ClientInformation *temp = head_->insertBack(new ClientInformation);
    275286    if(!temp){
     
    286297    COUT(3) << "Con.Man: added client id: " << temp->getID() << std::endl;
    287298    return true;
    288   }
     299  }*/
    289300
    290301  int ConnectionManager::getClientID(ENetPeer peer) {
     
    324335  }
    325336
    326   bool ConnectionManager::createClient(int clientID){
     337  /*bool ConnectionManager::createClient(int clientID){
    327338    ClientInformation *temp = head_->findClient(clientID);
    328339    if(!temp){
     
    342353    sendWelcome(temp->getID(), temp->getShipID(), true);
    343354    return true;
    344   }
    345  
    346   bool ConnectionManager::removeClient(int clientID){
     355  }*/
     356 
     357  /*bool ConnectionManager::removeClient(int clientID){
    347358    boost::recursive_mutex::scoped_lock lock(head_->mutex_);
    348359    orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start();
     
    361372    }
    362373    return false;
    363   }
    364  
    365   bool ConnectionManager::createShip(ClientInformation *client){
     374  }*/
     375 
     376/*  bool ConnectionManager::createShip(ClientInformation *client){
    366377    if(!client)
    367378      return false;
     
    389400    client->setShipID(no->objectID);
    390401    return true;
    391   }
     402  }*/
    392403 
    393404  bool ConnectionManager::removeShip(ClientInformation *client){
  • code/branches/network/src/network/ConnectionManager.h

    r1379 r1409  
    7777    ConnectionManager(int port, const char *address, ClientInformation *head);
    7878    ConnectionManager(int port, std::string address, ClientInformation *head);
    79     ENetPacket *getPacket(ENetAddress &address); // thread1
    80     ENetPacket *getPacket(int &clientID);
     79    //ENetPacket *getPacket(ENetAddress &address); // thread1
     80    //ENetPacket *getPacket(int &clientID);
     81    ENetEvent *getEvent();
    8182    bool queueEmpty();
    8283    void createListener();
     
    8788  //  bool sendPackets(ENetEvent *event);
    8889    bool sendPackets();
    89     bool createClient(int clientID);
     90    //bool createClient(int clientID);
    9091    void disconnectClient(ClientInformation *client);
     92    void syncClassid(int clientID);
     93    bool sendWelcome(int clientID, int shipID, bool allowed);
    9194
    9295  private:
    93     bool clientDisconnect(ENetPeer *peer);
    94     bool removeClient(int clientID);
     96//     bool clientDisconnect(ENetPeer *peer);
     97//     bool removeClient(int clientID);
    9598    bool processData(ENetEvent *event);
    96     bool addClient(ENetEvent *event);
     99    //bool addClient(ENetEvent *event);
    97100    void receiverThread();
    98101    void disconnectClients();
    99102    int getClientID(ENetPeer peer);
    100103    int getClientID(ENetAddress address);
    101     void syncClassid(int clientID);
    102104    ENetPeer *getClientPeer(int clientID);
    103     bool createShip(ClientInformation *client);
     105    //bool createShip(ClientInformation *client);
    104106    bool removeShip(ClientInformation *client);
    105     bool sendWelcome(int clientID, int shipID, bool allowed);
    106107    bool addFakeConnectRequest(ENetEvent *ev);
    107108    PacketBuffer buffer;
  • code/branches/network/src/network/PacketBuffer.cc

    r1360 r1409  
    6262      last->next=NULL;
    6363      // change this!!!!!!!  ---- we are not doing stl so we won't change this
    64       last->packet = ev->packet;
    65       last->address = ev->peer->address;
     64      last->event = ev;
    6665      //last->address = ev->peer->address;
    6766    } else {
     
    7271      last->next=NULL;
    7372      // save the packet to the new element
    74       last->packet = ev->packet;
    75       last->address = ev->peer->address;
     73      last->event = ev;
    7674      //last->address = ev->peer->address;
    7775    }
     
    8280  //returns the first element in the list without deleting it but
    8381  //moving first pointer to next element
    84   ENetPacket *PacketBuffer::pop() {
     82  /*ENetPacket *PacketBuffer::pop() {
    8583    ENetAddress address;
    8684    return pop(address);
    87   }
    88 
    89   ENetPacket *PacketBuffer::pop(ENetAddress &address) {
     85  }*/
     86 
     87  ENetEvent *PacketBuffer::pop(){
    9088    boost::recursive_mutex::scoped_lock lock(mutex_);
    9189    //std::cout << "packetbuffer pop(address)" << std::endl;
     
    9391      QueueItem *temp = first;
    9492      // get packet
    95       ENetPacket *pck=first->packet;
    96       address = first->address;
     93      ENetEvent *ev=first->event;
     94      //address = first->address;
     95      // remove first element
     96      first = first->next;
     97      delete temp;
     98      lock.unlock();
     99      //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl;
     100      return ev;
     101    } else{
     102      lock.unlock();
     103      return NULL;
     104    }
     105  }
     106
     107  /*ENetPacket *PacketBuffer::pop(ENetAddress &address) {
     108    boost::recursive_mutex::scoped_lock lock(mutex_);
     109    //std::cout << "packetbuffer pop(address)" << std::endl;
     110    if(first!=NULL ){
     111      QueueItem *temp = first;
     112      // get packet
     113      ENetPacket *pck=first->event->packet;
     114      address = first->event->peer->address;
    97115      // remove first element
    98116      first = first->next;
     
    105123      return NULL;
    106124    }
    107   }
     125  }*/
    108126
    109127  bool PacketBuffer::isEmpty() {
  • code/branches/network/src/network/PacketBuffer.h

    r1360 r1409  
    5555
    5656  struct QueueItem{
    57     ENetPacket *packet;
    58     ENetAddress address;
     57    ENetEvent *event;
     58    //ENetAddress address;
    5959    QueueItem *next;
    6060  };
     
    6868    void print();
    6969    // pops a packet from the queue
    70     ENetPacket *pop();
    71     ENetPacket *pop(ENetAddress &address);
     70    //ENetPacket *pop();
     71    //ENetPacket *pop(ENetAddress &address);
     72    ENetEvent *pop();
    7273    // pushs a packet to the queue
    7374    bool push(ENetEvent *ev);
  • 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){
  • code/branches/network/src/network/Server.h

    r1379 r1409  
    7272    void updateGamestate();
    7373  private:
     74    bool addClient(ENetEvent *event);
     75    bool createClient(int clientID);
     76    bool createShip(ClientInformation *client);
     77    bool disconnectClient(ENetEvent *event);
    7478    void disconnectClient(int clientID);
    7579    void disconnectClient( ClientInformation *client);
  • code/branches/network/src/orxonox/objects/Camera.cc

    r1293 r1409  
    6060  {
    6161    CameraHandler::getInstance()->releaseFocus(this);
     62    GraphicsEngine::getSingleton().getSceneManager()->getRootSceneNode()->removeAndDestroyChild(cameraNode_->getName());
    6263  }
    6364
Note: See TracChangeset for help on using the changeset viewer.