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.
- #to_s ⇒ Object
Methods inherited from Endpoint
#bound, each, #each, #hostname, #linger, #local_address, parse, #reuse_address, #reuse_port, socket, ssl, tcp, #timeout, try_convert, udp, unix, #with
Constructor Details
permalink #initialize(endpoint, wrappers, **options) ⇒ SharedEndpoint
Returns a new instance of SharedEndpoint.
56 57 58 59 60 61 |
# File 'lib/async/io/shared_endpoint.rb', line 56 def initialize(endpoint, wrappers, **) super(**) @endpoint = endpoint @wrappers = wrappers end |
Instance Attribute Details
permalink #endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
63 64 65 |
# File 'lib/async/io/shared_endpoint.rb', line 63 def endpoint @endpoint end |
permalink #wrappers ⇒ Object (readonly)
Returns the value of attribute wrappers.
64 65 66 |
# File 'lib/async/io/shared_endpoint.rb', line 64 def wrappers @wrappers end |
Class Method Details
permalink .bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false) ⇒ Object
Create a new ‘SharedEndpoint` by binding to the given endpoint.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/async/io/shared_endpoint.rb', line 30 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 |
permalink .connected(endpoint, close_on_exec: false) ⇒ Object
Create a new ‘SharedEndpoint` by connecting to the given endpoint.
47 48 49 50 51 52 53 54 |
# File 'lib/async/io/shared_endpoint.rb', line 47 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
permalink #accept(backlog = nil, &block) ⇒ Object
[View source]
108 109 110 111 112 |
# File 'lib/async/io/shared_endpoint.rb', line 108 def accept(backlog = nil, &block) bind do |server| server.accept_each(&block) end end |
permalink #bind ⇒ Object
[View source]
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/async/io/shared_endpoint.rb', line 72 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 |
permalink #close ⇒ Object
Close all the internal wrappers.
67 68 69 70 |
# File 'lib/async/io/shared_endpoint.rb', line 67 def close @wrappers.each(&:close) @wrappers.clear end |
permalink #connect ⇒ Object
[View source]
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/async/io/shared_endpoint.rb', line 90 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 |
permalink #to_s ⇒ Object
[View source]
114 115 116 |
# File 'lib/async/io/shared_endpoint.rb', line 114 def to_s "\#<#{self.class} #{@wrappers.size} descriptors for #{@endpoint}>" end |