Class: HTTPX::TCP
Constant Summary
Constants included from Loggable
Instance Attribute Summary collapse
-
#addresses ⇒ Object
readonly
Returns the value of attribute addresses.
-
#ip ⇒ Object
(also: #host)
readonly
Returns the value of attribute ip.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #connect ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(origin, addresses, options) ⇒ TCP
constructor
A new instance of TCP.
-
#inspect ⇒ Object
:nocov:.
- #interests ⇒ Object
- #protocol ⇒ Object
-
#read(size, buffer) ⇒ Object
:nocov:.
- #to_io ⇒ Object
- #write(buffer) ⇒ Object
Methods included from Loggable
Constructor Details
#initialize(origin, addresses, options) ⇒ TCP
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/httpx/io/tcp.rb', line 18 def initialize(origin, addresses, ) @state = :idle @hostname = origin.host @addresses = addresses = Options.new() @fallback_protocol = .fallback_protocol @port = origin.port if .io @io = case .io when Hash .io[origin.] else .io end _, _, _, @ip = @io.addr @addresses ||= [@ip] @ip_index = @addresses.size - 1 unless @io.nil? @keep_open = true @state = :connected end else @ip_index = @addresses.size - 1 @ip = @addresses[@ip_index] end @io ||= build_socket end |
Instance Attribute Details
#addresses ⇒ Object (readonly)
Returns the value of attribute addresses.
12 13 14 |
# File 'lib/httpx/io/tcp.rb', line 12 def addresses @addresses end |
#ip ⇒ Object (readonly) Also known as: host
Returns the value of attribute ip.
10 11 12 |
# File 'lib/httpx/io/tcp.rb', line 10 def ip @ip end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
10 11 12 |
# File 'lib/httpx/io/tcp.rb', line 10 def port @port end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
14 15 16 |
# File 'lib/httpx/io/tcp.rb', line 14 def state @state end |
Instance Method Details
#close ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/httpx/io/tcp.rb', line 129 def close return if @keep_open || closed? begin @io.close ensure transition(:closed) end end |
#closed? ⇒ Boolean
143 144 145 |
# File 'lib/httpx/io/tcp.rb', line 143 def closed? @state == :idle || @state == :closed end |
#connect ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/httpx/io/tcp.rb', line 58 def connect return unless closed? begin if @io.closed? transition(:idle) @io = build_socket end @io.connect_nonblock(Socket.sockaddr_in(@port, @ip.to_s)) rescue Errno::EISCONN end transition(:connected) rescue Errno::EHOSTUNREACH => e raise e if @ip_index <= 0 @ip_index -= 1 retry rescue Errno::ETIMEDOUT => e raise ConnectTimeoutError, e. if @ip_index <= 0 @ip_index -= 1 retry rescue Errno::EINPROGRESS, Errno::EALREADY, ::IO::WaitReadable end |
#connected? ⇒ Boolean
139 140 141 |
# File 'lib/httpx/io/tcp.rb', line 139 def connected? @state == :connected end |
#inspect ⇒ Object
:nocov:
148 149 150 151 |
# File 'lib/httpx/io/tcp.rb', line 148 def inspect id = @io.closed? ? "closed" : @io.fileno "#<TCP(fd: #{id}): #{@ip}:#{@port} (state: #{@state})>" end |
#interests ⇒ Object
46 47 48 |
# File 'lib/httpx/io/tcp.rb', line 46 def interests :w end |
#protocol ⇒ Object
54 55 56 |
# File 'lib/httpx/io/tcp.rb', line 54 def protocol @fallback_protocol end |
#read(size, buffer) ⇒ Object
:nocov:
87 88 89 90 91 92 93 94 95 |
# File 'lib/httpx/io/tcp.rb', line 87 def read(size, buffer) @io.read_nonblock(size, buffer) buffer.bytesize rescue ::IO::WaitReadable buffer.clear 0 rescue EOFError nil end |
#to_io ⇒ Object
50 51 52 |
# File 'lib/httpx/io/tcp.rb', line 50 def to_io @io.to_io end |
#write(buffer) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/httpx/io/tcp.rb', line 97 def write(buffer) siz = @io.write_nonblock(buffer) buffer.shift!(siz) siz rescue ::IO::WaitWritable 0 rescue EOFError nil end |