Class: Async::IO::Socket

Inherits:
BasicSocket show all
Includes:
Server
Defined in:
lib/async/io/socket.rb

Constant Summary

Constants inherited from Generic

Generic::WRAPPERS

Instance Attribute Summary

Attributes inherited from Generic

#timeout

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Server

#accept_each

Methods included from Peer

#connected?, #protocol, #sync, #sync=, #type

Methods inherited from Generic

#<<, #connected?, #dup, #nonblock, #nonblock=, #nonblock?, #read, #sysread, #syswrite, #wait, wrap, wrap_blocking_method, wraps, #write

Class Method Details

.accept(*args, backlog: SOMAXCONN, &block) ⇒ Object

Bind to a local address and accept connections in a loop.



241
242
243
244
245
246
247
# File 'lib/async/io/socket.rb', line 241

def self.accept(*args, backlog: SOMAXCONN, &block)
  bind(*args) do |server, task|
    server.listen(backlog) if backlog
    
    server.accept_each(task: task, &block)
  end
end

.bind(local_address, protocol: 0, task: Task.current, **options, &block) ⇒ Object

Bind to a local address.

Examples:

socket = Async::IO::Socket.bind(Async::IO::Address.tcp("0.0.0.0", 9090))

Parameters:

  • local_address (Address)

    The local address to bind to.

  • protocol (Hash) (defaults to: 0)

    a customizable set of options

Options Hash (protocol:):

  • The (Integer)

    socket protocol to use.



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/async/io/socket.rb', line 220

def self.bind(local_address, protocol: 0, task: Task.current, **options, &block)
  Async.logger.debug(self) {"Binding to #{local_address.inspect}"}
  
  wrapper = build(local_address.afamily, local_address.socktype, protocol, **options) do |socket|
    socket.bind(local_address.to_sockaddr)
  end
  
  return wrapper unless block_given?
  
  task.async do |task|
    task.annotate "binding to #{wrapper.local_address.inspect}"
    
    begin
      yield wrapper, task
    ensure
      wrapper.close
    end
  end
end

.build(*args, timeout: nil, reuse_address: true, reuse_port: nil, linger: nil, task: Task.current) ⇒ Object

Build and wrap the underlying io.

Parameters:

  • reuse_port (Hash) (defaults to: nil)

    a customizable set of options

  • reuse_address (Hash) (defaults to: true)

    a customizable set of options

Options Hash (reuse_address:):

  • Allow (Boolean)

    this port to be bound in multiple processes.

Options Hash (reuse_port:):

  • Allow (Boolean)

    this port to be bound in multiple processes.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/async/io/socket.rb', line 150

def self.build(*args, timeout: nil, reuse_address: true, reuse_port: nil, linger: nil, task: Task.current)
  socket = wrapped_klass.new(*args)
  
  if reuse_address
    socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
  end
  
  if reuse_port
    socket.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
  end
  
  if linger
    socket.setsockopt(SOL_SOCKET, SO_LINGER, linger)
  end
  
  yield socket
  
  wrapper = self.new(socket, task.reactor)
  wrapper.timeout = timeout
  
  return wrapper
rescue Exception
  socket.close if socket
  
  raise
end

.connect(remote_address, local_address: nil, task: Task.current, **options) ⇒ Object

Establish a connection to a given remote_address.

Examples:

socket = Async::IO::Socket.connect(Async::IO::Address.tcp("8.8.8.8", 53))

Parameters:

  • remote_address (Address)

    The remote address to connect to.

  • local_address (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (local_address:):

  • The (Address)

    local address to bind to before connecting.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/async/io/socket.rb', line 182

def self.connect(remote_address, local_address: nil, task: Task.current, **options)
  Async.logger.debug(self) {"Connecting to #{remote_address.inspect}"}
  
  task.annotate "connecting to #{remote_address.inspect}"
  
  wrapper = build(remote_address.afamily, remote_address.socktype, remote_address.protocol, **options) do |socket|
    if local_address
      if defined?(IP_BIND_ADDRESS_NO_PORT)
        # Inform the kernel (Linux 4.2+) to not reserve an ephemeral port when using bind(2) with a port number of 0. The port will later be automatically chosen at connect(2) time, in a way that allows sharing a source port as long as the 4-tuple is unique.
        socket.setsockopt(SOL_IP, IP_BIND_ADDRESS_NO_PORT, 1)
      end
      
      socket.bind(local_address.to_sockaddr)
    end
  end
  
  begin
    wrapper.connect(remote_address.to_sockaddr)
    task.annotate "connected to #{remote_address.inspect}"
  rescue Exception
    wrapper.close
    raise
  end
  
  return wrapper unless block_given?
  
  begin
    yield wrapper, task
  ensure
    wrapper.close
  end
end

.pair(*args) ⇒ Object



251
252
253
# File 'lib/async/io/socket.rb', line 251

def self.pair(*args)
  ::Socket.pair(*args).map(&self.method(:new))
end

Instance Method Details

#accept(timeout: nil, task: Task.current) ⇒ Object

Parameters:

  • duration (Numeric)

    the maximum time to wait for accepting a connection, if specified.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/async/io/socket.rb', line 125

def accept(timeout: nil, task: Task.current)
  peer, address = async_send(:accept_nonblock, timeout: timeout)
  wrapper = Socket.new(peer, task.reactor)
  
  wrapper.timeout = self.timeout
  
  return wrapper, address unless block_given?
  
  task.async do |task|
    task.annotate "incoming connection #{address.inspect}"
    
    begin
      yield wrapper, address
    ensure
      wrapper.close
    end
  end
end

#accept_nonblockObject

Parameters:

  • duration (Numeric)

    the maximum time to wait for accepting a connection, if specified.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/async/io/socket.rb', line 144

def accept(timeout: nil, task: Task.current)
  peer, address = async_send(:accept_nonblock, timeout: timeout)
  wrapper = Socket.new(peer, task.reactor)
  
  wrapper.timeout = self.timeout
  
  return wrapper, address unless block_given?
  
  task.async do |task|
    task.annotate "incoming connection #{address.inspect}"
    
    begin
      yield wrapper, address
    ensure
      wrapper.close
    end
  end
end

#connect(*args) ⇒ Object

Raises:

  • Errno::EAGAIN the connection failed due to the remote end being overloaded.



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

def connect(*args)
  begin
    async_send(:connect_nonblock, *args)
  rescue Errno::EISCONN
    # We are now connected.
  end
end

#connect_nonblockObject

Raises:

  • Errno::EAGAIN the connection failed due to the remote end being overloaded.



122
123
124
125
126
127
128
# File 'lib/async/io/socket.rb', line 122

def connect(*args)
  begin
    async_send(:connect_nonblock, *args)
  rescue Errno::EISCONN
    # We are now connected.
  end
end

#sysacceptObject

Parameters:

  • duration (Numeric)

    the maximum time to wait for accepting a connection, if specified.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/async/io/socket.rb', line 145

def accept(timeout: nil, task: Task.current)
  peer, address = async_send(:accept_nonblock, timeout: timeout)
  wrapper = Socket.new(peer, task.reactor)
  
  wrapper.timeout = self.timeout
  
  return wrapper, address unless block_given?
  
  task.async do |task|
    task.annotate "incoming connection #{address.inspect}"
    
    begin
      yield wrapper, address
    ensure
      wrapper.close
    end
  end
end