這個叫什么?Write Up嗎
lab0lab0要做的事,實現buffer部分的功能,要自己寫變量以及實現接口 。
成員函數 write()向buffer內寫數據,peek_out() pop_out() read() 函數從buffer內讀數據,
【cs144 lab0 lab1記錄】buffer長度capacity,需要變量記錄向buffer內一共寫入和讀取過多長的數據 。
有些變量在后面的lab會用上,第一次寫真不容易想,好些都是參照別人的博客寫的 。
lab1// Construct a `StreamReassembler` that will store up to `capacity` bytes.StreamReassembler(const size_t capacity);// Receive a substring and write any newly contiguous bytes into the stream,// while staying within the memory limits of the `capacity`. Bytes that would// exceed the capacity are silently discarded.//// `data`: the substring// `index` indicates the index (place in sequence) of the first byte in `data`// `eof`: the last byte of this substring will be the last byte in the entire streamvoid push_substring(const string &data, const uint64_t index, const bool eof);// Access the reassembled ByteStream (your code from Lab 0)ByteStream &stream_out();// The number of bytes in the substrings stored but not yet reassembledsize_t unassembled_bytes() const;// Is the internal state empty (other than the output stream)?bool empty() const;

文章插圖
push_substring,向buffer內寫入data,如“abcdefg”,“cdf” index = 2,eof代表傳完這段數據就沒有數據要傳了 。由于buffer有容量限制,收到的數據最后index不能比first_unacceptable大,
對于index在first_unread和first_unassembled之間的部分(已經放進buffer了),直接丟掉,
對于index在first_unassembled和first_unacceptable之間的部分(還沒有放進buffer,暫存在map中的),直接丟掉,_head_index用來記錄下一個實際要讀進buffer的首字符index,例:傳“abc”和“bcd”兩段字符串,后面 。。。
對于何時結束input,開始想的很復雜,其實有兩個條件:
1.收到with_eof信號
2.收到的字符串長度如果超過了first_unacceptable,說明還不能結束,還有數據要讀
在lab1學會了看測試文件和報錯 。測試文件放在tests目錄下,SubmitSegment 執行push_substring,BytesAssembled 用來測試上一步操作以后nwrite == _bytes, BytesAvailable 執行從buffer內讀操作,并判讀的數據是不是對的
把收到的數據放進map中有兩種思路:以capacity=3為例
