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 
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;
109 };
110 
116 
117 #endif // SPONGE_LIBSPONGE_FILE_DESCRIPTOR_HH
A non-owning temporary view (similar to std::string_view) of a discontiguous string.
Definition: buffer.hh:98
A handle on a kernel file descriptor.
FDWrapper(FDWrapper &&other)=delete
bool _eof
Flag indicating whether FDWrapper::_fd is at EOF.
bool _closed
Flag indicating whether FDWrapper::_fd has been closed.
FDWrapper(const int fd)
Construct from a file descriptor number returned by the kernel.
void close()
Calls close(2) on FDWrapper::_fd.
int _fd
The file descriptor number returned by the kernel.
unsigned _write_count
The numberof times FDWrapper::_fd has been written.
unsigned _read_count
The number of times FDWrapper::_fd has been read.
FDWrapper(const FDWrapper &other)=delete
~FDWrapper()
Closes the file descriptor upon destruction.
FDWrapper & operator=(const FDWrapper &other)=delete
FDWrapper & operator=(FDWrapper &&other)=delete
A reference-counted handle to a file descriptor.
void close()
Close the underlying file descriptor.
~FileDescriptor()=default
Free the std::shared_ptr; the FDWrapper destructor calls close() when the refcount goes to zero.
unsigned int read_count() const
number of reads
bool closed() const
closed flag state
FileDescriptor(FileDescriptor &&other)=default
move construction is allowed
FileDescriptor duplicate() const
Copy a FileDescriptor explicitly, increasing the FDWrapper refcount.
unsigned int write_count() const
number of writes
bool eof() const
EOF flag state.
int fd_num() const
underlying descriptor number
FileDescriptor & operator=(const FileDescriptor &other)=delete
copy assignment is forbidden
void register_read()
increment read count
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)
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 & operator=(FileDescriptor &&other)=default
move assignment is allowed
FileDescriptor(const FileDescriptor &other)=delete
copy construction is forbidden
FileDescriptor(std::shared_ptr< FDWrapper > other_shared_ptr)
Private constructor used by duplicate()
void register_write()
increment write count
size_t write(const std::string &str, const bool write_all=true)
Write a string, possibly blocking until all is written.
std::string buffer