Class: Async::IO::HostEndpoint

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

Instance Attribute Summary

Attributes inherited from Endpoint

#options

Instance Method Summary collapse

Methods inherited from Endpoint

#accept, each, parse, #reuse_port, socket, ssl, tcp, #timeout, try_convert, udp, unix

Constructor Details

#initialize(specification, **options) ⇒ HostEndpoint



26
27
28
29
30
# File 'lib/async/io/host_endpoint.rb', line 26

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

Instance Method Details

#bind {|Socket| ... } ⇒ Array<Socket>

Invokes the given block for every address which can be bound to.

Yields:

  • (Socket)

    the bound socket



63
64
65
66
67
# File 'lib/async/io/host_endpoint.rb', line 63

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

#connect {|Socket| ... } ⇒ Socket

Try to connect to the given host by connecting to each address in sequence until a connection is made.

Yields:

  • (Socket)

    the socket which is being connected, may be invoked more than once

Raises:

  • if no connection could complete successfully



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/async/io/host_endpoint.rb', line 46

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

#each {|AddressEndpoint| ... } ⇒ Object

Yields:

  • (AddressEndpoint)

    address endpoints by resolving the given host specification



70
71
72
73
74
75
76
# File 'lib/async/io/host_endpoint.rb', line 70

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

#hostnameObject



38
39
40
# File 'lib/async/io/host_endpoint.rb', line 38

def hostname
  @specification.first
end

#to_sObject



32
33
34
35
36
# File 'lib/async/io/host_endpoint.rb', line 32

def to_s
  nodename, service, family, socktype, protocol, flags = @specification
  
  "\#<#{self.class} name=#{nodename.inspect} service=#{service.inspect} family=#{family.inspect} type=#{socktype.inspect} protocol=#{protocol.inspect} flags=#{flags.inspect}>"
end