Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 10, 2018, 3:06:55 PM (6 years ago)
Author:
merholzl
Message:

Merged Masterserver, refresh button had to be removed

Location:
code/branches/mergeFS18
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/mergeFS18

  • code/branches/mergeFS18/src/libraries/network/Server.cc

    r11103 r12027  
    5555#include "packet/Gamestate.h"
    5656#include "packet/Welcome.h"
    57 // #include "ClientInformation.h"
    5857#include "FunctionCallManager.h"
    5958#include "GamestateManager.h"
     
    6968  Server::Server()
    7069  {
    71     this->timeSinceLastUpdate_=0;
    72   }
    73 
     70    this->timeSinceLastUpdate_ = 0;
     71  }
     72
     73  /**
     74  * Constructor
     75  * @param port Port to listen on
     76  */
    7477  Server::Server(int port)
    7578  {
    76     this->setPort( port );
    77     this->timeSinceLastUpdate_=0;
    78   }
    79 /*
    80   Server::Server(int port, const std::string name)
    81   {
    82     this->setPort( port );
    83     this->timeSinceLastUpdate_=0;
    84     this->serverName_=name;
    85   }*/
     79    this->setPort(port);
     80    this->timeSinceLastUpdate_ = 0;
     81  }
     82
    8683  /**
    8784  * Constructor
     
    9188  Server::Server(int port, const std::string& bindAddress)
    9289  {
    93     this->setPort( port );
    94     this->setBindAddress( bindAddress );
    95     this->timeSinceLastUpdate_=0;
     90    this->setPort(port);
     91    this->setBindAddress(bindAddress);
     92    this->timeSinceLastUpdate_ = 0;
    9693  }
    9794
     
    156153    LANDiscoverable::update();
    157154
    158     if ( GamestateManager::hasPeers() )
     155    if (GamestateManager::hasPeers())
    159156    {
    160157      // process incoming gamestates
     
    163160
    164161      // send function calls to clients
    165       FunctionCallManager::sendCalls( static_cast<Host*>(this) );
    166 
    167       //this steers our network frequency
    168       timeSinceLastUpdate_+=time.getDeltaTime();
    169       if(timeSinceLastUpdate_>=NETWORK_PERIOD)
     162      FunctionCallManager::sendCalls(static_cast<Host*>(this));
     163
     164      // this steers our network frequency
     165      timeSinceLastUpdate_ += time.getDeltaTime();
     166      if(timeSinceLastUpdate_ >= NETWORK_PERIOD)
    170167      {
    171         timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD;
    172         updateGamestate();
     168        timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD;
     169        this->updateGamestate();
    173170      }
    174 //       sendPackets(); // flush the enet queue
    175171    }
    176172  }
     
    182178
    183179  /**
    184    * @brief: returns ping time to client in milliseconds
    185    */
    186   unsigned int Server::getRTT(unsigned int clientID)
    187   {
    188 //     assert(ClientInformation::findClient(clientID));
    189 //     return ClientInformation::findClient(clientID)->getRTT();
    190     // TODO: reimplement
    191     return 0;
    192   }
    193 
     180   * Print ping time to client in milliseconds.
     181   */
    194182  void Server::printRTT()
    195183  {
    196 //     for( ClientInformation* temp=ClientInformation::getBegin(); temp!=nullptr; temp=temp->next() )
    197 //       orxout(message) << "Round trip time to client with ID: " << temp->getID() << " is " << temp->getRTT() << " ms" << endl;
    198   }
    199 
    200   /**
    201    * @brief: return packet loss ratio to client (scales from 0 to 1)
    202    */
    203   float Server::getPacketLoss(unsigned int clientID)
    204   {
    205 //     assert(ClientInformation::findClient(clientID));
    206 //     return ClientInformation::findClient(clientID)->getPacketLoss();
    207     return 0.;
    208   }
    209 
    210   /**
    211   * takes a new snapshot of the gamestate and sends it to the clients
    212   */
     184    // TODO: Implement
     185  }
     186
     187  /**
     188   * Take a new snapshot of the gamestate and send it to the clients.
     189   */
    213190  void Server::updateGamestate()
    214191  {
    215     if( this->clientIDs_.size()==0 )
    216       //no client connected
     192    if(this->clientIDs_.size() == 0)
     193    {
     194      // no client connected
    217195      return;
     196    }
    218197    GamestateManager::update();
    219 //     orxout(verbose_more, context::network) << "Server: one gamestate update complete, goig to sendGameState" << endl;
    220     //orxout(verbose_more, context::network) << "updated gamestate, sending it" << endl;
    221     //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
    222     sendGameStates();
    223     sendObjectDeletes();
    224 //     orxout(verbose_more, context::network) << "Server: one sendGameState turn complete, repeat in next tick" << endl;
    225     //orxout(verbose_more, context::network) << "sent gamestate" << endl;
    226   }
    227 
    228   /**
    229   * sends the current gamestate to all peers
    230   */
     198    this->sendGameStates();
     199    this->sendObjectDeletes();
     200  }
     201
     202  /**
     203   * Send the current gamestate to all peers.
     204   */
    231205  bool Server::sendGameStates()
    232206  {
    233207    std::vector<packet::Gamestate*> gamestates = GamestateManager::getGamestates();
    234     for( packet::Gamestate* gamestate : gamestates )
     208    for(packet::Gamestate* gamestate : gamestates)
    235209    {
    236210      gamestate->send(static_cast<Host*>(this));
     
    240214
    241215
     216  /**
     217   * Send 'DeleteObjects' packet
     218   */
    242219  bool Server::sendObjectDeletes()
    243220  {
    244 //     ClientInformation *temp = ClientInformation::getBegin();
    245 //     if( temp == nullptr )
    246       //no client connected
    247     if( this->clientIDs_.size()==0 )
     221    // no client connected
     222    if(this->clientIDs_.size() == 0)
     223    {
    248224      return true;
     225    }
     226
    249227    packet::DeleteObjects *del = new packet::DeleteObjects();
    250228    if(!del->fetchIDs())
     
    253231      return true;  //everything ok (no deletes this tick)
    254232    }
    255 //     orxout(verbose, context::network) << "sending DeleteObjects" << endl;
    256 //     while(temp != nullptr){
    257 //       if( !(temp->getSynched()) )
    258 //       {
    259 //         orxout(verbose_more, context::network) << "Server: not sending gamestate" << endl;
    260 //         temp=temp->next();
    261 //         continue;
    262 //       }
    263 //       int cid = temp->getID(); //get client id
    264 //       packet::DeleteObjects *cd = new packet::DeleteObjects(*del);
    265 //       assert(cd);
     233
    266234    del->setPeerID(NETWORK_PEER_ID_BROADCAST);
    267     if ( !del->send( static_cast<Host*>(this) ) )
     235    if (!del->send( static_cast<Host*>(this)))
     236    {
    268237      orxout(internal_warning, context::network) << "Server: could not broadcast deleteObjects packet" << endl;
    269 //       temp=temp->next();
    270       // gs gets automatically deleted by enet callback
    271 //     }
    272 //     delete del;
     238    }
     239
    273240    return true;
    274241  }
    275242
    276 
     243  /**
     244   * Add a new peer to the server.
     245   */
    277246  void Server::addPeer(uint32_t peerID)
    278247  {
    279 //     static unsigned int newid=1;
    280 //
    281 //     orxout(internal_info, context::network) << "Server: adding client" << endl;
    282 //     ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
    283 //     if(!temp)
    284 //     {
    285 //       orxout(internal_warning, context::network) << "Server: could not add client" << endl;
    286 //     }
    287 //     temp->setID(newid);
    288 //     temp->setPeer(event->peer);
    289 
    290248    // inform all the listeners
    291249    this->clientIDs_.push_back(peerID);
     
    296254    GamestateManager::addPeer(peerID);
    297255
    298 //     ++newid;
    299 
    300256    orxout(internal_info, context::network) << "Server: added client id: " << peerID << endl;
    301257
    302     createClient(peerID);
    303 }
    304 
     258    this->createClient(peerID);
     259  }
     260
     261  /**
     262   * Remove a peer from the server.
     263   */
    305264  void Server::removePeer(uint32_t peerID)
    306265  {
    307266    orxout(verbose, context::network) << "removing client from list" << endl;
    308 //     ClientInformation *client = ClientInformation::findClient(&event->peer->address);
    309 //     if(!client)
    310 //       return;
    311 //     else
    312 //     {
    313   std::vector<uint32_t>::iterator it;
    314   for( it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it )
    315   {
    316     if( *it == peerID )
    317     {
    318       this->clientIDs_.erase(it);
    319       break;
    320     }
    321   }
    322   WANDiscoverable::updateClientNumber(this->clientIDs_.size());
    323   LANDiscoverable::updateClientNumber(this->clientIDs_.size());
    324 
    325   ClientConnectionListener::broadcastClientDisconnected(peerID);
    326   GamestateManager::removePeer(peerID);
    327       //ServerConnection::disconnectClient( client );
    328       //ClientConnectionListener::broadcastClientDisconnected( client->getID() ); //this is done in ClientInformation now
    329 //       delete client;
    330 //     }
    331   }
    332 
     267
     268    // erase the peer from the list
     269    std::vector<uint32_t>::iterator it;
     270    for(it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it)
     271    {
     272      if(*it == peerID)
     273      {
     274        this->clientIDs_.erase(it);
     275        break;
     276      }
     277    }
     278
     279    WANDiscoverable::updateClientNumber(this->clientIDs_.size());
     280    LANDiscoverable::updateClientNumber(this->clientIDs_.size());
     281
     282    // Send 'clientDisconnected' message
     283    ClientConnectionListener::broadcastClientDisconnected(peerID);
     284
     285    GamestateManager::removePeer(peerID);
     286  }
     287
     288  /**
     289   * Process an incoming packet.
     290   */
    333291  void Server::processPacket(packet::Packet* packet)
    334292  {
    335     if( packet->isReliable() )
    336     {
    337       if( this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
     293    if(packet->isReliable())
     294    {
     295      if(this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID())
     296      {
    338297        packet->process(static_cast<Host*>(this));
     298      }
    339299      else
     300      {
    340301        this->packetQueue_.push_back(packet);
     302      }
    341303    }
    342304    else
     305    {
    343306      packet->process(static_cast<Host*>(this));
    344   }
    345 
    346 
     307    }
     308  }
     309
     310  /**
     311   * Create a client.
     312   */
    347313  bool Server::createClient(int clientID)
    348314  {
    349 //     ClientInformation *temp = ClientInformation::findClient(clientID);
    350 //     if(!temp)
    351 //     {
    352 //       orxout(internal_error, context::network) << "Server. could not create client with id: " << clientID << endl;
    353 //       return false;
    354 //     }
    355 //     orxout(verbose, context::network) << "Con.Man: creating client id: " << temp->getID() << endl;
    356315
    357316    // synchronise class ids
    358     syncClassid(clientID);
     317    this->syncClassid(clientID);
    359318
    360319    // now synchronise functionIDs
    361320    packet::FunctionIDs *fIDs = new packet::FunctionIDs();
    362321    fIDs->setPeerID(clientID);
    363     bool b = fIDs->send( static_cast<Host*>(this) );
     322    bool b = fIDs->send(static_cast<Host*>(this));
    364323    assert(b);
    365324
    366 //     temp->setSynched(true);
    367325    GamestateManager::setSynched(clientID);
    368326
     327    // Send 'Welcome' packet to client
    369328    orxout(verbose, context::network) << "sending welcome" << endl;
    370329    packet::Welcome *w = new packet::Welcome(clientID);
    371330    w->setPeerID(clientID);
    372     b = w->send( static_cast<Host*>(this) );
     331    b = w->send(static_cast<Host*>(this));
    373332    assert(b);
     333
    374334    (void)b; // avoid compiler warning
    375 //     packet::Gamestate *g = new packet::Gamestate();
    376 //     g->setPeerID(clientID);
    377 //     b = g->collectData(0,packet::GAMESTATE_MODE_SERVER);
    378 //     assert(b);
    379 //     if(!b)
    380 //       return false; //no data for the client
    381 // //     b = g->compressData();
    382 // //     assert(b);
    383 //     b = g->send( static_cast<Host*>(this) );
    384 //     assert(b);
    385335    return true;
    386336  }
    387337
    388   void Server::disconnectClient( uint32_t clientID )
    389   {
    390     ServerConnection::disconnectClient( clientID );
    391     GamestateManager::removePeer( clientID );
    392     // inform all the listeners
    393     // ClientConnectionListener::broadcastClientDisconnected(client->getID()); // this is done in ClientInformation now
     338  /**
     339   * Disconnect a client.
     340   */
     341  void Server::disconnectClient(uint32_t clientID)
     342  {
     343    ServerConnection::disconnectClient(clientID);
     344    GamestateManager::removePeer(clientID);
    394345  }
    395346
     
    403354  {
    404355    // check if the target exists. just ignore the message otherwise
    405     if (!this->isValidTarget(targetID)) // TODO: remove this if an invalid clientIDs don't trigger assertions anymore
     356    if (!this->isValidTarget(targetID))
     357    {
    406358      return;
     359    }
    407360
    408361    // send the message to the target
    409362    packet::Chat* packet = new packet::Chat(message, sourceID, targetID);
    410363    packet->setPeerID(targetID);
    411     packet->send( static_cast<Host*>(this) );
     364    packet->send(static_cast<Host*>(this));
    412365
    413366    // if the target is (or includes) this host as well, call the parent function which passes the message to the listeners
    414367    if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == Host::getPlayerID())
     368    {
    415369      Host::doReceiveChat(message, sourceID, targetID);
     370    }
    416371  }
    417372
     
    431386  bool Server::isValidTarget(unsigned int targetID)
    432387  {
     388    // Broadcast or server ID are okay
    433389    if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == NETWORK_PEER_ID_SERVER)
     390    {
    434391      return true;
    435 
    436     for( uint32_t id : this->clientIDs_ )
    437       if( id == targetID )
     392    }
     393
     394    // IDs in the client list are okay also
     395    for(uint32_t id : this->clientIDs_)
     396    {
     397      if(id == targetID)
     398      {
    438399        return true;
     400      }
     401    }
    439402
    440403    return false;
     
    443406  void Server::syncClassid(unsigned int clientID)
    444407  {
    445     int failures=0;
    446408    packet::ClassID *classid = new packet::ClassID();
    447409    classid->setPeerID(clientID);
    448     while(!classid->send( static_cast<Host*>(this) ) && failures < 10){
     410
     411    int failures = 0;
     412    while(!classid->send(static_cast<Host*>(this)) && failures < 10)\
     413    {
    449414      failures++;
    450415    }
Note: See TracChangeset for help on using the changeset viewer.