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 case rv = Kgio.trywrite(buf)
56 # used for reading headers (respecting keepalive_timeout)
59 case rv = Kgio.tryread(@to_io, 16384, buf)
61 return if expire && expire < Time.now
62 expire ||= read_expire
69 def readpartial(length, buf = "")
70 case rv = Kgio.tryread(@to_io, length, buf)
72 raise EOFError, "end of file reached", []
81 @to_io.kgio_read(*args)
85 @to_io.kgio_read!(*args)
88 def kgio_trywrite(*args)
89 @to_io.kgio_trywrite(*args)
92 autoload :Socket, 'rainbows/fiber/io/socket'
93 autoload :Pipe, 'rainbows/fiber/io/pipe'
97 require 'rainbows/fiber/io/methods'
98 require 'rainbows/fiber/io/compat'
99 Rainbows::Client.__send__(:include, Rainbows::Fiber::IO::Methods)
100 class Rainbows::Fiber::IO
101 include Rainbows::Fiber::IO::Compat
102 include Rainbows::Fiber::IO::Methods
103 alias_method :wait_readable, :kgio_wait_readable
104 alias_method :wait_writable, :kgio_wait_writable