CS144 lab1 lab1具体的相关事宜可以查看博客:https://kiprey.github.io/2021/11/cs144-lab1/ 完整项目代码: CS144StreamReassembler实现 StreamReassembler 类需要完成对底层发送来的数据的顺序进行正确的重组,如下图: img 具体来说你只需要实现下列的方法:class StreamReassembler { private: // Your code here -- add private members as necessary. size_t _unass_base; //!< The index of the first unassembled byte size_t _unass_size; //!< The number of bytes in the substrings stored but //!< not yet reassembled bool _eof; //!< The last byte has arrived std::string _buffer; //!< The unassembled strings std::vector<bool> _bitmap; //!< buffer bitmap ByteStream _output; //!< The reassembled in-order byte stream size_t _capacity; //!< The maximum number of bytes public: explicit StreamReassembler(size_t capacity); void push_substring(const std::string &data, uint64_t index, bool eof); [[nodiscard]] const ByteStream &stream_out() const { return _output; } ByteStream &stream_out() { return _output; } size_t unassembled_bytes() const; bool empty() const; size_t ack_index() const; }; 方法有很多,具体最重要的方法是 push_substring(const string& data,uint64_t index,bool eof) ,该方法传入需要重排的数据,以及这个数据开始位置的绝对序号(index),还有表示是否是最后一块数据(eof). 我这里实现重排的逻辑如下: 首先需要一个 bitmap 记录每个序号的数据是否已经存入需要被重排的相应位置中,这个结构记录的数据作用有二:对应序号的数据是否被加入重排队列.已经重排的数据的下一个序号是否存在连续可重排数据. 第一个作用大家都能理解,那么第二个作用是什么意思呢? 每次把数据push进去之后,需要判断是否存在一个连续的需要重排的数据,且这个连续数据的第一个位置是已经重排数据的下一个位置,如果存在,那么需要写入已重排的缓冲区中. 整个数据重排的过程可以用下图进行表示: img具体实现 我们先设计如下成员变量: size_t _unass_base; //!< The index of the first unassembled byte size_t _unass_size; //!< The number of bytes in the substrings stored but //!< not yet reassembled bool _eof; //!< The last byte has arrived std::string _buffer; //!< The unassembled strings std::vector<bool> _bitmap; //!< buffer bitmap ByteStream _output; //!< The reassembled in-order byte stream size_t _capacity; //!< The maximum number of bytes _unass_base 变量用于表示当前已经重排过多少数据,该值可以结合用户传入的 index 算出用户的数据在待重排队列中的 offset ._unass_size 变量表示待重排数据的长度._eof 表示传入的数据块是最后一个._buffer 是待重排的队列._output 是已重排的队列._capacity 待重排和已重拍队列的最大容量,且待重排和已重拍(未读取)…