upgrade to Kgio 2.x and Unicorn 3.x
[rainbows.git] / lib / rainbows / fiber / io / methods.rb
blob6c4d44d8b5c0a6b6f368b1d3bed518b65a772b93
1 # -*- encoding: binary -*-
3 # :enddoc:
5 # this is used to augment Kgio::Socket and Kgio::Pipe-enhanced classes
6 # for use with Rainbows!  Do no use this directly, see
7 # Rainbows::Fiber::IO::Pipe and Rainbows::Fiber::IO::Socket instead.
8 module Rainbows::Fiber::IO::Methods
9   RD = Rainbows::Fiber::RD
10   WR = Rainbows::Fiber::WR
11   attr_accessor :f
13   # for wrapping output response bodies
14   def each(&block)
15     if buf = kgio_read(16384)
16       yield buf
17       yield buf while kgio_read(16384, buf)
18     end
19     self
20   end
22   def close
23     fd = fileno
24     RD[fd] = WR[fd] = nil
25     super
26   end
28   def kgio_wait_readable
29     fd = fileno
30     @f = Fiber.current
31     RD[fd] = self
32     Fiber.yield
33     RD[fd] = nil
34   end
36   def kgio_wait_writable
37     fd = fileno
38     @f = Fiber.current
39     WR[fd] = self
40     Fiber.yield
41     WR[fd] = nil
42   end
44   def self.included(klass)
45     if klass.method_defined?(:kgio_write)
46       klass.__send__(:alias_method, :write, :kgio_write)
47     end
48   end
49 end