Class: Async::IO::Endpoint

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specification, **options) ⇒ Endpoint

Returns a new instance of Endpoint.



63
64
65
# File 'lib/async/io/endpoint.rb', line 63

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



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/async/io/endpoint.rb', line 50

def each(specifications, &block)
  specifications.each do |specification|
    if specification.is_a? self
      yield specification
    elsif specification.is_a? Array
      yield self.send(*specification)
    else
      yield self.new(specification)
    end
  end
end

.parse(string, **options) ⇒ Object



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

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

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



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

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

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



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

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

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



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

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

Instance Method Details

#accept(&block) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/async/io/endpoint.rb', line 113

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

#addressObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/async/io/endpoint.rb', line 74

def address
  @address ||= case specification
    when Addrinfo
      specification
    when ::BasicSocket, BasicSocket
      specification.local_address
  else
    raise ArgumentError, "Not sure how to convert #{specification} into address!"
  end
end

#bind(&block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/async/io/endpoint.rb', line 100

def bind(&block)
  case specification
  when Addrinfo
    Socket.bind(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



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/async/io/endpoint.rb', line 122

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

#socket_domainObject

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



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

def socket_domain
  address.afamily
end

#socket_protocolObject

IPPROTO_TCP, IPPROTO_UDP, IPPROTO_IPX, etc.



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

def socket_protocol
  address.protocol
end

#socket_typeObject

SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, etc.



86
87
88
# File 'lib/async/io/endpoint.rb', line 86

def socket_type
  address.socktype
end

#to_sockaddrObject Also known as: to_str



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

def to_sockaddr
  address.to_sockaddr
end