Class: HTTPX::TCP

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

Direct Known Subclasses

SSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log

Constructor Details

#initialize(hostname, port, options) ⇒ TCP



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/httpx/io.rb', line 13

def initialize(hostname, port, options)
  @state = :idle
  @options = Options.new(options)
  @fallback_protocol = @options.fallback_protocol
  @port = port
  if @options.io
    @io = case @options.io
          when Hash
            @ip = TCPSocket.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 = TCPSocket.getaddress(hostname)
  end
  @io ||= build_socket
end

Instance Attribute Details

#ipObject (readonly)

Returns the value of attribute ip.



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

def ip
  @ip
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#closeObject



101
102
103
104
105
106
107
108
# File 'lib/httpx/io.rb', line 101

def close
  return if @keep_open || closed?
  begin
    @io.close
  ensure
    transition(:closed)
  end
end

#closed?Boolean



114
115
116
# File 'lib/httpx/io.rb', line 114

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

#connectObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/httpx/io.rb', line 49

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



110
111
112
# File 'lib/httpx/io.rb', line 110

def connected?
  @state == :connected
end

#inspectObject



118
119
120
121
# File 'lib/httpx/io.rb', line 118

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

#protocolObject



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

def protocol
  @fallback_protocol
end

#read(size, buffer) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/httpx/io.rb', line 66

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

#schemeObject



37
38
39
# File 'lib/httpx/io.rb', line 37

def scheme
  "http"
end

#to_ioObject



41
42
43
# File 'lib/httpx/io.rb', line 41

def to_io
  @io.to_io
end

#write(buffer) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/httpx/io.rb', line 75

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