IO::EndpointSourceIOEndpointGeneric

class Generic

Endpoints represent a way of connecting or binding to an address.

Definitions

def bound(**options)

Create a bound endpoint from this endpoint.

Signature

parameter options Hash

Options to pass to IO::Endpoint::BoundEndpoint.bound.

returns BoundEndpoint

A new bound endpoint instance.

Implementation

def bound(**options)
	BoundEndpoint.bound(self, **options)
end

def connected(**options)

Create a connected endpoint from this endpoint.

Signature

parameter options Hash

Options to pass to IO::Endpoint::ConnectedEndpoint.connected.

returns ConnectedEndpoint

A new connected endpoint instance.

Implementation

def connected(**options)
	ConnectedEndpoint.connected(self, **options)
end

def initialize(**options)

Initialize a new generic endpoint.

Signature

parameter options Hash

Configuration options for the endpoint.

Implementation

def initialize(**options)
	@options = options.freeze
end

def with(**options)

Create a new endpoint with merged options.

Signature

parameter options Hash

Additional options to merge with existing options.

returns Generic

A new endpoint instance with merged options.

Implementation

def with(**options)
	dup = self.dup
	
	dup.options = @options.merge(options)
	
	return dup
end

def hostname

Signature

returns String

The hostname of the bound socket.

Implementation

def hostname
	@options[:hostname]
end

def reuse_port?

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.

Signature

returns Boolean, nil

The value for SO_REUSEPORT.

Implementation

def reuse_port?
	@options[:reuse_port]
end

def reuse_address?

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.

Signature

returns Boolean

The value for SO_REUSEADDR.

Implementation

def reuse_address?
	@options[:reuse_address]
end

def linger

Controls SO_LINGER. The amount of time the socket will stay in the TIME_WAIT state after being closed.

Signature

returns Integer, nil

The value for SO_LINGER.

Implementation

def linger
	@options[:linger]
end

def timeout

Signature

returns Numeric

The default timeout for socket operations.

Implementation

def timeout
	@options[:timeout]
end

def local_address

Signature

returns Address

the address to bind to before connecting.

Implementation

def local_address
	@options[:local_address]
end

def bind(wrapper = self.wrapper, &block)

Bind a socket to the given address. If a block is given, the socket will be automatically closed when the block exits.

Signature

parameter wrapper Wrapper

The wrapper to use for binding.

yields {|socket| ...}

If a block is given, yields the bound socket.

parameter socket Socket

The socket which has been bound.

returns Array(Socket)

the bound socket

Implementation

def bind(wrapper = self.wrapper, &block)
	raise NotImplementedError
end

def connect(wrapper = self.wrapper, &block)

Connects a socket to the given address. If a block is given, the socket will be automatically closed when the block exits.

Signature

parameter wrapper Wrapper

The wrapper to use for connecting.

returns Socket

the connected socket

Implementation

def connect(wrapper = self.wrapper, &block)
	raise NotImplementedError
end

def accept(wrapper = self.wrapper, &block)

Bind and accept connections on the given address.

Signature

parameter wrapper Wrapper

The wrapper to use for accepting connections.

yields {|socket| ...}

For each accepted connection, yields the socket.

parameter socket Socket

The accepted socket.

Implementation

def accept(wrapper = self.wrapper, &block)
	bind(wrapper) do |server|
		wrapper.accept(server, **@options, &block)
	end
end

def each

Enumerate all discrete paths as endpoints.

Signature

yields {|endpoint| ...}

For each endpoint, yields it.

parameter endpoint Endpoint

The endpoint.

Implementation

def each
	return to_enum unless block_given?
	
	yield self
end

def self.parse(string, **options)

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.

You should not use untrusted input as it may execute arbitrary code.

Signature

parameter string String

URI as string. Scheme will decide implementation used.

Implementation

def self.parse(string, **options)
	uri = URI.parse(string)
	
	IO::Endpoint.public_send(uri.scheme, uri.host, uri.port, **options)
end

def wrapper

The default wrapper to use for binding, connecting, and accepting connections.

Implementation

def wrapper
	@options[:wrapper] || Wrapper.default
end