about summary refs log tree commit homepage
path: root/lib/rainbows/epoll/response_pipe.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rainbows/epoll/response_pipe.rb')
-rw-r--r--lib/rainbows/epoll/response_pipe.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/rainbows/epoll/response_pipe.rb b/lib/rainbows/epoll/response_pipe.rb
new file mode 100644
index 0000000..ce240f5
--- /dev/null
+++ b/lib/rainbows/epoll/response_pipe.rb
@@ -0,0 +1,38 @@
+# -*- encoding: binary -*-
+# :enddoc:
+#
+class Rainbows::Epoll::ResponsePipe
+  include Rainbows::Epoll::State
+  attr_reader :io
+  alias to_io io
+  IN = SleepyPenguin::Epoll::IN | SleepyPenguin::Epoll::ET
+  RBUF = Rainbows::EvCore::RBUF
+
+  def initialize(io, client, body)
+    @io, @client, @body = io, client, body
+    @epoll_active = false
+  end
+
+  def epoll_run
+    return close if @client.closed?
+    @client.stream_pipe(self) or @client.on_deferred_write_complete
+    rescue => e
+      close
+      @client.handle_error(e)
+  end
+
+  def close
+    epoll_disable
+    @body.respond_to?(:close) and @body.close
+    @io = @body = nil
+  end
+
+  def tryread
+    io = @io
+    io.respond_to?(:kgio_tryread) and return io.kgio_tryread(16384, RBUF)
+    io.read_nonblock(16384, RBUF)
+    rescue Errno::EAGAIN
+      :wait_readable
+    rescue EOFError
+  end
+end