class Async::IO::Endpoint
Endpoints represent a way of connecting or binding to an address.
Attributes
Public Class Methods
Generate a list of endpoints from an array.
# File lib/async/io/endpoint/each.rb, line 47 def self.each(specifications, &block) return to_enum(:each, specifications) unless block_given? specifications.each do |specification| yield try_convert(specification) end end
# File lib/async/io/endpoint.rb, line 32 def initialize(**options) @options = options.freeze end
Create an Endpoint
instance by URI scheme. The host and port of the URI will be passed to the Endpoint
factory method, along with any options.
@param string [String] URI as string. Scheme will decide implementation used. @param options keyword arguments passed through to {#initialize}
@see Endpoint.ssl
ssl - invoked when parsing a URL with the ssl scheme “ssl://127.0.0.1” @see Endpoint.tcp
tcp - invoked when parsing a URL with the tcp scheme: “tcp://127.0.0.1” @see Endpoint.udp
udp - invoked when parsing a URL with the udp scheme: “udp://127.0.0.1” @see Endpoint.unix
unix - invoked when parsing a URL with the unix scheme: “unix://127.0.0.1”
# File lib/async/io/endpoint.rb, line 123 def self.parse(string, **options) uri = URI.parse(string) self.public_send(uri.scheme, uri.host, uri.port, **options) end
# File lib/async/io/socket_endpoint.rb, line 68 def self.socket(socket, **options) SocketEndpoint.new(socket, **options) end
@param args @param ssl_context [OpenSSL::SSL::SSLContext, nil] @param hostname [String, nil] @param options keyword arguments passed through to {Endpoint.tcp}
@return [SSLEndpoint]
# File lib/async/io/ssl_endpoint.rb, line 114 def self.ssl(*args, ssl_context: nil, hostname: nil, **options) SSLEndpoint.new(self.tcp(*args, **options), ssl_context: ssl_context, hostname: hostname) end
@param args 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]
# File lib/async/io/host_endpoint.rb, line 100 def self.tcp(*args, **options) args[3] = ::Socket::SOCK_STREAM HostEndpoint.new(args, **options) end
# File lib/async/io/endpoint/each.rb, line 30 def self.try_convert(specification) if specification.is_a? self specification elsif specification.is_a? Array self.send(*specification) elsif specification.is_a? String self.parse(specification) elsif specification.is_a? ::BasicSocket self.socket(specification) elsif specification.is_a? Generic self.new(specification) else raise ArgumentError.new("Not sure how to convert #{specification} to endpoint!") end end
@param args 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]
# File lib/async/io/host_endpoint.rb, line 110 def self.udp(*args, **options) args[3] = ::Socket::SOCK_DGRAM HostEndpoint.new(args, **options) end
@param path [String] @param type Socket
type @param options keyword arguments passed through to {UNIXEndpoint#initialize}
@return [UNIXEndpoint]
# File lib/async/io/unix_endpoint.rb, line 69 def self.unix(path = "", type = ::Socket::SOCK_STREAM, **options) UNIXEndpoint.new(path, type, **options) end
Public Instance Methods
Accept connections from the specified endpoint. @param backlog [Integer] the number of connections to listen for.
# File lib/async/io/endpoint.rb, line 89 def accept(backlog = Socket::SOMAXCONN, &block) bind do |server| server.listen(backlog) server.accept_each(&block) end end
Map all endpoints by invoking `#bind`. @yield the bound wrapper.
# File lib/async/io/endpoint.rb, line 99 def bound wrappers = [] self.each do |endpoint| wrapper = endpoint.bind wrappers << wrapper yield wrapper end return wrappers ensure wrappers.each(&:close) if $! end
Endpoints sometimes have multiple paths. @yield [Endpoint] Enumerate all discrete paths as endpoints.
# File lib/async/io/endpoint.rb, line 81 def each return to_enum unless block_given? yield self end
@return [String] The hostname of the bound socket.
# File lib/async/io/endpoint.rb, line 47 def hostname @options[:hostname] end
Controls SO_LINGER. The amount of time the socket will stay in the `TIME_WAIT` state after being closed. @return [Integer, nil] The value for SO_LINGER.
# File lib/async/io/endpoint.rb, line 65 def linger @options[:linger] end
@return [Address] the address to bind to before connecting.
# File lib/async/io/endpoint.rb, line 75 def local_address @options[:local_address] end
If `SO_REUSEADDR` is enabled on a socket prior to binding it, the socket can be successfully bound unless there is a conflict with another socket bound to exactly the same combination of source address and port. Additionally, when set, binding a socket to the address of an existing socket in `TIME_WAIT` is not an error. @return [Boolean] The value for `SO_REUSEADDR`.
# File lib/async/io/endpoint.rb, line 59 def reuse_address @options[:reuse_address] end
If `SO_REUSEPORT` is enabled on a socket, the socket can be successfully bound even if there are existing sockets bound to the same address, as long as all prior bound sockets also had `SO_REUSEPORT` set before they were bound. @return [Boolean, nil] The value for `SO_REUSEPORT`.
# File lib/async/io/endpoint.rb, line 53 def reuse_port @options[:reuse_port] end
@return [Numeric] The default timeout for socket operations.
# File lib/async/io/endpoint.rb, line 70 def timeout @options[:timeout] end
# File lib/async/io/endpoint.rb, line 36 def with(**options) dup = self.dup dup.options = @options.merge(options) return dup end