Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 22, 2010, 7:24:24 PM (13 years ago)
Author:
dafrick
Message:

Merging presentation2 branch back to trunk.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

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

    r7284 r7801  
    6060#include "FunctionCallManager.h"
    6161#include "GamestateManager.h"
     62#include "WANDiscovery.h"
    6263
    6364namespace orxonox
     
    99100  }
    100101
     102
     103  /** helper that connects to the master server */
     104  void Server::helper_ConnectToMasterserver()
     105  {
     106//     WANDiscovery::getInstance().msc.sendRequest( MSPROTO_GAME_SERVER " "
     107//       MSPROTO_REGISTER_SERVER );
     108  }
     109
    101110  /**
    102111  * This function opens the server by creating the listener thread
     
    107116    COUT(4) << "opening server" << endl;
    108117    this->openListener();
     118   
     119    /* make discoverable on LAN */
    109120    LANDiscoverable::setActivity(true);
     121
     122    /* make discoverable on WAN */
     123    WANDiscoverable::setActivity(true);
     124    /* TODO this needs to be optional, we need a switch from the UI to
     125     * enable/disable this
     126     */
     127//     helper_ConnectToMasterserver();
     128
     129    /* done */
    110130    return;
    111131  }
     
    120140    this->disconnectClients();
    121141    this->closeListener();
     142
     143    /* tell master server we're closing */
     144    COUT(2) << "disconnecting." << endl;
     145    WANDiscoverable::setActivity(false);   
     146    COUT(2) << "disconnecting done" << endl;
     147
    122148    LANDiscoverable::setActivity(false);
    123149    return;
     
    130156    while(temp){
    131157      chat = new packet::Chat(message, playerID);
    132       chat->setClientID(temp->getID());
    133       if(!chat->send())
     158      chat->setPeerID(temp->getID());
     159      if(!chat->send( static_cast<Host*>(this) ))
    134160        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
    135161      temp = temp->next();
     
    139165  }
    140166
     167
     168  /* handle incoming data */
     169  int rephandler( char *addr, ENetEvent *ev )
     170  {
     171    /* reply to pings */
     172    if( !strncmp( (char *)ev->packet->data, MSPROTO_PING_GAMESERVER,
     173      MSPROTO_PING_GAMESERVER_LEN ) )
     174      //this->msc.sendRequest( MSPROTO_ACK );
     175      /* NOTE implement this after pollForReply
     176       * reimplementation
     177       */
     178      return 0;
     179
     180    /* done handling, return all ok code 0 */
     181    return 0;
     182  }
     183
     184  void Server::helper_HandleMasterServerRequests()
     185  {
     186    /* poll the master server for replies and see whether something
     187     * has to be done or changed.
     188     */
     189    //WANDiscovery::getInstance().msc.pollForReply( rhandler, 10 );
     190  }
    141191
    142192  /**
     
    149199    // receive incoming packets
    150200    Connection::processQueue();
     201
    151202    // receive and process incoming discovery packets
    152203    LANDiscoverable::update();
     204   
     205    // receive and process requests from master server
     206    /* todo */
     207    //helper_HandleMasterServerRequests();
    153208
    154209    if ( ClientInformation::hasClients() )
     
    156211      // process incoming gamestates
    157212      GamestateManager::processGamestates();
     213      FunctionCallManager::processBufferedFunctionCalls();
    158214
    159215      // send function calls to clients
    160       FunctionCallManager::sendCalls();
     216      FunctionCallManager::sendCalls( static_cast<Host*>(this) );
    161217
    162218      //this steers our network frequency
     
    167223        updateGamestate();
    168224      }
    169       sendPackets(); // flush the enet queue
    170     }
    171   }
    172 
    173   bool Server::queuePacket(ENetPacket *packet, int clientID)
    174   {
    175     return ServerConnection::addPacket(packet, clientID);
     225//       sendPackets(); // flush the enet queue
     226    }
     227  }
     228
     229  void Server::queuePacket(ENetPacket *packet, int clientID, uint8_t channelID)
     230  {
     231    ServerConnection::addPacket(packet, clientID, channelID);
    176232  }
    177233
     
    209265      return;
    210266    GamestateManager::update();
    211     COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
     267//     COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
    212268    //std::cout << "updated gamestate, sending it" << std::endl;
    213269    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
    214     sendGameState();
     270    sendGameStates();
    215271    sendObjectDeletes();
    216     COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
     272//     COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
    217273    //std::cout << "sent gamestate" << std::endl;
    218274  }
    219275
    220   bool Server::processPacket( ENetPacket *packet, ENetPeer *peer ){
    221     packet::Packet *p = packet::Packet::createPacket(packet, peer);
    222     return p->process();
    223   }
    224 
    225   /**
    226   * sends the gamestate
    227   */
    228   bool Server::sendGameState()
    229   {
    230 //     COUT(5) << "Server: starting function sendGameState" << std::endl;
    231 //     ClientInformation *temp = ClientInformation::getBegin();
    232 //     bool added=false;
    233 //     while(temp != NULL){
    234 //       if( !(temp->getSynched()) ){
    235 //         COUT(5) << "Server: not sending gamestate" << std::endl;
    236 //         temp=temp->next();
    237 //         if(!temp)
    238 //           break;
    239 //         continue;
    240 //       }
    241 //       COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
    242 //       COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
    243 //       int cid = temp->getID(); //get client id
    244 //       packet::Gamestate *gs = GamestateManager::popGameState(cid);
    245 //       if(gs==NULL){
    246 //         COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
    247 //         temp = temp->next();
    248 //         continue;
    249 //       }
    250 //       //std::cout << "adding gamestate" << std::endl;
    251 //       gs->setClientID(cid);
    252 //       if ( !gs->send() ){
    253 //         COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
    254 //         temp->addFailure();
    255 //       }else
    256 //         temp->resetFailures();
    257 //       added=true;
    258 //       temp=temp->next();
    259 //       // gs gets automatically deleted by enet callback
    260 //     }
    261     GamestateManager::sendGamestates();
     276  /**
     277  * sends the current gamestate to all peers
     278  */
     279  bool Server::sendGameStates()
     280  {
     281    std::vector<packet::Gamestate*> gamestates = GamestateManager::getGamestates();
     282    std::vector<packet::Gamestate*>::iterator it;
     283    for( it = gamestates.begin(); it != gamestates.end(); ++it )
     284    {
     285      (*it)->send(static_cast<Host*>(this));
     286    }
    262287    return true;
    263288  }
     289
    264290
    265291  bool Server::sendObjectDeletes()
     
    286312      packet::DeleteObjects *cd = new packet::DeleteObjects(*del);
    287313      assert(cd);
    288       cd->setClientID(cid);
    289       if ( !cd->send() )
    290         COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
     314      cd->setPeerID(cid);
     315      if ( !cd->send( static_cast<Host*>(this) ) )
     316        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended" << std::endl;
    291317      temp=temp->next();
    292318      // gs gets automatically deleted by enet callback
     
    312338    // inform all the listeners
    313339    ClientConnectionListener::broadcastClientConnected(newid);
     340    GamestateManager::addPeer(newid);
    314341
    315342    ++newid;
     
    327354    else
    328355    {
     356      GamestateManager::removePeer(client->getID());
    329357      //ServerConnection::disconnectClient( client );
    330358      //ClientConnectionListener::broadcastClientDisconnected( client->getID() ); //this is done in ClientInformation now
     
    332360    }
    333361  }
     362 
     363  void Server::processPacket(packet::Packet* packet)
     364  {
     365    if( packet->isReliable() )
     366    {
     367      if( this->getLastProcessedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
     368        packet->process(static_cast<Host*>(this));
     369      else
     370        this->packetQueue_.push_back(packet);
     371    }
     372    else
     373      packet->process(static_cast<Host*>(this));
     374  }
     375
    334376
    335377  bool Server::createClient(int clientID)
     
    341383      return false;
    342384    }
    343     COUT(5) << "Con.Man: creating client id: " << temp->getID() << std::endl;
     385    COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
    344386
    345387    // synchronise class ids
     
    348390    // now synchronise functionIDs
    349391    packet::FunctionIDs *fIDs = new packet::FunctionIDs();
    350     fIDs->setClientID(clientID);
    351     bool b = fIDs->send();
     392    fIDs->setPeerID(clientID);
     393    bool b = fIDs->send( static_cast<Host*>(this) );
    352394    assert(b);
    353395
    354396    temp->setSynched(true);
     397    GamestateManager::setSynched(clientID);
     398   
    355399    COUT(4) << "sending welcome" << std::endl;
    356400    packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID());
    357     w->setClientID(temp->getID());
    358     b = w->send();
     401    w->setPeerID(temp->getID());
     402    b = w->send( static_cast<Host*>(this) );
    359403    assert(b);
    360404    packet::Gamestate *g = new packet::Gamestate();
    361     g->setClientID(temp->getID());
    362     b = g->collectData(0,0x1);
     405    g->setPeerID(temp->getID());
     406    b = g->collectData(0,packet::GAMESTATE_MODE_SERVER);
     407    assert(b);
    363408    if(!b)
    364409      return false; //no data for the client
    365     b = g->compressData();
    366     assert(b);
    367     b = g->send();
     410//     b = g->compressData();
     411//     assert(b);
     412    b = g->send( static_cast<Host*>(this) );
    368413    assert(b);
    369414    return true;
     
    373418  {
    374419    ServerConnection::disconnectClient( client );
    375     GamestateManager::removeClient(client);
     420    GamestateManager::removePeer(client->getID());
    376421    // inform all the listeners
    377422    // ClientConnectionListener::broadcastClientDisconnected(client->getID()); // this is done in ClientInformation now
     
    395440    {
    396441      chat = new packet::Chat(message, clientID);
    397       chat->setClientID(temp->getID());
    398       if(!chat->send())
     442      chat->setPeerID(temp->getID());
     443      if(!chat->send( static_cast<Host*>(this) ))
    399444        COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
    400445      temp = temp->next();
     
    411456    int failures=0;
    412457    packet::ClassID *classid = new packet::ClassID();
    413     classid->setClientID(clientID);
    414     while(!classid->send() && failures < 10){
     458    classid->setPeerID(clientID);
     459    while(!classid->send( static_cast<Host*>(this) ) && failures < 10){
    415460      failures++;
    416461    }
Note: See TracChangeset for help on using the changeset viewer.