nio4r 2.5.2 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/workflow.yml +61 -0
- data/.mailmap +16 -0
- data/.rubocop.yml +30 -11
- data/Gemfile +6 -6
- data/{CHANGES.md → changes.md} +78 -1
- data/examples/echo_server.rb +9 -2
- data/ext/libev/Changes +71 -2
- data/ext/libev/ev.c +611 -198
- data/ext/libev/ev.h +25 -22
- data/ext/libev/ev_epoll.c +16 -14
- data/ext/libev/ev_iouring.c +694 -0
- data/ext/libev/ev_kqueue.c +4 -4
- data/ext/libev/ev_linuxaio.c +78 -100
- data/ext/libev/ev_poll.c +6 -6
- data/ext/libev/ev_port.c +3 -3
- data/ext/libev/ev_select.c +6 -6
- data/ext/libev/ev_vars.h +34 -0
- data/ext/libev/ev_win32.c +2 -2
- data/ext/libev/ev_wrap.h +56 -0
- data/ext/nio4r/.clang-format +16 -0
- data/ext/nio4r/bytebuffer.c +101 -65
- data/ext/nio4r/extconf.rb +26 -0
- data/ext/nio4r/libev.h +1 -3
- data/ext/nio4r/monitor.c +81 -53
- data/ext/nio4r/nio4r.h +6 -15
- data/ext/nio4r/nio4r_ext.c +1 -1
- data/ext/nio4r/org/nio4r/ByteBuffer.java +2 -0
- data/ext/nio4r/org/nio4r/Monitor.java +1 -0
- data/ext/nio4r/org/nio4r/Selector.java +8 -10
- data/ext/nio4r/selector.c +132 -93
- data/lib/nio/bytebuffer.rb +10 -0
- data/lib/nio/monitor.rb +8 -1
- data/lib/nio/selector.rb +27 -10
- data/lib/nio/version.rb +6 -1
- data/lib/nio.rb +29 -1
- data/lib/nio4r.rb +5 -0
- data/license.md +77 -0
- data/nio4r.gemspec +6 -5
- data/rakelib/extension.rake +1 -2
- data/readme.md +91 -0
- data/spec/nio/acceptables_spec.rb +4 -0
- data/spec/nio/bytebuffer_spec.rb +6 -1
- data/spec/nio/monitor_spec.rb +7 -0
- data/spec/nio/selectables/pipe_spec.rb +6 -0
- data/spec/nio/selectables/ssl_socket_spec.rb +7 -0
- data/spec/nio/selectables/tcp_socket_spec.rb +7 -0
- data/spec/nio/selectables/udp_socket_spec.rb +9 -2
- data/spec/nio/selector_spec.rb +16 -1
- data/spec/spec_helper.rb +7 -2
- data/spec/support/selectable_examples.rb +8 -0
- metadata +20 -16
- data/.travis.yml +0 -44
- data/Guardfile +0 -10
- data/README.md +0 -150
- data/appveyor.yml +0 -40
data/ext/nio4r/monitor.c
CHANGED
@@ -4,14 +4,15 @@
|
|
4
4
|
*/
|
5
5
|
|
6
6
|
#include "nio4r.h"
|
7
|
+
#include <assert.h>
|
7
8
|
|
8
9
|
static VALUE mNIO = Qnil;
|
9
10
|
static VALUE cNIO_Monitor = Qnil;
|
10
11
|
|
11
12
|
/* Allocator/deallocator */
|
12
13
|
static VALUE NIO_Monitor_allocate(VALUE klass);
|
13
|
-
static void NIO_Monitor_mark(
|
14
|
-
static
|
14
|
+
static void NIO_Monitor_mark(void *data);
|
15
|
+
static size_t NIO_Monitor_memsize(const void *data);
|
15
16
|
|
16
17
|
/* Methods */
|
17
18
|
static VALUE NIO_Monitor_initialize(VALUE self, VALUE selector, VALUE io, VALUE interests);
|
@@ -33,6 +34,18 @@ static VALUE NIO_Monitor_readiness(VALUE self);
|
|
33
34
|
static int NIO_Monitor_symbol2interest(VALUE interests);
|
34
35
|
static void NIO_Monitor_update_interests(VALUE self, int interests);
|
35
36
|
|
37
|
+
/* Compatibility for Ruby <= 3.1 */
|
38
|
+
#ifndef HAVE_RB_IO_DESCRIPTOR
|
39
|
+
static int
|
40
|
+
io_descriptor_fallback(VALUE io)
|
41
|
+
{
|
42
|
+
rb_io_t *fptr;
|
43
|
+
GetOpenFile(io, fptr);
|
44
|
+
return fptr->fd;
|
45
|
+
}
|
46
|
+
#define rb_io_descriptor io_descriptor_fallback
|
47
|
+
#endif
|
48
|
+
|
36
49
|
/* Monitor control how a channel is being waited for by a monitor */
|
37
50
|
void Init_NIO_Monitor()
|
38
51
|
{
|
@@ -57,20 +70,36 @@ void Init_NIO_Monitor()
|
|
57
70
|
rb_define_method(cNIO_Monitor, "writeable?", NIO_Monitor_is_writable, 0);
|
58
71
|
}
|
59
72
|
|
73
|
+
static const rb_data_type_t NIO_Monitor_type = {
|
74
|
+
"NIO::Monitor",
|
75
|
+
{
|
76
|
+
NIO_Monitor_mark,
|
77
|
+
RUBY_TYPED_DEFAULT_FREE,
|
78
|
+
NIO_Monitor_memsize,
|
79
|
+
},
|
80
|
+
0,
|
81
|
+
0,
|
82
|
+
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
|
83
|
+
};
|
84
|
+
|
60
85
|
static VALUE NIO_Monitor_allocate(VALUE klass)
|
61
86
|
{
|
62
87
|
struct NIO_Monitor *monitor = (struct NIO_Monitor *)xmalloc(sizeof(struct NIO_Monitor));
|
63
|
-
|
64
|
-
|
88
|
+
assert(monitor);
|
89
|
+
*monitor = (struct NIO_Monitor){.self = Qnil};
|
90
|
+
return TypedData_Wrap_Struct(klass, &NIO_Monitor_type, monitor);
|
65
91
|
}
|
66
92
|
|
67
|
-
static void NIO_Monitor_mark(
|
93
|
+
static void NIO_Monitor_mark(void *data)
|
68
94
|
{
|
95
|
+
struct NIO_Monitor *monitor = (struct NIO_Monitor *)data;
|
96
|
+
rb_gc_mark(monitor->self);
|
69
97
|
}
|
70
98
|
|
71
|
-
static
|
99
|
+
static size_t NIO_Monitor_memsize(const void *data)
|
72
100
|
{
|
73
|
-
|
101
|
+
const struct NIO_Monitor *monitor = (const struct NIO_Monitor *)data;
|
102
|
+
return sizeof(*monitor);
|
74
103
|
}
|
75
104
|
|
76
105
|
static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE selector_obj)
|
@@ -78,33 +107,31 @@ static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE
|
|
78
107
|
struct NIO_Monitor *monitor;
|
79
108
|
struct NIO_Selector *selector;
|
80
109
|
ID interests_id;
|
81
|
-
rb_io_t *fptr;
|
82
110
|
|
83
111
|
interests_id = SYM2ID(interests);
|
84
112
|
|
85
|
-
|
113
|
+
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
|
86
114
|
|
87
|
-
if(interests_id == rb_intern("r")) {
|
115
|
+
if (interests_id == rb_intern("r")) {
|
88
116
|
monitor->interests = EV_READ;
|
89
|
-
} else if(interests_id == rb_intern("w")) {
|
117
|
+
} else if (interests_id == rb_intern("w")) {
|
90
118
|
monitor->interests = EV_WRITE;
|
91
|
-
} else if(interests_id == rb_intern("rw")) {
|
119
|
+
} else if (interests_id == rb_intern("rw")) {
|
92
120
|
monitor->interests = EV_READ | EV_WRITE;
|
93
121
|
} else {
|
94
|
-
rb_raise(rb_eArgError, "invalid event type %s (must be :r, :w, or :rw)",
|
95
|
-
RSTRING_PTR(rb_funcall(interests, rb_intern("inspect"), 0)));
|
122
|
+
rb_raise(rb_eArgError, "invalid event type %s (must be :r, :w, or :rw)", RSTRING_PTR(rb_funcall(interests, rb_intern("inspect"), 0)));
|
96
123
|
}
|
97
124
|
|
98
|
-
|
99
|
-
ev_io_init(&monitor->ev_io, NIO_Selector_monitor_callback,
|
125
|
+
int descriptor = rb_io_descriptor(rb_convert_type(io, T_FILE, "IO", "to_io"));
|
126
|
+
ev_io_init(&monitor->ev_io, NIO_Selector_monitor_callback, descriptor, monitor->interests);
|
100
127
|
|
101
128
|
rb_ivar_set(self, rb_intern("io"), io);
|
102
129
|
rb_ivar_set(self, rb_intern("interests"), interests);
|
103
130
|
rb_ivar_set(self, rb_intern("selector"), selector_obj);
|
104
131
|
|
105
|
-
|
132
|
+
selector = NIO_Selector_unwrap(selector_obj);
|
106
133
|
|
107
|
-
monitor->self
|
134
|
+
RB_OBJ_WRITE(self, &monitor->self, self);
|
108
135
|
monitor->ev_io.data = (void *)monitor;
|
109
136
|
|
110
137
|
/* We can safely hang onto this as we also hang onto a reference to the
|
@@ -112,7 +139,7 @@ static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE
|
|
112
139
|
monitor->selector = selector;
|
113
140
|
|
114
141
|
if (monitor->interests) {
|
115
|
-
|
142
|
+
ev_io_start(selector->ev_loop, &monitor->ev_io);
|
116
143
|
}
|
117
144
|
|
118
145
|
return Qnil;
|
@@ -122,22 +149,22 @@ static VALUE NIO_Monitor_close(int argc, VALUE *argv, VALUE self)
|
|
122
149
|
{
|
123
150
|
VALUE deregister, selector;
|
124
151
|
struct NIO_Monitor *monitor;
|
125
|
-
|
152
|
+
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
|
126
153
|
|
127
154
|
rb_scan_args(argc, argv, "01", &deregister);
|
128
155
|
selector = rb_ivar_get(self, rb_intern("selector"));
|
129
156
|
|
130
|
-
if(selector != Qnil) {
|
157
|
+
if (selector != Qnil) {
|
131
158
|
/* if ev_loop is 0, it means that the loop has been stopped already (see NIO_Selector_shutdown) */
|
132
|
-
if(monitor->interests && monitor->selector->ev_loop) {
|
133
|
-
|
159
|
+
if (monitor->interests && monitor->selector->ev_loop) {
|
160
|
+
ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io);
|
134
161
|
}
|
135
162
|
|
136
163
|
monitor->selector = 0;
|
137
164
|
rb_ivar_set(self, rb_intern("selector"), Qnil);
|
138
|
-
|
165
|
+
|
139
166
|
/* Default value is true */
|
140
|
-
if(deregister == Qtrue || deregister == Qnil) {
|
167
|
+
if (deregister == Qtrue || deregister == Qnil) {
|
141
168
|
rb_funcall(selector, rb_intern("deregister"), 1, rb_ivar_get(self, rb_intern("io")));
|
142
169
|
}
|
143
170
|
}
|
@@ -148,7 +175,7 @@ static VALUE NIO_Monitor_close(int argc, VALUE *argv, VALUE self)
|
|
148
175
|
static VALUE NIO_Monitor_is_closed(VALUE self)
|
149
176
|
{
|
150
177
|
struct NIO_Monitor *monitor;
|
151
|
-
|
178
|
+
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
|
152
179
|
|
153
180
|
return monitor->selector == 0 ? Qtrue : Qfalse;
|
154
181
|
}
|
@@ -165,7 +192,7 @@ static VALUE NIO_Monitor_interests(VALUE self)
|
|
165
192
|
|
166
193
|
static VALUE NIO_Monitor_set_interests(VALUE self, VALUE interests)
|
167
194
|
{
|
168
|
-
if(NIL_P(interests)) {
|
195
|
+
if (NIL_P(interests)) {
|
169
196
|
NIO_Monitor_update_interests(self, 0);
|
170
197
|
} else {
|
171
198
|
NIO_Monitor_update_interests(self, NIO_Monitor_symbol2interest(interests));
|
@@ -174,22 +201,24 @@ static VALUE NIO_Monitor_set_interests(VALUE self, VALUE interests)
|
|
174
201
|
return rb_ivar_get(self, rb_intern("interests"));
|
175
202
|
}
|
176
203
|
|
177
|
-
static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest)
|
204
|
+
static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest)
|
205
|
+
{
|
178
206
|
struct NIO_Monitor *monitor;
|
179
|
-
|
207
|
+
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
|
180
208
|
|
181
209
|
interest = monitor->interests | NIO_Monitor_symbol2interest(interest);
|
182
|
-
NIO_Monitor_update_interests(self, interest);
|
210
|
+
NIO_Monitor_update_interests(self, (int)interest);
|
183
211
|
|
184
212
|
return rb_ivar_get(self, rb_intern("interests"));
|
185
213
|
}
|
186
214
|
|
187
|
-
static VALUE NIO_Monitor_remove_interest(VALUE self, VALUE interest)
|
215
|
+
static VALUE NIO_Monitor_remove_interest(VALUE self, VALUE interest)
|
216
|
+
{
|
188
217
|
struct NIO_Monitor *monitor;
|
189
|
-
|
218
|
+
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
|
190
219
|
|
191
220
|
interest = monitor->interests & ~NIO_Monitor_symbol2interest(interest);
|
192
|
-
NIO_Monitor_update_interests(self, interest);
|
221
|
+
NIO_Monitor_update_interests(self, (int)interest);
|
193
222
|
|
194
223
|
return rb_ivar_get(self, rb_intern("interests"));
|
195
224
|
}
|
@@ -212,13 +241,13 @@ static VALUE NIO_Monitor_set_value(VALUE self, VALUE obj)
|
|
212
241
|
static VALUE NIO_Monitor_readiness(VALUE self)
|
213
242
|
{
|
214
243
|
struct NIO_Monitor *monitor;
|
215
|
-
|
244
|
+
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
|
216
245
|
|
217
|
-
if((monitor->revents & (EV_READ | EV_WRITE)) == (EV_READ | EV_WRITE)) {
|
246
|
+
if ((monitor->revents & (EV_READ | EV_WRITE)) == (EV_READ | EV_WRITE)) {
|
218
247
|
return ID2SYM(rb_intern("rw"));
|
219
|
-
} else if(monitor->revents & EV_READ) {
|
248
|
+
} else if (monitor->revents & EV_READ) {
|
220
249
|
return ID2SYM(rb_intern("r"));
|
221
|
-
} else if(monitor->revents & EV_WRITE) {
|
250
|
+
} else if (monitor->revents & EV_WRITE) {
|
222
251
|
return ID2SYM(rb_intern("w"));
|
223
252
|
} else {
|
224
253
|
return Qnil;
|
@@ -228,9 +257,9 @@ static VALUE NIO_Monitor_readiness(VALUE self)
|
|
228
257
|
static VALUE NIO_Monitor_is_readable(VALUE self)
|
229
258
|
{
|
230
259
|
struct NIO_Monitor *monitor;
|
231
|
-
|
260
|
+
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
|
232
261
|
|
233
|
-
if(monitor->revents & EV_READ) {
|
262
|
+
if (monitor->revents & EV_READ) {
|
234
263
|
return Qtrue;
|
235
264
|
} else {
|
236
265
|
return Qfalse;
|
@@ -240,9 +269,9 @@ static VALUE NIO_Monitor_is_readable(VALUE self)
|
|
240
269
|
static VALUE NIO_Monitor_is_writable(VALUE self)
|
241
270
|
{
|
242
271
|
struct NIO_Monitor *monitor;
|
243
|
-
|
272
|
+
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
|
244
273
|
|
245
|
-
if(monitor->revents & EV_WRITE) {
|
274
|
+
if (monitor->revents & EV_WRITE) {
|
246
275
|
return Qtrue;
|
247
276
|
} else {
|
248
277
|
return Qfalse;
|
@@ -256,15 +285,14 @@ static int NIO_Monitor_symbol2interest(VALUE interests)
|
|
256
285
|
ID interests_id;
|
257
286
|
interests_id = SYM2ID(interests);
|
258
287
|
|
259
|
-
if(interests_id == rb_intern("r")) {
|
288
|
+
if (interests_id == rb_intern("r")) {
|
260
289
|
return EV_READ;
|
261
|
-
} else if(interests_id == rb_intern("w")) {
|
290
|
+
} else if (interests_id == rb_intern("w")) {
|
262
291
|
return EV_WRITE;
|
263
|
-
} else if(interests_id == rb_intern("rw")) {
|
292
|
+
} else if (interests_id == rb_intern("rw")) {
|
264
293
|
return EV_READ | EV_WRITE;
|
265
294
|
} else {
|
266
|
-
rb_raise(rb_eArgError, "invalid interest type %s (must be :r, :w, or :rw)",
|
267
|
-
RSTRING_PTR(rb_funcall(interests, rb_intern("inspect"), 0)));
|
295
|
+
rb_raise(rb_eArgError, "invalid interest type %s (must be :r, :w, or :rw)", RSTRING_PTR(rb_funcall(interests, rb_intern("inspect"), 0)));
|
268
296
|
}
|
269
297
|
}
|
270
298
|
|
@@ -272,14 +300,14 @@ static void NIO_Monitor_update_interests(VALUE self, int interests)
|
|
272
300
|
{
|
273
301
|
ID interests_id;
|
274
302
|
struct NIO_Monitor *monitor;
|
275
|
-
|
303
|
+
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
|
276
304
|
|
277
|
-
if(NIO_Monitor_is_closed(self) == Qtrue) {
|
305
|
+
if (NIO_Monitor_is_closed(self) == Qtrue) {
|
278
306
|
rb_raise(rb_eEOFError, "monitor is closed");
|
279
307
|
}
|
280
308
|
|
281
|
-
if(interests) {
|
282
|
-
switch(interests) {
|
309
|
+
if (interests) {
|
310
|
+
switch (interests) {
|
283
311
|
case EV_READ:
|
284
312
|
interests_id = rb_intern("r");
|
285
313
|
break;
|
@@ -298,9 +326,9 @@ static void NIO_Monitor_update_interests(VALUE self, int interests)
|
|
298
326
|
rb_ivar_set(self, rb_intern("interests"), Qnil);
|
299
327
|
}
|
300
328
|
|
301
|
-
if(monitor->interests != interests) {
|
329
|
+
if (monitor->interests != interests) {
|
302
330
|
// If the monitor currently has interests, we should stop it.
|
303
|
-
if(monitor->interests) {
|
331
|
+
if (monitor->interests) {
|
304
332
|
ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io);
|
305
333
|
}
|
306
334
|
|
@@ -309,7 +337,7 @@ static void NIO_Monitor_update_interests(VALUE self, int interests)
|
|
309
337
|
ev_io_set(&monitor->ev_io, monitor->ev_io.fd, monitor->interests);
|
310
338
|
|
311
339
|
// If we are interested in events, schedule the monitor back into the event loop:
|
312
|
-
if(monitor->interests) {
|
340
|
+
if (monitor->interests) {
|
313
341
|
ev_io_start(monitor->selector->ev_loop, &monitor->ev_io);
|
314
342
|
}
|
315
343
|
}
|
data/ext/nio4r/nio4r.h
CHANGED
@@ -6,12 +6,11 @@
|
|
6
6
|
#ifndef NIO4R_H
|
7
7
|
#define NIO4R_H
|
8
8
|
|
9
|
+
#include "libev.h"
|
9
10
|
#include "ruby.h"
|
10
11
|
#include "ruby/io.h"
|
11
|
-
#include "libev.h"
|
12
12
|
|
13
|
-
struct NIO_Selector
|
14
|
-
{
|
13
|
+
struct NIO_Selector {
|
15
14
|
struct ev_loop *ev_loop;
|
16
15
|
struct ev_timer timer; /* for timeouts */
|
17
16
|
struct ev_io wakeup;
|
@@ -24,32 +23,24 @@ struct NIO_Selector
|
|
24
23
|
VALUE ready_array;
|
25
24
|
};
|
26
25
|
|
27
|
-
struct NIO_callback_data
|
28
|
-
{
|
26
|
+
struct NIO_callback_data {
|
29
27
|
VALUE *monitor;
|
30
28
|
struct NIO_Selector *selector;
|
31
29
|
};
|
32
30
|
|
33
|
-
struct NIO_Monitor
|
34
|
-
{
|
31
|
+
struct NIO_Monitor {
|
35
32
|
VALUE self;
|
36
33
|
int interests, revents;
|
37
34
|
struct ev_io ev_io;
|
38
35
|
struct NIO_Selector *selector;
|
39
36
|
};
|
40
37
|
|
41
|
-
struct NIO_ByteBuffer
|
42
|
-
{
|
38
|
+
struct NIO_ByteBuffer {
|
43
39
|
char *buffer;
|
44
40
|
int position, limit, capacity, mark;
|
45
41
|
};
|
46
42
|
|
47
|
-
|
48
|
-
#ifdef GetReadFile
|
49
|
-
# define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
|
50
|
-
#else
|
51
|
-
# define FPTR_TO_FD(fptr) fptr->fd
|
52
|
-
#endif /* GetReadFile */
|
43
|
+
struct NIO_Selector *NIO_Selector_unwrap(VALUE selector);
|
53
44
|
|
54
45
|
/* Thunk between libev callbacks in NIO::Monitors and NIO::Selectors */
|
55
46
|
void NIO_Selector_monitor_callback(struct ev_loop *ev_loop, struct ev_io *io, int revents);
|
data/ext/nio4r/nio4r_ext.c
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
package org.nio4r;
|
2
2
|
|
3
3
|
import java.io.IOException;
|
4
|
+
import java.io.Serializable;
|
4
5
|
import java.nio.channels.Channel;
|
5
6
|
import java.nio.channels.SelectableChannel;
|
6
7
|
import java.nio.channels.ReadableByteChannel;
|
@@ -25,6 +26,7 @@ import org.jruby.runtime.Block;
|
|
25
26
|
created by Upekshej
|
26
27
|
*/
|
27
28
|
public class ByteBuffer extends RubyObject {
|
29
|
+
private static final long serialVersionUID = -6903439483039149324L;
|
28
30
|
private java.nio.ByteBuffer byteBuffer;
|
29
31
|
|
30
32
|
public static RaiseException newOverflowError(ThreadContext context, String message) {
|
@@ -13,6 +13,7 @@ import org.jruby.runtime.ThreadContext;
|
|
13
13
|
import org.jruby.runtime.builtin.IRubyObject;
|
14
14
|
|
15
15
|
public class Monitor extends RubyObject {
|
16
|
+
private static final long serialVersionUID = -3733782997115074794L;
|
16
17
|
private SelectionKey key;
|
17
18
|
private RubyIO io;
|
18
19
|
private IRubyObject interests, selector, value, closed;
|
@@ -7,7 +7,6 @@ import java.io.IOException;
|
|
7
7
|
import java.nio.channels.Channel;
|
8
8
|
import java.nio.channels.SelectableChannel;
|
9
9
|
import java.nio.channels.SelectionKey;
|
10
|
-
import java.nio.channels.CancelledKeyException;
|
11
10
|
|
12
11
|
import org.jruby.Ruby;
|
13
12
|
import org.jruby.RubyArray;
|
@@ -21,9 +20,8 @@ import org.jruby.runtime.ThreadContext;
|
|
21
20
|
import org.jruby.runtime.builtin.IRubyObject;
|
22
21
|
import org.jruby.util.io.OpenFile;
|
23
22
|
|
24
|
-
import org.nio4r.Monitor;
|
25
|
-
|
26
23
|
public class Selector extends RubyObject {
|
24
|
+
private static final long serialVersionUID = -14562818539414873L;
|
27
25
|
private java.nio.channels.Selector selector;
|
28
26
|
private HashMap<SelectableChannel,SelectionKey> cancelledKeys;
|
29
27
|
private volatile boolean wakeupFired;
|
@@ -45,7 +43,7 @@ public class Selector extends RubyObject {
|
|
45
43
|
|
46
44
|
@JRubyMethod
|
47
45
|
public IRubyObject initialize(ThreadContext context, IRubyObject backend) {
|
48
|
-
if(backend != context.runtime.newSymbol("java")) {
|
46
|
+
if(backend != context.runtime.newSymbol("java") && !backend.isNil()) {
|
49
47
|
throw context.runtime.newArgumentError(":java is the only supported backend");
|
50
48
|
}
|
51
49
|
|
@@ -203,15 +201,15 @@ public class Selector extends RubyObject {
|
|
203
201
|
return context.nil;
|
204
202
|
}
|
205
203
|
|
206
|
-
RubyArray array = null;
|
204
|
+
RubyArray<?> array = null;
|
207
205
|
|
208
206
|
if(!block.isGiven()) {
|
209
207
|
array = runtime.newArray(this.selector.selectedKeys().size());
|
210
208
|
}
|
211
209
|
|
212
|
-
Iterator selectedKeys = this.selector.selectedKeys().iterator();
|
210
|
+
Iterator<SelectionKey> selectedKeys = this.selector.selectedKeys().iterator();
|
213
211
|
while(selectedKeys.hasNext()) {
|
214
|
-
SelectionKey key =
|
212
|
+
SelectionKey key = selectedKeys.next();
|
215
213
|
processKey(key);
|
216
214
|
|
217
215
|
selectedKeys.remove();
|
@@ -263,10 +261,10 @@ public class Selector extends RubyObject {
|
|
263
261
|
|
264
262
|
/* Flush our internal buffer of cancelled keys */
|
265
263
|
private void cancelKeys() {
|
266
|
-
Iterator cancelledKeys = this.cancelledKeys.entrySet().iterator();
|
264
|
+
Iterator<Map.Entry<SelectableChannel, SelectionKey>> cancelledKeys = this.cancelledKeys.entrySet().iterator();
|
267
265
|
while(cancelledKeys.hasNext()) {
|
268
|
-
Map.Entry entry =
|
269
|
-
SelectionKey key =
|
266
|
+
Map.Entry<SelectableChannel, SelectionKey> entry = cancelledKeys.next();
|
267
|
+
SelectionKey key = entry.getValue();
|
270
268
|
key.cancel();
|
271
269
|
cancelledKeys.remove();
|
272
270
|
}
|