Class: Async::IO::Endpoint

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

Direct Known Subclasses

AddressEndpoint, SecureEndpoint, SocketEndpoint

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specification, **options) ⇒ Endpoint

Returns a new instance of Endpoint.


75
76
77
# File 'lib/async/io/endpoint.rb', line 75

def initialize(specification, **options)
	super(specification, options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options


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

def options
  @options
end

#specificationObject

Returns the value of attribute specification

Returns:

  • (Object)

    the current value of specification


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

def specification
  @specification
end

Class Method Details

.each(specifications, &block) ⇒ Object

Generate a list of endpoints from an array.


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/async/io/endpoint.rb', line 54

def each(specifications, &block)
	return to_enum(:each, specifications) unless block_given?
	
	specifications.each do |specification|
		if specification.is_a? self
			yield specification
		elsif specification.is_a? Array
			yield self.send(*specification)
		elsif specification.is_a? String
			yield self.parse(specification)
		elsif specification.is_a? ::BasicSocket
			yield SocketEndpoint.new(specification)
		elsif specification.is_a? Generic
			yield Endpoint.new(specification)
		else
			raise ArgumentError.new("Not sure how to convert #{specification} to endpoint!")
		end
	end
end

.parse(string, **options) ⇒ Object


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

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

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


49
50
51
# File 'lib/async/io/endpoint.rb', line 49

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

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


37
38
39
# File 'lib/async/io/endpoint.rb', line 37

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

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


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

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

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


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

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

Instance Method Details

#accept(&block) ⇒ Object


109
110
111
112
113
114
115
116
# File 'lib/async/io/endpoint.rb', line 109

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

#addressObject


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

def address
	specification.local_address
end

#bind {|specification| ... } ⇒ Object

Yields:


105
106
107
# File 'lib/async/io/endpoint.rb', line 105

def bind
	yield specification
end

#connect {|specification| ... } ⇒ Object

Yields:


118
119
120
# File 'lib/async/io/endpoint.rb', line 118

def connect
	yield specification
end

#socket_domainObject

PF_* eg PF_INET etc, normally identical to AF_* constants.


96
97
98
# File 'lib/async/io/endpoint.rb', line 96

def socket_domain
	address.afamily
end

#socket_protocolObject

IPPROTO_TCP, IPPROTO_UDP, IPPROTO_IPX, etc.


101
102
103
# File 'lib/async/io/endpoint.rb', line 101

def socket_protocol
	address.protocol
end

#socket_typeObject

SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, etc.


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

def socket_type
	address.socktype
end

#to_sockaddrObject Also known as: to_str


83
84
85
# File 'lib/async/io/endpoint.rb', line 83

def to_sockaddr
	address.to_sockaddr
end