Sponge
CS144's user-space TCP library
file_descriptor.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_FILE_DESCRIPTOR_HH
2 #define SPONGE_LIBSPONGE_FILE_DESCRIPTOR_HH
3 
4 #include "buffer.hh"
5 
6 #include <array>
7 #include <cstddef>
8 #include <limits>
9 #include <memory>
10 
15  class FDWrapper {
16  public:
17  int _fd;
18  bool _eof = false;
19  bool _closed = false;
20  unsigned _read_count = 0;
21  unsigned _write_count = 0;
22 
24  explicit FDWrapper(const int fd);
26  ~FDWrapper();
28  void close();
29 
32 
34  FDWrapper(const FDWrapper &other) = delete;
35  FDWrapper &operator=(const FDWrapper &other) = delete;
36  FDWrapper(FDWrapper &&other) = delete;
37  FDWrapper &operator=(FDWrapper &&other) = delete;
39  };
40 
43 
44  // private constructor used to duplicate the FileDescriptor (increase the reference count)
45  explicit FileDescriptor(std::shared_ptr<FDWrapper> other_shared_ptr);
46 
47  protected:
48  void register_read() { ++_internal_fd->_read_count; }
49  void register_write() { ++_internal_fd->_write_count; }
50 
51  public:
53  explicit FileDescriptor(const int fd);
54 
56  ~FileDescriptor() = default;
57 
59  std::string read(const size_t limit = std::numeric_limits<size_t>::max());
60 
62  void read(std::string &str, const size_t limit = std::numeric_limits<size_t>::max());
63 
65  size_t write(const char *str, const bool write_all = true) { return write(BufferViewList(str), write_all); }
66 
68  size_t write(const std::string &str, const bool write_all = true) { return write(BufferViewList(str), write_all); }
69 
71  size_t write(BufferViewList buffer, const bool write_all = true);
72 
74  void close() { _internal_fd->close(); }
75 
77  FileDescriptor duplicate() const;
78 
80  void set_blocking(const bool blocking_state);
81 
84 
86  int fd_num() const { return _internal_fd->_fd; }
87 
89  bool eof() const { return _internal_fd->_eof; }
90 
92  bool closed() const { return _internal_fd->_closed; }
93 
95  unsigned int read_count() const { return _internal_fd->_read_count; }
96 
98  unsigned int write_count() const { return _internal_fd->_write_count; }
100 
104  FileDescriptor(const FileDescriptor &other) = delete;
105  FileDescriptor &operator=(const FileDescriptor &other) = delete;
106  FileDescriptor(FileDescriptor &&other) = default;
107  FileDescriptor &operator=(FileDescriptor &&other) = default;
108 };
110 
116 
117 #endif // SPONGE_LIBSPONGE_FILE_DESCRIPTOR_HH
std::string
std::shared_ptr
FileDescriptor::eof
bool eof() const
EOF flag state.
Definition: file_descriptor.hh:89
FileDescriptor::write
size_t write(const char *str, const bool write_all=true)
Write a string, possibly blocking until all is written.
Definition: file_descriptor.hh:65
FileDescriptor::FDWrapper::close
void close()
Calls close(2) on FDWrapper::_fd.
Definition: file_descriptor.cc:21
FileDescriptor::FDWrapper::FDWrapper
FDWrapper(const int fd)
Construct from a file descriptor number returned by the kernel.
Definition: file_descriptor.cc:15
FileDescriptor::close
void close()
Close the underlying file descriptor.
Definition: file_descriptor.hh:74
FileDescriptor::fd_num
int fd_num() const
underlying descriptor number
Definition: file_descriptor.hh:86
BufferViewList
A non-owning temporary view (similar to std::string_view) of a discontiguous string.
Definition: buffer.hh:97
FileDescriptor
A reference-counted handle to a file descriptor.
Definition: file_descriptor.hh:12
FileDescriptor::FDWrapper::_closed
bool _closed
Flag indicating whether FDWrapper::_fd has been closed.
Definition: file_descriptor.hh:19
FileDescriptor::read_count
unsigned int read_count() const
number of reads
Definition: file_descriptor.hh:95
FileDescriptor::read
std::string read(const size_t limit=std::numeric_limits< size_t >::max())
Read up to limit bytes.
FileDescriptor::FDWrapper::_write_count
unsigned _write_count
The numberof times FDWrapper::_fd has been written.
Definition: file_descriptor.hh:21
FileDescriptor::FDWrapper::_eof
bool _eof
Flag indicating whether FDWrapper::_fd is at EOF.
Definition: file_descriptor.hh:18
FileDescriptor::FDWrapper::operator=
FDWrapper & operator=(const FDWrapper &other)=delete
buffer
std::string buffer
Definition: parser_example.cc:7
FileDescriptor::operator=
FileDescriptor & operator=(const FileDescriptor &other)=delete
copy assignment is forbidden
FileDescriptor::set_blocking
void set_blocking(const bool blocking_state)
Set blocking(true) or non-blocking(false)
Definition: file_descriptor.cc:101
FileDescriptor::write_count
unsigned int write_count() const
number of writes
Definition: file_descriptor.hh:98
FileDescriptor::FDWrapper
A handle on a kernel file descriptor.
Definition: file_descriptor.hh:15
FileDescriptor::write
size_t write(const std::string &str, const bool write_all=true)
Write a string, possibly blocking until all is written.
Definition: file_descriptor.hh:68
buffer.hh
FileDescriptor::FileDescriptor
FileDescriptor(std::shared_ptr< FDWrapper > other_shared_ptr)
Private constructor used by duplicate()
Definition: file_descriptor.cc:42
FileDescriptor::FDWrapper::~FDWrapper
~FDWrapper()
Closes the file descriptor upon destruction.
Definition: file_descriptor.cc:26
FileDescriptor::FDWrapper::_read_count
unsigned _read_count
The number of times FDWrapper::_fd has been read.
Definition: file_descriptor.hh:20
FileDescriptor::FDWrapper::_fd
int _fd
The file descriptor number returned by the kernel.
Definition: file_descriptor.hh:17
FileDescriptor::_internal_fd
std::shared_ptr< FDWrapper > _internal_fd
A reference-counted handle to a shared FDWrapper.
Definition: file_descriptor.hh:42
FileDescriptor::~FileDescriptor
~FileDescriptor()=default
Free the std::shared_ptr; the FDWrapper destructor calls close() when the refcount goes to zero.
std::numeric_limits
FileDescriptor::duplicate
FileDescriptor duplicate() const
Copy a FileDescriptor explicitly, increasing the FDWrapper refcount.
Definition: file_descriptor.cc:45
FileDescriptor::closed
bool closed() const
closed flag state
Definition: file_descriptor.hh:92