Class: Async::IO::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/async/io/endpoint.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Endpoint

Returns a new instance of Endpoint.



28
29
30
# File 'lib/async/io/endpoint.rb', line 28

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.each(specifications, &block) ⇒ Object

Generate a list of endpoint from an array.



82
83
84
85
86
87
88
# File 'lib/async/io/endpoint.rb', line 82

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



38
39
40
41
42
# File 'lib/async/io/endpoint.rb', line 38

def self.parse(string, **options)
	uri = URI.parse(string)
	
	self.send(uri.scheme, uri.host, uri.port, **options)
end

.ssl(*args, **options) ⇒ Object



61
62
63
# File 'lib/async/io/endpoint.rb', line 61

def self.ssl(*args, **options)
	SecureEndpoint.new(self.tcp(*args, **options), **options)
end

.tcp(*args, **options) ⇒ Object

args: nodename, service, family, socktype, protocol, flags



45
46
47
48
49
# File 'lib/async/io/endpoint.rb', line 45

def self.tcp(*args, **options)
	args[3] = ::Socket::SOCK_STREAM
	
	HostEndpoint.new(args, **options)
end

.try_convert(specification) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/async/io/endpoint.rb', line 65

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
		SocketEndpoint.new(specification)
	elsif specification.is_a? Generic
		Endpoint.new(specification)
	else
		raise ArgumentError.new("Not sure how to convert #{specification} to endpoint!")
	end
end

.udp(*args, **options) ⇒ Object



51
52
53
54
55
# File 'lib/async/io/endpoint.rb', line 51

def self.udp(*args, **options)
	args[3] = ::Socket::SOCK_DGRAM
	
	HostEndpoint.new(args, **options)
end

.unix(*args, **options) ⇒ Object



57
58
59
# File 'lib/async/io/endpoint.rb', line 57

def self.unix(*args, **options)
	AddressEndpoint.new(Address.unix(*args), **options)
end

Instance Method Details

#accept(backlog = Socket::SOMAXCONN, &block) ⇒ Object



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

def accept(backlog = Socket::SOMAXCONN, &block)
	bind do |server|
		server.listen(backlog)
		
		server.accept_each(&block)
	end
end

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

Yields:

  • (_self)

Yield Parameters:



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

def each
	return to_enum unless block_given?
	
	yield self
end

#hostnameObject



34
35
36
# File 'lib/async/io/endpoint.rb', line 34

def hostname
	@options[:hostname]
end