Class: Async::IO::SharedEndpoint

Inherits:
Endpoint
  • Object
show all
Defined in:
lib/async/io/shared_endpoint.rb

Instance Attribute Summary collapse

Attributes inherited from Endpoint

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Endpoint

#each, each, #hostname, parse, #reuse_port, socket, ssl, tcp, #timeout, try_convert, udp, unix

Constructor Details

#initialize(endpoint, wrappers, **options) ⇒ SharedEndpoint

Returns a new instance of SharedEndpoint.



52
53
54
55
56
57
# File 'lib/async/io/shared_endpoint.rb', line 52

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

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



59
60
61
# File 'lib/async/io/shared_endpoint.rb', line 59

def endpoint
  @endpoint
end

#wrappersObject (readonly)

Returns the value of attribute wrappers.



60
61
62
# File 'lib/async/io/shared_endpoint.rb', line 60

def wrappers
  @wrappers
end

Class Method Details

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/async/io/shared_endpoint.rb', line 26

def self.bound(endpoint, backlog = Socket::SOMAXCONN)
	wrappers = []
	
	endpoint.each do |endpoint|
		server = endpoint.bind
		
		server.listen(backlog)
		
		server.close_on_exec = false
		server.reactor = nil
		
		wrappers << server
	end
	
	self.new(endpoint, wrappers)
end

.connected(endpoint) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/async/io/shared_endpoint.rb', line 43

def self.connected(endpoint)
	peer = endpoint.connect
	
	peer.close_on_exec = false
	peer.reactor = nil
	
	self.new(endpoint, [peer])
end

Instance Method Details

#accept(backlog = nil, &block) ⇒ Object



102
103
104
105
106
# File 'lib/async/io/shared_endpoint.rb', line 102

def accept(backlog = nil, &block)
	bind do |server|
		server.accept_each(&block)
	end
end

#bindObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/async/io/shared_endpoint.rb', line 66

def bind
	task = Async::Task.current
	
	@wrappers.each do |server|
		server = server.dup
		
		task.async do |task|
			task.annotate "binding to #{server.inspect}"
			
			begin
				yield server, task
			ensure
				server.close
			end
		end
	end
end

#closeObject



62
63
64
# File 'lib/async/io/shared_endpoint.rb', line 62

def close
	@wrappers.each(&:close)
end

#connectObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/async/io/shared_endpoint.rb', line 84

def connect
	task = Async::Task.current
	
	@wrappers.each do |peer|
		peer = peer.dup
		
		task.async do |task|
			task.annotate "connected to #{peer.inspect}"
			
			begin
				yield peer, task
			ensure
				peer.close
			end
		end
	end
end

#to_sObject



108
109
110
# File 'lib/async/io/shared_endpoint.rb', line 108

def to_s
	"\#<#{self.class} #{@wrappers.count} descriptors for #{@endpoint}>"
end