Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.

Commit 4ecc047

Browse files
committed
Aligning implementation with Async 2.x.
1 parent b7cd705 commit 4ecc047

File tree

3 files changed

+6
-39
lines changed

3 files changed

+6
-39
lines changed

lib/async/io/stream.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,20 @@ def fill_read_buffer(size = @block_size)
258258
flush
259259

260260
if @read_buffer.empty?
261-
if @io.read(size, @read_buffer)
261+
if @io.sysread(size, @read_buffer)
262262
# Console.logger.debug(self, name: "read") {@read_buffer.inspect}
263263
return true
264264
end
265265
else
266-
if chunk = @io.read(size, @input_buffer)
266+
if chunk = @io.sysread(size, @input_buffer)
267267
@read_buffer << chunk
268268
# Console.logger.debug(self, name: "read") {@read_buffer.inspect}
269269

270270
return true
271271
end
272272
end
273273

274+
rescue EOFError
274275
# else for both cases above:
275276
@eof = true
276277
return false

lib/async/io/tcp_socket.rb

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,55 +28,21 @@ module Async
2828
module IO
2929
# Asynchronous TCP socket wrapper.
3030
class TCPSocket < IPSocket
31-
wraps ::TCPSocket
31+
wraps ::TCPSocket, :gets, :puts
3232

3333
def initialize(remote_host, remote_port = nil, local_host = nil, local_port = nil)
3434
if remote_host.is_a? ::TCPSocket
3535
super(remote_host)
3636
else
37-
remote_address = Addrinfo.tcp(remote_host, remote_port)
38-
local_address = Addrinfo.tcp(local_host, local_port) if local_host
39-
40-
# We do this unusual dance to avoid leaking an "open" socket instance.
41-
socket = Socket.connect(remote_address, local_address: local_address)
42-
fd = socket.fcntl(Fcntl::F_DUPFD)
43-
Console.logger.debug(self) {"Connected to #{remote_address.inspect}: #{fd}"}
44-
socket.close
45-
46-
super(::TCPSocket.for_fd(fd))
47-
48-
# The equivalent blocking operation. Unfortunately there is no trivial way to make this non-blocking.
49-
# super(::TCPSocket.new(remote_host, remote_port, local_host, local_port))
37+
super(::TCPSocket.new(remote_host, remote_port, local_host, local_port))
5038
end
51-
52-
@stream = Stream.new(self)
5339
end
5440

5541
class << self
5642
alias open new
5743
end
5844

59-
def close
60-
@stream.flush
61-
super
62-
end
63-
6445
include Peer
65-
66-
attr :stream
67-
68-
# The way this buffering works is pretty atrocious.
69-
def_delegators :@stream, :gets, :puts
70-
71-
def sysread(size, buffer = nil)
72-
data = @stream.read_partial(size)
73-
74-
if buffer
75-
buffer.replace(data)
76-
end
77-
78-
return data
79-
end
8046
end
8147

8248
# Asynchronous TCP server wrappper.

spec/async/io/tcp_socket_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
# Shared port for localhost network tests.
3333
let(:server_address) {Async::IO::Address.tcp("localhost", 6788)}
34-
let(:data) {"The quick brown fox jumped over the lazy dog."}
34+
let(:data) {"The quick brown fox jumped over the lazy dog.\n"}
3535

3636
describe Async::IO::TCPServer do
3737
it_should_behave_like Async::IO::Generic

0 commit comments

Comments
 (0)