Class: IO::Endpoint::BoundEndpoint

Inherits:
Generic
  • Object
show all
Defined in:
lib/io/endpoint/bound_endpoint.rb

Overview

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

Instance Attribute Summary collapse

Attributes inherited from Generic

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#accept, #bound, #connect, #connected, #each, #hostname, #linger, #local_address, parse, #reuse_address?, #reuse_port?, #timeout, #with, #wrapper

Constructor Details

#initialize(endpoint, sockets, **options) ⇒ BoundEndpoint

Initialize a new bound endpoint.



32
33
34
35
36
37
# File 'lib/io/endpoint/bound_endpoint.rb', line 32

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

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



40
41
42
# File 'lib/io/endpoint/bound_endpoint.rb', line 40

def endpoint
  @endpoint
end

#socketsObject (readonly)

Returns the value of attribute sockets.



42
43
44
# File 'lib/io/endpoint/bound_endpoint.rb', line 42

def sockets
  @sockets
end

#The original endpoint that was bound.(originalendpointthatwasbound.) ⇒ Object (readonly)



40
# File 'lib/io/endpoint/bound_endpoint.rb', line 40

attr :endpoint

#The sockets that were bound.(socketsthatwerebound.) ⇒ Object (readonly)



42
# File 'lib/io/endpoint/bound_endpoint.rb', line 42

attr :sockets

Class Method Details

.bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false) ⇒ Object

Create a bound endpoint from an existing endpoint.

Parameters:

  • backlog (Hash) (defaults to: Socket::SOMAXCONN)

    a customizable set of options

  • close_on_exec (Hash) (defaults to: false)

    a customizable set of options

Options Hash (backlog:):

  • The (Integer)

    maximum length of the queue of pending connections.

Options Hash (close_on_exec:):

  • Whether (Boolean)

    to close sockets on exec.



18
19
20
21
22
23
24
25
26
# File 'lib/io/endpoint/bound_endpoint.rb', line 18

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

Instance Method Details

#bind(wrapper = self.wrapper, &block) ⇒ Object

Bind the endpoint using the given wrapper.



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/io/endpoint/bound_endpoint.rb', line 87

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

#closeObject

Close all bound sockets.



65
66
67
68
# File 'lib/io/endpoint/bound_endpoint.rb', line 65

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

#inspectObject

Get a detailed string representation of the bound endpoint.



78
79
80
# File 'lib/io/endpoint/bound_endpoint.rb', line 78

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

#local_address_endpoint(**options) ⇒ Object

A endpoint for the local end of the bound socket.



46
47
48
49
50
51
52
# File 'lib/io/endpoint/bound_endpoint.rb', line 46

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

#remote_address_endpoint(**options) ⇒ Object

A endpoint for the remote end of the bound socket.



56
57
58
59
60
61
62
# File 'lib/io/endpoint/bound_endpoint.rb', line 56

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

#to_sObject

Get a string representation of the bound endpoint.



72
73
74
# File 'lib/io/endpoint/bound_endpoint.rb', line 72

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