nio4r 2.5.2 → 2.5.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/workflow.yml +61 -0
  3. data/.rubocop.yml +30 -11
  4. data/CHANGES.md +60 -1
  5. data/Gemfile +2 -4
  6. data/README.md +7 -58
  7. data/examples/echo_server.rb +2 -2
  8. data/ext/libev/Changes +71 -2
  9. data/ext/libev/ev.c +611 -198
  10. data/ext/libev/ev.h +25 -22
  11. data/ext/libev/ev_epoll.c +16 -14
  12. data/ext/libev/ev_iouring.c +694 -0
  13. data/ext/libev/ev_kqueue.c +4 -4
  14. data/ext/libev/ev_linuxaio.c +78 -100
  15. data/ext/libev/ev_poll.c +6 -6
  16. data/ext/libev/ev_port.c +3 -3
  17. data/ext/libev/ev_select.c +6 -6
  18. data/ext/libev/ev_vars.h +34 -0
  19. data/ext/libev/ev_win32.c +2 -2
  20. data/ext/libev/ev_wrap.h +56 -0
  21. data/ext/nio4r/.clang-format +16 -0
  22. data/ext/nio4r/bytebuffer.c +27 -28
  23. data/ext/nio4r/extconf.rb +11 -0
  24. data/ext/nio4r/libev.h +1 -3
  25. data/ext/nio4r/monitor.c +34 -31
  26. data/ext/nio4r/nio4r.h +7 -12
  27. data/ext/nio4r/nio4r_ext.c +1 -1
  28. data/ext/nio4r/org/nio4r/ByteBuffer.java +2 -0
  29. data/ext/nio4r/org/nio4r/Monitor.java +1 -0
  30. data/ext/nio4r/org/nio4r/Selector.java +8 -10
  31. data/ext/nio4r/selector.c +89 -75
  32. data/lib/nio/bytebuffer.rb +4 -0
  33. data/lib/nio/monitor.rb +1 -1
  34. data/lib/nio/selector.rb +12 -10
  35. data/lib/nio/version.rb +1 -1
  36. data/lib/nio.rb +20 -1
  37. data/license.md +66 -0
  38. data/nio4r.gemspec +2 -2
  39. data/rakelib/extension.rake +1 -2
  40. data/spec/nio/bytebuffer_spec.rb +0 -1
  41. data/spec/nio/selectables/udp_socket_spec.rb +2 -2
  42. data/spec/nio/selector_spec.rb +4 -1
  43. data/spec/spec_helper.rb +2 -3
  44. metadata +13 -12
  45. data/.travis.yml +0 -44
  46. data/Guardfile +0 -10
  47. data/appveyor.yml +0 -40
@@ -42,7 +42,7 @@ void Init_NIO_ByteBuffer()
42
42
  cNIO_ByteBuffer = rb_define_class_under(mNIO, "ByteBuffer", rb_cObject);
43
43
  rb_define_alloc_func(cNIO_ByteBuffer, NIO_ByteBuffer_allocate);
44
44
 
45
- cNIO_ByteBuffer_OverflowError = rb_define_class_under(cNIO_ByteBuffer, "OverflowError", rb_eIOError);
45
+ cNIO_ByteBuffer_OverflowError = rb_define_class_under(cNIO_ByteBuffer, "OverflowError", rb_eIOError);
46
46
  cNIO_ByteBuffer_UnderflowError = rb_define_class_under(cNIO_ByteBuffer, "UnderflowError", rb_eIOError);
47
47
  cNIO_ByteBuffer_MarkUnsetError = rb_define_class_under(cNIO_ByteBuffer, "MarkUnsetError", rb_eIOError);
48
48
 
@@ -85,8 +85,8 @@ static void NIO_ByteBuffer_gc_mark(struct NIO_ByteBuffer *buffer)
85
85
 
86
86
  static void NIO_ByteBuffer_free(struct NIO_ByteBuffer *buffer)
87
87
  {
88
- if(buffer->buffer)
89
- xfree(buffer->buffer);
88
+ if (buffer->buffer)
89
+ xfree(buffer->buffer);
90
90
  xfree(buffer);
91
91
  }
92
92
 
@@ -133,17 +133,17 @@ static VALUE NIO_ByteBuffer_set_position(VALUE self, VALUE new_position)
133
133
 
134
134
  pos = NUM2INT(new_position);
135
135
 
136
- if(pos < 0) {
136
+ if (pos < 0) {
137
137
  rb_raise(rb_eArgError, "negative position given");
138
138
  }
139
139
 
140
- if(pos > buffer->limit) {
140
+ if (pos > buffer->limit) {
141
141
  rb_raise(rb_eArgError, "specified position exceeds limit");
142
142
  }
143
143
 
144
144
  buffer->position = pos;
145
145
 
146
- if(buffer->mark > buffer->position) {
146
+ if (buffer->mark > buffer->position) {
147
147
  buffer->mark = MARK_UNSET;
148
148
  }
149
149
 
@@ -166,21 +166,21 @@ static VALUE NIO_ByteBuffer_set_limit(VALUE self, VALUE new_limit)
166
166
 
167
167
  lim = NUM2INT(new_limit);
168
168
 
169
- if(lim < 0) {
169
+ if (lim < 0) {
170
170
  rb_raise(rb_eArgError, "negative limit given");
171
171
  }
172
172
 
173
- if(lim > buffer->capacity) {
173
+ if (lim > buffer->capacity) {
174
174
  rb_raise(rb_eArgError, "specified limit exceeds capacity");
175
175
  }
176
176
 
177
177
  buffer->limit = lim;
178
178
 
179
- if(buffer->position > lim) {
179
+ if (buffer->position > lim) {
180
180
  buffer->position = lim;
181
181
  }
182
182
 
183
- if(buffer->mark > lim) {
183
+ if (buffer->mark > lim) {
184
184
  buffer->mark = MARK_UNSET;
185
185
  }
186
186
 
@@ -220,17 +220,17 @@ static VALUE NIO_ByteBuffer_get(int argc, VALUE *argv, VALUE self)
220
220
 
221
221
  rb_scan_args(argc, argv, "01", &length);
222
222
 
223
- if(length == Qnil) {
223
+ if (length == Qnil) {
224
224
  len = buffer->limit - buffer->position;
225
225
  } else {
226
226
  len = NUM2INT(length);
227
227
  }
228
228
 
229
- if(len < 0) {
229
+ if (len < 0) {
230
230
  rb_raise(rb_eArgError, "negative length given");
231
231
  }
232
232
 
233
- if(len > buffer->limit - buffer->position) {
233
+ if (len > buffer->limit - buffer->position) {
234
234
  rb_raise(cNIO_ByteBuffer_UnderflowError, "not enough data in buffer");
235
235
  }
236
236
 
@@ -248,11 +248,11 @@ static VALUE NIO_ByteBuffer_fetch(VALUE self, VALUE index)
248
248
 
249
249
  i = NUM2INT(index);
250
250
 
251
- if(i < 0) {
251
+ if (i < 0) {
252
252
  rb_raise(rb_eArgError, "negative index given");
253
253
  }
254
254
 
255
- if(i >= buffer->limit) {
255
+ if (i >= buffer->limit) {
256
256
  rb_raise(rb_eArgError, "specified index exceeds limit");
257
257
  }
258
258
 
@@ -268,7 +268,7 @@ static VALUE NIO_ByteBuffer_put(VALUE self, VALUE string)
268
268
  StringValue(string);
269
269
  length = RSTRING_LEN(string);
270
270
 
271
- if(length > buffer->limit - buffer->position) {
271
+ if (length > buffer->limit - buffer->position) {
272
272
  rb_raise(cNIO_ByteBuffer_OverflowError, "buffer is full");
273
273
  }
274
274
 
@@ -289,14 +289,14 @@ static VALUE NIO_ByteBuffer_read_from(VALUE self, VALUE io)
289
289
  rb_io_set_nonblock(fptr);
290
290
 
291
291
  nbytes = buffer->limit - buffer->position;
292
- if(nbytes == 0) {
292
+ if (nbytes == 0) {
293
293
  rb_raise(cNIO_ByteBuffer_OverflowError, "buffer is full");
294
294
  }
295
295
 
296
296
  bytes_read = read(FPTR_TO_FD(fptr), buffer->buffer + buffer->position, nbytes);
297
297
 
298
- if(bytes_read < 0) {
299
- if(errno == EAGAIN) {
298
+ if (bytes_read < 0) {
299
+ if (errno == EAGAIN) {
300
300
  return INT2NUM(0);
301
301
  } else {
302
302
  rb_sys_fail("write");
@@ -319,14 +319,14 @@ static VALUE NIO_ByteBuffer_write_to(VALUE self, VALUE io)
319
319
  rb_io_set_nonblock(fptr);
320
320
 
321
321
  nbytes = buffer->limit - buffer->position;
322
- if(nbytes == 0) {
322
+ if (nbytes == 0) {
323
323
  rb_raise(cNIO_ByteBuffer_UnderflowError, "no data remaining in buffer");
324
324
  }
325
325
 
326
326
  bytes_written = write(FPTR_TO_FD(fptr), buffer->buffer + buffer->position, nbytes);
327
327
 
328
- if(bytes_written < 0) {
329
- if(errno == EAGAIN) {
328
+ if (bytes_written < 0) {
329
+ if (errno == EAGAIN) {
330
330
  return INT2NUM(0);
331
331
  } else {
332
332
  rb_sys_fail("write");
@@ -375,7 +375,7 @@ static VALUE NIO_ByteBuffer_reset(VALUE self)
375
375
  struct NIO_ByteBuffer *buffer;
376
376
  Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
377
377
 
378
- if(buffer->mark < 0) {
378
+ if (buffer->mark < 0) {
379
379
  rb_raise(cNIO_ByteBuffer_MarkUnsetError, "mark has not been set");
380
380
  } else {
381
381
  buffer->position = buffer->mark;
@@ -402,8 +402,8 @@ static VALUE NIO_ByteBuffer_each(VALUE self)
402
402
  struct NIO_ByteBuffer *buffer;
403
403
  Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
404
404
 
405
- if(rb_block_given_p()) {
406
- for(i = 0; i < buffer->limit; i++) {
405
+ if (rb_block_given_p()) {
406
+ for (i = 0; i < buffer->limit; i++) {
407
407
  rb_yield(INT2NUM(buffer->buffer[i]));
408
408
  }
409
409
  } else {
@@ -421,9 +421,8 @@ static VALUE NIO_ByteBuffer_inspect(VALUE self)
421
421
  return rb_sprintf(
422
422
  "#<%s:%p @position=%d @limit=%d @capacity=%d>",
423
423
  rb_class2name(CLASS_OF(self)),
424
- (void*)self,
424
+ (void *)self,
425
425
  buffer->position,
426
426
  buffer->limit,
427
- buffer->capacity
428
- );
427
+ buffer->capacity);
429
428
  }
data/ext/nio4r/extconf.rb CHANGED
@@ -4,6 +4,10 @@ require "rubygems"
4
4
 
5
5
  # Write a dummy Makefile on Windows because we use the pure Ruby implementation there
6
6
  if Gem.win_platform?
7
+ begin
8
+ require "devkit" if RUBY_PLATFORM.include?("mingw")
9
+ rescue LoadError => e
10
+ end
7
11
  File.write("Makefile", "all install::\n")
8
12
  File.write("nio4r_ext.so", "")
9
13
  exit
@@ -14,6 +18,7 @@ require "mkmf"
14
18
  have_header("unistd.h")
15
19
 
16
20
  $defs << "-DEV_USE_LINUXAIO" if have_header("linux/aio_abi.h")
21
+ $defs << "-DEV_USE_IOURING" if have_header("linux/io_uring.h")
17
22
  $defs << "-DEV_USE_SELECT" if have_header("sys/select.h")
18
23
  $defs << "-DEV_USE_POLL" if have_type("port_event_t", "poll.h")
19
24
  $defs << "-DEV_USE_EPOLL" if have_header("sys/epoll.h")
@@ -21,7 +26,13 @@ $defs << "-DEV_USE_KQUEUE" if have_header("sys/event.h") && have_header("s
21
26
  $defs << "-DEV_USE_PORT" if have_type("port_event_t", "port.h")
22
27
  $defs << "-DHAVE_SYS_RESOURCE_H" if have_header("sys/resource.h")
23
28
 
29
+ $defs << "-DEV_STANDALONE" # prevent libev from assuming "config.h" exists
30
+
24
31
  CONFIG["optflags"] << " -fno-strict-aliasing" unless RUBY_PLATFORM =~ /mswin/
25
32
 
33
+ if RUBY_PLATFORM =~ /darwin/
34
+ $DLDFLAGS.gsub!(/\-arch\s+[^\s]+/, "")
35
+ end
36
+
26
37
  dir_config "nio4r_ext"
27
38
  create_makefile "nio4r_ext"
data/ext/nio4r/libev.h CHANGED
@@ -1,9 +1,7 @@
1
- #define EV_STANDALONE /* keeps ev from requiring config.h */
2
-
3
1
  #ifdef _WIN32
4
2
  #define EV_SELECT_IS_WINSOCKET 1
5
3
  #define EV_USE_MONOTONIC 0
6
4
  #define EV_USE_REALTIME 0
7
5
  #endif
8
6
 
9
- #include "../libev/ev.h"
7
+ #include "../libev/ev.h"
data/ext/nio4r/monitor.c CHANGED
@@ -4,6 +4,7 @@
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;
@@ -60,12 +61,14 @@ void Init_NIO_Monitor()
60
61
  static VALUE NIO_Monitor_allocate(VALUE klass)
61
62
  {
62
63
  struct NIO_Monitor *monitor = (struct NIO_Monitor *)xmalloc(sizeof(struct NIO_Monitor));
63
-
64
+ assert(monitor);
65
+ *monitor = (struct NIO_Monitor){.self = Qnil};
64
66
  return Data_Wrap_Struct(klass, NIO_Monitor_mark, NIO_Monitor_free, monitor);
65
67
  }
66
68
 
67
69
  static void NIO_Monitor_mark(struct NIO_Monitor *monitor)
68
70
  {
71
+ rb_gc_mark(monitor->self);
69
72
  }
70
73
 
71
74
  static void NIO_Monitor_free(struct NIO_Monitor *monitor)
@@ -84,15 +87,14 @@ static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE
84
87
 
85
88
  Data_Get_Struct(self, struct NIO_Monitor, monitor);
86
89
 
87
- if(interests_id == rb_intern("r")) {
90
+ if (interests_id == rb_intern("r")) {
88
91
  monitor->interests = EV_READ;
89
- } else if(interests_id == rb_intern("w")) {
92
+ } else if (interests_id == rb_intern("w")) {
90
93
  monitor->interests = EV_WRITE;
91
- } else if(interests_id == rb_intern("rw")) {
94
+ } else if (interests_id == rb_intern("rw")) {
92
95
  monitor->interests = EV_READ | EV_WRITE;
93
96
  } 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)));
97
+ rb_raise(rb_eArgError, "invalid event type %s (must be :r, :w, or :rw)", RSTRING_PTR(rb_funcall(interests, rb_intern("inspect"), 0)));
96
98
  }
97
99
 
98
100
  GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
@@ -112,7 +114,7 @@ static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE
112
114
  monitor->selector = selector;
113
115
 
114
116
  if (monitor->interests) {
115
- ev_io_start(selector->ev_loop, &monitor->ev_io);
117
+ ev_io_start(selector->ev_loop, &monitor->ev_io);
116
118
  }
117
119
 
118
120
  return Qnil;
@@ -127,17 +129,17 @@ static VALUE NIO_Monitor_close(int argc, VALUE *argv, VALUE self)
127
129
  rb_scan_args(argc, argv, "01", &deregister);
128
130
  selector = rb_ivar_get(self, rb_intern("selector"));
129
131
 
130
- if(selector != Qnil) {
132
+ if (selector != Qnil) {
131
133
  /* 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
- ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io);
134
+ if (monitor->interests && monitor->selector->ev_loop) {
135
+ ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io);
134
136
  }
135
137
 
136
138
  monitor->selector = 0;
137
139
  rb_ivar_set(self, rb_intern("selector"), Qnil);
138
-
140
+
139
141
  /* Default value is true */
140
- if(deregister == Qtrue || deregister == Qnil) {
142
+ if (deregister == Qtrue || deregister == Qnil) {
141
143
  rb_funcall(selector, rb_intern("deregister"), 1, rb_ivar_get(self, rb_intern("io")));
142
144
  }
143
145
  }
@@ -165,7 +167,7 @@ static VALUE NIO_Monitor_interests(VALUE self)
165
167
 
166
168
  static VALUE NIO_Monitor_set_interests(VALUE self, VALUE interests)
167
169
  {
168
- if(NIL_P(interests)) {
170
+ if (NIL_P(interests)) {
169
171
  NIO_Monitor_update_interests(self, 0);
170
172
  } else {
171
173
  NIO_Monitor_update_interests(self, NIO_Monitor_symbol2interest(interests));
@@ -174,7 +176,8 @@ static VALUE NIO_Monitor_set_interests(VALUE self, VALUE interests)
174
176
  return rb_ivar_get(self, rb_intern("interests"));
175
177
  }
176
178
 
177
- static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest) {
179
+ static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest)
180
+ {
178
181
  struct NIO_Monitor *monitor;
179
182
  Data_Get_Struct(self, struct NIO_Monitor, monitor);
180
183
 
@@ -184,7 +187,8 @@ static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest) {
184
187
  return rb_ivar_get(self, rb_intern("interests"));
185
188
  }
186
189
 
187
- static VALUE NIO_Monitor_remove_interest(VALUE self, VALUE interest) {
190
+ static VALUE NIO_Monitor_remove_interest(VALUE self, VALUE interest)
191
+ {
188
192
  struct NIO_Monitor *monitor;
189
193
  Data_Get_Struct(self, struct NIO_Monitor, monitor);
190
194
 
@@ -214,11 +218,11 @@ static VALUE NIO_Monitor_readiness(VALUE self)
214
218
  struct NIO_Monitor *monitor;
215
219
  Data_Get_Struct(self, struct NIO_Monitor, monitor);
216
220
 
217
- if((monitor->revents & (EV_READ | EV_WRITE)) == (EV_READ | EV_WRITE)) {
221
+ if ((monitor->revents & (EV_READ | EV_WRITE)) == (EV_READ | EV_WRITE)) {
218
222
  return ID2SYM(rb_intern("rw"));
219
- } else if(monitor->revents & EV_READ) {
223
+ } else if (monitor->revents & EV_READ) {
220
224
  return ID2SYM(rb_intern("r"));
221
- } else if(monitor->revents & EV_WRITE) {
225
+ } else if (monitor->revents & EV_WRITE) {
222
226
  return ID2SYM(rb_intern("w"));
223
227
  } else {
224
228
  return Qnil;
@@ -230,7 +234,7 @@ static VALUE NIO_Monitor_is_readable(VALUE self)
230
234
  struct NIO_Monitor *monitor;
231
235
  Data_Get_Struct(self, struct NIO_Monitor, monitor);
232
236
 
233
- if(monitor->revents & EV_READ) {
237
+ if (monitor->revents & EV_READ) {
234
238
  return Qtrue;
235
239
  } else {
236
240
  return Qfalse;
@@ -242,7 +246,7 @@ static VALUE NIO_Monitor_is_writable(VALUE self)
242
246
  struct NIO_Monitor *monitor;
243
247
  Data_Get_Struct(self, struct NIO_Monitor, monitor);
244
248
 
245
- if(monitor->revents & EV_WRITE) {
249
+ if (monitor->revents & EV_WRITE) {
246
250
  return Qtrue;
247
251
  } else {
248
252
  return Qfalse;
@@ -256,15 +260,14 @@ static int NIO_Monitor_symbol2interest(VALUE interests)
256
260
  ID interests_id;
257
261
  interests_id = SYM2ID(interests);
258
262
 
259
- if(interests_id == rb_intern("r")) {
263
+ if (interests_id == rb_intern("r")) {
260
264
  return EV_READ;
261
- } else if(interests_id == rb_intern("w")) {
265
+ } else if (interests_id == rb_intern("w")) {
262
266
  return EV_WRITE;
263
- } else if(interests_id == rb_intern("rw")) {
267
+ } else if (interests_id == rb_intern("rw")) {
264
268
  return EV_READ | EV_WRITE;
265
269
  } 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)));
270
+ rb_raise(rb_eArgError, "invalid interest type %s (must be :r, :w, or :rw)", RSTRING_PTR(rb_funcall(interests, rb_intern("inspect"), 0)));
268
271
  }
269
272
  }
270
273
 
@@ -274,12 +277,12 @@ static void NIO_Monitor_update_interests(VALUE self, int interests)
274
277
  struct NIO_Monitor *monitor;
275
278
  Data_Get_Struct(self, struct NIO_Monitor, monitor);
276
279
 
277
- if(NIO_Monitor_is_closed(self) == Qtrue) {
280
+ if (NIO_Monitor_is_closed(self) == Qtrue) {
278
281
  rb_raise(rb_eEOFError, "monitor is closed");
279
282
  }
280
283
 
281
- if(interests) {
282
- switch(interests) {
284
+ if (interests) {
285
+ switch (interests) {
283
286
  case EV_READ:
284
287
  interests_id = rb_intern("r");
285
288
  break;
@@ -298,9 +301,9 @@ static void NIO_Monitor_update_interests(VALUE self, int interests)
298
301
  rb_ivar_set(self, rb_intern("interests"), Qnil);
299
302
  }
300
303
 
301
- if(monitor->interests != interests) {
304
+ if (monitor->interests != interests) {
302
305
  // If the monitor currently has interests, we should stop it.
303
- if(monitor->interests) {
306
+ if (monitor->interests) {
304
307
  ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io);
305
308
  }
306
309
 
@@ -309,7 +312,7 @@ static void NIO_Monitor_update_interests(VALUE self, int interests)
309
312
  ev_io_set(&monitor->ev_io, monitor->ev_io.fd, monitor->interests);
310
313
 
311
314
  // If we are interested in events, schedule the monitor back into the event loop:
312
- if(monitor->interests) {
315
+ if (monitor->interests) {
313
316
  ev_io_start(monitor->selector->ev_loop, &monitor->ev_io);
314
317
  }
315
318
  }
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,31 +23,27 @@ 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
43
  #ifdef GetReadFile
49
- # define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
44
+ #define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
50
45
  #else
51
- # define FPTR_TO_FD(fptr) fptr->fd
46
+ #define FPTR_TO_FD(fptr) fptr->fd
52
47
  #endif /* GetReadFile */
53
48
 
54
49
  /* Thunk between libev callbacks in NIO::Monitors and NIO::Selectors */
@@ -3,8 +3,8 @@
3
3
  * See LICENSE.txt for further details.
4
4
  */
5
5
 
6
- #include "nio4r.h"
7
6
  #include "../libev/ev.c"
7
+ #include "nio4r.h"
8
8
 
9
9
  void Init_NIO_Selector();
10
10
  void Init_NIO_Monitor();
@@ -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 = (SelectionKey)selectedKeys.next();
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 = (Map.Entry)cancelledKeys.next();
269
- SelectionKey key = (SelectionKey)entry.getValue();
266
+ Map.Entry<SelectableChannel, SelectionKey> entry = cancelledKeys.next();
267
+ SelectionKey key = entry.getValue();
270
268
  key.cancel();
271
269
  cancelledKeys.remove();
272
270
  }