1 # -*- encoding: binary -*-
3 # A Fiber-aware IO class, gives users the illusion of a synchronous
4 # interface that yields away from the current Fiber whenever
5 # the underlying descriptor is blocked on reads or write
7 # This is a stable, legacy interface and should be preserved for all
8 # future versions of Rainbows! However, new apps should use
9 # Rainbows::Fiber::IO::Socket or Rainbows::Fiber::IO::Pipe instead.
11 class Rainbows::Fiber::IO
15 # see Rainbows::Fiber::IO::Compat for initialize implementation
21 # no longer used internally within Rainbows!, only for compatibility
22 def write_nonblock(buf)
23 @to_io.write_nonblock(buf)
30 # for wrapping output response bodies
32 buf = readpartial(16384)
34 yield buf while readpartial(16384, buf)
48 if @to_io.respond_to?(:kgio_trywrite)
50 case rv = @to_io.kgio_trywrite(buf)
61 (rv = @to_io.write_nonblock(buf)) == buf.bytesize and return
62 buf = byte_slice(buf, rv)
69 def byte_slice(buf, start) # :nodoc:
70 buf.encoding == Encoding::BINARY or
71 buf = buf.dup.force_encoding(Encoding::BINARY)
72 buf.slice(start, buf.size)
75 # used for reading headers (respecting keepalive_timeout)
78 if @to_io.respond_to?(:kgio_tryread)
80 case rv = @to_io.kgio_tryread(16384, buf)
82 return if expire && expire < Time.now
83 expire ||= read_expire
91 return @to_io.read_nonblock(16384, buf)
93 return if expire && expire < Time.now
94 expire ||= read_expire
100 def readpartial(length, buf = "")
101 if @to_io.respond_to?(:kgio_tryread)
103 rv = @to_io.kgio_tryread(length, buf)
106 raise EOFError, "end of file reached", []
115 return @to_io.read_nonblock(length, buf)
123 @to_io.kgio_read(*args)
126 def kgio_read!(*args)
127 @to_io.kgio_read!(*args)
130 def kgio_trywrite(*args)
131 @to_io.kgio_trywrite(*args)
134 autoload :Socket, 'rainbows/fiber/io/socket'
135 autoload :Pipe, 'rainbows/fiber/io/pipe'
139 require 'rainbows/fiber/io/methods'
140 require 'rainbows/fiber/io/compat'
141 Rainbows::Client.__send__(:include, Rainbows::Fiber::IO::Methods)
142 class Rainbows::Fiber::IO
143 include Rainbows::Fiber::IO::Compat
144 include Rainbows::Fiber::IO::Methods
145 alias_method :wait_readable, :kgio_wait_readable
146 alias_method :wait_writable, :kgio_wait_writable