Class: IO::Endpoint::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/io/endpoint/generic.rb,
lib/io/endpoint/bound_endpoint.rb,
lib/io/endpoint/connected_endpoint.rb

Overview

Endpoints represent a way of connecting or binding to an address.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Generic

Initialize a new generic endpoint.



17
18
19
# File 'lib/io/endpoint/generic.rb', line 17

def initialize(**options)
  @options = options.freeze
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



32
33
34
# File 'lib/io/endpoint/generic.rb', line 32

def options
  @options
end

Class Method Details

.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.

You should not use untrusted input as it may execute arbitrary code.



113
114
115
116
117
# File 'lib/io/endpoint/generic.rb', line 113

def self.parse(string, **options)
  uri = URI.parse(string)
  
  IO::Endpoint.public_send(uri.scheme, uri.host, uri.port, **options)
end

Instance Method Details

#accept(wrapper = self.wrapper, &block) ⇒ Object

Bind and accept connections on the given address.



87
88
89
90
91
# File 'lib/io/endpoint/generic.rb', line 87

def accept(wrapper = self.wrapper, &block)
  bind(wrapper) do |server|
    wrapper.accept(server, **@options, &block)
  end
end

#bind(wrapper = self.wrapper, &block) ⇒ Object

Bind a socket to the given address. If a block is given, the socket will be automatically closed when the block exits.

Raises:

  • (NotImplementedError)


72
73
74
# File 'lib/io/endpoint/generic.rb', line 72

def bind(wrapper = self.wrapper, &block)
  raise NotImplementedError
end

#bound(**options) ⇒ Object

Create a bound endpoint from this endpoint.



104
105
106
# File 'lib/io/endpoint/bound_endpoint.rb', line 104

def bound(**options)
  BoundEndpoint.bound(self, **options)
end

#connect(wrapper = self.wrapper, &block) ⇒ Object

Connects a socket to the given address. If a block is given, the socket will be automatically closed when the block exits.

Raises:

  • (NotImplementedError)


79
80
81
# File 'lib/io/endpoint/generic.rb', line 79

def connect(wrapper = self.wrapper, &block)
  raise NotImplementedError
end

#connected(**options) ⇒ Object

Create a connected endpoint from this endpoint.



93
94
95
# File 'lib/io/endpoint/connected_endpoint.rb', line 93

def connected(**options)
  ConnectedEndpoint.connected(self, **options)
end

#each {|_self| ... } ⇒ Object

Enumerate all discrete paths as endpoints.

Yields:

  • (_self)

Yield Parameters:



96
97
98
99
100
# File 'lib/io/endpoint/generic.rb', line 96

def each
  return to_enum unless block_given?
  
  yield self
end

#hostnameObject



35
36
37
# File 'lib/io/endpoint/generic.rb', line 35

def hostname
  @options[:hostname]
end

#lingerObject

Controls SO_LINGER. The amount of time the socket will stay in the ‘TIME_WAIT` state after being closed.



53
54
55
# File 'lib/io/endpoint/generic.rb', line 53

def linger
  @options[:linger]
end

#local_addressObject



63
64
65
# File 'lib/io/endpoint/generic.rb', line 63

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.



47
48
49
# File 'lib/io/endpoint/generic.rb', line 47

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.



41
42
43
# File 'lib/io/endpoint/generic.rb', line 41

def reuse_port?
  @options[:reuse_port]
end

#timeoutObject



58
59
60
# File 'lib/io/endpoint/generic.rb', line 58

def timeout
  @options[:timeout]
end

#with(**options) ⇒ Object

Create a new endpoint with merged options.



24
25
26
27
28
29
30
# File 'lib/io/endpoint/generic.rb', line 24

def with(**options)
  dup = self.dup
  
  dup.options = @options.merge(options)
  
  return dup
end

#wrapperObject

The default wrapper to use for binding, connecting, and accepting connections.



120
121
122
# File 'lib/io/endpoint/generic.rb', line 120

def wrapper
  @options[:wrapper] || Wrapper.default
end