Skip to content

Commit

Permalink
Extract #system_read and #system_write for FileDescriptor and `…
Browse files Browse the repository at this point in the history
…Socket` (#14626)
  • Loading branch information
straight-shoota authored May 25, 2024
1 parent b12f17f commit f3be084
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 35 deletions.
4 changes: 4 additions & 0 deletions src/crystal/system/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module Crystal::System::FileDescriptor
# For the duration of the block, enables raw mode if *enable* is true, enables
# cooked mode otherwise.
# private def system_raw(enable : Bool, & : ->)

# private def system_read(slice : Bool) : Int32

# private def system_write(slice : Bool) : Int32
end

{% if flag?(:wasi) %}
Expand Down
4 changes: 2 additions & 2 deletions src/crystal/system/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ module Crystal::System::Socket

# def self.fcntl(fd, cmd, arg = 0)

# private def unbuffered_read(slice : Bytes) : Int32
# private def system_read(slice : Bytes) : Int32

# private def unbuffered_write(slice : Bytes) : Nil
# private def system_write(slice : Bytes) : Int32

# private def system_close

Expand Down
4 changes: 2 additions & 2 deletions src/crystal/system/unix/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Crystal::System::FileDescriptor
STDOUT_HANDLE = 1
STDERR_HANDLE = 2

private def unbuffered_read(slice : Bytes) : Int32
private def system_read(slice : Bytes) : Int32
evented_read(slice, "Error reading file") do
LibC.read(fd, slice, slice.size).tap do |return_code|
if return_code == -1 && Errno.value == Errno::EBADF
Expand All @@ -27,7 +27,7 @@ module Crystal::System::FileDescriptor
end
end

private def unbuffered_write(slice : Bytes) : Nil
private def system_write(slice : Bytes) : Int32
evented_write(slice, "Error writing file") do |slice|
LibC.write(fd, slice, slice.size).tap do |return_code|
if return_code == -1 && Errno.value == Errno::EBADF
Expand Down
4 changes: 2 additions & 2 deletions src/crystal/system/unix/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,13 @@ module Crystal::System::Socket
LibC.isatty(fd) == 1
end

private def unbuffered_read(slice : Bytes) : Int32
private def system_read(slice : Bytes) : Int32
evented_read(slice, "Error reading socket") do
LibC.recv(fd, slice, slice.size, 0).to_i32
end
end

private def unbuffered_write(slice : Bytes) : Nil
private def system_write(slice : Bytes) : Int32
evented_write(slice, "Error writing to socket") do |slice|
LibC.send(fd, slice, slice.size, 0)
end
Expand Down
4 changes: 2 additions & 2 deletions src/crystal/system/wasi/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ module Crystal::System::Socket
LibC.isatty(fd) == 1
end

private def unbuffered_read(slice : Bytes) : Int32
private def system_read(slice : Bytes) : Int32
evented_read(slice, "Error reading socket") do
LibC.recv(fd, slice, slice.size, 0).to_i32
end
end

private def unbuffered_write(slice : Bytes) : Nil
private def system_write(slice : Bytes) : Int32
evented_write(slice, "Error writing to socket") do |slice|
LibC.send(fd, slice, slice.size, 0)
end
Expand Down
22 changes: 9 additions & 13 deletions src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Crystal::System::FileDescriptor

@system_blocking = true

private def unbuffered_read(slice : Bytes) : Int32
private def system_read(slice : Bytes) : Int32
handle = windows_handle
if ConsoleUtils.console?(handle)
ConsoleUtils.read(handle, slice)
Expand Down Expand Up @@ -48,19 +48,15 @@ module Crystal::System::FileDescriptor
bytes_read.to_i32
end

private def unbuffered_write(slice : Bytes) : Nil
private def system_write(slice : Bytes) : Int32
handle = windows_handle
until slice.empty?
if system_blocking?
bytes_written = write_blocking(handle, slice)
else
bytes_written = overlapped_operation(handle, "WriteFile", write_timeout, writing: true) do |overlapped|
ret = LibC.WriteFile(handle, slice, slice.size, out byte_count, overlapped)
{ret, byte_count}
end
end

slice += bytes_written
if system_blocking?
write_blocking(handle, slice).to_i32
else
overlapped_operation(handle, "WriteFile", write_timeout, writing: true) do |overlapped|
ret = LibC.WriteFile(handle, slice, slice.size, out byte_count, overlapped)
{ret, byte_count}
end.to_i32
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/crystal/system/win32/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ module Crystal::System::Socket
LibC.GetConsoleMode(LibC::HANDLE.new(fd), out _) != 0
end

private def unbuffered_read(slice : Bytes) : Int32
private def system_read(slice : Bytes) : Int32
wsabuf = wsa_buffer(slice)

bytes_read = overlapped_read(fd, "WSARecv", connreset_is_error: false) do |overlapped|
Expand All @@ -457,7 +457,7 @@ module Crystal::System::Socket
bytes_read.to_i32
end

private def unbuffered_write(slice : Bytes) : Nil
private def system_write(slice : Bytes) : Int32
wsabuf = wsa_buffer(slice)

bytes = overlapped_write(fd, "WSASend") do |overlapped|
Expand Down
20 changes: 8 additions & 12 deletions src/io/evented.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,18 @@ module IO::Evented
resume_pending_readers
end

def evented_write(slice : Bytes, errno_msg : String, &) : Nil
return if slice.empty?

def evented_write(slice : Bytes, errno_msg : String, &) : Int32
begin
loop do
# TODO: Investigate why the .to_i64 is needed as a workaround for #8230
bytes_written = (yield slice).to_i64
bytes_written = yield slice
if bytes_written != -1
slice += bytes_written
return if slice.size == 0
return bytes_written.to_i32
end

if Errno.value == Errno::EAGAIN
wait_writable
else
if Errno.value == Errno::EAGAIN
wait_writable
else
raise IO::Error.from_errno(errno_msg, target: self)
end
raise IO::Error.from_errno(errno_msg, target: self)
end
end
ensure
Expand Down
10 changes: 10 additions & 0 deletions src/io/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ class IO::FileDescriptor < IO
pp.text inspect
end

private def unbuffered_read(slice : Bytes) : Int32
system_read(slice)
end

private def unbuffered_write(slice : Bytes) : Nil
until slice.empty?
slice += system_write(slice)
end
end

private def unbuffered_rewind : Nil
self.pos = 0
end
Expand Down
10 changes: 10 additions & 0 deletions src/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,16 @@ class Socket < IO
system_tty?
end

private def unbuffered_read(slice : Bytes) : Int32
system_read(slice)
end

private def unbuffered_write(slice : Bytes) : Nil
until slice.empty?
slice += system_write(slice)
end
end

private def unbuffered_rewind : Nil
raise Socket::Error.new("Can't rewind")
end
Expand Down

0 comments on commit f3be084

Please sign in to comment.