Class: Async::IO::Socket
- Inherits:
-
BasicSocket
- Object
- Wrapper
- Generic
- BasicSocket
- Async::IO::Socket
- Includes:
- ServerSocket, Socket::Constants
- Defined in:
- lib/async/io/socket.rb
Constant Summary
Constants inherited from Generic
Class Method Summary collapse
-
.accept(*args, backlog: SOMAXCONN, &block) ⇒ Object
Bind to a local address and accept connections in a loop.
-
.bind(local_address, protocol: 0, reuse_port: false, task: Task.current, &block) ⇒ Object
Bind to a local address.
- .build(*args, task: Task.current) ⇒ Object
-
.connect(remote_address, local_address = nil, task: Task.current) ⇒ Object
Establish a connection to a given ‘remote_address`.
Instance Method Summary collapse
Methods included from ServerSocket
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.
164 165 166 167 168 169 170 |
# File 'lib/async/io/socket.rb', line 164 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, reuse_port: false, task: Task.current, &block) ⇒ Object
Bind to a local address.
143 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 143 def self.bind(local_address, protocol: 0, reuse_port: false, task: Task.current, &block) task.annotate "binding to #{local_address.inspect}" wrapper = build(local_address.afamily, local_address.socktype, protocol) do |socket| socket.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, true) socket.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEPORT, true) if reuse_port socket.bind(local_address.to_sockaddr) end if block_given? begin yield wrapper, task ensure wrapper.close end else return wrapper end end |
.build(*args, task: Task.current) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/async/io/socket.rb', line 87 def self.build(*args, task: Task.current) socket = ::Socket.new(*args) yield socket return self.new(socket, task.reactor) rescue Exception socket.close if socket raise end |
.connect(remote_address, local_address = nil, task: Task.current) ⇒ Object
Establish a connection to a given ‘remote_address`.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/async/io/socket.rb', line 105 def self.connect(remote_address, local_address = nil, task: Task.current) task.annotate "connecting to #{remote_address.inspect}" wrapper = build(remote_address.afamily, remote_address.socktype, remote_address.protocol) do |socket| socket.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, true) if local_address socket.bind(local_address.to_sockaddr) end self.new(socket, task.reactor) end begin wrapper.connect(remote_address.to_sockaddr) task.annotate "connected to #{remote_address.inspect}" rescue wrapper.close raise end if block_given? begin yield wrapper, task 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 |