简体   繁体   中英

Poll command to send in shared queue in boost asio

I'm making a server using Boost asio and I'm facing a problem. My server should be able to receive data from multiple connected clients and to send commands to specific client too.

To do this i'm instantiating a new connection session for each client that connect in which I call async_read with a callback that call async_write with a callback that call async_read etc...

The problem that I'm facing is the following:

A have a GUI for the server in which I can click on commands to send, those commands are then put in a shared queue. As the queue is shared, each connection session can poll to see if there is a command to be send to their connected client.

The problem is, with that "callback that call itself" scheme in the session, how can I check when there is a command for me in the queue without ruining the performance (my first solution was to put a deadline timer on the async_write operation so that I can check each X seconds) ?

Any idea ?

The point is that pro-actor-based event handling is fundamentally opposite to polling a queue.

Usually you will make a queue and schedule a write-loop (using the async_ callback style) until that is depleted. An example of that is here: http://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/example/cpp03/chat/chat_client.cpp (look for chat_message_queue and write_in_progress ).

Basically, you inject an event (namely "message queued") and let the async call chain drain the queue. The benefit over queueing an action for each message is that the async call chain does implicit ordering/synchronization¹

¹ implicit strands - Why do I need strand per connection when using boost::asio?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM