libuv 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +17 -0
  3. data/.gitmodules +3 -0
  4. data/.rspec +1 -0
  5. data/.travis.yml +16 -0
  6. data/Gemfile +2 -0
  7. data/LICENSE +24 -0
  8. data/README.md +73 -0
  9. data/Rakefile +31 -0
  10. data/lib/libuv.rb +34 -0
  11. data/lib/libuv/assertions.rb +23 -0
  12. data/lib/libuv/async.rb +33 -0
  13. data/lib/libuv/check.rb +49 -0
  14. data/lib/libuv/error.rb +70 -0
  15. data/lib/libuv/ext/ext.rb +257 -0
  16. data/lib/libuv/ext/platform/darwin_x64.rb +12 -0
  17. data/lib/libuv/ext/platform/linux.rb +8 -0
  18. data/lib/libuv/ext/platform/unix.rb +14 -0
  19. data/lib/libuv/ext/platform/windows.rb +27 -0
  20. data/lib/libuv/ext/tasks.rb +27 -0
  21. data/lib/libuv/ext/tasks/mac.rb +23 -0
  22. data/lib/libuv/ext/tasks/unix.rb +23 -0
  23. data/lib/libuv/ext/tasks/win.rb +11 -0
  24. data/lib/libuv/ext/types.rb +230 -0
  25. data/lib/libuv/fs_event.rb +31 -0
  26. data/lib/libuv/handle.rb +82 -0
  27. data/lib/libuv/idle.rb +49 -0
  28. data/lib/libuv/listener.rb +34 -0
  29. data/lib/libuv/loop.rb +310 -0
  30. data/lib/libuv/net.rb +38 -0
  31. data/lib/libuv/pipe.rb +97 -0
  32. data/lib/libuv/prepare.rb +49 -0
  33. data/lib/libuv/q.rb +429 -0
  34. data/lib/libuv/resource.rb +28 -0
  35. data/lib/libuv/simple_async.rb +28 -0
  36. data/lib/libuv/stream.rb +124 -0
  37. data/lib/libuv/tcp.rb +194 -0
  38. data/lib/libuv/timer.rb +75 -0
  39. data/lib/libuv/tty.rb +34 -0
  40. data/lib/libuv/udp.rb +256 -0
  41. data/lib/libuv/version.rb +3 -0
  42. data/lib/libuv/work.rb +62 -0
  43. data/libuv.gemspec +54 -0
  44. data/spec/async_spec.rb +60 -0
  45. data/spec/defer_spec.rb +980 -0
  46. data/spec/idle_spec.rb +56 -0
  47. data/spec/pipe_spec.rb +148 -0
  48. data/spec/tcp_spec.rb +188 -0
  49. metadata +382 -0
@@ -0,0 +1,12 @@
1
+ module Libuv
2
+ module Ext
3
+ class UvFSStat < FFI::Struct
4
+ layout :st_dev, :dev_t, :st_mode, :mode_t, :st_nlink, :nlink_t,
5
+ :st_ino, :ino_t, :st_uid, :uid_t, :st_gid, :gid_t, :st_rdev,
6
+ :dev_t, :st_atime, :time_t, :st_mtime, :time_t, :st_ctime,
7
+ :time_t, :st_size, :off_t, :st_blocks, :blkcnt_t, :st_blksize,
8
+ :blksize_t, :st_flags, :uint32, :st_gen, :uint32, :st_lspare,
9
+ :int32, :st_qspare_0, :int64, :st_qspare_1, :int64
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module Libuv
2
+ module Ext
3
+ # blksize_t, in_addr_t is not yet part of types.conf on linux
4
+ typedef :long, :blksize_t
5
+ typedef :uint32, :in_addr_t
6
+ typedef :ushort, :in_port_t
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ module Libuv
2
+ module Ext
3
+ class UvBuf < FFI::Struct
4
+ layout :base, :pointer, :len, :size_t
5
+ end
6
+
7
+ class UvFSStat < FFI::Struct
8
+ layout :st_dev, :dev_t, :st_ino, :ino_t, :st_mode, :mode_t, :st_nlink, :nlink_t,
9
+ :st_uid, :uid_t, :st_gid, :gid_t, :st_rdev, :dev_t, :st_size, :off_t,
10
+ :st_blksize, :blksize_t, :st_blocks, :blkcnt_t, :st_atime, :time_t,
11
+ :st_mtime, :time_t, :st_ctime, :time_t
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ module Libuv
2
+ module Ext
3
+ typedef :uint32_t, :in_addr_t
4
+ typedef :uint16, :in_port_t
5
+ typedef :int, :mode_t
6
+
7
+ module WS2
8
+ extend FFI::Library
9
+ ffi_lib('Ws2_32.dll').first # this is for ntohs
10
+ attach_function :ntohs, [:ushort], :ushort, :blocking => true
11
+ end
12
+ def_delegators :WS2, :ntohs
13
+ module_function :ntohs
14
+
15
+ # win32 has a different uv_buf_t layout to everything else.
16
+ class UvBuf < FFI::Struct
17
+ layout :len, :ulong, :base, :pointer
18
+ end
19
+
20
+ # win32 uses _stati64
21
+ class UvFSStat < FFI::Struct
22
+ layout :st_gid, :gid_t, :st_atime, :time_t, :st_ctime, :time_t, :st_dev, :dev_t,
23
+ :st_ino, :ino_t, :st_mode, :mode_t, :st_mtime, :time_t, :st_nlink, :nlink_t,
24
+ :st_rdev, :dev_t, :st_size, :off_t, :st_uid, :uid_t
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module FFI::Platform
2
+ def self.ia32?
3
+ ARCH == "i386"
4
+ end
5
+
6
+ def self.x64?
7
+ ARCH == "x86_64"
8
+ end
9
+ end
10
+
11
+ file 'ext/libuv/build' do
12
+ system "git", "submodule", "update", "--init"
13
+ end
14
+
15
+ file 'ext/libuv/build/gyp' => 'ext/libuv/build' do
16
+ system "svn", "export", "-r1214", "http://gyp.googlecode.com/svn/trunk", "ext/libuv/build/gyp"
17
+ end
18
+
19
+ CLEAN.include('ext/libuv/build/gyp')
20
+
21
+ if FFI::Platform.windows?
22
+ require File.join File.expand_path("../", __FILE__), 'tasks/win'
23
+ elsif FFI::Platform.mac?
24
+ require File.join File.expand_path("../", __FILE__), 'tasks/mac'
25
+ else # UNIX
26
+ require File.join File.expand_path("../", __FILE__), 'tasks/unix'
27
+ end
@@ -0,0 +1,23 @@
1
+ file 'ext/libuv/uv.xcodeproj' => 'ext/libuv/build/gyp' do
2
+ target_arch = 'ia32'if FFI::Platform.ia32?
3
+ target_arch = 'x64' if FFI::Platform.x64?
4
+
5
+ abort "Don't know how to build on #{FFI::Platform::ARCH} (yet)" unless target_arch
6
+
7
+ Dir.chdir("ext/libuv") do |path|
8
+ system "./gyp_uv -f xcode -Dtarget_arch=#{target_arch} -Dlibrary=shared_library -Dcomponent=shared_library"
9
+ end
10
+ end
11
+
12
+ file "ext/libuv/build/Release/libuv.#{FFI::Platform::LIBSUFFIX}" => 'ext/libuv/uv.xcodeproj' do
13
+ Dir.chdir("ext/libuv") do |path|
14
+ system 'xcodebuild -project uv.xcodeproj -configuration Release -target libuv'
15
+ end
16
+ end
17
+
18
+ file "ext/libuv.#{FFI::Platform::LIBSUFFIX}" => "ext/libuv/build/Release/libuv.#{FFI::Platform::LIBSUFFIX}" do
19
+ File.symlink("libuv/build/Release/libuv.#{FFI::Platform::LIBSUFFIX}", "ext/libuv.#{FFI::Platform::LIBSUFFIX}")
20
+ end
21
+
22
+ CLEAN.include('ext/libuv/uv.xcodeproj')
23
+ CLOBBER.include("ext/libuv/build/Release/libuv.#{FFI::Platform::LIBSUFFIX}")
@@ -0,0 +1,23 @@
1
+ file 'ext/libuv/out' => 'ext/libuv/build/gyp' do
2
+ target_arch = 'ia32'if FFI::Platform.ia32?
3
+ target_arch = 'x64' if FFI::Platform.x64?
4
+
5
+ abort "Don't know how to build on #{FFI::Platform::ARCH} (yet)" unless target_arch
6
+
7
+ Dir.chdir("ext/libuv") do |path|
8
+ system "./gyp_uv -f make -Dtarget_arch=#{target_arch} -Dlibrary=shared_library -Dcomponent=shared_library"
9
+ end
10
+ end
11
+
12
+ file "ext/libuv/out/Release/lib.target/libuv.#{FFI::Platform::LIBSUFFIX}" => 'ext/libuv/out' do
13
+ Dir.chdir("ext/libuv") do |path|
14
+ system 'make -C out BUILDTYPE=Release'
15
+ end
16
+ end
17
+
18
+ file "ext/libuv.#{FFI::Platform::LIBSUFFIX}" => "ext/libuv/out/Release/lib.target/libuv.#{FFI::Platform::LIBSUFFIX}" do
19
+ File.symlink("libuv/out/Release/lib.target/libuv.#{FFI::Platform::LIBSUFFIX}", "ext/libuv.#{FFI::Platform::LIBSUFFIX}")
20
+ end
21
+
22
+ CLEAN.include('ext/libuv/out')
23
+ CLOBBER.include("ext/libuv/out/Release/lib.target/libuv.#{FFI::Platform::LIBSUFFIX}")
@@ -0,0 +1,11 @@
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}")
@@ -0,0 +1,230 @@
1
+ module FFI::Platform
2
+ def self.linux?
3
+ IS_LINUX
4
+ end
5
+ end
6
+
7
+ module Libuv
8
+ module Ext
9
+ require 'libuv/ext/platform/linux.rb' if FFI::Platform.linux?
10
+ require 'libuv/ext/platform/unix.rb' if FFI::Platform.unix?
11
+ require 'libuv/ext/platform/darwin_x64.rb' if FFI::Platform.mac? and FFI::Platform::ARCH == 'x86_64'
12
+ require 'libuv/ext/platform/windows.rb' if FFI::Platform.windows?
13
+
14
+ enum :uv_handle_type, [
15
+ :uv_unknown_handle, 0,
16
+ :uv_async, # start UV_HANDLE_TYPE_MAP
17
+ :uv_check,
18
+ :uv_fs_event,
19
+ :uv_fs_poll,
20
+ :uv_handle,
21
+ :uv_idle,
22
+ :uv_pipe,
23
+ :uv_poll,
24
+ :uv_prepare,
25
+ :uv_process,
26
+ :uv_stream,
27
+ :uv_tcp,
28
+ :uv_timer,
29
+ :uv_tty,
30
+ :uv_udp,
31
+ :uv_signal, # end UV_HANDLE_TYPE_MAP
32
+ :uv_file,
33
+ :uv_handle_type_max
34
+ ]
35
+ enum :uv_req_type, [
36
+ :uv_unknown_req, 0,
37
+ :uv_req, # start UV_REQ_TYPE_MAP
38
+ :uv_connect,
39
+ :uv_write,
40
+ :uv_shutdown,
41
+ :uv_udp_send,
42
+ :uv_fs,
43
+ :uv_work,
44
+ :uv_getaddrinfo, # end UV_REQ_TYPE_MAP
45
+ :uv_req_type_private,
46
+ :uv_req_type_max
47
+ ]
48
+ enum :uv_membership, [
49
+ :uv_leave_group, 0,
50
+ :uv_join_group
51
+ ]
52
+ enum :uv_fs_type, [
53
+ :UV_FS_UNKNOWN, -1,
54
+ :UV_FS_CUSTOM,
55
+ :UV_FS_OPEN,
56
+ :UV_FS_CLOSE,
57
+ :UV_FS_READ,
58
+ :UV_FS_WRITE,
59
+ :UV_FS_SENDFILE,
60
+ :UV_FS_STAT,
61
+ :UV_FS_LSTAT,
62
+ :UV_FS_FSTAT,
63
+ :UV_FS_FTRUNCATE,
64
+ :UV_FS_UTIME,
65
+ :UV_FS_FUTIME,
66
+ :UV_FS_CHMOD,
67
+ :UV_FS_FCHMOD,
68
+ :UV_FS_FSYNC,
69
+ :UV_FS_FDATASYNC,
70
+ :UV_FS_UNLINK,
71
+ :UV_FS_RMDIR,
72
+ :UV_FS_MKDIR,
73
+ :UV_FS_RENAME,
74
+ :UV_FS_READDIR,
75
+ :UV_FS_LINK,
76
+ :UV_FS_SYMLINK,
77
+ :UV_FS_READLINK,
78
+ :UV_FS_CHOWN,
79
+ :UV_FS_FCHOWN
80
+ ]
81
+ enum :uv_fs_event, [
82
+ :UV_RENAME, 1,
83
+ :UV_CHANGE, 2
84
+ ]
85
+ enum :uv_run_mode, [
86
+ :UV_RUN_DEFAULT, 0,
87
+ :UV_RUN_ONCE,
88
+ :UV_RUN_NOWAIT
89
+ ]
90
+
91
+ typedef UvBuf.by_value, :uv_buf_t
92
+ typedef UvFSStat.by_value, :uv_fs_stat_t
93
+
94
+
95
+ class Sockaddr < FFI::Struct
96
+ layout :sa_len, :uint8,
97
+ :sa_family, :sa_family_t,
98
+ :sa_data, [:char, 14]
99
+ end
100
+
101
+ class InAddr < FFI::Struct
102
+ layout :s_addr, :in_addr_t
103
+ end
104
+
105
+ class SockaddrIn < FFI::Struct
106
+ layout :sin_len, :uint8,
107
+ :sin_family, :sa_family_t,
108
+ :sin_port, :in_port_t,
109
+ :sin_addr, InAddr,
110
+ :sin_zero, [:char, 8]
111
+ end
112
+
113
+ typedef SockaddrIn.by_value, :sockaddr_in
114
+
115
+ class U6Addr < FFI::Union
116
+ layout :__u6_addr8, [:uint8, 16],
117
+ :__u6_addr16, [:uint16, 8]
118
+ end
119
+
120
+ class In6Addr < FFI::Struct
121
+ layout :__u6_addr, U6Addr
122
+ end
123
+
124
+ class SockaddrIn6 < FFI::Struct
125
+ layout :sin6_len, :uint8,
126
+ :sin6_family, :sa_family_t,
127
+ :sin6_port, :in_port_t,
128
+ :sin6_flowinfo, :uint32,
129
+ :sin6_addr, In6Addr,
130
+ :sin6_scope_id, :uint32
131
+ end
132
+
133
+ typedef SockaddrIn6.by_value, :sockaddr_in6
134
+
135
+
136
+ class UvTimespec < FFI::Struct
137
+ layout :tv_sec, :long,
138
+ :tv_nsec, :long
139
+ end
140
+
141
+ class UvStat < FFI::Struct
142
+ layout :st_dev, :uint64,
143
+ :st_mode, :uint64,
144
+ :st_nlink, :uint64,
145
+ :st_uid, :uint64,
146
+ :st_gid, :uint64,
147
+ :st_rdev, :uint64,
148
+ :st_ino, :uint64,
149
+ :st_size, :uint64,
150
+ :st_blksize, :uint64,
151
+ :st_blocks, :uint64,
152
+ :st_flags, :uint64,
153
+ :st_gen, :uint64,
154
+ :st_atim, UvTimespec,
155
+ :st_mtim, UvTimespec,
156
+ :st_ctim, UvTimespec,
157
+ :st_birthtim, UvTimespec
158
+ end
159
+
160
+ typedef UvStat.by_value, :uv_stat_t
161
+
162
+ typedef :pointer, :uv_handle_t
163
+ typedef :pointer, :uv_fs_event_t
164
+ typedef :pointer, :uv_fs_poll_t
165
+ typedef :pointer, :uv_stream_t
166
+ typedef :pointer, :uv_tcp_t
167
+ typedef :pointer, :uv_udp_t
168
+ typedef :pointer, :uv_tty_t
169
+ typedef :pointer, :uv_pipe_t
170
+ typedef :pointer, :uv_prepare_t
171
+ typedef :pointer, :uv_check_t
172
+ typedef :pointer, :uv_idle_t
173
+ typedef :pointer, :uv_async_t
174
+ typedef :pointer, :uv_timer_t
175
+ typedef :pointer, :uv_process_t
176
+ typedef :pointer, :uv_getaddrinfo_cb
177
+ typedef :pointer, :addrinfo
178
+ typedef :pointer, :uv_fs_t
179
+ typedef :pointer, :uv_work_t
180
+ typedef :pointer, :uv_loop_t
181
+ typedef :pointer, :uv_shutdown_t
182
+ typedef :pointer, :uv_write_t
183
+ typedef :pointer, :uv_connect_t
184
+ typedef :pointer, :uv_udp_send_t
185
+ typedef :int, :uv_file
186
+ typedef :pointer, :ares_channel
187
+ typedef :pointer, :ares_options
188
+ typedef :pointer, :uv_getaddrinfo_t
189
+ typedef :pointer, :uv_options_t
190
+ typedef :pointer, :uv_cpu_info_t
191
+ typedef :pointer, :uv_interface_address_t
192
+ typedef :pointer, :uv_fs_t
193
+ typedef :pointer, :uv_lib_t
194
+ typedef :pointer, :uv_mutex_t
195
+ typedef :pointer, :uv_rwlock_t
196
+ typedef :pointer, :uv_once_t
197
+ typedef :pointer, :uv_thread_t
198
+ typedef :pointer, :uv_poll_t
199
+ typedef :pointer, :uv_stat_t
200
+ typedef :int, :status
201
+ typedef :int, :events
202
+
203
+ callback :uv_alloc_cb, [:uv_handle_t, :size_t], :uv_buf_t
204
+ callback :uv_read_cb, [:uv_stream_t, :ssize_t, :uv_buf_t], :void
205
+ callback :uv_read2_cb, [:uv_pipe_t, :ssize_t, :uv_buf_t, :uv_handle_type], :void
206
+ callback :uv_write_cb, [:uv_write_t, :status], :void
207
+ callback :uv_connect_cb, [:uv_connect_t, :status], :void
208
+ callback :uv_shutdown_cb, [:uv_shutdown_t, :status], :void
209
+ callback :uv_connection_cb, [:uv_stream_t, :status], :void
210
+ callback :uv_close_cb, [:uv_handle_t], :void
211
+ callback :uv_poll_cb, [:uv_poll_t, :status, :events], :void
212
+ callback :uv_timer_cb, [:uv_timer_t, :status], :void
213
+ callback :uv_async_cb, [:uv_async_t, :status], :void
214
+ callback :uv_prepare_cb, [:uv_prepare_t, :status], :void
215
+ callback :uv_check_cb, [:uv_check_t, :status], :void
216
+ callback :uv_idle_cb, [:uv_idle_t, :status], :void
217
+ callback :uv_getaddrinfo_cb, [:uv_getaddrinfo_t, :status, :addrinfo], :void
218
+ callback :uv_exit_cb, [:uv_process_t, :int, :int], :void
219
+ callback :uv_walk_cb, [:uv_handle_t, :pointer], :void
220
+ callback :uv_fs_cb, [:uv_fs_t], :void
221
+ callback :uv_work_cb, [:uv_work_t], :void
222
+ callback :uv_after_work_cb, [:uv_work_t, :int], :void
223
+ callback :uv_fs_event_cb, [:uv_fs_event_t, :string, :int, :int], :void
224
+ callback :uv_fs_poll_cb, [:uv_fs_poll_t, :status, :uv_stat_t, :uv_stat_t], :void
225
+ #callback :uv_signal_cb, []
226
+ callback :uv_udp_send_cb, [:uv_udp_send_t, :int], :void
227
+ callback :uv_udp_recv_cb, [:uv_udp_t, :ssize_t, :uv_buf_t, Sockaddr, :uint], :void
228
+ callback :uv_cb, [], :void
229
+ end
230
+ end
@@ -0,0 +1,31 @@
1
+ module Libuv
2
+ class FSEvent < Handle
3
+
4
+
5
+ EVENTS = {1 => :rename, 2 => :change}.freeze
6
+
7
+
8
+ def initialize(loop, path)
9
+ @loop = loop
10
+
11
+ fs_event_ptr = ::Libuv::Ext.create_handle(:uv_fs_event)
12
+ error = check_result ::Libuv::Ext.fs_event_init(loop.handle, fs_event_ptr, path, callback(:on_fs_event), 0)
13
+
14
+ super(fs_event_ptr, error)
15
+ end
16
+
17
+
18
+ private
19
+
20
+
21
+ def on_fs_event(handle, filename, events, status)
22
+ e = check_result(status)
23
+
24
+ if e
25
+ reject(e)
26
+ else
27
+ defer.notify(filename, EVENTS[events]) # notify of a change
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,82 @@
1
+ module Libuv
2
+ class Handle < Q::DeferredPromise
3
+ include Assertions, Resource, Listener
4
+
5
+
6
+ def initialize(pointer, error)
7
+ @pointer = pointer
8
+
9
+ # Initialise the promise
10
+ super(loop, loop.defer)
11
+
12
+ # clean up on init error (always raise here)
13
+ if error
14
+ ::Libuv::Ext.free(pointer)
15
+ defer.reject(error)
16
+ @closed = true
17
+ raise error
18
+ end
19
+ end
20
+
21
+ # Public: Increment internal ref counter for the handle on the loop. Useful for
22
+ # extending the loop with custom watchers that need to make loop not stop
23
+ #
24
+ # Returns self
25
+ def ref
26
+ return if @closed
27
+ ::Libuv::Ext.ref(handle)
28
+ end
29
+
30
+ # Public: Decrement internal ref counter for the handle on the loop, useful to stop
31
+ # loop even when there are outstanding open handles
32
+ #
33
+ # Returns self
34
+ def unref
35
+ return if @closed
36
+ ::Libuv::Ext.unref(handle)
37
+ end
38
+
39
+ def close
40
+ return if @closed
41
+ @closed = true
42
+ Libuv::Ext.close(handle, callback(:on_close))
43
+ end
44
+
45
+ def active?
46
+ ::Libuv::Ext.is_active(handle) > 0
47
+ end
48
+
49
+ def closing?
50
+ ::Libuv::Ext.is_closing(handle) > 0
51
+ end
52
+
53
+
54
+ protected
55
+
56
+
57
+ def loop; @loop; end
58
+ def handle; @pointer; end
59
+ def defer; @defer; end
60
+
61
+
62
+ private
63
+
64
+
65
+ # Clean up and throw an error
66
+ def reject(reason)
67
+ @close_error = reason
68
+ close
69
+ end
70
+
71
+ def on_close(pointer)
72
+ ::Libuv::Ext.free(pointer)
73
+ clear_callbacks
74
+
75
+ if @close_error
76
+ defer.reject(@close_error)
77
+ else
78
+ defer.resolve(nil)
79
+ end
80
+ end
81
+ end
82
+ end