async-io 1.3.0 → 1.4.0
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.
- checksums.yaml +4 -4
- data/.travis.yml +1 -2
- data/async-io.gemspec +1 -1
- data/lib/async/io/generic.rb +7 -8
- data/lib/async/io/notification.rb +0 -2
- data/lib/async/io/socket.rb +21 -27
- data/lib/async/io/ssl_socket.rb +12 -14
- data/lib/async/io/stream.rb +18 -1
- data/lib/async/io/tcp_socket.rb +43 -1
- data/lib/async/io/udp_socket.rb +9 -1
- data/lib/async/io/unix_socket.rb +7 -10
- data/lib/async/io/version.rb +1 -1
- data/spec/async/io/socket/tcp_spec.rb +115 -0
- data/spec/async/io/socket/udp_spec.rb +73 -0
- data/spec/async/io/tcp_socket_spec.rb +17 -79
- data/spec/async/io/udp_socket_spec.rb +19 -40
- data/spec/async/io/unix_socket_spec.rb +4 -0
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b5507d72c8c7e07851a347dc15eacfbb51f06a03a50ef438af12bb889b28a8c
|
4
|
+
data.tar.gz: b53e91190155fad15b9a8097ec5243a8af5fc81623f4acb2e68f5d1591752868
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbbf5038f8a3d9bed87b8d51a81a34b2c454cb79a80405a560c857d1cfab1ec49e8ca75363eec37f0e71ff3dc2c177b7394b8562a2b7a965c01e9a67af0134a9
|
7
|
+
data.tar.gz: 2885f7b39a0bed3e4091900768499d3c0837a82b19e7cd5fb7d8770655a6f678283b4d49af2f195666b4fb6ffd003c093167f3d6ec7ce2f37ecbae436469a596
|
data/.travis.yml
CHANGED
data/async-io.gemspec
CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.require_paths = ["lib"]
|
17
17
|
spec.has_rdoc = "yard"
|
18
18
|
|
19
|
-
spec.add_dependency "async", "~> 1.
|
19
|
+
spec.add_dependency "async", "~> 1.3"
|
20
20
|
spec.add_development_dependency "async-rspec", "~> 1.2"
|
21
21
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.3"
|
data/lib/async/io/generic.rb
CHANGED
@@ -69,14 +69,12 @@ module Async
|
|
69
69
|
def wrap(*args)
|
70
70
|
wrapper = self.new(@wrapped_klass.new(*args))
|
71
71
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
else
|
79
|
-
return wrapper
|
72
|
+
return wrapper unless block_given?
|
73
|
+
|
74
|
+
begin
|
75
|
+
yield wrapper
|
76
|
+
ensure
|
77
|
+
wrapper.close
|
80
78
|
end
|
81
79
|
end
|
82
80
|
end
|
@@ -133,6 +131,7 @@ module Async
|
|
133
131
|
end
|
134
132
|
end
|
135
133
|
else
|
134
|
+
# This is also correct for Rubinius.
|
136
135
|
def async_send(*args)
|
137
136
|
async do
|
138
137
|
@io.__send__(*args)
|
data/lib/async/io/socket.rb
CHANGED
@@ -45,18 +45,16 @@ module Async
|
|
45
45
|
|
46
46
|
wrapper = Socket.new(peer, self.reactor)
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
48
|
+
return wrapper, address unless block_given?
|
49
|
+
|
50
|
+
task.async do |task|
|
51
|
+
task.annotate "incoming connection #{address.inspect}"
|
52
|
+
|
53
|
+
begin
|
54
|
+
yield wrapper, address
|
55
|
+
ensure
|
56
|
+
wrapper.close
|
57
57
|
end
|
58
|
-
else
|
59
|
-
return wrapper, address
|
60
58
|
end
|
61
59
|
end
|
62
60
|
|
@@ -124,14 +122,12 @@ module Async
|
|
124
122
|
raise
|
125
123
|
end
|
126
124
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
else
|
134
|
-
return wrapper
|
125
|
+
return wrapper unless block_given?
|
126
|
+
|
127
|
+
begin
|
128
|
+
yield wrapper, task
|
129
|
+
ensure
|
130
|
+
wrapper.close
|
135
131
|
end
|
136
132
|
end
|
137
133
|
|
@@ -150,14 +146,12 @@ module Async
|
|
150
146
|
socket.bind(local_address.to_sockaddr)
|
151
147
|
end
|
152
148
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
else
|
160
|
-
return wrapper
|
149
|
+
return wrapper unless block_given?
|
150
|
+
|
151
|
+
begin
|
152
|
+
yield wrapper, task
|
153
|
+
ensure
|
154
|
+
wrapper.close
|
161
155
|
end
|
162
156
|
end
|
163
157
|
|
data/lib/async/io/ssl_socket.rb
CHANGED
@@ -76,22 +76,20 @@ module Async
|
|
76
76
|
|
77
77
|
wrapper = SSLSocket.connect_socket(peer, @context)
|
78
78
|
|
79
|
-
|
80
|
-
|
81
|
-
|
79
|
+
return wrapper, address unless block_given?
|
80
|
+
|
81
|
+
task.async do
|
82
|
+
task.annotate "accepting secure connection #{address}"
|
83
|
+
|
84
|
+
begin
|
85
|
+
wrapper.accept
|
82
86
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
Async.logger.error($!.class) {$!}
|
89
|
-
ensure
|
90
|
-
wrapper.close
|
91
|
-
end
|
87
|
+
yield wrapper, address
|
88
|
+
rescue SSLError
|
89
|
+
Async.logger.error($!.class) {$!}
|
90
|
+
ensure
|
91
|
+
wrapper.close
|
92
92
|
end
|
93
|
-
else
|
94
|
-
return wrapper, address
|
95
93
|
end
|
96
94
|
end
|
97
95
|
end
|
data/lib/async/io/stream.rb
CHANGED
@@ -24,10 +24,13 @@ require_relative 'generic'
|
|
24
24
|
module Async
|
25
25
|
module IO
|
26
26
|
class Stream
|
27
|
-
def initialize(io, block_size: 1024*8)
|
27
|
+
def initialize(io, block_size: 1024*8, sync: true)
|
28
28
|
@io = io
|
29
29
|
@eof = false
|
30
30
|
|
31
|
+
# We don't want Ruby to do any IO buffering.
|
32
|
+
@io.sync = sync
|
33
|
+
|
31
34
|
@block_size = block_size
|
32
35
|
|
33
36
|
@read_buffer = BinaryString.new
|
@@ -74,6 +77,20 @@ module Async
|
|
74
77
|
@write_buffer.clear
|
75
78
|
end
|
76
79
|
|
80
|
+
def gets(separator = $/)
|
81
|
+
flush
|
82
|
+
|
83
|
+
read_until(separator)
|
84
|
+
end
|
85
|
+
|
86
|
+
def puts(*args, separator: $/)
|
87
|
+
args.each do |arg|
|
88
|
+
@write_buffer << arg << separator
|
89
|
+
end
|
90
|
+
|
91
|
+
flush
|
92
|
+
end
|
93
|
+
|
77
94
|
# Closes the stream and flushes any unwritten data.
|
78
95
|
def close
|
79
96
|
flush rescue nil
|
data/lib/async/io/tcp_socket.rb
CHANGED
@@ -19,19 +19,61 @@
|
|
19
19
|
# THE SOFTWARE.
|
20
20
|
|
21
21
|
require_relative 'socket'
|
22
|
+
require_relative 'stream'
|
23
|
+
require 'fcntl'
|
22
24
|
|
23
25
|
module Async
|
24
26
|
module IO
|
25
27
|
# Asynchronous TCP socket wrapper.
|
26
28
|
class TCPSocket < IPSocket
|
27
29
|
wraps ::TCPSocket
|
30
|
+
|
31
|
+
def initialize(remote_host, remote_port = nil, local_host = nil, local_port = nil)
|
32
|
+
if remote_host.is_a? ::TCPSocket
|
33
|
+
super(remote_host)
|
34
|
+
else
|
35
|
+
remote_address = Addrinfo.tcp(remote_host, remote_port)
|
36
|
+
local_address = Addrinfo.tcp(local_host, local_port) if local_host
|
37
|
+
|
38
|
+
# We do this unusual dance to avoid leaking an "open" socket instance.
|
39
|
+
socket = Socket.connect(remote_address, local_address)
|
40
|
+
fd = socket.fcntl(Fcntl::F_DUPFD)
|
41
|
+
socket.close
|
42
|
+
|
43
|
+
super(::TCPSocket.for_fd(fd))
|
44
|
+
|
45
|
+
# The equivalent blocking operation. Unfortunately there is no trivial way to make this non-blocking.
|
46
|
+
# super(::TCPSocket.new(remote_host, remote_port, local_host, local_port))
|
47
|
+
end
|
48
|
+
|
49
|
+
@buffer = Stream.new(self)
|
50
|
+
end
|
51
|
+
|
52
|
+
attr :buffer
|
53
|
+
|
54
|
+
def_delegators :@buffer, :gets, :puts, :flush
|
28
55
|
end
|
29
56
|
|
30
57
|
# Asynchronous TCP server wrappper.
|
31
58
|
class TCPServer < TCPSocket
|
32
59
|
wraps ::TCPServer, :listen
|
33
60
|
|
34
|
-
|
61
|
+
def initialize(*args)
|
62
|
+
if args.first.is_a? ::TCPServer
|
63
|
+
super(args.first)
|
64
|
+
else
|
65
|
+
# We assume this operation doesn't block (for long):
|
66
|
+
super(::TCPServer.new(*args))
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def accept(task: Task.current)
|
71
|
+
peer, address = async_send(:accept_nonblock)
|
72
|
+
|
73
|
+
wrapper = TCPSocket.new(peer)
|
74
|
+
|
75
|
+
return wrapper, address
|
76
|
+
end
|
35
77
|
end
|
36
78
|
end
|
37
79
|
end
|
data/lib/async/io/udp_socket.rb
CHANGED
@@ -26,8 +26,16 @@ module Async
|
|
26
26
|
class UDPSocket < IPSocket
|
27
27
|
wraps ::UDPSocket, :bind
|
28
28
|
|
29
|
+
def initialize(family)
|
30
|
+
if family.is_a? ::UDPSocket
|
31
|
+
super(family)
|
32
|
+
else
|
33
|
+
super(::UDPSocket.new(family))
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
29
37
|
# We pass `send` through directly, but in theory it might block. Internally, it uses sendto.
|
30
|
-
def_delegators :@io, :send
|
38
|
+
def_delegators :@io, :send, :connect
|
31
39
|
|
32
40
|
# This function is so fucked. Why does `UDPSocket#recvfrom` return the remote address as an array, but `Socket#recfrom` return it as an `Addrinfo`? You should prefer `recvmsg`.
|
33
41
|
wrap_blocking_method :recvfrom, :recvfrom_nonblock
|
data/lib/async/io/unix_socket.rb
CHANGED
@@ -34,17 +34,14 @@ module Async
|
|
34
34
|
|
35
35
|
def accept
|
36
36
|
peer = async_send(:accept_nonblock)
|
37
|
+
wrapper = UNIXSocket.new(peer, self.reactor)
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
wrapper.close
|
45
|
-
end
|
46
|
-
else
|
47
|
-
return UNIXSocket.new(peer, self.reactor)
|
39
|
+
return wrapper unless block_given?
|
40
|
+
|
41
|
+
begin
|
42
|
+
yield wrapper
|
43
|
+
ensure
|
44
|
+
wrapper.close
|
48
45
|
end
|
49
46
|
end
|
50
47
|
end
|
data/lib/async/io/version.rb
CHANGED
@@ -0,0 +1,115 @@
|
|
1
|
+
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'async/io/tcp_socket'
|
22
|
+
|
23
|
+
RSpec.describe Async::Reactor do
|
24
|
+
include_context Async::RSpec::Leaks
|
25
|
+
|
26
|
+
# Shared port for localhost network tests.
|
27
|
+
let(:server_address) {Async::IO::Address.tcp("localhost", 6788)}
|
28
|
+
let(:data) {"The quick brown fox jumped over the lazy dog."}
|
29
|
+
|
30
|
+
around(:each) do |example|
|
31
|
+
# Accept a single incoming connection and then finish.
|
32
|
+
subject.async do |task|
|
33
|
+
Async::IO::Socket.bind(server_address) do |server|
|
34
|
+
server.listen(10)
|
35
|
+
|
36
|
+
server.accept do |peer, address|
|
37
|
+
data = peer.read(512)
|
38
|
+
peer.write(data)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
result = example.run
|
44
|
+
|
45
|
+
if result.is_a? Exception
|
46
|
+
result
|
47
|
+
else
|
48
|
+
subject.run
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe 'basic tcp server' do
|
53
|
+
it "should start server and send data" do
|
54
|
+
subject.async do
|
55
|
+
Async::IO::Socket.connect(server_address) do |client|
|
56
|
+
client.write(data)
|
57
|
+
expect(client.read(512)).to be == data
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'non-blocking tcp connect' do
|
64
|
+
it "should start server and send data" do
|
65
|
+
subject.async do |task|
|
66
|
+
Async::IO::Socket.connect(server_address) do |client|
|
67
|
+
client.write(data)
|
68
|
+
expect(client.read(512)).to be == data
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
it "can connect socket and read/write in a different task" do
|
74
|
+
socket = nil
|
75
|
+
|
76
|
+
subject.async do |task|
|
77
|
+
socket = Async::IO::Socket.connect(server_address)
|
78
|
+
|
79
|
+
# Stop the reactor once the connection was made.
|
80
|
+
subject.stop
|
81
|
+
end
|
82
|
+
|
83
|
+
subject.run
|
84
|
+
|
85
|
+
expect(socket).to_not be_nil
|
86
|
+
expect(socket).to be_kind_of Async::Wrapper
|
87
|
+
|
88
|
+
subject.async do
|
89
|
+
socket.write(data)
|
90
|
+
|
91
|
+
expect(socket.read(512)).to be == data
|
92
|
+
end
|
93
|
+
|
94
|
+
subject.run
|
95
|
+
|
96
|
+
socket.close
|
97
|
+
end
|
98
|
+
|
99
|
+
it "can't use a socket in nested tasks" do
|
100
|
+
subject.async do |task|
|
101
|
+
socket = Async::IO::Socket.connect(server_address)
|
102
|
+
expect(socket).to be_kind_of Async::Wrapper
|
103
|
+
|
104
|
+
expect do
|
105
|
+
subject.async do
|
106
|
+
socket.write(data)
|
107
|
+
# expect(socket.read(512)).to be == data
|
108
|
+
end
|
109
|
+
end.to_not raise_error
|
110
|
+
|
111
|
+
socket.close
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'async/io/udp_socket'
|
22
|
+
|
23
|
+
RSpec.describe Async::Reactor do
|
24
|
+
include_context Async::RSpec::Leaks
|
25
|
+
|
26
|
+
# Shared port for localhost network tests.
|
27
|
+
let(:server_address) {Async::IO::Address.udp("127.0.0.1", 6778)}
|
28
|
+
let(:data) {"The quick brown fox jumped over the lazy dog."}
|
29
|
+
|
30
|
+
describe 'basic udp server' do
|
31
|
+
it "should echo data back to peer" do
|
32
|
+
subject.async do
|
33
|
+
Async::IO::Socket.bind(server_address) do |server|
|
34
|
+
packet, address = server.recvfrom(512)
|
35
|
+
|
36
|
+
server.send(packet, 0, address)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
subject.async do
|
41
|
+
Async::IO::Socket.connect(server_address) do |client|
|
42
|
+
client.send(data)
|
43
|
+
response = client.recv(512)
|
44
|
+
|
45
|
+
expect(response).to be == data
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
subject.run
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should use unconnected socket" do
|
53
|
+
subject.async do
|
54
|
+
Async::IO::Socket.bind(server_address) do |server|
|
55
|
+
packet, address = server.recvfrom(512)
|
56
|
+
|
57
|
+
server.send(packet, 0, address)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
subject.async do
|
62
|
+
Async::IO::UDPSocket.wrap(server_address.afamily) do |client|
|
63
|
+
client.send(data, 0, server_address)
|
64
|
+
response, address = client.recvfrom(512)
|
65
|
+
|
66
|
+
expect(response).to be == data
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
subject.run
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -5,7 +5,7 @@
|
|
5
5
|
# in the Software without restriction, including without limitation the rights
|
6
6
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
7
|
# copies of the Software, and to permit persons to whom the Software is
|
8
|
-
# furnished to do so,
|
8
|
+
# furnished to do so, reactor to the following conditions:
|
9
9
|
#
|
10
10
|
# The above copyright notice and this permission notice shall be included in
|
11
11
|
# all copies or substantial portions of the Software.
|
@@ -20,96 +20,34 @@
|
|
20
20
|
|
21
21
|
require 'async/io/tcp_socket'
|
22
22
|
|
23
|
-
RSpec.describe Async::
|
24
|
-
include_context Async::RSpec::
|
23
|
+
RSpec.describe Async::IO::TCPSocket do
|
24
|
+
include_context Async::RSpec::Reactor
|
25
25
|
|
26
26
|
# Shared port for localhost network tests.
|
27
27
|
let(:server_address) {Async::IO::Address.tcp("localhost", 6788)}
|
28
28
|
let(:data) {"The quick brown fox jumped over the lazy dog."}
|
29
29
|
|
30
|
-
around(:each) do |example|
|
31
|
-
# Accept a single incoming connection and then finish.
|
32
|
-
subject.async do |task|
|
33
|
-
Async::IO::Socket.bind(server_address) do |server|
|
34
|
-
server.listen(10)
|
35
|
-
|
36
|
-
server.accept do |peer, address|
|
37
|
-
data = peer.read(512)
|
38
|
-
peer.write(data)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
result = example.run
|
44
|
-
|
45
|
-
if result.is_a? Exception
|
46
|
-
result
|
47
|
-
else
|
48
|
-
subject.run
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
30
|
describe 'basic tcp server' do
|
53
31
|
it "should start server and send data" do
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe 'non-blocking tcp connect' do
|
64
|
-
it "should start server and send data" do
|
65
|
-
subject.async do |task|
|
66
|
-
Async::IO::Socket.connect(server_address) do |client|
|
67
|
-
client.write(data)
|
68
|
-
expect(client.read(512)).to be == data
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
it "can connect socket and read/write in a different task" do
|
74
|
-
socket = nil
|
75
|
-
|
76
|
-
subject.async do |task|
|
77
|
-
socket = Async::IO::Socket.connect(server_address)
|
32
|
+
# Accept a single incoming connection and then finish.
|
33
|
+
server_task = reactor.async do |task|
|
34
|
+
server = Async::IO::TCPServer.new("localhost", 6788)
|
35
|
+
peer, address = server.accept
|
78
36
|
|
79
|
-
|
80
|
-
|
81
|
-
end
|
82
|
-
|
83
|
-
subject.run
|
84
|
-
|
85
|
-
expect(socket).to_not be_nil
|
86
|
-
expect(socket).to be_kind_of Async::Wrapper
|
87
|
-
|
88
|
-
subject.async do
|
89
|
-
socket.write(data)
|
37
|
+
data = peer.gets
|
38
|
+
peer.puts(data)
|
90
39
|
|
91
|
-
|
40
|
+
peer.close
|
41
|
+
server.close
|
92
42
|
end
|
93
43
|
|
94
|
-
|
44
|
+
client = Async::IO::TCPSocket.new("localhost", 6788)
|
95
45
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
socket = Async::IO::Socket.connect(server_address)
|
102
|
-
expect(socket).to be_kind_of Async::Wrapper
|
103
|
-
|
104
|
-
expect do
|
105
|
-
subject.async do
|
106
|
-
socket.write(data)
|
107
|
-
# expect(socket.read(512)).to be == data
|
108
|
-
end
|
109
|
-
end.to_not raise_error
|
110
|
-
|
111
|
-
socket.close
|
112
|
-
end
|
46
|
+
client.puts(data)
|
47
|
+
expect(client.gets).to be == data
|
48
|
+
|
49
|
+
client.close
|
50
|
+
server_task.wait
|
113
51
|
end
|
114
52
|
end
|
115
53
|
end
|
@@ -20,54 +20,33 @@
|
|
20
20
|
|
21
21
|
require 'async/io/udp_socket'
|
22
22
|
|
23
|
-
RSpec.describe Async::
|
24
|
-
include_context Async::RSpec::
|
23
|
+
RSpec.describe Async::IO::UDPSocket do
|
24
|
+
include_context Async::RSpec::Reactor
|
25
25
|
|
26
|
-
# Shared port for localhost network tests.
|
27
|
-
let(:server_address) {Async::IO::Address.udp("127.0.0.1", 6778)}
|
28
26
|
let(:data) {"The quick brown fox jumped over the lazy dog."}
|
29
27
|
|
30
28
|
describe 'basic udp server' do
|
31
29
|
it "should echo data back to peer" do
|
32
|
-
|
33
|
-
Async::IO::
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
30
|
+
reactor.async do
|
31
|
+
server = Async::IO::UDPSocket.new(Socket::AF_INET)
|
32
|
+
server.bind("127.0.0.1", 6778)
|
33
|
+
|
34
|
+
packet, address = server.recvfrom(512)
|
35
|
+
server.send(packet, 0, address[3], address[1])
|
36
|
+
|
37
|
+
server.close
|
38
38
|
end
|
39
39
|
|
40
|
-
|
41
|
-
Async::IO::
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
40
|
+
reactor.async do
|
41
|
+
client = Async::IO::UDPSocket.new(Socket::AF_INET)
|
42
|
+
client.connect("127.0.0.1", 6778)
|
43
|
+
|
44
|
+
client.send(data, 0)
|
45
|
+
response = client.recv(512)
|
46
|
+
client.close
|
47
|
+
|
48
|
+
expect(response).to be == data
|
47
49
|
end
|
48
|
-
|
49
|
-
subject.run
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should use unconnected socket" do
|
53
|
-
subject.async do
|
54
|
-
Async::IO::Socket.bind(server_address) do |server|
|
55
|
-
packet, address = server.recvfrom(512)
|
56
|
-
|
57
|
-
server.send(packet, 0, address)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
subject.async do
|
62
|
-
Async::IO::UDPSocket.wrap(server_address.afamily) do |client|
|
63
|
-
client.send(data, 0, server_address)
|
64
|
-
response, address = client.recvfrom(512)
|
65
|
-
|
66
|
-
expect(response).to be == data
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
subject.run
|
71
50
|
end
|
72
51
|
end
|
73
52
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: async-io
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: async
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.3'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: async-rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -115,6 +115,8 @@ files:
|
|
115
115
|
- spec/async/io/generic_spec.rb
|
116
116
|
- spec/async/io/notification_spec.rb
|
117
117
|
- spec/async/io/protocol/line_spec.rb
|
118
|
+
- spec/async/io/socket/tcp_spec.rb
|
119
|
+
- spec/async/io/socket/udp_spec.rb
|
118
120
|
- spec/async/io/socket_spec.rb
|
119
121
|
- spec/async/io/ssl_socket_spec.rb
|
120
122
|
- spec/async/io/stream_spec.rb
|
@@ -154,6 +156,8 @@ test_files:
|
|
154
156
|
- spec/async/io/generic_spec.rb
|
155
157
|
- spec/async/io/notification_spec.rb
|
156
158
|
- spec/async/io/protocol/line_spec.rb
|
159
|
+
- spec/async/io/socket/tcp_spec.rb
|
160
|
+
- spec/async/io/socket/udp_spec.rb
|
157
161
|
- spec/async/io/socket_spec.rb
|
158
162
|
- spec/async/io/ssl_socket_spec.rb
|
159
163
|
- spec/async/io/stream_spec.rb
|