IO::EndpointSourceIOEndpointBoundEndpoint

class BoundEndpoint

Represents an endpoint that has been bound to one or more sockets.

Definitions

def self.bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false)

Create a bound endpoint from an existing endpoint.

Signature

parameter endpoint Generic

The endpoint to bind.

option backlog Integer

The maximum length of the queue of pending connections.

option close_on_exec Boolean

Whether to close sockets on exec.

returns BoundEndpoint

A new bound endpoint instance.

Implementation

def self.bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false)
	sockets = endpoint.bind
	
	sockets.each do |socket|
		socket.close_on_exec = close_on_exec
	end
	
	return self.new(endpoint, sockets, **endpoint.options)
end

def initialize(endpoint, sockets, **options)

Initialize a new bound endpoint.

Signature

parameter endpoint Generic

The original endpoint that was bound.

parameter sockets Array(Socket)

The sockets that were bound.

parameter options Hash

Additional options to pass to the parent class.

Implementation

def initialize(endpoint, sockets, **options)
	super(**options)
	
	@endpoint = endpoint
	@sockets = sockets
end

attr :endpoint

Signature

attribute Generic

The original endpoint that was bound.

attr :sockets

Signature

attribute Array(Socket)

The sockets that were bound.

def local_address_endpoint(**options)

A endpoint for the local end of the bound socket.

Signature

returns CompositeEndpoint

A composite endpoint for the local end of the bound socket.

Implementation

def local_address_endpoint(**options)
	endpoints = @sockets.map do |socket|
		AddressEndpoint.new(socket.to_io.local_address, **options)
	end
	
	return CompositeEndpoint.new(endpoints)
end

def remote_address_endpoint(**options)

A endpoint for the remote end of the bound socket.

Signature

returns CompositeEndpoint

A composite endpoint for the remote end of the bound socket.

Implementation

def remote_address_endpoint(**options)
	endpoints = @sockets.map do |wrapper|
		AddressEndpoint.new(socket.to_io.remote_address, **options)
	end
	
	return CompositeEndpoint.new(endpoints)
end

def close

Close all bound sockets.

Implementation

def close
	@sockets.each(&:close)
	@sockets.clear
end

def to_s

Get a string representation of the bound endpoint.

Signature

returns String

A string representation of the bound endpoint.

Implementation

def to_s
	"bound:#{@endpoint}"
end

def inspect

Get a detailed string representation of the bound endpoint.

Signature

returns String

A detailed string representation including socket count.

Implementation

def inspect
	"\#<#{self.class} #{@sockets.size} bound sockets for #{@endpoint}>"
end

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

Bind the endpoint using the given wrapper.

Signature

parameter wrapper Wrapper

The wrapper to use for binding.

yields {|socket| ...}

If a block is given, yields each bound socket.

parameter socket Socket

A bound socket.

returns Array(Socket)

An array of bound sockets.

Implementation

def bind(wrapper = self.wrapper, &block)
	@sockets.map do |server|
		if block_given?
			wrapper.schedule do
				yield server
			end
		else
			server.dup
		end
	end
end