mirror of
https://github.com/zeromq/libzmq.git
synced 2024-12-12 18:40:27 +01:00
Change zmq_monitor_fn type to cast between pointer-to-object and pointer-to-function in a more standards compliant way
This commit is contained in:
parent
5ef63318f8
commit
06cce15479
@ -434,7 +434,7 @@ Applicable socket types:: all listening sockets, when using TCP transports.
|
|||||||
ZMQ_MONITOR: Registers a callback for socket state changes
|
ZMQ_MONITOR: Registers a callback for socket state changes
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
Registers a callback function / event sink for changes in underlying socket state.
|
Registers a callback function / event sink for changes in underlying socket state.
|
||||||
Expected signature is `void (zmq_monitor_fn) (void *s, int event, zmq_event_data_t *data)`
|
Expected signature of function member of zmq_monitor_fn union is `void (*function)(void *s, int event, zmq_event_data_t *data);`
|
||||||
To remove the callback function call `zmq_setsockopt(socket, ZMQ_MONITOR, NULL, 0)`
|
To remove the callback function call `zmq_setsockopt(socket, ZMQ_MONITOR, NULL, 0)`
|
||||||
The default value of `NULL` means no monitor callback function.
|
The default value of `NULL` means no monitor callback function.
|
||||||
Supported events are :
|
Supported events are :
|
||||||
|
@ -300,7 +300,10 @@ typedef union {
|
|||||||
} zmq_event_data_t;
|
} zmq_event_data_t;
|
||||||
|
|
||||||
/* Callback template for socket state changes */
|
/* Callback template for socket state changes */
|
||||||
typedef void (zmq_monitor_fn) (void *s, int event, zmq_event_data_t *data);
|
typedef union {
|
||||||
|
void *object;
|
||||||
|
void (*function)(void *s, int event, zmq_event_data_t *data);
|
||||||
|
} zmq_monitor_fn;
|
||||||
|
|
||||||
ZMQ_EXPORT void *zmq_socket (void *, int type);
|
ZMQ_EXPORT void *zmq_socket (void *, int type);
|
||||||
ZMQ_EXPORT int zmq_close (void *s);
|
ZMQ_EXPORT int zmq_close (void *s);
|
||||||
|
@ -1030,7 +1030,7 @@ void zmq::socket_base_t::monitor_event (int event_, ...)
|
|||||||
default:
|
default:
|
||||||
zmq_assert (false);
|
zmq_assert (false);
|
||||||
}
|
}
|
||||||
options.monitor ((void *)this, event_, &data);
|
options.monitor->function ((void *)this, event_, &data);
|
||||||
va_end (args);
|
va_end (args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,20 +99,20 @@ int main (int argc, char *argv [])
|
|||||||
assert (rep);
|
assert (rep);
|
||||||
|
|
||||||
// Expects failure - invalid size
|
// Expects failure - invalid size
|
||||||
zmq_monitor_fn *monitor;
|
zmq_monitor_fn monitor;
|
||||||
monitor = listening_sock_monitor;
|
monitor.function = listening_sock_monitor;
|
||||||
|
|
||||||
rc = zmq_setsockopt (rep, ZMQ_MONITOR, *(void **)&monitor, 20);
|
rc = zmq_setsockopt (rep, ZMQ_MONITOR, &monitor, 20);
|
||||||
assert (rc == -1);
|
assert (rc == -1);
|
||||||
assert (errno == EINVAL);
|
assert (errno == EINVAL);
|
||||||
|
|
||||||
rc = zmq_setsockopt (rep, ZMQ_MONITOR, *(void **)&monitor, sizeof (void *));
|
rc = zmq_setsockopt (rep, ZMQ_MONITOR, &monitor, sizeof (void *));
|
||||||
assert (rc == 0);
|
assert (rc == 0);
|
||||||
|
|
||||||
size_t sz = sizeof (void *);
|
size_t sz = sizeof (void *);
|
||||||
rc = zmq_getsockopt (rep, ZMQ_MONITOR, &monitor, &sz);
|
rc = zmq_getsockopt (rep, ZMQ_MONITOR, &monitor, &sz);
|
||||||
assert (rc == 0);
|
assert (rc == 0);
|
||||||
assert (monitor == listening_sock_monitor);
|
assert (monitor.function == listening_sock_monitor);
|
||||||
|
|
||||||
// Remove socket monitor callback
|
// Remove socket monitor callback
|
||||||
rc = zmq_setsockopt (rep, ZMQ_MONITOR, NULL, 0);
|
rc = zmq_setsockopt (rep, ZMQ_MONITOR, NULL, 0);
|
||||||
@ -120,7 +120,7 @@ int main (int argc, char *argv [])
|
|||||||
|
|
||||||
rc = zmq_getsockopt (rep, ZMQ_MONITOR, &monitor, &sz);
|
rc = zmq_getsockopt (rep, ZMQ_MONITOR, &monitor, &sz);
|
||||||
assert (rc == 0);
|
assert (rc == 0);
|
||||||
assert (monitor == listening_sock_monitor);
|
assert (monitor.function == listening_sock_monitor);
|
||||||
|
|
||||||
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);
|
||||||
@ -128,8 +128,8 @@ int main (int argc, char *argv [])
|
|||||||
void *req = zmq_socket (ctx, ZMQ_REQ);
|
void *req = zmq_socket (ctx, ZMQ_REQ);
|
||||||
assert (req);
|
assert (req);
|
||||||
|
|
||||||
monitor = connecting_sock_monitor;
|
monitor.function = connecting_sock_monitor;
|
||||||
rc = zmq_setsockopt (req, ZMQ_MONITOR, *(void **)&monitor, sizeof (void *));
|
rc = zmq_setsockopt (req, ZMQ_MONITOR, &monitor, sizeof (void *));
|
||||||
assert (rc == 0);
|
assert (rc == 0);
|
||||||
|
|
||||||
rc = zmq_connect (req, "tcp://127.0.0.1:5560");
|
rc = zmq_connect (req, "tcp://127.0.0.1:5560");
|
||||||
|
Loading…
Reference in New Issue
Block a user