一.实验目的:
TCP创建了一种可靠数据传输服务,其确保了一个进程从缓存中读出的数据流是无损坏,无间隙,非冗余和按序的数据流。
在lab1中,将实现一个流重组器——一个将字节流的小段(称为字符串或段)缝合回正确序列的连续的字节流的模块。
二.代码:
stream_reassembler.hh
#ifndef SPONGE_LIBSPONGE_STREAM_REASSEMBLER_HH
#define SPONGE_LIBSPONGE_STREAM_REASSEMBLER_HH
#include "byte_stream.hh"
#include <iostream>
#include <cstdint>
#include <string>
#include <set>
//! \brief A class that assembles a series of excerpts from a byte stream (possibly out of order,
//! possibly overlapping) into an in-order byte stream.
class StreamReassembler {
private:
// Your code here -- add private members as necessary.
std::set<pair<uint64_t, string> > _blocks{};
size_t _unassembled_bytes = 0;
size_t _head_index = 0;
bool _eof{false};
ByteStream _output; //!< The reassembled in-order byte stream
size_t _capacity; //!< The maximum number of bytes
pair<uint64_t, string> merge_node(pair<uint64_t, string> a, pair<uint64_t, string> b);
public:
//! \brief 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.