libuv 0.10.0 → 0.10.2

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