Class: HTTPX::TCP

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/httpx/io/tcp.rb

Direct Known Subclasses

SSL, UNIX

Constant Summary

Constants included from Loggable

Loggable::COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log, #log_exception

Constructor Details

#initialize(origin, addresses, options) ⇒ TCP

Returns a new instance of TCP.


16
17
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
# File 'lib/httpx/io/tcp.rb', line 16

def initialize(origin, addresses, options)
  @state = :idle
  @hostname = origin.host
  @addresses = addresses
  @options = Options.new(options)
  @fallback_protocol = @options.fallback_protocol
  @port = origin.port
  if @options.io
    @io = case @options.io
          when Hash
            @options.io[origin.authority]
          else
            @options.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

#addressesObject (readonly)

Returns the value of attribute addresses


12
13
14
# File 'lib/httpx/io/tcp.rb', line 12

def addresses
  @addresses
end

#ipObject (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

#portObject (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

#closeObject


116
117
118
119
120
121
122
123
124
# File 'lib/httpx/io/tcp.rb', line 116

def close
  return if @keep_open || closed?

  begin
    @io.close
  ensure
    transition(:closed)
  end
end

#closed?Boolean

Returns:

  • (Boolean)

130
131
132
# File 'lib/httpx/io/tcp.rb', line 130

def closed?
  @state == :idle || @state == :closed
end

#connectObject


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/httpx/io/tcp.rb', line 56

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::EINPROGRESS,
       Errno::EALREADY,
       ::IO::WaitReadable
end

#connected?Boolean

Returns:

  • (Boolean)

126
127
128
# File 'lib/httpx/io/tcp.rb', line 126

def connected?
  @state == :connected
end

#inspectObject


134
135
136
137
# File 'lib/httpx/io/tcp.rb', line 134

def inspect
  id = @io.closed? ? "closed" : @io.fileno
  "#<TCP(fd: #{id}): #{@ip}:#{@port} (state: #{@state})>"
end

#protocolObject


52
53
54
# File 'lib/httpx/io/tcp.rb', line 52

def protocol
  @fallback_protocol
end

#read(size, buffer) ⇒ Object


79
80
81
82
83
84
85
86
# File 'lib/httpx/io/tcp.rb', line 79

def read(size, buffer)
  @io.read_nonblock(size, buffer)
  buffer.bytesize
rescue ::IO::WaitReadable
  0
rescue EOFError
  nil
end

#schemeObject


44
45
46
# File 'lib/httpx/io/tcp.rb', line 44

def scheme
  "http"
end

#to_ioObject


48
49
50
# File 'lib/httpx/io/tcp.rb', line 48

def to_io
  @io.to_io
end

#write(buffer) ⇒ Object


88
89
90
91
92
93
94
95
96
# File 'lib/httpx/io/tcp.rb', line 88

def write(buffer)
  siz = @io.write_nonblock(buffer)
  buffer.shift!(siz)
  siz
rescue ::IO::WaitWritable
  0
rescue EOFError
  nil
end