From f2d8e050836d030a0227df264accce0d5722322b Mon Sep 17 00:00:00 2001 From: ridethepig Date: Wed, 15 Feb 2023 15:22:55 +0000 Subject: [PATCH] add comments for lab1 --- libsponge/stream_reassembler.cc | 16 ++++++++++------ libsponge/stream_reassembler.hh | 5 ++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/libsponge/stream_reassembler.cc b/libsponge/stream_reassembler.cc index 76c9a08..f4f5b5f 100644 --- a/libsponge/stream_reassembler.cc +++ b/libsponge/stream_reassembler.cc @@ -21,37 +21,41 @@ StreamReassembler::StreamReassembler(const size_t capacity) //! possibly out-of-order, from the logical stream, and assembles any newly //! 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) { - DUMMY_CODE(data, index, eof); - _unread = _unassem - _output.buffer_size(); + auto _unread = _unassem - _output.buffer_size(); _aux_buffer.resize(_capacity - _output.buffer_size(), '\0'); _aux_buffer_valid.resize(_aux_buffer.size(), false); + // resize to spare space for incoming bytes if (eof) { _eof = index + data.size(); + // record first, in case eof packet arrives ahead of non-eof ones } auto start = std::max(index, _unassem); 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) { _aux_buffer[i - _unassem] = data[i - index]; if (!_aux_buffer_valid[i - _unassem]) { _aux_buffer_valid[i - _unassem] = true; _unassem_count++; + // calculate unassembled bytes, this only counts valid bytes } } std::string wr_data; 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()); _aux_buffer.pop_front(); - _aux_buffer_valid.pop_front(); + _aux_buffer_valid.pop_front(); // remember to pop assistent queue ++_unassem; --_unassem_count; } - if (_unassem == _eof) { - _output.end_input(); - } if (wr_data.size()) { _output.write(wr_data); } + if (_unassem == _eof) { + _output.end_input(); // stream approached to eof + } } size_t StreamReassembler::unassembled_bytes() const { return _unassem_count; } diff --git a/libsponge/stream_reassembler.hh b/libsponge/stream_reassembler.hh index a1a3dfd..b6bb692 100644 --- a/libsponge/stream_reassembler.hh +++ b/libsponge/stream_reassembler.hh @@ -15,12 +15,11 @@ class StreamReassembler { ByteStream _output; //!< The reassembled in-order byte stream 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_count{}; + size_t _unassem_count{}; //!< Keep the number of unassembled bytes std::deque _aux_buffer; //!< auxiliary storage, initially allocated _capacity bytes std::deque _aux_buffer_valid; //!< indicate whether the bit in _aux_buffer is filled with a valid byte - size_t _eof{static_cast(-1)}; + size_t _eof{static_cast(-1)}; //!< indicate the eof byte's index in stream public: //! \brief Construct a `StreamReassembler` that will store up to `capacity` bytes.