-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPoller.cpp
113 lines (85 loc) · 2.67 KB
/
Poller.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Copyright (c) Richard Smith 2011
This file is part of hxzmq.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the Lesser GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
0MQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Lesser GNU General Public License for more details.
You should have received a copy of the Lesser GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <cstring>
#include <zmq.h>
#include <hx/CFFI.h>
#include "socket.h"
value hx_zmq_poll (value sockets_, value events_, value timeout_) {
if (!val_is_int(timeout_) || !val_is_array(sockets_) || !val_is_array(events_)) {
val_throw(alloc_int(EINVAL));
return alloc_null();
}
//value *sockets_array;
//value *events_array;
//sockets_array = val_array_ptr( sockets_ );
//events_array = val_array_ptr ( events_ );
int ls = val_array_size(sockets_);
int le = val_array_size(events_);
if (ls != le) {
val_throw(alloc_int(EINVAL));
return alloc_null();
}
zmq_pollitem_t *pitem = new zmq_pollitem_t [ls];
for (int i = 0; i < ls; i++) {
value socket = val_array_i(sockets_, i);
value event = val_array_i(events_, i);
// Test that array index values are of the expected value type
val_check_kind(socket, k_zmq_socket_handle);
if (!val_is_int(event)) {
val_throw(alloc_int(EINVAL));
delete [] pitem;
return alloc_null();
}
pitem [i].socket = val_data (socket);
pitem [i].fd = 0;
pitem [i].events = val_int (event);
pitem [i].revents = 0;
}
gc_enter_blocking();
int rc = 0;
int err = 0;
long tout = val_int(timeout_);
rc = zmq_poll (pitem, ls, tout);
err = zmq_errno();
gc_exit_blocking();
if (rc == -1) {
val_throw(alloc_int(err));
delete [] pitem;
return alloc_null();
}
// return results
value retObj = alloc_empty_object ();
alloc_field( retObj, val_id("_ret"), alloc_int(rc));
// build returned _revents int array
value haxe_revents = alloc_array(ls);
for (int i = 0; i < ls; i++) {
val_array_set_i (haxe_revents, i, alloc_int(pitem[i].revents));
}
alloc_field( retObj, val_id("_revents"), haxe_revents);
delete [] pitem;
pitem = NULL;
return retObj;
}
DEFINE_PRIM (hx_zmq_poll, 3);
value hx_zmq_ZMQ_POLL_MSEC()
{
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,0,0)
return alloc_int(1);
#else
return alloc_int(1000);
#endif
}
DEFINE_PRIM( hx_zmq_ZMQ_POLL_MSEC,0);