Verify the value of the "void *s" passed to the monitor function.

This formerly unused parameter actually represents the socket
on which the event was received. As such, we should check that
its value makes sense: it must be either "rep" or "req", and in
the case of some kinds of events, it must be specifically one
or the other.

After this change, "s" is no longer unused.
This commit is contained in:
Arthur O'Dwyer 2012-08-27 16:06:51 -07:00
parent 3b984d40e9
commit c29aef4dd8

View File

@ -28,29 +28,38 @@
static int events; static int events;
void socket_monitor (void *s, int event_, zmq_event_data_t *data_) typedef void *ZmqSocket;
ZmqSocket rep, req;
void socket_monitor (ZmqSocket s, int event_, zmq_event_data_t *data_)
{ {
assert(s == rep || s == req);
const char *addr = "tcp://127.0.0.1:5560"; const char *addr = "tcp://127.0.0.1:5560";
// Only some of the exceptional events could fire // Only some of the exceptional events could fire
switch (event_) { switch (event_) {
// listener specific // listener specific
case ZMQ_EVENT_LISTENING: case ZMQ_EVENT_LISTENING:
assert (s == rep);
assert (data_->listening.fd > 0); assert (data_->listening.fd > 0);
assert (!strcmp (data_->listening.addr, addr)); assert (!strcmp (data_->listening.addr, addr));
events |= ZMQ_EVENT_LISTENING; events |= ZMQ_EVENT_LISTENING;
break; break;
case ZMQ_EVENT_ACCEPTED: case ZMQ_EVENT_ACCEPTED:
assert (s == rep);
assert (data_->accepted.fd > 0); assert (data_->accepted.fd > 0);
assert (!strcmp (data_->accepted.addr, addr)); assert (!strcmp (data_->accepted.addr, addr));
events |= ZMQ_EVENT_ACCEPTED; events |= ZMQ_EVENT_ACCEPTED;
break; break;
// connecter specific // connecter specific
case ZMQ_EVENT_CONNECTED: case ZMQ_EVENT_CONNECTED:
assert (s == req);
assert (data_->connected.fd > 0); assert (data_->connected.fd > 0);
assert (!strcmp (data_->connected.addr, addr)); assert (!strcmp (data_->connected.addr, addr));
events |= ZMQ_EVENT_CONNECTED; events |= ZMQ_EVENT_CONNECTED;
break; break;
case ZMQ_EVENT_CONNECT_DELAYED: case ZMQ_EVENT_CONNECT_DELAYED:
assert (s == req);
assert (data_->connect_delayed.err != 0); assert (data_->connect_delayed.err != 0);
assert (!strcmp (data_->connect_delayed.addr, addr)); assert (!strcmp (data_->connect_delayed.addr, addr));
events |= ZMQ_EVENT_CONNECT_DELAYED; events |= ZMQ_EVENT_CONNECT_DELAYED;
@ -77,7 +86,7 @@ void socket_monitor (void *s, int event_, zmq_event_data_t *data_)
} }
} }
int main (int argc, char *argv []) int main (void)
{ {
int rc; int rc;
@ -87,13 +96,13 @@ int main (int argc, char *argv [])
// set socket monitor // set socket monitor
rc = zmq_ctx_set_monitor (ctx, socket_monitor); rc = zmq_ctx_set_monitor (ctx, socket_monitor);
assert (rc == 0); assert (rc == 0);
void *rep = zmq_socket (ctx, ZMQ_REP); rep = zmq_socket (ctx, ZMQ_REP);
assert (rep); assert (rep);
rc = zmq_bind (rep, "tcp://127.0.0.1:5560"); rc = zmq_bind (rep, "tcp://127.0.0.1:5560");
assert (rc == 0); assert (rc == 0);
void *req = zmq_socket (ctx, ZMQ_REQ); req = zmq_socket (ctx, ZMQ_REQ);
assert (req); assert (req);
rc = zmq_connect (req, "tcp://127.0.0.1:5560"); rc = zmq_connect (req, "tcp://127.0.0.1:5560");