libuv 0.10.3 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 769f10cadb566b542368277dfee691a5aa2af059
4
- data.tar.gz: 8114fb75a4da94c6341e6ead1faf5d59182c9245
3
+ metadata.gz: 8f50ac13e289f22e33239e26cbd273c6559284ab
4
+ data.tar.gz: 8c342b9f3c447b5f72e92170c29e62d606236c1d
5
5
  SHA512:
6
- metadata.gz: 33e0394111e14048717934b2cffbc6f37d660117f6bc871eb7bb4da8089c0a303629b28febb6ee762f870039968029f620c35303b68302f1b8e1fc18bdbd2522
7
- data.tar.gz: b1d5f5af132c26fdb88d732c188f6b067714512e3466feb63bef970f56374f961810c1a63d8d5e8c9301af28f23e0b70aae7a6bf917e07fd38c0b6e806565216
6
+ metadata.gz: 9c4b6ec4ac447ca967050fea8487850ac29041a2abb2d3437c4a7ce529be51d522be416c0dc719a25fda640c98b4bdcb67d1b9f6d8c6083155ee302882580821
7
+ data.tar.gz: 6667e4276f32dea396149d8118eb99d35dc73c62ddd1339ca834ca563cd3adf8c326f39a84f7c69dcb60bc9000c4d79b5bd037da05f256269dad77f37f8a138c
data/README.md CHANGED
@@ -38,7 +38,7 @@ Check out the [yard documentation](http://rubydoc.info/gems/libuv/Libuv/Loop)
38
38
 
39
39
  ## Installation
40
40
 
41
- ```Shell
41
+ ```shell
42
42
  gem install libuv
43
43
  ```
44
44
 
@@ -62,7 +62,7 @@ or
62
62
  * if you have a compatible `libuv.(so | dylib | dll)` on the PATH already
63
63
 
64
64
 
65
- ## What's supported
65
+ ## Libuv features supported
66
66
 
67
67
  * TCP
68
68
  * UDP
@@ -72,11 +72,11 @@ or
72
72
  * Prepare
73
73
  * Check
74
74
  * Idle
75
- * Async
76
75
  * Signals
76
+ * Async callbacks
77
+ * Async DNS Resolution
77
78
  * Filesystem Events
78
- * File manipulation
79
79
  * Filesystem manipulation
80
+ * File manipulation
80
81
  * Errors
81
82
  * Work queue (thread pool)
82
-
@@ -29,6 +29,7 @@ module Libuv
29
29
  require 'libuv/idle' # Called when there are no events to process
30
30
  require 'libuv/work' # Provide work to be completed on another thread (thread pool)
31
31
  require 'libuv/udp' # Communicate over UDP
32
+ require 'libuv/dns' # Async DNS lookup
32
33
 
33
34
  # Streams
34
35
  require 'libuv/pipe' # Communicate over Pipes
@@ -0,0 +1,83 @@
1
+ module Libuv
2
+ class Dns < Q::DeferredPromise
3
+ include Resource, Listener, Net
4
+
5
+
6
+ attr_reader :results
7
+ attr_reader :domain
8
+ attr_reader :port
9
+ attr_reader :hint
10
+
11
+
12
+ HINTS = {
13
+ :IPv4 => ::Libuv::Ext::UvAddrinfo.new,
14
+ :IPv6 => ::Libuv::Ext::UvAddrinfo.new
15
+ }
16
+ HINTS[:IPv4].tap do |hint|
17
+ hint[:family] = Socket::Constants::AF_INET
18
+ hint[:socktype] = Socket::Constants::SOCK_STREAM
19
+ hint[:protocol] = Socket::Constants::IPPROTO_TCP
20
+ end
21
+ HINTS[:IPv6].tap do |hint|
22
+ hint[:family] = Socket::Constants::AF_INET6
23
+ hint[:socktype] = Socket::Constants::SOCK_STREAM
24
+ hint[:protocol] = Socket::Constants::IPPROTO_TCP
25
+ end
26
+
27
+
28
+ # @param loop [::Libuv::Loop] loop this work request will be associated
29
+ # @param domain [String] the domain name to resolve
30
+ # @param port [Integer, String] the port we wish to use
31
+ def initialize(loop, domain, port, hint = :IPv4)
32
+ super(loop, loop.defer)
33
+
34
+ @domain = domain
35
+ @port = port
36
+ @hint = hint
37
+ @complete = false
38
+ @pointer = ::Libuv::Ext.create_request(:uv_getaddrinfo)
39
+ @error = nil # error in callback
40
+
41
+ error = check_result ::Libuv::Ext.getaddrinfo(@loop, @pointer, callback(:on_complete), domain, port.to_s, HINTS[hint])
42
+ if error
43
+ ::Libuv::Ext.free(@pointer)
44
+ @complete = true
45
+ @defer.reject(error)
46
+ end
47
+ end
48
+
49
+ # Indicates if the lookup has completed yet or not.
50
+ #
51
+ # @return [true, false]
52
+ def completed?
53
+ return @complete
54
+ end
55
+
56
+
57
+ private
58
+
59
+
60
+ def on_complete(req, status, addrinfo)
61
+ @complete = true
62
+ ::Libuv::Ext.free(req)
63
+
64
+ e = check_result(status)
65
+ if e
66
+ @defer.reject(e)
67
+ else
68
+ begin
69
+ current = addrinfo
70
+ @results = []
71
+ while !current.null?
72
+ @results << get_ip_and_port(current[:addr])
73
+ current = current[:next]
74
+ end
75
+ @defer.resolve(@results)
76
+ rescue Exception => e
77
+ @defer.reject(e)
78
+ end
79
+ ::Libuv::Ext.freeaddrinfo(addrinfo)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -155,9 +155,9 @@ module Libuv
155
155
  attach_function :timer_again, :uv_timer_again, [:uv_timer_t], :int, :blocking => true
156
156
  attach_function :timer_set_repeat, :uv_timer_set_repeat, [:uv_timer_t, :int64_t], :void, :blocking => true
157
157
  attach_function :timer_get_repeat, :uv_timer_get_repeat, [:uv_timer_t], :int64_t, :blocking => true
158
-
159
- attach_function :getaddrinfo, :uv_getaddrinfo, [:uv_loop_t, :uv_getaddrinfo_t, :uv_getaddrinfo_cb, :string, :string, :addrinfo], :int, :blocking => true
160
- attach_function :freeaddrinfo, :uv_freeaddrinfo, [:addrinfo], :void, :blocking => true
158
+ #:addrinfo
159
+ attach_function :getaddrinfo, :uv_getaddrinfo, [:uv_loop_t, :uv_getaddrinfo_t, :uv_getaddrinfo_cb, :string, :string, UvAddrinfo.by_ref], :int, :blocking => true
160
+ attach_function :freeaddrinfo, :uv_freeaddrinfo, [UvAddrinfo.by_ref], :void, :blocking => true
161
161
 
162
162
  attach_function :spawn, :uv_spawn, [:uv_loop_t, :uv_process_t, :uv_options_t], :int, :blocking => true
163
163
  attach_function :process_kill, :uv_process_kill, [:uv_process_t, :int], :int, :blocking => true
@@ -8,5 +8,16 @@ module Libuv
8
8
  :blksize_t, :st_flags, :uint32, :st_gen, :uint32, :st_lspare,
9
9
  :int32, :st_qspare_0, :int64, :st_qspare_1, :int64
10
10
  end
11
+
12
+ class UvAddrinfo < FFI::Struct
13
+ layout :flags, :int,
14
+ :family, :int,
15
+ :socktype, :int,
16
+ :protocol, :int,
17
+ :addrlen, :socklen_t,
18
+ :canonname, :string,
19
+ :addr, Sockaddr.by_ref,
20
+ :next, UvAddrinfo.by_ref
21
+ end
11
22
  end
12
23
  end
@@ -13,6 +13,17 @@ module Libuv
13
13
  :st_mtime, :time_t, :st_ctime, :time_t
14
14
  end
15
15
 
16
+ class UvAddrinfo < FFI::Struct
17
+ layout :flags, :int,
18
+ :family, :int,
19
+ :socktype, :int,
20
+ :protocol, :int,
21
+ :addrlen, :socklen_t,
22
+ :addr, Sockaddr.by_ref,
23
+ :canonname, :string,
24
+ :next, UvAddrinfo.by_ref
25
+ end
26
+
16
27
  attach_function :ntohs, [:ushort], :ushort, :blocking => true
17
28
  end
18
- end
29
+ end
@@ -25,5 +25,16 @@ module Libuv
25
25
  :st_ino, :ino_t, :st_mode, :mode_t, :st_mtime, :time_t, :st_nlink, :nlink_t,
26
26
  :st_rdev, :dev_t, :st_size, :off_t, :st_uid, :uid_t
27
27
  end
28
+
29
+ class UvAddrinfo < FFI::Struct
30
+ layout :flags, :int,
31
+ :family, :int,
32
+ :socktype, :int,
33
+ :protocol, :int,
34
+ :addrlen, :size_t,
35
+ :canonname, :string,
36
+ :addr, Sockaddr.by_ref,
37
+ :next, UvAddrinfo.by_ref
38
+ end
28
39
  end
29
- end
40
+ end
@@ -1,3 +1,5 @@
1
+ require 'socket' # Addrinfo
2
+
1
3
  module FFI::Platform
2
4
  def self.linux?
3
5
  IS_LINUX
@@ -6,6 +8,23 @@ end
6
8
 
7
9
  module Libuv
8
10
  module Ext
11
+ class Sockaddr < FFI::Struct
12
+ layout :sa_len, :uint8,
13
+ :sa_family, :sa_family_t,
14
+ :sa_data, [:char, 14]
15
+ end
16
+
17
+ class UvAddrinfo < FFI::Struct
18
+ layout :flags, :int,
19
+ :family, :int,
20
+ :socktype, :int,
21
+ :protocol, :int,
22
+ :addrlen, :socklen_t,
23
+ :addr, Sockaddr.by_ref,
24
+ :canonname, :string,
25
+ :next, :pointer
26
+ end
27
+
9
28
  require 'libuv/ext/platform/linux.rb' if FFI::Platform.linux?
10
29
  require 'libuv/ext/platform/unix.rb' if FFI::Platform.unix?
11
30
  require 'libuv/ext/platform/darwin_x64.rb' if FFI::Platform.mac? and FFI::Platform::ARCH == 'x86_64'
@@ -90,13 +109,6 @@ module Libuv
90
109
 
91
110
  typedef UvBuf.by_ref, :uv_buf_t
92
111
 
93
-
94
- class Sockaddr < FFI::Struct
95
- layout :sa_len, :uint8,
96
- :sa_family, :sa_family_t,
97
- :sa_data, [:char, 14]
98
- end
99
-
100
112
  class InAddr < FFI::Struct
101
113
  layout :s_addr, :in_addr_t
102
114
  end
@@ -173,7 +185,6 @@ module Libuv
173
185
  typedef :pointer, :uv_signal_t
174
186
  typedef :pointer, :uv_process_t
175
187
  typedef :pointer, :uv_getaddrinfo_cb
176
- typedef :pointer, :addrinfo
177
188
  typedef :pointer, :uv_work_t
178
189
  typedef :pointer, :uv_loop_t
179
190
  typedef :pointer, :uv_shutdown_t
@@ -214,7 +225,7 @@ module Libuv
214
225
  callback :uv_prepare_cb, [:uv_prepare_t, :status], :void
215
226
  callback :uv_check_cb, [:uv_check_t, :status], :void
216
227
  callback :uv_idle_cb, [:uv_idle_t, :status], :void
217
- callback :uv_getaddrinfo_cb, [:uv_getaddrinfo_t, :status, :addrinfo], :void
228
+ callback :uv_getaddrinfo_cb, [:uv_getaddrinfo_t, :status, UvAddrinfo.by_ref], :void
218
229
  callback :uv_exit_cb, [:uv_process_t, :int, :int], :void
219
230
  callback :uv_walk_cb, [:uv_handle_t, :pointer], :void
220
231
  callback :uv_work_cb, [:uv_work_t], :void
@@ -269,6 +269,18 @@ module Libuv
269
269
  Work.new(@loop, callback) # Work is a promise object
270
270
  end
271
271
 
272
+ # Lookup a hostname
273
+ #
274
+ # @param hostname [String] the domain name to lookup
275
+ # @param port [Integer, String] the service being connected too
276
+ # @param callback [Proc] the callback to be called on success
277
+ # @return [::Libuv::Dns]
278
+ def lookup(hostname, hint = :IPv4, port = 9, &block)
279
+ dns = Dns.new(@loop, hostname, port, hint) # Work is a promise object
280
+ dns.then block if block_given?
281
+ dns
282
+ end
283
+
272
284
  # Get a new FSEvent instance
273
285
  #
274
286
  # @param path [String] the path to the file or folder for watching
@@ -6,28 +6,30 @@ module Libuv
6
6
 
7
7
  IP_ARGUMENT_ERROR = "ip must be a String".freeze # Arguments specifying an IP address
8
8
  PORT_ARGUMENT_ERROR = "port must be an Integer".freeze # Arguments specifying an IP port
9
+ INET_ADDRSTRLEN = 16
10
+ INET6_ADDRSTRLEN = 46
9
11
 
10
12
 
11
13
  private
12
14
 
13
15
 
14
16
  def get_sockaddr_and_len
15
- sockaddr = FFI::MemoryPointer.new(UV::Sockaddr)
17
+ sockaddr = FFI::MemoryPointer.new(::Libuv::Ext::Sockaddr)
16
18
  len = FFI::MemoryPointer.new(:int)
17
- len.put_int(0, UV::Sockaddr.size)
19
+ len.put_int(0, ::Libuv::Ext::Sockaddr.size)
18
20
  [sockaddr, len]
19
21
  end
20
22
 
21
- def get_ip_and_port(sockaddr, len=nil)
23
+ def get_ip_and_port(sockaddr, len = nil)
22
24
  if sockaddr[:sa_family] == Socket::Constants::AF_INET6
23
- len ||= Socket::Constants::INET6_ADDRSTRLEN
24
- sockaddr_in6 = UV::SockaddrIn6.new(sockaddr.pointer)
25
+ len ||= INET6_ADDRSTRLEN
26
+ sockaddr_in6 = ::Libuv::Ext::SockaddrIn6.new(sockaddr.pointer)
25
27
  ip_ptr = FFI::MemoryPointer.new(:char, len)
26
28
  ::Libuv::Ext.ip6_name(sockaddr_in6, ip_ptr, len)
27
29
  port = ::Libuv::Ext.ntohs(sockaddr_in6[:sin6_port])
28
30
  else
29
- len ||= Socket::Constants::INET_ADDRSTRLEN
30
- sockaddr_in = UV::SockaddrIn.new(sockaddr.pointer)
31
+ len ||= INET_ADDRSTRLEN
32
+ sockaddr_in = ::Libuv::Ext::SockaddrIn.new(sockaddr.pointer)
31
33
  ip_ptr = FFI::MemoryPointer.new(:char, len)
32
34
  ::Libuv::Ext.ip4_name(sockaddr_in, ip_ptr, len)
33
35
  port = ::Libuv::Ext.ntohs(sockaddr_in[:sin_port])
@@ -35,4 +37,4 @@ module Libuv
35
37
  [ip_ptr.read_string, port]
36
38
  end
37
39
  end
38
- end
40
+ end
@@ -77,14 +77,14 @@ module Libuv
77
77
  return [] if @closed
78
78
  sockaddr, len = get_sockaddr_and_len
79
79
  check_result! ::Libuv::Ext.tcp_getsockname(handle, sockaddr, len)
80
- get_ip_and_port(::Libuv::Sockaddr.new(sockaddr), len.get_int(0))
80
+ get_ip_and_port(::Libuv::Ext::Sockaddr.new(sockaddr), len.get_int(0))
81
81
  end
82
82
 
83
83
  def peername
84
84
  return [] if @closed
85
85
  sockaddr, len = get_sockaddr_and_len
86
86
  check_result! ::Libuv::Ext.tcp_getpeername(handle, sockaddr, len)
87
- get_ip_and_port(::Libuv::Sockaddr.new(sockaddr), len.get_int(0))
87
+ get_ip_and_port(::Libuv::Ext::Sockaddr.new(sockaddr), len.get_int(0))
88
88
  end
89
89
 
90
90
  def enable_nodelay
@@ -1,3 +1,3 @@
1
1
  module Libuv
2
- VERSION = '0.10.3'
2
+ VERSION = '0.11.0'
3
3
  end
@@ -0,0 +1,90 @@
1
+ require 'libuv'
2
+
3
+
4
+ describe Libuv::Dns do
5
+ before :each do
6
+ @log = []
7
+ @general_failure = []
8
+
9
+ @loop = Libuv::Loop.new
10
+ @timeout = @loop.timer do
11
+ @loop.stop
12
+ @general_failure << "test timed out"
13
+ end
14
+ @timeout.start(5000)
15
+
16
+ @loop.all(@server, @client, @timeout).catch do |reason|
17
+ @general_failure << reason.inspect
18
+ p "Failed with: #{reason.message}\n#{reason.backtrace.join("\n")}\n"
19
+ end
20
+ end
21
+
22
+ it "should resolve localhost using IP4", :network => true do
23
+ @loop.run { |logger|
24
+ logger.progress do |level, errorid, error|
25
+ begin
26
+ p "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
27
+ rescue Exception
28
+ p 'error in logger'
29
+ end
30
+ end
31
+
32
+ @loop.lookup('localhost').then proc { |addrinfo|
33
+ @result = addrinfo[0][0]
34
+ @loop.stop
35
+ }, proc { |err|
36
+ @general_failure << err
37
+ @loop.stop
38
+ }
39
+ }
40
+
41
+ @general_failure.should == []
42
+ @result.should == '127.0.0.1'
43
+ end
44
+
45
+ it "should resolve localhost using IP6", :network => true do
46
+ @loop.run { |logger|
47
+ logger.progress do |level, errorid, error|
48
+ begin
49
+ p "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
50
+ rescue Exception
51
+ p 'error in logger'
52
+ end
53
+ end
54
+
55
+ @loop.lookup('localhost', :IPv6).then proc { |addrinfo|
56
+ @result = addrinfo[0][0]
57
+ @loop.stop
58
+ }, proc { |err|
59
+ @general_failure << err
60
+ @loop.stop
61
+ }
62
+ }
63
+
64
+ @general_failure.should == []
65
+ @result.should == '::1'
66
+ end
67
+
68
+ it "should resolve loop back" do
69
+ @loop.run { |logger|
70
+ logger.progress do |level, errorid, error|
71
+ begin
72
+ p "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
73
+ rescue Exception
74
+ p 'error in logger'
75
+ end
76
+ end
77
+
78
+ @loop.lookup('127.0.0.1').then proc { |addrinfo|
79
+ @result = addrinfo[0][0]
80
+ @loop.stop
81
+ }, proc { |err|
82
+ @general_failure << err
83
+ @loop.stop
84
+ }
85
+ }
86
+
87
+ @general_failure.should == []
88
+ @result.should == '127.0.0.1'
89
+ end
90
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libuv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.3
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bulat Shakirzyanov
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-06 00:00:00.000000000 Z
12
+ date: 2013-11-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ffi
@@ -103,6 +103,7 @@ files:
103
103
  - lib/libuv.rb
104
104
  - lib/libuv/async.rb
105
105
  - lib/libuv/check.rb
106
+ - lib/libuv/dns.rb
106
107
  - lib/libuv/error.rb
107
108
  - lib/libuv/ext/ext.rb
108
109
  - lib/libuv/ext/platform/darwin_x64.rb
@@ -140,6 +141,7 @@ files:
140
141
  - spec/async_spec.rb
141
142
  - spec/cpu_spec.rb
142
143
  - spec/defer_spec.rb
144
+ - spec/dns_spec.rb
143
145
  - spec/filesystem_spec.rb
144
146
  - spec/idle_spec.rb
145
147
  - spec/pipe_spec.rb
@@ -389,6 +391,7 @@ test_files:
389
391
  - spec/async_spec.rb
390
392
  - spec/cpu_spec.rb
391
393
  - spec/defer_spec.rb
394
+ - spec/dns_spec.rb
392
395
  - spec/filesystem_spec.rb
393
396
  - spec/idle_spec.rb
394
397
  - spec/pipe_spec.rb