Class: Async::IO::Socket

Inherits:
BasicSocket show all
Includes:
ServerSocket, Socket::Constants
Defined in:
lib/async/io/socket.rb

Constant Summary

Constants inherited from Generic

Generic::WRAPPERS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ServerSocket

#accept, #accept_each

Methods inherited from Generic

#read, 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.



146
147
148
149
150
151
152
# File 'lib/async/io/socket.rb', line 146

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, &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.

  • protcol (Hash)

    a customizable set of options



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

def self.bind(local_address, protocol: 0, task: Task.current, &block)
  task.annotate "binding to #{local_address.inspect}"
  
  socket = ::Socket.new(local_address.afamily, local_address.socktype, protocol)
  
  socket.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, true)
  socket.bind(local_address.to_sockaddr)
  
  wrapper = self.new(socket, task.reactor)
  
  if block_given?
    begin
      return yield(wrapper, task)
    ensure
      wrapper.close
    end
  else
    return wrapper
  end
end

.connect(remote_address, local_address = nil, protocol: 0, task: Task.current) ⇒ 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 (Addrinfo)

    The remote address to connect to.

  • local_address (Addrinfo) (defaults to: nil)

    The local address to bind to before connecting.

  • protcol (Hash)

    a customizable set of options



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/async/io/socket.rb', line 93

def self.connect(remote_address, local_address = nil, protocol: 0, task: Task.current)
  task.annotate "connecting to #{remote_address.inspect}"
  
  socket = ::Socket.new(remote_address.afamily, remote_address.socktype, protocol)
  
  if local_address
    socket.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, true)
    socket.bind(local_address.to_sockaddr) if local_address
  end
  
  wrapper = self.new(socket, task.reactor)
  wrapper.connect(remote_address.to_sockaddr)
  
  task.annotate "connected to #{remote_address.inspect}"
  
  if block_given?
    begin
      return yield(wrapper)
    ensure
      wrapper.close
    end
  else
    return wrapper
  end
end

Instance Method Details

#connect(*args) ⇒ Object



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

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