Class: IO::Endpoint::HostEndpoint
- Defined in:
- lib/io/endpoint/host_endpoint.rb
Instance Attribute Summary collapse
-
#specification ⇒ Object
readonly
Returns the value of attribute specification.
Attributes inherited from Generic
Instance Method Summary collapse
-
#bind(wrapper = self.wrapper) {|Socket| ... } ⇒ Array<Socket>
Invokes the given block for every address which can be bound to.
-
#connect(wrapper = self.wrapper) {|Socket| ... } ⇒ Socket
Try to connect to the given host by connecting to each address in sequence until a connection is made.
- #each {|AddressEndpoint| ... } ⇒ Object
- #hostname ⇒ Object
-
#initialize(specification, **options) ⇒ HostEndpoint
constructor
A new instance of HostEndpoint.
- #inspect ⇒ Object
- #service ⇒ Object
- #to_s ⇒ Object
Methods inherited from Generic
#accept, #bound, #connected, #linger, #local_address, parse, #reuse_address?, #reuse_port?, #timeout, #with, #wrapper
Constructor Details
#initialize(specification, **options) ⇒ HostEndpoint
Returns a new instance of HostEndpoint.
10 11 12 13 14 |
# File 'lib/io/endpoint/host_endpoint.rb', line 10 def initialize(specification, **) super(**) @specification = specification end |
Instance Attribute Details
#specification ⇒ Object (readonly)
Returns the value of attribute specification.
27 28 29 |
# File 'lib/io/endpoint/host_endpoint.rb', line 27 def specification @specification end |
Instance Method Details
#bind(wrapper = self.wrapper) {|Socket| ... } ⇒ Array<Socket>
Invokes the given block for every address which can be bound to.
66 67 68 69 70 |
# File 'lib/io/endpoint/host_endpoint.rb', line 66 def bind(wrapper = self.wrapper, &block) Addrinfo.foreach(*@specification).map do |address| wrapper.bind(address, **@options, &block) end end |
#connect(wrapper = self.wrapper) {|Socket| ... } ⇒ Socket
Try to connect to the given host by connecting to each address in sequence until a connection is made.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/io/endpoint/host_endpoint.rb', line 41 def connect(wrapper = self.wrapper, &block) last_error = nil Addrinfo.foreach(*@specification) do |address| begin socket = wrapper.connect(address, **@options) rescue Errno::ECONNREFUSED, Errno::ENETUNREACH, Errno::EAGAIN => last_error # Try again unless if possible, otherwise raise... else return socket unless block_given? begin return yield(socket) ensure socket.close end end end raise last_error end |
#each {|AddressEndpoint| ... } ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/io/endpoint/host_endpoint.rb', line 73 def each return to_enum unless block_given? Addrinfo.foreach(*@specification) do |address| yield AddressEndpoint.new(address, **@options) end end |
#hostname ⇒ Object
29 30 31 |
# File 'lib/io/endpoint/host_endpoint.rb', line 29 def hostname @specification[0] end |
#inspect ⇒ Object
20 21 22 23 24 |
# File 'lib/io/endpoint/host_endpoint.rb', line 20 def inspect 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 |
#service ⇒ Object
33 34 35 |
# File 'lib/io/endpoint/host_endpoint.rb', line 33 def service @specification[1] end |
#to_s ⇒ Object
16 17 18 |
# File 'lib/io/endpoint/host_endpoint.rb', line 16 def to_s "host:#{@specification[0]}:#{@specification[1]}" end |