class HTTPX::TCP

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

Methods

Public Class

  1. new

Public Instance

  1. addresses
  2. close
  3. closed?
  4. connect
  5. connected?
  6. inspect
  7. ip
  8. port
  9. protocol
  10. read
  11. to_io
  12. write

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
119 def close
120   return if @keep_open || closed?
121 
122   begin
123     @io.close
124   ensure
125     transition(:closed)
126   end
127 end
closed? ()
[show source]
    # File lib/httpx/io/tcp.rb
133 def closed?
134   @state == :idle || @state == :closed
135 end
connect ()
[show source]
   # File lib/httpx/io/tcp.rb
52 def connect
53   return unless closed?
54 
55   begin
56     if @io.closed?
57       transition(:idle)
58       @io = build_socket
59     end
60     @io.connect_nonblock(Socket.sockaddr_in(@port, @ip.to_s))
61   rescue Errno::EISCONN
62   end
63   transition(:connected)
64 rescue Errno::EHOSTUNREACH => e
65   raise e if @ip_index <= 0
66 
67   @ip_index -= 1
68   retry
69 rescue Errno::ETIMEDOUT => e
70   raise ConnectTimeout, e.message if @ip_index <= 0
71 
72   @ip_index -= 1
73   retry
74 rescue Errno::EINPROGRESS,
75        Errno::EALREADY,
76        ::IO::WaitReadable
77 end
connected? ()
[show source]
    # File lib/httpx/io/tcp.rb
129 def connected?
130   @state == :connected
131 end
inspect ()

:nocov:

[show source]
    # File lib/httpx/io/tcp.rb
138 def inspect
139   id = @io.closed? ? "closed" : @io.fileno
140   "#<TCP(fd: #{id}): #{@ip}:#{@port} (state: #{@state})>"
141 end
protocol ()
[show source]
   # File lib/httpx/io/tcp.rb
48 def protocol
49   @fallback_protocol
50 end
read (size, buffer)

:nocov:

[show source]
   # File lib/httpx/io/tcp.rb
81 def read(size, buffer)
82   @io.read_nonblock(size, buffer)
83   buffer.bytesize
84 rescue ::IO::WaitReadable
85   0
86 rescue EOFError
87   nil
88 end
to_io ()
[show source]
   # File lib/httpx/io/tcp.rb
44 def to_io
45   @io.to_io
46 end
write (buffer)
[show source]
   # File lib/httpx/io/tcp.rb
90 def write(buffer)
91   siz = @io.write_nonblock(buffer)
92   buffer.shift!(siz)
93   siz
94 rescue ::IO::WaitWritable
95   0
96 rescue EOFError
97   nil
98 end