Class: Async::IO::SharedEndpoint
- Defined in:
- lib/async/io/shared_endpoint.rb
Overview
Pre-connect and pre-bind sockets so that it can be used between processes.
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#wrappers ⇒ Object
readonly
Returns the value of attribute wrappers.
Attributes inherited from Endpoint
Class Method Summary collapse
-
.bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false) ⇒ Object
Create a new ‘SharedEndpoint` by binding to the given endpoint.
-
.connected(endpoint, close_on_exec: false) ⇒ Object
Create a new ‘SharedEndpoint` by connecting to the given endpoint.
Instance Method Summary collapse
- #accept(backlog = nil, &block) ⇒ Object
- #bind ⇒ Object
-
#close ⇒ Object
Close all the internal wrappers.
- #connect ⇒ Object
-
#initialize(endpoint, wrappers, **options) ⇒ SharedEndpoint
constructor
A new instance of SharedEndpoint.
- #local_address_endpoint(**options) ⇒ Object
- #remote_address_endpoint(**options) ⇒ Object
- #to_s ⇒ Object
Methods inherited from Endpoint
#bound, composite, #each, each, #hostname, #linger, #local_address, parse, #reuse_address, #reuse_port, socket, ssl, tcp, #timeout, try_convert, udp, unix, #with
Constructor Details
#initialize(endpoint, wrappers, **options) ⇒ SharedEndpoint
Returns a new instance of SharedEndpoint.
57 58 59 60 61 62 |
# File 'lib/async/io/shared_endpoint.rb', line 57 def initialize(endpoint, wrappers, **) super(**) @endpoint = endpoint @wrappers = wrappers end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
64 65 66 |
# File 'lib/async/io/shared_endpoint.rb', line 64 def endpoint @endpoint end |
#wrappers ⇒ Object (readonly)
Returns the value of attribute wrappers.
65 66 67 |
# File 'lib/async/io/shared_endpoint.rb', line 65 def wrappers @wrappers end |
Class Method Details
.bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false) ⇒ Object
Create a new ‘SharedEndpoint` by binding to the given endpoint.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/async/io/shared_endpoint.rb', line 31 def self.bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false) wrappers = endpoint.bound do |server| # This is somewhat optional. We want to have a generic interface as much as possible so that users of this interface can just call it without knowing a lot of internal details. Therefore, we ignore errors here if it's because the underlying socket does not support the operation. begin server.listen(backlog) rescue Errno::EOPNOTSUPP # Ignore. end server.close_on_exec = close_on_exec server.reactor = nil end return self.new(endpoint, wrappers) end |
.connected(endpoint, close_on_exec: false) ⇒ Object
Create a new ‘SharedEndpoint` by connecting to the given endpoint.
48 49 50 51 52 53 54 55 |
# File 'lib/async/io/shared_endpoint.rb', line 48 def self.connected(endpoint, close_on_exec: false) wrapper = endpoint.connect wrapper.close_on_exec = close_on_exec wrapper.reactor = nil return self.new(endpoint, [wrapper]) end |
Instance Method Details
#accept(backlog = nil, &block) ⇒ Object
125 126 127 128 129 |
# File 'lib/async/io/shared_endpoint.rb', line 125 def accept(backlog = nil, &block) bind do |server| server.accept_each(&block) end end |
#bind ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/async/io/shared_endpoint.rb', line 89 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 |
#close ⇒ Object
Close all the internal wrappers.
84 85 86 87 |
# File 'lib/async/io/shared_endpoint.rb', line 84 def close @wrappers.each(&:close) @wrappers.clear end |
#connect ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/async/io/shared_endpoint.rb', line 107 def connect task = Async::Task.current @wrappers.each do |peer| peer = peer.dup task.async do |task| task.annotate "connected to #{peer.inspect} [#{peer.fileno}]" begin yield peer, task ensure peer.close end end end end |
#local_address_endpoint(**options) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/async/io/shared_endpoint.rb', line 67 def local_address_endpoint(**) endpoints = @wrappers.map do |wrapper| AddressEndpoint.new(wrapper.to_io.local_address) end return CompositeEndpoint.new(endpoints, **) end |
#remote_address_endpoint(**options) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/async/io/shared_endpoint.rb', line 75 def remote_address_endpoint(**) endpoints = @wrappers.map do |wrapper| AddressEndpoint.new(wrapper.to_io.remote_address) end return CompositeEndpoint.new(endpoints, **) end |
#to_s ⇒ Object
131 132 133 |
# File 'lib/async/io/shared_endpoint.rb', line 131 def to_s "\#<#{self.class} #{@wrappers.size} descriptors for #{@endpoint}>" end |