Class: Async::IO::HostEndpoint

Inherits:
Endpoint
  • Object
show all
Defined in:
lib/async/io/endpoint.rb

Instance Attribute Summary

Attributes inherited from Endpoint

#options

Instance Method Summary collapse

Methods inherited from Endpoint

#accept, each, parse, ssl, tcp, try_convert, udp, unix

Constructor Details

#initialize(specification, **options) ⇒ HostEndpoint

Returns a new instance of HostEndpoint.



106
107
108
109
110
# File 'lib/async/io/endpoint.rb', line 106

def initialize(specification, **options)
  super(**options)
  
  @specification = specification
end

Instance Method Details

#bind(&block) ⇒ Object



134
135
136
137
138
# File 'lib/async/io/endpoint.rb', line 134

def bind(&block)
  Addrinfo.foreach(*@specification) do |address|
    Socket.bind(address, **@options, &block)
  end
end

#connect(&block) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/async/io/endpoint.rb', line 120

def connect(&block)
  last_error = nil
  
  Addrinfo.foreach(*@specification).each do |address|
    begin
      return Socket.connect(address, **@options, &block)
    rescue
      last_error = $!
    end
  end
  
  raise last_error
end

#eachObject



140
141
142
143
144
145
146
# File 'lib/async/io/endpoint.rb', line 140

def each
  return to_enum unless block_given?
  
  Addrinfo.foreach(*@specification).each do |address|
    yield AddressEndpoint.new(address, **@options)
  end
end

#hostnameObject



116
117
118
# File 'lib/async/io/endpoint.rb', line 116

def hostname
  @specification.first
end

#to_sObject



112
113
114
# File 'lib/async/io/endpoint.rb', line 112

def to_s
  "\#<#{self.class} #{@specification.inspect}>"
end