switch from IO#sendfile_nonblock to IO#trysendfile
[rainbows.git] / lib / rainbows / writer_thread_pool / client.rb
blobf02826e1f8421881f9e2337fdf338aa65dcfff59
1 # -*- encoding: binary -*-
2 # :enddoc:
3 # used to wrap a BasicSocket to use with +q+ for all writes
4 # this is compatible with IO.select
5 class Rainbows::WriterThreadPool::Client < Struct.new(:to_io, :q)
6   include Rainbows::SocketProxy
7   include Rainbows::ProcessClient
9   module Methods
10     def write_body_each(body)
11       q << [ to_io, :write_body_each, body ]
12     end
14     def write_response_close(status, headers, body, alive)
15       to_io.instance_variable_set(:@hp, @hp) # XXX ugh
16       Rainbows::SyncClose.new(body) { |sync_body|
17         q << [ to_io, :write_response, status, headers, sync_body, alive ]
18       }
19     end
21     if IO.respond_to?(:copy_stream) || IO.method_defined?(:trysendfile)
22       def write_response(status, headers, body, alive)
23         if body.respond_to?(:close)
24           write_response_close(status, headers, body, alive)
25         elsif body.respond_to?(:to_path)
26           write_response_path(status, headers, body, alive)
27         else
28           super
29         end
30       end
32       def write_body_file(body, range)
33         q << [ to_io, :write_body_file, body, range ]
34       end
36       def write_body_stream(body)
37         q << [ to_io, :write_body_stream, body ]
38       end
39     else # each-only body response
40       def write_response(status, headers, body, alive)
41         if body.respond_to?(:close)
42           write_response_close(status, headers, body, alive)
43         else
44           super
45         end
46       end
47     end # each-only body response
48   end # module Methods
49   include Methods
51   def write(buf)
52     q << [ to_io, buf ]
53   end
55   def close
56     q << [ to_io, :close ]
57   end
59   def closed?
60     to_io.closed?
61   end
62 end