This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
socket.rb
198 lines (155 loc) · 6.29 KB
/
socket.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# frozen_string_literal: true
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require 'socket'
require 'async/task'
require_relative 'peer'
require_relative 'server'
require_relative 'generic'
module Async
module IO
class BasicSocket < Generic
wraps ::BasicSocket, :setsockopt, :connect_address, :close_read, :close_write, :local_address, :remote_address, :do_not_reverse_lookup, :do_not_reverse_lookup=, :shutdown, :getsockopt, :getsockname, :getpeername, :getpeereid
wrap_blocking_method :recv, :recv_nonblock
wrap_blocking_method :recvmsg, :recvmsg_nonblock
wrap_blocking_method :sendmsg, :sendmsg_nonblock
wrap_blocking_method :send, :sendmsg_nonblock, invert: false
include Peer
end
class Socket < BasicSocket
wraps ::Socket, :bind, :ipv6only!, :listen
wrap_blocking_method :recvfrom, :recvfrom_nonblock
# @raise Errno::EAGAIN the connection failed due to the remote end being overloaded.
def connect(*args)
begin
async_send(:connect_nonblock, *args)
rescue Errno::EISCONN
# We are now connected.
end
end
alias connect_nonblock connect
# @param timeout [Numeric] the maximum time to wait for accepting a connection, if specified.
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} [fd=#{wrapper.fileno}]"
begin
yield wrapper, address
ensure
wrapper.close
end
end
end
alias accept_nonblock accept
alias sysaccept accept
# Build and wrap the underlying io.
# @option reuse_port [Boolean] Allow this port to be bound in multiple processes.
# @option reuse_address [Boolean] Allow this port to be bound in multiple processes.
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
# Establish a connection to a given `remote_address`.
# @example
# socket = Async::IO::Socket.connect(Async::IO::Address.tcp("8.8.8.8", 53))
# @param remote_address [Address] The remote address to connect to.
# @option local_address [Address] The local address to bind to before connecting.
def self.connect(remote_address, local_address: nil, task: Task.current, **options)
Console.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} [fd=#{wrapper.fileno}]"
rescue Exception
wrapper.close
raise
end
return wrapper unless block_given?
begin
yield wrapper, task
ensure
wrapper.close
end
end
# Bind to a local address.
# @example
# socket = Async::IO::Socket.bind(Async::IO::Address.tcp("0.0.0.0", 9090))
# @param local_address [Address] The local address to bind to.
# @option protocol [Integer] The socket protocol to use.
def self.bind(local_address, protocol: 0, task: Task.current, **options, &block)
Console.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
# Bind to a local address and accept connections in a loop.
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
include Server
def self.pair(*args)
::Socket.pair(*args).map(&self.method(:new))
end
end
class IPSocket < BasicSocket
wraps ::IPSocket, :addr, :peeraddr
wrap_blocking_method :recvfrom, :recvfrom_nonblock
end
end
end