Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1245


Ignore:
Timestamp:
May 7, 2008, 4:39:42 PM (16 years ago)
Author:
scheusso
Message:

different enhancements in input/network handling and synchronisation

Location:
code/branches/network3/src
Files:
15 edited

Legend:

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

    r1233 r1245  
    8484    // set server address to localhost
    8585    isConnected=false;
     86    test_once=false;
    8687  }
    8788
     
    9394  Client::Client(std::string address, int port) : client_connection(port, address){
    9495    isConnected=false;
     96    test_once=false;
    9597  }
    9698
     
    102104  Client::Client(const char *address, int port) : client_connection(port, address){
    103105    isConnected=false;
     106    test_once=false;
    104107  }
    105108
     
    226229  */
    227230  void Client::tick(float time){
     231    if(client_connection.isConnected()){
     232      COUT(4) << "popping partial gamestate: " << std::endl;
     233      GameStateCompressed *gs = gamestate.popPartialGameState();
     234      if(gs){
     235        COUT(4) << "client tick: sending gs " << gs << std::endl;
     236        if(client_connection.addPacket(pck_gen.gstate(gs)))
     237          if(!client_connection.sendPackets())
     238            COUT(3) << "Problem sending partial gamestate" << std::endl;
     239        // now delete it to save memory
     240        delete [] gs->data;
     241        delete gs;
     242      }
     243    }
    228244    ENetPacket *packet;
    229245    // stop if the packet queue is empty
     
    231247      packet = client_connection.getPacket();
    232248      COUT(5) << "tick packet size " << packet->dataLength << std::endl;
    233       elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
     249      if(!test_once){
     250        elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
     251      }
    234252    }
    235253    return;
    236254  }
    237255
    238   void Client::processGamestate( GameStateCompressed *data){
    239     int id = data->id;
    240     COUT(5) << "received gamestate id: " << data->id << std::endl;
    241     if(gamestate.pushGameState(data)){
    242       client_connection.addPacket(pck_gen.acknowledgement(id));
    243       if(!client_connection.sendPackets())
    244         COUT(2) << "Could not send acknowledgment" << std::endl;
     256  void Client::processGamestate( GameStateCompressed *data, int clientID){
     257    if(!test_once){
     258      int id = data->id;
     259      COUT(5) << "received gamestate id: " << data->id << std::endl;
     260      if(gamestate.pushGameState(data)){
     261        client_connection.addPacket(pck_gen.acknowledgement(id));
     262        if(!client_connection.sendPackets())
     263          COUT(2) << "Could not send acknowledgment" << std::endl;
     264      }
     265//       test_once=true;
    245266    }
    246267  }
     
    263284    clientID_ = w->clientID;
    264285    shipID_ = w->shipID;
    265    
     286    return true;
    266287  }
    267288
  • code/branches/network3/src/network/Client.h

    r1232 r1245  
    103103
    104104    // implement data processing functions of PacketDecoder
    105     void processGamestate( GameStateCompressed *data);
     105    void processGamestate( GameStateCompressed *data, int clientID);
    106106    void processClassid(classid *clid);
    107107    void processChat( chat *data);
     
    109109    int clientID_;     // this is the id the server gave to us
    110110    int shipID_;
     111    bool test_once;
    111112  };
    112113
  • code/branches/network3/src/network/ClientConnection.h

    r1168 r1245  
    7676    bool sendPackets(ENetEvent *event);
    7777    bool waitEstablished(int milisec);
     78    bool isConnected(){return established;}
    7879  private:
    7980    bool processData(ENetEvent *event);
  • code/branches/network3/src/network/ConnectionManager.cc

    r1234 r1245  
    165165    enet_initialize();
    166166    atexit(enet_deinitialize);
    167     ENetEvent event;
     167    ENetEvent *event = new ENetEvent;
    168168    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
    169169    if(server==NULL){
     
    174174
    175175    while(!quit){
    176       if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){
     176      if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
    177177        // we should never reach this point
    178178        quit=true;
    179179        // add some error handling here ========================
    180180      }
    181       switch(event.type){
     181      switch(event->type){
    182182        // log handling ================
    183183        case ENET_EVENT_TYPE_CONNECT:
    184           addClient(&event);
     184          addClient(event);
    185185          //this is a workaround to ensure thread safety
    186186          /*if(!addFakeConnectRequest(&event))
     
    191191          //std::cout << "received data" << std::endl;
    192192          COUT(5) << "Con.Man: receive event has occured" << std::endl;
    193           processData(&event);
     193          processData(event);
    194194          break;
    195195        case ENET_EVENT_TYPE_DISCONNECT:
    196           // add some error/log handling here
    197           clientDisconnect(event.peer);
     196          clientDisconnect(event->peer);
    198197          break;
    199198        case ENET_EVENT_TYPE_NONE:
    200199          break;
    201200      }
     201//       usleep(1000);
     202      //yield(); //TODO: find apropriate
    202203    }
    203204    disconnectClients();
     
    246247  bool ConnectionManager::clientDisconnect(ENetPeer *peer) {
    247248    COUT(4) << "removing client from list" << std::endl;
    248     return head_->removeClient(peer);
     249    return removeClient(head_->findClient(&(peer->address))->getID());
    249250  }
    250251/**
     
    311312    sendWelcome(temp->getID(), temp->getShipID(), true);
    312313    return true;
     314  }
     315 
     316  bool ConnectionManager::removeClient(int clientID){
     317    orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start();
     318    while(it){
     319      if(it->objectID!=head_->findClient(clientID)->getShipID()){
     320        ++it;
     321        continue;
     322      }
     323      orxonox::Iterator<orxonox::SpaceShip> temp=it;
     324      ++it;
     325      delete  *temp;
     326      return head_->removeClient(clientID);
     327    }
     328    return false;
    313329  }
    314330 
     
    335351   
    336352    client->setShipID(no->objectID);
    337     no->getFocus();
    338353    return true;
    339354  }
     
    342357    addPacket(packet_gen.generateWelcome(clientID, shipID, allowed),clientID);
    343358    sendPackets();
     359    return true;
    344360  }
    345361 
  • code/branches/network3/src/network/ConnectionManager.h

    r1232 r1245  
    8989  private:
    9090    bool clientDisconnect(ENetPeer *peer);
     91    bool removeClient(int clientID);
    9192    bool processData(ENetEvent *event);
    9293    bool addClient(ENetEvent *event);
  • code/branches/network3/src/network/GameStateClient.cc

    r1232 r1245  
    166166
    167167  GameState *GameStateClient::getPartialSnapshot(){
     168   
     169    GameState *reference;
     170//     std::map<int, GameState*>::iterator it = --gameStateMap.end();
     171//     reference=(--gameStateMap.end())->second;
     172   
    168173    //std::cout << "begin getSnapshot" << std::endl;
    169174    //the size of the gamestate
     
    178183
    179184    GameState *retval=new GameState; //return value
    180     retval->id=reference->id;
     185//     retval->id=reference->id;
     186    if(gameStateMap.size()!=0)
     187      retval->id=(--gameStateMap.end())->second->id;
    181188    retval->diffed=false;
    182189    retval->complete=false;
     
    190197      size+=it->getSize(); // size of the actual data of the synchronisable
    191198      size+=3*sizeof(int); // size of datasize, classID and objectID
     199      COUT(4) << "getpartialsnapshot: size: " << size << std::endl;
    192200    }
    193201    //retval->data = (unsigned char*)malloc(size);
  • code/branches/network3/src/network/GameStateClient.h

    r1232 r1245  
    7070    void printGameStateMap();
    7171
    72     GameState     *reference;
    7372    int           last_diff_;
    7473    std::map<int, GameState *> gameStateMap;
  • code/branches/network3/src/network/GameStateManager.cc

    r1233 r1245  
    106106    if(gID != GAMESTATEID_INITIAL){
    107107      // TODO something with the gamestatemap is wrong
    108       GameState *client = gameStateMap.find(gID)->second;
     108      GameState *client=NULL;
     109      std::map<int, GameState*>::iterator it = gameStateMap.find(gID);
     110      if(it!=gameStateMap.end())
     111        client = it->second;
    109112      GameState *server = reference;
    110113      //head_->findClient(clientID)->setGamestateID(id);
     
    203206
    204207  bool GameStateManager::loadPartialSnapshot(GameState *state, int clientID){
     208    if(!state)
     209      return false;
    205210    unsigned char *data=state->data;
    206211    COUT(4) << "loadSnapshot: loading gs: " << state->id << std::endl;
     
    219224      sync.data = data;
    220225      data+=sync.length;
    221 
     226      COUT(4) << "objectID: " << sync.objectID << " classID: " << sync.classID << std::endl;
    222227      while(it && it->objectID!=sync.objectID)
    223228        ++it;
     
    226231      if(!it){
    227232        // the object does not exist yet
     233        COUT(4) << "loadsnapshot: creating new object " << std::endl;
    228234        //COUT(4) << "loadSnapshot:\tclassid: " << sync.classID << ", name: " << ID((unsigned int) sync.classID)->getName() << std::endl;
    229235        orxonox::Identifier* id = ID((unsigned int)sync.classID);
     
    244250      }else{
    245251        // we have our object
     252        COUT(4) << "loadpartialsnapshot: we found the appropriate object" << std::endl;
    246253        if(checkAccess(clientID, sync.objectID)){
    247254          if(! it->updateData(sync))
     
    396403    //std::cout << "length " << length << std::endl;
    397404    switch ( retval ) {
    398       case Z_OK: COUT(4) << "successfully decompressed" << std::endl; break;
     405      case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break;
    399406      case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return NULL;
    400407      case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return NULL;
     
    444451  bool GameStateManager::checkAccess(int clientID, int objectID){
    445452    // currently we only check, wheter the object is the clients spaceship
    446     return head_->findClient(objectID)->getShipID()==objectID;
     453//     return head_->findClient(objectID)->getShipID()==objectID;
     454    return true; // TODO: change this
    447455  }
    448456
  • code/branches/network3/src/network/PacketDecoder.cc

    r1233 r1245  
    7878      return true;
    7979    case GAMESTATE:
    80       gstate( packet );
     80      gstate( packet, clientId );
    8181      return true;
    8282    case CLASSID:
     
    155155  }
    156156
    157   void PacketDecoder::gstate( ENetPacket* packet )
     157  void PacketDecoder::gstate( ENetPacket* packet, int clientID )
    158158  {
    159159    GameStateCompressed* currentState = NULL;
     
    190190    //clean memory
    191191    enet_packet_destroy( packet );
    192     processGamestate(currentState);
     192    processGamestate(currentState, clientID);
    193193  }
    194194
     
    234234  }
    235235
    236   void PacketDecoder::processGamestate( GameStateCompressed *state )
     236  void PacketDecoder::processGamestate( GameStateCompressed *state, int clientID )
    237237  {
    238238    COUT(5) << "PacketDecoder: processing Gamestate" << std::endl;
  • code/branches/network3/src/network/PacketManager.h

    r1232 r1245  
    9191    void mousem( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
    9292    void keystrike( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
    93     void chatMessage( ENetPacket* packet, int clientId = CLIENTID_CLIENT);
    94     void gstate( ENetPacket* packet );
     93    void chatMessage( ENetPacket* packet, int clientId = CLIENTID_CLIENT );
     94    void gstate( ENetPacket* packet, int clientID = CLIENTID_CLIENT );
    9595    void clid( ENetPacket *packet);
    9696    bool decodeWelcome( ENetPacket* packet, int clientID = CLIENTID_CLIENT );
     
    9999    //process data
    100100    //two functions are note yet implemented!
    101     virtual void processGamestate(GameStateCompressed *state);
     101    virtual void processGamestate(GameStateCompressed *state, int clientID);
    102102    virtual void processAck( ack *data, int clientID);
    103103    virtual void processClassid( classid *cid);
  • code/branches/network3/src/network/Server.cc

    r1232 r1245  
    143143    updateGamestate();
    144144
    145     usleep(200000); // TODO remove
     145//     usleep(500000); // TODO remove
    146146    return;
    147147  }
     
    228228    //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID);
    229229    connection->createClient(clientID);
     230    return true;
     231  }
     232 
     233  void Server::processGamestate( GameStateCompressed *data, int clientID){
     234    COUT(4) << "processing partial gamestate from client " << clientID << std::endl;
     235    if(!gamestates->pushGameState(data, clientID))
     236        COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;
     237//     delete[] data->data;
     238//     delete data;
    230239  }
    231240
  • code/branches/network3/src/network/Server.h

    r1232 r1245  
    7474    void processAck( ack *data, int clientID);
    7575    bool processConnectRequest( connectRequest *con, int clientID );
     76    void processGamestate( GameStateCompressed *data, int clientID);
    7677    ConnectionManager *connection;
    7778    GameStateManager *gamestates;
  • code/branches/network3/src/network/Synchronisable.cc

    r1232 r1245  
    5050 
    5151 
    52   int Synchronisable::state_=1; // detemines wheter we are server (default) or client
     52  int Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
    5353 
    5454  /**
     
    165165    for(i=syncList->begin(); n<datasize && i!=syncList->end(); ++i){
    166166      //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int));
    167       if( ((*i)->mode & state_) == 0 )
     167      if( ((*i)->mode & state_) == 0 ){
     168        COUT(4) << "not getting data: " << std::endl;
    168169        continue;  // this variable should only be received
     170      }
    169171      switch((*i)->type){
    170172      case DATA:
     
    199201    COUT(5) << "Synchronisable: objectID " << vars.objectID << ", classID " << vars.classID << " size: " << vars.length << " synchronising data" << std::endl;
    200202    for(i=syncList->begin(); i!=syncList->end(); i++){
    201       if( ((*i)->mode ^ state_) == 0 )
    202         continue;  // this variable should only be sent
     203      if( ((*i)->mode ^ state_) == 0 ){
     204        COUT(5) << "synchronisable: not updating variable " << std::endl;
     205        continue;  // this variable should only be updated
     206      }
    203207      COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl;
    204208      switch((*i)->type){
  • code/branches/network3/src/orxonox/objects/SpaceShip.cc

    r1234 r1245  
    104104        this->setRotationAxis(1, 0, 0);
    105105        this->setStatic(false);
    106 /*
    107         this->moveForward_ = 0;
    108         this->rotateUp_ = 0;
    109         this->rotateDown_ = 0;
    110         this->rotateRight_ = 0;
    111         this->rotateLeft_ = 0;
    112         this->loopRight_ = 0;
    113         this->loopLeft_ = 0;
    114         this->brakeForward_ = 0;
    115         this->brakeRotate_ = 0;
    116         this->brakeLoop_ = 0;
    117         this->speedForward_ = 0;
    118         this->speedRotateUpDown_ = 0;
    119         this->speedRotateRightLeft_ = 0;
    120         this->speedLoopRightLeft_ = 0;
    121         this->maxSpeedForward_ = 0;
    122         this->maxSpeedRotateUpDown_ = 0;
    123         this->maxSpeedRotateRightLeft_ = 0;
    124         this->maxSpeedLoopRightLeft_ = 0;
    125         this->accelerationForward_ = 0;
    126         this->accelerationRotateUpDown_ = 0;
    127         this->accelerationRotateRightLeft_ = 0;
    128         this->accelerationLoopRightLeft_ = 0;
    129 
    130         this->speed = 250;
    131         this->loop = 100;
    132         this->rotate = 10;
    133         this->mouseX = 0;
    134         this->mouseY = 0;
    135         this->maxMouseX = 0;
    136         this->minMouseX = 0;
    137         this->moved = false;
    138 
    139         this->brakeRotate(rotate*10);
    140         this->brakeLoop(loop);
    141 */
    142 //         this->create();
    143106
    144107
     
    161124
    162125    void SpaceShip::registerAllVariables(){
    163       registerVar( &camName_, camName_.length()+1, network::STRING);
     126      registerVar( &camName_, camName_.length()+1, network::STRING, 0x1);
     127      registerVar( &maxSpeed_, sizeof(maxSpeed_), network::DATA, 0x1);
     128      registerVar( &maxSideAndBackSpeed_, sizeof(maxSideAndBackSpeed_), network::DATA, 0x1);
     129      registerVar( &maxRotation_, sizeof(maxRotation_), network::DATA, 0x1);
     130      registerVar( &translationAcceleration_, sizeof(translationAcceleration_), network::DATA, 0x1);
     131      registerVar( &rotationAcceleration_, sizeof(rotationAcceleration_), network::DATA, 0x1);
     132      registerVar( &rotationAccelerationRadian_, sizeof(rotationAccelerationRadian_), network::DATA, 0x1);
     133      registerVar( &translationDamping_, sizeof(translationDamping_), network::DATA, 0x1);
     134      registerVar( &rotationDamping_, sizeof(rotationDamping_), network::DATA, 0x1);
     135      registerVar( &rotationDampingRadian_, sizeof(rotationDampingRadian_), network::DATA, 0x1);
     136     
    164137    }
    165138
    166139    void SpaceShip::init()
    167140    {
    168         COUT(4) << "================ \t\t\t\tspaceship::init" << std::endl;
     141//         COUT(4) << "================ \t\t\t\tspaceship::init" << std::endl;
    169142        // START CREATING THRUSTER
    170143        this->tt_ = new ParticleInterface(GraphicsEngine::getSingleton().getSceneManager(),"twinthruster" + this->getName(),"Orxonox/engineglow");
     
    197170        this->greenNode_->setInheritScale(false);
    198171
    199         COUT(4) << "attaching red billboard" << std::endl;
     172//         COUT(4) << "attaching red billboard" << std::endl;
    200173        this->redNode_->attachObject(this->redBillboard_.getBillboardSet());
    201174        this->redNode_->setScale(0.3, 0.3, 0.3);
    202175
    203         COUT(4) << "attaching green billboard" << std::endl;
     176//         COUT(4) << "attaching green billboard" << std::endl;
    204177        this->greenNode_->attachObject(this->greenBillboard_.getBillboardSet());
    205178        this->greenNode_->setScale(0.3, 0.3, 0.3);
     
    215188        this->chFarNode_->setInheritScale(false);
    216189
    217         COUT(4) << "attaching crosshair near billboard" << std::endl;
     190//         COUT(4) << "attaching crosshair near billboard" << std::endl;
    218191        this->chNearNode_->attachObject(this->crosshairNear_.getBillboardSet());
    219192        this->chNearNode_->setScale(0.2, 0.2, 0.2);
    220193
    221         COUT(4) << "attaching crosshair far billboard" << std::endl;
     194//         COUT(4) << "attaching crosshair far billboard" << std::endl;
    222195        this->chFarNode_->attachObject(this->crosshairFar_.getBillboardSet());
    223196        this->chFarNode_->setScale(0.4, 0.4, 0.4);
    224         COUT(4) << "attaching camera" << std::endl;
     197//         COUT(4) << "attaching camera" << std::endl;
    225198       
    226199        createCamera();
     
    237210    void SpaceShip::loadParams(TiXmlElement* xmlElem)
    238211    {
    239       COUT(4) << "using loadparams" << std::endl;
     212//       COUT(4) << "using loadparams" << std::endl;
    240213        Model::loadParams(xmlElem);
    241214        this->create();
     
    303276   
    304277    void SpaceShip::createCamera(){
    305       COUT(4) << "begin camera creation" << std::endl;
     278//       COUT(4) << "begin camera creation" << std::endl;
    306279      this->camNode_ = this->getNode()->createChildSceneNode(camName_);
    307280      COUT(4) << "position: (this)" << this->getNode()->getPosition() << std::endl;
     
    313286      cam->lookAt(Vector3(0,20,0));
    314287      cam->roll(Degree(0));
    315       */COUT(4) << "creating new camera" << std::endl;
     288      *//*COUT(4) << "creating new camera" << std::endl;*/
    316289      cam_ = new Camera(this->camNode_);
    317       COUT(4) << "setting target node" << std::endl;
     290//       COUT(4) << "setting target node" << std::endl;
    318291      cam_->setTargetNode(this->getNode());
    319292      // this only applies to clients
    320       if(network::Client::getSingleton()!=0 && network::Client::getSingleton()->getShipID()==objectID)
     293      if(network::Client::getSingleton()!=0 && network::Client::getSingleton()->getShipID()==objectID){
     294        this->setBacksync(true);
    321295        CameraHandler::getInstance()->requestFocus(cam_);
     296      }
    322297//        cam->setPosition(Vector3(0,-350,0));
    323298      //cam->roll(Degree(-90));
    324299
    325300      //this->camNode_->attachObject(cam);
    326       COUT(4) << "created camera" << std::endl;
     301//       COUT(4) << "created camera" << std::endl;
    327302    }
    328303
     
    460435    {
    461436        // only do this if not client TODO: remove this hack
    462       if (!network::Client::getSingleton() && InputManager::getSingleton().getMouse()->getEventCallback() != this)
     437      if ((!network::Client::getSingleton() || network::Client::getSingleton()->getShipID()==objectID ) && InputManager::getSingleton().getMouse()->getEventCallback() != this)
    463438        {
    464439            if (InputManager::getSingleton().getMouse())
     
    485460        if (this->bLMousePressed_ && this->timeToReload_ <= 0)
    486461        {
    487             new Projectile(this);
     462            Projectile *p = new Projectile(this);
     463            p->setBacksync(true);
    488464            this->timeToReload_ = this->reloadTime_;
    489465        }
     
    560536
    561537        if(!network::Client::getSingleton() || network::Client::getSingleton()->getShipID() == objectID){
     538          COUT(4) << "steering our ship: " << objectID << " mkeyboard: " << mKeyboard << std::endl;
    562539          if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W))
    563540              this->acceleration_.x = this->translationAcceleration_;
     
    580557          else
    581558              this->momentum_ = 0;
    582         }
     559        }/*else
     560          COUT(4) << "not steering ship: " << objectID << " our ship: " << network::Client::getSingleton()->getShipID() << std::endl;*/
    583561
    584562        WorldEntity::tick(dt);
  • code/branches/network3/src/orxonox/objects/WorldEntity.cc

    r1232 r1245  
    8181        if (!this->bStatic_)
    8282        {
     83//             COUT(4) << "acceleration: " << this->acceleration_ << " velocity: " << this->velocity_ << std::endl;
    8384            this->velocity_ += (dt * this->acceleration_);
    8485            this->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL);
     
    222223      //register acceleration
    223224      // register velocity_
    224       registerVar( (void*) &(this->getAcceleration().x), sizeof(this->getAcceleration().x), network::DATA, 0x3);
    225       registerVar( (void*) &(this->getAcceleration().y), sizeof(this->getAcceleration().y), network::DATA, 0x3);
    226       registerVar( (void*) &(this->getAcceleration().z), sizeof(this->getAcceleration().z), network::DATA, 0x3);
     225//       registerVar( (void*) &(this->getAcceleration().x), sizeof(this->getAcceleration().x), network::DATA, 0x3);
     226//       registerVar( (void*) &(this->getAcceleration().y), sizeof(this->getAcceleration().y), network::DATA, 0x3);
     227//       registerVar( (void*) &(this->getAcceleration().z), sizeof(this->getAcceleration().z), network::DATA, 0x3);
    227228    }
    228229
Note: See TracChangeset for help on using the changeset viewer.