add comments for lab1
This commit is contained in:
parent
9295c107ef
commit
f2d8e05083
@ -21,37 +21,41 @@ StreamReassembler::StreamReassembler(const size_t capacity)
|
|||||||
//! possibly out-of-order, from the logical stream, and assembles any newly
|
//! possibly out-of-order, from the logical stream, and assembles any newly
|
||||||
//! contiguous substrings and writes them into the output stream in order.
|
//! contiguous substrings and writes them into the output stream in order.
|
||||||
void StreamReassembler::push_substring(const string &data, const size_t index, const bool eof) {
|
void StreamReassembler::push_substring(const string &data, const size_t index, const bool eof) {
|
||||||
DUMMY_CODE(data, index, eof);
|
auto _unread = _unassem - _output.buffer_size();
|
||||||
_unread = _unassem - _output.buffer_size();
|
|
||||||
_aux_buffer.resize(_capacity - _output.buffer_size(), '\0');
|
_aux_buffer.resize(_capacity - _output.buffer_size(), '\0');
|
||||||
_aux_buffer_valid.resize(_aux_buffer.size(), false);
|
_aux_buffer_valid.resize(_aux_buffer.size(), false);
|
||||||
|
// resize to spare space for incoming bytes
|
||||||
|
|
||||||
if (eof) {
|
if (eof) {
|
||||||
_eof = index + data.size();
|
_eof = index + data.size();
|
||||||
|
// record first, in case eof packet arrives ahead of non-eof ones
|
||||||
}
|
}
|
||||||
auto start = std::max(index, _unassem);
|
auto start = std::max(index, _unassem);
|
||||||
auto end = std::min(index + data.size(), _unread + _capacity);
|
auto end = std::min(index + data.size(), _unread + _capacity);
|
||||||
|
// it is possible that index < _unassem (overlap with packets that have already been pushed into bytestream)
|
||||||
for (auto i = start; i < end; ++i) {
|
for (auto i = start; i < end; ++i) {
|
||||||
_aux_buffer[i - _unassem] = data[i - index];
|
_aux_buffer[i - _unassem] = data[i - index];
|
||||||
if (!_aux_buffer_valid[i - _unassem]) {
|
if (!_aux_buffer_valid[i - _unassem]) {
|
||||||
_aux_buffer_valid[i - _unassem] = true;
|
_aux_buffer_valid[i - _unassem] = true;
|
||||||
_unassem_count++;
|
_unassem_count++;
|
||||||
|
// calculate unassembled bytes, this only counts valid bytes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::string wr_data;
|
std::string wr_data;
|
||||||
while (!_aux_buffer_valid.empty() && _aux_buffer_valid.front()) {
|
while (!_aux_buffer_valid.empty() && _aux_buffer_valid.front()) {
|
||||||
|
// watch out for fetching front from an empty queue
|
||||||
wr_data.push_back(_aux_buffer.front());
|
wr_data.push_back(_aux_buffer.front());
|
||||||
_aux_buffer.pop_front();
|
_aux_buffer.pop_front();
|
||||||
_aux_buffer_valid.pop_front();
|
_aux_buffer_valid.pop_front(); // remember to pop assistent queue
|
||||||
++_unassem;
|
++_unassem;
|
||||||
--_unassem_count;
|
--_unassem_count;
|
||||||
}
|
}
|
||||||
if (_unassem == _eof) {
|
|
||||||
_output.end_input();
|
|
||||||
}
|
|
||||||
if (wr_data.size()) {
|
if (wr_data.size()) {
|
||||||
_output.write(wr_data);
|
_output.write(wr_data);
|
||||||
}
|
}
|
||||||
|
if (_unassem == _eof) {
|
||||||
|
_output.end_input(); // stream approached to eof
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t StreamReassembler::unassembled_bytes() const { return _unassem_count; }
|
size_t StreamReassembler::unassembled_bytes() const { return _unassem_count; }
|
||||||
|
|||||||
@ -15,12 +15,11 @@ class StreamReassembler {
|
|||||||
|
|
||||||
ByteStream _output; //!< The reassembled in-order byte stream
|
ByteStream _output; //!< The reassembled in-order byte stream
|
||||||
size_t _capacity{}; //!< The maximum number of bytes
|
size_t _capacity{}; //!< The maximum number of bytes
|
||||||
size_t _unread{}; //!< The index (in the stream) of the first unread byte
|
|
||||||
size_t _unassem{}; //!< The index (in the stream) of the first unassembled byte
|
size_t _unassem{}; //!< The index (in the stream) of the first unassembled byte
|
||||||
size_t _unassem_count{};
|
size_t _unassem_count{}; //!< Keep the number of unassembled bytes
|
||||||
std::deque<char> _aux_buffer; //!< auxiliary storage, initially allocated _capacity bytes
|
std::deque<char> _aux_buffer; //!< auxiliary storage, initially allocated _capacity bytes
|
||||||
std::deque<bool> _aux_buffer_valid; //!< indicate whether the bit in _aux_buffer is filled with a valid byte
|
std::deque<bool> _aux_buffer_valid; //!< indicate whether the bit in _aux_buffer is filled with a valid byte
|
||||||
size_t _eof{static_cast<size_t>(-1)};
|
size_t _eof{static_cast<size_t>(-1)}; //!< indicate the eof byte's index in stream
|
||||||
|
|
||||||
public:
|
public:
|
||||||
//! \brief Construct a `StreamReassembler` that will store up to `capacity` bytes.
|
//! \brief Construct a `StreamReassembler` that will store up to `capacity` bytes.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user