class HTTPX::TCP

  1. lib/httpx/io/tcp.rb
Superclass: Object

Included modules

  1. Loggable

Attributes

addresses [R]
host [R]
ip [R]
port [R]

Public Class methods

new (origin, addresses, options)
[show source]
   # File lib/httpx/io/tcp.rb
16 def initialize(origin, addresses, options)
17   @state = :idle
18   @hostname = origin.host
19   @addresses = addresses
20   @options = Options.new(options)
21   @fallback_protocol = @options.fallback_protocol
22   @port = origin.port
23   if @options.io
24     @io = case @options.io
25           when Hash
26             @options.io[origin.authority]
27           else
28             @options.io
29     end
30     _, _, _, @ip = @io.addr
31     @addresses ||= [@ip]
32     @ip_index = @addresses.size - 1
33     unless @io.nil?
34       @keep_open = true
35       @state = :connected
36     end
37   else
38     @ip_index = @addresses.size - 1
39     @ip = @addresses[@ip_index]
40   end
41   @io ||= build_socket
42 end

Public Instance methods

close ()
[show source]
    # File lib/httpx/io/tcp.rb
116 def close
117   return if @keep_open || closed?
118 
119   begin
120     @io.close
121   ensure
122     transition(:closed)
123   end
124 end
closed? ()
[show source]
    # File lib/httpx/io/tcp.rb
130 def closed?
131   @state == :idle || @state == :closed
132 end
connect ()
[show source]
   # File lib/httpx/io/tcp.rb
56 def connect
57   return unless closed?
58 
59   begin
60     if @io.closed?
61       transition(:idle)
62       @io = build_socket
63     end
64     @io.connect_nonblock(Socket.sockaddr_in(@port, @ip.to_s))
65   rescue Errno::EISCONN
66   end
67   transition(:connected)
68 rescue Errno::EHOSTUNREACH => e
69   raise e if @ip_index <= 0
70 
71   @ip_index -= 1
72   retry
73 rescue Errno::EINPROGRESS,
74        Errno::EALREADY,
75        ::IO::WaitReadable
76 end
connected? ()
[show source]
    # File lib/httpx/io/tcp.rb
126 def connected?
127   @state == :connected
128 end
inspect ()
[show source]
    # File lib/httpx/io/tcp.rb
134 def inspect
135   id = @io.closed? ? "closed" : @io.fileno
136   "#<TCP(fd: #{id}): #{@ip}:#{@port} (state: #{@state})>"
137 end
protocol ()
[show source]
   # File lib/httpx/io/tcp.rb
52 def protocol
53   @fallback_protocol
54 end
read (size, buffer)
[show source]
   # File lib/httpx/io/tcp.rb
79 def read(size, buffer)
80   @io.read_nonblock(size, buffer)
81   buffer.bytesize
82 rescue ::IO::WaitReadable
83   0
84 rescue EOFError
85   nil
86 end
scheme ()
[show source]
   # File lib/httpx/io/tcp.rb
44 def scheme
45   "http"
46 end
to_io ()
[show source]
   # File lib/httpx/io/tcp.rb
48 def to_io
49   @io.to_io
50 end
write (buffer)
[show source]
   # File lib/httpx/io/tcp.rb
88 def write(buffer)
89   siz = @io.write_nonblock(buffer)
90   buffer.shift!(siz)
91   siz
92 rescue ::IO::WaitWritable
93   0
94 rescue EOFError
95   nil
96 end