libuv 0.11.21 → 0.11.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +17 -17
  3. data/.gitmodules +3 -3
  4. data/.rspec +1 -1
  5. data/.travis.yml +16 -16
  6. data/Gemfile +4 -4
  7. data/LICENSE +23 -23
  8. data/README.md +89 -88
  9. data/Rakefile +31 -31
  10. data/lib/libuv.rb +54 -54
  11. data/lib/libuv/async.rb +47 -47
  12. data/lib/libuv/check.rb +55 -55
  13. data/lib/libuv/dns.rb +85 -85
  14. data/lib/libuv/error.rb +74 -73
  15. data/lib/libuv/ext/ext.rb +258 -258
  16. data/lib/libuv/ext/platform/darwin_x64.rb +23 -23
  17. data/lib/libuv/ext/platform/linux.rb +7 -7
  18. data/lib/libuv/ext/platform/unix.rb +29 -29
  19. data/lib/libuv/ext/platform/windows.rb +40 -40
  20. data/lib/libuv/ext/tasks.rb +31 -31
  21. data/lib/libuv/ext/tasks/mac.rb +23 -23
  22. data/lib/libuv/ext/tasks/unix.rb +23 -23
  23. data/lib/libuv/ext/tasks/win.rb +14 -14
  24. data/lib/libuv/ext/types.rb +238 -238
  25. data/lib/libuv/file.rb +269 -269
  26. data/lib/libuv/filesystem.rb +232 -232
  27. data/lib/libuv/fs_event.rb +31 -31
  28. data/lib/libuv/handle.rb +85 -85
  29. data/lib/libuv/idle.rb +56 -56
  30. data/lib/libuv/loop.rb +412 -412
  31. data/lib/libuv/mixins/assertions.rb +23 -23
  32. data/lib/libuv/mixins/fs_checks.rb +58 -58
  33. data/lib/libuv/mixins/listener.rb +34 -34
  34. data/lib/libuv/mixins/net.rb +40 -40
  35. data/lib/libuv/mixins/resource.rb +27 -27
  36. data/lib/libuv/mixins/stream.rb +154 -154
  37. data/lib/libuv/pipe.rb +203 -203
  38. data/lib/libuv/prepare.rb +56 -56
  39. data/lib/libuv/signal.rb +51 -51
  40. data/lib/libuv/tcp.rb +334 -332
  41. data/lib/libuv/timer.rb +85 -85
  42. data/lib/libuv/tty.rb +37 -37
  43. data/lib/libuv/udp.rb +240 -240
  44. data/lib/libuv/version.rb +3 -3
  45. data/lib/libuv/work.rb +75 -75
  46. data/libuv.gemspec +56 -56
  47. data/spec/async_spec.rb +60 -60
  48. data/spec/cpu_spec.rb +10 -10
  49. data/spec/defer_spec.rb +980 -980
  50. data/spec/dns_spec.rb +90 -90
  51. data/spec/filesystem_spec.rb +261 -250
  52. data/spec/idle_spec.rb +56 -56
  53. data/spec/pipe_spec.rb +160 -160
  54. data/spec/tcp_spec.rb +267 -267
  55. metadata +55 -61
data/lib/libuv/file.rb CHANGED
@@ -1,270 +1,270 @@
1
- module Libuv
2
- class File < Q::DeferredPromise
3
- include Assertions, Resource, Listener, FsChecks
4
-
5
-
6
- EOF = "0\r\n\r\n".freeze
7
- CRLF = "\r\n".freeze
8
-
9
-
10
- attr_reader :fileno, :closed
11
-
12
-
13
- def initialize(loop, path, flags = 0, mode = 0)
14
- super(loop, loop.defer)
15
-
16
- @fileno = -1
17
- @closed = true
18
- @path, @flags, @mode = path, flags, mode
19
-
20
- request = ::Libuv::Ext.create_request(:uv_fs)
21
- pre_check @defer, request, ::Libuv::Ext.fs_open(@loop, request, @path, @flags, @mode, callback(:on_open))
22
- end
23
-
24
- def close
25
- @closed = true
26
- request = ::Libuv::Ext.create_request(:uv_fs)
27
- pre_check(@defer, request, ::Libuv::Ext.fs_close(@loop.handle, request, @fileno, callback(:on_close)))
28
- end
29
-
30
- def read(length, offset = 0)
31
- assert_type(Integer, length, "length must be an Integer")
32
- assert_type(Integer, offset, "offset must be an Integer")
33
- @read_deferred = @loop.defer
34
-
35
- @read_buffer = FFI::MemoryPointer.new(length)
36
-
37
- request = ::Libuv::Ext.create_request(:uv_fs)
38
- pre_check(@read_deferred, request, ::Libuv::Ext.fs_read(@loop.handle, request, @fileno, @read_buffer, length, offset, callback(:on_read)))
39
- @read_deferred.promise
40
- end
41
-
42
- def write(data, offset = 0)
43
- assert_type(String, data, "data must be a String")
44
- assert_type(Integer, offset, "offset must be an Integer")
45
- @write_deferred = @loop.defer
46
-
47
- length = data.respond_to?(:bytesize) ? data.bytesize : data.size
48
- @write_buffer = FFI::MemoryPointer.from_string(data)
49
-
50
- request = ::Libuv::Ext.create_request(:uv_fs)
51
- pre_check(@write_deferred, request, ::Libuv::Ext.fs_write(@loop.handle, request, @fileno, @write_buffer, length, offset, callback(:on_write)))
52
- @write_deferred.promise
53
- end
54
-
55
- def sync
56
- @sync_deferred = @loop.defer
57
-
58
- request = ::Libuv::Ext.create_request(:uv_fs)
59
- pre_check @sync_deferred, request, ::Libuv::Ext.fs_fsync(@loop.handle, request, @fileno, callback(:on_sync))
60
- @sync_deferred.promise
61
- end
62
-
63
- def datasync
64
- @datasync_deferred = @loop.defer
65
-
66
- request = ::Libuv::Ext.create_request(:uv_fs)
67
- pre_check @datasync_deferred, request, ::Libuv::Ext.fs_fdatasync(@loop.handle, request, @fileno, callback(:on_datasync))
68
- @datasync_deferred.promise
69
- end
70
-
71
- def truncate(offset)
72
- assert_type(Integer, offset, "offset must be an Integer")
73
- @truncate_deferred = @loop.defer
74
-
75
- request = ::Libuv::Ext.create_request(:uv_fs)
76
- pre_check @truncate_deferred, request, ::Libuv::Ext.fs_ftruncate(@loop.handle, request, @fileno, offset, callback(:on_truncate))
77
- @truncate_deferred.promise
78
- end
79
-
80
- def utime(atime, mtime)
81
- assert_type(Integer, atime, "atime must be an Integer")
82
- assert_type(Integer, mtime, "mtime must be an Integer")
83
- @utime_deferred = @loop.defer
84
-
85
- request = ::Libuv::Ext.create_request(:uv_fs)
86
- pre_check @utime_deferred, request, ::Libuv::Ext.fs_futime(@loop.handle, request, @fileno, atime, mtime, callback(:on_utime))
87
- @utime_deferred.promise
88
- end
89
-
90
- def chmod(mode)
91
- assert_type(Integer, mode, "mode must be an Integer")
92
- @chmod_deferred = @loop.defer
93
-
94
- request = ::Libuv::Ext.create_request(:uv_fs)
95
- pre_check @chmod_deferred, request, ::Libuv::Ext.fs_fchmod(@loop.handle, request, @fileno, mode, callback(:on_chmod))
96
- @chmod_deferred.promise
97
- end
98
-
99
- def chown(uid, gid)
100
- assert_type(Integer, uid, "uid must be an Integer")
101
- assert_type(Integer, gid, "gid must be an Integer")
102
- @chown_deferred = @loop.defer
103
-
104
- request = ::Libuv::Ext.create_request(:uv_fs)
105
- pre_check @chown_deferred, request, ::Libuv::Ext.fs_fchown(@loop.handle, request, @fileno, uid, gid, callback(:on_chown))
106
- @chown_deferred.promise
107
- end
108
-
109
- def send_file(stream, type = :raw, chunk_size = 4096)
110
- @transmit_failure ||= method(:transmit_failure)
111
- @transmit_data ||= method(:transmit_data)
112
- @start_transmit ||= method(:start_transmit)
113
- @next_chunk ||= method(:next_chunk)
114
-
115
- @sending_file = @loop.defer
116
- @file_stream = stream
117
- @file_stream_type = type
118
- @file_chunk_size = chunk_size
119
- @file_chunk_count = 0
120
-
121
- stat.then @start_transmit, @transmit_failure
122
-
123
- @sending_file.promise.finally &method(:clean_up_send)
124
- end
125
-
126
-
127
- private
128
-
129
-
130
- ##
131
- # File transmit functions -------------
132
- def start_transmit(stats)
133
- @file_stream_total = stats[:st_size]
134
- next_chunk
135
- end
136
-
137
- def transmit_data(data)
138
- @file_chunk_count += 1
139
- if @file_stream_type == :http
140
- resp = ''
141
- resp << data.bytesize.to_s(16) << CRLF
142
- resp << data
143
- resp << CRLF
144
- data = resp
145
- end
146
- @file_stream.write(data).then @next_chunk, @transmit_failure
147
- nil
148
- end
149
-
150
- def next_chunk(*args)
151
- next_size = @file_chunk_size
152
- next_offset = @file_chunk_size * @file_chunk_count
153
-
154
- if next_offset >= @file_stream_total
155
- if @file_stream_type == :http
156
- @file_stream.write(EOF.dup).then(proc {
157
- @sending_file.resolve(@file_stream_total)
158
- }, @transmit_failure)
159
- else
160
- @sending_file.resolve(@file_stream_total)
161
- end
162
- else
163
- if next_offset + next_size > @file_stream_total
164
- next_size = @file_stream_total - next_offset
165
- end
166
- read(next_size, next_offset).then(@transmit_data, @transmit_failure)
167
- end
168
- nil
169
- end
170
-
171
- def transmit_failure(reason)
172
- @sending_file.reject(reason)
173
- end
174
-
175
- def clean_up_send
176
- @sending_file = nil
177
- @file_stream = nil
178
- @file_stream_type = nil
179
- @file_chunk_size = nil
180
- @file_chunk_count = nil
181
- @file_stream_total = nil
182
- end
183
- # -------------------------------------
184
- ##
185
-
186
-
187
- def on_open(req)
188
- if post_check(req, @defer)
189
- @fileno = req[:result]
190
- cleanup(req)
191
- @closed = false
192
- @defer.notify(self)
193
- end
194
- end
195
-
196
- def on_close(req)
197
- if post_check(req, @defer)
198
- cleanup(req)
199
- @defer.resolve(nil)
200
- end
201
- end
202
-
203
- def on_read(req)
204
- if post_check(req, @read_deferred)
205
- data = @read_buffer.read_string(req[:result])
206
- cleanup(req)
207
- @read_deferred.resolve(data)
208
- end
209
- @read_buffer = nil
210
- @read_deferred = nil
211
- end
212
-
213
- def on_write(req)
214
- if post_check(req, @write_deferred)
215
- cleanup(req)
216
- @write_deferred.resolve(nil)
217
- end
218
- @write_buffer = nil
219
- @write_deferred = nil
220
- end
221
-
222
- def on_sync(req)
223
- if post_check(req, @sync_deferred)
224
- cleanup(req)
225
- @sync_deferred.resolve(nil)
226
- end
227
- @sync_deferred = nil
228
- end
229
-
230
- def on_datasync(req)
231
- if post_check(req, @datasync_deferred)
232
- cleanup(req)
233
- @datasync_deferred.resolve(nil)
234
- end
235
- @datasync_deferred = nil
236
- end
237
-
238
- def on_truncate(req)
239
- if post_check(req, @truncate_deferred)
240
- cleanup(req)
241
- @truncate_deferred.resolve(nil)
242
- end
243
- @truncate_deferred = nil
244
- end
245
-
246
- def on_utime(req)
247
- if post_check(req, @utime_deferred)
248
- cleanup(req)
249
- @utime_deferred.resolve(nil)
250
- end
251
- @utime_deferred = nil
252
- end
253
-
254
- def on_chmod(req)
255
- if post_check(req, @chmod_deferred)
256
- cleanup(req)
257
- @chmod_deferred.resolve(nil)
258
- end
259
- @chmod_deferred = nil
260
- end
261
-
262
- def on_chown(req)
263
- if post_check(req, @chown_deferred)
264
- cleanup(req)
265
- @chown_deferred.resolve(nil)
266
- end
267
- @chown_deferred = nil
268
- end
269
- end
1
+ module Libuv
2
+ class File < Q::DeferredPromise
3
+ include Assertions, Resource, Listener, FsChecks
4
+
5
+
6
+ EOF = "0\r\n\r\n".freeze
7
+ CRLF = "\r\n".freeze
8
+
9
+
10
+ attr_reader :fileno, :closed
11
+
12
+
13
+ def initialize(loop, path, flags = 0, mode = 0)
14
+ super(loop, loop.defer)
15
+
16
+ @fileno = -1
17
+ @closed = true
18
+ @path, @flags, @mode = path, flags, mode
19
+
20
+ request = ::Libuv::Ext.create_request(:uv_fs)
21
+ pre_check @defer, request, ::Libuv::Ext.fs_open(@loop, request, @path, @flags, @mode, callback(:on_open))
22
+ end
23
+
24
+ def close
25
+ @closed = true
26
+ request = ::Libuv::Ext.create_request(:uv_fs)
27
+ pre_check(@defer, request, ::Libuv::Ext.fs_close(@loop.handle, request, @fileno, callback(:on_close)))
28
+ end
29
+
30
+ def read(length, offset = 0)
31
+ assert_type(Integer, length, "length must be an Integer")
32
+ assert_type(Integer, offset, "offset must be an Integer")
33
+ @read_deferred = @loop.defer
34
+
35
+ @read_buffer = FFI::MemoryPointer.new(length)
36
+
37
+ request = ::Libuv::Ext.create_request(:uv_fs)
38
+ pre_check(@read_deferred, request, ::Libuv::Ext.fs_read(@loop.handle, request, @fileno, @read_buffer, length, offset, callback(:on_read)))
39
+ @read_deferred.promise
40
+ end
41
+
42
+ def write(data, offset = 0)
43
+ assert_type(String, data, "data must be a String")
44
+ assert_type(Integer, offset, "offset must be an Integer")
45
+ @write_deferred = @loop.defer
46
+
47
+ length = data.respond_to?(:bytesize) ? data.bytesize : data.size
48
+ @write_buffer = FFI::MemoryPointer.from_string(data)
49
+
50
+ request = ::Libuv::Ext.create_request(:uv_fs)
51
+ pre_check(@write_deferred, request, ::Libuv::Ext.fs_write(@loop.handle, request, @fileno, @write_buffer, length, offset, callback(:on_write)))
52
+ @write_deferred.promise
53
+ end
54
+
55
+ def sync
56
+ @sync_deferred = @loop.defer
57
+
58
+ request = ::Libuv::Ext.create_request(:uv_fs)
59
+ pre_check @sync_deferred, request, ::Libuv::Ext.fs_fsync(@loop.handle, request, @fileno, callback(:on_sync))
60
+ @sync_deferred.promise
61
+ end
62
+
63
+ def datasync
64
+ @datasync_deferred = @loop.defer
65
+
66
+ request = ::Libuv::Ext.create_request(:uv_fs)
67
+ pre_check @datasync_deferred, request, ::Libuv::Ext.fs_fdatasync(@loop.handle, request, @fileno, callback(:on_datasync))
68
+ @datasync_deferred.promise
69
+ end
70
+
71
+ def truncate(offset)
72
+ assert_type(Integer, offset, "offset must be an Integer")
73
+ @truncate_deferred = @loop.defer
74
+
75
+ request = ::Libuv::Ext.create_request(:uv_fs)
76
+ pre_check @truncate_deferred, request, ::Libuv::Ext.fs_ftruncate(@loop.handle, request, @fileno, offset, callback(:on_truncate))
77
+ @truncate_deferred.promise
78
+ end
79
+
80
+ def utime(atime, mtime)
81
+ assert_type(Integer, atime, "atime must be an Integer")
82
+ assert_type(Integer, mtime, "mtime must be an Integer")
83
+ @utime_deferred = @loop.defer
84
+
85
+ request = ::Libuv::Ext.create_request(:uv_fs)
86
+ pre_check @utime_deferred, request, ::Libuv::Ext.fs_futime(@loop.handle, request, @fileno, atime, mtime, callback(:on_utime))
87
+ @utime_deferred.promise
88
+ end
89
+
90
+ def chmod(mode)
91
+ assert_type(Integer, mode, "mode must be an Integer")
92
+ @chmod_deferred = @loop.defer
93
+
94
+ request = ::Libuv::Ext.create_request(:uv_fs)
95
+ pre_check @chmod_deferred, request, ::Libuv::Ext.fs_fchmod(@loop.handle, request, @fileno, mode, callback(:on_chmod))
96
+ @chmod_deferred.promise
97
+ end
98
+
99
+ def chown(uid, gid)
100
+ assert_type(Integer, uid, "uid must be an Integer")
101
+ assert_type(Integer, gid, "gid must be an Integer")
102
+ @chown_deferred = @loop.defer
103
+
104
+ request = ::Libuv::Ext.create_request(:uv_fs)
105
+ pre_check @chown_deferred, request, ::Libuv::Ext.fs_fchown(@loop.handle, request, @fileno, uid, gid, callback(:on_chown))
106
+ @chown_deferred.promise
107
+ end
108
+
109
+ def send_file(stream, type = :raw, chunk_size = 4096)
110
+ @transmit_failure ||= method(:transmit_failure)
111
+ @transmit_data ||= method(:transmit_data)
112
+ @start_transmit ||= method(:start_transmit)
113
+ @next_chunk ||= method(:next_chunk)
114
+
115
+ @sending_file = @loop.defer
116
+ @file_stream = stream
117
+ @file_stream_type = type
118
+ @file_chunk_size = chunk_size
119
+ @file_chunk_count = 0
120
+
121
+ stat.then @start_transmit, @transmit_failure
122
+
123
+ @sending_file.promise.finally &method(:clean_up_send)
124
+ end
125
+
126
+
127
+ private
128
+
129
+
130
+ ##
131
+ # File transmit functions -------------
132
+ def start_transmit(stats)
133
+ @file_stream_total = stats[:st_size]
134
+ next_chunk
135
+ end
136
+
137
+ def transmit_data(data)
138
+ @file_chunk_count += 1
139
+ if @file_stream_type == :http
140
+ resp = ''
141
+ resp << data.bytesize.to_s(16) << CRLF
142
+ resp << data
143
+ resp << CRLF
144
+ data = resp
145
+ end
146
+ @file_stream.write(data).then @next_chunk, @transmit_failure
147
+ nil
148
+ end
149
+
150
+ def next_chunk(*args)
151
+ next_size = @file_chunk_size
152
+ next_offset = @file_chunk_size * @file_chunk_count
153
+
154
+ if next_offset >= @file_stream_total
155
+ if @file_stream_type == :http
156
+ @file_stream.write(EOF.dup).then(proc {
157
+ @sending_file.resolve(@file_stream_total)
158
+ }, @transmit_failure)
159
+ else
160
+ @sending_file.resolve(@file_stream_total)
161
+ end
162
+ else
163
+ if next_offset + next_size > @file_stream_total
164
+ next_size = @file_stream_total - next_offset
165
+ end
166
+ read(next_size, next_offset).then(@transmit_data, @transmit_failure)
167
+ end
168
+ nil
169
+ end
170
+
171
+ def transmit_failure(reason)
172
+ @sending_file.reject(reason)
173
+ end
174
+
175
+ def clean_up_send
176
+ @sending_file = nil
177
+ @file_stream = nil
178
+ @file_stream_type = nil
179
+ @file_chunk_size = nil
180
+ @file_chunk_count = nil
181
+ @file_stream_total = nil
182
+ end
183
+ # -------------------------------------
184
+ ##
185
+
186
+
187
+ def on_open(req)
188
+ if post_check(req, @defer)
189
+ @fileno = req[:result]
190
+ cleanup(req)
191
+ @closed = false
192
+ @defer.notify(self)
193
+ end
194
+ end
195
+
196
+ def on_close(req)
197
+ if post_check(req, @defer)
198
+ cleanup(req)
199
+ @defer.resolve(nil)
200
+ end
201
+ end
202
+
203
+ def on_read(req)
204
+ if post_check(req, @read_deferred)
205
+ data = @read_buffer.read_string(req[:result])
206
+ cleanup(req)
207
+ @read_deferred.resolve(data)
208
+ end
209
+ @read_buffer = nil
210
+ @read_deferred = nil
211
+ end
212
+
213
+ def on_write(req)
214
+ if post_check(req, @write_deferred)
215
+ cleanup(req)
216
+ @write_deferred.resolve(nil)
217
+ end
218
+ @write_buffer = nil
219
+ @write_deferred = nil
220
+ end
221
+
222
+ def on_sync(req)
223
+ if post_check(req, @sync_deferred)
224
+ cleanup(req)
225
+ @sync_deferred.resolve(nil)
226
+ end
227
+ @sync_deferred = nil
228
+ end
229
+
230
+ def on_datasync(req)
231
+ if post_check(req, @datasync_deferred)
232
+ cleanup(req)
233
+ @datasync_deferred.resolve(nil)
234
+ end
235
+ @datasync_deferred = nil
236
+ end
237
+
238
+ def on_truncate(req)
239
+ if post_check(req, @truncate_deferred)
240
+ cleanup(req)
241
+ @truncate_deferred.resolve(nil)
242
+ end
243
+ @truncate_deferred = nil
244
+ end
245
+
246
+ def on_utime(req)
247
+ if post_check(req, @utime_deferred)
248
+ cleanup(req)
249
+ @utime_deferred.resolve(nil)
250
+ end
251
+ @utime_deferred = nil
252
+ end
253
+
254
+ def on_chmod(req)
255
+ if post_check(req, @chmod_deferred)
256
+ cleanup(req)
257
+ @chmod_deferred.resolve(nil)
258
+ end
259
+ @chmod_deferred = nil
260
+ end
261
+
262
+ def on_chown(req)
263
+ if post_check(req, @chown_deferred)
264
+ cleanup(req)
265
+ @chown_deferred.resolve(nil)
266
+ end
267
+ @chown_deferred = nil
268
+ end
269
+ end
270
270
  end