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, #bound, each, #linger, #local_address, parse, #reuse_address, #reuse_port, socket, ssl, tcp, #timeout, try_convert, udp, unix, #with

Constructor Details

#initialize(specification, **options) ⇒ HostEndpoint

Returns a new instance of HostEndpoint.



28
29
30
31
32
# File 'lib/async/io/host_endpoint.rb', line 28

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

Instance Method Details

#addressObject



40
41
42
# File 'lib/async/io/host_endpoint.rb', line 40

def address
	@specification
end

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

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

Yields:

  • (Socket)

    the bound socket

Returns:

  • (Array<Socket>)

    an array of bound sockets



79
80
81
82
83
# File 'lib/async/io/host_endpoint.rb', line 79

def bind(&block)
	Addrinfo.foreach(*@specification).map 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

Returns:

  • (Socket)

    the connected socket

Raises:

  • if no connection could complete successfully



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/async/io/host_endpoint.rb', line 52

def connect
	last_error = nil
	
	task = Task.current
	
	Addrinfo.foreach(*@specification) do |address|
		begin
			wrapper = Socket.connect(address, **@options, task: task)
		rescue Errno::ECONNREFUSED, Errno::ENETUNREACH, Errno::EAGAIN
			last_error = $!
		else
			return wrapper unless block_given?
			
			begin
				return yield wrapper, task
			ensure
				wrapper.close
			end
		end
	end
	
	raise last_error
end

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

Yields:

  • (AddressEndpoint)

    address endpoints by resolving the given host specification



86
87
88
89
90
91
92
# File 'lib/async/io/host_endpoint.rb', line 86

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

#hostnameObject



44
45
46
# File 'lib/async/io/host_endpoint.rb', line 44

def hostname
	@specification.first
end

#to_sObject



34
35
36
37
38
# File 'lib/async/io/host_endpoint.rb', line 34

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