简体   繁体   English

轮询命令以在Boost Asio中发送共享队列

[英]Poll command to send in shared queue in boost asio

I'm making a server using Boost asio and I'm facing a problem. 我正在使用Boost asio制作服务器,但遇到了问题。 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... 为此,我为每个连接的客户端实例化一个新的连接会话,在该客户端中,我通过调用async_write的回调与调用async_read等的回调来调用async_read。

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. 有一个服务器的GUI,我可以在其中单击要发送的命令,然后将这些命令放在共享队列中。 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) ? 问题是,使用会话中的“调用自身的回调”方案,如何在不影响性能的情况下检查队列中是否有针对我的命令(我的第一个解决方案是在async_write操作上放置一个截止时间计时器,我可以每X秒检查一次)?

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. 通常,您将排队并安排一个写循环(使用async_回调样式),直到耗尽为止。 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 ). 例如: http//www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/example/cpp03/chat/chat_client.cpp (查找chat_message_queuewrite_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? ¹隐式链- 为什么在使用boost :: asio时每个连接都需要链?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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