Class: Async::IO::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/async/io/endpoint.rb,
lib/async/io/ssl_endpoint.rb,
lib/async/io/endpoint/each.rb,
lib/async/io/host_endpoint.rb,
lib/async/io/socket_endpoint.rb,
lib/async/io/address_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) ⇒ Endpoint

Returns a new instance of Endpoint.



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

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.each(specifications, &block) ⇒ Object

Generate a list of endpoint from an array.



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

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



54
55
56
57
58
# File 'lib/async/io/endpoint.rb', line 54

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

.socket(socket, **options) ⇒ Object



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

def self.socket(socket, **options)
  SocketEndpoint.new(socket, **options)
end

.ssl(*args, ssl_context: nil, hostname: nil, **options) ⇒ Object



102
103
104
# File 'lib/async/io/ssl_endpoint.rb', line 102

def self.ssl(*args, ssl_context: nil, hostname: nil, **options)
  SSLEndpoint.new(self.tcp(*args, **options), ssl_context: ssl_context, hostname: hostname)
end

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

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



79
80
81
82
83
# File 'lib/async/io/host_endpoint.rb', line 79

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

.try_convert(specification) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/async/io/endpoint/each.rb', line 28

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

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



85
86
87
88
89
# File 'lib/async/io/host_endpoint.rb', line 85

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

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



56
57
58
# File 'lib/async/io/address_endpoint.rb', line 56

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

Instance Method Details

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



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

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:



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

def each
  return to_enum unless block_given?
  
  yield self
end

#hostnameObject



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

def hostname
  @options[:hostname]
end