Sponge
CS144's user-space TCP library
|
An in-order byte stream. More...
#include <byte_stream.hh>
Public Member Functions | |
ByteStream (const size_t capacity) | |
Construct a stream with room for capacity bytes. More... | |
"Input" interface for the writer | |
size_t | write (const std::string &data) |
size_t | remaining_capacity () const |
void | end_input () |
Signal that the byte stream has reached its ending. More... | |
void | set_error () |
Indicate that the stream suffered an error. More... | |
"Output" interface for the reader | |
std::string | peek_output (const size_t len) const |
void | pop_output (const size_t len) |
Remove bytes from the buffer. More... | |
std::string | read (const size_t len) |
bool | input_ended () const |
bool | error () const |
size_t | buffer_size () const |
bool | buffer_empty () const |
bool | eof () const |
General accounting | |
size_t | bytes_written () const |
Total number of bytes written. More... | |
size_t | bytes_read () const |
Total number of bytes popped. More... | |
Private Attributes | |
bool | _error {} |
Flag indicating that the stream suffered an error. More... | |
An in-order byte stream.
Bytes are written on the "input" side and read from the "output" side. The byte stream is finite: the writer can end the input, and then no more bytes can be written.
Definition at line 11 of file byte_stream.hh.
ByteStream::ByteStream | ( | const size_t | capacity | ) |
Construct a stream with room for capacity
bytes.
Definition at line 15 of file byte_stream.cc.
bool ByteStream::buffer_empty | ( | ) | const |
true
if the buffer is empty Definition at line 45 of file byte_stream.cc.
size_t ByteStream::buffer_size | ( | ) | const |
Definition at line 43 of file byte_stream.cc.
size_t ByteStream::bytes_read | ( | ) | const |
Total number of bytes popped.
Definition at line 51 of file byte_stream.cc.
size_t ByteStream::bytes_written | ( | ) | const |
Total number of bytes written.
Definition at line 49 of file byte_stream.cc.
void ByteStream::end_input | ( | ) |
Signal that the byte stream has reached its ending.
Definition at line 39 of file byte_stream.cc.
bool ByteStream::eof | ( | ) | const |
true
if the output has reached the ending Definition at line 47 of file byte_stream.cc.
|
inline |
true
if the stream has suffered an error Definition at line 62 of file byte_stream.hh.
bool ByteStream::input_ended | ( | ) | const |
true
if the stream input has ended Definition at line 41 of file byte_stream.cc.
Peek at next "len" bytes of the stream
[in] | len | bytes will be copied from the output side of the buffer |
Definition at line 23 of file byte_stream.cc.
void ByteStream::pop_output | ( | const size_t | len | ) |
Remove bytes from the buffer.
[in] | len | bytes will be removed from the output side of the buffer |
Definition at line 29 of file byte_stream.cc.
std::string ByteStream::read | ( | const size_t | len | ) |
Read (i.e., copy and then pop) the next "len" bytes of the stream
Read (i.e., copy and then pop) the next "len" bytes of the stream
[in] | len | bytes will be popped and returned |
Definition at line 34 of file byte_stream.cc.
size_t ByteStream::remaining_capacity | ( | ) | const |
Definition at line 53 of file byte_stream.cc.
|
inline |
Indicate that the stream suffered an error.
Definition at line 41 of file byte_stream.hh.
size_t ByteStream::write | ( | const std::string & | data | ) |
Write a string of bytes into the stream. Write as many as will fit, and return how many were written.
Definition at line 17 of file byte_stream.cc.
|
private |
Flag indicating that the stream suffered an error.
Definition at line 20 of file byte_stream.hh.