lib/ionian/extension/io.rb in ionian-0.6.6 vs lib/ionian/extension/io.rb in ionian-0.6.7
- old
+ new
@@ -1,5 +1,7 @@
+Thread.abort_on_exception = true
+
module Ionian
module Extension
# A mixin for IO objects that allows regular expression matching
# and convenient notification of received data.
#
@@ -29,11 +31,11 @@
# Returns true if there is data in the receive buffer.
# Args:
# Timeout: Number of seconds to wait for data until
# giving up. Set to nil for blocking.
- def has_data?(timeout: 0)
+ def has_data? timeout: 0
::IO.select([self], nil, nil, timeout) ? true : false
end
# Returns the regular expression used for #read_match.
def expression
@@ -51,11 +53,14 @@
@ionian_expression = Regexp.new "(.*?)#{expression}" if exp.is_a? String
end
# Read all data in the buffer.
# An alternative to using #readpartial with a large length.
- # Blocks until data is available.
- def read_all
+ # Blocks until data is available unless nonblocking: true.
+ # If nonblocking, returns nil if no data available.
+ def read_all nonblocking: false
+ return nil if nonblocking and not has_data?
+
# Block until data has arrived.
data = readpartial 0xFFFF
# If there is more data in the buffer, retrieve it nonblocking.
data += readpartial 0xFFFF while has_data?
data
\ No newline at end of file