Metiu Metiu - 2 years ago 87
Linux Question

How to integrate a ZeroMQ socket into a glib main loop?

I wanted to add a ZeroMQ socket to a glib program.

The pitty is, a zmq socket is not

poll()
-able, and they give their implementation, which overloads the old
poll()
func. How could I integrate that into the main loop efficiently? I tried using their poll (
zmq_poll()
) instead of the default one, but there's no good way of giving it the zmq socket, besides from making it a
global
.

Defining a new
GSource
works, but it can get high CPU usage ( by setting
timeout = 0
) or arbitrary poll timeouts ( e.g. setting
timeout = 100
to be polled at least every 100 ms ), which is not really efficient, since there is the possibility of polling.

Answer Source

I found that newer zmq libraries support the ZMQ_FD getsockopt() parameter, which gives you back a unix fd which you can poll(). The only caveat is that you can't just poll() it to know if you can recv() or send() from / to it, but you need to use the ZMQ_EVENTS getsockopt() parameter to get back the real fd status.

It seems to be working quite well in glib.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download