async-io 1.30.1 → 1.43.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,24 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2017-2023, by Samuel Williams.
5
+ # Copyright, 2018, by Thibaut Girka.
6
+ # Copyright, 2022, by Hal Brodigan.
22
7
 
23
8
  require 'socket'
24
9
  require 'async/task'
@@ -57,7 +42,7 @@ module Async
57
42
 
58
43
  alias connect_nonblock connect
59
44
 
60
- # @param duration [Numeric] the maximum time to wait for accepting a connection, if specified.
45
+ # @param timeout [Numeric] the maximum time to wait for accepting a connection, if specified.
61
46
  def accept(timeout: nil, task: Task.current)
62
47
  peer, address = async_send(:accept_nonblock, timeout: timeout)
63
48
  wrapper = Socket.new(peer, task.reactor)
@@ -116,7 +101,7 @@ module Async
116
101
  # @param remote_address [Address] The remote address to connect to.
117
102
  # @option local_address [Address] The local address to bind to before connecting.
118
103
  def self.connect(remote_address, local_address: nil, task: Task.current, **options)
119
- Async.logger.debug(self) {"Connecting to #{remote_address.inspect}"}
104
+ Console.logger.debug(self) {"Connecting to #{remote_address.inspect}"}
120
105
 
121
106
  task.annotate "connecting to #{remote_address.inspect}"
122
107
 
@@ -154,7 +139,7 @@ module Async
154
139
  # @param local_address [Address] The local address to bind to.
155
140
  # @option protocol [Integer] The socket protocol to use.
156
141
  def self.bind(local_address, protocol: 0, task: Task.current, **options, &block)
157
- Async.logger.debug(self) {"Binding to #{local_address.inspect}"}
142
+ Console.logger.debug(self) {"Binding to #{local_address.inspect}"}
158
143
 
159
144
  wrapper = build(local_address.afamily, local_address.socktype, protocol, **options) do |socket|
160
145
  socket.bind(local_address.to_sockaddr)
@@ -1,24 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2018-2023, by Samuel Williams.
22
5
 
23
6
  require_relative 'endpoint'
24
7
 
@@ -1,24 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2018-2024, by Samuel Williams.
5
+ # Copyright, 2019, by Olle Jonsson.
22
6
 
23
7
  require_relative 'host_endpoint'
24
8
  require_relative 'ssl_socket'
@@ -81,7 +65,9 @@ module Async
81
65
  yield SSLServer.new(server, context)
82
66
  end
83
67
  else
84
- return SSLServer.new(@endpoint.bind, context)
68
+ @endpoint.bind.map do |server|
69
+ SSLServer.new(server, context)
70
+ end
85
71
  end
86
72
  end
87
73
 
@@ -1,24 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2018-2023, by Samuel Williams.
22
5
 
23
6
  require_relative 'socket'
24
7
 
@@ -30,7 +13,7 @@ module Async
30
13
 
31
14
  # Asynchronous TCP socket wrapper.
32
15
  class SSLSocket < Generic
33
- wraps OpenSSL::SSL::SSLSocket, :alpn_protocol, :cert, :cipher, :client_ca, :context, :getsockopt, :hostname, :hostname=, :npn_protocol, :peer_cert, :peer_cert_chain, :pending, :post_connection_check, :setsockopt, :session, :session=, :session_reused?, :ssl_version, :state, :sync_close, :sync_close=, :sysclose, :verify_result, :tmp_key
16
+ wraps OpenSSL::SSL::SSLSocket, :alpn_protocol, :cert, :cipher, :client_ca, :context, :export_keying_material, :finished_message, :peer_finished_message, :getsockopt, :hostname, :hostname=, :npn_protocol, :peer_cert, :peer_cert_chain, :pending, :post_connection_check, :setsockopt, :session, :session=, :session_reused?, :ssl_version, :state, :sync_close, :sync_close=, :sysclose, :verify_result, :tmp_key
34
17
 
35
18
  wrap_blocking_method :accept, :accept_nonblock
36
19
  wrap_blocking_method :connect, :connect_nonblock
@@ -68,17 +51,21 @@ module Async
68
51
  super
69
52
  else
70
53
  io = self.class.wrapped_klass.new(socket.to_io, context)
71
-
72
- # We detach the socket from the reactor, otherwise it's possible to add the file descriptor to the selector twice, which is bad.
73
- socket.reactor = nil
54
+ if socket.respond_to?(:reactor)
55
+ super(io, socket.reactor)
56
+
57
+ # We detach the socket from the reactor, otherwise it's possible to add the file descriptor to the selector twice, which is bad.
58
+ socket.reactor = nil
59
+ else
60
+ super(io)
61
+ end
74
62
 
75
63
  # This ensures that when the internal IO is closed, it also closes the internal socket:
76
64
  io.sync_close = true
77
65
 
78
- # Copy the timeout:
79
- @timeout = socket.timeout
80
-
81
- super(io, socket.reactor)
66
+ if socket.respond_to?(:timeout)
67
+ @timeout = socket.timeout
68
+ end
82
69
  end
83
70
  end
84
71
 
@@ -91,11 +78,12 @@ module Async
91
78
  end
92
79
 
93
80
  def close_write
94
- self.shutdown(Socket::SHUT_WR)
81
+ # Invokes SSL_shutdown, which sends a close_notify message to the peer.
82
+ @io.__send__(:stop)
95
83
  end
96
84
 
97
85
  def close_read
98
- self.shutdown(Socket::SHUT_RD)
86
+ @io.to_io.shutdown(Socket::SHUT_RD)
99
87
  end
100
88
 
101
89
  def shutdown(how)
@@ -121,7 +109,7 @@ module Async
121
109
  self.class.new(@server.dup, @context)
122
110
  end
123
111
 
124
- def_delegators :@server, :local_address, :setsockopt, :getsockopt, :close, :close_on_exec=, :reactor=, :timeout, :timeout=
112
+ def_delegators :@server, :local_address, :setsockopt, :getsockopt, :close, :close_on_exec=, :reactor=, :timeout, :timeout=, :to_io
125
113
 
126
114
  attr :server
127
115
  attr :context
@@ -130,14 +118,18 @@ module Async
130
118
  @server.listen(*args)
131
119
  end
132
120
 
133
- def accept(task: Task.current, **options)
134
- peer, address = @server.accept(**options)
121
+ def accept(task: Task.current, timeout: nil)
122
+ peer, address = @server.accept
123
+
124
+ if timeout and peer.respond_to?(:timeout=)
125
+ peer.timeout = timeout
126
+ end
135
127
 
136
128
  wrapper = SSLSocket.new(peer, @context)
137
129
 
138
130
  return wrapper, address unless block_given?
139
131
 
140
- task.async do
132
+ task.async do |task|
141
133
  task.annotate "accepting secure connection #{address.inspect}"
142
134
 
143
135
  begin
@@ -1,24 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2018-2023, by Samuel Williams.
22
5
 
23
6
  require_relative 'generic'
24
7
 
@@ -1,24 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2017-2023, by Samuel Williams.
5
+ # Copyright, 2018, by Janko Marohnić.
6
+ # Copyright, 2021, by Aurora Nockert.
7
+ # Copyright, 2023, by Maruth Goyal.
22
8
 
23
9
  require_relative 'buffer'
24
10
  require_relative 'generic'
@@ -72,7 +58,7 @@ module Async
72
58
 
73
59
  # Reads `size` bytes from the stream. If size is not specified, read until end of file.
74
60
  def read(size = nil)
75
- return '' if size == 0
61
+ return String.new(encoding: Encoding::BINARY) if size == 0
76
62
 
77
63
  if size
78
64
  until @eof or @read_buffer.bytesize >= size
@@ -93,7 +79,7 @@ module Async
93
79
 
94
80
  # Read at most `size` bytes from the stream. Will avoid reading from the underlying stream if possible.
95
81
  def read_partial(size = nil)
96
- return '' if size == 0
82
+ return String.new(encoding: Encoding::BINARY) if size == 0
97
83
 
98
84
  if !@eof and @read_buffer.empty?
99
85
  fill_read_buffer
@@ -114,7 +100,9 @@ module Async
114
100
  raise exception, "encountered eof while reading data"
115
101
  end
116
102
 
117
- alias readpartial read_partial
103
+ def readpartial(size = nil)
104
+ read_partial(size) or raise EOFError, "Encountered eof while reading data!"
105
+ end
118
106
 
119
107
  # Efficiently read data from the stream until encountering pattern.
120
108
  # @param pattern [String] The pattern to match.
@@ -138,10 +126,21 @@ module Async
138
126
  return matched
139
127
  end
140
128
 
141
- def peek
142
- until yield(@read_buffer) or @eof
129
+ def peek(size = nil)
130
+ if size
131
+ until @eof or @read_buffer.bytesize >= size
132
+ # Compute the amount of data we need to read from the underlying stream:
133
+ read_size = size - @read_buffer.bytesize
134
+
135
+ # Don't read less than @block_size to avoid lots of small reads:
136
+ fill_read_buffer(read_size > @block_size ? read_size : @block_size)
137
+ end
138
+ return @read_buffer.slice(0, [size, @read_buffer.size].min)
139
+ end
140
+ until (block_given? && yield(@read_buffer)) or @eof
143
141
  fill_read_buffer
144
142
  end
143
+ return @read_buffer
145
144
  end
146
145
 
147
146
  def gets(separator = $/, **options)
@@ -198,6 +197,10 @@ module Async
198
197
  @io.connected?
199
198
  end
200
199
 
200
+ def readable?
201
+ @io.readable?
202
+ end
203
+
201
204
  def closed?
202
205
  @io.closed?
203
206
  end
@@ -247,6 +250,21 @@ module Async
247
250
 
248
251
  private
249
252
 
253
+ def sysread(size, buffer)
254
+ while true
255
+ result = @io.read_nonblock(size, buffer, exception: false)
256
+
257
+ case result
258
+ when :wait_readable
259
+ @io.wait_readable
260
+ when :wait_writable
261
+ @io.wait_writable
262
+ else
263
+ return result
264
+ end
265
+ end
266
+ end
267
+
250
268
  # Fills the buffer from the underlying stream.
251
269
  def fill_read_buffer(size = @block_size)
252
270
  # We impose a limit because the underlying `read` system call can fail if we request too much data in one go.
@@ -258,14 +276,14 @@ module Async
258
276
  flush
259
277
 
260
278
  if @read_buffer.empty?
261
- if @io.read_nonblock(size, @read_buffer, exception: false)
262
- # Async.logger.debug(self, name: "read") {@read_buffer.inspect}
279
+ if sysread(size, @read_buffer)
280
+ # Console.logger.debug(self, name: "read") {@read_buffer.inspect}
263
281
  return true
264
282
  end
265
283
  else
266
- if chunk = @io.read_nonblock(size, @input_buffer, exception: false)
284
+ if chunk = sysread(size, @input_buffer)
267
285
  @read_buffer << chunk
268
- # Async.logger.debug(self, name: "read") {@read_buffer.inspect}
286
+ # Console.logger.debug(self, name: "read") {@read_buffer.inspect}
269
287
 
270
288
  return true
271
289
  end
@@ -1,24 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2017-2023, by Samuel Williams.
5
+ # Copyright, 2018, by Jiang Jinyang.
22
6
 
23
7
  require_relative 'socket'
24
8
  require_relative 'stream'
@@ -40,7 +24,7 @@ module Async
40
24
  # We do this unusual dance to avoid leaking an "open" socket instance.
41
25
  socket = Socket.connect(remote_address, local_address: local_address)
42
26
  fd = socket.fcntl(Fcntl::F_DUPFD)
43
- Async.logger.debug(self) {"Connected to #{remote_address.inspect}: #{fd}"}
27
+ Console.logger.debug(self) {"Connected to #{remote_address.inspect}: #{fd}"}
44
28
  socket.close
45
29
 
46
30
  super(::TCPSocket.for_fd(fd))
@@ -1,24 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2020-2023, by Samuel Williams.
22
5
 
23
6
  require_relative 'notification'
24
7
 
@@ -29,24 +12,18 @@ module Async
29
12
  @parent = parent
30
13
  end
31
14
 
32
- def async(parent: (@parent or Task.current))
33
- parent.async do |task|
34
- notification = Async::IO::Notification.new
35
-
36
- thread = Thread.new do
37
- yield
38
- ensure
39
- notification.signal
40
- end
41
-
42
- task.annotate "Waiting for thread to finish..."
43
-
44
- notification.wait
45
-
46
- thread.value
47
- ensure
48
- if thread&.alive?
49
- thread.raise(Stop)
15
+ if Async::Scheduler.supported?
16
+ def async(parent: (@parent or Task.current))
17
+ parent.async do
18
+ thread = ::Thread.new do
19
+ yield
20
+ end
21
+
22
+ thread.join
23
+ rescue Stop
24
+ if thread&.alive?
25
+ thread.raise(Stop)
26
+ end
50
27
 
51
28
  begin
52
29
  thread.join
@@ -54,8 +31,36 @@ module Async
54
31
  # Ignore.
55
32
  end
56
33
  end
57
-
58
- notification&.close
34
+ end
35
+ else
36
+ def async(parent: (@parent or Task.current))
37
+ parent.async do |task|
38
+ notification = Async::IO::Notification.new
39
+
40
+ thread = ::Thread.new do
41
+ yield
42
+ ensure
43
+ notification.signal
44
+ end
45
+
46
+ task.annotate "Waiting for thread to finish..."
47
+
48
+ notification.wait
49
+
50
+ thread.value
51
+ ensure
52
+ if thread&.alive?
53
+ thread.raise(Stop)
54
+
55
+ begin
56
+ thread.join
57
+ rescue Stop
58
+ # Ignore.
59
+ end
60
+ end
61
+
62
+ notification&.close
63
+ end
59
64
  end
60
65
  end
61
66
  end
data/lib/async/io/trap.rb CHANGED
@@ -1,24 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2018-2023, by Samuel Williams.
22
5
 
23
6
  require_relative 'notification'
24
7
 
@@ -1,24 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2017-2023, by Samuel Williams.
22
5
 
23
6
  require_relative 'socket'
24
7
 
@@ -1,24 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2019-2023, by Samuel Williams.
5
+ # Copyright, 2019, by Olle Jonsson.
6
+ # Copyright, 2023, by Hasan Kumar.
22
7
 
23
8
  require_relative 'address_endpoint'
24
9
 
@@ -51,8 +36,8 @@ module Async
51
36
  Socket.bind(@address, **@options, &block)
52
37
  rescue Errno::EADDRINUSE
53
38
  # If you encounter EADDRINUSE from `bind()`, you can check if the socket is actually accepting connections by attempting to `connect()` to it. If the socket is still bound by an active process, the connection will succeed. Otherwise, it should be safe to `unlink()` the path and try again.
54
- if !bound? && File.exist?(@path)
55
- File.unlink(@path)
39
+ if !bound?
40
+ File.unlink(@path) rescue nil
56
41
  retry
57
42
  else
58
43
  raise
@@ -1,24 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2017-2023, by Samuel Williams.
22
5
 
23
6
  require_relative 'socket'
24
7