about summary refs log tree commit homepage
path: root/lib/yahns/wbuf.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/yahns/wbuf.rb')
-rw-r--r--lib/yahns/wbuf.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/yahns/wbuf.rb b/lib/yahns/wbuf.rb
new file mode 100644
index 0000000..4828056
--- /dev/null
+++ b/lib/yahns/wbuf.rb
@@ -0,0 +1,36 @@
+# -*- encoding: binary -*-
+# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> et. al.
+# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+require_relative 'wbuf_common'
+
+class Yahns::Wbuf # :nodoc:
+  include Yahns::WbufCommon
+
+  def initialize(body, persist)
+    @tmpio = Yahns::TmpIO.new
+    @sf_offset = @sf_count = 0
+    @wbuf_persist = persist # whether or not we keep the connection alive
+    @body = body
+  end
+
+  def wbuf_write(client, buf)
+    @sf_count += @tmpio.write(buf)
+    case rv = client.trysendfile(@tmpio, @sf_offset, @sf_count)
+    when Integer
+      @sf_count -= rv
+      @sf_offset += rv
+    when :wait_writable, :wait_readable
+      return rv
+    else
+      raise "BUG: #{rv.nil ? "EOF" : rv.inspect} on tmpio " \
+            "sf_offset=#@sf_offset sf_count=#@sf_count"
+    end while @sf_count > 0
+    nil
+  end
+
+  # called by last wbuf_flush
+  def wbuf_close(client)
+    @tmpio = @tmpio.close
+    wbuf_close_common(client)
+  end
+end