Sha256: 889b7cbb0c267f0b30ccb13df3e50b2e726fa41b0c08c281ff98ac364064db4a

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# -*- 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, tmpdir)
    @tmpio = Yahns::TmpIO.new(tmpdir)
    @sf_offset = @sf_count = 0
    @wbuf_persist = persist # whether or not we keep the connection alive
    @body = body
    @bypass = false
  end

  def wbuf_write(client, buf)
    # try to bypass the VFS layer if we're all caught up
    case rv = client.kgio_trywrite(buf)
    when String
      buf = rv # retry in loop
    when nil
      return # yay! hopefully we don't have to buffer again
    when :wait_writable, :wait_readable
      @bypass = false # ugh, continue to buffering to file
    end while @bypass

    @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

    # we're all caught up, try to prevent dirty data from getting flushed
    # to disk if we can help it.
    @tmpio.truncate(@sf_offset = 0)
    @tmpio.rewind
    @bypass = true
    nil
  end

  # called by last wbuf_flush
  def wbuf_close(client)
    @tmpio = @tmpio.close
    wbuf_close_common(client)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yahns-0.0.2 lib/yahns/wbuf.rb