与升压一起使用ZeroMQ :: ASIO [英] Using ZeroMQ together with Boost::ASIO

查看:606
本文介绍了与升压一起使用ZeroMQ :: ASIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ZeroMQ一些消息C ++应用程序。但它也有提供一个基于AJAX /彗星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?

注:我可以解决,通过使用多线程 - 但它是将与SCGI的流量非常低量运行该程序只有一点点的单核/ CPU中,所以多线程是一种资源的浪费?

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...

推荐答案

这里阅读文档后和这里,特别是这一段

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.

我想你可以使用 null_buffers zmq_pollitem_t 并推迟事件循环到 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插口之一的处理程序被触发,你就必须处理,我认为在事件发生前做更多的工作。未编译伪code是低于

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 ::绑定成员函数就足够了。

note you don't have to use a lambda here, boost::bind to a member function would be sufficient.

这篇关于与升压一起使用ZeroMQ :: ASIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆