Class: HTTPX::TCP
Constant Summary
Constants included from Loggable
Instance Attribute Summary collapse
-
#ip ⇒ Object
(also: #host)
readonly
Returns the value of attribute ip.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #connect ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(uri, options) ⇒ TCP
constructor
A new instance of TCP.
- #inspect ⇒ Object
- #protocol ⇒ Object
- #read(size, buffer) ⇒ Object
- #scheme ⇒ Object
- #to_io ⇒ Object
- #write(buffer) ⇒ Object
Methods included from Loggable
Constructor Details
#initialize(uri, options) ⇒ TCP
Returns a new instance of TCP.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/httpx/io/tcp.rb', line 14 def initialize(uri, ) @state = :idle @hostname = uri.host @options = Options.new() @fallback_protocol = @options.fallback_protocol @port = uri.port if @options.io @io = case @options.io when Hash @ip = Resolv.getaddress(@hostname) @options.io[@ip] || @options.io["#{@ip}:#{@port}"] else @ip = @hostname @options.io end unless @io.nil? @keep_open = true @state = :connected end else @ip = Resolv.getaddress(@hostname) end @io ||= build_socket end |
Instance Attribute Details
#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 |
Instance Method Details
#close ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/httpx/io/tcp.rb', line 103 def close return if @keep_open || closed? begin @io.close ensure transition(:closed) end end |
#closed? ⇒ Boolean
116 117 118 |
# File 'lib/httpx/io/tcp.rb', line 116 def closed? @state == :idle || @state == :closed end |
#connect ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/httpx/io/tcp.rb', line 51 def connect return unless closed? begin if @io.closed? transition(:idle) @io = build_socket end @io.connect_nonblock(Socket.sockaddr_in(@port, @ip)) rescue Errno::EISCONN end transition(:connected) rescue Errno::EINPROGRESS, Errno::EALREADY, ::IO::WaitReadable end |
#connected? ⇒ Boolean
112 113 114 |
# File 'lib/httpx/io/tcp.rb', line 112 def connected? @state == :connected end |
#inspect ⇒ Object
120 121 122 123 |
# File 'lib/httpx/io/tcp.rb', line 120 def inspect id = @io.closed? ? "closed" : @io.fileno "#<TCP(fd: #{id}): #{@ip}:#{@port} (state: #{@state})>" end |
#protocol ⇒ Object
47 48 49 |
# File 'lib/httpx/io/tcp.rb', line 47 def protocol @fallback_protocol end |
#read(size, buffer) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/httpx/io/tcp.rb', line 68 def read(size, buffer) @io.read_nonblock(size, buffer) buffer.bytesize rescue ::IO::WaitReadable 0 rescue EOFError nil end |
#scheme ⇒ Object
39 40 41 |
# File 'lib/httpx/io/tcp.rb', line 39 def scheme "http" end |
#to_io ⇒ Object
43 44 45 |
# File 'lib/httpx/io/tcp.rb', line 43 def to_io @io.to_io end |
#write(buffer) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/httpx/io/tcp.rb', line 77 def write(buffer) siz = @io.write_nonblock(buffer) buffer.slice!(0, siz) siz rescue ::IO::WaitWritable 0 rescue EOFError nil end |