libevent+zeromq

 

ZeroMq with libevent (cont)

I have just discovered the reason why my libevent program did not receive any more ZeroMq messages after the first one. It seems that, after creating the ZeroMq socket, you have to read until you get a EAGAIN error or you will not set the socket events in the right state. If you don’t do so, the idea libevent and ZeroMq have about that socket will not match, so your program will not receive/send anything.

From this thread:

The socket is initially considered ready for reading & writing. Given that ZMQ_FD is edge triggered, you won’t get next event unless you fail to send/recv a message.

So, when you create a socket, you have to do something like this:

zsocket  = new zmq::socket_t (*ZMqLink::m_pZContext, ZMQ_XREQ);

/* read until EAGAIN */
while (TRUE)
{
    zmq::message_t  msg;
    bool more = zsocket->recv(&msg, ZMQ_NOBLOCK);
    if (!more) break;
}

/* now we can get the zsocket fd */
int      fd        = (-1);
size_t   fd_size   = sizeof(fd);
zocket->getsockopt(ZMQ_FD, &fd, &fd_size);

/* ... and register it with libevent for EV_READ */

原文地址:http://inercia.tumblr.com/post/3600122381/zeromq-with-libevent-cont

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值