about summary refs log tree commit homepage
path: root/lib/dtas/nonblock.rb
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2015-12-13 12:19:00 +0000
committerEric Wong <e@80x24.org>2015-12-13 12:29:43 +0000
commitd3cf61b05d9507e7b6ea5a1a1192e107a8612049 (patch)
tree7e150cbc3177d58941aa3f26017627ac006b13aa /lib/dtas/nonblock.rb
parente751a396e43f07f15d9b639a850c0c8cbebf9539 (diff)
downloaddtas-d3cf61b05d9507e7b6ea5a1a1192e107a8612049.tar.gz
Ruby 2.3 will have `exception: false' support in socket-related
classes.  Additionally, 2.3 will implement the existing
IO#*_nonblock methods more efficiently than before by avoiding
the hash allocation necessary for keywords.

For users on older Rubies, we'll continue supporting them with
compatibility wrappers; even Ruby 1.9.3 users (for now).
Diffstat (limited to 'lib/dtas/nonblock.rb')
-rw-r--r--lib/dtas/nonblock.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/dtas/nonblock.rb b/lib/dtas/nonblock.rb
new file mode 100644
index 0000000..c8beecd
--- /dev/null
+++ b/lib/dtas/nonblock.rb
@@ -0,0 +1,24 @@
+# Copyright (C) 2015 all contributors <dtas-all@nongnu.org>
+# License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
+
+class DTAS::Nonblock < IO
+  if RUBY_VERSION.to_f <= 2.0
+    EX = {}.freeze
+    def read_nonblock(len, buf = nil, opts = EX)
+      super(len, buf)
+    rescue IO::WaitReadable
+      raise if opts[:exception]
+      :wait_readable
+    rescue EOFError
+      raise if opts[:exception]
+      nil
+    end
+
+    def write_nonblock(buf, opts = EX)
+      super(buf)
+    rescue IO::WaitWritable
+      raise if opts[:exception]
+      :wait_writable
+    end
+  end
+end