Sponge
CS144's user-space TCP library
|
A reference-counted handle to a file descriptor. More...
#include <file_descriptor.hh>
Classes | |
class | FDWrapper |
A handle on a kernel file descriptor. More... | |
Public Member Functions | |
FileDescriptor (const int fd) | |
Construct from a file descriptor number returned by the kernel. More... | |
~FileDescriptor ()=default | |
Free the std::shared_ptr; the FDWrapper destructor calls close() when the refcount goes to zero. More... | |
void | close () |
Close the underlying file descriptor. More... | |
FileDescriptor | duplicate () const |
Copy a FileDescriptor explicitly, increasing the FDWrapper refcount. More... | |
std::string | read (const size_t limit=std::numeric_limits< size_t >::max()) |
Read up to limit bytes. More... | |
void | read (std::string &str, const size_t limit=std::numeric_limits< size_t >::max()) |
Read up to limit bytes into str (caller can allocate storage) More... | |
void | set_blocking (const bool blocking_state) |
Set blocking(true) or non-blocking(false) More... | |
size_t | write (BufferViewList buffer, const bool write_all=true) |
Write a buffer (or list of buffers), possibly blocking until all is written. More... | |
size_t | write (const char *str, const bool write_all=true) |
Write a string, possibly blocking until all is written. More... | |
size_t | write (const std::string &str, const bool write_all=true) |
Write a string, possibly blocking until all is written. More... | |
FDWrapper accessors | |
int | fd_num () const |
underlying descriptor number More... | |
bool | eof () const |
EOF flag state. More... | |
bool | closed () const |
closed flag state More... | |
unsigned int | read_count () const |
number of reads More... | |
unsigned int | write_count () const |
number of writes More... | |
Copy/move constructor/assignment operators | |
FileDescriptor can be moved, but cannot be copied (but see duplicate()) | |
FileDescriptor (const FileDescriptor &other)=delete | |
copy construction is forbidden More... | |
FileDescriptor & | operator= (const FileDescriptor &other)=delete |
copy assignment is forbidden More... | |
FileDescriptor (FileDescriptor &&other)=default | |
move construction is allowed More... | |
FileDescriptor & | operator= (FileDescriptor &&other)=default |
move assignment is allowed More... | |
Protected Member Functions | |
void | register_read () |
increment read count More... | |
void | register_write () |
increment write count More... | |
Private Member Functions | |
FileDescriptor (std::shared_ptr< FDWrapper > other_shared_ptr) | |
Private constructor used by duplicate() More... | |
Private Attributes | |
std::shared_ptr< FDWrapper > | _internal_fd |
A reference-counted handle to a shared FDWrapper. More... | |
A reference-counted handle to a file descriptor.
In addition, FileDescriptor tracks EOF state and calls to FileDescriptor::read and FileDescriptor::write, which EventLoop uses to detect busy loop conditions.
For an example of FileDescriptor use, see the EventLoop class documentation.
Definition at line 12 of file file_descriptor.hh.
|
explicitprivate |
Private constructor used by duplicate()
Definition at line 42 of file file_descriptor.cc.
|
explicit |
Construct from a file descriptor number returned by the kernel.
[in] | fd | is the file descriptor number returned by open(2) or similar |
Definition at line 39 of file file_descriptor.cc.
|
default |
Free the std::shared_ptr; the FDWrapper destructor calls close() when the refcount goes to zero.
|
delete |
copy construction is forbidden
|
default |
move construction is allowed
|
inline |
Close the underlying file descriptor.
Definition at line 74 of file file_descriptor.hh.
|
inline |
closed flag state
Definition at line 92 of file file_descriptor.hh.
FileDescriptor FileDescriptor::duplicate | ( | ) | const |
Copy a FileDescriptor explicitly, increasing the FDWrapper refcount.
Definition at line 45 of file file_descriptor.cc.
|
inline |
EOF flag state.
Definition at line 89 of file file_descriptor.hh.
|
inline |
underlying descriptor number
Definition at line 86 of file file_descriptor.hh.
|
delete |
copy assignment is forbidden
|
default |
move assignment is allowed
std::string FileDescriptor::read | ( | const size_t | limit = std::numeric_limits< size_t >::max() | ) |
Read up to limit
bytes.
void FileDescriptor::read | ( | std::string & | str, |
const size_t | limit = std::numeric_limits< size_t >::max() |
||
) |
Read up to limit
bytes into str
(caller can allocate storage)
|
inline |
number of reads
Definition at line 95 of file file_descriptor.hh.
|
inlineprotected |
increment read count
Definition at line 48 of file file_descriptor.hh.
|
inlineprotected |
increment write count
Definition at line 49 of file file_descriptor.hh.
void FileDescriptor::set_blocking | ( | const bool | blocking_state | ) |
Set blocking(true) or non-blocking(false)
Definition at line 101 of file file_descriptor.cc.
size_t FileDescriptor::write | ( | BufferViewList | buffer, |
const bool | write_all = true |
||
) |
Write a buffer (or list of buffers), possibly blocking until all is written.
Definition at line 76 of file file_descriptor.cc.
|
inline |
Write a string, possibly blocking until all is written.
Definition at line 65 of file file_descriptor.hh.
|
inline |
Write a string, possibly blocking until all is written.
Definition at line 68 of file file_descriptor.hh.
|
inline |
number of writes
Definition at line 98 of file file_descriptor.hh.
|
private |
A reference-counted handle to a shared FDWrapper.
Definition at line 42 of file file_descriptor.hh.