Sponge
CS144's user-space TCP library
byte_stream.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_BYTE_STREAM_HH
2 #define SPONGE_LIBSPONGE_BYTE_STREAM_HH
3 
4 #include <string>
5 
7 
11 class ByteStream {
12  private:
13  // Your code here -- add private members as necessary.
14 
15  // Hint: This doesn't need to be a sophisticated data structure at
16  // all, but if any of your tests are taking longer than a second,
17  // that's a sign that you probably want to keep exploring
18  // different approaches.
19 
20  bool _error{};
21 
22  public:
24  ByteStream(const size_t capacity);
25 
28 
32  size_t write(const std::string &data);
33 
35  size_t remaining_capacity() const;
36 
38  void end_input();
39 
41  void set_error() { _error = true; }
43 
46 
49  std::string peek_output(const size_t len) const;
50 
52  void pop_output(const size_t len);
53 
56  std::string read(const size_t len);
57 
59  bool input_ended() const;
60 
62  bool error() const { return _error; }
63 
65  size_t buffer_size() const;
66 
68  bool buffer_empty() const;
69 
71  bool eof() const;
73 
76 
78  size_t bytes_written() const;
79 
81  size_t bytes_read() const;
83 };
84 
85 #endif // SPONGE_LIBSPONGE_BYTE_STREAM_HH
ByteStream::bytes_written
size_t bytes_written() const
Total number of bytes written.
Definition: byte_stream.cc:49
std::string
ByteStream::eof
bool eof() const
Definition: byte_stream.cc:47
len
constexpr size_t len
Definition: tcp_benchmark.cc:12
ByteStream::pop_output
void pop_output(const size_t len)
Remove bytes from the buffer.
Definition: byte_stream.cc:29
ByteStream::end_input
void end_input()
Signal that the byte stream has reached its ending.
Definition: byte_stream.cc:39
ByteStream::set_error
void set_error()
Indicate that the stream suffered an error.
Definition: byte_stream.hh:41
ByteStream::buffer_size
size_t buffer_size() const
Definition: byte_stream.cc:43
ByteStream::read
std::string read(const size_t len)
Definition: byte_stream.cc:34
ByteStream
An in-order byte stream.
Definition: byte_stream.hh:11
ByteStream::peek_output
std::string peek_output(const size_t len) const
Definition: byte_stream.cc:23
ByteStream::bytes_read
size_t bytes_read() const
Total number of bytes popped.
Definition: byte_stream.cc:51
ByteStream::ByteStream
ByteStream(const size_t capacity)
Construct a stream with room for capacity bytes.
Definition: byte_stream.cc:15
ByteStream::write
size_t write(const std::string &data)
Definition: byte_stream.cc:17
ByteStream::error
bool error() const
Definition: byte_stream.hh:62
ByteStream::buffer_empty
bool buffer_empty() const
Definition: byte_stream.cc:45
ByteStream::remaining_capacity
size_t remaining_capacity() const
Definition: byte_stream.cc:53
ByteStream::_error
bool _error
Flag indicating that the stream suffered an error.
Definition: byte_stream.hh:20
ByteStream::input_ended
bool input_ended() const
Definition: byte_stream.cc:41