Class: XS::Poller
- Inherits:
-
Object
- Object
- XS::Poller
- Defined in:
- lib/ffi-rxs/poll.rb
Instance Attribute Summary collapse
-
#readables ⇒ Object
readonly
Returns the value of attribute readables.
-
#writables ⇒ Object
readonly
Returns the value of attribute writables.
Instance Method Summary collapse
-
#delete(sock) ⇒ Object
Deletes the sock for all subscribed events.
-
#deregister(sock, events, fd = 0) ⇒ Object
Deregister the sock for events.
-
#deregister_readable(sock) ⇒ Object
A helper method to deregister a sock for readable events.
-
#deregister_writable(sock) ⇒ Object
A helper method to deregister a sock for writable events.
-
#initialize ⇒ Poller
constructor
A new instance of Poller.
-
#inspect ⇒ Object
Convenience method to inspect items array.
-
#poll(timeout = :blocking) ⇒ Object
When return code -1 use XS::Util.errno to get the related error number.
-
#poll_nonblock ⇒ Object
The non-blocking version of #poll.
-
#register(sock, events = XS::POLLIN | XS::POLLOUT, fd = 0) ⇒ Object
Register the sock for events.
-
#register_readable(sock) ⇒ Object
A helper method to register a sock as readable events only.
-
#register_writable(sock) ⇒ Object
A helper method to register a sock for writable events only.
-
#size ⇒ Object
Convenience method to return size of items array.
-
#to_s ⇒ Object
Convenience method to inspect poller.
Constructor Details
Instance Attribute Details
#readables ⇒ Object (readonly)
Returns the value of attribute readables.
7 8 9 |
# File 'lib/ffi-rxs/poll.rb', line 7 def readables @readables end |
#writables ⇒ Object (readonly)
Returns the value of attribute writables.
7 8 9 |
# File 'lib/ffi-rxs/poll.rb', line 7 def writables @writables end |
Instance Method Details
#delete(sock) ⇒ Object
Deletes the sock for all subscribed events. Called internally when a socket has been deregistered and has no more events registered anywhere.
Can also be called directly to remove the socket from the polling array.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/ffi-rxs/poll.rb', line 174 def delete sock unless (size = @sockets.size).zero? @sockets.delete_if { |socket| socket.socket.address == sock.socket.address } socket_deleted = size != @sockets.size item_deleted = @items.delete sock raw_deleted = @raw_to_socket.delete(sock.socket.address) socket_deleted && item_deleted && raw_deleted else false end end |
#deregister(sock, events, fd = 0) ⇒ Object
Deregister the sock for events. When there are no events left, this also deletes the socket from the poll items.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/ffi-rxs/poll.rb', line 107 def deregister sock, events, fd = 0 return unless sock || !fd.zero? item = @items.get(@sockets.index(sock)) if item && (item[:events] & events) > 0 # change the value in place item[:events] ^= events delete sock if item[:events].zero? true else false end end |
#deregister_readable(sock) ⇒ Object
A helper method to deregister a sock for readable events.
149 150 151 |
# File 'lib/ffi-rxs/poll.rb', line 149 def deregister_readable sock deregister sock, XS::POLLIN, 0 end |
#deregister_writable(sock) ⇒ Object
A helper method to deregister a sock for writable events.
159 160 161 |
# File 'lib/ffi-rxs/poll.rb', line 159 def deregister_writable sock deregister sock, XS::POLLOUT, 0 end |
#inspect ⇒ Object
Convenience method to inspect items array
194 195 196 |
# File 'lib/ffi-rxs/poll.rb', line 194 def inspect @items.inspect end |
#poll(timeout = :blocking) ⇒ Object
When return code -1 use XS::Util.errno to get the related error number.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ffi-rxs/poll.rb', line 39 def poll timeout = :blocking unless @items.empty? timeout = adjust timeout items_triggered = LibXS.xs_poll @items.address, @items.size, timeout if Util.resultcode_ok?(items_triggered) update_selectables end items_triggered else 0 end end |
#poll_nonblock ⇒ Object
The non-blocking version of #poll. See the #poll description for potential exceptions.
When return code -1 check XS::Util.errno to determine the underlying cause.
60 61 62 |
# File 'lib/ffi-rxs/poll.rb', line 60 def poll_nonblock poll 0 end |
#register(sock, events = XS::POLLIN | XS::POLLOUT, fd = 0) ⇒ Object
Register the sock for events. This method is idempotent meaning it can be called multiple times with the same data and the socket will only get registered at most once. Calling multiple times with different values for events will OR the event information together.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ffi-rxs/poll.rb', line 75 def register sock, events = XS::POLLIN | XS::POLLOUT, fd = 0 return false if (sock.nil? && fd.zero?) || events.zero? item = @items.get(@sockets.index(sock)) unless item @sockets << sock item = LibXS::PollItem.new if sock.kind_of?(XS::Socket) || sock.kind_of?(Socket) item[:socket] = sock.socket item[:fd] = 0 else item[:socket] = FFI::MemoryPointer.new(0) item[:fd] = fd end @raw_to_socket[item.socket.address] = sock @items << item end item[:events] |= events end |
#register_readable(sock) ⇒ Object
A helper method to register a sock as readable events only.
129 130 131 |
# File 'lib/ffi-rxs/poll.rb', line 129 def register_readable sock register sock, XS::POLLIN, 0 end |
#register_writable(sock) ⇒ Object
A helper method to register a sock for writable events only.
139 140 141 |
# File 'lib/ffi-rxs/poll.rb', line 139 def register_writable sock register sock, XS::POLLOUT, 0 end |
#size ⇒ Object
Convenience method to return size of items array
191 |
# File 'lib/ffi-rxs/poll.rb', line 191 def size(); @items.size; end |
#to_s ⇒ Object
Convenience method to inspect poller
199 |
# File 'lib/ffi-rxs/poll.rb', line 199 def to_s(); inspect; end |