Sha256: ea47db69832784dcbbdf3ba995f72ed7fc5c4700919f5dd1867da7a77e1fdc12

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

# -*- encoding: binary -*-
# :enddoc:
module Rainbows::Revactor::Body
  # TODO non-blocking splice(2) under Linux
  ALIASES = {
    :write_body_stream => :write_body_each
  }

  if IO.method_defined?(:sendfile_nonblock)
    def write_body_file(client, body, range)
      body = body_to_io(body)
      sock = client.instance_variable_get(:@_io)
      pfx = Revactor::TCP::Socket === client ? :tcp : :unix
      write_complete = T[:"#{pfx}_write_complete", client]
      closed = T[:"#{pfx}_closed", client]
      offset, count = range ? range : [ 0, body.stat.size ]
      begin
        offset += (n = sock.sendfile_nonblock(body, offset, count))
      rescue Errno::EAGAIN
        # The @_write_buffer is empty at this point, trigger the
        # on_readable method which in turn triggers on_write_complete
        # even though nothing was written
        client.controller = Actor.current
        client.__send__(:enable_write_watcher)
        Actor.receive do |filter|
          filter.when(write_complete) {}
          filter.when(closed) { raise Errno::EPIPE }
        end
        retry
      rescue EOFError
        break
      end while (count -= n) > 0
      ensure
        close_if_private(body)
    end
  else
    ALIASES[:write_body] = :write_body_each
  end

  def self.included(klass)
    ALIASES.each do |new_method, orig_method|
      klass.__send__(:alias_method, new_method, orig_method)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rainbows-2.1.0 lib/rainbows/revactor/body.rb