libuv 0.11.4 → 0.11.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/.gitignore +17 -17
- data/.gitmodules +3 -3
- data/.rspec +1 -1
- data/.travis.yml +16 -16
- data/Gemfile +4 -4
- data/LICENSE +23 -23
- data/README.md +82 -82
- data/Rakefile +31 -31
- data/lib/libuv.rb +54 -54
- data/lib/libuv/async.rb +47 -47
- data/lib/libuv/check.rb +55 -55
- data/lib/libuv/dns.rb +85 -85
- data/lib/libuv/error.rb +73 -73
- data/lib/libuv/ext/ext.rb +258 -258
- data/lib/libuv/ext/platform/darwin_x64.rb +23 -23
- data/lib/libuv/ext/platform/linux.rb +7 -7
- data/lib/libuv/ext/platform/unix.rb +29 -29
- data/lib/libuv/ext/platform/windows.rb +40 -40
- data/lib/libuv/ext/tasks.rb +29 -29
- 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 +238 -238
- data/lib/libuv/file.rb +191 -191
- data/lib/libuv/filesystem.rb +232 -232
- data/lib/libuv/fs_event.rb +31 -31
- data/lib/libuv/handle.rb +85 -85
- data/lib/libuv/idle.rb +56 -56
- data/lib/libuv/loop.rb +387 -387
- data/lib/libuv/mixins/assertions.rb +23 -23
- data/lib/libuv/mixins/fs_checks.rb +55 -55
- data/lib/libuv/mixins/listener.rb +34 -34
- data/lib/libuv/mixins/net.rb +40 -40
- data/lib/libuv/mixins/resource.rb +27 -27
- data/lib/libuv/mixins/stream.rb +154 -154
- data/lib/libuv/pipe.rb +197 -197
- data/lib/libuv/prepare.rb +56 -56
- data/lib/libuv/signal.rb +51 -51
- data/lib/libuv/tcp.rb +317 -317
- data/lib/libuv/timer.rb +91 -91
- data/lib/libuv/tty.rb +37 -37
- data/lib/libuv/udp.rb +224 -224
- data/lib/libuv/version.rb +3 -3
- data/lib/libuv/work.rb +75 -75
- data/libuv.gemspec +56 -56
- data/spec/async_spec.rb +60 -60
- data/spec/cpu_spec.rb +10 -10
- data/spec/defer_spec.rb +980 -980
- data/spec/dns_spec.rb +90 -90
- data/spec/filesystem_spec.rb +124 -124
- data/spec/idle_spec.rb +56 -56
- data/spec/pipe_spec.rb +160 -160
- data/spec/tcp_spec.rb +267 -267
- metadata +29 -21
data/lib/libuv/signal.rb
CHANGED
@@ -1,51 +1,51 @@
|
|
1
|
-
module Libuv
|
2
|
-
class Signal < Handle
|
3
|
-
|
4
|
-
|
5
|
-
SIGNALS = {
|
6
|
-
:HUP => 1,
|
7
|
-
:SIGHUP => 1,
|
8
|
-
:INT => 2,
|
9
|
-
:SIGINT => 2,
|
10
|
-
:BREAK => 21,
|
11
|
-
:SIGBREAK => 21,
|
12
|
-
:WINCH => 28,
|
13
|
-
:SIGWINCH => 28
|
14
|
-
}
|
15
|
-
|
16
|
-
|
17
|
-
# @param loop [::Libuv::Loop] loop this signal handler will be associated
|
18
|
-
# @param callback [Proc] callback to be called when the signal is triggered
|
19
|
-
def initialize(loop)
|
20
|
-
@loop = loop
|
21
|
-
|
22
|
-
signal_ptr = ::Libuv::Ext.create_handle(:uv_signal)
|
23
|
-
error = check_result(::Libuv::Ext.signal_init(loop.handle, signal_ptr))
|
24
|
-
|
25
|
-
super(signal_ptr, error)
|
26
|
-
end
|
27
|
-
|
28
|
-
# Enables the signal handler.
|
29
|
-
def start(signal)
|
30
|
-
return if @closed
|
31
|
-
signal = SIGNALS[signal] if signal.is_a? Symbol
|
32
|
-
error = check_result ::Libuv::Ext.signal_start(handle, callback(:on_sig), signal)
|
33
|
-
reject(error) if error
|
34
|
-
end
|
35
|
-
|
36
|
-
# Disables the signal handler.
|
37
|
-
def stop
|
38
|
-
return if @closed
|
39
|
-
error = check_result ::Libuv::Ext.signal_stop(handle)
|
40
|
-
reject(error) if error
|
41
|
-
end
|
42
|
-
|
43
|
-
|
44
|
-
private
|
45
|
-
|
46
|
-
|
47
|
-
def on_sig(handle, signal)
|
48
|
-
defer.notify(signal) # notify of a call
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
1
|
+
module Libuv
|
2
|
+
class Signal < Handle
|
3
|
+
|
4
|
+
|
5
|
+
SIGNALS = {
|
6
|
+
:HUP => 1,
|
7
|
+
:SIGHUP => 1,
|
8
|
+
:INT => 2,
|
9
|
+
:SIGINT => 2,
|
10
|
+
:BREAK => 21,
|
11
|
+
:SIGBREAK => 21,
|
12
|
+
:WINCH => 28,
|
13
|
+
:SIGWINCH => 28
|
14
|
+
}
|
15
|
+
|
16
|
+
|
17
|
+
# @param loop [::Libuv::Loop] loop this signal handler will be associated
|
18
|
+
# @param callback [Proc] callback to be called when the signal is triggered
|
19
|
+
def initialize(loop)
|
20
|
+
@loop = loop
|
21
|
+
|
22
|
+
signal_ptr = ::Libuv::Ext.create_handle(:uv_signal)
|
23
|
+
error = check_result(::Libuv::Ext.signal_init(loop.handle, signal_ptr))
|
24
|
+
|
25
|
+
super(signal_ptr, error)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Enables the signal handler.
|
29
|
+
def start(signal)
|
30
|
+
return if @closed
|
31
|
+
signal = SIGNALS[signal] if signal.is_a? Symbol
|
32
|
+
error = check_result ::Libuv::Ext.signal_start(handle, callback(:on_sig), signal)
|
33
|
+
reject(error) if error
|
34
|
+
end
|
35
|
+
|
36
|
+
# Disables the signal handler.
|
37
|
+
def stop
|
38
|
+
return if @closed
|
39
|
+
error = check_result ::Libuv::Ext.signal_stop(handle)
|
40
|
+
reject(error) if error
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
|
47
|
+
def on_sig(handle, signal)
|
48
|
+
defer.notify(signal) # notify of a call
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/libuv/tcp.rb
CHANGED
@@ -1,318 +1,318 @@
|
|
1
|
-
require 'ipaddr'
|
2
|
-
require 'ruby-tls'
|
3
|
-
|
4
|
-
|
5
|
-
module Libuv
|
6
|
-
class TCP < Handle
|
7
|
-
include Stream, Net
|
8
|
-
|
9
|
-
|
10
|
-
KEEPALIVE_ARGUMENT_ERROR = "delay must be an Integer".freeze
|
11
|
-
TLS_ERROR = "TLS write failed".freeze
|
12
|
-
|
13
|
-
|
14
|
-
attr_reader :connected
|
15
|
-
# Check if tls active on the socket
|
16
|
-
def tls?; !@tls.nil?; end
|
17
|
-
|
18
|
-
|
19
|
-
def initialize(loop, acceptor = nil)
|
20
|
-
@loop = loop
|
21
|
-
|
22
|
-
tcp_ptr = ::Libuv::Ext.create_handle(:uv_tcp)
|
23
|
-
error = check_result(::Libuv::Ext.tcp_init(loop.handle, tcp_ptr))
|
24
|
-
|
25
|
-
if acceptor && error.nil?
|
26
|
-
error = check_result(::Libuv::Ext.accept(acceptor, tcp_ptr))
|
27
|
-
@connected = true
|
28
|
-
else
|
29
|
-
@connected = false
|
30
|
-
end
|
31
|
-
|
32
|
-
super(tcp_ptr, error)
|
33
|
-
end
|
34
|
-
|
35
|
-
|
36
|
-
#
|
37
|
-
# TLS Abstraction ----------------------
|
38
|
-
# --------------------------------------
|
39
|
-
#
|
40
|
-
def start_tls(args = {})
|
41
|
-
return unless @connected && @tls.nil?
|
42
|
-
|
43
|
-
@handshake = false
|
44
|
-
@pending_writes = []
|
45
|
-
@tls = ::RubyTls::Connection.new(self)
|
46
|
-
@tls.start(args)
|
47
|
-
end
|
48
|
-
|
49
|
-
# Push through any pending writes when handshake has completed
|
50
|
-
def handshake_cb
|
51
|
-
@handshake = true
|
52
|
-
writes = @pending_writes
|
53
|
-
@pending_writes = nil
|
54
|
-
writes.each do |deferred, data|
|
55
|
-
@pending_write = deferred
|
56
|
-
@tls.encrypt(data)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
# This is clear text data that has been decrypted
|
61
|
-
# Same as stream.rb on_read for clear text
|
62
|
-
def dispatch_cb(data)
|
63
|
-
begin
|
64
|
-
@progress.call data, self
|
65
|
-
rescue Exception => e
|
66
|
-
@loop.log :error, :stream_progress_cb, e
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
# We resolve the existing tls write promise with a the
|
71
|
-
# real writes promise (a close may have occurred)
|
72
|
-
def transmit_cb(data)
|
73
|
-
if not @pending_write.nil?
|
74
|
-
@pending_write.resolve(direct_write(data))
|
75
|
-
@pending_write = nil
|
76
|
-
else
|
77
|
-
direct_write(data)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
# Close can be called multiple times
|
82
|
-
def close_cb
|
83
|
-
if not @pending_write.nil?
|
84
|
-
@pending_write.reject(TLS_ERROR)
|
85
|
-
@pending_write = nil
|
86
|
-
end
|
87
|
-
|
88
|
-
# Shutdown the stream
|
89
|
-
close
|
90
|
-
end
|
91
|
-
|
92
|
-
# overwrite the default close to ensure
|
93
|
-
# pending writes are rejected
|
94
|
-
def close
|
95
|
-
@connected = false
|
96
|
-
|
97
|
-
if not @pending_writes.nil?
|
98
|
-
@pending_writes.each do |deferred, data|
|
99
|
-
deferred.reject(TLS_ERROR)
|
100
|
-
end
|
101
|
-
@pending_writes = nil
|
102
|
-
end
|
103
|
-
|
104
|
-
super
|
105
|
-
end
|
106
|
-
|
107
|
-
# Verify peers will be called for each cert in the chain
|
108
|
-
def verify_peer(&block)
|
109
|
-
@tls.verify_cb &block
|
110
|
-
end
|
111
|
-
|
112
|
-
alias_method :direct_write, :write
|
113
|
-
def write(data)
|
114
|
-
if @tls.nil?
|
115
|
-
direct_write(data)
|
116
|
-
else
|
117
|
-
deferred = @loop.defer
|
118
|
-
|
119
|
-
if @handshake == true
|
120
|
-
@pending_write = deferred
|
121
|
-
@tls.encrypt(data)
|
122
|
-
else
|
123
|
-
@pending_writes << [deferred, data]
|
124
|
-
end
|
125
|
-
|
126
|
-
deferred.promise
|
127
|
-
end
|
128
|
-
end
|
129
|
-
#
|
130
|
-
# END TLS Abstraction ------------------
|
131
|
-
# --------------------------------------
|
132
|
-
#
|
133
|
-
|
134
|
-
def bind(ip, port, callback = nil, &blk)
|
135
|
-
return if @closed
|
136
|
-
@on_listen = callback || blk
|
137
|
-
assert_type(String, ip, IP_ARGUMENT_ERROR)
|
138
|
-
assert_type(Integer, port, PORT_ARGUMENT_ERROR)
|
139
|
-
|
140
|
-
begin
|
141
|
-
@tcp_socket = create_socket(IPAddr.new(ip), port)
|
142
|
-
@tcp_socket.bind
|
143
|
-
rescue Exception => e
|
144
|
-
reject(e)
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
def open(fd, binding = true, callback = nil, &blk)
|
149
|
-
return if @closed
|
150
|
-
if binding
|
151
|
-
@on_listen = callback || blk
|
152
|
-
else
|
153
|
-
@callback = callback || blk
|
154
|
-
end
|
155
|
-
error = check_result UV.tcp_open(handle, fd)
|
156
|
-
reject(error) if error
|
157
|
-
end
|
158
|
-
|
159
|
-
def accept(callback = nil, &blk)
|
160
|
-
begin
|
161
|
-
raise RuntimeError, CLOSED_HANDLE_ERROR if @closed
|
162
|
-
tcp = TCP.new(loop, handle)
|
163
|
-
begin
|
164
|
-
(callback || blk).call(tcp)
|
165
|
-
rescue Exception => e
|
166
|
-
@loop.log :error, :tcp_accept_cb, e
|
167
|
-
end
|
168
|
-
rescue Exception => e
|
169
|
-
@loop.log :info, :tcp_accept_failed, e
|
170
|
-
end
|
171
|
-
nil
|
172
|
-
end
|
173
|
-
|
174
|
-
def connect(ip, port, callback = nil, &blk)
|
175
|
-
return if @closed
|
176
|
-
@callback = callback || blk
|
177
|
-
assert_type(String, ip, IP_ARGUMENT_ERROR)
|
178
|
-
assert_type(Integer, port, PORT_ARGUMENT_ERROR)
|
179
|
-
|
180
|
-
begin
|
181
|
-
@tcp_socket = create_socket(IPAddr.new(ip), port)
|
182
|
-
@tcp_socket.connect(callback(:on_connect))
|
183
|
-
rescue Exception => e
|
184
|
-
reject(e)
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
def sockname
|
189
|
-
return [] if @closed
|
190
|
-
sockaddr, len = get_sockaddr_and_len
|
191
|
-
check_result! ::Libuv::Ext.tcp_getsockname(handle, sockaddr, len)
|
192
|
-
get_ip_and_port(::Libuv::Ext::Sockaddr.new(sockaddr), len.get_int(0))
|
193
|
-
end
|
194
|
-
|
195
|
-
def peername
|
196
|
-
return [] if @closed
|
197
|
-
sockaddr, len = get_sockaddr_and_len
|
198
|
-
check_result! ::Libuv::Ext.tcp_getpeername(handle, sockaddr, len)
|
199
|
-
get_ip_and_port(::Libuv::Ext::Sockaddr.new(sockaddr), len.get_int(0))
|
200
|
-
end
|
201
|
-
|
202
|
-
def enable_nodelay
|
203
|
-
return if @closed
|
204
|
-
check_result ::Libuv::Ext.tcp_nodelay(handle, 1)
|
205
|
-
end
|
206
|
-
|
207
|
-
def disable_nodelay
|
208
|
-
return if @closed
|
209
|
-
check_result ::Libuv::Ext.tcp_nodelay(handle, 0)
|
210
|
-
end
|
211
|
-
|
212
|
-
def enable_keepalive(delay)
|
213
|
-
return if @closed
|
214
|
-
assert_type(Integer, delay, KEEPALIVE_ARGUMENT_ERROR)
|
215
|
-
check_result ::Libuv::Ext.tcp_keepalive(handle, 1, delay)
|
216
|
-
end
|
217
|
-
|
218
|
-
def disable_keepalive
|
219
|
-
return if @closed
|
220
|
-
check_result ::Libuv::Ext.tcp_keepalive(handle, 0, 0)
|
221
|
-
end
|
222
|
-
|
223
|
-
def enable_simultaneous_accepts
|
224
|
-
return if @closed
|
225
|
-
check_result ::Libuv::Ext.tcp_simultaneous_accepts(handle, 1)
|
226
|
-
end
|
227
|
-
|
228
|
-
def disable_simultaneous_accepts
|
229
|
-
return if @closed
|
230
|
-
check_result ::Libuv::Ext.tcp_simultaneous_accepts(handle, 0)
|
231
|
-
end
|
232
|
-
|
233
|
-
|
234
|
-
private
|
235
|
-
|
236
|
-
|
237
|
-
def create_socket(ip, port)
|
238
|
-
if ip.ipv4?
|
239
|
-
Socket4.new(loop, handle, ip.to_s, port)
|
240
|
-
else
|
241
|
-
Socket6.new(loop, handle, ip.to_s, port)
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
def on_connect(req, status)
|
246
|
-
::Libuv::Ext.free(req)
|
247
|
-
@connected = true
|
248
|
-
|
249
|
-
begin
|
250
|
-
@callback.call(self)
|
251
|
-
rescue Exception => e
|
252
|
-
@loop.log :error, :connect_cb, e
|
253
|
-
end
|
254
|
-
end
|
255
|
-
|
256
|
-
|
257
|
-
class SocketBase
|
258
|
-
include Resource
|
259
|
-
|
260
|
-
def initialize(loop, tcp, ip, port)
|
261
|
-
@tcp, @sockaddr = tcp, ip_addr(ip, port)
|
262
|
-
end
|
263
|
-
|
264
|
-
def bind
|
265
|
-
check_result!(tcp_bind)
|
266
|
-
end
|
267
|
-
|
268
|
-
def connect(callback)
|
269
|
-
check_result!(tcp_connect(callback))
|
270
|
-
end
|
271
|
-
|
272
|
-
|
273
|
-
protected
|
274
|
-
|
275
|
-
|
276
|
-
def connect_req
|
277
|
-
::Libuv::Ext.create_request(:uv_connect)
|
278
|
-
end
|
279
|
-
|
280
|
-
def tcp_connect(callback)
|
281
|
-
::Libuv::Ext.tcp_connect(
|
282
|
-
connect_req,
|
283
|
-
@tcp,
|
284
|
-
@sockaddr,
|
285
|
-
callback
|
286
|
-
)
|
287
|
-
end
|
288
|
-
|
289
|
-
def tcp_bind
|
290
|
-
::Libuv::Ext.tcp_bind(@tcp, @sockaddr)
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
|
295
|
-
class Socket4 < SocketBase
|
296
|
-
protected
|
297
|
-
|
298
|
-
|
299
|
-
def ip_addr(ip, port)
|
300
|
-
addr = Ext::SockaddrIn.new
|
301
|
-
check_result! ::Libuv::Ext.ip4_addr(ip, port, addr)
|
302
|
-
addr
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
|
307
|
-
class Socket6 < SocketBase
|
308
|
-
protected
|
309
|
-
|
310
|
-
|
311
|
-
def ip_addr(ip, port)
|
312
|
-
addr = Ext::SockaddrIn6.new
|
313
|
-
check_result! ::Libuv::Ext.ip6_addr(ip, port, addr)
|
314
|
-
addr
|
315
|
-
end
|
316
|
-
end
|
317
|
-
end
|
1
|
+
require 'ipaddr'
|
2
|
+
require 'ruby-tls'
|
3
|
+
|
4
|
+
|
5
|
+
module Libuv
|
6
|
+
class TCP < Handle
|
7
|
+
include Stream, Net
|
8
|
+
|
9
|
+
|
10
|
+
KEEPALIVE_ARGUMENT_ERROR = "delay must be an Integer".freeze
|
11
|
+
TLS_ERROR = "TLS write failed".freeze
|
12
|
+
|
13
|
+
|
14
|
+
attr_reader :connected
|
15
|
+
# Check if tls active on the socket
|
16
|
+
def tls?; !@tls.nil?; end
|
17
|
+
|
18
|
+
|
19
|
+
def initialize(loop, acceptor = nil)
|
20
|
+
@loop = loop
|
21
|
+
|
22
|
+
tcp_ptr = ::Libuv::Ext.create_handle(:uv_tcp)
|
23
|
+
error = check_result(::Libuv::Ext.tcp_init(loop.handle, tcp_ptr))
|
24
|
+
|
25
|
+
if acceptor && error.nil?
|
26
|
+
error = check_result(::Libuv::Ext.accept(acceptor, tcp_ptr))
|
27
|
+
@connected = true
|
28
|
+
else
|
29
|
+
@connected = false
|
30
|
+
end
|
31
|
+
|
32
|
+
super(tcp_ptr, error)
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
#
|
37
|
+
# TLS Abstraction ----------------------
|
38
|
+
# --------------------------------------
|
39
|
+
#
|
40
|
+
def start_tls(args = {})
|
41
|
+
return unless @connected && @tls.nil?
|
42
|
+
|
43
|
+
@handshake = false
|
44
|
+
@pending_writes = []
|
45
|
+
@tls = ::RubyTls::Connection.new(self)
|
46
|
+
@tls.start(args)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Push through any pending writes when handshake has completed
|
50
|
+
def handshake_cb
|
51
|
+
@handshake = true
|
52
|
+
writes = @pending_writes
|
53
|
+
@pending_writes = nil
|
54
|
+
writes.each do |deferred, data|
|
55
|
+
@pending_write = deferred
|
56
|
+
@tls.encrypt(data)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# This is clear text data that has been decrypted
|
61
|
+
# Same as stream.rb on_read for clear text
|
62
|
+
def dispatch_cb(data)
|
63
|
+
begin
|
64
|
+
@progress.call data, self
|
65
|
+
rescue Exception => e
|
66
|
+
@loop.log :error, :stream_progress_cb, e
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# We resolve the existing tls write promise with a the
|
71
|
+
# real writes promise (a close may have occurred)
|
72
|
+
def transmit_cb(data)
|
73
|
+
if not @pending_write.nil?
|
74
|
+
@pending_write.resolve(direct_write(data))
|
75
|
+
@pending_write = nil
|
76
|
+
else
|
77
|
+
direct_write(data)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Close can be called multiple times
|
82
|
+
def close_cb
|
83
|
+
if not @pending_write.nil?
|
84
|
+
@pending_write.reject(TLS_ERROR)
|
85
|
+
@pending_write = nil
|
86
|
+
end
|
87
|
+
|
88
|
+
# Shutdown the stream
|
89
|
+
close
|
90
|
+
end
|
91
|
+
|
92
|
+
# overwrite the default close to ensure
|
93
|
+
# pending writes are rejected
|
94
|
+
def close
|
95
|
+
@connected = false
|
96
|
+
|
97
|
+
if not @pending_writes.nil?
|
98
|
+
@pending_writes.each do |deferred, data|
|
99
|
+
deferred.reject(TLS_ERROR)
|
100
|
+
end
|
101
|
+
@pending_writes = nil
|
102
|
+
end
|
103
|
+
|
104
|
+
super
|
105
|
+
end
|
106
|
+
|
107
|
+
# Verify peers will be called for each cert in the chain
|
108
|
+
def verify_peer(&block)
|
109
|
+
@tls.verify_cb &block
|
110
|
+
end
|
111
|
+
|
112
|
+
alias_method :direct_write, :write
|
113
|
+
def write(data)
|
114
|
+
if @tls.nil?
|
115
|
+
direct_write(data)
|
116
|
+
else
|
117
|
+
deferred = @loop.defer
|
118
|
+
|
119
|
+
if @handshake == true
|
120
|
+
@pending_write = deferred
|
121
|
+
@tls.encrypt(data)
|
122
|
+
else
|
123
|
+
@pending_writes << [deferred, data]
|
124
|
+
end
|
125
|
+
|
126
|
+
deferred.promise
|
127
|
+
end
|
128
|
+
end
|
129
|
+
#
|
130
|
+
# END TLS Abstraction ------------------
|
131
|
+
# --------------------------------------
|
132
|
+
#
|
133
|
+
|
134
|
+
def bind(ip, port, callback = nil, &blk)
|
135
|
+
return if @closed
|
136
|
+
@on_listen = callback || blk
|
137
|
+
assert_type(String, ip, IP_ARGUMENT_ERROR)
|
138
|
+
assert_type(Integer, port, PORT_ARGUMENT_ERROR)
|
139
|
+
|
140
|
+
begin
|
141
|
+
@tcp_socket = create_socket(IPAddr.new(ip), port)
|
142
|
+
@tcp_socket.bind
|
143
|
+
rescue Exception => e
|
144
|
+
reject(e)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def open(fd, binding = true, callback = nil, &blk)
|
149
|
+
return if @closed
|
150
|
+
if binding
|
151
|
+
@on_listen = callback || blk
|
152
|
+
else
|
153
|
+
@callback = callback || blk
|
154
|
+
end
|
155
|
+
error = check_result UV.tcp_open(handle, fd)
|
156
|
+
reject(error) if error
|
157
|
+
end
|
158
|
+
|
159
|
+
def accept(callback = nil, &blk)
|
160
|
+
begin
|
161
|
+
raise RuntimeError, CLOSED_HANDLE_ERROR if @closed
|
162
|
+
tcp = TCP.new(loop, handle)
|
163
|
+
begin
|
164
|
+
(callback || blk).call(tcp)
|
165
|
+
rescue Exception => e
|
166
|
+
@loop.log :error, :tcp_accept_cb, e
|
167
|
+
end
|
168
|
+
rescue Exception => e
|
169
|
+
@loop.log :info, :tcp_accept_failed, e
|
170
|
+
end
|
171
|
+
nil
|
172
|
+
end
|
173
|
+
|
174
|
+
def connect(ip, port, callback = nil, &blk)
|
175
|
+
return if @closed
|
176
|
+
@callback = callback || blk
|
177
|
+
assert_type(String, ip, IP_ARGUMENT_ERROR)
|
178
|
+
assert_type(Integer, port, PORT_ARGUMENT_ERROR)
|
179
|
+
|
180
|
+
begin
|
181
|
+
@tcp_socket = create_socket(IPAddr.new(ip), port)
|
182
|
+
@tcp_socket.connect(callback(:on_connect))
|
183
|
+
rescue Exception => e
|
184
|
+
reject(e)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def sockname
|
189
|
+
return [] if @closed
|
190
|
+
sockaddr, len = get_sockaddr_and_len
|
191
|
+
check_result! ::Libuv::Ext.tcp_getsockname(handle, sockaddr, len)
|
192
|
+
get_ip_and_port(::Libuv::Ext::Sockaddr.new(sockaddr), len.get_int(0))
|
193
|
+
end
|
194
|
+
|
195
|
+
def peername
|
196
|
+
return [] if @closed
|
197
|
+
sockaddr, len = get_sockaddr_and_len
|
198
|
+
check_result! ::Libuv::Ext.tcp_getpeername(handle, sockaddr, len)
|
199
|
+
get_ip_and_port(::Libuv::Ext::Sockaddr.new(sockaddr), len.get_int(0))
|
200
|
+
end
|
201
|
+
|
202
|
+
def enable_nodelay
|
203
|
+
return if @closed
|
204
|
+
check_result ::Libuv::Ext.tcp_nodelay(handle, 1)
|
205
|
+
end
|
206
|
+
|
207
|
+
def disable_nodelay
|
208
|
+
return if @closed
|
209
|
+
check_result ::Libuv::Ext.tcp_nodelay(handle, 0)
|
210
|
+
end
|
211
|
+
|
212
|
+
def enable_keepalive(delay)
|
213
|
+
return if @closed
|
214
|
+
assert_type(Integer, delay, KEEPALIVE_ARGUMENT_ERROR)
|
215
|
+
check_result ::Libuv::Ext.tcp_keepalive(handle, 1, delay)
|
216
|
+
end
|
217
|
+
|
218
|
+
def disable_keepalive
|
219
|
+
return if @closed
|
220
|
+
check_result ::Libuv::Ext.tcp_keepalive(handle, 0, 0)
|
221
|
+
end
|
222
|
+
|
223
|
+
def enable_simultaneous_accepts
|
224
|
+
return if @closed
|
225
|
+
check_result ::Libuv::Ext.tcp_simultaneous_accepts(handle, 1)
|
226
|
+
end
|
227
|
+
|
228
|
+
def disable_simultaneous_accepts
|
229
|
+
return if @closed
|
230
|
+
check_result ::Libuv::Ext.tcp_simultaneous_accepts(handle, 0)
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
private
|
235
|
+
|
236
|
+
|
237
|
+
def create_socket(ip, port)
|
238
|
+
if ip.ipv4?
|
239
|
+
Socket4.new(loop, handle, ip.to_s, port)
|
240
|
+
else
|
241
|
+
Socket6.new(loop, handle, ip.to_s, port)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
def on_connect(req, status)
|
246
|
+
::Libuv::Ext.free(req)
|
247
|
+
@connected = true
|
248
|
+
|
249
|
+
begin
|
250
|
+
@callback.call(self)
|
251
|
+
rescue Exception => e
|
252
|
+
@loop.log :error, :connect_cb, e
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
|
257
|
+
class SocketBase
|
258
|
+
include Resource
|
259
|
+
|
260
|
+
def initialize(loop, tcp, ip, port)
|
261
|
+
@tcp, @sockaddr = tcp, ip_addr(ip, port)
|
262
|
+
end
|
263
|
+
|
264
|
+
def bind
|
265
|
+
check_result!(tcp_bind)
|
266
|
+
end
|
267
|
+
|
268
|
+
def connect(callback)
|
269
|
+
check_result!(tcp_connect(callback))
|
270
|
+
end
|
271
|
+
|
272
|
+
|
273
|
+
protected
|
274
|
+
|
275
|
+
|
276
|
+
def connect_req
|
277
|
+
::Libuv::Ext.create_request(:uv_connect)
|
278
|
+
end
|
279
|
+
|
280
|
+
def tcp_connect(callback)
|
281
|
+
::Libuv::Ext.tcp_connect(
|
282
|
+
connect_req,
|
283
|
+
@tcp,
|
284
|
+
@sockaddr,
|
285
|
+
callback
|
286
|
+
)
|
287
|
+
end
|
288
|
+
|
289
|
+
def tcp_bind
|
290
|
+
::Libuv::Ext.tcp_bind(@tcp, @sockaddr)
|
291
|
+
end
|
292
|
+
end
|
293
|
+
|
294
|
+
|
295
|
+
class Socket4 < SocketBase
|
296
|
+
protected
|
297
|
+
|
298
|
+
|
299
|
+
def ip_addr(ip, port)
|
300
|
+
addr = Ext::SockaddrIn.new
|
301
|
+
check_result! ::Libuv::Ext.ip4_addr(ip, port, addr)
|
302
|
+
addr
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
|
307
|
+
class Socket6 < SocketBase
|
308
|
+
protected
|
309
|
+
|
310
|
+
|
311
|
+
def ip_addr(ip, port)
|
312
|
+
addr = Ext::SockaddrIn6.new
|
313
|
+
check_result! ::Libuv::Ext.ip6_addr(ip, port, addr)
|
314
|
+
addr
|
315
|
+
end
|
316
|
+
end
|
317
|
+
end
|
318
318
|
end
|