[Asio] How to implement a server and client that replies to all clients but one
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_caststd::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
On 18/03/2018 06:57, Gonzalo Garramuño wrote:
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.
When your server receives the change, it knows which client it was received from (since it's received on that client's connection). When you loop through the clients to forward the changes on, simply skip that client instead of sending. (Or send it a confirmation or something instead.) Depending on how your code is structured, this might require you to pass an identifier or pointer from the receiving connection down to the code that forwards the changes, but it should be simple enough.
participants (2)
-
Gavin Lambert
-
Gonzalo Garramuño