Class: Async::IO::Address

Inherits:
Struct
  • Object
show all
Includes:
Comparable, Socket::Constants
Defined in:
lib/async/io/address.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specification, **options) ⇒ Address

Returns a new instance of Address.



54
55
56
# File 'lib/async/io/address.rb', line 54

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



25
26
27
# File 'lib/async/io/address.rb', line 25

def options
  @options
end

#specificationObject

Returns the value of attribute specification

Returns:

  • (Object)

    the current value of specification



25
26
27
# File 'lib/async/io/address.rb', line 25

def specification
  @specification
end

Class Method Details

.each(specifications, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/async/io/address.rb', line 42

def each(specifications, &block)
	specifications.each do |specification|
		if specification.is_a? self
			yield self
		else
			# Perhaps detect options here?
			yield self.new(specification)
		end
	end
end

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



30
31
32
# File 'lib/async/io/address.rb', line 30

def tcp(*args, **options)
	self.new([:tcp, *args], **options)
end

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



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

def udp(*args, **options)
	self.new([:udp, *args], **options)
end

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



38
39
40
# File 'lib/async/io/address.rb', line 38

def unix(*args, **options)
	self.new([:unix, *args], **options)
end

Instance Method Details

#<=>(other) ⇒ Object



62
63
64
# File 'lib/async/io/address.rb', line 62

def <=> other
	self.to_sockaddr <=> other.to_sockaddr
end

#==(other) ⇒ Object



58
59
60
# File 'lib/async/io/address.rb', line 58

def == other
	self.to_sockaddr == other.to_sockaddr
end

#accept(&block) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/async/io/address.rb', line 104

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

#afamilyObject Also known as: family



80
81
82
# File 'lib/async/io/address.rb', line 80

def afamily
	addrinfo.afamily
end

#bind(&block) ⇒ Object

def connect? accept? DatagramHandler StreamHandler



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/async/io/address.rb', line 89

def bind(&block)
	case specification
	when Addrinfo
		Socket.bind(specification, **options, &block)
	when Array
		Socket.bind(Addrinfo.send(*specification), **options, &block)
	when ::BasicSocket
		yield Socket.new(specification)
	when BasicSocket
		yield specification
	else
		raise ArgumentError, "Not sure how to bind to #{specification}!"
	end
end

#connect(&block) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/async/io/address.rb', line 113

def connect(&block)
	case specification
	when Addrinfo, Array
		Socket.connect(self, &block)
	when ::BasicSocket
		yield Async::IO.try_convert(specification)
	when BasicSocket
		yield specification
	else
		raise ArgumentError, "Not sure how to bind to #{specification}!"
	end
end

#socktypeObject Also known as: type



73
74
75
# File 'lib/async/io/address.rb', line 73

def socktype
	addrinfo.socktype
end

#to_sockaddrObject Also known as: to_str



66
67
68
# File 'lib/async/io/address.rb', line 66

def to_sockaddr
	addrinfo.to_sockaddr
end