rb-kqueue 0.0.2 → 0.0.3

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -13,7 +13,7 @@ require 'rb-kqueue/queue'
13
13
  # * {Watcher} -- A watcher for a single sort of event
14
14
  # * {Event} -- A notification that an event has occurred
15
15
  module KQueue
16
- VERSION = [0, 0, 2]
16
+ VERSION = [0, 0, 3]
17
17
 
18
18
  # Raise an exception for a native kqueue error.
19
19
  #
@@ -33,5 +33,8 @@ module KQueue
33
33
 
34
34
  attach_function :kqueue, [], :int
35
35
  attach_function :kevent, [:int, :pointer, :int, :pointer, :int, :pointer], :int
36
+
37
+ attach_function :open, [:string, :int], :int
38
+ attach_function :close, [:int], :int
36
39
  end
37
40
  end
@@ -66,6 +66,10 @@ module KQueue
66
66
  # The {Event#data} field is set to the number of bytes available.
67
67
  # When the last writer disconnects, {Event#eof?} will be set.
68
68
  #
69
+ # Note that this isn't compatible with JRuby
70
+ # unless a native-code file descriptor is passed in.
71
+ # This means the file descriptor must be returned by an FFI-wrapped C function.
72
+ #
69
73
  # @param fd [IO, Fixnum] A Ruby IO stream, or the file descriptor
70
74
  # for a native IO stream.
71
75
  # @yield [event] A block that will be run when the specified stream
@@ -75,7 +79,6 @@ module KQueue
75
79
  # @return [Watcher] The Watcher for this event.
76
80
  # @raise [SystemCallError] If something goes wrong when registering the Watcher.
77
81
  def watch_stream_for_read(fd, &callback)
78
- fd = fd.fileno if fd.respond_to?(:fileno)
79
82
  Watcher::ReadWrite.new(self, fd, :read, callback)
80
83
  end
81
84
 
@@ -97,6 +100,10 @@ module KQueue
97
100
  # It's possible for {Event#eof?} to be set while there's still
98
101
  # data pending in the socket buffer.
99
102
  #
103
+ # Note that this isn't compatible with JRuby
104
+ # unless a native-code file descriptor is passed in.
105
+ # This means the file descriptor must be returned by an FFI-wrapped C function.
106
+ #
100
107
  # @param fd [Socket, Fixnum] A Ruby Socket, or the file descriptor
101
108
  # for a native Socket.
102
109
  # @param low_water [Fixnum] The low-water mark for new data.
@@ -107,7 +114,6 @@ module KQueue
107
114
  # @return [Watcher] The Watcher for this event.
108
115
  # @raise [SystemCallError] If something goes wrong when registering the Watcher.
109
116
  def watch_socket_for_read(fd, low_water = nil, &callback)
110
- fd = fd.fileno if fd.respond_to?(:fileno)
111
117
  Watcher::SocketReadWrite.new(self, fd, :read, low_water, callback)
112
118
  end
113
119
 
@@ -119,6 +125,10 @@ module KQueue
119
125
  # remaining in the write buffer.
120
126
  # When the reader disconnects, {Event#eof?} will be set.
121
127
  #
128
+ # Note that this isn't compatible with JRuby
129
+ # unless a native-code file descriptor is passed in.
130
+ # This means the file descriptor must be returned by an FFI-wrapped C function.
131
+ #
122
132
  # @param fd [IO, Fixnum] A Ruby IO stream, or the file descriptor
123
133
  # for a native IO stream.
124
134
  # @yield [event] A block that will be run when the specified stream
@@ -128,7 +138,6 @@ module KQueue
128
138
  # @return [Watcher] The Watcher for this event.
129
139
  # @raise [SystemCallError] If something goes wrong when registering the Watcher.
130
140
  def watch_stream_for_write(fd, &callback)
131
- fd = fd.fileno if fd.respond_to?(:fileno)
132
141
  Watcher::ReadWrite.new(self, fd, :write, callback)
133
142
  end
134
143
 
@@ -146,6 +155,10 @@ module KQueue
146
155
  # It's possible for {Event#eof?} to be set while there's still
147
156
  # data pending in the socket buffer.
148
157
  #
158
+ # Note that this isn't compatible with JRuby
159
+ # unless a native-code file descriptor is passed in.
160
+ # This means the file descriptor must be returned by an FFI-wrapped C function.
161
+ #
149
162
  # @param fd [Socket, Fixnum] A Ruby Socket, or the file descriptor
150
163
  # for a native Socket.
151
164
  # @param low_water [Fixnum] The low-water mark for new data.
@@ -156,7 +169,6 @@ module KQueue
156
169
  # @return [Watcher] The Watcher for this event.
157
170
  # @raise [SystemCallError] If something goes wrong when registering the Watcher.
158
171
  def watch_socket_for_write(fd, low_water = nil, &callback)
159
- fd = fd.fileno if fd.respond_to?(:fileno)
160
172
  Watcher::SocketReadWrite.new(self, fd, :write, low_water, callback)
161
173
  end
162
174
 
@@ -13,7 +13,42 @@ module KQueue
13
13
  # @private
14
14
  def initialize(queue, path, flags, callback)
15
15
  @path = path
16
- @file = File.open(path) # TODO: not JRuby-compatible
16
+ @fd = Native.open(path, 0) # 0 means "read only"
17
+
18
+ if @fd < 0
19
+ raise SystemCallError.new(
20
+ "Failed to open file #{path}" +
21
+ case FFI.errno
22
+ when Errno::EACCES::Errno; ": Permission denied."
23
+ when Errno::EAGAIN::Errno; ": Slave side of a locked pseudo-terminal device."
24
+ when Errno::EFAULT::Errno; ": Outside the process's allocated address space."
25
+ when Errno::EINTR::Errno; ": Interrupted."
26
+ when Errno::ELOOP::Errno; ": Too many symbolic links (possible loop)."
27
+ when Errno::EMFILE::Errno; ": Too many open files."
28
+ when Errno::ENAMETOOLONG::Errno; ": Name too long."
29
+ when Errno::ENFILE::Errno; ": System file table is full."
30
+ when Errno::ENOENT::Errno; ": File doesn't exist."
31
+ when Errno::ENOTDIR::Errno; ": A component of the path prefix is not a directory."
32
+ when Errno::ENXIO::Errno; ": The device associated with this file doesn't exist."
33
+ when Errno::EOPNOTSUPP::Errno; ": File type not supported."
34
+ when Errno::EOVERFLOW::Errno; ": File too big."
35
+ else; ""
36
+ end,
37
+ FFI.errno)
38
+ end
39
+
40
+ ObjectSpace.define_finalizer(self, lambda do
41
+ next unless Native.close(@fd) < 0
42
+ raise SystemCallError.new(
43
+ "Failed to close file #{path}" +
44
+ case FFI.errno
45
+ when Errno::EBADF::Errno; ": Invalid file descriptor."
46
+ when Errno::EINTR::Errno; ": Closing interrupted."
47
+ when Errno::EIO::Errno; ": IO error."
48
+ else; ""
49
+ end,
50
+ FFI.errno)
51
+ end)
17
52
  super(queue, @file.fileno, :vnode, flags, nil, callback)
18
53
  end
19
54
  end
@@ -9,6 +9,13 @@ module KQueue
9
9
  # Note that read and write events for sockets
10
10
  # use the {SocketReadWrite} class.
11
11
  class ReadWrite < Watcher
12
+ # The Ruby IO object from which the file descriptor was extracted.
13
+ # This is only set if an IO object was used to construct this watcher.
14
+ # Otherwise, it's `nil`.
15
+ #
16
+ # @return [IO, nil]
17
+ attr_reader :io
18
+
12
19
  # The file descriptor for the stream being watched.
13
20
  #
14
21
  # @return [Fixnum]
@@ -23,6 +30,11 @@ module KQueue
23
30
  #
24
31
  # @private
25
32
  def initialize(queue, fd, type, callback)
33
+ if fd.is_a?(IO)
34
+ @io = fd
35
+ fd = fd.fileno
36
+ end
37
+
26
38
  @fd = fd
27
39
  @type = type
28
40
  super(queue, @fd, type, [], nil, callback)
@@ -20,6 +20,11 @@ module KQueue
20
20
  #
21
21
  # @private
22
22
  def initialize(queue, fd, type, low_water, callback)
23
+ if fd.is_a?(IO)
24
+ @io = fd
25
+ fd = fd.fileno
26
+ end
27
+
23
28
  @fd = fd
24
29
  @type = type
25
30
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rb-kqueue}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nathan Weizenbaum"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb-kqueue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Weizenbaum