The ZeroMQ API documentation states that the event (source here):
ZMQ_EVENT_DISCONNECTED
The socket was disconnected unexpectedly. The event value is the FD of the underlying network socket. Warning: this socket will be closed.
How is "unexpected" defined here, and how would it be possible to trigger a "clean" disconnect on, say, a REQ
-REP
pair?
I've tried:
zmq_close(client)
signalsZMQ_EVENT_DISCONNECTED
, although I would have expected this to be cleanzmq_disconnect(client, addr)
before the above does not change the monitored events (i.e., I still get a signal for an "unexpected disconnect" when the explicit disconnect is successful)
Is there a way to distinguish a clean socket closing from, say, a kill -9
on the client? (the latter still signals FIN
, so this should theoretically be possible to detect and distinguish)
Code for illustration below (modified from here; compile and run with gcc -lzmq zmq_test.c && ./a.out
)
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <zmq.h>
// Read one event off the monitor socket; return value and address
// by reference, if not null, and event number by value. Returns -1
// in case of error.
static int get_monitor_event (void *monitor, int *value, char **address)
{
// First frame in message contains event number and value
zmq_msg_t msg;
zmq_msg_init (&msg);
if (zmq_msg_recv (&msg, monitor, 0) == -1)
return -1; // Interruped, presumably
assert (zmq_msg_more (&msg));
uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
uint16_t event = *(uint16_t *) (data);
if (value)
*value = *(uint32_t *) (data + 2);
// Second frame in message contains event address
zmq_msg_init (&msg);
if (zmq_msg_recv (&msg, monitor, 0) == -1)
return -1; // Interruped, presumably
assert (!zmq_msg_more (&msg));
if (address) {
uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
size_t size = zmq_msg_size (&msg);
*address = (char *) malloc (size + 1);
memcpy (*address, data, size);
*address [size] = 0;
}
printf("event: %d\n", event);
return event;
}
static void close_zero_linger(void *socket)
{
int linger = 0;
zmq_setsockopt(socket, ZMQ_LINGER, &linger, sizeof(linger));
zmq_close(socket);
}
static void bounce(void *client, void *server)
{
char send_buf[] = "Hello";
char recv_buf[sizeof(send_buf) / sizeof(send_buf[0])];
zmq_send(client, send_buf, sizeof(send_buf), 0);
zmq_recv(server, recv_buf, sizeof(recv_buf), 0);
zmq_send(server, send_buf, sizeof(send_buf), 0);
zmq_recv(client, recv_buf, sizeof(recv_buf), 0);
}
int main (void)
{
void *ctx = zmq_ctx_new ();
void *client = zmq_socket (ctx, ZMQ_REQ);
void *server = zmq_socket (ctx, ZMQ_REP);
int rc = zmq_socket_monitor (client, "tcp://127.0.0.1:9999", 0);
// Monitor all events on client and server sockets
rc = zmq_socket_monitor (server, "inproc://monitor-server", ZMQ_EVENT_ALL)
void *server_mon = zmq_socket (ctx, ZMQ_PAIR);
rc = zmq_connect (server_mon, "inproc://monitor-server");
// Now do a basic ping test
rc = zmq_bind (server, "tcp://127.0.0.1:9998");
rc = zmq_connect (client, "tcp://127.0.0.1:9998");
bounce (client, server);
// Close client
rc = zmq_disconnect(client, "tcp://127.0.0.1:9998");
printf("disconnect: %d\n", rc);
// close_zero_linger (client);
rc = zmq_close(client);
printf("close: %d\n", rc);
// sleep(1); // no difference
// This is the flow of server events
event = get_monitor_event (server_mon, NULL, NULL);
assert (event == ZMQ_EVENT_LISTENING);
event = get_monitor_event (server_mon, NULL, NULL);
assert (event == ZMQ_EVENT_ACCEPTED);
event = get_monitor_event (server_mon, NULL, NULL);
assert (event == ZMQ_EVENT_HANDSHAKE_SUCCEEDED);
// sleep(1); // no difference
event = get_monitor_event (server_mon, NULL, NULL);
// assert (event == ZMQ_EVENT_CLOSED);
// event = get_monitor_event (server_mon, NULL, NULL);
// assert (event == ZMQ_EVENT_MONITOR_STOPPED);
assert (event == ZMQ_EVENT_DISCONNECTED); // why??
// Close down the sockets
close_zero_linger (server);
close_zero_linger (server_mon);
zmq_ctx_term (ctx);
return 0;
}