-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathev_zsock_test.c
72 lines (57 loc) · 1.61 KB
/
ev_zsock_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ev.h>
#include <zmq.h>
#include "ev_zsock.h"
static void
sigint_cb(struct ev_loop *loop, ev_signal *w, int revents)
{
ev_break(loop, EVBREAK_ALL);
}
void timeout_cb (struct ev_loop *loop, ev_timer *w, int revents)
{
ev_tstamp ts = ev_now (loop);
void *zsock = w->data;
zmq_send(zsock, &ts, sizeof(ts), 0);
zmq_send(zsock, &ts, sizeof(ts), 0);
}
void zsock_cb(struct ev_loop *loop, ev_zsock_t *wz, int revents)
{
ev_tstamp ts_recv = ev_now (loop);
ev_tstamp ts_send;
zmq_recv(wz->zsock, &ts_send, sizeof(ts_send), 0);
printf("%f\n", ts_recv - ts_send);
}
int main()
{
struct ev_loop *loop = ev_default_loop(0);
ev_signal signal_watcher;
ev_signal *p_signal_watcher = &signal_watcher;
ev_signal_init(p_signal_watcher, sigint_cb, SIGINT);
ev_signal_start(loop, &signal_watcher);
void *zctx = zmq_ctx_new();
void *zsock_recv = zmq_socket(zctx, ZMQ_PULL);
assert(zsock_recv!=NULL);
int rc = zmq_bind(zsock_recv, "inproc://channel");
assert(rc!=-1);
ev_zsock_t wz;
ev_zsock_init(&wz, zsock_cb, zsock_recv, EV_READ);
ev_zsock_start(loop, &wz);
void *zsock_send = zmq_socket(zctx, ZMQ_PUSH);
assert(zsock_send!=NULL);
rc = zmq_connect(zsock_send, "inproc://channel");
assert(rc!=-1);
ev_timer timeout_watcher;
ev_timer *p_timeout_watcher = &timeout_watcher;
ev_timer_init (p_timeout_watcher, timeout_cb, 1.0, 1.0);
timeout_watcher.data = zsock_send;
ev_timer_start (loop, &timeout_watcher);
ev_run (loop, 0);
printf("loop exited\n");
zmq_close(zsock_recv);
zmq_close(zsock_send);
zmq_ctx_destroy(zctx);
return 0;
}