nio4r 1.2.1-java → 2.0.0-java
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.
- checksums.yaml +4 -4
- data/.rspec +0 -1
- data/.rubocop.yml +31 -38
- data/.ruby-version +1 -0
- data/.travis.yml +15 -14
- data/CHANGES.md +75 -42
- data/Gemfile +10 -5
- data/Guardfile +10 -0
- data/LICENSE.txt +1 -1
- data/README.md +57 -161
- data/Rakefile +2 -1
- data/examples/echo_server.rb +1 -0
- data/ext/libev/Changes +4 -13
- data/ext/libev/ev.c +101 -74
- data/ext/libev/ev.h +3 -3
- data/ext/libev/ev_epoll.c +6 -3
- data/ext/libev/ev_kqueue.c +8 -4
- data/ext/libev/ev_poll.c +6 -3
- data/ext/libev/ev_port.c +8 -4
- data/ext/libev/ev_select.c +4 -2
- data/ext/nio4r/bytebuffer.c +421 -0
- data/ext/nio4r/extconf.rb +2 -10
- data/ext/nio4r/monitor.c +93 -46
- data/ext/nio4r/nio4r.h +11 -13
- data/ext/nio4r/org/nio4r/ByteBuffer.java +295 -0
- data/ext/nio4r/org/nio4r/Monitor.java +164 -0
- data/ext/nio4r/org/nio4r/Nio4r.java +22 -391
- data/ext/nio4r/org/nio4r/Selector.java +278 -0
- data/ext/nio4r/selector.c +55 -53
- data/lib/nio.rb +4 -3
- data/lib/nio/bytebuffer.rb +222 -0
- data/lib/nio/monitor.rb +64 -4
- data/lib/nio/selector.rb +52 -20
- data/lib/nio/version.rb +1 -1
- data/nio4r.gemspec +25 -19
- data/spec/nio/acceptables_spec.rb +6 -4
- data/spec/nio/bytebuffer_spec.rb +349 -0
- data/spec/nio/monitor_spec.rb +122 -79
- data/spec/nio/selectables/pipe_spec.rb +5 -1
- data/spec/nio/selectables/ssl_socket_spec.rb +15 -12
- data/spec/nio/selectables/tcp_socket_spec.rb +42 -31
- data/spec/nio/selectables/udp_socket_spec.rb +2 -0
- data/spec/nio/selector_spec.rb +10 -4
- data/spec/spec_helper.rb +24 -3
- data/spec/support/selectable_examples.rb +7 -5
- data/tasks/extension.rake +2 -0
- data/tasks/rspec.rake +2 -0
- data/tasks/rubocop.rake +2 -0
- metadata +21 -14
- data/.rubocop_todo.yml +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2685d0b193772fb648d55b9ce46f6ce0ebffa646
|
4
|
+
data.tar.gz: babb78d3c1e768b192b8b51062ac23eb4d94270e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71ceb314a13ec706fd72168850787bf2dd7942fe66b0cdbbf1d9b5bce4f4977a5dbcd9b4f0c04ab6f8daf3769f4a3e7daebea714c1b609df4371472082f63216
|
7
|
+
data.tar.gz: e16b5359765c62a6fcc443ea26c3bad5e7466b7e3d6c4cc93f8dc2993a872a13887bb3756d1ec6581dfc77a1a1e79798cce960042a830e2b6732c9ff2931c8b0
|
data/.rspec
CHANGED
data/.rubocop.yml
CHANGED
@@ -1,61 +1,54 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisplayCopNames: true
|
3
|
+
|
1
4
|
#
|
2
|
-
#
|
5
|
+
# Lint
|
3
6
|
#
|
4
7
|
|
5
|
-
|
6
|
-
|
8
|
+
Lint/HandleExceptions:
|
9
|
+
Enabled: false
|
7
10
|
|
8
|
-
|
9
|
-
|
11
|
+
Lint/Loop:
|
12
|
+
Enabled: false
|
10
13
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
+
#
|
15
|
+
# Metrics
|
16
|
+
#
|
14
17
|
|
15
18
|
Metrics/AbcSize:
|
16
|
-
Max:
|
19
|
+
Max: 35
|
17
20
|
|
18
|
-
Metrics/
|
19
|
-
Max:
|
21
|
+
Metrics/ClassLength:
|
22
|
+
Max: 128
|
20
23
|
|
21
|
-
Metrics/
|
22
|
-
Max:
|
24
|
+
Metrics/LineLength:
|
25
|
+
Max: 128
|
23
26
|
|
24
|
-
|
25
|
-
|
27
|
+
Metrics/MethodLength:
|
28
|
+
CountComments: false
|
29
|
+
Max: 50
|
26
30
|
|
27
|
-
|
28
|
-
|
31
|
+
Metrics/CyclomaticComplexity:
|
32
|
+
Max: 15
|
33
|
+
|
34
|
+
Metrics/PerceivedComplexity:
|
35
|
+
Max: 15
|
29
36
|
|
30
37
|
#
|
31
|
-
#
|
38
|
+
# Style
|
32
39
|
#
|
33
40
|
|
34
|
-
|
35
|
-
|
36
|
-
Enabled: false
|
37
|
-
|
38
|
-
# Offense count: 3
|
39
|
-
Lint/Loop:
|
40
|
-
Enabled: false
|
41
|
+
Style/StringLiterals:
|
42
|
+
EnforcedStyle: double_quotes
|
41
43
|
|
42
|
-
|
43
|
-
Lint/NonLocalExitFromIterator:
|
44
|
+
Style/GlobalVars:
|
44
45
|
Enabled: false
|
45
46
|
|
46
|
-
|
47
|
-
Lint/UselessAssignment:
|
47
|
+
Style/NumericPredicate:
|
48
48
|
Enabled: false
|
49
49
|
|
50
|
-
|
51
|
-
# Configuration parameters: SupportedStyles, IndentOneStep.
|
52
|
-
Style/CaseIndentation:
|
53
|
-
IndentWhenRelativeTo: end
|
54
|
-
|
55
|
-
# Offense count: 1
|
56
|
-
Style/Documentation:
|
50
|
+
Style/RescueModifier:
|
57
51
|
Enabled: false
|
58
52
|
|
59
|
-
|
60
|
-
Style/RescueModifier:
|
53
|
+
Style/TrivialAccessors:
|
61
54
|
Enabled: false
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.0
|
data/.travis.yml
CHANGED
@@ -1,27 +1,28 @@
|
|
1
1
|
language: ruby
|
2
2
|
sudo: false
|
3
|
+
cache: bundler
|
4
|
+
|
5
|
+
bundler_args: --without development
|
6
|
+
|
7
|
+
branches:
|
8
|
+
only:
|
9
|
+
- master
|
3
10
|
|
4
11
|
rvm:
|
5
|
-
-
|
6
|
-
- 2.1.1
|
12
|
+
- jruby-9.1.6.0 # latest stable
|
7
13
|
- 2.2.2
|
8
|
-
-
|
9
|
-
-
|
10
|
-
- jruby-head
|
11
|
-
- rbx-2
|
14
|
+
- 2.3.3
|
15
|
+
- 2.4.0
|
12
16
|
|
13
17
|
env:
|
14
|
-
|
15
|
-
|
18
|
+
global:
|
19
|
+
- JRUBY_OPTS="--dev -J-Djruby.launch.inproc=true -J-Xmx1024M"
|
20
|
+
matrix:
|
21
|
+
- NIO4R_PURE=false
|
22
|
+
- NIO4R_PURE=true
|
16
23
|
|
17
24
|
matrix:
|
18
25
|
fast_finish: true
|
19
|
-
allow_failures:
|
20
|
-
- rvm: ruby-head
|
21
|
-
- rvm: jruby-head
|
22
|
-
- rvm: rbx-2
|
23
|
-
- rvm: jruby
|
24
|
-
env: NIO4R_PURE=true
|
25
26
|
|
26
27
|
notifications:
|
27
28
|
irc: "irc.freenode.org#celluloid"
|
data/CHANGES.md
CHANGED
@@ -1,28 +1,57 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
## 2.0.0 (2016-12-28)
|
2
|
+
|
3
|
+
* [#53](https://github.com/socketry/nio4r/pull/53)
|
4
|
+
Limit lock scope to prevent recursive locking.
|
5
|
+
([@johnnyt])
|
6
|
+
|
7
|
+
* [#95](https://github.com/socketry/nio4r/pull/95)
|
8
|
+
NIO::ByteBuffer Google Summer of Code project.
|
9
|
+
([@UpeksheJay], [@tarcieri])
|
10
|
+
|
11
|
+
* [#111](https://github.com/socketry/nio4r/pull/111)
|
12
|
+
NIO::Selector#backend introspection support.
|
13
|
+
([@tarcieri])
|
14
|
+
|
15
|
+
* [#112](https://github.com/socketry/nio4r/pull/112)
|
16
|
+
Upgrade to libev 4.23.
|
17
|
+
([@tarcieri])
|
18
|
+
|
19
|
+
* [#119](https://github.com/socketry/nio4r/pull/119)
|
20
|
+
Disambiguate wakeup vs timeout (fixes #63, #66).
|
21
|
+
([@tarcieri])
|
22
|
+
|
23
|
+
* [#124](https://github.com/socketry/nio4r/pull/124)
|
24
|
+
Monitor interests API improvements.
|
25
|
+
([@tarcieri])
|
26
|
+
|
27
|
+
* Drop Ruby 2.0 and 2.1 support, require Ruby 2.2.2+.
|
28
|
+
([@tarcieri])
|
29
|
+
|
30
|
+
## 1.2.1 (2016-01-31)
|
31
|
+
|
3
32
|
* Fix bug in the JRuby backend which cases indefinite blocking when small
|
4
33
|
timeout values are passed to the selector
|
5
34
|
|
6
|
-
1.2.0 (2015-12-22)
|
7
|
-
|
35
|
+
## 1.2.0 (2015-12-22)
|
36
|
+
|
8
37
|
* Add NIO::Monitor#interests= API for changing interests. Contributed by
|
9
38
|
Upekshe Jayasekera as a Google Summer of Code project.
|
10
39
|
* Update to libev 4.22
|
11
40
|
|
12
|
-
1.1.1 (2015-07-17)
|
13
|
-
|
41
|
+
## 1.1.1 (2015-07-17)
|
42
|
+
|
14
43
|
* Update to libev 4.20
|
15
44
|
* Fall back to io.h if unistd.h is not found
|
16
45
|
* RSpec updates
|
17
46
|
* RuboCop
|
18
47
|
|
19
|
-
1.1.0 (2015-01-10)
|
20
|
-
|
48
|
+
## 1.1.0 (2015-01-10)
|
49
|
+
|
21
50
|
* Update to libev 4.19
|
22
51
|
* Do not call ev_io_stop on monitors if the loop is already closed
|
23
52
|
|
24
|
-
1.0.1 (2014-09-01)
|
25
|
-
|
53
|
+
## 1.0.1 (2014-09-01)
|
54
|
+
|
26
55
|
* Fix C compiler warnings
|
27
56
|
* Eliminate Ruby warnings about @lock_holder
|
28
57
|
* Windows improvements
|
@@ -30,59 +59,59 @@
|
|
30
59
|
* Automatically require 'set'
|
31
60
|
* Update to RSpec 3
|
32
61
|
|
33
|
-
1.0.0 (2014-01-14)
|
34
|
-
|
62
|
+
## 1.0.0 (2014-01-14)
|
63
|
+
|
35
64
|
* Have Selector#register obtain the actual IO from a Monitor object
|
36
65
|
because Monitor#initialize might convert it.
|
37
66
|
* Drop 1.8 support
|
38
67
|
|
39
|
-
0.5.0 (2013-08-06)
|
40
|
-
|
68
|
+
## 0.5.0 (2013-08-06)
|
69
|
+
|
41
70
|
* Fix segv when attempting to register to a closed selector
|
42
71
|
* Fix Windows support on Ruby 2.0.0
|
43
72
|
* Upgrade to libev 4.15
|
44
73
|
|
45
|
-
0.4.6 (2013-05-27)
|
46
|
-
|
74
|
+
## 0.4.6 (2013-05-27)
|
75
|
+
|
47
76
|
* Fix for JRuby on Windows
|
48
77
|
|
49
|
-
0.4.5
|
50
|
-
|
78
|
+
## 0.4.5
|
79
|
+
|
51
80
|
* Fix botched gem release
|
52
81
|
|
53
|
-
0.4.4
|
54
|
-
|
82
|
+
## 0.4.4
|
83
|
+
|
55
84
|
* Fix return values for Selector_synchronize and Selector_unlock
|
56
85
|
|
57
|
-
0.4.3
|
58
|
-
|
86
|
+
## 0.4.3
|
87
|
+
|
59
88
|
* REALLY have thread synchronization when closing selectors ;)
|
60
89
|
|
61
|
-
0.4.2
|
62
|
-
|
90
|
+
## 0.4.2
|
91
|
+
|
63
92
|
* Attempt to work around packaging problems with bundler-api o_O
|
64
93
|
|
65
|
-
0.4.1
|
66
|
-
|
94
|
+
## 0.4.1
|
95
|
+
|
67
96
|
* Thread synchronization when closing selectors
|
68
97
|
|
69
|
-
0.4.0
|
70
|
-
|
98
|
+
## 0.4.0
|
99
|
+
|
71
100
|
* OpenSSL::SSL::SSLSocket support
|
72
101
|
|
73
|
-
0.3.3
|
74
|
-
|
102
|
+
## 0.3.3
|
103
|
+
|
75
104
|
* NIO::Selector#select_each removed
|
76
105
|
* Remove event buffer
|
77
106
|
* Patch GIL unlock directly into libev
|
78
107
|
* Re-release since 0.3.2 was botched :(
|
79
108
|
|
80
|
-
0.3.1
|
81
|
-
|
109
|
+
## 0.3.1
|
110
|
+
|
82
111
|
* Prevent CancelledKeyExceptions on JRuby
|
83
112
|
|
84
|
-
0.3.0
|
85
|
-
|
113
|
+
## 0.3.0
|
114
|
+
|
86
115
|
* NIO::Selector#select now takes a block and behaves like select_each
|
87
116
|
* NIO::Selector#select_each is now deprecated and will be removed
|
88
117
|
* Closing monitors detaches them from their selector
|
@@ -91,23 +120,27 @@
|
|
91
120
|
* Bugfixes for zero/negative select timeouts
|
92
121
|
* Handle OP_CONNECT properly on JRuby
|
93
122
|
|
94
|
-
0.2.2
|
95
|
-
|
123
|
+
## 0.2.2
|
124
|
+
|
96
125
|
* Raise IOError if asked to wake up a closed selector
|
97
126
|
|
98
|
-
0.2.1
|
99
|
-
|
127
|
+
## 0.2.1
|
128
|
+
|
100
129
|
* Implement wakeup mechanism using raw pipes instead of ev_async, since
|
101
130
|
ev_async likes to cause segvs when used across threads (despite claims
|
102
131
|
in the documentation to the contrary)
|
103
132
|
|
104
|
-
0.2.0
|
105
|
-
|
133
|
+
## 0.2.0
|
134
|
+
|
106
135
|
* NIO::Monitor#readiness API to query readiness, along with #readable? and
|
107
136
|
#writable? helper methods
|
108
137
|
* NIO::Selector#select_each API which avoids memory allocations if possible
|
109
138
|
* Bugfixes for the JRuby implementation
|
110
139
|
|
111
|
-
0.1.0
|
112
|
-
|
140
|
+
## 0.1.0
|
141
|
+
|
113
142
|
* Initial release. Merry Christmas!
|
143
|
+
|
144
|
+
[@tarcieri]: https://github.com/tarcieri
|
145
|
+
[@johnnyt]: https://github.com/johnnyt
|
146
|
+
[@UpeksheJay]: https://github.com/UpeksheJay
|
data/Gemfile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
source "https://rubygems.org"
|
2
4
|
|
3
5
|
gemspec
|
@@ -5,11 +7,14 @@ gemspec
|
|
5
7
|
gem "jruby-openssl" if defined? JRUBY_VERSION
|
6
8
|
|
7
9
|
group :development do
|
8
|
-
gem "
|
10
|
+
gem "guard-rspec", require: false
|
11
|
+
gem "pry", require: false
|
9
12
|
end
|
10
13
|
|
11
|
-
group :test do
|
12
|
-
gem "
|
13
|
-
gem "
|
14
|
-
gem "
|
14
|
+
group :development, :test do
|
15
|
+
gem "coveralls", require: false
|
16
|
+
gem "rake-compiler", require: false
|
17
|
+
gem "rspec", "~> 3", require: false
|
18
|
+
gem "rspec-retry", require: false
|
19
|
+
gem "rubocop", "0.46.0", require: false
|
15
20
|
end
|
data/Guardfile
ADDED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,196 +1,92 @@
|
|
1
|
-

|
2
|
+
|
3
3
|
[](http://rubygems.org/gems/nio4r)
|
4
|
-
[](https://github.com/
|
4
|
+
[](http://travis-ci.org/socketry/nio4r)
|
5
|
+
[](https://codeclimate.com/github/socketry/nio4r)
|
6
|
+
[](https://coveralls.io/r/socketry/nio4r)
|
7
|
+
[](https://github.com/socketry/nio4r/blob/master/LICENSE.txt)
|
8
|
+
|
9
|
+
_NOTE: This is the 2.x **development** branch of nio4r. For the 1.x **stable**
|
10
|
+
branch (used by [Rails 5]), please see:_
|
8
11
|
|
9
|
-
nio4r
|
12
|
+
https://github.com/socketry/nio4r/tree/1-x-stable
|
10
13
|
|
11
|
-
|
14
|
+
**New I/O for Ruby (nio4r)**: cross-platform asynchronous I/O primitives for
|
15
|
+
scalable network clients and servers. Modeled after the Java NIO API, but
|
16
|
+
simplified for ease-of-use.
|
17
|
+
|
18
|
+
**nio4r** provides an abstract, cross-platform stateful I/O selector API for Ruby.
|
12
19
|
I/O selectors are the heart of "reactor"-based event loops, and monitor
|
13
20
|
multiple I/O objects for various types of readiness, e.g. ready for reading or
|
14
21
|
writing.
|
15
22
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
you register I/O objects with a selector then handle them when they're selected
|
20
|
-
for various types of events.
|
23
|
+
[Rails 5]: https://rubygems.org/gems/actioncable
|
24
|
+
|
25
|
+
## Projects using nio4r
|
21
26
|
|
22
|
-
|
27
|
+
* [ActionCable]: Rails 5 WebSocket protocol, uses nio4r for a WebSocket server
|
28
|
+
* [Celluloid::IO]: Actor-based concurrency framework, uses nio4r for async I/O
|
23
29
|
|
24
|
-
|
30
|
+
[ActionCable]: https://rubygems.org/gems/actioncable
|
31
|
+
[Celluloid::IO]: https://github.com/socketry/socketry-io
|
32
|
+
|
33
|
+
## Goals
|
25
34
|
|
26
35
|
* Expose high-level interfaces for stateful IO selectors
|
27
36
|
* Keep the API small to maximize both portability and performance across many
|
28
37
|
different OSes and Ruby VMs
|
29
38
|
* Provide inherently thread-safe facilities for working with IO objects
|
30
39
|
|
31
|
-
|
32
|
-
monitor multiple IO objects from a single Celluloid actor.
|
33
|
-
|
34
|
-
Supported Platforms
|
35
|
-
-------------------
|
36
|
-
|
37
|
-
nio4r is known to work on the following Ruby implementations:
|
38
|
-
|
39
|
-
* MRI/YARV 1.9.3, 2.0.0, 2.1.0
|
40
|
-
* JRuby 1.7.x
|
41
|
-
* Rubinius 2.x
|
42
|
-
* A pure Ruby implementation based on Kernel.select is also provided
|
43
|
-
|
44
|
-
Platform notes:
|
45
|
-
|
46
|
-
* MRI/YARV and Rubinius implement nio4r with a C extension based on libev,
|
47
|
-
which provides a high performance binding to native IO APIs
|
48
|
-
* JRuby uses a Java extension based on the high performance Java NIO subsystem
|
49
|
-
* A pure Ruby implementation is also provided for Ruby implementations which
|
50
|
-
don't implement the MRI C extension API
|
51
|
-
|
52
|
-
Usage
|
53
|
-
-----
|
40
|
+
## Supported platforms
|
54
41
|
|
55
|
-
|
42
|
+
* Ruby 2.2.2+
|
43
|
+
* Ruby 2.3
|
44
|
+
* Ruby 2.4
|
45
|
+
* JRuby 9000
|
56
46
|
|
57
|
-
|
58
|
-
might otherwise use Kernel.select, but want to monitor the same set of IO
|
59
|
-
objects across multiple select calls rather than having to reregister them
|
60
|
-
every single time:
|
61
|
-
|
62
|
-
```ruby
|
63
|
-
require 'nio'
|
47
|
+
## Supported backends
|
64
48
|
|
65
|
-
selector
|
66
|
-
|
67
|
-
|
68
|
-
To monitor IO objects, attach them to the selector with the NIO::Selector#register
|
69
|
-
method, monitoring them for read readiness with the :r parameter, write
|
70
|
-
readiness with the :w parameter, or both with :rw.
|
49
|
+
* **libev**: MRI C extension targeting multiple native IO selector APIs (e.g epoll, kqueue)
|
50
|
+
* **Java NIO**: JRuby extension which wraps the Java NIO subsystem
|
51
|
+
* **Pure Ruby**: `Kernel.select`-based backend that should work on any Ruby interpreter
|
71
52
|
|
72
|
-
|
73
|
-
>> reader, writer = IO.pipe
|
74
|
-
=> [#<IO:0xf30>, #<IO:0xf34>]
|
75
|
-
>> monitor = selector.register(reader, :r)
|
76
|
-
=> #<NIO::Monitor:0xfbc>
|
77
|
-
```
|
53
|
+
## Documentation
|
78
54
|
|
79
|
-
|
80
|
-
|
81
|
-
monitored. Monitors will store an arbitrary value of your choice, which
|
82
|
-
provides an easy way to implement callbacks:
|
55
|
+
[Please see the nio4r wiki](https://github.com/socketry/nio4r/wiki)
|
56
|
+
for more detailed documentation and usage notes:
|
83
57
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
=> #<Proc:0x1000@(irb):4>
|
89
|
-
```
|
58
|
+
* [Getting Started]: Introduction to nio4r's components
|
59
|
+
* [Selectors]: monitor multiple `IO` objects for readiness events
|
60
|
+
* [Monitors]: control interests and inspect readiness for specific `IO` objects
|
61
|
+
* [Byte Buffers]: fixed-size native buffers for high-performance I/O
|
90
62
|
|
91
|
-
|
92
|
-
|
63
|
+
[Getting Started]: https://github.com/socketry/nio4r/wiki/Getting-Started
|
64
|
+
[Selectors]: https://github.com/socketry/nio4r/wiki/Selectors
|
65
|
+
[Monitors]: https://github.com/socketry/nio4r/wiki/Monitors
|
66
|
+
[Byte Buffers]: https://github.com/socketry/nio4r/wiki/Byte-Buffers
|
93
67
|
|
94
|
-
|
95
|
-
>> writer << "Hi there!"
|
96
|
-
=> #<IO:0x103c>
|
97
|
-
>> ready = selector.select
|
98
|
-
=> [#<NIO::Monitor:0xfbc>]
|
99
|
-
>> ready.each { |m| m.value.call }
|
100
|
-
Got some data: Hi there!
|
101
|
-
=> [#<NIO::Monitor:0xfbc>]
|
102
|
-
```
|
68
|
+
See also:
|
103
69
|
|
104
|
-
|
105
|
-
objects being monitored becomes ready. However, you can also pass a timeout to
|
106
|
-
wait in seconds to NIO::Selector#select just like you can with Kernel.select:
|
70
|
+
* [YARD API documentation](http://www.rubydoc.info/gems/nio4r/frames)
|
107
71
|
|
108
|
-
|
109
|
-
ready = selector.select(15) # Wait 15 seconds
|
110
|
-
```
|
72
|
+
## Non-goals
|
111
73
|
|
112
|
-
|
113
|
-
|
114
|
-
You can avoid allocating an array each time you call NIO::Selector#select by
|
115
|
-
passing a block to select. The block will be called for each ready monitor
|
116
|
-
object, with that object passed as an argument. The number of ready monitors
|
117
|
-
is returned as a Fixnum:
|
118
|
-
|
119
|
-
```ruby
|
120
|
-
>> selector.select { |m| m.value.call }
|
121
|
-
Got some data: Hi there!
|
122
|
-
=> 1
|
123
|
-
```
|
124
|
-
|
125
|
-
When you're done monitoring a particular IO object, just deregister it from
|
126
|
-
the selector:
|
127
|
-
|
128
|
-
```ruby
|
129
|
-
selector.deregister(reader)
|
130
|
-
```
|
131
|
-
|
132
|
-
### Monitors
|
133
|
-
|
134
|
-
Monitors provide methods which let you introspect on why a particular IO
|
135
|
-
object was selected. These methods are not thread safe unless you are holding
|
136
|
-
the selector lock (i.e. if you're in a block passed to #select). Only use them
|
137
|
-
if you aren't concerned with thread safety, or you're within a #select
|
138
|
-
block:
|
139
|
-
|
140
|
-
- ***#interests***: what this monitor is interested in (:r, :w, or :rw)
|
141
|
-
- ***#interests=***: change the current interests for a monitor (to :r, :w, or :rw)
|
142
|
-
- ***#readiness***: what I/O operations the monitored object is ready for
|
143
|
-
- ***#readable?***: was the IO readable last time it was selected?
|
144
|
-
- ***#writable?***: was the IO writable last time it was selected?
|
145
|
-
|
146
|
-
Monitors also support a ***#value*** and ***#value=*** method for storing a
|
147
|
-
handle to an arbitrary object of your choice (e.g. a proc)
|
148
|
-
|
149
|
-
### Flow Control
|
150
|
-
|
151
|
-
For information on how to compose nio4r selectors inside of event loops,
|
152
|
-
please read the [Flow Control Guide on the
|
153
|
-
Wiki](https://github.com/celluloid/nio4r/wiki/Basic-Flow-Control)
|
154
|
-
|
155
|
-
Concurrency
|
156
|
-
-----------
|
157
|
-
|
158
|
-
nio4r provides internal locking to ensure that it's safe to use from multiple
|
159
|
-
concurrent threads. Only one thread can select on a NIO::Selector at a given
|
160
|
-
time, and while a thread is selecting other threads are blocked from
|
161
|
-
registering or deregistering IO objects. Once a pending select has completed,
|
162
|
-
requests to register/unregister IO objects will be processed.
|
163
|
-
|
164
|
-
NIO::Selector#wakeup allows one thread to unblock another thread that's in the
|
165
|
-
middle of an NIO::Selector#select operation. This lets other threads that need
|
166
|
-
to communicate immediately with the selector unblock it so it can process
|
167
|
-
other events that it's not presently selecting on.
|
168
|
-
|
169
|
-
What nio4r is not
|
170
|
-
-----------------
|
171
|
-
|
172
|
-
nio4r is not a full-featured event framework like EventMachine or Cool.io.
|
74
|
+
**nio4r** is not a full-featured event framework like [EventMachine] or [Cool.io].
|
173
75
|
Instead, nio4r is the sort of thing you might write a library like that on
|
174
76
|
top of. nio4r provides a minimal API such that individual Ruby implementers
|
175
77
|
may choose to produce optimized versions for their platform, without having
|
176
78
|
to maintain a large codebase.
|
177
79
|
|
178
|
-
|
80
|
+
[EventMachine]: https://github.com/eventmachine/eventmachine
|
81
|
+
[Cool.io]: https://coolio.github.io/
|
179
82
|
|
180
|
-
|
181
|
-
* 700 lines of "custom" C code (not counting libev)
|
182
|
-
* 400 lines of Java code
|
183
|
-
|
184
|
-
nio4r is also not a replacement for Kinder Gentler IO (KGIO), a set of
|
185
|
-
advanced Ruby IO APIs. At some point in the future nio4r might provide a
|
186
|
-
cross-platform implementation that uses KGIO on CRubies, and Java NIO on JRuby,
|
187
|
-
however this is not the case today.
|
188
|
-
|
189
|
-
License
|
190
|
-
-------
|
83
|
+
## License
|
191
84
|
|
192
85
|
Copyright (c) 2011-2016 Tony Arcieri. Distributed under the MIT License.
|
193
|
-
See LICENSE.txt for further details.
|
86
|
+
See [LICENSE.txt] for further details.
|
87
|
+
|
88
|
+
Includes libev 4.23. Copyright (c) 2007-2016 Marc Alexander Lehmann.
|
89
|
+
Distributed under the BSD license. See [ext/libev/LICENSE] for details.
|
194
90
|
|
195
|
-
|
196
|
-
|
91
|
+
[LICENSE.txt]: https://github.com/socketry/nio4r/blob/master/LICENSE.txt
|
92
|
+
[ext/libev/LICENSE]: https://github.com/socketry/nio4r/blob/master/ext/libev/LICENSE
|