Class: IO
- Inherits:
-
Object
- Object
- IO
- Extended by:
- FFI::Library
- Defined in:
- lib/io/poll.rb,
lib/io/poll.rb
Defined Under Namespace
Modules: Poll Classes: PollFdStruct
Class Method Summary collapse
Class Method Details
.select_using_poll(read, write, error, timeout) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/io/poll.rb', line 90 def IO.select_using_poll(read, write, error, timeout) read = [] if read.nil? write = [] if write.nil? error = [] if error.nil? all = read.map { |f| { :io => f, :events => IO::Poll::POLLIN } } + write.map { |f| { :io => f, :events => IO::Poll::POLLOUT } } + error.map { |f| { :io => f, :events => IO::Poll::POLLERR } } pollfds = FFI::MemoryPointer.new(IO::PollFdStruct, all.length) all.each_with_index do |poll, i| struct = IO::PollFdStruct.new(pollfds[i]) struct[:fd] = poll[:io].fileno struct[:events] = poll[:events] struct[:revents] = 0 end ret = IO.poll(pollfds, all.length, timeout) if ret < 0 # error. IO.select() returns nil in this case return nil elsif 0 == ret # timed out, exit fast return [[],[],[]] else # returned some interesting descriptors ret_read, ret_write, ret_error = [], [], [] all.each_with_index do |poll, i| # select() will signal unrequested flags (eg POLLNVAL) which must be passed # back to the correct read/write/error fdset. So, test what we were requesting, # and return the fd if *any* notification occured, even if it wasn't what we asked for struct = IO::PollFdStruct.new(pollfds[i]) next if 0 == struct[:revents] ret_read << poll[:io] if poll[:events] == IO::Poll::POLLIN and not struct[:revents].zero? ret_write << poll[:io] if poll[:events] == IO::Poll::POLLOUT and not struct[:revents].zero? ret_error << poll[:io] if poll[:events] == IO::Poll::POLLERR and not struct[:revents].zero? end return [ret_read, ret_write, ret_error] end # if/else ret end |