23 _eof = _closed =
true;
50 constexpr
size_t BUFFER_SIZE = 1024 * 1024;
51 const size_t size_to_read =
min(BUFFER_SIZE, limit);
55 if (limit > 0 && bytes_read == 0) {
58 if (bytes_read >
static_cast<ssize_t
>(size_to_read)) {
77 size_t total_bytes_written = 0;
80 auto iovecs =
buffer.as_iovecs();
82 const ssize_t bytes_written =
SystemCall(
"writev", ::writev(
fd_num(), iovecs.data(), iovecs.size()));
83 if (bytes_written == 0 and
buffer.
size() != 0) {
84 throw runtime_error(
"write returned 0 given non-empty input buffer");
88 throw runtime_error(
"write wrote more than length of input buffer");
93 buffer.remove_prefix(bytes_written);
95 total_bytes_written += bytes_written;
98 return total_bytes_written;
103 if (blocking_state) {
104 flags ^= (flags & O_NONBLOCK);
A non-owning temporary view (similar to std::string_view) of a discontiguous string.
A handle on a kernel file descriptor.
FDWrapper(const int fd)
Construct from a file descriptor number returned by the kernel.
void close()
Calls close(2) on FDWrapper::_fd.
~FDWrapper()
Closes the file descriptor upon destruction.
A reference-counted handle to a file descriptor.
void close()
Close the underlying file descriptor.
FileDescriptor duplicate() const
Copy a FileDescriptor explicitly, increasing the FDWrapper refcount.
int fd_num() const
underlying descriptor number
void register_read()
increment read count
std::shared_ptr< FDWrapper > _internal_fd
A reference-counted handle to a shared FDWrapper.
void set_blocking(const bool blocking_state)
Set blocking(true) or non-blocking(false)
size_t write(const char *str, const bool write_all=true)
Write a string, possibly blocking until all is written.
std::string read(const size_t limit=std::numeric_limits< size_t >::max())
Read up to limit bytes.
FileDescriptor(std::shared_ptr< FDWrapper > other_shared_ptr)
Private constructor used by duplicate()
void register_write()
increment write count
SystemCall("socketpair", ::socketpair(AF_UNIX, SOCK_STREAM, 0, fds.data()))