epoll_wait: flags argument is unused
[rainbows.git] / lib / rainbows / revactor / client.rb
blobc587589bd0c255cf3ac636528821fab95fe8e02b
1 # -*- encoding: binary -*-
2 # :enddoc:
3 require 'fcntl'
4 class Rainbows::Revactor::Client
5   autoload :TeeSocket, 'rainbows/revactor/client/tee_socket'
6   RD_ARGS = {}
7   Rainbows.keepalive_timeout > 0 and
8     RD_ARGS[:timeout] = Rainbows.keepalive_timeout
9   attr_reader :kgio_addr
11   def initialize(client)
12     @client, @rd_args, @ts = client, [ nil ], nil
13     io = client.instance_variable_get(:@_io)
14     io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
15     @kgio_addr = if Revactor::TCP::Socket === client
16       @rd_args << RD_ARGS
17       client.remote_addr
18     else
19       Kgio::LOCALHOST
20     end
21   end
23   def kgio_read!(nr, buf)
24     buf.replace(@client.read)
25   end
27   def write(buf)
28     @client.write(buf)
29   end
31   def timed_read(buf2)
32     buf2.replace(@client.read(*@rd_args))
33   end
35   def set_input(env, hp)
36     env[RACK_INPUT] = 0 == hp.content_length ?
37                       NULL_IO : IC.new(@ts = TeeSocket.new(@client), hp)
38   end
40   def to_io
41     @client.instance_variable_get(:@_io)
42   end
44   def close
45     @client.close
46     @client = nil
47   end
49   def closed?
50     @client.nil?
51   end
53   def self.setup
54     self.const_set(:IC, Unicorn::HttpRequest.input_class)
55     include Rainbows::ProcessClient
56     include Methods
57   end
58 end
59 require 'rainbows/revactor/client/methods'