Sponge
CS144's user-space TCP library
Classes | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
FileDescriptor Class Reference

A reference-counted handle to a file descriptor. More...

#include <file_descriptor.hh>

Inheritance diagram for FileDescriptor:
Inheritance graph
[legend]

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...
 
FileDescriptoroperator= (const FileDescriptor &other)=delete
 copy assignment is forbidden More...
 
 FileDescriptor (FileDescriptor &&other)=default
 move construction is allowed More...
 
FileDescriptoroperator= (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...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ FileDescriptor() [1/4]

FileDescriptor::FileDescriptor ( std::shared_ptr< FDWrapper other_shared_ptr)
explicitprivate

Private constructor used by duplicate()

Definition at line 42 of file file_descriptor.cc.

◆ FileDescriptor() [2/4]

FileDescriptor::FileDescriptor ( const int  fd)
explicit

Construct from a file descriptor number returned by the kernel.

Parameters
[in]fdis the file descriptor number returned by open(2) or similar

Definition at line 39 of file file_descriptor.cc.

◆ ~FileDescriptor()

FileDescriptor::~FileDescriptor ( )
default

Free the std::shared_ptr; the FDWrapper destructor calls close() when the refcount goes to zero.

◆ FileDescriptor() [3/4]

FileDescriptor::FileDescriptor ( const FileDescriptor other)
delete

copy construction is forbidden

◆ FileDescriptor() [4/4]

FileDescriptor::FileDescriptor ( FileDescriptor &&  other)
default

move construction is allowed

Member Function Documentation

◆ close()

void FileDescriptor::close ( )
inline

Close the underlying file descriptor.

Definition at line 74 of file file_descriptor.hh.

◆ closed()

bool FileDescriptor::closed ( ) const
inline

closed flag state

Definition at line 92 of file file_descriptor.hh.

◆ duplicate()

FileDescriptor FileDescriptor::duplicate ( ) const

Copy a FileDescriptor explicitly, increasing the FDWrapper refcount.

Returns
a copy of this FileDescriptor

Definition at line 45 of file file_descriptor.cc.

◆ eof()

bool FileDescriptor::eof ( ) const
inline

EOF flag state.

Definition at line 89 of file file_descriptor.hh.

◆ fd_num()

int FileDescriptor::fd_num ( ) const
inline

underlying descriptor number

Definition at line 86 of file file_descriptor.hh.

◆ operator=() [1/2]

FileDescriptor& FileDescriptor::operator= ( const FileDescriptor other)
delete

copy assignment is forbidden

◆ operator=() [2/2]

FileDescriptor& FileDescriptor::operator= ( FileDescriptor &&  other)
default

move assignment is allowed

◆ read() [1/2]

std::string FileDescriptor::read ( const size_t  limit = std::numeric_limits< size_t >::max())

Read up to limit bytes.

◆ read() [2/2]

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)

◆ read_count()

unsigned int FileDescriptor::read_count ( ) const
inline

number of reads

Definition at line 95 of file file_descriptor.hh.

◆ register_read()

void FileDescriptor::register_read ( )
inlineprotected

increment read count

Definition at line 48 of file file_descriptor.hh.

◆ register_write()

void FileDescriptor::register_write ( )
inlineprotected

increment write count

Definition at line 49 of file file_descriptor.hh.

◆ set_blocking()

void FileDescriptor::set_blocking ( const bool  blocking_state)

Set blocking(true) or non-blocking(false)

Definition at line 101 of file file_descriptor.cc.

◆ write() [1/3]

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.

◆ write() [2/3]

size_t FileDescriptor::write ( const char *  str,
const bool  write_all = true 
)
inline

Write a string, possibly blocking until all is written.

Definition at line 65 of file file_descriptor.hh.

◆ write() [3/3]

size_t FileDescriptor::write ( const std::string str,
const bool  write_all = true 
)
inline

Write a string, possibly blocking until all is written.

Definition at line 68 of file file_descriptor.hh.

◆ write_count()

unsigned int FileDescriptor::write_count ( ) const
inline

number of writes

Definition at line 98 of file file_descriptor.hh.

Member Data Documentation

◆ _internal_fd

std::shared_ptr<FDWrapper> FileDescriptor::_internal_fd
private

A reference-counted handle to a shared FDWrapper.

Definition at line 42 of file file_descriptor.hh.


The documentation for this class was generated from the following files: