lib/io/endpoint/host_endpoint.rb in io-endpoint-0.1.0 vs lib/io/endpoint/host_endpoint.rb in io-endpoint-0.2.0

- old
+ new

@@ -17,29 +17,29 @@ 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 - def address - @specification - end + attr :specification + def hostname @specification.first end # Try to connect to the given host by connecting to each address in sequence until a connection is made. # @yield [Socket] the socket which is being connected, may be invoked more than once # @return [Socket] the connected socket # @raise if no connection could complete successfully - def connect + def connect(wrapper = Wrapper.default, &block) last_error = nil Addrinfo.foreach(*@specification) do |address| begin - socket = Socket.connect(address, **@options) + 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) @@ -53,13 +53,13 @@ end # Invokes the given block for every address which can be bound to. # @yield [Socket] the bound socket # @return [Array<Socket>] an array of bound sockets - def bind(&block) + def bind(wrapper = Wrapper.default, &block) Addrinfo.foreach(*@specification).map do |address| - Socket.bind(address, **@options, &block) + wrapper.bind(address, **@options, &block) end end # @yield [AddressEndpoint] address endpoints by resolving the given host specification def each @@ -69,25 +69,25 @@ yield AddressEndpoint.new(address, **@options) end end end - # @param args nodename, service, family, socktype, protocol, flags. `socktype` will be set to Socket::SOCK_STREAM. + # @param arguments nodename, service, family, socktype, protocol, flags. `socktype` will be set to Socket::SOCK_STREAM. # @param options keyword arguments passed on to {HostEndpoint#initialize} # # @return [HostEndpoint] - def self.tcp(*args, **options) - args[3] = ::Socket::SOCK_STREAM + def self.tcp(*arguments, **options) + arguments[3] = ::Socket::SOCK_STREAM - HostEndpoint.new(args, **options) + HostEndpoint.new(arguments, **options) end - # @param args nodename, service, family, socktype, protocol, flags. `socktype` will be set to Socket::SOCK_DGRAM. + # @param arguments nodename, service, family, socktype, protocol, flags. `socktype` will be set to Socket::SOCK_DGRAM. # @param options keyword arguments passed on to {HostEndpoint#initialize} # # @return [HostEndpoint] - def self.udp(*args, **options) - args[3] = ::Socket::SOCK_DGRAM + def self.udp(*arguments, **options) + arguments[3] = ::Socket::SOCK_DGRAM - HostEndpoint.new(args, **options) + HostEndpoint.new(arguments, **options) end end