Reputation: 1864
I'm confused by a warning in the api of zmq_poll: "The zmq_send() function will clear all pending events on a socket. Thus, if you use zmq_poll() to monitor input on a socket, use it before output as well, and process all events after each zmq_poll() call."
I don't understand what that means. Since events are level-triggered. If I call zmq_send()
and then zmq_poll()
, any pending messages in the socket's buffer should trigger the zmq_poll
again immediately. Why one needs to "use it (zmq_poll
) before output as well" or "process all events after each zmq_poll() call"?
Upvotes: 1
Views: 2018
Reputation: 32056
I see your point, the documentation is confusing. Here's a simple test in Java using a client-side DEALER
socket with a poller (from asyncsrv) . The server sends 3 messages to the client. The client polls and outputs each message it receives. I've added send()
in the client to test your theory. Assuming send()
clears the poller, we expect the client to output receipt of only a single message:
Server
public static void main(String[] args) {
Context context = ZMQ.context(1);
ZMQ.Socket server = context.socket(ZMQ.ROUTER);
server.bind("tcp://*:5555");
server.sendMore("clientId");
server.send("msg1");
server.sendMore("clientId");
server.send("msg2");
server.sendMore("clientId");
server.send("msg3");
}
Client
public void run() {
socket = context.socket(ZMQ.DEALER);
socket.setIdentity("clientId".getBytes());
socket.connect("tcp://localhost:5555");
ZMQ.Poller poller = new ZMQ.Poller(1);
poller.register(socket, ZMQ.Poller.POLLIN);
while (true) {
poller.poll();
if (poller.pollin(0)) {
String msg = socket.recvStr(0);
System.out.println("Client got msg: " + msg);
socket.send("whatever", 0);
}
}
}
outputs...
Client got msg: msg1
Client got msg: msg2
Client got msg: msg3
Based on the results, doing send()
does not clear the poller for socket
, and it should be obvious why. We configured the poller with POLLIN
, meaning the poller listens for inbound messages to socket
. When doing socket.send()
, it creates outbound messages, on which the poller is not listening.
Hope it helps...
Upvotes: 1