uvrb 0.1.0 → 0.1.1

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.
@@ -6,19 +6,25 @@ module UV
6
6
 
7
7
  def assert_arity(expected, proc, msg = nil)
8
8
  actual = proc.arity
9
- arg = expected == 1 ? "argument" : "arguments"
10
- msg ||= "block must accept #{expected} #{arg}, but accepts #{actual}"
11
- raise ArgumentError, msg, caller if expected != actual
9
+ if expected != actual
10
+ arg = expected == 1 ? "argument" : "arguments"
11
+ msg ||= "block must accept #{expected} #{arg}, but accepts #{actual}"
12
+ raise ArgumentError, msg, caller
13
+ end
12
14
  end
13
15
 
14
16
  def assert_type(type, actual, msg = nil)
15
- msg ||= "value #{actual.inspect} is not a valid #{type}"
16
- raise ArgumentError, msg, caller unless actual.kind_of?(type)
17
+ if not actual.kind_of?(type)
18
+ msg ||= "value #{actual.inspect} is not a valid #{type}"
19
+ raise ArgumentError, msg, caller
20
+ end
17
21
  end
18
22
 
19
23
  def assert_boolean(actual, msg = nil)
20
- msg ||= "value #{actual.inspect} is not a valid Boolean"
21
- raise ArgumentError, msg, caller unless actual.kind_of?(TrueClass) || actual.kind_of?(FalseClass)
24
+ if not (actual.kind_of?(TrueClass) || actual.kind_of?(FalseClass))
25
+ msg ||= "value #{actual.inspect} is not a valid Boolean"
26
+ raise ArgumentError, msg, caller
27
+ end
22
28
  end
23
29
  end
24
30
  end
@@ -27,10 +27,12 @@ module UV
27
27
  end
28
28
 
29
29
  def close(&block)
30
- assert_block(block)
31
- assert_arity(0, block)
30
+ if not block.nil?
31
+ assert_block(block)
32
+ assert_arity(0, block)
32
33
 
33
- @close_block = block
34
+ @close_block = block
35
+ end
34
36
 
35
37
  UV.close(@pointer, callback(:on_close))
36
38
 
@@ -59,7 +61,8 @@ module UV
59
61
  def on_close(pointer)
60
62
  UV.free(pointer)
61
63
  clear_callbacks
62
- @close_block.call
64
+
65
+ @close_block.call unless @close_block.nil?
63
66
  end
64
67
  end
65
68
  end
@@ -34,6 +34,7 @@ module UV
34
34
  # Returns nothing
35
35
  def initialize(pointer) # :notnew:
36
36
  @pointer = pointer
37
+ @loop = self
37
38
  end
38
39
 
39
40
  # Public: Run the actual event loop. This method will block for the duration of event loop
@@ -93,69 +94,15 @@ module UV
93
94
  # Raises UV::Error::EXDEV
94
95
  # Raises UV::Error::ENOTEMPTY
95
96
  # Raises UV::Error::ENOSPC
96
- def run
97
- check_result! UV.run(@pointer)
97
+ def run(run_type = :UV_RUN_DEFAULT)
98
+ check_result! UV.run(@pointer, run_type)
98
99
  end
99
100
 
100
- # Public: Runs outstanding events once, yields control back
101
+ # Public: (Deprecated - use loop.run with a run_type specified) Runs outstanding events once, yields control back
101
102
  #
102
- # Returns nothing.
103
- #
104
- # Raises UV::Error::UNKNOWN
105
- # Raises UV::Error::EOF
106
- # Raises UV::Error::EADDRINFO
107
- # Raises UV::Error::EACCES
108
- # Raises UV::Error::EAGAIN
109
- # Raises UV::Error::EADDRINUSE
110
- # Raises UV::Error::EADDRNOTAVAIL
111
- # Raises UV::Error::EAFNOSUPPORT
112
- # Raises UV::Error::EALREADY
113
- # Raises UV::Error::EBADF
114
- # Raises UV::Error::EBUSY
115
- # Raises UV::Error::ECONNABORTED
116
- # Raises UV::Error::ECONNREFUSED
117
- # Raises UV::Error::ECONNRESET
118
- # Raises UV::Error::EDESTADDRREQ
119
- # Raises UV::Error::EFAULT
120
- # Raises UV::Error::EHOSTUNREACH
121
- # Raises UV::Error::EINTR
122
- # Raises UV::Error::EINVAL
123
- # Raises UV::Error::EISCONN
124
- # Raises UV::Error::EMFILE
125
- # Raises UV::Error::EMSGSIZE
126
- # Raises UV::Error::ENETDOWN
127
- # Raises UV::Error::ENETUNREACH
128
- # Raises UV::Error::ENFILE
129
- # Raises UV::Error::ENOBUFS
130
- # Raises UV::Error::ENOMEM
131
- # Raises UV::Error::ENOTDIR
132
- # Raises UV::Error::EISDIR
133
- # Raises UV::Error::ENONET
134
- # Raises UV::Error::ENOTCONN
135
- # Raises UV::Error::ENOTSOCK
136
- # Raises UV::Error::ENOTSUP
137
- # Raises UV::Error::ENOENT
138
- # Raises UV::Error::ENOSYS
139
- # Raises UV::Error::EPIPE
140
- # Raises UV::Error::EPROTO
141
- # Raises UV::Error::EPROTONOSUPPORT
142
- # Raises UV::Error::EPROTOTYPE
143
- # Raises UV::Error::ETIMEDOUT
144
- # Raises UV::Error::ECHARSE
145
- # Raises UV::Error::EAIFAMNOSUPPORT
146
- # Raises UV::Error::EAISERVICE
147
- # Raises UV::Error::EAISOCKTYPE
148
- # Raises UV::Error::ESHUTDOWN
149
- # Raises UV::Error::EEXIST
150
- # Raises UV::Error::ESRCH
151
- # Raises UV::Error::ENAMETOOLONG
152
- # Raises UV::Error::EPERM
153
- # Raises UV::Error::ELOOP
154
- # Raises UV::Error::EXDEV
155
- # Raises UV::Error::ENOTEMPTY
156
- # Raises UV::Error::ENOSPC
103
+ # Returns nothing
157
104
  def run_once
158
- check_result! UV.run_once(@pointer)
105
+ run(:UV_RUN_ONCE)
159
106
  end
160
107
 
161
108
  # Public: forces loop time update, useful for getting more granular times
@@ -175,8 +122,7 @@ module UV
175
122
  # Internal: Get last error from the loop
176
123
  #
177
124
  # Returns one of UV::Error or nil
178
- def last_error
179
- err = UV.last_error(@pointer)
125
+ def lookup_error(err)
180
126
  name = UV.err_name(err)
181
127
  msg = UV.strerror(err)
182
128
 
@@ -295,6 +241,24 @@ module UV
295
241
  async
296
242
  end
297
243
 
244
+ # Public: Do some work in the libuv thread pool
245
+ #
246
+ # Returns UV::Work
247
+ #
248
+ # Raises ArgumentError if block is not given and if the block or optional callback are expecting any arguments
249
+ def work(callback = nil, op = nil, &block)
250
+ block = block || op
251
+ assert_block(block)
252
+ assert_arity(0, block)
253
+
254
+ if not callback.nil?
255
+ assert_block(callback)
256
+ assert_arity(0, callback)
257
+ end
258
+
259
+ work = Work.new(self, block, callback)
260
+ end
261
+
298
262
  # Public: Get a new Filesystem instance
299
263
  #
300
264
  # Returns UV::Filesystem
@@ -13,6 +13,7 @@ module UV
13
13
  def bind(name)
14
14
  assert_type(String, name, "name must be a String")
15
15
 
16
+ name = windows_path name if FFI::Platform.windows?
16
17
  check_result! UV.pipe_bind(handle, name)
17
18
 
18
19
  self
@@ -25,6 +26,7 @@ module UV
25
26
 
26
27
  @connect_block = block
27
28
 
29
+ name = windows_path name if FFI::Platform.windows?
28
30
  UV.pipe_connect(UV.create_request(:uv_connect), handle, name, callback(:on_connect))
29
31
 
30
32
  self
@@ -43,5 +45,13 @@ module UV
43
45
  UV.free(req)
44
46
  @connect_block.call(check_result(status))
45
47
  end
48
+
49
+ def windows_path(name)
50
+ # test for \\\\.\\pipe
51
+ if not name =~ /(\/|\\){2}\.(\/|\\)pipe/i
52
+ name = ::File.join("\\\\.\\pipe", name)
53
+ end
54
+ name.gsub("/", "\\")
55
+ end
46
56
  end
47
57
  end
@@ -1,7 +1,7 @@
1
1
  module UV
2
2
  module Resource
3
3
  def check_result(rc)
4
- @loop.last_error if rc == -1
4
+ @loop.lookup_error(rc) unless rc.nil? || rc >= 0
5
5
  end
6
6
 
7
7
  def check_result!(rc)
@@ -19,9 +19,9 @@ end
19
19
  CLEAN.include('ext/libuv/build/gyp')
20
20
 
21
21
  if FFI::Platform.windows?
22
- require 'uv/tasks/win'
22
+ require File.join File.expand_path("../", __FILE__), 'tasks/win'
23
23
  elsif FFI::Platform.mac?
24
- require 'uv/tasks/mac'
24
+ require File.join File.expand_path("../", __FILE__), 'tasks/mac'
25
25
  else # UNIX
26
- require 'uv/tasks/unix'
26
+ require File.join File.expand_path("../", __FILE__), 'tasks/unix'
27
27
  end
@@ -5,13 +5,13 @@ file 'ext/libuv/uv.xcodeproj' => 'ext/libuv/build/gyp' do
5
5
  abort "Don't know how to build on #{FFI::Platform::ARCH} (yet)" unless target_arch
6
6
 
7
7
  Dir.chdir("ext/libuv") do |path|
8
- system "./gyp_uv -f xcode -Dtarget_arch=#{target_arch}"
8
+ system "./gyp_uv -f xcode -Dtarget_arch=#{target_arch} -Dlibrary=shared_library -Dcomponent=shared_library"
9
9
  end
10
10
  end
11
11
 
12
12
  file "ext/libuv/build/Release/libuv.#{FFI::Platform::LIBSUFFIX}" => 'ext/libuv/uv.xcodeproj' do
13
13
  Dir.chdir("ext/libuv") do |path|
14
- system 'xcodebuild -project uv.xcodeproj -configuration Release -target All'
14
+ system 'xcodebuild -project uv.xcodeproj -configuration Release -target libuv'
15
15
  end
16
16
  end
17
17
 
@@ -5,7 +5,7 @@ file 'ext/libuv/out' => 'ext/libuv/build/gyp' do
5
5
  abort "Don't know how to build on #{FFI::Platform::ARCH} (yet)" unless target_arch
6
6
 
7
7
  Dir.chdir("ext/libuv") do |path|
8
- system "./gyp_uv -f make -Dtarget_arch=#{target_arch}"
8
+ system "./gyp_uv -f make -Dtarget_arch=#{target_arch} -Dlibrary=shared_library -Dcomponent=shared_library"
9
9
  end
10
10
  end
11
11
 
@@ -1,2 +1,11 @@
1
- abort "can't build on windows using this rake task (yet)"
2
- configure_command = 'vcbuild.bat'
1
+ file "ext/libuv/Release/libuv.#{FFI::Platform::LIBSUFFIX}" do
2
+ Dir.chdir("ext/libuv") do |path|
3
+ system 'vcbuild.bat', 'shared', 'release'
4
+ end
5
+ end
6
+
7
+ file "ext/libuv.#{FFI::Platform::LIBSUFFIX}" => "ext/libuv/Release/libuv.#{FFI::Platform::LIBSUFFIX}" do
8
+ FileUtils.mv("ext/libuv/Release/libuv.#{FFI::Platform::LIBSUFFIX}", "ext/libuv.#{FFI::Platform::LIBSUFFIX}")
9
+ end
10
+
11
+ CLOBBER.include("ext/libuv/Release/libuv.#{FFI::Platform::LIBSUFFIX}")
@@ -1,6 +1,6 @@
1
1
  module UV
2
2
  class Timer
3
- include Handle
3
+ include Assertions, Handle
4
4
 
5
5
  def start(timeout, repeat, &block)
6
6
  assert_block(block)
@@ -12,31 +12,35 @@ module UV
12
12
 
13
13
  enum :uv_handle_type, [
14
14
  :uv_unknown_handle, 0,
15
- :uv_async,
15
+ :uv_async, # start UV_HANDLE_TYPE_MAP
16
16
  :uv_check,
17
17
  :uv_fs_event,
18
+ :uv_fs_poll,
19
+ :uv_handle,
18
20
  :uv_idle,
19
21
  :uv_pipe,
20
22
  :uv_poll,
21
23
  :uv_prepare,
22
24
  :uv_process,
25
+ :uv_stream,
23
26
  :uv_tcp,
24
27
  :uv_timer,
25
28
  :uv_tty,
26
29
  :uv_udp,
27
- :uv_ares_task,
30
+ :uv_signal, # end UV_HANDLE_TYPE_MAP
28
31
  :uv_file,
29
32
  :uv_handle_type_max
30
33
  ]
31
34
  enum :uv_req_type, [
32
35
  :uv_unknown_req, 0,
36
+ :uv_req, # start UV_REQ_TYPE_MAP
33
37
  :uv_connect,
34
38
  :uv_write,
35
39
  :uv_shutdown,
36
40
  :uv_udp_send,
37
41
  :uv_fs,
38
42
  :uv_work,
39
- :uv_getaddrinfo,
43
+ :uv_getaddrinfo, # end UV_REQ_TYPE_MAP
40
44
  :uv_req_type_private,
41
45
  :uv_req_type_max
42
46
  ]
@@ -44,65 +48,6 @@ module UV
44
48
  :uv_leave_group, 0,
45
49
  :uv_join_group
46
50
  ]
47
- enum :uv_err_code, [
48
- :UNKNOWN, -1,
49
- :OK, 0,
50
- :EOF, 1,
51
- :EADDRINFO, 2,
52
- :EACCES, 3,
53
- :EAGAIN, 4,
54
- :EADDRINUSE, 5,
55
- :EADDRNOTAVAIL, 6,
56
- :EAFNOSUPPORT, 7,
57
- :EALREADY, 8,
58
- :EBADF, 9,
59
- :EBUSY, 10,
60
- :ECONNABORTED, 11,
61
- :ECONNREFUSED, 12,
62
- :ECONNRESET, 13,
63
- :EDESTADDRREQ, 14,
64
- :EFAULT, 15,
65
- :EHOSTUNREACH, 16,
66
- :EINTR, 17,
67
- :EINVAL, 18,
68
- :EISCONN, 19,
69
- :EMFILE, 20,
70
- :EMSGSIZE, 21,
71
- :ENETDOWN, 22,
72
- :ENETUNREACH, 23,
73
- :ENFILE, 24,
74
- :ENOBUFS, 25,
75
- :ENOMEM, 26,
76
- :ENOTDIR, 27,
77
- :EISDIR, 28,
78
- :ENONET, 29,
79
- :ENOTCONN, 31,
80
- :ENOTSOCK, 32,
81
- :ENOTSUP, 33,
82
- :ENOENT, 34,
83
- :ENOSYS, 35,
84
- :EPIPE, 36,
85
- :EPROTO, 37,
86
- :EPROTONOSUPPORT, 38,
87
- :EPROTOTYPE, 39,
88
- :ETIMEDOUT, 40,
89
- :ECHARSET, 41,
90
- :EAIFAMNOSUPPORT, 42,
91
- :EAISERVICE, 44,
92
- :EAISOCKTYPE, 45,
93
- :ESHUTDOWN, 46,
94
- :EEXIST, 47,
95
- :ESRCH, 48,
96
- :ENAMETOOLONG, 49,
97
- :EPERM, 50,
98
- :ELOOP, 51,
99
- :EXDEV, 52,
100
- :ENOTEMPTY, 53,
101
- :ENOSPC, 54,
102
- :EIO, 55,
103
- :EROFS, 56,
104
- :UV_MAX_ERRORS
105
- ]
106
51
  enum :uv_fs_type, [
107
52
  :UV_FS_UNKNOWN, -1,
108
53
  :UV_FS_CUSTOM,
@@ -136,16 +81,15 @@ module UV
136
81
  :UV_RENAME, 1,
137
82
  :UV_CHANGE, 2
138
83
  ]
84
+ enum :uv_run_mode, [
85
+ :UV_RUN_DEFAULT, 0,
86
+ :UV_RUN_ONCE,
87
+ :UV_RUN_NOWAIT
88
+ ]
139
89
 
140
90
  typedef UvBuf.by_value, :uv_buf_t
141
91
  typedef UvFSStat.by_value, :uv_fs_stat_t
142
92
 
143
- class UvErr < FFI::Struct
144
- layout :code, :uv_err_code,
145
- :sys_errno_, :int
146
- end
147
-
148
- typedef UvErr.by_value, :uv_err_t
149
93
 
150
94
  class Sockaddr < FFI::Struct
151
95
  layout :sa_len, :uint8,
@@ -187,8 +131,36 @@ module UV
187
131
 
188
132
  typedef SockaddrIn6.by_value, :sockaddr_in6
189
133
 
134
+
135
+ class UvTimespec < FFI::Struct
136
+ layout :tv_sec, :long,
137
+ :tv_nsec, :long
138
+ end
139
+
140
+ class UvStat < FFI::Struct
141
+ layout :st_dev, :uint64,
142
+ :st_mode, :uint64,
143
+ :st_nlink, :uint64,
144
+ :st_uid, :uint64,
145
+ :st_gid, :uint64,
146
+ :st_rdev, :uint64,
147
+ :st_ino, :uint64,
148
+ :st_size, :uint64,
149
+ :st_blksize, :uint64,
150
+ :st_blocks, :uint64,
151
+ :st_flags, :uint64,
152
+ :st_gen, :uint64,
153
+ :st_atim, UvTimespec,
154
+ :st_mtim, UvTimespec,
155
+ :st_ctim, UvTimespec,
156
+ :st_birthtim, UvTimespec
157
+ end
158
+
159
+ typedef UvStat.by_value, :uv_stat_t
160
+
190
161
  typedef :pointer, :uv_handle_t
191
162
  typedef :pointer, :uv_fs_event_t
163
+ typedef :pointer, :uv_fs_poll_t
192
164
  typedef :pointer, :uv_stream_t
193
165
  typedef :pointer, :uv_tcp_t
194
166
  typedef :pointer, :uv_udp_t
@@ -222,7 +194,10 @@ module UV
222
194
  typedef :pointer, :uv_rwlock_t
223
195
  typedef :pointer, :uv_once_t
224
196
  typedef :pointer, :uv_thread_t
197
+ typedef :pointer, :uv_poll_t
198
+ typedef :pointer, :uv_stat_t
225
199
  typedef :int, :status
200
+ typedef :int, :events
226
201
 
227
202
  callback :uv_alloc_cb, [:uv_handle_t, :size_t], :uv_buf_t
228
203
  callback :uv_read_cb, [:uv_stream_t, :ssize_t, :uv_buf_t], :void
@@ -232,6 +207,7 @@ module UV
232
207
  callback :uv_shutdown_cb, [:uv_shutdown_t, :status], :void
233
208
  callback :uv_connection_cb, [:uv_stream_t, :status], :void
234
209
  callback :uv_close_cb, [:uv_handle_t], :void
210
+ callback :uv_poll_cb, [:uv_poll_t, :status, :events], :void
235
211
  callback :uv_timer_cb, [:uv_timer_t, :status], :void
236
212
  callback :uv_async_cb, [:uv_async_t, :status], :void
237
213
  callback :uv_prepare_cb, [:uv_prepare_t, :status], :void
@@ -239,11 +215,14 @@ module UV
239
215
  callback :uv_idle_cb, [:uv_idle_t, :status], :void
240
216
  callback :uv_getaddrinfo_cb, [:uv_getaddrinfo_t, :status, :addrinfo], :void
241
217
  callback :uv_exit_cb, [:uv_process_t, :int, :int], :void
218
+ callback :uv_walk_cb, [:uv_handle_t, :pointer], :void
242
219
  callback :uv_fs_cb, [:uv_fs_t], :void
243
220
  callback :uv_work_cb, [:uv_work_t], :void
244
221
  callback :uv_after_work_cb, [:uv_work_t], :void
245
222
  callback :uv_fs_event_cb, [:uv_fs_event_t, :string, :int, :int], :void
223
+ callback :uv_fs_poll_cb, [:uv_fs_poll_t, :status, :uv_stat_t, :uv_stat_t], :void
224
+ #callback :uv_signal_cb, []
246
225
  callback :uv_udp_send_cb, [:uv_udp_send_t, :int], :void
247
- callback :uv_udp_recv_cb, [:uv_udp_t, :ssize_t, :uv_buf_t, :pointer, :uint], :void
226
+ callback :uv_udp_recv_cb, [:uv_udp_t, :ssize_t, :uv_buf_t, Sockaddr, :uint], :void
248
227
  callback :uv_cb, [], :void
249
228
  end