問題描述
我有一個使用 ZeroMQ 進行消息傳遞的 C++ 應用程序.但它還必須為基于 AJAX/Comet 的 Web 服務提供 SGCI 連接.
I've got a C++ application that is using ZeroMQ for some messaging. But it also has to provide a SGCI connection for an AJAX / Comet based web service.
為此,我需要一個普通的 TCP 套接字.我可以通過普通的 Posix 套接字做到這一點,但為了保持跨平臺的便攜性并使我的生活更輕松(我希望......)我正在考慮使用 Boost::ASIO.
For this I need a normal TCP socket. I could do that by normal Posix sockets, but to stay cross platform portable and make my life easier (I hope...) I was thinking of using Boost::ASIO.
但現在我有 ZMQ 的沖突,想要使用它自己的 zmq_poll()
和 ASIO 它是 io_service.run()
...
But now I have the clash of ZMQ wanting to use it's own zmq_poll()
and ASIO it's io_service.run()
...
有沒有辦法讓 ASIO 與 0MQ zmq_poll()
一起工作?
Is there a way to get ASIO to work together with the 0MQ zmq_poll()
?
或者是否有其他推薦的方法來實現這樣的設置?
Or is there an other recommended way to achieve such a setup?
注意:我可以通過使用多線程來解決這個問題——但它只是一個小小的單核/CPU 盒子,它可以以非常低的 SCGI 流量運行該程序,所以多線程會浪費資源......
Note: I could solve that by using multiple threads - but it's only a little single core / CPU box that'll run that program with a very low amount of SCGI traffic, so multithreading would be a waste of resources...
推薦答案
閱讀文檔后 這里和這里,特別是這一段
After reading the documentation here and here, specifically this paragraph
ZMQ_FD:檢索與套接字關聯的文件描述符 ZMQ_FD選項應檢索與關聯的文件描述符指定的套接字.返回的文件描述符可用于將套接字集成到現有的事件循環中;?MQ 庫應以邊緣觸發的方式向套接字上的任何未決事件發出信號通過使文件描述符準備好讀取來實現時尚.
ZMQ_FD: Retrieve file descriptor associated with the socket The ZMQ_FD option shall retrieve the file descriptor associated with the specified socket. The returned file descriptor can be used to integrate the socket into an existing event loop; the ?MQ library shall signal any pending events on the socket in an edge-triggered fashion by making the file descriptor become ready for reading.
我認為您可以為每個 zmq_pollitem_t
使用 null_buffers
并將事件循環推遲到 io_service
,完全繞過 zmq_poll()
.然而,上述文檔中似乎有一些警告,特別是
I think you can use null_buffers
for every zmq_pollitem_t
and defer the event loop to an io_service
, completely bypassing zmq_poll()
altogether. There appear to be some caveats in the aforementioned documentation however, notably
從返回的文件描述符中讀取的能力不必然表明消息可供讀取,或可以寫入,底層套接字;應用程序必須檢索實際事件狀態,隨后檢索 ZMQ_EVENTS選項.
The ability to read from the returned file descriptor does not necessarily indicate that messages are available to be read from, or can be written to, the underlying socket; applications must retrieve the actual event state with a subsequent retrieval of the ZMQ_EVENTS option.
因此,當您的一個 zmq 套接字的處理程序被觸發時,您必須在處理我認為的事件之前做更多的工作.未編譯的偽代碼如下
So when the handler for one of your zmq sockets is fired, you'll have to do a little more work before handling the event I think. Uncompiled pseudo-code is below
const int fd = getZmqDescriptorSomehow();
boost::asio::posix::stream_descriptor socket( _io_service, fd );
socket->async_read_some(
boost::asio::null_buffers(),
[=](const boost::system::error_code& error)
{
if (!error) {
// handle data ready to be read
}
}
);
注意你不必在這里使用 lambda,boost::bind
到成員函數就足夠了.
note you don't have to use a lambda here, boost::bind
to a member function would be sufficient.
這篇關于將 ZeroMQ 與 Boost::ASIO 一起使用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!