Class: IO::Endpoint::BoundEndpoint

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

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

Returns a new instance of BoundEndpoint.



22
23
24
25
26
27
# File 'lib/io/endpoint/bound_endpoint.rb', line 22

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

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



29
30
31
# File 'lib/io/endpoint/bound_endpoint.rb', line 29

def endpoint
  @endpoint
end

#socketsObject (readonly)

Returns the value of attribute sockets.



30
31
32
# File 'lib/io/endpoint/bound_endpoint.rb', line 30

def sockets
  @sockets
end

Class Method Details

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



12
13
14
15
16
17
18
19
20
# File 'lib/io/endpoint/bound_endpoint.rb', line 12

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



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/io/endpoint/bound_endpoint.rb', line 65

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



52
53
54
55
# File 'lib/io/endpoint/bound_endpoint.rb', line 52

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

#inspectObject



61
62
63
# File 'lib/io/endpoint/bound_endpoint.rb', line 61

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.



34
35
36
37
38
39
40
# File 'lib/io/endpoint/bound_endpoint.rb', line 34

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.



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

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



57
58
59
# File 'lib/io/endpoint/bound_endpoint.rb', line 57

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