[zeromq-dev] missing events ZMQ_FD / ZMQ_EVENTS

Paul Colomiets paul at colomiets.name
Fri Apr 27 21:10:32 CEST 2012


Hi Gerhard,

On Fri, Apr 27, 2012 at 2:10 PM, Gerhard Lipp <gelipp at googlemail.com> wrote:
> Ok, so i must always check if there are more events to process before
> returning from the io handler (frankly I don't understand the
> explanation). A short test still shows the lock explained earlier:

Try the following:

local zmq = require'zmq'
local ev = require'ev'
local c = zmq.init(1)
local xreq = c:socket(zmq.XREQ)
xreq:bind('tcp://127.0.0.1:13333')
local xrep = c:socket(zmq.XREP)
xrep:bind('tcp://127.0.0.1:13334')

local is_readable =
  function(sock)
     local events = sock:getopt(zmq.EVENTS)
     return events == zmq.POLLIN or events == (zmq.POLLIN + zmq.POLLOUT)
  end

local forward_io =
  function(src,dst)
     return ev.IO.new(
        function(loop,io) -- called whenever src:getopt(zmq.FD) becomes readable
            while is_readable(src) or is_readable(dst) do
               if is_readable(src) do
                  repeat
                     local data = assert(src:recv(zmq.NOBLOCK))
                     local more = src:getopt(zmq.RCVMORE) > 0
                     dst:send(data,more and zmq.SNDMORE or 0)
                  until not more
               end
               if is_readable(dst) do
                  repeat
                     local data = assert(dst:recv(zmq.NOBLOCK))
                     local more = dst:getopt(zmq.RCVMORE) > 0
                     src:send(data,more and zmq.SNDMORE or 0)
                  until not more
               end
            end
        end,
        src:getopt(zmq.FD),
        ev.READ
     )
  end
local xrep_io = forward_io(xrep,xreq)
local xreq_io = forward_io(xreq,xrep)
xreq_io:start(ev.Loop.default)
xrep_io:start(ev.Loop.default)
ev.Loop.default:loop()

If this works. You can optimize (and clarify) it more.

-- 
Paul



More information about the zeromq-dev mailing list