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, socket, ssl, tcp, try_convert, udp, unix

Constructor Details

#initialize(endpoint, wrappers) ⇒ SharedEndpoint

Returns a new instance of SharedEndpoint.



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

def initialize(endpoint, wrappers)
	@endpoint = endpoint
	@wrappers = wrappers
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



57
58
59
# File 'lib/async/io/shared_endpoint.rb', line 57

def endpoint
  @endpoint
end

#wrappersObject (readonly)

Returns the value of attribute wrappers.



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

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



100
101
102
103
104
# File 'lib/async/io/shared_endpoint.rb', line 100

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

#bindObject



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

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



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

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

#connectObject



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

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



106
107
108
# File 'lib/async/io/shared_endpoint.rb', line 106

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