国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

CS 144 Lab One -- 流重組器

這篇具有很好參考價值的文章主要介紹了CS 144 Lab One -- 流重組器。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。


對應課程視頻: 【計算機網(wǎng)絡】 斯坦福大學CS144課程

Lab 1 對應的PDF: Lab Checkpoint 1: stitching substrings into a byte stream


實驗結(jié)構(gòu)

這幅圖完整的說明了CS144 這門實驗的結(jié)構(gòu):
CS 144 Lab One -- 流重組器,# CS 144 & MIT 6.829,網(wǎng)絡
其中, ByteStream 是我們已經(jīng)在 Lab0 中實現(xiàn)完成的。

我們將在接下來的實驗中分別實現(xiàn):

  • Lab1 StreamReassembler:實現(xiàn)一個流重組器,一個將字節(jié)流的字串或者小段按照正確順序來拼接回連續(xù)字節(jié)流的模塊
  • Lab2 TCPReceiver:實現(xiàn)入站字節(jié)流的TCP部分。
  • Lab3 TCPSender:實現(xiàn)出站字節(jié)流的TCP部分。
  • Lab4 TCPConnection: 結(jié)合之前的工作來創(chuàng)建一個有效的 TCP 實現(xiàn)。最后我們可以使用這個 TCP 實現(xiàn)來和真實世界的服務器進行通信。

該實驗引導我們以模塊化的方式構(gòu)建一個 TCP 實現(xiàn)。

流重組器在 TCP 起到了相當重要的作用。迫于網(wǎng)絡環(huán)境的限制,TCP 發(fā)送者會將數(shù)據(jù)切割成一個個小段的數(shù)據(jù)分批發(fā)送。但這就可能帶來一些新的問題:數(shù)據(jù)在網(wǎng)絡中傳輸時可能丟失、重排、多次重傳等等。而TCP接收者就必須通過流重組器,將接收到的這些重排重傳等等的數(shù)據(jù)包重新組裝成新的連續(xù)字節(jié)流。


如何調(diào)試

先 cmake && make 一個 Debug 版本的程序。

所有的評測程序位于build/tests/中,先一個個手動執(zhí)行過去。

若輸出了錯誤信息,則使用 gdb 調(diào)試一下。


StreamReassembler 實現(xiàn)

在我們所實現(xiàn)的流重組器中,有以下幾種特性:

  • 接收子字符串。這些子字符串中包含了一串字節(jié),以及該字符串在總的數(shù)據(jù)流中的第一個字節(jié)的索引。

  • 流的每個字節(jié)都有自己唯一的索引,從零開始向上計數(shù)。

  • StreamReassembler 中存在一個 ByteStream 用于輸出,當重組器知道了流的下一個字節(jié),它就會將其寫入至 ByteStream中。

需要注意的是,傳入的子串中:

  • 子串之間可能相互重復,存在重疊部分

    • 但假設重疊部分數(shù)據(jù)完全重復。
    • 不存在某些 index 下的數(shù)據(jù)在某個子串中是一種數(shù)據(jù),在另一個子串里又是另一種數(shù)據(jù)。
    • 重疊部分的處理最為麻煩。
  • 可能會傳一些已經(jīng)被裝配了的數(shù)據(jù)

  • 如果 ByteStream 已滿,則必須暫停裝配,將未裝配數(shù)據(jù)暫時保存起來

除了上面的要求以外,容量 Capacity 需要嚴格限制:

CS 144 Lab One -- 流重組器,# CS 144 & MIT 6.829,網(wǎng)絡
為了便于說明,將圖中的綠色區(qū)域稱為 ByteStream,將圖中存放紅色區(qū)域的內(nèi)存范圍(即 first unassembled - first unacceptable)稱為 Unassembled_strs。

CS144 要求將 ByteStream + Unassembled_strs 的內(nèi)存占用總和限制在 Reassember 中構(gòu)造函數(shù)傳入的 capacity 大小。因此我們在構(gòu)造 Reassembler 時,需要既將傳入的 capacity 參數(shù)設置為 ByteStream的緩沖區(qū)大小上限,也將其設置為first unassembled - first unacceptable的范圍大小,以避免極端情況下的內(nèi)存使用。

注意:first unassembled - first unacceptable的范圍大小,并不等同于存放尚未裝配子串的結(jié)構(gòu)體內(nèi)存大小上限,別混淆了。

Capacity 這個概念很重要,因為它不僅用于限制高內(nèi)存占用,而且它還會起到流量控制的作用(見 lab2)。

代碼實現(xiàn)如下:

  • stream_reassembler.hh
//! \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.
    struct Datum {
        char ch = 0;
        bool valid = false;
    };
    // 用于存放未按序達到的字節(jié)流
    std::vector<Datum> buffer_;  // buffer to store unassembled data.
    size_t buffer_header_;       // pointer to the begining of the unssembled bytes in
                                 // buffer.
    size_t unassembled_bytes_;
    size_t eof_byte_;
    bool is_eof_set_;    // true if read the eof
    // 存放按序到達的字節(jié)流,但是這部分字節(jié)流還沒有被read
    ByteStream _output;  //!< The reassembled in-order byte stream
    size_t _capacity;    //!< The maximum number of bytes

  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.
    StreamReassembler(const size_t capacity);

    //! \brief 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.
    //!
    //! \param data the substring
    //! \param index indicates the index (place in sequence) of the first
    //! byte in `data` \param eof the last byte of `data` will be the last
    //! byte in the entire stream
    void push_substring(const std::string &data, const uint64_t index, const bool eof);

    //! \name Access the reassembled byte stream
    //!@{
    const ByteStream &stream_out() const { return _output; }
    ByteStream &stream_out() { return _output; }
    //!@}

    //! 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.
    size_t unassembled_bytes() const;

    //! \brief Is the internal state empty (other than the output stream)?
    //! \returns `true` if no substrings are waiting to be assembled
    bool empty() const;
};
  • stream_reassembler.cc
#include "stream_reassembler.hh"

#include <cassert>

// For Lab 1, please replace with a real implementation that passes the
// automated checks run by `make check_lab1`.

// You will need to add private members to the class declaration in
// `stream_reassembler.hh`

using namespace std;

StreamReassembler::StreamReassembler(const size_t capacity)
    : buffer_(capacity)
    , buffer_header_(0)
    , unassembled_bytes_(0)
    , eof_byte_(0)
    , is_eof_set_(false)
    , _output(capacity)
    , _capacity(capacity) {}

//! \details 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.
void StreamReassembler::push_substring(const string &data, const size_t index, const bool eof) {
    if (_output.input_ended())
        return;
    const size_t max_byte = _output.bytes_read() + _capacity;
    const size_t index_start = std::max(_output.bytes_written(), index);
    size_t index_end = std::min(max_byte, index + data.length());
    // check for eof
    if (eof) {
        is_eof_set_ = true;
        eof_byte_ = index + data.length();
    }
    if (is_eof_set_)
        index_end = std::min(index_end, eof_byte_);

    // buffer the data
    for (size_t write_index = index_start; write_index < index_end; write_index++) {
        size_t cache_index = (buffer_header_ + write_index - _output.bytes_written()) % _capacity;
        assert(!(buffer_[cache_index].valid && buffer_[cache_index].ch != data[write_index - index]));
        if (!buffer_[cache_index].valid)
            unassembled_bytes_++;
        buffer_[cache_index].valid = true;
        buffer_[cache_index].ch = data[write_index - index];
    }

    // write the data.
    while (buffer_[buffer_header_].valid) {
        buffer_[buffer_header_].valid = false;
        _output.write_char(buffer_[buffer_header_].ch);
        unassembled_bytes_--;
        buffer_header_ = (buffer_header_ + 1) % _capacity;
    }
    if (is_eof_set_ && _output.bytes_written() >= eof_byte_)
        _output.end_input();
}

size_t StreamReassembler::unassembled_bytes() const { return unassembled_bytes_; }

bool StreamReassembler::empty() const { return unassembled_bytes_ == 0; }

代碼可能不太容易理解,但是大家對照下圖,把幾種可能出現(xiàn)的情況看明白,再回去看代碼,相信就不難了:

CS 144 Lab One -- 流重組器,# CS 144 &amp; MIT 6.829,網(wǎng)絡
核心一點: buffer用于暫存未按序到達的這部分不連續(xù)的字節(jié)流,而output用于存放按序到達的這部分字節(jié)流,但是這段字節(jié)流還沒有被read。

測試代碼正確性:
CS 144 Lab One -- 流重組器,# CS 144 &amp; MIT 6.829,網(wǎng)絡文章來源地址http://www.zghlxwxcb.cn/news/detail-595432.html

到了這里,關于CS 144 Lab One -- 流重組器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • CS144 計算機網(wǎng)絡 Lab2:TCP Receiver

    CS144 計算機網(wǎng)絡 Lab2:TCP Receiver

    Lab1 中我們使用雙端隊列實現(xiàn)了字節(jié)流重組器,可以將無序到達的數(shù)據(jù)重組為有序的字節(jié)流。Lab2 將在此基礎上實現(xiàn) TCP Receiver,在收到報文段之后將數(shù)據(jù)寫入重組器中,并回復發(fā)送方。 TCP 接收方除了將收到的數(shù)據(jù)寫入重組器中外,還需要告訴發(fā)送發(fā)送方: 下一個需要的但是

    2023年04月25日
    瀏覽(18)
  • CS144 計算機網(wǎng)絡 Lab0:Networking Warmup

    CS144 計算機網(wǎng)絡 Lab0:Networking Warmup

    本科期間修讀了《計算機網(wǎng)絡》課程,但是課上布置的作業(yè)比較簡單,只是分析了一下 Wireshark 抓包的結(jié)構(gòu),沒有動手實現(xiàn)過協(xié)議。所以最近在嗶哩大學在線學習了斯坦福大學的 CS144 計算機網(wǎng)課程,這門課搭配了幾個 Lab,要求動手實現(xiàn)一個 TCP 協(xié)議,而不是簡單地調(diào)用系統(tǒng)為

    2023年04月18日
    瀏覽(30)
  • CS144-Lab6

    在本周的實驗中,你將在現(xiàn)有的 NetworkInterface 基礎上實現(xiàn)一個IP路由器,從而結(jié)束本課程。路由器有幾個網(wǎng)絡接口,可以在其中任何一個接口上接收互聯(lián)網(wǎng)數(shù)據(jù)報。路由器的工作是根據(jù) 路由表 轉(zhuǎn)發(fā)它得到的數(shù)據(jù)報:一個規(guī)則列表,它告訴路由器,對于任何給定的數(shù)據(jù)報: 發(fā)

    2024年02月09日
    瀏覽(18)
  • CS144--Lab1筆記

    CS144--Lab1筆記

    CS144——Lab1筆記 作為使用了版本管理的項目,開始新的開發(fā),當然先要新建一個開發(fā)分支啦,可以用命令或者直接在IDE中GIT的圖形控制界面操作,太簡單就不細說。(我習慣命名:dev-lab1) 首先要從原始倉庫合并Lab1的相關文件到本地倉庫,接著在build目錄下編譯。執(zhí)行下面的

    2024年02月20日
    瀏覽(20)
  • CS 144 Lab Four -- the TCP connection

    CS 144 Lab Four -- the TCP connection

    對應課程視頻: 【計算機網(wǎng)絡】 斯坦福大學CS144課程 Lab Four 對應的PDF: Lab Checkpoint 4: down the stack (the network interface) TCPConnection 需要將 TCPSender 和 TCPReceiver 結(jié)合,實現(xiàn)成一個 TCP 終端,同時收發(fā)數(shù)據(jù)。 TCPConnection 有幾個規(guī)則需要遵守: 對于 接收數(shù)據(jù)段 而言: 如果接收到的數(shù)據(jù)包

    2024年02月13日
    瀏覽(19)
  • CS 144 Lab Six -- building an IP router

    CS 144 Lab Six -- building an IP router

    對應課程視頻: 【計算機網(wǎng)絡】 斯坦福大學CS144課程 Lab Six 對應的PDF: Lab Checkpoint 5: building an IP router 在本實驗中,你將在現(xiàn)有的 NetworkInterface 基礎上實現(xiàn)一個IP路由器,從而結(jié)束本課程。路由器有幾個網(wǎng)絡接口,可以在其中任何一個接口上接收互聯(lián)網(wǎng)數(shù)據(jù)報。路由器的工作是根

    2024年02月14日
    瀏覽(17)
  • CS144(2023 Spring)Lab 0:networking warmup(環(huán)境搭建 & webget & bytestream)

    CS144(2023 Spring)Lab 0:networking warmup(環(huán)境搭建 & webget & bytestream)

    最近心情非常郁悶,搓一個CS144玩玩吧,正好2023 spring出新版了。。。CS144的頭4個Lab(加上0是5個),一步步實現(xiàn)了一個TCP。在開始之前,我想貼一下Lab中的這句話: The lab documents aren’t “specifications”—meaning they’re not intended to be consumed in a one-way fashion. They’re written closer

    2024年02月11日
    瀏覽(17)
  • mit 6.824 lab1分析

    mit 6.824 lab1分析

    略 map階段每個worker應該把中間文件分成nReduce份,nReduce是reduce任務的數(shù)量 worker完成reduce任務后生成文件名 mr-out-X mr-out-X 文件每行應該是 \\\"%v %v\\\" 格式,參考 main/mrsequential.go worker處理完map任務,應該把生成的中間文件放到當前目錄中,便于worker執(zhí)行reduce任務時讀取中間文件 當所

    2023年04月10日
    瀏覽(20)
  • MIT 6.S081 Lab Three

    MIT 6.S081 Lab Three

    本文為 MIT 6.S081 2020 操作系統(tǒng) 實驗三解析。 MIT 6.S081課程前置基礎參考: 基于RISC-V搭建操作系統(tǒng)系列 在本實驗中,您將探索頁表并對其進行修改,以簡化將數(shù)據(jù)從用戶空間復制到內(nèi)核空間的函數(shù)。 開始編碼之前,請閱讀xv6手冊的第3章和相關文件: * kernel/memlayout.h* ,它捕獲了

    2024年02月09日
    瀏覽(20)
  • mit6.828 - lab5筆記(上)

    unix的文件系統(tǒng)相關知識 unix將可用的磁盤空間劃分為兩種主要類型的區(qū)域: inode區(qū)域 和 數(shù)據(jù)區(qū)域 。 unix為每個文件分配一個inode,其中保存文件的 關鍵元數(shù)據(jù) ,如文件的stat屬性和指向文件數(shù)據(jù)塊的指針。 數(shù)據(jù)區(qū)域中的空間會被分成大小相同的數(shù)據(jù)塊(就像內(nèi)存管理中的分

    2024年02月02日
    瀏覽(46)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包