{"id":109020,"date":"2026-09-23T07:33:00","date_gmt":"2026-09-22T23:33:00","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/109020.html"},"modified":"2026-09-23T07:33:00","modified_gmt":"2026-09-22T23:33:00","slug":"c-%e4%bb%bf-muduo-%e9%ab%98%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%9a%e7%94%a8%e6%88%b7%e6%80%81%e7%bc%93%e5%86%b2%e5%8c%ba-buffer-%e6%a8%a1%e5%9d%97%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/109020.html","title":{"rendered":"C++ \u4eff muduo \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\uff1a\u7528\u6237\u6001\u7f13\u51b2\u533a Buffer \u6a21\u5757\u5b9e\u73b0"},"content":{"rendered":"<h2>C&#043;&#043; \u4eff muduo \u9ad8\u5e76\u53d1\u670d\u52a1\u5668&#xff1a;\u7528\u6237\u6001\u7f13\u51b2\u533a Buffer \u6a21\u5757\u5b9e\u73b0<\/h2>\n<hr \/>\n<h3>1. \u4e3a\u4ec0\u4e48\u9700\u8981 Buffer&#xff1f;<\/h3>\n<p>\u5728\u975e\u963b\u585e I\/O \u7684\u7f51\u7edc\u7f16\u7a0b\u4e2d&#xff0c;read \u548c write \u4e0d\u4fdd\u8bc1\u4e00\u6b21\u5c31\u80fd\u8bfb\u5b8c\u6216\u5199\u5b8c\u6240\u6709\u6570\u636e&#xff1a;<\/p>\n<ul>\n<li>\u8bfb\u534a\u5305&#xff1a;\u4e00\u6b21 read \u53ef\u80fd\u53ea\u6536\u5230\u8bf7\u6c42\u7684\u4e00\u90e8\u5206&#xff0c;\u9700\u8981\u6682\u5b58\u8d77\u6765&#xff0c;\u7b49\u4e0b\u6b21\u6570\u636e\u5230\u8fbe\u518d\u62fc\u63a5\u3002<\/li>\n<li>\u5199\u534a\u5305&#xff1a;\u4e00\u6b21 write \u53ef\u80fd\u53ea\u53d1\u9001\u4e86\u90e8\u5206\u6570\u636e&#xff0c;\u5269\u4f59\u6570\u636e\u9700\u8981\u7f13\u51b2&#xff0c;\u7b49 socket \u53ef\u5199\u65f6\u7ee7\u7eed\u53d1\u9001\u3002<\/li>\n<li>\u7c98\u5305&#xff1a;\u591a\u6b21\u53d1\u9001\u7684\u6570\u636e\u53ef\u80fd\u88ab\u5408\u5e76\u6210\u4e00\u4e2a TCP \u5305\u5230\u8fbe&#xff0c;\u9700\u8981\u6309\u534f\u8bae\u683c\u5f0f\u62c6\u5206\u3002<\/li>\n<\/ul>\n<p>\u56e0\u6b64&#xff0c;\u670d\u52a1\u5668\u9700\u8981\u4e00\u4e2a\u7528\u6237\u6001\u7f13\u51b2\u533a&#xff0c;\u5728\u5e94\u7528\u5c42\u7ba1\u7406\u6570\u636e\u7684\u63a5\u6536\u548c\u53d1\u9001\u3002<br \/>\nmuduo \u4e2d\u7684 Buffer \u5c31\u662f\u8fd9\u6837\u4e00\u4e2a\u7ec4\u4ef6&#xff0c;\u672c\u9879\u76ee\u4eff\u7167\u5b83\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5316\u7248\u3002<\/p>\n<hr \/>\n<h3>2. \u4ee3\u7801\u603b\u89c8<\/h3>\n<p>#include &lt;vector&gt;<br \/>\n#include &lt;stdint.h&gt;<br \/>\n#include &lt;assert.h&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;ctime&gt;<\/p>\n<p>#define BUFFER_DEFAULT_SIZE 1024<\/p>\n<p>class Buffer {<br \/>\nprivate:<br \/>\n    std::vector&lt;char&gt; _buffer;   \/\/ \u4f7f\u7528 vector \u7ba1\u7406\u5185\u5b58<br \/>\n    uint64_t _reader_idx;        \/\/ \u8bfb\u504f\u79fb<br \/>\n    uint64_t _writer_idx;        \/\/ \u5199\u504f\u79fb<\/p>\n<p>public:<br \/>\n    Buffer() : _reader_idx(0), _writer_idx(0), _buffer(BUFFER_DEFAULT_SIZE) {}<\/p>\n<p>    char *Begin() { return &amp;*_buffer.begin(); }<\/p>\n<p>    char *WritePosition() { return Begin() &#043; _writer_idx; }<br \/>\n    char *ReadPosition()  { return Begin() &#043; _reader_idx; }<\/p>\n<p>    uint64_t TailIdleSize() { return _buffer.size() &#8211; _writer_idx; }<br \/>\n    uint64_t HeadIdleSize() { return _reader_idx; }<br \/>\n    uint64_t ReadAbleSize() { return _writer_idx &#8211; _reader_idx; }<\/p>\n<p>    void MoveReadOffset(uint64_t len) {<br \/>\n        assert(len &lt;&#061; ReadAbleSize());<br \/>\n        _reader_idx &#043;&#061; len;<br \/>\n    }<\/p>\n<p>    void MoveWriteOffset(uint64_t len) {<br \/>\n        assert(len &lt;&#061; TailIdleSize());<br \/>\n        _writer_idx &#043;&#061; len;<br \/>\n    }<\/p>\n<p>    void EnsureWriteSpace(uint64_t len) {<br \/>\n        if (TailIdleSize() &gt;&#061; len) return;<br \/>\n        if (len &lt;&#061; TailIdleSize() &#043; HeadIdleSize()) {<br \/>\n            uint64_t rsz &#061; ReadAbleSize();<br \/>\n            std::copy(ReadPosition(), ReadPosition() &#043; rsz, Begin());<br \/>\n            _reader_idx &#061; 0;<br \/>\n            _writer_idx &#061; rsz;<br \/>\n        } else {<br \/>\n            _buffer.resize(_writer_idx &#043; len);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void Write(const void *data, uint64_t len) {<br \/>\n        EnsureWriteSpace(len);<br \/>\n        const char *d &#061; (const char *)data;<br \/>\n        std::copy(d, d &#043; len, WritePosition());<br \/>\n    }<\/p>\n<p>    void WriteAndPush(const void *data, uint64_t len) {<br \/>\n        Write(data, len);<br \/>\n        MoveWriteOffset(len);<br \/>\n    }<\/p>\n<p>    void WriteString(const std::string &amp;data) {<br \/>\n        return Write(data.c_str(), data.size());<br \/>\n    }<\/p>\n<p>    void WriteStringAndPush(const std::string &amp;data) {<br \/>\n        WriteString(data);<br \/>\n        MoveWriteOffset(data.size());<br \/>\n    }<\/p>\n<p>    void WriteBuffer(Buffer &amp;data) {<br \/>\n        return Write(data.ReadPosition(), data.ReadAbleSize());<br \/>\n    }<\/p>\n<p>    void WriteBufferAndPush(Buffer &amp;data) {<br \/>\n        WriteBuffer(data);<br \/>\n        MoveWriteOffset(data.ReadAbleSize());<br \/>\n    }<\/p>\n<p>    void Read(void *buf, uint64_t len) {<br \/>\n        assert(len &lt;&#061; ReadAbleSize());<br \/>\n        std::copy(ReadPosition(), ReadPosition() &#043; len, (char *)buf);<br \/>\n    }<\/p>\n<p>    void ReadAndPop(void *buf, int len) {<br \/>\n        Read(buf, len);<br \/>\n        MoveReadOffset(len);<br \/>\n    }<\/p>\n<p>    std::string ReadAsString(uint64_t len) {<br \/>\n        assert(len &lt;&#061; ReadAbleSize());<br \/>\n        std::string str;<br \/>\n        str.resize(len);<br \/>\n        Read(&amp;str[0], len);<br \/>\n        return str;<br \/>\n    }<\/p>\n<p>    std::string ReadAsStringAndPop(uint64_t len) {<br \/>\n        assert(len &lt;&#061; ReadAbleSize());<br \/>\n        std::string str &#061; ReadAsString(len);<br \/>\n        MoveReadOffset(len);<br \/>\n        return str;<br \/>\n    }<\/p>\n<p>    char *FindCRLF() {<br \/>\n        char *res &#061; (char *)memchr(ReadPosition(), &#039;\\\\n&#039;, ReadAbleSize());<br \/>\n        return res;<br \/>\n    }<\/p>\n<p>    std::string GetLine() {<br \/>\n        char *pos &#061; FindCRLF();<br \/>\n        if (pos &#061;&#061; NULL) return &#034;&#034;;<br \/>\n        return ReadAsString(pos &#8211; ReadPosition() &#043; 1);<br \/>\n    }<\/p>\n<p>    std::string GetLlineAndPop() {   \/\/ \u6ce8\u610f&#xff1a;\u62fc\u5199\u9519\u8bef&#xff0c;\u5e94\u4e3a GetLineAndPop<br \/>\n        std::string str &#061; GetLine();<br \/>\n        MoveReadOffset(str.size());<br \/>\n        return str;<br \/>\n    }<\/p>\n<p>    void Clear() {<br \/>\n        _reader_idx &#061; 0;<br \/>\n        _writer_idx &#061; 0;<br \/>\n    }<br \/>\n};<\/p>\n<hr \/>\n<h3>3. \u6838\u5fc3\u8bbe\u8ba1\u601d\u60f3<\/h3>\n<p>Buffer \u7684\u8bbe\u8ba1\u56f4\u7ed5\u4e09\u4e2a\u6838\u5fc3&#xff1a;<\/p>\n<li>\u8bfb\u5199\u504f\u79fb\u5206\u79bb<br \/>\n\u7528 _reader_idx \u548c _writer_idx \u6807\u8bb0\u8bfb\u4f4d\u7f6e\u548c\u5199\u4f4d\u7f6e&#xff0c;\u800c\u4e0d\u662f\u6bcf\u6b21\u8bfb\u8d70\u6570\u636e\u5c31\u5220\u9664 vector \u5934\u90e8\u3002<br \/>\n\u8fd9\u6837\u907f\u514d\u9891\u7e41\u5185\u5b58\u79fb\u52a8&#xff0c;\u63d0\u9ad8\u6548\u7387\u3002<\/li>\n<li>\u7a7a\u95f4\u6574\u7406\u4e0e\u6269\u5bb9<br \/>\n\u5f53\u5c3e\u90e8\u7a7a\u95f4\u4e0d\u8db3\u65f6&#xff0c;\u5148\u5c1d\u8bd5\u628a\u53ef\u8bfb\u6570\u636e\u79fb\u52a8\u5230\u5934\u90e8&#xff0c;\u5229\u7528\u5934\u90e8\u7a7a\u95f2\u7a7a\u95f4&#xff1b;<br \/>\n\u5982\u679c\u603b\u7a7a\u95f4\u4ecd\u4e0d\u8db3&#xff0c;\u518d\u5bf9 vector \u6269\u5bb9\u3002<\/li>\n<li>\u6309\u884c\u8bfb\u53d6\u652f\u6301<br \/>\n\u63d0\u4f9b FindCRLF\u3001GetLine\u3001GetLineAndPop&#xff0c;\u65b9\u4fbf HTTP \u7b49\u6587\u672c\u534f\u8bae\u6309\u884c\u89e3\u6790\u3002<\/li>\n<hr \/>\n<h3>4. \u6210\u5458\u53d8\u91cf\u4e0e\u63a5\u53e3\u62c6\u89e3<\/h3>\n<h4>4.1 \u6210\u5458\u53d8\u91cf<\/h4>\n<p>std::vector&lt;char&gt; _buffer;   \/\/ \u5e95\u5c42\u5185\u5b58<br \/>\nuint64_t _reader_idx;        \/\/ \u8bfb\u504f\u79fb<br \/>\nuint64_t _writer_idx;        \/\/ \u5199\u504f\u79fb<\/p>\n<ul>\n<li>_buffer&#xff1a;\u7528 std::vector&lt;char&gt; \u7ba1\u7406\u5185\u5b58&#xff0c;\u81ea\u52a8\u6269\u5bb9\u3002<\/li>\n<li>_reader_idx&#xff1a;\u4e0b\u4e00\u4e2a\u5f85\u8bfb\u53d6\u6570\u636e\u7684\u4f4d\u7f6e\u3002<\/li>\n<li>_writer_idx&#xff1a;\u4e0b\u4e00\u4e2a\u5f85\u5199\u5165\u6570\u636e\u7684\u4f4d\u7f6e\u3002<\/li>\n<li>\u53ef\u8bfb\u6570\u636e\u533a\u95f4&#xff1a;[_reader_idx, _writer_idx)\u3002<\/li>\n<li>\u7a7a\u95f2\u7a7a\u95f4&#xff1a;\n<ul>\n<li>\u5934\u90e8\u7a7a\u95f2&#xff1a;[0, _reader_idx)<\/li>\n<li>\u5c3e\u90e8\u7a7a\u95f2&#xff1a;[_writer_idx, _buffer.size())<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>4.2 \u83b7\u53d6\u4f4d\u7f6e<\/h4>\n<p>char *Begin() { return &amp;*_buffer.begin(); }<br \/>\nchar *WritePosition() { return Begin() &#043; _writer_idx; }<br \/>\nchar *ReadPosition()  { return Begin() &#043; _reader_idx; }<\/p>\n<ul>\n<li>Begin()&#xff1a;\u8fd4\u56de vector \u9996\u5143\u7d20\u5730\u5740\u3002&amp;*_buffer.begin() \u662f\u83b7\u53d6\u9996\u5730\u5740\u7684\u60ef\u7528\u5199\u6cd5\u3002<\/li>\n<li>WritePosition()&#xff1a;\u5199\u8d77\u59cb\u5730\u5740\u3002<\/li>\n<li>ReadPosition()&#xff1a;\u8bfb\u8d77\u59cb\u5730\u5740\u3002<\/li>\n<\/ul>\n<h4>4.3 \u7a7a\u95f2\u7a7a\u95f4\u8ba1\u7b97<\/h4>\n<p>uint64_t TailIdleSize() { return _buffer.size() &#8211; _writer_idx; }<br \/>\nuint64_t HeadIdleSize() { return _reader_idx; }<br \/>\nuint64_t ReadAbleSize() { return _writer_idx &#8211; _reader_idx; }<\/p>\n<ul>\n<li>TailIdleSize()&#xff1a;\u5199\u504f\u79fb\u4e4b\u540e\u7684\u7a7a\u95f2\u7a7a\u95f4\u3002<\/li>\n<li>HeadIdleSize()&#xff1a;\u8bfb\u504f\u79fb\u4e4b\u524d\u7684\u7a7a\u95f2\u7a7a\u95f4\u3002<\/li>\n<li>ReadAbleSize()&#xff1a;\u53ef\u8bfb\u6570\u636e\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<h4>4.4 \u79fb\u52a8\u504f\u79fb<\/h4>\n<p>void MoveReadOffset(uint64_t len) {<br \/>\n    assert(len &lt;&#061; ReadAbleSize());<br \/>\n    _reader_idx &#043;&#061; len;<br \/>\n}<\/p>\n<p>void MoveWriteOffset(uint64_t len) {<br \/>\n    assert(len &lt;&#061; TailIdleSize());<br \/>\n    _writer_idx &#043;&#061; len;<br \/>\n}<\/p>\n<ul>\n<li>\u79fb\u52a8\u524d\u7528 assert \u68c0\u67e5\u8d8a\u754c&#xff0c;Debug \u4e0b\u53ef\u53ca\u65f6\u53d1\u73b0\u95ee\u9898\u3002<\/li>\n<li>\u8bfb\u504f\u79fb\u540e\u79fb\u8868\u793a\u6570\u636e\u5df2\u88ab\u6d88\u8d39\u3002<\/li>\n<li>\u5199\u504f\u79fb\u540e\u79fb\u8868\u793a\u6570\u636e\u5df2\u5199\u5165\u3002<\/li>\n<\/ul>\n<h4>4.5 \u786e\u4fdd\u53ef\u5199\u7a7a\u95f4&#xff1a;\u6574\u7406\u4e0e\u6269\u5bb9<\/h4>\n<p>void EnsureWriteSpace(uint64_t len) {<br \/>\n    if (TailIdleSize() &gt;&#061; len) return;<\/p>\n<p>    if (len &lt;&#061; TailIdleSize() &#043; HeadIdleSize()) {<br \/>\n        uint64_t rsz &#061; ReadAbleSize();<br \/>\n        std::copy(ReadPosition(), ReadPosition() &#043; rsz, Begin());<br \/>\n        _reader_idx &#061; 0;<br \/>\n        _writer_idx &#061; rsz;<br \/>\n    } else {<br \/>\n        _buffer.resize(_writer_idx &#043; len);<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8fd9\u662f Buffer \u6700\u6838\u5fc3\u7684\u51fd\u6570\u4e4b\u4e00&#xff1a;<\/p>\n<li>\u5c3e\u90e8\u7a7a\u95f4\u8db3\u591f&#xff1a;\u76f4\u63a5\u8fd4\u56de\u3002<\/li>\n<li>\u5c3e\u90e8\u4e0d\u591f&#xff0c;\u4f46\u5934\u90e8&#043;\u5c3e\u90e8\u591f&#xff1a;\u628a\u53ef\u8bfb\u6570\u636e\u6574\u4f53\u79fb\u52a8\u5230\u5934\u90e8\u3002\n<ul>\n<li>\u4fdd\u5b58\u53ef\u8bfb\u6570\u636e\u5927\u5c0f rsz\u3002<\/li>\n<li>\u7528 std::copy \u628a\u6570\u636e\u642c\u5230 Begin()\u3002<\/li>\n<li>_reader_idx &#061; 0&#xff0c;_writer_idx &#061; rsz\u3002<\/li>\n<li>\u8fd9\u6837\u5c3e\u90e8\u5c31\u817e\u51fa\u4e86\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u603b\u7a7a\u95f4\u4e0d\u591f&#xff1a;\u76f4\u63a5\u6269\u5bb9\u5230 _writer_idx &#043; len&#xff0c;\u4fdd\u8bc1\u80fd\u5199\u4e0b\u65b0\u6570\u636e\u3002\n<ul>\n<li>\u4e0d\u79fb\u52a8\u6570\u636e&#xff0c;\u56e0\u4e3a\u79fb\u52a8\u4e5f\u6ca1\u7528&#xff0c;\u603b\u7a7a\u95f4\u4e0d\u591f\u3002<\/li>\n<\/ul>\n<\/li>\n<p>\u4e3a\u4ec0\u4e48\u5148\u79fb\u52a8\u518d\u6269\u5bb9&#xff1f;<br \/>\n\u79fb\u52a8\u53ef\u4ee5\u590d\u7528\u5934\u90e8\u7a7a\u95f2\u7a7a\u95f4&#xff0c;\u51cf\u5c11\u5185\u5b58\u5206\u914d\u6b21\u6570&#xff0c;\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<h4>4.6 \u5199\u5165\u6570\u636e<\/h4>\n<p>void Write(const void *data, uint64_t len) {<br \/>\n    EnsureWriteSpace(len);<br \/>\n    const char *d &#061; (const char *)data;<br \/>\n    std::copy(d, d &#043; len, WritePosition());<br \/>\n}<\/p>\n<p>void WriteAndPush(const void *data, uint64_t len) {<br \/>\n    Write(data, len);<br \/>\n    MoveWriteOffset(len);<br \/>\n}<\/p>\n<ul>\n<li>Write&#xff1a;\u53ea\u62f7\u8d1d\u6570\u636e&#xff0c;\u4e0d\u79fb\u52a8\u5199\u504f\u79fb\u3002<\/li>\n<li>WriteAndPush&#xff1a;\u62f7\u8d1d\u540e\u79fb\u52a8\u5199\u504f\u79fb&#xff0c;\u8868\u793a\u6570\u636e\u5df2\u5199\u5165\u3002<\/li>\n<li>\u5176\u4ed6\u5199\u5165\u63a5\u53e3\u90fd\u662f\u5bf9\u8fd9\u4e24\u4e2a\u7684\u5c01\u88c5&#xff1a;\n<ul>\n<li>WriteString \/ WriteStringAndPush&#xff1a;\u5199\u5165 std::string\u3002<\/li>\n<li>WriteBuffer \/ WriteBufferAndPush&#xff1a;\u628a\u53e6\u4e00\u4e2a Buffer \u7684\u53ef\u8bfb\u6570\u636e\u5199\u5165\u5f53\u524d Buffer\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>4.7 \u8bfb\u53d6\u6570\u636e<\/h4>\n<p>void Read(void *buf, uint64_t len) {<br \/>\n    assert(len &lt;&#061; ReadAbleSize());<br \/>\n    std::copy(ReadPosition(), ReadPosition() &#043; len, (char *)buf);<br \/>\n}<\/p>\n<p>void ReadAndPop(void *buf, int len) {<br \/>\n    Read(buf, len);<br \/>\n    MoveReadOffset(len);<br \/>\n}<\/p>\n<ul>\n<li>Read&#xff1a;\u53ea\u62f7\u8d1d\u6570\u636e&#xff0c;\u4e0d\u79fb\u52a8\u8bfb\u504f\u79fb\u3002<\/li>\n<li>ReadAndPop&#xff1a;\u8bfb\u53d6\u540e\u79fb\u52a8\u8bfb\u504f\u79fb&#xff0c;\u8868\u793a\u6570\u636e\u5df2\u6d88\u8d39\u3002<\/li>\n<li>ReadAsString \/ ReadAsStringAndPop&#xff1a;\u8bfb\u53d6\u4e3a std::string\u3002<\/li>\n<\/ul>\n<p>\u6ce8\u610f&#xff1a;ReadAndPop \u7684\u53c2\u6570\u662f int len&#xff0c;\u800c\u5176\u4ed6\u63a5\u53e3\u662f uint64_t&#xff0c;\u7c7b\u578b\u4e0d\u4e00\u81f4&#xff0c;\u5efa\u8bae\u7edf\u4e00\u4e3a uint64_t\u3002<\/p>\n<h4>4.8 \u6309\u884c\u8bfb\u53d6<\/h4>\n<p>char *FindCRLF() {<br \/>\n    char *res &#061; (char *)memchr(ReadPosition(), &#039;\\\\n&#039;, ReadAbleSize());<br \/>\n    return res;<br \/>\n}<\/p>\n<p>std::string GetLine() {<br \/>\n    char *pos &#061; FindCRLF();<br \/>\n    if (pos &#061;&#061; NULL) return &#034;&#034;;<br \/>\n    return ReadAsString(pos &#8211; ReadPosition() &#043; 1);<br \/>\n}<\/p>\n<p>std::string GetLlineAndPop() {   \/\/ \u62fc\u5199\u9519\u8bef&#xff0c;\u5e94\u4e3a GetLineAndPop<br \/>\n    std::string str &#061; GetLine();<br \/>\n    MoveReadOffset(str.size());<br \/>\n    return str;<br \/>\n}<\/p>\n<ul>\n<li>FindCRLF()&#xff1a;\u5728\u53ef\u8bfb\u6570\u636e\u4e2d\u67e5\u627e &#039;\\\\n&#039;&#xff0c;\u8fd4\u56de\u6307\u9488\u3002<\/li>\n<li>GetLine()&#xff1a;\u4ece\u8bfb\u4f4d\u7f6e\u5230 &#039;\\\\n&#039;&#xff08;\u5305\u542b &#039;\\\\n&#039;&#xff09;\u8bfb\u53d6\u4e3a\u5b57\u7b26\u4e32\u3002\u5982\u679c\u6ca1\u627e\u5230\u6362\u884c&#xff0c;\u8fd4\u56de\u7a7a\u4e32\u3002<\/li>\n<li>GetLineAndPop()&#xff1a;\u8bfb\u53d6\u4e00\u884c\u5e76\u79fb\u52a8\u8bfb\u504f\u79fb\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u4ec0\u4e48\u627e &#039;\\\\n&#039; \u800c\u4e0d\u662f &#034;\\\\r\\\\n&#034;&#xff1f;<br \/>\nHTTP \u534f\u8bae\u4e2d\u884c\u7ed3\u675f\u53ef\u80fd\u662f \\\\r\\\\n \u6216 \\\\n&#xff0c;\u67e5\u627e \\\\n \u66f4\u901a\u7528&#xff0c;\u89e3\u6790\u65f6\u518d\u5904\u7406 \\\\r\u3002<\/p>\n<p>\u6ce8\u610f\u51fd\u6570\u540d\u62fc\u5199\u9519\u8bef&#xff1a;GetLlineAndPop \u5e94\u4e3a GetLineAndPop\u3002<\/p>\n<h4>4.9 \u6e05\u7a7a\u7f13\u51b2\u533a<\/h4>\n<p>void Clear() {<br \/>\n    _reader_idx &#061; 0;<br \/>\n    _writer_idx &#061; 0;<br \/>\n}<\/p>\n<p>\u53ea\u91cd\u7f6e\u504f\u79fb\u91cf&#xff0c;\u4e0d\u91ca\u653e\u5185\u5b58&#xff0c;\u4e0b\u6b21\u5199\u5165\u76f4\u63a5\u8986\u76d6\u3002<\/p>\n<hr \/>\n<h3>5. \u5173\u952e\u6d41\u7a0b<\/h3>\n<h4>5.1 \u5199\u5165\u6d41\u7a0b<\/h4>\n<li>\u8c03\u7528 Write \u6216 WriteAndPush\u3002<\/li>\n<li>EnsureWriteSpace(len) \u786e\u4fdd\u7a7a\u95f4\u8db3\u591f&#xff1a;\n<ul>\n<li>\u5c3e\u90e8\u591f \u2192 \u76f4\u63a5\u5199\u3002<\/li>\n<li>\u5c3e\u90e8\u4e0d\u591f\u4f46\u603b\u7a7a\u95f2\u591f \u2192 \u6570\u636e\u79fb\u5230\u5934\u90e8&#xff0c;\u518d\u5199\u3002<\/li>\n<li>\u603b\u7a7a\u95f4\u4e0d\u591f \u2192 \u6269\u5bb9&#xff0c;\u518d\u5199\u3002<\/li>\n<\/ul>\n<\/li>\n<li>std::copy \u62f7\u8d1d\u6570\u636e\u5230 WritePosition()\u3002<\/li>\n<li>\u5982\u679c\u662f WriteAndPush&#xff0c;MoveWriteOffset(len)\u3002<\/li>\n<h4>5.2 \u8bfb\u53d6\u6d41\u7a0b<\/h4>\n<li>\u8c03\u7528 Read \u6216 ReadAndPop\u3002<\/li>\n<li>assert \u68c0\u67e5\u53ef\u8bfb\u6570\u636e\u8db3\u591f\u3002<\/li>\n<li>std::copy \u4ece ReadPosition() \u62f7\u8d1d\u5230\u76ee\u6807\u7f13\u51b2\u533a\u3002<\/li>\n<li>\u5982\u679c\u662f ReadAndPop&#xff0c;MoveReadOffset(len)\u3002<\/li>\n<h4>5.3 \u6309\u884c\u89e3\u6790\u6d41\u7a0b<\/h4>\n<li>FindCRLF() \u7528 memchr \u627e &#039;\\\\n&#039;\u3002<\/li>\n<li>GetLine() \u8ba1\u7b97\u957f\u5ea6\u5e76\u8bfb\u53d6\u3002<\/li>\n<li>GetLineAndPop() \u8bfb\u53d6\u540e\u79fb\u52a8\u8bfb\u504f\u79fb\u3002<\/li>\n<li>\u5982\u679c\u6ca1\u627e\u5230 &#039;\\\\n&#039;&#xff0c;\u8fd4\u56de\u7a7a\u4e32&#xff0c;\u7b49\u5f85\u66f4\u591a\u6570\u636e\u3002<\/li>\n<hr \/>\n<h3>6. \u4f7f\u7528\u793a\u4f8b<\/h3>\n<p>Buffer buf;<br \/>\nstd::string data &#061; &#034;GET \/hello HTTP\/1.1\\\\r\\\\nHost: localhost\\\\r\\\\n\\\\r\\\\n&#034;;<br \/>\nbuf.WriteStringAndPush(data);<\/p>\n<p>std::string line &#061; buf.GetLineAndPop();  \/\/ &#034;GET \/hello HTTP\/1.1\\\\r\\\\n&#034;<br \/>\nstd::cout &lt;&lt; line;<\/p>\n<p>while (true) {<br \/>\n    std::string l &#061; buf.GetLineAndPop();<br \/>\n    if (l.empty()) break;<br \/>\n    std::cout &lt;&lt; l;<br \/>\n}<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>C \u4eff muduo \u9ad8\u5e76\u53d1\u670d\u52a1\u5668&#xff1a;\u7528\u6237\u6001\u7f13\u51b2\u533a Buffer \u6a21\u5757\u5b9e\u73b01. \u4e3a\u4ec0\u4e48\u9700\u8981 Buffer&#xff1f;<br \/>\n\u5728\u975e\u963b\u585e I\/O \u7684\u7f51\u7edc\u7f16\u7a0b\u4e2d&#xff0c;read \u548c write \u4e0d\u4fdd\u8bc1\u4e00\u6b21\u5c31\u80fd\u8bfb\u5b8c\u6216\u5199\u5b8c\u6240\u6709\u6570\u636e&#xff1a;<br \/>\n\u8bfb\u534a\u5305&#xff1a;\u4e00\u6b21 read \u53ef\u80fd\u53ea\u6536\u5230\u8bf7\u6c42\u7684\u4e00\u90e8\u5206&#xff0c;\u9700\u8981\u6682\u5b58\u8d77\u6765&#xff0c;\u7b49\u4e0b\u6b21\u6570\u636e\u5230\u8fbe\u518d\u62fc\u63a5\u3002\u5199\u534a\u5305&#xff1a;\u4e00\u6b21 write \u53ef\u80fd\u53ea\u53d1\u9001\u4e86\u90e8\u5206\u6570\u636e&#xff0c;\u5269\u4f59\u6570\u636e\u9700\u8981\u7f13\u51b2&amp;#xf<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58],"topic":[],"class_list":["post-109020","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-linux"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ \u4eff muduo \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\uff1a\u7528\u6237\u6001\u7f13\u51b2\u533a Buffer \u6a21\u5757\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/109020.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ \u4eff muduo \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\uff1a\u7528\u6237\u6001\u7f13\u51b2\u533a Buffer \u6a21\u5757\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"C \u4eff muduo \u9ad8\u5e76\u53d1\u670d\u52a1\u5668&#xff1a;\u7528\u6237\u6001\u7f13\u51b2\u533a Buffer \u6a21\u5757\u5b9e\u73b01. \u4e3a\u4ec0\u4e48\u9700\u8981 Buffer&#xff1f; \u5728\u975e\u963b\u585e I\/O \u7684\u7f51\u7edc\u7f16\u7a0b\u4e2d&#xff0c;read \u548c write \u4e0d\u4fdd\u8bc1\u4e00\u6b21\u5c31\u80fd\u8bfb\u5b8c\u6216\u5199\u5b8c\u6240\u6709\u6570\u636e&#xff1a; \u8bfb\u534a\u5305&#xff1a;\u4e00\u6b21 read \u53ef\u80fd\u53ea\u6536\u5230\u8bf7\u6c42\u7684\u4e00\u90e8\u5206&#xff0c;\u9700\u8981\u6682\u5b58\u8d77\u6765&#xff0c;\u7b49\u4e0b\u6b21\u6570\u636e\u5230\u8fbe\u518d\u62fc\u63a5\u3002\u5199\u534a\u5305&#xff1a;\u4e00\u6b21 write \u53ef\u80fd\u53ea\u53d1\u9001\u4e86\u90e8\u5206\u6570\u636e&#xff0c;\u5269\u4f59\u6570\u636e\u9700\u8981\u7f13\u51b2&amp;#xf\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/109020.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-22T23:33:00+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/109020.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/109020.html\",\"name\":\"C++ \u4eff muduo \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\uff1a\u7528\u6237\u6001\u7f13\u51b2\u533a Buffer \u6a21\u5757\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-22T23:33:00+00:00\",\"dateModified\":\"2026-09-22T23:33:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/109020.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/109020.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/109020.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ \u4eff muduo \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\uff1a\u7528\u6237\u6001\u7f13\u51b2\u533a Buffer \u6a21\u5757\u5b9e\u73b0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ \u4eff muduo \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\uff1a\u7528\u6237\u6001\u7f13\u51b2\u533a Buffer \u6a21\u5757\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/109020.html","og_locale":"zh_CN","og_type":"article","og_title":"C++ \u4eff muduo \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\uff1a\u7528\u6237\u6001\u7f13\u51b2\u533a Buffer \u6a21\u5757\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"C \u4eff muduo \u9ad8\u5e76\u53d1\u670d\u52a1\u5668&#xff1a;\u7528\u6237\u6001\u7f13\u51b2\u533a Buffer \u6a21\u5757\u5b9e\u73b01. \u4e3a\u4ec0\u4e48\u9700\u8981 Buffer&#xff1f; \u5728\u975e\u963b\u585e I\/O \u7684\u7f51\u7edc\u7f16\u7a0b\u4e2d&#xff0c;read \u548c write \u4e0d\u4fdd\u8bc1\u4e00\u6b21\u5c31\u80fd\u8bfb\u5b8c\u6216\u5199\u5b8c\u6240\u6709\u6570\u636e&#xff1a; \u8bfb\u534a\u5305&#xff1a;\u4e00\u6b21 read \u53ef\u80fd\u53ea\u6536\u5230\u8bf7\u6c42\u7684\u4e00\u90e8\u5206&#xff0c;\u9700\u8981\u6682\u5b58\u8d77\u6765&#xff0c;\u7b49\u4e0b\u6b21\u6570\u636e\u5230\u8fbe\u518d\u62fc\u63a5\u3002\u5199\u534a\u5305&#xff1a;\u4e00\u6b21 write \u53ef\u80fd\u53ea\u53d1\u9001\u4e86\u90e8\u5206\u6570\u636e&#xff0c;\u5269\u4f59\u6570\u636e\u9700\u8981\u7f13\u51b2&amp;#xf","og_url":"https:\/\/www.wsisp.com\/helps\/109020.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-22T23:33:00+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/109020.html","url":"https:\/\/www.wsisp.com\/helps\/109020.html","name":"C++ \u4eff muduo \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\uff1a\u7528\u6237\u6001\u7f13\u51b2\u533a Buffer \u6a21\u5757\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-22T23:33:00+00:00","dateModified":"2026-09-22T23:33:00+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/109020.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/109020.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/109020.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++ \u4eff muduo \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\uff1a\u7528\u6237\u6001\u7f13\u51b2\u533a Buffer \u6a21\u5757\u5b9e\u73b0"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/109020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=109020"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/109020\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=109020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=109020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=109020"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=109020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}