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

Returns a new instance of Generic.



15
16
17
# File 'lib/io/endpoint/generic.rb', line 15

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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



27
28
29
# File 'lib/io/endpoint/generic.rb', line 27

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.



107
108
109
110
111
# File 'lib/io/endpoint/generic.rb', line 107

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.



81
82
83
84
85
# File 'lib/io/endpoint/generic.rb', line 81

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)


67
68
69
# File 'lib/io/endpoint/generic.rb', line 67

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

#bound(**options) ⇒ Object



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

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

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

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

Returns:

  • (Socket)

    the connected socket

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/io/endpoint/generic.rb', line 74

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

#connected(**options) ⇒ Object



69
70
71
# File 'lib/io/endpoint/connected_endpoint.rb', line 69

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

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

Enumerate all discrete paths as endpoints.

Yields:

  • (_self)

Yield Parameters:



90
91
92
93
94
# File 'lib/io/endpoint/generic.rb', line 90

def each
	return to_enum unless block_given?
	
	yield self
end

#hostnameString

Returns The hostname of the bound socket.

Returns:

  • (String)

    The hostname of the bound socket.



30
31
32
# File 'lib/io/endpoint/generic.rb', line 30

def hostname
	@options[:hostname]
end

#lingerInteger?

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

Returns:

  • (Integer, nil)

    The value for SO_LINGER.



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

def linger
	@options[:linger]
end

#local_addressAddress

Returns the address to bind to before connecting.

Returns:

  • (Address)

    the address to bind to before connecting.



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

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.

Returns:

  • (Boolean)

    The value for ‘SO_REUSEADDR`.



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

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.

Returns:

  • (Boolean, nil)

    The value for ‘SO_REUSEPORT`.



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

def reuse_port?
	@options[:reuse_port]
end

#timeoutNumeric

Returns The default timeout for socket operations.

Returns:

  • (Numeric)

    The default timeout for socket operations.



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

def timeout
	@options[:timeout]
end

#with(**options) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/io/endpoint/generic.rb', line 19

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.



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

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