Class: Async::IO::Endpoint
- Inherits:
-
Object
- Object
- Async::IO::Endpoint
- Defined in:
- lib/async/io/endpoint.rb,
lib/async/io/ssl_endpoint.rb,
lib/async/io/endpoint/each.rb,
lib/async/io/host_endpoint.rb,
lib/async/io/unix_endpoint.rb,
lib/async/io/shared_endpoint.rb,
lib/async/io/socket_endpoint.rb,
lib/async/io/composite_endpoint.rb
Overview
Endpoints represent a way of connecting or binding to an address.
Direct Known Subclasses
AddressEndpoint, CompositeEndpoint, HostEndpoint, SSLEndpoint, SharedEndpoint, SocketEndpoint
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
- .composite(*endpoints, **options) ⇒ Object
-
.each(specifications, &block) ⇒ Object
Generate a list of endpoints from an array.
-
.parse(string, **options) ⇒ Object
Create an Endpoint instance by URI scheme.
- .socket(socket, **options) ⇒ Object
- .ssl(*args, ssl_context: nil, hostname: nil, **options) ⇒ SSLEndpoint
- .tcp(*args, **options) ⇒ HostEndpoint
- .try_convert(specification) ⇒ Object
- .udp(*args, **options) ⇒ HostEndpoint
- .unix(path = "", type = ::Socket::SOCK_STREAM, **options) ⇒ UNIXEndpoint
Instance Method Summary collapse
-
#accept(backlog = Socket::SOMAXCONN, &block) ⇒ Object
Accept connections from the specified endpoint.
- #bound(**options) ⇒ Object
- #connected(**options) ⇒ Object
-
#each {|Endpoint| ... } ⇒ Object
Endpoints sometimes have multiple paths.
-
#hostname ⇒ String
The hostname of the bound socket.
-
#initialize(**options) ⇒ Endpoint
constructor
A new instance of Endpoint.
-
#linger ⇒ Integer?
Controls SO_LINGER.
-
#local_address ⇒ Address
The address to bind to before connecting.
-
#reuse_address ⇒ Boolean
If ‘SO_REUSEADDR` is enabled on a socket prior to binding it, the socket can be successfully bound unless there is a conflict with another socket bound to exactly the same combination of source address and port.
-
#reuse_port ⇒ Boolean?
If ‘SO_REUSEPORT` is enabled on a socket, the socket can be successfully bound even if there are existing sockets bound to the same address, as long as all prior bound sockets also had `SO_REUSEPORT` set before they were bound.
-
#timeout ⇒ Numeric
The default timeout for socket operations.
- #with(**options) ⇒ Object
Constructor Details
#initialize(**options) ⇒ Endpoint
Returns a new instance of Endpoint.
16 17 18 |
# File 'lib/async/io/endpoint.rb', line 16 def initialize(**) @options = .freeze end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
28 29 30 |
# File 'lib/async/io/endpoint.rb', line 28 def @options end |
Class Method Details
.composite(*endpoints, **options) ⇒ Object
39 40 41 |
# File 'lib/async/io/composite_endpoint.rb', line 39 def self.composite(*endpoints, **) CompositeEndpoint.new(endpoints, **) end |
.each(specifications, &block) ⇒ Object
Generate a list of endpoints from an array.
31 32 33 34 35 36 37 |
# File 'lib/async/io/endpoint/each.rb', line 31 def self.each(specifications, &block) return to_enum(:each, specifications) unless block_given? specifications.each do |specification| yield try_convert(specification) end end |
.parse(string, **options) ⇒ Object
Create an Endpoint instance by URI scheme. The host and port of the URI will be passed to the Endpoint factory method, along with any options.
90 91 92 93 94 |
# File 'lib/async/io/endpoint.rb', line 90 def self.parse(string, **) uri = URI.parse(string) self.public_send(uri.scheme, uri.host, uri.port, **) end |
.socket(socket, **options) ⇒ Object
51 52 53 |
# File 'lib/async/io/socket_endpoint.rb', line 51 def self.socket(socket, **) SocketEndpoint.new(socket, **) end |
.ssl(*args, ssl_context: nil, hostname: nil, **options) ⇒ SSLEndpoint
100 101 102 |
# File 'lib/async/io/ssl_endpoint.rb', line 100 def self.ssl(*args, ssl_context: nil, hostname: nil, **) SSLEndpoint.new(self.tcp(*args, **), ssl_context: ssl_context, hostname: hostname) end |
.tcp(*args, **options) ⇒ HostEndpoint
85 86 87 88 89 |
# File 'lib/async/io/host_endpoint.rb', line 85 def self.tcp(*args, **) args[3] = ::Socket::SOCK_STREAM HostEndpoint.new(args, **) end |
.try_convert(specification) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/async/io/endpoint/each.rb', line 14 def self.try_convert(specification) if specification.is_a? self specification elsif specification.is_a? Array self.send(*specification) elsif specification.is_a? String self.parse(specification) elsif specification.is_a? ::BasicSocket self.socket(specification) elsif specification.is_a? Generic self.new(specification) else raise ArgumentError.new("Not sure how to convert #{specification} to endpoint!") end end |
.udp(*args, **options) ⇒ HostEndpoint
95 96 97 98 99 |
# File 'lib/async/io/host_endpoint.rb', line 95 def self.udp(*args, **) args[3] = ::Socket::SOCK_DGRAM HostEndpoint.new(args, **) end |
.unix(path = "", type = ::Socket::SOCK_STREAM, **options) ⇒ UNIXEndpoint
54 55 56 |
# File 'lib/async/io/unix_endpoint.rb', line 54 def self.unix(path = "", type = ::Socket::SOCK_STREAM, **) UNIXEndpoint.new(path, type, **) end |
Instance Method Details
#accept(backlog = Socket::SOMAXCONN, &block) ⇒ Object
Accept connections from the specified endpoint.
73 74 75 76 77 78 79 |
# File 'lib/async/io/endpoint.rb', line 73 def accept(backlog = Socket::SOMAXCONN, &block) bind do |server| server.listen(backlog) server.accept_each(&block) end end |
#bound(**options) ⇒ Object
117 118 119 |
# File 'lib/async/io/shared_endpoint.rb', line 117 def bound(**) SharedEndpoint.bound(self, **) end |
#connected(**options) ⇒ Object
121 122 123 |
# File 'lib/async/io/shared_endpoint.rb', line 121 def connected(**) SharedEndpoint.connected(self, **) end |
#each {|Endpoint| ... } ⇒ Object
Endpoints sometimes have multiple paths.
65 66 67 68 69 |
# File 'lib/async/io/endpoint.rb', line 65 def each return to_enum unless block_given? yield self end |
#hostname ⇒ String
Returns The hostname of the bound socket.
31 32 33 |
# File 'lib/async/io/endpoint.rb', line 31 def hostname @options[:hostname] end |
#linger ⇒ Integer?
Controls SO_LINGER. The amount of time the socket will stay in the ‘TIME_WAIT` state after being closed.
49 50 51 |
# File 'lib/async/io/endpoint.rb', line 49 def linger @options[:linger] end |
#local_address ⇒ Address
Returns the address to bind to before connecting.
59 60 61 |
# File 'lib/async/io/endpoint.rb', line 59 def local_address @options[:local_address] end |
#reuse_address ⇒ Boolean
If ‘SO_REUSEADDR` is enabled on a socket prior to binding it, the socket can be successfully bound unless there is a conflict with another socket bound to exactly the same combination of source address and port. Additionally, when set, binding a socket to the address of an existing socket in `TIME_WAIT` is not an error.
43 44 45 |
# File 'lib/async/io/endpoint.rb', line 43 def reuse_address @options[:reuse_address] end |
#reuse_port ⇒ Boolean?
If ‘SO_REUSEPORT` is enabled on a socket, the socket can be successfully bound even if there are existing sockets bound to the same address, as long as all prior bound sockets also had `SO_REUSEPORT` set before they were bound.
37 38 39 |
# File 'lib/async/io/endpoint.rb', line 37 def reuse_port @options[:reuse_port] end |
#timeout ⇒ Numeric
Returns The default timeout for socket operations.
54 55 56 |
# File 'lib/async/io/endpoint.rb', line 54 def timeout @options[:timeout] end |
#with(**options) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/async/io/endpoint.rb', line 20 def with(**) dup = self.dup dup. = @options.merge() return dup end |