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

A class that assembles a series of excerpts from a byte stream (possibly out of order, possibly overlapping) into an in-order byte stream. More...

#include <stream_reassembler.hh>

Public Member Functions

 StreamReassembler (const size_t capacity)
 Construct a StreamReassembler that will store up to capacity bytes. More...
 
bool empty () const
 Is the internal state empty (other than the output stream)? More...
 
void push_substring (const std::string &data, const uint64_t index, const bool eof)
 Receive a substring and write any newly contiguous bytes into the stream. More...
 
size_t unassembled_bytes () const
 
Access the reassembled byte stream
const ByteStreamstream_out () const
 
ByteStreamstream_out ()
 

Private Attributes

size_t _capacity
 The maximum number of bytes. More...
 
ByteStream _output
 The reassembled in-order byte stream. More...
 

Detailed Description

A class that assembles a series of excerpts from a byte stream (possibly out of order, possibly overlapping) into an in-order byte stream.

Definition at line 11 of file stream_reassembler.hh.

Constructor & Destructor Documentation

◆ StreamReassembler()

StreamReassembler::StreamReassembler ( const size_t  capacity)

Construct a StreamReassembler that will store up to capacity bytes.

Note
This capacity limits both the bytes that have been reassembled, and those that have not yet been reassembled.

Definition at line 15 of file stream_reassembler.cc.

Member Function Documentation

◆ empty()

bool StreamReassembler::empty ( ) const

Is the internal state empty (other than the output stream)?

Returns
true if no substrings are waiting to be assembled

Definition at line 26 of file stream_reassembler.cc.

◆ push_substring()

void StreamReassembler::push_substring ( const std::string data,
const uint64_t  index,
const bool  eof 
)

Receive a substring and write any newly contiguous bytes into the stream.

The StreamReassembler will stay within the memory limits of the capacity. Bytes that would exceed the capacity are silently discarded.

Parameters
datathe substring
indexindicates the index (place in sequence) of the first byte in data
eofthe last byte of data will be the last byte in the entire stream

This function accepts a substring (aka a segment) of bytes, possibly out-of-order, from the logical stream, and assembles any newly contiguous substrings and writes them into the output stream in order.

Definition at line 20 of file stream_reassembler.cc.

◆ stream_out() [1/2]

ByteStream& StreamReassembler::stream_out ( )
inline

Definition at line 37 of file stream_reassembler.hh.

◆ stream_out() [2/2]

const ByteStream& StreamReassembler::stream_out ( ) const
inline

Definition at line 36 of file stream_reassembler.hh.

◆ unassembled_bytes()

size_t StreamReassembler::unassembled_bytes ( ) const

The number of bytes in the substrings stored but not yet reassembled

Note
If the byte at a particular index has been pushed more than once, it should only be counted once for the purpose of this function.

Definition at line 24 of file stream_reassembler.cc.

Member Data Documentation

◆ _capacity

size_t StreamReassembler::_capacity
private

The maximum number of bytes.

Definition at line 16 of file stream_reassembler.hh.

◆ _output

ByteStream StreamReassembler::_output
private

The reassembled in-order byte stream.

Definition at line 15 of file stream_reassembler.hh.


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