Class: Async::IO::Socket
Constant Summary
Constants inherited
from Generic
Generic::WRAPPERS
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, **options, &block) ⇒ Object
-
.build(*args, task: Task.current) ⇒ Object
-
.connect(remote_address, local_address = nil, reuse_port: false, task: Task.current, **options) ⇒ Object
Establish a connection to a given ‘remote_address`.
-
.pair(*args) ⇒ Object
Instance Method Summary
collapse
Methods included from Server
#accept_each
Methods inherited from BasicSocket
#type
Methods inherited from Generic
#nonblock, #nonblock?, #read, #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.
168
169
170
171
172
173
174
|
# File 'lib/async/io/socket.rb', line 168
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, **options, &block) ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/async/io/socket.rb', line 145
def self.bind(local_address, protocol: 0, reuse_port: false, 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.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
return wrapper unless block_given?
task.async do |task|
task.annotate "binding to #{local_address.inspect}"
begin
yield wrapper, task
ensure
wrapper.close
end
end
end
|
.build(*args, task: Task.current) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/async/io/socket.rb', line 89
def self.build(*args, task: Task.current)
socket = wrapped_klass.new(*args)
yield socket
return self.new(socket, task.reactor)
rescue Exception
socket.close if socket
raise
end
|
.connect(remote_address, local_address = nil, reuse_port: false, task: Task.current, **options) ⇒ Object
Establish a connection to a given ‘remote_address`.
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
136
137
|
# File 'lib/async/io/socket.rb', line 107
def self.connect(remote_address, local_address = nil, reuse_port: false, task: Task.current, **options)
task.annotate "connecting to #{remote_address.inspect}"
wrapper = build(remote_address.afamily, remote_address.socktype, remote_address.protocol, **options) do |socket|
socket.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, reuse_port)
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
return wrapper unless block_given?
begin
yield wrapper, task
ensure
wrapper.close
end
end
|
.pair(*args) ⇒ Object
178
179
180
|
# File 'lib/async/io/socket.rb', line 178
def self.pair(*args)
::Socket.pair(*args).map(&self.method(:new))
end
|
Instance Method Details
#accept(task: Task.current) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/async/io/socket.rb', line 69
def accept(task: Task.current)
peer, address = async_send(:accept_nonblock)
wrapper = Socket.new(peer, task.reactor)
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_nonblock ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/async/io/socket.rb', line 86
def accept(task: Task.current)
peer, address = async_send(:accept_nonblock)
wrapper = Socket.new(peer, task.reactor)
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
59
60
61
62
63
64
65
|
# File 'lib/async/io/socket.rb', line 59
def connect(*args)
begin
async_send(:connect_nonblock, *args)
rescue Errno::EISCONN
end
end
|
#connect_nonblock ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/async/io/socket.rb', line 67
def connect(*args)
begin
async_send(:connect_nonblock, *args)
rescue Errno::EISCONN
end
end
|
#sysaccept ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/async/io/socket.rb', line 87
def accept(task: Task.current)
peer, address = async_send(:accept_nonblock)
wrapper = Socket.new(peer, task.reactor)
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
|