Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 28, 2010, 4:46:42 PM (14 years ago)
Author:
scheusso
Message:

again some structural changes in network to increase modularity/encapsulation
precondition for fixing client-disconnect bug

File:
1 edited

Legend:

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

    r7801 r7823  
    5757#include "packet/Welcome.h"
    5858#include "ChatListener.h"
    59 #include "ClientInformation.h"
     59// #include "ClientInformation.h"
    6060#include "FunctionCallManager.h"
    6161#include "GamestateManager.h"
     
    152152  bool Server::processChat(const std::string& message, unsigned int playerID)
    153153  {
    154     ClientInformation *temp = ClientInformation::getBegin();
     154//     ClientInformation *temp = ClientInformation::getBegin();
    155155    packet::Chat *chat;
    156     while(temp){
    157       chat = new packet::Chat(message, playerID);
    158       chat->setPeerID(temp->getID());
    159       if(!chat->send( static_cast<Host*>(this) ))
    160         COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
    161       temp = temp->next();
    162     }
     156//     while(temp){
     157    chat = new packet::Chat(message, playerID);
     158    chat->setPeerID(NETWORK_PEER_ID_BROADCAST);
     159    chat->send( static_cast<Host*>(this) );
     160//         COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
     161//       temp = temp->next();
     162//     }
    163163//    COUT(1) << "Player " << playerID << ": " << message << std::endl;
    164164    return true;
     
    207207    //helper_HandleMasterServerRequests();
    208208
    209     if ( ClientInformation::hasClients() )
     209    if ( GamestateManager::hasPeers() )
    210210    {
    211211      // process incoming gamestates
     
    237237  unsigned int Server::getRTT(unsigned int clientID)
    238238  {
    239     assert(ClientInformation::findClient(clientID));
    240     return ClientInformation::findClient(clientID)->getRTT();
     239//     assert(ClientInformation::findClient(clientID));
     240//     return ClientInformation::findClient(clientID)->getRTT();
     241    // TODO: reimplement
     242    return 0;
    241243  }
    242244
    243245  void Server::printRTT()
    244246  {
    245     for( ClientInformation* temp=ClientInformation::getBegin(); temp!=0; temp=temp->next() )
    246       COUT(0) << "Round trip time to client with ID: " << temp->getID() << " is " << temp->getRTT() << " ms" << endl;
     247//     for( ClientInformation* temp=ClientInformation::getBegin(); temp!=0; temp=temp->next() )
     248//       COUT(0) << "Round trip time to client with ID: " << temp->getID() << " is " << temp->getRTT() << " ms" << endl;
    247249  }
    248250
     
    252254  double Server::getPacketLoss(unsigned int clientID)
    253255  {
    254     assert(ClientInformation::findClient(clientID));
    255     return ClientInformation::findClient(clientID)->getPacketLoss();
     256//     assert(ClientInformation::findClient(clientID));
     257//     return ClientInformation::findClient(clientID)->getPacketLoss();
     258    return 0.;
    256259  }
    257260
     
    261264  void Server::updateGamestate()
    262265  {
    263     if( ClientInformation::getBegin()==NULL )
     266    if( this->clientIDs_.size()==0 )
    264267      //no client connected
    265268      return;
     
    291294  bool Server::sendObjectDeletes()
    292295  {
    293     ClientInformation *temp = ClientInformation::getBegin();
    294     if( temp == NULL )
     296//     ClientInformation *temp = ClientInformation::getBegin();
     297//     if( temp == NULL )
    295298      //no client connected
     299    if( this->clientIDs_.size()==0 )
    296300      return true;
    297301    packet::DeleteObjects *del = new packet::DeleteObjects();
     
    302306    }
    303307//     COUT(3) << "sending DeleteObjects" << std::endl;
    304     while(temp != NULL){
    305       if( !(temp->getSynched()) )
    306       {
    307         COUT(5) << "Server: not sending gamestate" << std::endl;
    308         temp=temp->next();
    309         continue;
    310       }
    311       int cid = temp->getID(); //get client id
    312       packet::DeleteObjects *cd = new packet::DeleteObjects(*del);
    313       assert(cd);
    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;
    317       temp=temp->next();
     308//     while(temp != NULL){
     309//       if( !(temp->getSynched()) )
     310//       {
     311//         COUT(5) << "Server: not sending gamestate" << std::endl;
     312//         temp=temp->next();
     313//         continue;
     314//       }
     315//       int cid = temp->getID(); //get client id
     316//       packet::DeleteObjects *cd = new packet::DeleteObjects(*del);
     317//       assert(cd);
     318    del->setPeerID(NETWORK_PEER_ID_BROADCAST);
     319    if ( !del->send( static_cast<Host*>(this) ) )
     320      COUT(3) << "Server: could not broadcast deleteObjects packet" << std::endl;
     321//       temp=temp->next();
    318322      // gs gets automatically deleted by enet callback
    319     }
    320     delete del;
     323//     }
     324//     delete del;
    321325    return true;
    322326  }
    323327
    324328
    325   void Server::addPeer(ENetEvent *event)
    326   {
    327     static unsigned int newid=1;
    328 
    329     COUT(2) << "Server: adding client" << std::endl;
    330     ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
    331     if(!temp)
    332     {
    333       COUT(2) << "Server: could not add client" << std::endl;
    334     }
    335     temp->setID(newid);
    336     temp->setPeer(event->peer);
     329  void Server::addPeer(uint32_t peerID)
     330  {
     331//     static unsigned int newid=1;
     332//
     333//     COUT(2) << "Server: adding client" << std::endl;
     334//     ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
     335//     if(!temp)
     336//     {
     337//       COUT(2) << "Server: could not add client" << std::endl;
     338//     }
     339//     temp->setID(newid);
     340//     temp->setPeer(event->peer);
    337341
    338342    // inform all the listeners
    339     ClientConnectionListener::broadcastClientConnected(newid);
    340     GamestateManager::addPeer(newid);
    341 
    342     ++newid;
    343 
    344     COUT(3) << "Server: added client id: " << temp->getID() << std::endl;
    345     createClient(temp->getID());
     343    this->clientIDs_.push_back(peerID);
     344    ClientConnectionListener::broadcastClientConnected(peerID);
     345    GamestateManager::addPeer(peerID);
     346
     347//     ++newid;
     348
     349    COUT(3) << "Server: added client id: " << peerID << std::endl;
     350    createClient(peerID);
    346351}
    347352
    348   void Server::removePeer(ENetEvent *event)
     353  void Server::removePeer(uint32_t peerID)
    349354  {
    350355    COUT(4) << "removing client from list" << std::endl;
    351     ClientInformation *client = ClientInformation::findClient(&event->peer->address);
    352     if(!client)
    353       return;
    354     else
    355     {
    356       GamestateManager::removePeer(client->getID());
     356//     ClientInformation *client = ClientInformation::findClient(&event->peer->address);
     357//     if(!client)
     358//       return;
     359//     else
     360//     {
     361  std::vector<uint32_t>::iterator it;
     362  for( it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it )
     363  {
     364    if( *it == peerID )
     365    {
     366      this->clientIDs_.erase(it);
     367      break;
     368    }
     369  }
     370  ClientConnectionListener::broadcastClientDisconnected(peerID);
     371  GamestateManager::removePeer(peerID);
    357372      //ServerConnection::disconnectClient( client );
    358373      //ClientConnectionListener::broadcastClientDisconnected( client->getID() ); //this is done in ClientInformation now
    359       delete client;
    360     }
     374//       delete client;
     375//     }
    361376  }
    362377 
     
    377392  bool Server::createClient(int clientID)
    378393  {
    379     ClientInformation *temp = ClientInformation::findClient(clientID);
    380     if(!temp)
    381     {
    382       COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl;
    383       return false;
    384     }
    385     COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
     394//     ClientInformation *temp = ClientInformation::findClient(clientID);
     395//     if(!temp)
     396//     {
     397//       COUT(2) << "Server. could not create client with id: " << clientID << std::endl;
     398//       return false;
     399//     }
     400//     COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
    386401
    387402    // synchronise class ids
    388     syncClassid(temp->getID());
     403    syncClassid(clientID);
    389404
    390405    // now synchronise functionIDs
     
    394409    assert(b);
    395410
    396     temp->setSynched(true);
     411//     temp->setSynched(true);
    397412    GamestateManager::setSynched(clientID);
    398413   
    399414    COUT(4) << "sending welcome" << std::endl;
    400     packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID());
    401     w->setPeerID(temp->getID());
     415    packet::Welcome *w = new packet::Welcome(clientID, OBJECTID_UNKNOWN);
     416    w->setPeerID(clientID);
    402417    b = w->send( static_cast<Host*>(this) );
    403418    assert(b);
    404419    packet::Gamestate *g = new packet::Gamestate();
    405     g->setPeerID(temp->getID());
     420    g->setPeerID(clientID);
    406421    b = g->collectData(0,packet::GAMESTATE_MODE_SERVER);
    407422    assert(b);
     
    415430  }
    416431
    417   void Server::disconnectClient( ClientInformation *client )
    418   {
    419     ServerConnection::disconnectClient( client );
    420     GamestateManager::removePeer(client->getID());
     432  void Server::disconnectClient( uint32_t clientID )
     433  {
     434    ServerConnection::disconnectClient( clientID );
     435    GamestateManager::removePeer( clientID );
    421436    // inform all the listeners
    422437    // ClientConnectionListener::broadcastClientDisconnected(client->getID()); // this is done in ClientInformation now
     
    430445  bool Server::broadcast(const std::string& message)
    431446  {
    432       return this->sendChat(message, CLIENTID_UNKNOWN);
     447      return this->sendChat(message, NETWORK_PEER_ID_BROADCAST);
    433448  }
    434449
    435450  bool Server::sendChat(const std::string& message, unsigned int clientID)
    436451  {
    437     ClientInformation *temp = ClientInformation::getBegin();
     452//     ClientInformation *temp = ClientInformation::getBegin();
    438453    packet::Chat *chat;
    439     while(temp)
     454//     while(temp)
    440455    {
    441456      chat = new packet::Chat(message, clientID);
    442       chat->setPeerID(temp->getID());
    443       if(!chat->send( static_cast<Host*>(this) ))
    444         COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
    445       temp = temp->next();
     457      chat->setPeerID(NETWORK_PEER_ID_BROADCAST);
     458      chat->send( static_cast<Host*>(this) );
     459//         COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
     460//       temp = temp->next();
    446461    }
    447462//    COUT(1) << "Player " << Host::getPlayerID() << ": " << message << std::endl;
Note: See TracChangeset for help on using the changeset viewer.