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 # It's not recommended to use any of this in your applications
8 # unless you're willing to accept breakage. Most of this is very
9 # difficult-to-use, fragile and we don't have much time to devote to
10 # supporting these in the future.
12 # This is a stable, legacy interface and should be preserved for all
13 # future versions of Rainbows! However, new apps should use
14 # Rainbows::Fiber::IO::Socket or Rainbows::Fiber::IO::Pipe instead
15 # (or better yet, avoid any of the Rainbows::Fiber* stuff).
16 class Rainbows::Fiber::IO
20 # see Rainbows::Fiber::IO::Compat for initialize implementation
26 # no longer used internally within Rainbows!, only for compatibility
27 def write_nonblock(buf)
28 @to_io.write_nonblock(buf)
35 # for wrapping output response bodies
37 buf = readpartial(16384)
39 yield buf while readpartial(16384, buf)
53 case rv = Kgio.trywrite(@to_io, buf)
61 # used for reading headers (respecting keepalive_timeout)
64 case rv = Kgio.tryread(@to_io, 16384, buf)
66 return if expire && expire < Rainbows.now
67 expire ||= read_expire
74 def readpartial(length, buf = "")
75 case rv = Kgio.tryread(@to_io, length, buf)
77 raise EOFError, "end of file reached", []
86 @to_io.kgio_read(*args)
90 @to_io.kgio_read!(*args)
93 def kgio_trywrite(*args)
94 @to_io.kgio_trywrite(*args)
97 autoload :Socket, 'rainbows/fiber/io/socket'
98 autoload :Pipe, 'rainbows/fiber/io/pipe'
102 require 'rainbows/fiber/io/methods'
103 require 'rainbows/fiber/io/compat'
104 class Rainbows::Fiber::IO
105 include Rainbows::Fiber::IO::Compat
106 include Rainbows::Fiber::IO::Methods
107 alias_method :wait_readable, :kgio_wait_readable
108 alias_method :wait_writable, :kgio_wait_writable