Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 7, 2011, 3:11:16 PM (14 years ago)
Author:
landauf
Message:

enhanced chat system. chat related code is now separated into network-side code (located in Host, Client, Server) and client-side code (located in ChatManager).
note that there are now two chat related listeners: NetworkChatListener, which is used to send chat from the network to ChatManager, and ChatListener, which is used to send online and offline chat from ChatManager to the actual chat interfaces (ChatOverlay, ChatInputHandler, …).
the "chat" console command now supports a second argument which is the clientID of the receiver. this allows private messages (or gameplay messages directed to only one specific player).

File:
1 edited

Legend:

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

    r8807 r8829  
    5656#include "packet/Gamestate.h"
    5757#include "packet/Welcome.h"
    58 #include "ChatListener.h"
    5958// #include "ClientInformation.h"
    6059#include "FunctionCallManager.h"
     
    104103  void Server::helper_ConnectToMasterserver()
    105104  {
    106 //     WANDiscovery::getInstance().msc.sendRequest( MSPROTO_GAME_SERVER " " 
     105//     WANDiscovery::getInstance().msc.sendRequest( MSPROTO_GAME_SERVER " "
    107106//       MSPROTO_REGISTER_SERVER );
    108107  }
     
    116115    orxout(verbose, context::network) << "opening server" << endl;
    117116    this->openListener();
    118    
     117
    119118    /* make discoverable on LAN */
    120119    LANDiscoverable::setActivity(true);
     
    123122    WANDiscoverable::setActivity(true);
    124123    /* TODO this needs to be optional, we need a switch from the UI to
    125      * enable/disable this 
     124     * enable/disable this
    126125     */
    127126//     helper_ConnectToMasterserver();
     
    143142    /* tell master server we're closing */
    144143    orxout(internal_info, context::network) << "disconnecting." << endl;
    145     WANDiscoverable::setActivity(false);   
     144    WANDiscoverable::setActivity(false);
    146145    orxout(internal_info, context::network) << "disconnecting done" << endl;
    147146
     
    150149  }
    151150
    152   bool Server::processChat(const std::string& message, unsigned int playerID)
    153   {
    154 //     ClientInformation *temp = ClientInformation::getBegin();
    155     packet::Chat *chat;
    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 //         orxout(internal_warning, context::network) << "could not send Chat message to client ID: " << temp->getID() << endl;
    161 //       temp = temp->next();
    162 //     }
    163 //    orxout(message) << "Player " << playerID << ": " << message << endl;
    164     return true;
    165   }
    166 
    167 
    168151  /* handle incoming data */
    169152  int rephandler( char *addr, ENetEvent *ev )
    170   { 
     153  {
    171154    /* reply to pings */
    172     if( !strncmp( (char *)ev->packet->data, MSPROTO_PING_GAMESERVER, 
     155    if( !strncmp( (char *)ev->packet->data, MSPROTO_PING_GAMESERVER,
    173156      MSPROTO_PING_GAMESERVER_LEN ) )
    174157      //this->msc.sendRequest( MSPROTO_ACK );
    175158      /* NOTE implement this after pollForReply
    176        * reimplementation 
     159       * reimplementation
    177160       */
    178161      return 0;
     
    183166
    184167  void Server::helper_HandleMasterServerRequests()
    185   { 
    186     /* poll the master server for replies and see whether something 
     168  {
     169    /* poll the master server for replies and see whether something
    187170     * has to be done or changed.
    188171     */
     
    202185    // receive and process incoming discovery packets
    203186    LANDiscoverable::update();
    204    
     187
    205188    // receive and process requests from master server
    206189    /* todo */
     
    330313  {
    331314//     static unsigned int newid=1;
    332 // 
     315//
    333316//     orxout(internal_info, context::network) << "Server: adding client" << endl;
    334317//     ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);
     
    375358//     }
    376359  }
    377  
     360
    378361  void Server::processPacket(packet::Packet* packet)
    379362  {
     
    411394//     temp->setSynched(true);
    412395    GamestateManager::setSynched(clientID);
    413    
     396
    414397    orxout(verbose, context::network) << "sending welcome" << endl;
    415398    packet::Welcome *w = new packet::Welcome(clientID);
     
    438421  }
    439422
    440   bool Server::chat(const std::string& message)
    441   {
    442       return this->sendChat(message, Host::getPlayerID());
    443   }
    444 
    445   bool Server::broadcast(const std::string& message)
    446   {
    447       return this->sendChat(message, NETWORK_PEER_ID_BROADCAST);
    448   }
    449 
    450   bool Server::sendChat(const std::string& message, unsigned int clientID)
    451   {
    452 //     ClientInformation *temp = ClientInformation::getBegin();
    453     packet::Chat *chat;
    454 //     while(temp)
    455     {
    456       chat = new packet::Chat(message, clientID);
    457       chat->setPeerID(NETWORK_PEER_ID_BROADCAST);
    458       chat->send( static_cast<Host*>(this) );
    459 //         orxout(internal_warning, context::network) << "could not send Chat message to client ID: " << temp->getID() << endl;
    460 //       temp = temp->next();
    461     }
    462 //    orxout(message) << "Player " << Host::getPlayerID() << ": " << message << endl;
    463     for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it)
    464       it->incomingChat(message, clientID);
    465 
    466     return true;
     423  /**
     424   * @brief Sends a chat message to the given target ID.
     425   * @param message message to be sent
     426   * @param sourceID the ID of the sender
     427   * @param targetID the ID of the receiver
     428   */
     429  void Server::doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID)
     430  {
     431    // check if the target exists. just ignore the message otherwise
     432    if (!this->isValidTarget(targetID)) // TODO: remove this if an invalid clientIDs don't trigger assertions anymore
     433      return;
     434
     435    // send the message to the target
     436    packet::Chat* packet = new packet::Chat(message, sourceID, targetID);
     437    packet->setPeerID(targetID);
     438    packet->send( static_cast<Host*>(this) );
     439
     440    // if the target is (or includes) this host as well, call the parent function which passes the message to the listeners
     441    if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == Host::getPlayerID())
     442      Host::doReceiveChat(message, sourceID, targetID);
     443  }
     444
     445  /**
     446   * @brief Gets called if a packet::Chat packet is received. Forwards the packet to the target
     447   * and calls the parent function if necessary.
     448   */
     449  void Server::doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID)
     450  {
     451      this->doSendChat(message, sourceID, targetID);
     452  }
     453
     454  /**
     455   * @brief Returns true if the target ID is in the list of clients (or if it
     456   * corresponds to the broadcast or the server ID).
     457   */
     458  bool Server::isValidTarget(unsigned int targetID)
     459  {
     460    if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == NETWORK_PEER_ID_SERVER)
     461      return true;
     462
     463    std::vector<uint32_t>::iterator it;
     464    for( it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it )
     465      if( *it == targetID )
     466        return true;
     467
     468    return false;
    467469  }
    468470
Note: See TracChangeset for help on using the changeset viewer.