-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add EventLoop::FileDescriptor
module
#14639
Changes from 4 commits
897b377
c16b6f2
4e5688c
1385fcc
7c22490
cecea80
0290987
9baef8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
abstract class Crystal::EventLoop | ||
module FileDescriptor | ||
# Reads at least one byte from the file descriptor into *slice* and continues | ||
# fiber when the read is complete. | ||
# Returns the number of bytes read. | ||
abstract def read(file_descriptor : Crystal::System::FileDescriptor, slice : Bytes) : Int32 | ||
|
||
# Writes at least one byte from *slice* to the file descriptor and continues | ||
# fiber when the write is complete. | ||
# Returns the number of bytes written. | ||
abstract def write(file_descriptor : Crystal::System::FileDescriptor, slice : Bytes) : Int32 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: same and "when the write is complete" can be ambiguous (writes fully?)
|
||
|
||
# Closes the file descriptor resource. | ||
abstract def close(file_descriptor : Crystal::System::FileDescriptor) : Nil | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,26 +17,6 @@ module Crystal::System::FileDescriptor | |
STDOUT_HANDLE = 1 | ||
STDERR_HANDLE = 2 | ||
|
||
private def system_read(slice : Bytes) : Int32 | ||
evented_read("Error reading file") do | ||
LibC.read(fd, slice, slice.size).tap do |return_code| | ||
if return_code == -1 && Errno.value == Errno::EBADF | ||
raise IO::Error.new "File not open for reading", target: self | ||
end | ||
end | ||
end | ||
end | ||
|
||
private def system_write(slice : Bytes) : Int32 | ||
evented_write("Error writing file") do | ||
LibC.write(fd, slice, slice.size).tap do |return_code| | ||
if return_code == -1 && Errno.value == Errno::EBADF | ||
raise IO::Error.new "File not open for writing", target: self | ||
end | ||
end | ||
end | ||
end | ||
|
||
private def system_blocking? | ||
flags = fcntl(LibC::F_GETFL) | ||
!flags.bits_set? LibC::O_NONBLOCK | ||
|
@@ -131,14 +111,14 @@ module Crystal::System::FileDescriptor | |
# Mark the handle open, since we had to have dup'd a live handle. | ||
@closed = false | ||
|
||
evented_reopen | ||
event_loop.close(self) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to propose to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh good point. Actually, all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, this is what I mean with "public". I guess we can deprecate all of it... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eventually, everything from |
||
end | ||
|
||
private def system_close | ||
# Perform libevent cleanup before LibC.close. | ||
# Using a file descriptor after it has been closed is never defined and can | ||
# always lead to undefined results. This is not specific to libevent. | ||
evented_close | ||
event_loop.close(self) | ||
|
||
file_descriptor_close | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: maybe explicit both the blocking and EOF behaviors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think that's better 👍
Btw. these descriptions have been up for review in crystal-lang/rfcs#7 😏
Maybe you want to take a look at the other there ones as well?