Boost logo

Boost Users :

Subject: [Boost-users] [Asio] How to implement a server and client that replies to all clients but one
From: Gonzalo Garramuño (ggarra13_at_[hidden])
Date: 2018-03-17 17:57:23


Hi.  I want to have a variation on the chat server/client model in that
there's a language with instructions being sent.

Both the server and the client need to parse this language.  When the
server changes something, it sends the change in the language to all
clients.  When the client changes something it sends the change to the
server which in turn resends it to all clients but, here's the catch,
not to the one that sent the change in the first place.

I managed the removal of the client by doing:

    ParserList::const_iterator i = clients.begin();
    ParserList::const_iterator e = clients.end();

    for ( ; i != e; ++i )
    {
        try
        {

             if ( !(*i)->socket_.is_open() ) continue;
            std::string p = boost::lexical_cast<std::string>(
(*i)->socket_.remote_endpoint() );

            if ( p == id )
                continue;

            (*i)->deliver( cmd );
        }
        catch( const std::exception& e )
        {
            LOG_CONN( "Parser::write " << e.what() );
        }
        catch( ... )
        {
            LOG_CONN( "Parser::write unhandled exception" );
        }
    }
}

I managed to create a working version of the above also by modifying the
asio async example using multiple inheritance for the parser  (which
both the client and the tcp_session inherit). However I am unhappy with
this.  I ask here to see if someone can suggest a more simple approach
to this.

-- 
Gonzalo Garramuño

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net