|
Sponge
CS144's user-space TCP library
|
Go to the documentation of this file.
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);
size_t write(const char *str, const bool write_all=true)
Write a string, possibly blocking until all is written.
void close()
Calls close(2) on FDWrapper::_fd.
SystemCall("socketpair", ::socketpair(AF_UNIX, SOCK_STREAM, 0, fds.data()))
FDWrapper(const int fd)
Construct from a file descriptor number returned by the kernel.
void close()
Close the underlying file descriptor.
int fd_num() const
underlying descriptor number
A non-owning temporary view (similar to std::string_view) of a discontiguous string.
A reference-counted handle to a file descriptor.
std::string read(const size_t limit=std::numeric_limits< size_t >::max())
Read up to limit bytes.
void set_blocking(const bool blocking_state)
Set blocking(true) or non-blocking(false)
A handle on a kernel file descriptor.
FileDescriptor(std::shared_ptr< FDWrapper > other_shared_ptr)
Private constructor used by duplicate()
~FDWrapper()
Closes the file descriptor upon destruction.
std::shared_ptr< FDWrapper > _internal_fd
A reference-counted handle to a shared FDWrapper.
FileDescriptor duplicate() const
Copy a FileDescriptor explicitly, increasing the FDWrapper refcount.