Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 22, 2018, 4:12:23 PM (6 years ago)
Author:
mdedial
Message:

WIP 22.03.18: Begin documenting server-side code

File:
1 edited

Legend:

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

    r11103 r11829  
    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;
     168        timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD;
    172169        updateGamestate();
    173170      }
    174 //       sendPackets(); // flush the enet queue
    175171    }
    176172  }
     
    182178
    183179  /**
    184    * @brief: returns ping time to client in milliseconds
     180   * Return ping time to client in milliseconds.
    185181   */
    186182  unsigned int Server::getRTT(unsigned int clientID)
    187183  {
    188 //     assert(ClientInformation::findClient(clientID));
    189 //     return ClientInformation::findClient(clientID)->getRTT();
    190     // TODO: reimplement
     184    // TODO: Implement
    191185    return 0;
    192186  }
    193187
     188  /**
     189   * Print ping time to client in milliseconds.
     190   */
    194191  void Server::printRTT()
    195192  {
    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)
     193    // TODO: Implement
     194  }
     195
     196  /**
     197   * Return packet loss ratio to client (scales from 0 to 1).
    202198   */
    203199  float Server::getPacketLoss(unsigned int clientID)
    204200  {
    205 //     assert(ClientInformation::findClient(clientID));
    206 //     return ClientInformation::findClient(clientID)->getPacketLoss();
     201    // TODO: Implement
    207202    return 0.;
    208203  }
    209204
    210205  /**
    211   * takes a new snapshot of the gamestate and sends it to the clients
    212   */
     206   * Take a new snapshot of the gamestate and send it to the clients.
     207   */
    213208  void Server::updateGamestate()
    214209  {
    215     if( this->clientIDs_.size()==0 )
    216       //no client connected
     210    if(this->clientIDs_.size() == 0)
     211    {
     212      // no client connected
    217213      return;
     214    }
    218215    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)
    222216    sendGameStates();
    223217    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   */
     218  }
     219
     220  /**
     221   * Send the current gamestate to all peers.
     222   */
    231223  bool Server::sendGameStates()
    232224  {
    233225    std::vector<packet::Gamestate*> gamestates = GamestateManager::getGamestates();
    234     for( packet::Gamestate* gamestate : gamestates )
     226    for(packet::Gamestate* gamestate : gamestates)
    235227    {
    236228      gamestate->send(static_cast<Host*>(this));
     
    240232
    241233
     234  /**
     235   * Send 'DeleteObjects' packet
     236   */
    242237  bool Server::sendObjectDeletes()
    243238  {
    244 //     ClientInformation *temp = ClientInformation::getBegin();
    245 //     if( temp == nullptr )
    246       //no client connected
    247     if( this->clientIDs_.size()==0 )
     239    // no client connected
     240    if(this->clientIDs_.size() == 0)
     241    {
    248242      return true;
     243    }
     244
    249245    packet::DeleteObjects *del = new packet::DeleteObjects();
    250246    if(!del->fetchIDs())
     
    253249      return true;  //everything ok (no deletes this tick)
    254250    }
    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);
     251
    266252    del->setPeerID(NETWORK_PEER_ID_BROADCAST);
    267     if ( !del->send( static_cast<Host*>(this) ) )
     253    if (!del->send( static_cast<Host*>(this)))
     254    {
    268255      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;
     256    }
     257
     258    // TODO: Possible memory leak?
     259    // del is allocated but only deleted if fetchIDs() returns false
     260
    273261    return true;
    274262  }
    275263
    276 
     264  /**
     265   * Add a new peer to the server.
     266   */
    277267  void Server::addPeer(uint32_t peerID)
    278268  {
    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 
    290269    // inform all the listeners
    291270    this->clientIDs_.push_back(peerID);
     
    296275    GamestateManager::addPeer(peerID);
    297276
    298 //     ++newid;
    299 
    300277    orxout(internal_info, context::network) << "Server: added client id: " << peerID << endl;
    301278
    302279    createClient(peerID);
    303 }
    304 
     280  }
     281
     282  /**
     283   * Remove a peer from the server.
     284   */
    305285  void Server::removePeer(uint32_t peerID)
    306286  {
    307287    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 
     288
     289    // erase the peer from the list
     290    std::vector<uint32_t>::iterator it;
     291    for(it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it)
     292    {
     293      if(*it == peerID)
     294      {
     295        this->clientIDs_.erase(it);
     296        break;
     297      }
     298    }
     299
     300    // TODO: What happens if no peer with this ID is found?
     301    // Should probably at least log
     302
     303    WANDiscoverable::updateClientNumber(this->clientIDs_.size());
     304    LANDiscoverable::updateClientNumber(this->clientIDs_.size());
     305
     306    // Send 'clientDisconnected' message
     307    ClientConnectionListener::broadcastClientDisconnected(peerID);
     308
     309    GamestateManager::removePeer(peerID);
     310  }
     311
     312  /**
     313   * Process an incoming packet.
     314   */
    333315  void Server::processPacket(packet::Packet* packet)
    334316  {
    335     if( packet->isReliable() )
    336     {
    337       if( this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
     317    if(packet->isReliable())
     318    {
     319      if(this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID())
     320      {
    338321        packet->process(static_cast<Host*>(this));
     322      }
    339323      else
     324      {
    340325        this->packetQueue_.push_back(packet);
     326      }
    341327    }
    342328    else
     329    {
    343330      packet->process(static_cast<Host*>(this));
    344   }
    345 
    346 
     331    }
     332  }
     333
     334  /**
     335   * Create a client.
     336   */
    347337  bool Server::createClient(int clientID)
    348338  {
    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;
    356339
    357340    // synchronise class ids
     
    361344    packet::FunctionIDs *fIDs = new packet::FunctionIDs();
    362345    fIDs->setPeerID(clientID);
    363     bool b = fIDs->send( static_cast<Host*>(this) );
     346    bool b = fIDs->send(static_cast<Host*>(this));
    364347    assert(b);
    365 
    366 //     temp->setSynched(true);
     348    // TODO: assert probably isn't the way to go here, as a packet which fails to send will crash the game...
     349
    367350    GamestateManager::setSynched(clientID);
    368351
     352    // Send 'Welcome' packet to client
    369353    orxout(verbose, context::network) << "sending welcome" << endl;
    370354    packet::Welcome *w = new packet::Welcome(clientID);
    371355    w->setPeerID(clientID);
    372     b = w->send( static_cast<Host*>(this) );
     356    b = w->send(static_cast<Host*>(this));
    373357    assert(b);
     358    // TODO: assert probably isn't the way to go here, as a packet which fails to send will crash the game...
     359
    374360    (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);
    385361    return true;
    386362  }
    387363
    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
     364  /**
     365   * Disconnect a client.
     366   */
     367  void Server::disconnectClient(uint32_t clientID)
     368  {
     369    ServerConnection::disconnectClient(clientID);
     370    GamestateManager::removePeer(clientID);
    394371  }
    395372
     
    404381    // check if the target exists. just ignore the message otherwise
    405382    if (!this->isValidTarget(targetID)) // TODO: remove this if an invalid clientIDs don't trigger assertions anymore
     383    {
    406384      return;
     385    }
    407386
    408387    // send the message to the target
    409388    packet::Chat* packet = new packet::Chat(message, sourceID, targetID);
    410389    packet->setPeerID(targetID);
    411     packet->send( static_cast<Host*>(this) );
     390    packet->send(static_cast<Host*>(this));
    412391
    413392    // if the target is (or includes) this host as well, call the parent function which passes the message to the listeners
    414393    if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == Host::getPlayerID())
     394    {
    415395      Host::doReceiveChat(message, sourceID, targetID);
     396    }
    416397  }
    417398
     
    431412  bool Server::isValidTarget(unsigned int targetID)
    432413  {
     414    // Broadcast or server ID are okay
    433415    if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == NETWORK_PEER_ID_SERVER)
     416    {
    434417      return true;
    435 
    436     for( uint32_t id : this->clientIDs_ )
    437       if( id == targetID )
     418    }
     419
     420    // IDs in the client list are okay also
     421    for(uint32_t id : this->clientIDs_)
     422    {
     423      if(id == targetID)
     424      {
    438425        return true;
     426      }
     427    }
    439428
    440429    return false;
     
    443432  void Server::syncClassid(unsigned int clientID)
    444433  {
    445     int failures=0;
    446434    packet::ClassID *classid = new packet::ClassID();
    447435    classid->setPeerID(clientID);
    448     while(!classid->send( static_cast<Host*>(this) ) && failures < 10){
     436    // TODO: Better to do this with a for loop
     437    int failures = 0;
     438    while(!classid->send(static_cast<Host*>(this)) && failures < 10)\
     439    {
    449440      failures++;
    450441    }
    451442    assert(failures<10);
     443    // TODO: assert probably isn't the way to go here, as 10 failed packets will crash the game...
    452444    orxout(verbose, context::network) << "syncClassid:\tall synchClassID packets have been sent" << endl;
    453445  }
Note: See TracChangeset for help on using the changeset viewer.