libuv 0.10.0 → 0.10.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -17
- data/.gitmodules +3 -3
- data/.rspec +1 -1
- data/.travis.yml +16 -16
- data/Gemfile +2 -2
- data/LICENSE +23 -23
- data/README.md +82 -73
- data/Rakefile +31 -31
- data/lib/libuv.rb +53 -34
- data/lib/libuv/async.rb +47 -33
- data/lib/libuv/check.rb +55 -48
- data/lib/libuv/error.rb +70 -70
- data/lib/libuv/ext/ext.rb +264 -256
- data/lib/libuv/ext/platform/darwin_x64.rb +12 -12
- data/lib/libuv/ext/platform/linux.rb +7 -7
- data/lib/libuv/ext/platform/unix.rb +13 -13
- data/lib/libuv/ext/platform/windows.rb +26 -26
- data/lib/libuv/ext/tasks.rb +27 -27
- data/lib/libuv/ext/tasks/mac.rb +23 -23
- data/lib/libuv/ext/tasks/unix.rb +23 -23
- data/lib/libuv/ext/tasks/win.rb +11 -11
- data/lib/libuv/ext/types.rb +234 -229
- data/lib/libuv/file.rb +192 -0
- data/lib/libuv/filesystem.rb +233 -0
- data/lib/libuv/fs_event.rb +31 -31
- data/lib/libuv/handle.rb +85 -81
- data/lib/libuv/idle.rb +56 -49
- data/lib/libuv/loop.rb +338 -310
- data/lib/libuv/{assertions.rb → mixins/assertions.rb} +23 -23
- data/lib/libuv/mixins/fs_checks.rb +55 -0
- data/lib/libuv/{listener.rb → mixins/listener.rb} +34 -34
- data/lib/libuv/{net.rb → mixins/net.rb} +37 -37
- data/lib/libuv/{resource.rb → mixins/resource.rb} +27 -27
- data/lib/libuv/{stream.rb → mixins/stream.rb} +143 -123
- data/lib/libuv/pipe.rb +197 -97
- data/lib/libuv/prepare.rb +56 -49
- data/lib/libuv/q.rb +1 -1
- data/lib/libuv/signal.rb +51 -0
- data/lib/libuv/tcp.rb +204 -193
- data/lib/libuv/timer.rb +88 -75
- data/lib/libuv/tty.rb +37 -34
- data/lib/libuv/udp.rb +273 -255
- data/lib/libuv/version.rb +3 -3
- data/lib/libuv/work.rb +63 -61
- data/libuv.gemspec +54 -54
- data/spec/async_spec.rb +60 -60
- data/spec/cpu_spec.rb +10 -0
- data/spec/defer_spec.rb +980 -980
- data/spec/filesystem_spec.rb +119 -0
- data/spec/idle_spec.rb +56 -56
- data/spec/pipe_spec.rb +153 -148
- data/spec/tcp_spec.rb +203 -188
- metadata +73 -49
- checksums.yaml +0 -15
- data/lib/libuv/simple_async.rb +0 -28
data/lib/libuv/tcp.rb
CHANGED
@@ -1,194 +1,205 @@
|
|
1
|
-
require 'ipaddr'
|
2
|
-
|
3
|
-
|
4
|
-
module Libuv
|
5
|
-
class TCP < Handle
|
6
|
-
include Stream, Net
|
7
|
-
|
8
|
-
|
9
|
-
KEEPALIVE_ARGUMENT_ERROR = "delay must be an Integer".freeze
|
10
|
-
|
11
|
-
|
12
|
-
def initialize(loop, acceptor = nil)
|
13
|
-
@loop = loop
|
14
|
-
|
15
|
-
tcp_ptr = ::Libuv::Ext.create_handle(:uv_tcp)
|
16
|
-
error = check_result(::Libuv::Ext.tcp_init(loop.handle, tcp_ptr))
|
17
|
-
error = check_result(::Libuv::Ext.accept(acceptor, tcp_ptr)) if acceptor && error.nil?
|
18
|
-
|
19
|
-
super(tcp_ptr, error)
|
20
|
-
end
|
21
|
-
|
22
|
-
def bind(ip, port, callback = nil, &blk)
|
23
|
-
|
24
|
-
|
25
|
-
assert_type(
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
@tcp_socket.
|
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
|
-
sockaddr, len
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
check_result ::Libuv::Ext.
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
check_result ::Libuv::Ext.
|
96
|
-
end
|
97
|
-
|
98
|
-
def
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
1
|
+
require 'ipaddr'
|
2
|
+
|
3
|
+
|
4
|
+
module Libuv
|
5
|
+
class TCP < Handle
|
6
|
+
include Stream, Net
|
7
|
+
|
8
|
+
|
9
|
+
KEEPALIVE_ARGUMENT_ERROR = "delay must be an Integer".freeze
|
10
|
+
|
11
|
+
|
12
|
+
def initialize(loop, acceptor = nil)
|
13
|
+
@loop = loop
|
14
|
+
|
15
|
+
tcp_ptr = ::Libuv::Ext.create_handle(:uv_tcp)
|
16
|
+
error = check_result(::Libuv::Ext.tcp_init(loop.handle, tcp_ptr))
|
17
|
+
error = check_result(::Libuv::Ext.accept(acceptor, tcp_ptr)) if acceptor && error.nil?
|
18
|
+
|
19
|
+
super(tcp_ptr, error)
|
20
|
+
end
|
21
|
+
|
22
|
+
def bind(ip, port, callback = nil, &blk)
|
23
|
+
return if @closed
|
24
|
+
@on_listen = callback || blk
|
25
|
+
assert_type(String, ip, IP_ARGUMENT_ERROR)
|
26
|
+
assert_type(Integer, port, PORT_ARGUMENT_ERROR)
|
27
|
+
|
28
|
+
begin
|
29
|
+
@tcp_socket = create_socket(IPAddr.new(ip), port)
|
30
|
+
@tcp_socket.bind
|
31
|
+
rescue Exception => e
|
32
|
+
reject(e)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def accept(callback = nil, &blk)
|
37
|
+
tcp = nil
|
38
|
+
begin
|
39
|
+
raise RuntimeError, CLOSED_HANDLE_ERROR if @closed
|
40
|
+
tcp = TCP.new(loop, handle)
|
41
|
+
rescue Exception => e
|
42
|
+
@loop.log :info, :tcp_accept_failed, e
|
43
|
+
end
|
44
|
+
if tcp
|
45
|
+
begin
|
46
|
+
(callback || blk).call(tcp)
|
47
|
+
rescue Exception => e
|
48
|
+
@loop.log :error, :tcp_accept_cb, e
|
49
|
+
end
|
50
|
+
end
|
51
|
+
nil
|
52
|
+
end
|
53
|
+
|
54
|
+
def connect(ip, port, callback = nil, &blk)
|
55
|
+
return if @closed
|
56
|
+
@callback = callback || blk
|
57
|
+
assert_type(String, ip, IP_ARGUMENT_ERROR)
|
58
|
+
assert_type(Integer, port, PORT_ARGUMENT_ERROR)
|
59
|
+
|
60
|
+
begin
|
61
|
+
@tcp_socket = create_socket(IPAddr.new(ip), port)
|
62
|
+
@tcp_socket.connect(callback(:on_connect))
|
63
|
+
rescue Exception => e
|
64
|
+
reject(e)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def sockname
|
69
|
+
return [] if @closed
|
70
|
+
sockaddr, len = get_sockaddr_and_len
|
71
|
+
check_result! ::Libuv::Ext.tcp_getsockname(handle, sockaddr, len)
|
72
|
+
get_ip_and_port(::Libuv::Sockaddr.new(sockaddr), len.get_int(0))
|
73
|
+
end
|
74
|
+
|
75
|
+
def peername
|
76
|
+
return [] if @closed
|
77
|
+
sockaddr, len = get_sockaddr_and_len
|
78
|
+
check_result! ::Libuv::Ext.tcp_getpeername(handle, sockaddr, len)
|
79
|
+
get_ip_and_port(::Libuv::Sockaddr.new(sockaddr), len.get_int(0))
|
80
|
+
end
|
81
|
+
|
82
|
+
def enable_nodelay
|
83
|
+
return if @closed
|
84
|
+
check_result ::Libuv::Ext.tcp_nodelay(handle, 1)
|
85
|
+
end
|
86
|
+
|
87
|
+
def disable_nodelay
|
88
|
+
return if @closed
|
89
|
+
check_result ::Libuv::Ext.tcp_nodelay(handle, 0)
|
90
|
+
end
|
91
|
+
|
92
|
+
def enable_keepalive(delay)
|
93
|
+
return if @closed
|
94
|
+
assert_type(Integer, delay, KEEPALIVE_ARGUMENT_ERROR)
|
95
|
+
check_result ::Libuv::Ext.tcp_keepalive(handle, 1, delay)
|
96
|
+
end
|
97
|
+
|
98
|
+
def disable_keepalive
|
99
|
+
return if @closed
|
100
|
+
check_result ::Libuv::Ext.tcp_keepalive(handle, 0, 0)
|
101
|
+
end
|
102
|
+
|
103
|
+
def enable_simultaneous_accepts
|
104
|
+
return if @closed
|
105
|
+
check_result ::Libuv::Ext.tcp_simultaneous_accepts(handle, 1)
|
106
|
+
end
|
107
|
+
|
108
|
+
def disable_simultaneous_accepts
|
109
|
+
return if @closed
|
110
|
+
check_result ::Libuv::Ext.tcp_simultaneous_accepts(handle, 0)
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
|
117
|
+
def create_socket(ip, port)
|
118
|
+
if ip.ipv4?
|
119
|
+
Socket4.new(loop, handle, ip.to_s, port)
|
120
|
+
else
|
121
|
+
Socket6.new(loop, handle, ip.to_s, port)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def on_connect(req, status)
|
126
|
+
::Libuv::Ext.free(req)
|
127
|
+
@callback.call(self)
|
128
|
+
end
|
129
|
+
|
130
|
+
|
131
|
+
class SocketBase
|
132
|
+
include Resource
|
133
|
+
|
134
|
+
def initialize(loop, tcp, ip, port)
|
135
|
+
@tcp, @sockaddr = tcp, ip_addr(ip, port)
|
136
|
+
end
|
137
|
+
|
138
|
+
def bind
|
139
|
+
check_result!(tcp_bind)
|
140
|
+
end
|
141
|
+
|
142
|
+
def connect(callback)
|
143
|
+
check_result!(tcp_connect(callback))
|
144
|
+
end
|
145
|
+
|
146
|
+
|
147
|
+
private
|
148
|
+
|
149
|
+
|
150
|
+
def connect_req
|
151
|
+
::Libuv::Ext.create_request(:uv_connect)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
class Socket4 < SocketBase
|
157
|
+
|
158
|
+
|
159
|
+
private
|
160
|
+
|
161
|
+
|
162
|
+
def ip_addr(ip, port)
|
163
|
+
::Libuv::Ext.ip4_addr(ip, port)
|
164
|
+
end
|
165
|
+
|
166
|
+
def tcp_bind
|
167
|
+
::Libuv::Ext.tcp_bind(@tcp, @sockaddr)
|
168
|
+
end
|
169
|
+
|
170
|
+
def tcp_connect(callback)
|
171
|
+
::Libuv::Ext.tcp_connect(
|
172
|
+
connect_req,
|
173
|
+
@tcp,
|
174
|
+
@sockaddr,
|
175
|
+
callback
|
176
|
+
)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
|
181
|
+
class Socket6 < SocketBase
|
182
|
+
|
183
|
+
|
184
|
+
private
|
185
|
+
|
186
|
+
|
187
|
+
def ip_addr(ip, port)
|
188
|
+
::Libuv::Ext.ip6_addr(ip, port)
|
189
|
+
end
|
190
|
+
|
191
|
+
def tcp_bind
|
192
|
+
::Libuv::Ext.tcp_bind6(@tcp, @sockaddr)
|
193
|
+
end
|
194
|
+
|
195
|
+
def tcp_connect(callback)
|
196
|
+
::Libuv::Ext.tcp_connect6(
|
197
|
+
connect_req,
|
198
|
+
@tcp,
|
199
|
+
@sockaddr,
|
200
|
+
callback
|
201
|
+
)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
194
205
|
end
|
data/lib/libuv/timer.rb
CHANGED
@@ -1,75 +1,88 @@
|
|
1
|
-
module Libuv
|
2
|
-
class Timer < Handle
|
3
|
-
include Assertions
|
4
|
-
|
5
|
-
|
6
|
-
TIMEOUT_ERROR = "timeout must be an Integer".freeze
|
7
|
-
REPEAT_ERROR = "repeat must be an Integer".freeze
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
error = check_result ::Libuv::Ext.
|
32
|
-
reject(error) if error
|
33
|
-
end
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
1
|
+
module Libuv
|
2
|
+
class Timer < Handle
|
3
|
+
include Assertions
|
4
|
+
|
5
|
+
|
6
|
+
TIMEOUT_ERROR = "timeout must be an Integer".freeze
|
7
|
+
REPEAT_ERROR = "repeat must be an Integer".freeze
|
8
|
+
|
9
|
+
|
10
|
+
# @param loop [::Libuv::Loop] loop this timer will be associated
|
11
|
+
# @param callback [Proc] callback to be called when the timer is triggered
|
12
|
+
def initialize(loop, callback = nil)
|
13
|
+
@loop, @callback = loop, callback
|
14
|
+
|
15
|
+
timer_ptr = ::Libuv::Ext.create_handle(:uv_timer)
|
16
|
+
error = check_result(::Libuv::Ext.timer_init(loop.handle, timer_ptr))
|
17
|
+
|
18
|
+
super(timer_ptr, error)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Enables the timer. A repeat of 0 means no repeat
|
22
|
+
#
|
23
|
+
# @param timeout [Fixnum] time in milliseconds before the timer callback is triggered the first time
|
24
|
+
# @param repeat [Fixnum] time in milliseconds between repeated callbacks after the first
|
25
|
+
def start(timeout, repeat = 0)
|
26
|
+
return if @closed
|
27
|
+
|
28
|
+
assert_type(Integer, timeout, TIMEOUT_ERROR)
|
29
|
+
assert_type(Integer, repeat, REPEAT_ERROR)
|
30
|
+
|
31
|
+
error = check_result ::Libuv::Ext.timer_start(handle, callback(:on_timer), timeout, repeat)
|
32
|
+
reject(error) if error
|
33
|
+
end
|
34
|
+
|
35
|
+
# Disables the timer.
|
36
|
+
def stop
|
37
|
+
return if @closed
|
38
|
+
error = check_result ::Libuv::Ext.timer_stop(handle)
|
39
|
+
reject(error) if error
|
40
|
+
end
|
41
|
+
|
42
|
+
# Resets the current repeat
|
43
|
+
def again
|
44
|
+
return if @closed
|
45
|
+
error = check_result ::Libuv::Ext.timer_again(handle)
|
46
|
+
reject(error) if error
|
47
|
+
end
|
48
|
+
|
49
|
+
# Updates the repeat timeout
|
50
|
+
def repeat=(repeat)
|
51
|
+
return if @closed
|
52
|
+
assert_type(Integer, repeat, REPEAT_ERROR)
|
53
|
+
check_result ::Libuv::Ext.timer_set_repeat(handle, repeat)
|
54
|
+
reject(error) if error
|
55
|
+
end
|
56
|
+
|
57
|
+
# Returns the current repeat timeout
|
58
|
+
def repeat
|
59
|
+
return if @closed
|
60
|
+
::Libuv::Ext.timer_get_repeat(handle)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Used to update the callback to be triggered by the timer
|
64
|
+
#
|
65
|
+
# @param callback [Proc] the callback to be called by the timer
|
66
|
+
def progress(callback = nil, &blk)
|
67
|
+
@callback = callback || blk
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
|
74
|
+
def on_timer(handle, status)
|
75
|
+
e = check_result(status)
|
76
|
+
|
77
|
+
if e
|
78
|
+
reject(e)
|
79
|
+
else
|
80
|
+
begin
|
81
|
+
@callback.call
|
82
|
+
rescue Exception => e
|
83
|
+
@loop.log :error, :timer_cb, e
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|