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) ⇒ Object
Create a new
SharedEndpoint
by binding to the given endpoint. -
.connected(endpoint) ⇒ 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
#initialize(endpoint, wrappers, **options) ⇒ SharedEndpoint
Returns a new instance of SharedEndpoint.
48 49 50 51 52 53 |
# File 'lib/async/io/shared_endpoint.rb', line 48 def initialize(endpoint, wrappers, **) super(**) @endpoint = endpoint @wrappers = wrappers end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
55 56 57 |
# File 'lib/async/io/shared_endpoint.rb', line 55 def endpoint @endpoint end |
#wrappers ⇒ Object (readonly)
Returns the value of attribute wrappers.
56 57 58 |
# File 'lib/async/io/shared_endpoint.rb', line 56 def wrappers @wrappers end |
Class Method Details
.bound(endpoint, backlog = Socket::SOMAXCONN) ⇒ Object
Create a new SharedEndpoint
by binding to the given endpoint.
28 29 30 31 32 33 34 35 36 |
# File 'lib/async/io/shared_endpoint.rb', line 28 def self.bound(endpoint, backlog = Socket::SOMAXCONN) wrappers = endpoint.bound do |server| server.listen(backlog) server.close_on_exec = false server.reactor = nil end return self.new(endpoint, wrappers) end |
.connected(endpoint) ⇒ Object
Create a new SharedEndpoint
by connecting to the given endpoint.
39 40 41 42 43 44 45 46 |
# File 'lib/async/io/shared_endpoint.rb', line 39 def self.connected(endpoint) wrapper = endpoint.connect wrapper.close_on_exec = false wrapper.reactor = nil return self.new(endpoint, [wrapper]) 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 |
#bind ⇒ Object
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 |
#close ⇒ Object
Close all the internal wrappers.
59 60 61 62 |
# File 'lib/async/io/shared_endpoint.rb', line 59 def close @wrappers.each(&:close) @wrappers.clear end |
#connect ⇒ Object
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} [#{peer.fileno}]" begin yield peer, task ensure peer.close end end end end |
#to_s ⇒ Object
106 107 108 |
# File 'lib/async/io/shared_endpoint.rb', line 106 def to_s "\#<#{self.class} #{@wrappers.size} descriptors for #{@endpoint}>" end |