{"id":44461,"date":"2025-06-19T07:36:19","date_gmt":"2025-06-18T23:36:19","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/44461.html"},"modified":"2025-06-19T07:36:19","modified_gmt":"2025-06-18T23:36:19","slug":"%e3%80%90%e9%a1%b9%e7%9b%ae%e3%80%91%e4%bb%bfmuduo%e5%ba%93one-thread-one-loop%e5%bc%8f%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%88%e4%b8%8a%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/44461.html","title":{"rendered":"\u3010\u9879\u76ee\u3011\u4effmuduo\u5e93one thread one loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\uff08\u4e0a\uff09"},"content":{"rendered":"<p>\u00a0\u00a0&#x1f4da;\u00a0\u535a\u4e3b\u7684\u4e13\u680f<\/p>\n<p>&#x1f427;\u00a0Linux\u00a0\u00a0\u00a0|\u00a0\u00a0 &#x1f5a5;\ufe0f\u00a0C&#043;&#043;\u00a0\u00a0\u00a0|\u00a0\u00a0 &#x1f4ca;\u00a0\u6570\u636e\u7ed3\u6784\u00a0\u00a0|\u00a0&#x1f4a1;C&#043;&#043; \u7b97\u6cd5\u00a0|\u00a0&#x1f152;\u00a0C \u8bed\u8a00\u00a0 |\u00a0&#x1f310;\u00a0\u8ba1\u7b97\u673a\u7f51\u7edc\u00a0|&#x1f5c3;\ufe0f\u00a0mysql<\/p>\n<p><span style=\"color:#0d0016\">\u9879\u76ee\u6587\u7ae0&#xff1a;<\/span>\u4effmuduo\u5e93one thread one loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\u524d\u7f6e\u77e5\u8bc6\u51c6\u5907<\/p>\n<p><span style=\"color:#0d0016\">\u601d\u7ef4\u5bfc\u56fe\u4e0e\u9879\u76ee\u4ee3\u7801\u5df2\u4e0a\u4f20\u81f3\u6211\u7684gitee<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u672c\u6587\u6458\u8981&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u7f51\u7edc\u7f16\u7a0b\u4e2d\u5173\u952e\u6a21\u5757\u7684\u8bbe\u8ba1\u4e0e\u5b9e\u73b0&#xff0c;\u5305\u62ecBuffer\u7f13\u51b2\u533a\u6a21\u5757\u3001Socket\u5957\u63a5\u5b57\u6a21\u5757\u3001Channel\u4e8b\u4ef6\u7ba1\u7406\u6a21\u5757\u3001Poller\u63cf\u8ff0\u7b26\u76d1\u63a7\u6a21\u5757\u3001EventLoop\u4e8b\u4ef6\u5faa\u73af\u6a21\u5757\u548cTimerWheel\u5b9a\u65f6\u5668\u6a21\u5757\u3002Buffer\u6a21\u5757\u91c7\u7528vector&lt;char&gt;\u4f5c\u4e3a\u5e95\u5c42\u5bb9\u5668&#xff0c;\u907f\u514dstring\u7684&#039;\\\\0&#039;\u622a\u65ad\u95ee\u9898&#xff0c;\u63d0\u4f9b\u6570\u636e\u5b58\u50a8\u548c\u7ba1\u7406\u529f\u80fd&#xff1b;Socket\u6a21\u5757\u5c01\u88c5\u4e86TCP\u5957\u63a5\u5b57\u64cd\u4f5c&#xff1b;Channel\u548cPoller\u6a21\u5757\u5b9e\u73b0\u5bf9\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7\u7ba1\u7406&#xff1b;EventLoop\u6a21\u5757\u91c7\u7528one thread one loop\u6a21\u578b&#xff0c;\u901a\u8fc7\u4efb\u52a1\u961f\u5217\u548ceventfd\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u4e8b\u4ef6\u5904\u7406&#xff1b;TimerWheel\u6a21\u5757\u63d0\u4f9b\u5b9a\u65f6\u4efb\u52a1\u529f\u80fd\u3002\u5404\u6a21\u5757\u901a\u8fc7\u56de\u8c03\u673a\u5236\u534f\u540c\u5de5\u4f5c&#xff0c;\u6700\u7ec8\u5b9e\u73b0\u4e86\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u670d\u52a1\u5668\u6846\u67b6&#xff0c;\u5e76\u901a\u8fc7TCP\u5ba2\u6237\u7aef\/\u670d\u52a1\u5668\u6d4b\u8bd5\u9a8c\u8bc1\u4e86\u529f\u80fd\u5b8c\u6574\u6027\u3002\u4ee3\u7801\u5df2\u4e0a\u4f20\u81f3Gitee\u4ed3\u5e93\u3002\u7531\u4e8e\u6587\u7ae0\u5b57\u6570\u8fc7\u591a&#xff0c;\u5c06\u65b0\u66f4\u65b0\u4e00\u7bc7\u6587\u7ae0\u8bb2\u89e3\u5176\u4f59\u6a21\u5757&#xff08;Connection\u3001Accepter\u3001LoopThread\u3001LoopThreadPool\u3001TcpServer\u3001EchoServer\u7b49\u6a21\u5757&#8230;..&#xff09;<\/span><\/p>\n<p><span style=\"color:#ed7976\">\u9879\u76ee\u4ee3\u7801\u4f1a\u968f\u6587\u7ae0\u5185\u5bb9\u540c\u6b65\u66f4\u65b0&#xff0c;\u60a8\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4e3b\u9875\u52a8\u6001\u76f4\u63a5\u67e5\u770b\u672c\u6587\u5404\u6a21\u5757\u5bf9\u5e94\u7684\u8bb2\u89e3\u4ee3\u7801\u3002<\/span><span style=\"color:#0d0016\">&#xff08;\u533a\u95f4\u57286.13-6.16\u8fd9\u51e0\u5929&#xff09;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u672c\u6587\u4ecb\u7ecd\u7684\u90e8\u5206\u9879\u76ee\u7ed3\u6784\u5982\u4e0b\u56fe\u6240\u793a&#xff0c;\u6709\u52a9\u4e8e\u8bfb\u8005\u5feb\u901f\u4e86\u89e3\u6574\u4f53\u6846\u67b6\u3002<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"3997\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233559-68534d5fd167f.png\" width=\"4321\" \/><\/p>\n<h2><span style=\"color:#0d0016\">\u4e00\u3001Buffer\u6a21\u5757&#xff08;\u7f13\u51b2\u533a\u6a21\u5757&#xff09;<\/span><\/h2>\n<p><span style=\"color:#0d0016\">\u63d0\u4f9b\u7684\u529f\u80fd&#xff1a;\u5b58\u50a8\u6570\u636e\u3001\u53d6\u51fa\u6570\u636e\u3001\u6570\u636e\u7ba1\u7406&#xff08;\u81ea\u52a8\u6269\u5c55\u5b58\u50a8\u7a7a\u95f4&#xff09;<\/span><\/p>\n<h3><span style=\"color:#0d0016\">1.1 Buffer\u7f13\u51b2\u533a\u8bbe\u8ba1\u601d\u60f3&#xff1a;<\/span><\/h3>\n<h4><span style=\"color:#0d0016\">\u5185\u5b58\u7a7a\u95f4\u7ba1\u7406&#xff1a;<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u91c7\u7528<\/span><span style=\"color:#ff9900\"><span style=\"background-color:null\">vector&lt;char&gt;\u4f5c\u4e3a\u5e95\u5c42\u5bb9\u5668<\/span><\/span><span style=\"color:#0d0016\">&#xff0c;\u539f\u56e0\u5982\u4e0b&#xff1a;<\/span><\/p>\n<li><span style=\"color:#0d0016\">\u8fde\u7eed\u5185\u5b58\u7279\u6027&#xff1a;\u4fdd\u8bc1\u6570\u636e\u5b58\u50a8\u7684\u8fde\u7eed\u6027&#xff0c;\u63d0\u9ad8\u8bbf\u95ee\u6548\u7387<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u81ea\u52a8\u6269\u5bb9\u673a\u5236&#xff1a;\u5f53\u5b58\u50a8\u7a7a\u95f4\u4e0d\u8db3\u65f6\u81ea\u52a8\u6269\u5c55<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u7075\u6d3b\u5927\u5c0f\u8c03\u6574&#xff1a;\u53ef\u6839\u636e\u5b9e\u9645\u9700\u6c42\u7cbe\u786e\u63a7\u5236\u7f13\u51b2\u533a\u5927\u5c0f<\/span><\/li>\n<p><span style=\"color:#0d0016\">\u4e0d\u4f7f\u7528string\u7684\u539f\u56e0&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5b57\u7b26\u4e32\u7ec8\u6b62\u95ee\u9898&#xff1a;string\u4ee5&#039;\\\\0&#039;\u4f5c\u4e3a\u7ed3\u675f\u6807\u5fd7&#xff0c;\u5728\u5904\u7406\u7f51\u7edc\u6570\u636e\u65f6\u53ef\u80fd&#xff1a;<\/span><\/p>\n<li><span style=\"color:#0d0016\">\u7f51\u7edc\u6570\u636e\u4e2d\u5e38\u5305\u542b\u4e8c\u8fdb\u5236\u6570\u636e&#xff08;\u5982\u6587\u4ef6\u4f20\u8f93&#xff09;<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u4e8c\u8fdb\u5236\u6570\u636e\u53ef\u80fd\u5305\u542b\u591a\u4e2a&#039;\\\\0&#039;\u5b57\u7b26<\/span>&#xff0c;<span style=\"color:#0d0016\">\u4f7f\u7528string\u4f1a\u5bfc\u81f4\u6570\u636e\u622a\u65ad&#xff0c;\u9020\u6210\u4fe1\u606f\u4e22\u5931<\/span><\/li>\n<p><span style=\"color:#0d0016\">\u6027\u80fd\u8003\u8651&#xff1a;vector&lt;char&gt;\u66f4\u9002\u5408\u9891\u7e41\u7684\u6570\u636e\u5199\u5165\u548c\u8bfb\u53d6\u64cd\u4f5c<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5185\u5b58\u7ba1\u7406&#xff1a;vector\u63d0\u4f9b\u66f4\u76f4\u63a5\u7684\u5185\u5b58\u8bbf\u95ee\u548c\u63a7\u5236<\/span><\/p>\n<h4><span style=\"color:#0d0016\">\u8981\u7d20\u4e0e\u64cd\u4f5c<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u8981\u7d20<\/span><\/p>\n<p><span style=\"color:#0d0016\">1.\u9ed8\u8ba4\u7684\u7a7a\u95f4\u5927\u5c0f<\/span><\/p>\n<p><span style=\"color:#0d0016\">2.\u5f53\u524d\u7684\u8bfb\u53d6\u6570\u636e\u4f4d\u7f6e&#8212;-read<\/span><\/p>\n<p><span style=\"color:#0d0016\">3.\u5f53\u524d\u7684\u5199\u5165\u6570\u636e\u4f4d\u7f6e&#8212;-write<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u64cd\u4f5c&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">1.\u5199\u5165\u6570\u636e&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5f53\u524d\u5199\u5165\u4f4d\u7f6e\u6307\u5411\u54ea\u91cc&#xff0c;\u5c31\u4ece\u54ea\u91cc\u5f00\u59cb\u5199\u5165 \u5982\u679c\u540e\u7eed\u5269\u4f59\u7a7a\u95f2\u7a7a\u95f4\u4e0d\u591f\u4e86<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u8003\u8651\u6574\u4f53\u7f13\u51b2\u533a\u7a7a\u95f2\u7a7a\u95f4\u662f\u5426\u8db3\u591f&#xff08;\u56e0\u4e3a\u8bfb\u4f4d\u7f6e\u4e5f\u4f1a\u5411\u540e\u504f\u79fb&#xff0c;\u524d\u8fb9\u6709\u53ef\u80fd\u4f1a\u6709\u7a7a\u95f2\u7a7a\u95f4&#xff09; <\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u8db3\u591f&#xff1a;\u5c06\u6570\u636e\u79fb\u52a8\u5230\u8d77\u59cb\u4f4d\u7f6e\u5373\u53ef <\/span><\/li>\n<li><span style=\"color:#0d0016\">\u4e0d\u591f&#xff1a;\u6269\u5bb9&#xff0c;\u4ece\u5f53\u524d\u5199\u4f4d\u7f6e\u5f00\u59cb\u6269\u5bb9\u8db3\u591f\u5927\u5c0f \u6570\u636e\u4e00\u65e6\u5199\u5165\u6210\u529f&#xff0c;\u5f53\u524d\u5199\u4f4d\u7f6e&#xff0c;\u5c31\u8981\u5411\u540e\u504f\u79fb<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u5f53\u4e0b\u4e00\u6b21\u6240\u8981\u5199\u5165\u7684\u6570\u636e\u5927\u5c0f\u8d85\u8fc7\u603b\u7684\u5269\u4f59\u7a7a\u95f4&#061;read\u524d&#043;write\u540e&#xff08;\u6ce8\u610f&#xff0c;read\u6307\u9488\u4e4b\u524d\u7684\u7a7a\u95f4\u4e5f\u5305\u542b\u5728\u5185&#xff0c;\u4e5f\u5c31\u662f\u8bfb\u8fc7\u7684\u533a\u57df&#xff09;\u65f6\u5c31\u6269\u5bb9&#xff0c;\u5426\u5219\u5c31\u79fb\u52a8\u5df2\u5199\u672a\u8bfb\u7684\u5185\u5bb9\u81f3\u5f00\u59cb&#xff0c;read\u6307\u9488\u79fb\u52a8\u81f3\u5f00\u59cb&#xff0c;\u518d\u7ee7\u7eed\u5199\u5165\u3002\u6570\u636e\u4e00\u65e6\u5199\u5165\u6210\u529f&#xff0c;\u5199\u4f4d\u7f6e\u5411\u540e\u504f\u79fb\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5982\u56fe\u6240\u793a&#xff1a;<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"185\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233606-68534d66396f7.png\" width=\"439\" \/><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"167\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233606-68534d66543d5.png\" width=\"444\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u5c06\u5185\u5bb9\u5199\u5165&#xff1a;\u00a0<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"164\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233606-68534d66638a4.png\" width=\"450\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u73b0\u5728\u8fd8\u6709\u5185\u5bb9\u8981\u5199\u5165&#xff0c;\u56e0\u4e3a\u7f13\u51b2\u533a\u5df2\u7ecf\u6ca1\u6709\u7a7a\u4f59\u4f4d\u7f6e&#xff0c;\u5c31\u5bf9vector\u8fdb\u884c\u6269\u5bb9&#xff1a;<\/span><\/p>\n<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"135\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233606-68534d6672cdf.png\" width=\"397\" \/><\/p>\n<p><span style=\"color:#0d0016\">2.\u8bfb\u53d6\u6570\u636e<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5f53\u524d\u7684\u8bfb\u53d6\u4f4d\u7f6e\u6307\u5411\u54ea\u91cc&#xff0c;\u5c31\u4ece\u54ea\u91cc\u5f00\u59cb\u8bfb\u53d6&#xff0c;\u524d\u63d0\u662f\u6709\u6570\u636e\u53ef\u8bfb <\/span><\/p>\n<p><span style=\"color:#0d0016\">\u53ef\u8bfb\u6570\u636e\u5927\u5c0f&#xff1a;\u5f53\u524d\u5199\u5165\u4f4d\u7f6e&#xff0c;\u51cf\u53bb\u5f53\u524d\u8bfb\u53d6\u4f4d\u7f6e<\/span><\/p>\n<h3><span style=\"color:#0d0016\">1.2 Buffer\u7f13\u51b2\u533a\u7c7b\u529f\u80fd\u63a5\u53e3\u8bbe\u8ba1<\/span><\/h3>\n<p> #define BUFFER_DEFAULT_SIZE 1024<\/p>\n<p>class Buffer {<br \/>\nprivate:<br \/>\n    std::vector&lt;char&gt; _buffer;<br \/>\n    \/*\u662f\u4e00\u4e2a\u76f8\u5bf9\u504f\u79fb\u91cf&#xff0c;\u800c\u4e0d\u662f\u7edd\u5bf9\u5730\u5740*\/<br \/>\n    uint64_t _read_idx;   \/\/ \u76f8\u5bf9\u8bfb\u504f\u79fb\u91cf<br \/>\n    uint64_t _writer_idx; \/\/ \u76f8\u5bf9\u5199\u504f\u79fb\u91cf<\/p>\n<p>public:<br \/>\n    \/\/ \u6784\u9020\u51fd\u6570<br \/>\n    Buffer() : _read_idx(0), _writer_idx(0), _buffer(BUFFER_DEFAULT_SIZE) {}<\/p>\n<p>    \/\/ \u83b7\u53d6\u5f53\u524d\u5199\u4f4d\u7f6e\u5730\u5740&#xff1a;_Buffer\u7684\u7a7a\u95f4\u8d77\u59cb\u5730\u5740&#xff0c;\u52a0\u4e0a\u5199\u504f\u79fb\u91cf<br \/>\n    void* WritePosition();<br \/>\n    \/\/ \u83b7\u53d6\u5f53\u524d\u8bfb\u4f4d\u7f6e\u5730\u5740<br \/>\n    void* ReadPosition();<br \/>\n    \/\/ \u83b7\u53d6\u524d\u6cbf\u7a7a\u95f2\u7a7a\u95f4\u5927\u5c0f<br \/>\n    uint64_t HeadIdleSize();<br \/>\n    \/\/ \u83b7\u53d6\u540e\u6cbf\u7a7a\u95f2\u7a7a\u95f4\u5927\u5c0f<br \/>\n    uint64_t TailIdleSize();<br \/>\n    \/\/ \u83b7\u53d6\u53ef\u8bfb\u6570\u636e\u5927\u5c0f<br \/>\n    uint64_t ReadableSize();<br \/>\n    \/\/ \u5c06\u8bfb\u504f\u79fb\u5411\u540e\u79fb\u52a8\u6307\u5b9a\u957f\u5ea6<br \/>\n    void MoveReadOffset(uint64_t len);<br \/>\n    \/\/ \u5c06\u5199\u504f\u79fb\u5411\u540e\u79fb\u52a8\u6307\u5b9a\u957f\u5ea6<br \/>\n    void MoveWriteOffset(uint64_t len);<br \/>\n    \/\/ \u786e\u4fdd\u53ef\u5199\u7a7a\u95f4\u8db3\u591f&#xff08;\u79fb\u52a8&#043;\u6269\u5bb9&#xff09;<br \/>\n    void EnsureWriteSpace(uint64_t len);<br \/>\n    \/\/ \u5199\u5165\u6570\u636e<br \/>\n    void Write(void* data, uint64_t len);<br \/>\n    \/\/ \u8bfb\u53d6\u6570\u636e<br \/>\n    void Read(void* buf, uint64_t len);<br \/>\n    \/\/ \u6e05\u7406\u529f\u80fd&#xff1a;\u8bfb\u5199\u504f\u79fb\u5f520<br \/>\n    void Clear();<br \/>\n}; <\/p>\n<h3><span style=\"color:#0d0016\">1.3\u00a0Buffer\u7f13\u51b2\u533a\u7c7b\u63a5\u53e3\u5b9e\u73b0<\/span><\/h3>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;cassert&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>#define BUFFER_DEFAULT_SIZE 1024<\/p>\n<p>class Buffer {<br \/>\nprivate:<br \/>\n    std::vector&lt;char&gt; _buffer;<br \/>\n    \/*\u662f\u4e00\u4e2a\u76f8\u5bf9\u504f\u79fb\u91cf&#xff0c;\u800c\u4e0d\u662f\u7edd\u5bf9\u5730\u5740*\/<br \/>\n    uint64_t _read_idx;   \/\/ \u76f8\u5bf9\u8bfb\u504f\u79fb\u91cf<br \/>\n    uint64_t _writer_idx; \/\/ \u76f8\u5bf9\u5199\u504f\u79fb\u91cf<\/p>\n<p>public:<br \/>\n    \/\/ \u6784\u9020\u51fd\u6570<br \/>\n    Buffer() : _read_idx(0), _writer_idx(0), _buffer(BUFFER_DEFAULT_SIZE) {}<br \/>\n    char* Begin() {<br \/>\n        \/\/ &amp;*_buffer.begin(): \u8fed\u4ee3\u5668buffer.begin()\u6307\u5411\u8d77\u59cb\u5143\u7d20&#xff0c;*buffer.begin()<br \/>\n        \/\/ &amp;*_buffer.begin()&#8212;&gt;\u5f97\u5230\u7b2c0\u4e2a\u5143\u7d20\u7684\u7a7a\u95f4\u5730\u5740<br \/>\n        return &amp;*_buffer.begin();<br \/>\n    }<br \/>\n    \/\/ \u83b7\u53d6\u5f53\u524d\u5199\u4f4d\u7f6e\u5730\u5740&#xff1a;_Buffer\u7684\u7a7a\u95f4\u8d77\u59cb\u5730\u5740&#xff0c;\u52a0\u4e0a\u5199\u504f\u79fb\u91cf<br \/>\n    char* WritePosition() {<br \/>\n        \/\/ \u5c06\u83b7\u53d6_buffer\u8d77\u59cb\u5730\u5740\u7684\u8fc7\u7a0b\u7528begin()\u5c01\u88c5\u8d77\u6765<br \/>\n        return Begin() &#043; _writer_idx;<br \/>\n    }<br \/>\n    \/\/ \u83b7\u53d6\u5f53\u524d\u8bfb\u4f4d\u7f6e\u5730\u5740<br \/>\n    char* ReadPosition() { return Begin() &#043; _read_idx; }<br \/>\n    \/\/ \u83b7\u53d6\u524d\u6cbf\u7a7a\u95f2\u7a7a\u95f4\u5927\u5c0f&#xff1a;\u8bfb\u504f\u79fb<br \/>\n    uint64_t HeadIdleSize() { return _read_idx; }<br \/>\n    \/\/ \u83b7\u53d6\u540e\u6cbf\u7a7a\u95f2\u7a7a\u95f4\u5927\u5c0f&#xff1a;\u603b\u4f53\u7a7a\u95f4\u5927\u5c0f-\u5199\u504f\u79fb<br \/>\n    uint64_t TailIdleSize() { return _buffer.size() &#8211; _writer_idx; }<br \/>\n    \/\/ \u83b7\u53d6\u53ef\u5199\u7a7a\u95f4\u5927\u5c0f<br \/>\n    uint64_t WritableSize() { return HeadIdleSize() &#043; TailIdleSize(); }<br \/>\n    \/\/ \u83b7\u53d6\u53ef\u8bfb\u6570\u636e\u5927\u5c0f&#xff1a;\u5199\u504f\u79fb-\u8bfb\u504f\u79fb<br \/>\n    uint64_t ReadableSize() { return _writer_idx &#8211; _read_idx; }<br \/>\n    \/\/ \u5c06\u8bfb\u504f\u79fb\u5411\u540e\u79fb\u52a8\u6307\u5b9a\u957f\u5ea6<br \/>\n    void MoveReadOffset(uint64_t len) {<br \/>\n        \/\/ \u5411\u540e\u79fb\u52a8\u7684\u5927\u5c0f&#xff0c;\u5fc5\u987b\u5c0f\u4e8e\u53ef\u8bfb\u6570\u636e\u5927\u5c0f<br \/>\n        assert(len &lt;&#061; ReadableSize());<br \/>\n        _read_idx &#043;&#061; len;<br \/>\n    }<br \/>\n    \/\/ \u5c06\u5199\u504f\u79fb\u5411\u540e\u79fb\u52a8\u6307\u5b9a\u957f\u5ea6<br \/>\n    void MoveWriteOffset(uint64_t len) {<br \/>\n        \/\/ \u5411\u540e\u79fb\u52a8\u7684\u5927\u5c0f&#xff0c;\u5fc5\u987b\u5c0f\u4e8e\u5f53\u524d\u540e\u8fb9\u7684\u7a7a\u95f2\u7a7a\u95f4\u5927\u5c0f<br \/>\n        assert(len &lt;&#061; TailIdleSize());<\/p>\n<p>        _writer_idx &#043;&#061; len;<br \/>\n    }<br \/>\n    \/\/ \u786e\u4fdd\u53ef\u5199\u7a7a\u95f4\u8db3\u591f&#xff08;\u79fb\u52a8&#043;\u6269\u5bb9&#xff09;<br \/>\n    void EnsureWriteSpace(uint64_t len) {<br \/>\n        if (TailIdleSize() &gt;&#061; len) {<br \/>\n            return;<br \/>\n        }<br \/>\n        \/\/ \u672b\u5c3e\u7a7a\u95f2\u7a7a\u95f4\u4e0d\u591f&#xff1a;\u5219\u5224\u65ad\u52a0\u4e0a\u524d\u6cbf\u7a7a\u95f2\u7a7a\u95f4\u5927\u5c0f\u662f\u5426\u8db3\u591f&#xff0c;\u8db3\u591f\u5c31\u632a\u52a8\u6570\u636e<br \/>\n        if (HeadIdleSize() &#043; TailIdleSize() &gt;&#061; len) {<br \/>\n            \/\/ \u79fb\u52a8\u6570\u636e\u81f3\u8d77\u59cb\u4f4d\u7f6e\u5148\u4fdd\u5b58\u597d\u5f53\u524d\u6570\u636e\u5927\u5c0f<br \/>\n            uint64_t rsz &#061; ReadableSize(); \/\/ \u5148\u4fdd\u5b58\u597d\u5f53\u524d\u6570\u636e\u5927\u5c0f<br \/>\n                                           \/\/ \u5c06\u53ef\u8bfb\u6570\u636e\u62f7\u8d1d\u5230\u8d77\u59cb\u4f4d\u7f6e<br \/>\n            \/\/ \u5c06\u53ef\u8bfb\u6570\u636e\u62f7\u8d1d\u5230\u8d77\u59cb\u4f4d\u7f6e&#xff0c;\u6ce8\u610f\u7c7b\u578b\u8f6c\u6362<br \/>\n            std::copy(ReadPosition(), ReadPosition() &#043; rsz, Begin());<br \/>\n            _read_idx &#061; 0;<br \/>\n            _writer_idx &#061; rsz;<br \/>\n        } else {<br \/>\n            \/\/ \u603b\u4f53\u7a7a\u95f4\u4e0d\u591f&#xff0c;\u9700\u8981\u6269\u5bb9&#xff0c;\u4e0d\u79fb\u52a8\u6570\u636e&#xff0c;\u76f4\u63a5\u7ed9\u5199\u504f\u79fb\u4e4b\u540e\u6269\u5bb9\u8db3\u591f\u7a7a\u95f4&#xff1a;<br \/>\n            _buffer.resize(_writer_idx &#043; len);<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u5199\u5165\u6570\u636e<br \/>\n    void Write(const void* data, uint64_t len) {<br \/>\n        \/\/ 1.\u786e\u4fdd\u6709\u8db3\u591f\u7684\u7a7a\u95f4&#xff0c;2.\u62f7\u8d1d\u6570\u636e\u8fdb\u53bb<br \/>\n        EnsureWriteSpace(len);<br \/>\n        const char* d &#061; (const char*)data;<\/p>\n<p>        std::copy((char*)data, (char*)(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    }<br \/>\n    void WriteStringAndPush(const std::string&amp; data) {<br \/>\n        WriteString(data);<br \/>\n        \/\/ std::cout &lt;&lt; WritePosition() &lt;&lt; std::endl;\/\/\u6d4b\u8bd5<br \/>\n        MoveWriteOffset(data.size());<br \/>\n        \/\/ std::cout &lt;&lt; ReadableSize() &lt;&lt; std::endl;\/\/\u6d4b\u8bd5<br \/>\n    }<br \/>\n    void WriteBuffer(Buffer&amp; data) {<br \/>\n        return Write(data.ReadPosition(), data.ReadableSize());<br \/>\n    }<br \/>\n    void WriteBufferAndPush(Buffer&amp; data) {<br \/>\n        WriteBuffer(data);<br \/>\n        MoveWriteOffset(data.ReadableSize());<br \/>\n    }<br \/>\n    \/\/ \u8bfb\u53d6\u6570\u636e<br \/>\n    void Read(void* buf, uint64_t len) {<br \/>\n        assert(len &lt;&#061; ReadableSize());<br \/>\n        \/\/ std::cout &lt;&lt; &#034;&#8212;&#8212;&#034; &lt;&lt; std::endl;\/\/ \u6d4b\u8bd5<br \/>\n        \/\/ std::cout &lt;&lt; ReadPosition() &lt;&lt; std::endl; \/\/ \u6d4b\u8bd5<br \/>\n        std::copy(ReadPosition(), ReadPosition() &#043; len, (char*)buf);<br \/>\n        \/\/ std::cout &lt;&lt; buf &lt;&lt; std::endl; \/\/ \u6d4b\u8bd5<br \/>\n    }<\/p>\n<p>    \/\/ \u8bfb\u5e76\u5f39\u51fa<br \/>\n    void ReadAndPop(void* buf, uint64_t 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        \/\/ std::cout &lt;&lt; &#034;******&#034; &lt;&lt; std::endl; \/\/ \u6d4b\u8bd5<br \/>\n        \/\/ std::cout &lt;&lt; str &lt;&lt; std::endl;      \/\/ \u6d4b\u8bd5<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        \/\/ std::cout &lt;&lt; &#034;&#043;&#043;&#043;&#043;&#043;&#043;&#034; &lt;&lt; std::endl; \/\/ \u6d4b\u8bd5<br \/>\n        \/\/ std::cout &lt;&lt; str &lt;&lt; std::endl;      \/\/ \u6d4b\u8bd5<br \/>\n        return str;<br \/>\n    }<\/p>\n<p>    \/\/ \u5bfb\u627e\u6362\u884c\u5b57\u7b26<br \/>\n    char* FindCRLF() {<br \/>\n        char* res &#061; (char*)memchr(ReadPosition(), &#039;\\\\n&#039;, ReadableSize());<br \/>\n        return res;<br \/>\n    }<\/p>\n<p>    \/\/ \u83b7\u53d6\u4e00\u884c\u6570\u636e<br \/>\n    \/\/  \u901a\u5e38\u83b7\u53d6\u4e00\u884c\u6570\u636e&#xff0c;\u8fd9\u79cd\u60c5\u51b5\u9488\u5bf9\u7684\u662f<br \/>\n    std::string GetLine() {<br \/>\n        char* pos &#061; FindCRLF();<br \/>\n        if (pos &#061;&#061; nullptr) {<br \/>\n            return &#034;&#034;;<br \/>\n        }<br \/>\n        \/\/&#043;1\u662f\u4e3a\u4e86\u628a\u6362\u884c\u5b57\u7b26\u4e5f\u53d6\u51fa<br \/>\n        return ReadAsString(pos &#8211; ReadPosition() &#043; 1);<br \/>\n    }<br \/>\n    std::string GetLineAndPop() {<br \/>\n        std::string str &#061; GetLine();<br \/>\n        MoveReadOffset(str.size());<br \/>\n        return str;<br \/>\n    }<br \/>\n    \/\/ \u6e05\u7406\u529f\u80fd&#xff1a;\u8bfb\u5199\u504f\u79fb\u5f520<br \/>\n    void Clear() {<br \/>\n        _read_idx &#061; 0;<br \/>\n        _writer_idx &#061; 0;<br \/>\n    }<br \/>\n}; <\/p>\n<h3 style=\"background-color:transparent\"><span style=\"color:#0d0016\">1.4\u00a0Buffer\u7f13\u51b2\u533a\u7c7b\u7f16\u8bd1\u4e0e\u6d4b\u8bd5<\/span><\/h3>\n<p>#include &#034;server.hpp&#034;<\/p>\n<p>\/\/ \u6d4b\u8bd51<br \/>\n\/\/ int main()<br \/>\n\/\/ {<br \/>\n\/\/     Buffer buf;<\/p>\n<p>\/\/     \/\/ \u529f\u80fd\u6d4b\u8bd5<br \/>\n\/\/     std::string str &#061; &#034;hello&#034;;<br \/>\n\/\/     buf.WriteStringAndPush(str);<\/p>\n<p>\/\/     Buffer buf1;<br \/>\n\/\/     buf1.WriteBufferAndPush(buf); \/\/ \u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4e5f\u53ef\u4ee5<\/p>\n<p>\/\/     std::string tmp;<br \/>\n\/\/     \/\/ tmp &#061; buf.ReadAsStringAndPop(buf.ReadableSize());<br \/>\n\/\/     tmp &#061; buf1.ReadAsStringAndPop(buf1.ReadableSize());<\/p>\n<p>\/\/     std::cout &lt;&lt; tmp &lt;&lt; std::endl; \/\/hello<br \/>\n\/\/     std::cout &lt;&lt; buf.ReadableSize() &lt;&lt; std::endl;\/\/5<br \/>\n\/\/     std::cout &lt;&lt; buf1.ReadableSize() &lt;&lt; std::endl;\/\/0<\/p>\n<p>\/\/     return 0;<br \/>\n\/\/ }<\/p>\n<p>\/\/ \u6d4b\u8bd5\u6269\u5bb9\u548c\u83b7\u53d6\u884c<br \/>\nint main() {<br \/>\n    Buffer buf;<br \/>\n    for (int i &#061; 0; i &lt; 300; i&#043;&#043;) {<br \/>\n        std::string str &#061; &#034;hello&#034; &#043; std::to_string(i) &#043; &#039;\\\\n&#039;;<br \/>\n        buf.WriteStringAndPush(str);<br \/>\n    }<br \/>\n    \/\/ \u6d4b\u8bd5\u83b7\u53d6\u884c<\/p>\n<p>    while (buf.ReadableSize() &gt; 0) {<br \/>\n        std::string line &#061; buf.GetLineAndPop();<br \/>\n        std::cout &lt;&lt; line &lt;&lt; std::endl;<br \/>\n    }<br \/>\n    \/\/ \u6d4b\u8bd5\u6269\u5bb9<br \/>\n    \/\/ std::string tmp;<br \/>\n    \/\/ tmp &#061; buf.ReadAsStringAndPop(buf.ReadableSize());<br \/>\n    \/\/ std::cout &lt;&lt; tmp &lt;&lt; std::endl;<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<h2><span style=\"color:#0d0016\">\u4e8c\u3001\u65e5\u5fd7\u5b8f\u7684\u5b9e\u73b0<\/span><\/h2>\n<\/p>\n<p><span style=\"color:#0d0016\">\u65e5\u5fd7\u7f16\u5199\u53ef\u4ee5\u770b\u6211\u7684\u8fd9\u7bc7\u535a\u5ba2&#xff0c;\u6709\u8be6\u7ec6\u7684\u89e3\u8bf4<\/span><\/p>\n<h3><span style=\"color:#0d0016\">2.1 \u6211\u4eec\u8fd9\u91cc\u4ee5\u4e0a\u9762\u7684\u6d4b\u8bd5\u4e3a\u4f8b&#xff0c;\u6dfb\u52a0\u5b8f&#xff1a;<\/span><\/h3>\n<p> #define LOG(format, &#8230;)                                                       \\\\<br \/>\n    fprintf(stdout, &#034;[%s:%d] &#034; format, __FILE__, __LINE__, __VA_ARGS__)<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"226\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233606-68534d6682749.png\" width=\"367\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u8f93\u51fa&#xff1a;<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"206\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233606-68534d66c933e.png\" width=\"352\" \/><\/p>\n<h3><span style=\"color:#0d0016\">2.2 \u6ca1\u6709\u4e0d\u5b9a\u53c2\u6570&#xff1a;##__VA_ARGS__<\/span><\/h3>\n<p> #define LOG(format, &#8230;)                                                       \\\\<br \/>\n    fprintf(stdout, &#034;[%s:%d] &#034; format, __FILE__, __LINE__, ##__VA_ARGS__)<\/p>\n<p><span style=\"color:#0d0016\">\u8f93\u51fa&#xff1a;<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"286\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233606-68534d66db5c3.png\" width=\"389\" \/><\/p>\n<h3><span style=\"color:#0d0016\">2.3 \u6dfb\u52a0\u65f6\u95f4&#xff1a;#include&lt;ctime&gt;<\/span><\/h3>\n<p> #define LOG(format, &#8230;)                                                                    \\\\<br \/>\n    do                                                                                      \\\\<br \/>\n    {                                                                                       \\\\<br \/>\n        time_t t &#061; time(nullptr);                                                           \\\\<br \/>\n        struct tm *ltm &#061; localtime(&amp;t);                                                     \\\\<br \/>\n        char tmp[32] &#061; {0};                                                                 \\\\<br \/>\n        strftime(tmp, 31, &#034;%H:%M:%S&#034;, ltm);                                                 \\\\<br \/>\n        fprintf(stdout, &#034;[%s:%s:%d] &#034; format &#034;\\\\n&#034;, tmp, __FILE__, __LINE__, ##__VA_ARGS__); \\\\<br \/>\n    } while (0) <\/p>\n<p><span style=\"color:#0d0016\">\u6548\u679c&#xff1a;<\/span><\/p>\n<\/p>\n<p> [15:52:09:main.cc:53] hi, pupu<\/p>\n<h3><span style=\"color:#0d0016\">2.4 \u6dfb\u52a0\u7b49\u7ea7<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u65e5\u5fd7\u6253\u5370\u5b8f&#xff0c;\u53ef\u4ee5\u63a7\u5236\u65e5\u5fd7\u7b49\u7ea7<\/span><\/p>\n<p> #define INF 0<br \/>\n#define DBG 1<br \/>\n#define ERR 2<br \/>\n#define LOG_LEVEL DBG \/\/ \u63a7\u5236&#xff1a; \u8c03\u6574\u6253\u5370\u7b49\u7ea7\u81f3INF\u7684\u7b49\u7ea7\u624d\u80fd\u6253\u5370<\/p>\n<p>#define LOG(LEVEL, format, &#8230;)                                                \\\\<br \/>\n    do {                                                                       \\\\<br \/>\n        if (LEVEL &lt; LOG_LEVEL)                                                 \\\\<br \/>\n            break;                                                             \\\\<br \/>\n        time_t t &#061; time(nullptr);                                              \\\\<br \/>\n        struct tm* ltm &#061; localtime(&amp;t);                                        \\\\<br \/>\n        char tmp[32] &#061; {0};                                                    \\\\<br \/>\n        strftime(tmp, 31, &#034;%H:%M:%S&#034;, ltm);                                    \\\\<br \/>\n        fprintf(stdout, &#034;[%s:%s:%d] &#034; format &#034;\\\\n&#034;, tmp, __FILE__, __LINE__,           \\\\<br \/>\n         ##__VA_ARGS__);                                                       \\\\<br \/>\n    } while (0)<\/p>\n<p>#define INF_LOG(format, &#8230;) LOG(INF, format, ##__VA_ARGS__)<br \/>\n#define DBG_LOG(format, &#8230;) LOG(DBG, format, ##__VA_ARGS__)<br \/>\n#define ERR_LOG(format, &#8230;) LOG(ERR, format, ##__VA_ARGS__) <\/p>\n<p><span style=\"color:#0d0016\">\u6d4b\u8bd5&#xff1a;\u56e0\u4e3a\u6211\u8bbe\u7f6e\u7684\u6253\u5370\u7b49\u7ea7\u9700\u8981INF\u4ee5\u4e0a&#xff0c;\u6240\u4ee5\u65e0\u8f93\u51fa<\/span><\/p>\n<p>         INF_LOG(&#034;hi, pupu&#034;);<\/p>\n<h2><span style=\"color:#0d0016\">\u4e09\u3001Socket\u5957\u63a5\u5b57\u6a21\u5757<\/span>\u00a0<\/h2>\n<p><span style=\"color:#0d0016\">Socket\u5957\u63a5\u5b57\u7684\u8be6\u7ec6\u89e3\u8bf4\u53ef\u4ee5\u770b\u6211\u7684\u8fd9\u51e0\u7bc7\u6587\u7ae0&#xff1a;<\/span><\/p>\n<p>UDP\u5957\u63a5\u5b57\u7f16\u7a0b\u00a0TCP\u5957\u63a5\u5b57\u7f16\u7a0b\u00a0\u5c01\u88c5\u597d\u7684Socket.hpp&#xff0c;\u6784\u5efa\u7f51\u7edc\u7248\u8ba1\u7b97\u5668<\/p>\n<p><span style=\"color:#0d0016\">\u529f\u80fd&#xff1a;\u5bf9\u5957\u63a5\u5b57\u7684\u64cd\u4f5c\u8fdb\u884c\u5c01\u88c5<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u610f\u4e49&#xff1a;\u7a0b\u5e8f\u4e2d\u5bf9\u4e8e\u5957\u63a5\u5b57\u7684\u5404\u9879\u64cd\u4f5c\u66f4\u52a0\u7b80\u4fbf<\/span><\/p>\n<h3><span style=\"color:#0d0016\">3.1 Socket\u5957\u63a5\u5b57\u7c7b\u529f\u80fd\u8bbe\u8ba1<\/span><\/h3>\n<\/p>\n<p> #define Max_LISTEN 1024<br \/>\nclass Socket {<br \/>\nprivate:<br \/>\n    int _sockfd;<\/p>\n<p>public:<br \/>\n    \/\/ \u5957\u63a5\u5b57\u57fa\u7840\u64cd\u4f5c<\/p>\n<p>    Socket();<br \/>\n    Socket(int fd);<br \/>\n    ~Socket();<br \/>\n    \/\/ \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n    bool Create();<br \/>\n    \/\/ \u7ed1\u5b9a\u5730\u5740\u4fe1\u606f<br \/>\n    bool Bind(const std::string&amp; ip, uint16_t port);<br \/>\n    \/\/ \u5f00\u59cb\u76d1\u542c<br \/>\n    bool Listen(int backlog);<br \/>\n    \/\/ \u5411\u670d\u52a1\u5668\u53d1\u8d77\u8fde\u63a5<br \/>\n    bool Connect(const std::string&amp; ip, uint16_t port);<br \/>\n    \/\/ \u83b7\u53d6\u65b0\u8fde\u63a5<br \/>\n    int Accept(); \/\/ \u8fd4\u56de\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    \/\/ \u63a5\u6536\u6570\u636e<br \/>\n    ssize_t<br \/>\n    Recv(void* buf, size_t len,<br \/>\n         int flag &#061; 0); \/\/ ssize_t&#xff1a;\u6709\u7b26\u53f7\u7684\u957f\u6574\u578b, flag &#061; 0&#xff1a;\u9ed8\u8ba4\u963b\u585e\u64cd\u4f5c<br \/>\n    \/\/ \u53d1\u9001\u6570\u636e<br \/>\n    ssize_t Send(void* buf, size_t len, int flag &#061; 0);<br \/>\n    \/\/ \u5173\u95ed\u5957\u63a5\u5b57<br \/>\n    void Close();<\/p>\n<p>    \/\/ \u5957\u63a5\u5b57\u5176\u4f59\u91cd\u8981\u64cd\u4f5c<br \/>\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u7aef&#xff08;\u76d1\u542c&#xff09;\u8fde\u63a5<br \/>\n    bool CreateServer(uint16_t port,<br \/>\n                      const std::string&amp; ip &#061;<br \/>\n                          &#034;0.0.0.0&#034;); \/\/ \u4e3b\u673a\u4e00\u822c\u4f1a\u7ed1\u5b9a\u5f53\u524d\u4e3b\u673a\u4e0a\u6240\u6709\u7684\u7f51\u5361<br \/>\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5<br \/>\n    bool CreateClient(uint16_t port, const std::string&amp; ip);<br \/>\n    \/\/ \u8bbe\u7f6e\u5957\u63a5\u5b57\u9009\u9879&#8211;\u5f00\u542f\u5730\u5740\u7aef\u53e3\u91cd\u7528<br \/>\n    void ReuseAddress();<br \/>\n    \/\/ \u8bbe\u7f6e\u5957\u63a5\u5b57\u963b\u585e\u5c5e\u6027&#8211;\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e&#xff08;\u56e0\u4e3a\u9ed8\u8ba4\u662f\u963b\u585e&#xff09;<br \/>\n    void NonBlock();<br \/>\n}; <\/p>\n<h3 style=\"background-color:transparent\"><span style=\"color:#0d0016\">3.2 Socket\u5957\u63a5\u5b57\u7c7b\u63a5\u53e3\u5b9e\u73b0<\/span><\/h3>\n<p>#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<\/p>\n<p>#include &lt;arpa\/inet.h&gt; \/\/\u5b57\u8282\u5e8f\u8f6c\u6362<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>#define Max_LISTEN 1024<br \/>\nclass Socket {<br \/>\nprivate:<br \/>\n    int _sockfd;<\/p>\n<p>public:<br \/>\n    Socket() : _sockfd(-1) {}<br \/>\n    Socket(int fd) : _sockfd(fd) {}<br \/>\n    ~Socket() { Close(); }<br \/>\n    int Fd() { return _sockfd; }<br \/>\n    \/\/ \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n    bool Create() {<br \/>\n        \/\/ int socket(int domain ,int type, int protocol);<br \/>\n        _sockfd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n        if (_sockfd &lt; 0) {<br \/>\n            ERR_LOG(&#034;CREATE SOCKET FAILED&#8230;&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u7ed1\u5b9a\u5730\u5740\u4fe1\u606f<br \/>\n    bool Bind(const std::string&amp; ip, uint16_t port) {<br \/>\n        struct sockaddr_in addr;<br \/>\n        addr.sin_family &#061; AF_INET;<br \/>\n        addr.sin_port &#061; htons(port);<br \/>\n        addr.sin_addr.s_addr &#061; inet_addr(ip.c_str());<br \/>\n        socklen_t len &#061; sizeof(struct sockaddr_in);<br \/>\n        \/\/ int bind(int sockfd, struct sockaddr *addr, socklen_t len);<br \/>\n        int ret &#061; bind(_sockfd, (struct sockaddr*)&amp;addr, len);<br \/>\n        if (ret &lt; 0) {<br \/>\n            ERR_LOG(&#034;BIND ADDRESS FAILED&#8230;&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u5f00\u59cb\u76d1\u542c<br \/>\n    bool Listen(int backlog) {<br \/>\n        \/\/ int listen(int socket, int backlog); \/\/<br \/>\n        \/\/ backlog:\u8bbe\u7f6e\u4ee5\u4e0b\u540c\u4e00\u65f6\u95f4\u6700\u5927\u5e76\u53d1\u8fde\u63a5\u6570<br \/>\n        int ret &#061; listen(_sockfd, backlog);<br \/>\n        if (ret &lt; 0) {<br \/>\n            ERR_LOG(&#034;SOCKET LISTEN FAILED&#8230;&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u5411\u670d\u52a1\u5668\u53d1\u8d77\u8fde\u63a5<br \/>\n    bool Connect(const std::string&amp; ip, uint16_t port) {<br \/>\n        struct sockaddr_in addr;<br \/>\n        addr.sin_family &#061; AF_INET;<br \/>\n        addr.sin_port &#061; htons(port);<br \/>\n        addr.sin_addr.s_addr &#061; inet_addr(ip.c_str());<br \/>\n        socklen_t len &#061; sizeof(struct sockaddr_in);<br \/>\n        \/\/ int connect(int sockfd, struct sockaddr *addr, socklen_t len);<br \/>\n        int ret &#061; connect(_sockfd, (struct sockaddr*)&amp;addr, len);<br \/>\n        if (ret &lt; 0) {<br \/>\n            ERR_LOG(&#034;CONNECT ADDRESS FAILED&#8230;&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u83b7\u53d6\u65b0\u8fde\u63a5: \u8fd4\u56de\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    int Accept() {<br \/>\n        \/\/ int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);<br \/>\n        int newfd &#061; accept(_sockfd, nullptr, nullptr);<br \/>\n        if (newfd &lt; 0) {<br \/>\n            ERR_LOG(&#034;SOCKET ACCEPT FAILED&#8230;&#034;);<br \/>\n            return -1;<br \/>\n        }<br \/>\n        return newfd;<br \/>\n    }<br \/>\n    \/\/ \u63a5\u6536\u6570\u636e<br \/>\n    \/\/ ssize_t&#xff1a;\u6709\u7b26\u53f7\u7684\u957f\u6574\u578b, flag &#061; 0&#xff1a;\u9ed8\u8ba4\u963b\u585e\u64cd\u4f5c<br \/>\n    ssize_t Recv(void* buf, size_t len, int flag &#061; 0) {<br \/>\n        \/\/ ssize_t recv(int sockfd, void *buf, size_t len, int flags);<br \/>\n        ssize_t ret &#061; recv(_sockfd, buf, len, flag);<br \/>\n        if (ret &lt; 0) {<br \/>\n            \/\/ EAGAIN:<br \/>\n            \/\/ \u5f53\u524dsocket\u7684\u63a5\u6536\u7f13\u51b2\u533a\u4e2d\u6ca1\u6709\u6570\u636e\u4e86&#xff0c;\u5728\u975e\u963b\u585e\u7684\u60c5\u51b5\u4e0b\u624d\u4f1a\u6709\u8fd9\u4e2a\u9519\u8bef<br \/>\n            \/\/ EINTR: \u5f53\u524dsocket\u7684\u963b\u585e\u7b49\u5f85&#xff0c;\u88ab\u4fe1\u53f7\u6253\u65ad\u4e86&#xff0c;<br \/>\n            if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EINTR) {<br \/>\n                ERR_LOG(&#034;SOCKET RECV NULL&#8230;&#034;);<\/p>\n<p>                return 0; \/\/ \u8868\u793a\u8fd9\u6b21\u6ca1\u6709\u63a5\u6536\u5230\u6570\u636e<br \/>\n            }<br \/>\n            ERR_LOG(&#034;SOCKET RECV FAILED&#8230;&#034;);<br \/>\n            return -1;<br \/>\n        }<br \/>\n        \/\/ INF_LOG(&#034;%ld&#034;, ret);<br \/>\n        return ret; \/\/ \u5b9e\u9645\u63a5\u6536\u5230\u7684\u6570\u636e\u957f\u5ea6<br \/>\n    }<\/p>\n<p>    \/\/ \u975e\u963b\u585e\u63a5\u6536\u6570\u636e<br \/>\n    ssize_t NonBlockRecv(void* buf, size_t len) {<br \/>\n        return Recv(buf, len,<br \/>\n                    MSG_DONTWAIT); \/\/ MSG_DONTWAIT: \u8868\u793a\u5f53\u524d\u63a5\u6536\u4e3a\u975e\u963b\u585e<br \/>\n    }<\/p>\n<p>    \/\/ \u53d1\u9001\u6570\u636e<br \/>\n    ssize_t Send(const void* buf, size_t len, int flag &#061; 0) {<br \/>\n        \/\/ ssize_t send(int sockfd, const void *buf, size_t len, int flags);<br \/>\n        ssize_t ret &#061; send(_sockfd, buf, len, flag);<br \/>\n        if (ret &lt; 0) {<br \/>\n            if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EINTR) {<br \/>\n                ERR_LOG(&#034;SOCKET RECV NULL&#8230;&#034;);<\/p>\n<p>                return 0; \/\/ \u8868\u793a\u8fd9\u6b21\u6ca1\u6709\u63a5\u6536\u5230\u6570\u636e<br \/>\n            }<br \/>\n            ERR_LOG(&#034;SOCKET SEND FAILED&#8230;&#034;);<br \/>\n            return -1;<br \/>\n        }<br \/>\n        \/\/ ret:<br \/>\n        \/\/ \u5b9e\u9645\u53d1\u9001\u7684\u6570\u636e\u957f\u5ea6&#xff0c;\u6709\u53ef\u80fdbuf\u4e2d\u6570\u636e\u5e76\u6ca1\u6709\u53d1\u9001\u5b8c&#xff0c;\u5916\u90e8\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u53d1\u9001\u957f\u5ea6\u6765\u51b3\u5b9a\u5982\u4f55\u7ee7\u7eed\u5904\u7406<br \/>\n        \/\/ std::cout &lt;&lt; ret &lt;&lt; std::endl;<br \/>\n        return ret;<br \/>\n    }<br \/>\n    \/\/ \u975e\u963b\u585e\u53d1\u9001\u6570\u636e<br \/>\n    ssize_t NonBlockSend(void* buf, size_t len) {<br \/>\n        return Send(buf, len,<br \/>\n                    MSG_DONTWAIT); \/\/ MSG_DONTWAIT: \u8868\u793a\u5f53\u524d\u53d1\u9001\u4e3a\u975e\u963b\u585e<br \/>\n    }<\/p>\n<p>    \/\/ \u5173\u95ed\u5957\u63a5\u5b57<br \/>\n    void Close() {<br \/>\n        if (_sockfd !&#061; -1) {<br \/>\n            close(_sockfd);<br \/>\n            _sockfd &#061; -1;<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u5957\u63a5\u5b57\u5176\u4f59\u91cd\u8981\u64cd\u4f5c<br \/>\n    \/\/  \u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u7aef&#xff08;\u76d1\u542c&#xff09;\u8fde\u63a5, \u4e3b\u673a\u4e00\u822c\u4f1a\u7ed1\u5b9a\u5f53\u524d\u4e3b\u673a\u4e0a\u6240\u6709\u7684\u7f51\u5361<br \/>\n    bool CreateServer(uint16_t port, const std::string&amp; ip &#061; &#034;0.0.0.0&#034;,<br \/>\n                      bool block_flag &#061; false) {<br \/>\n        \/\/ 1.\u521b\u5efa\u5957\u63a5\u5b57 2.\u7ed1\u5b9a\u5730\u5740 3.\u5f00\u59cb\u76d1\u542c 4.\u8bbe\u7f6e\u975e\u963b\u585e 5.\u542f\u52a8\u5730\u5740\u91cd\u7528<br \/>\n        if (Create() &#061;&#061; false)<br \/>\n            return false;<br \/>\n        if (Bind(ip, port) &#061;&#061; false)<br \/>\n            return false;<br \/>\n        if (Listen(1024) &#061;&#061; false)<br \/>\n            return false;<br \/>\n        if (block_flag)<br \/>\n            NonBlock();<br \/>\n        ReuseAddress();<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5<br \/>\n    bool CreateClient(uint16_t port, const std::string&amp; ip) {<br \/>\n        \/\/ 1.\u521b\u5efa\u5957\u63a5\u5b57 2.\u76f4\u63a5\u8fde\u63a5\u670d\u52a1\u5668 3.\u5f00\u59cb\u76d1\u542c 4.\u8bbe\u7f6e\u975e\u963b\u585e 5.\u542f\u52a8\u5730\u5740\u91cd\u7528<br \/>\n        if (Create() &#061;&#061; false) {<br \/>\n            DBG_LOG(&#034;SOCKET CREATECLIENT FAILED&#8230;&#034;);<br \/>\n            return false;<br \/>\n        }<\/p>\n<p>        if (Connect(ip, port) &#061;&#061; false)<br \/>\n            return false;<br \/>\n        INF_LOG(&#034;SOCKET CREATECLIENT SUCCESS&#8230;&#034;);<\/p>\n<p>        return true;<br \/>\n    }<br \/>\n    \/\/ \u8bbe\u7f6e\u5957\u63a5\u5b57\u9009\u9879&#8211;\u5f00\u542f\u5730\u5740\u7aef\u53e3\u91cd\u7528<br \/>\n    void ReuseAddress() {<br \/>\n        \/\/  int setsockopt(int sockfd, int level, int optname,<br \/>\n        \/\/               const void *optval, socklen_t optlen);<\/p>\n<p>        int val &#061; 1;<br \/>\n        \/\/ \u5730\u5740\u91cd\u7528<br \/>\n        setsockopt(_sockfd, SOL_SOCKET, SO_REUSEADDR, &amp;val, sizeof(int));<br \/>\n        val &#061; 1;<br \/>\n        \/\/ \u7aef\u53e3\u91cd\u7528<br \/>\n        setsockopt(_sockfd, SOL_SOCKET, SO_REUSEPORT, &amp;val, sizeof(int));<br \/>\n    }<br \/>\n    \/\/ \u8bbe\u7f6e\u5957\u63a5\u5b57\u963b\u585e\u5c5e\u6027&#8211;\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e&#xff08;\u56e0\u4e3a\u9ed8\u8ba4\u662f\u963b\u585e&#xff09;<br \/>\n    void NonBlock() {<br \/>\n        \/\/ int fcntl(int fd, int cmd, &#8230; \/* arg *\/ );<br \/>\n        int flag &#061; fcntl(_sockfd, F_GETFL, 0);<br \/>\n        fcntl(_sockfd, F_SETFL, flag | O_NONBLOCK);<br \/>\n    }<br \/>\n}; <\/p>\n<h3><span style=\"color:#0d0016\">3.3 Socket\u5957\u63a5\u5b57\u7c7b\u7f16\u8bd1\u6d4b\u8bd5<\/span><\/h3>\n<p><span style=\"color:#0d0016\">tcp_srv.cc<\/span><\/p>\n<p> #include &#034;..\/source\/server.hpp&#034;<\/p>\n<p>int main() {<br \/>\n    Socket lst_sock;<br \/>\n    lst_sock.CreateServer(8500);<br \/>\n    while (1) {<br \/>\n        int newfd &#061; lst_sock.Accept();<br \/>\n        if (newfd &lt; 0) {<br \/>\n            continue;<br \/>\n        }<br \/>\n        Socket cli_sock(newfd);<br \/>\n        char buffer[1024] &#061; {0};<br \/>\n        int ret &#061; cli_sock.Recv(buffer, 1023);<br \/>\n        if (ret &lt; 0) {<br \/>\n            cli_sock.Close();<br \/>\n            continue;<br \/>\n        }<br \/>\n        cli_sock.Send(buffer, ret);<br \/>\n        cli_sock.Close();<br \/>\n    }<\/p>\n<p>    lst_sock.Close();<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<p><span style=\"color:#0d0016\">tcp_cli.cc<\/span><\/p>\n<p> #include &#034;..\/source\/server.hpp&#034;<\/p>\n<p>int main() {<br \/>\n    Socket cli_sock;<br \/>\n    cli_sock.CreateClient(8500, &#034;127.0.0.1&#034;);<br \/>\n    std::string str &#061; &#034;hi pupu&#034;;<br \/>\n    cli_sock.Send((void*)str.c_str(), str.size());<br \/>\n    char buf[1024] &#061; {0};<br \/>\n    cli_sock.Recv(buf, 1023);<br \/>\n    DBG_LOG(&#034;%s&#034;, buf);<br \/>\n    return 0;<br \/>\n} <\/p>\n<h2><span style=\"color:#0d0016\">\u56db\u3001Channel\u6a21\u5757<\/span><\/h2>\n<p><span style=\"color:#0d0016\">\u76ee\u7684&#xff1a;\u5bf9\u63cf\u8ff0\u7b26\u7684\u76d1\u63a7\u4e8b\u4ef6\u7ba1\u7406<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u529f\u80fd&#xff1a;\u5bf9\u4e8e\u4e00\u4e2a\u63cf\u8ff0\u7b26\u8fdb\u884c\u76d1\u63a7\u4e8b\u4ef6\u7ba1\u7406<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u610f\u4e49&#xff1a;\u5bf9\u4e8e\u63cf\u8ff0\u7b26\u7684\u76d1\u63a7\u4e8b\u4ef6\u5728\u7528\u6237\u6001\u66f4\u5bb9\u6613\u7ef4\u62a4&#xff0c;\u4ee5\u53ca\u89e6\u53d1\u4e8b\u4ef6\u540e\u7684\u64cd\u4f5c\u6d41\u7a0b\u66f4\u52a0\u7684\u6e05\u6670<\/span><\/p>\n<h3><span style=\"color:#0d0016\">4.1 Channel\u4e8b\u4ef6\u7ba1\u7406\u7c7b\u529f\u80fd\u8bbe\u8ba1<\/span><\/h3>\n<p><span style=\"color:#0d0016\">a\u3001\u5bf9\u76d1\u63a7\u4e8b\u4ef6\u7684\u7ba1\u7406<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u662f\u5426\u6b63\u5728\u76d1\u6d4b\u63cf\u8ff0\u7b26\u7684\u8bfb\u72b6\u6001 <\/span><\/p>\n<p><span style=\"color:#0d0016\">\u662f\u5426\u6b63\u5728\u76d1\u6d4b\u63cf\u8ff0\u7b26\u7684\u5199\u72b6\u6001<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5f00\u59cb\u76d1\u6d4b\u63cf\u8ff0\u7b26\u7684\u8bfb\u4e8b\u4ef6 <\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5f00\u59cb\u76d1\u6d4b\u63cf\u8ff0\u7b26\u7684\u5199\u4e8b\u4ef6<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u505c\u6b62\u76d1\u6d4b\u8bfb\u4e8b\u4ef6 <\/span><\/p>\n<p><span style=\"color:#0d0016\">\u505c\u6b62\u76d1\u6d4b\u5199\u4e8b\u4ef6 <\/span><\/p>\n<p><span style=\"color:#0d0016\">\u505c\u6b62\u6240\u6709\u4e8b\u4ef6\u76d1\u6d4b<\/span><\/p>\n<p><span style=\"color:#0d0016\">b\u3001\u5bf9\u4e8b\u4ef6\u89e6\u53d1\u540e\u7684\u5904\u7406<\/span><\/p>\n<p><span style=\"color:#0d0016\">1.\u9700\u8981\u5904\u7406\u7684\u4e8b\u4ef6&#xff1a;\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u6302\u65ad\u3001\u9519\u8bef\u3001\u4efb\u610f<\/span><\/p>\n<p><span style=\"color:#0d0016\">2.\u4e8b\u4ef6\u5904\u7406\u7684\u56de\u8c03\u51fd\u6570<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5c06\u4f7f\u7528epoll\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7&#xff0c;epoll\u7684\u8be6\u89e3&#xff1a;<\/span><\/p>\n<p>\u591a\u8def\u8f6c\u63a5\u8be6\u89e3\u00a0\u00a0 \u00a0 epoll\u7684\u673a\u5236\u4e0e\u5b9e\u73b0\u539f\u7406\u00a0epoll\u8be6\u7ec6\u8bb2\u89e3\u3001ET\u6a21\u5f0f\u3001\u57fa\u4e8eReactor\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7f51\u7edc\u670d\u52a1\u5668<\/p>\n<p><span style=\"color:#0d0016\">\u6210\u5458<\/span><\/p>\n<p><span style=\"color:#0d0016\">EPOLLIN \u53ef\u8bfb EPOLLOUT \u53ef\u5199 EPOLLRDHUP \u8fde\u63a5\u65ad\u5f00 EPOLLPRI \u4f18\u5148\u6570\u636e EPOLLERR \u51fa\u9519\u4e86 EPOLLHUP \u6302\u65ad \u800c\u4ee5\u4e0a\u7684\u4e8b\u4ef6\u90fd\u662f\u4e00\u4e2a\u6570\u503c uint32_t \u8fdb\u884c\u4fdd\u5b58<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u8981\u8fdb\u884c\u4e8b\u4ef6\u7ba1\u7406&#xff0c;\u5c31\u9700\u8981\u6709\u4e00\u4e2auint32_t \u7c7b\u578b\u7684\u6210\u5458\u4fdd\u5b58\u5f53\u524d\u9700\u8981\u76d1\u63a7\u7684\u4e8b\u4ef6<\/span><\/p>\n<p><span style=\"color:#faa572\">\u4e8b\u4ef6\u5904\u7406\u8fd9\u91cc&#xff0c;\u56e0\u4e3a\u6709\u4e94\u79cd\u4e8b\u4ef6\u9700\u8981\u5904\u7406&#xff0c;\u5c31\u9700\u8981\u4e94\u4e2a\u56de\u8c03\u51fd\u6570<\/span><\/p>\n<p>class Channel {<br \/>\nprivate:<br \/>\n    int _fd;<br \/>\n    uint32_t _events;  \/\/ \u5f53\u524d\u9700\u8981\u76d1\u63a7\u7684\u4e8b\u4ef6<br \/>\n    uint32_t _revents; \/\/ \u5f53\u524d\u8fde\u63a5\u89e6\u53d1\u7684\u4e8b\u4ef6<br \/>\n    using EventCallBack &#061; std::function&lt;void()&gt;;<br \/>\n    EventCallBack _read_callback;     \/\/ \u53ef\u8bfb\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    EventCallBack _write_callback;    \/\/ \u53ef\u5199\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    EventCallBack _error_callback;    \/\/ \u9519\u8bef\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    EventCallBack _close_callback;    \/\/ \u8fde\u63a5\u65ad\u5f00\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    EventCallBack _anyevent_callback; \/\/ \u4efb\u610f\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\npublic:<br \/>\n    Channel();<br \/>\n    \/\/ \u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u4e4b\u540e&#xff0c;fd\u5c31\u7eea\u4e86\u7684\u4e8b\u4ef6&#xff0c;<br \/>\n    \/\/ EventLoop\u5c31\u4f1a\u901a\u8fc7\u8fd9\u4e2a\u63a5\u53e3\u628a\u5b9e\u9645\u5c31\u7eea\u7684\u4e8b\u4ef6\u8bbe\u7f6e\u8fdb\u6765&#xff0c;\u6839\u636erevents\u5224\u65ad\u4ec0\u4e48\u4e8b\u4ef6\u5c31\u7eea<br \/>\n    void SetREvent(uint32_t events){}<br \/>\n    \/\/ \u8bbe\u7f6e\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u4f60\u4f20\u51fd\u6570&#xff0c;\u6211\u56de\u8c03<br \/>\n    void SetReadCallBack(const EventCallBack&amp; cb);<br \/>\n    void SetWriteCallBack(const EventCallBack&amp; cb);<br \/>\n    void SetErrorCallBack(const EventCallBack&amp; cb);<br \/>\n    void SetCloseCallBack(const EventCallBack&amp; cb);<br \/>\n    void SetAnyEventCallBack(const EventCallBack&amp; cb);<br \/>\n    \/\/ \u5f53\u524d\u662f\u5426\u76d1\u63a7\u53ef\u8bfb<br \/>\n    bool Readable();<br \/>\n    \/\/ \u5f53\u524d\u662f\u5426\u76d1\u63a7\u53ef\u5199<br \/>\n    bool Writable();<br \/>\n    \/\/ \u542f\u52a8\u8bfb\u4e8b\u4ef6\u76d1\u63a7 &#8211;&gt;\u6302\u5230EventLoop\u4e0a<br \/>\n    void EnableRead();<br \/>\n    \/\/ \u542f\u52a8\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void EnableWrite();<br \/>\n    \/\/ \u89e3\u9664\u53ef\u8bfb\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void DisableRead();<br \/>\n    \/\/ \u89e3\u9664\u53ef\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void DisableWrite();<br \/>\n    \/\/ \u89e3\u9664\u6240\u6709\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void DisableAll();<br \/>\n    \/\/ \u79fb\u9664\u76d1\u63a7&#xff1a;\u4eceepoll\u7ea2\u9ed1\u6811\u4e0a\u76f4\u63a5\u79fb\u9664&#xff0c;\u4eceEventLoop\u4e0a\u79fb\u9664<br \/>\n    void Remove();<br \/>\n    \/\/ \u4e8b\u4ef6\u5904\u7406&#xff1a;\u4e00\u65e6\u8fde\u63a5\u89e6\u53d1\u4e86\u4e8b\u4ef6&#xff0c;\u5c31\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570&#xff0c;EventLoop\u6a21\u5757\u53ea\u7ba1\u76d1\u63a7\u89e6\u53d1\u4f55\u79cd\u4e8b\u4ef6,\u8c03\u7528Channel\u5904\u7406\u51fd\u6570<br \/>\n    \/\/ \u5982\u4f55\u5904\u7406\u7531Channel\u51b3\u5b9a<br \/>\n    void HandleEvent();<br \/>\n}; <\/p>\n<h3><span style=\"color:#0d0016\">4.2 Channel\u4e8b\u4ef6\u7ba1\u7406\u7c7b\u63a5\u53e3\u5b9e\u73b0<\/span><\/h3>\n<p>#include &lt;functional&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\nclass Channel {<br \/>\nprivate:<br \/>\n    int _fd;<br \/>\n    uint32_t _events;  \/\/ \u5f53\u524d\u9700\u8981\u76d1\u63a7\u7684\u4e8b\u4ef6<br \/>\n    uint32_t _revents; \/\/ \u5f53\u524d\u8fde\u63a5\u89e6\u53d1\u7684\u4e8b\u4ef6<br \/>\n    using EventCallBack &#061; std::function&lt;void()&gt;;<br \/>\n    EventCallBack _read_callback;     \/\/ \u53ef\u8bfb\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    EventCallBack _write_callback;    \/\/ \u53ef\u5199\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    EventCallBack _error_callback;    \/\/ \u9519\u8bef\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    EventCallBack _close_callback;    \/\/ \u8fde\u63a5\u65ad\u5f00\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    EventCallBack _anyevent_callback; \/\/ \u4efb\u610f\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\npublic:<br \/>\n    Channel(int fd) : _fd(fd), _events(0), _revents(0) {}<br \/>\n    int Fd() { return _fd; }<br \/>\n    uint32_t Events() { return _events; } \/\/ \u83b7\u53d6\u60f3\u8981\u76d1\u63a7\u7684\u4e8b\u4ef6<\/p>\n<p>    \/\/ \u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u4e4b\u540e&#xff0c;fd\u5c31\u7eea\u4e86\u7684\u4e8b\u4ef6&#xff0c;<br \/>\n    \/\/ EventLoop\u5c31\u4f1a\u901a\u8fc7\u8fd9\u4e2a\u63a5\u53e3\u628a\u5b9e\u9645\u5c31\u7eea\u7684\u4e8b\u4ef6\u8bbe\u7f6e\u8fdb\u6765&#xff0c;\u6839\u636erevents\u5224\u65ad\u4ec0\u4e48\u4e8b\u4ef6\u5c31\u7eea<br \/>\n    void SetREvent(uint32_t events) { _revents &#061; events; }<br \/>\n    \/\/ \u8bbe\u7f6e\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u4f60\u4f20\u51fd\u6570&#xff0c;\u6211\u56de\u8c03<br \/>\n    void SetReadCallBack(const EventCallBack&amp; cb) { _read_callback &#061; cb; }<br \/>\n    void SetWriteCallBack(const EventCallBack&amp; cb) { _write_callback &#061; cb; }<br \/>\n    void SetErrorCallBack(const EventCallBack&amp; cb) { _error_callback &#061; cb; }<br \/>\n    void SetCloseCallBack(const EventCallBack&amp; cb) { _close_callback &#061; cb; }<br \/>\n    void SetAnyEventCallBack(const EventCallBack&amp; cb) { _anyevent_callback &#061; cb;}<br \/>\n    \/\/ \u662f\u5426\u6b63\u5728\u76d1\u63a7\u63cf\u8ff0\u7b26\u7684\u8bfb\u4e8b\u4ef6<br \/>\n    bool Readable() { return _events &amp; EPOLLIN; }<br \/>\n    \/\/ \u662f\u5426\u6b63\u5728\u76d1\u63a7\u63cf\u8ff0\u7b26\u7684\u5199\u4e8b\u4ef6<br \/>\n    bool Writable() { return _events &amp; EPOLLOUT; }<br \/>\n    \/\/ \u542f\u52a8\u8bfb\u4e8b\u4ef6\u63cf\u8ff0\u7b26\u7684\u76d1\u63a7 &#8211;&gt;\u6302\u5230EventLoop\u4e0a<br \/>\n    void EnableRead() {<br \/>\n        _events |&#061; EPOLLIN; \/\/ \u540e\u8fb9\u4f1a\u6dfb\u52a0\u5230EventLoop\u7684\u4e8b\u4ef6\u76d1\u63a7\u4e2d<br \/>\n    }<\/p>\n<p>    \/\/ \u542f\u52a8\u5199\u4e8b\u4ef6\u63cf\u8ff0\u7b26\u7684\u76d1\u63a7<br \/>\n    void EnableWrite() {<br \/>\n        _events |&#061; EPOLLOUT; \/\/ \u540e\u8fb9\u4f1a\u6dfb\u52a0\u5230EventLoop\u7684\u4e8b\u4ef6\u76d1\u63a7\u4e2d<br \/>\n    }<br \/>\n    \/\/ \u89e3\u9664\u53ef\u8bfb\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void DisableRead() {<br \/>\n        _events &amp;&#061; ~EPOLLIN; \/\/ \u540e\u8fb9\u4f1a\u4fee\u6539\u5230EventLoop\u7684\u4e8b\u4ef6\u76d1\u63a7\u4e2d<br \/>\n    }<br \/>\n    \/\/ \u89e3\u9664\u53ef\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void DisableWrite() {<br \/>\n        _events &amp;&#061; ~EPOLLOUT; \/\/ \u540e\u8fb9\u4f1a\u4fee\u6539\u5230EventLoop\u7684\u4e8b\u4ef6\u76d1\u63a7\u4e2d<br \/>\n    }<br \/>\n    \/\/ \u89e3\u9664\u6240\u6709\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void DisableAll() { _events &#061; 0; }<br \/>\n    \/\/ \u79fb\u9664\u76d1\u63a7&#xff1a;\u4eceepoll\u7ea2\u9ed1\u6811\u4e0a\u76f4\u63a5\u79fb\u9664&#xff0c;\u4eceEventLoop\u4e0a\u79fb\u9664<br \/>\n    void Remove() {<br \/>\n        \/\/ \u540e\u8fb9\u4f1a\u8c03\u7528EventLoop\u63a5\u53e3\u6765\u79fb\u9664\u76d1\u63a7<br \/>\n    }<br \/>\n    \/\/ \u4e8b\u4ef6\u5904\u7406&#xff1a;\u4e00\u65e6\u8fde\u63a5\u89e6\u53d1\u4e86\u4e8b\u4ef6&#xff0c;\u5c31\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570&#xff0c;EventLoop\u6a21\u5757\u53ea\u7ba1\u76d1\u63a7\u89e6\u53d1\u4f55\u79cd\u4e8b\u4ef6,\u8c03\u7528Channel\u5904\u7406\u51fd\u6570<br \/>\n    \/\/ \u5982\u4f55\u5904\u7406\u7531Channel\u51b3\u5b9a<br \/>\n    void HandleEvent() {<br \/>\n        if ((_revents &amp; EPOLLIN) ||<br \/>\n            (_revents &amp; EPOLLRDHUP || _revents &amp; (EPOLLPRI))) {<br \/>\n            if (_read_callback)<br \/>\n                _read_callback();<br \/>\n            \/\/ \u65e0\u8bba\u4ec0\u4e48\u4e8b\u4ef6&#xff0c;\u90fd\u8981\u8c03\u7528\u7684\u56de\u8c03\u51fd\u6570<br \/>\n            if (_anyevent_callback)<br \/>\n                _anyevent_callback();<br \/>\n        }<br \/>\n        \/*\u6709\u53ef\u80fd\u4f1a\u51fa\u73b0\u91ca\u653e\u8fde\u63a5\u7684\u64cd\u4f5c&#xff0c;\u4e00\u6b21\u53ea\u5904\u7406\u4e00\u4e2a*\/<br \/>\n        if ((_revents &amp; EPOLLOUT)) {<br \/>\n            if (_write_callback)<br \/>\n                _write_callback();<br \/>\n            if (_anyevent_callback)<br \/>\n                _anyevent_callback(); \/\/ \u653e\u5230\u4e8b\u4ef6\u5904\u7406\u5b8c\u6bd5\u4e4b\u540e\u8c03\u7528&#xff0c;\u7528\u4e8e\u5237\u65b0\u6d3b\u8dc3\u5ea6<br \/>\n        } else if ((_revents &amp; EPOLLERR)) {<br \/>\n            if (_anyevent_callback)<br \/>\n                _anyevent_callback(); \/\/ \u51fa\u9519\u4f1a\u91ca\u653e\u8fde\u63a5&#xff0c;\u653e\u5230\u4e8b\u4ef6\u5904\u7406\u4e4b\u524d\u8c03\u7528\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03<br \/>\n            if (_error_callback)<br \/>\n                _error_callback();<br \/>\n        } else if ((_revents &amp; EPOLLHUP)) {<br \/>\n            if (_anyevent_callback)<br \/>\n                _anyevent_callback();<br \/>\n            if (_close_callback)<br \/>\n                _close_callback();<br \/>\n        }<br \/>\n    }<br \/>\n}; <\/p>\n<p><span style=\"color:#0d0016\">\u5728\u4e0b\u4e2a\u6a21\u5757\u8fdb\u884c\u8054\u5408poller\u6d4b\u8bd5<\/span><\/p>\n<hr \/>\n<h2><span style=\"color:#0d0016\">\u4e94\u3001Poller\u6a21\u5757<\/span><\/h2>\n<h3><span style=\"color:#0d0016\">5.1 Poller\u63cf\u8ff0\u7b26\u76d1\u63a7\u7c7b\u529f\u80fd\u8bbe\u8ba1<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u529f\u80fd&#xff1a;\u5bf9epoll\u8fdb\u884c\u5c01\u88c5&#xff0c;\u8ba9\u5bf9\u63cf\u8ff0\u7b26\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u7684\u64cd\u4f5c\u66f4\u52a0\u7b80\u5355<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u610f\u4e49&#xff1a;\u901a\u8fc7epoll\u5b9e\u73b0\u5bf9\u63cf\u8ff0\u7b26\u7684IO\u4e8b\u4ef6\u76d1\u63a7<\/span><\/p>\n<li><span style=\"color:#0d0016\">\u6dfb\u52a0\/\u4fee\u6539 \u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7(\u4e0d\u5b58\u5728\u5219\u6dfb\u52a0&#xff0c;\u5b58\u5728\u5219\u4fee\u6539)<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u79fb\u9664\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7<\/span><\/li>\n<h4><span style=\"color:#0d0016\">\u5c01\u88c5\u601d\u60f3&#xff1a;<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u5fc5\u987b\u62e5\u6709\u4e00\u4e2aepoll\u7684\u64cd\u4f5c\u53e5\u67c4\u3001<\/span><\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u00a0epoll \u5b9e\u4f8b&#xff0c;\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;_epfd&#xff09;&#xff0c;\u540e\u7eed\u6240\u6709\u64cd\u4f5c\u5747\u57fa\u4e8e\u6b64\u63cf\u8ff0\u7b26\u3002\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u62e5\u6709\u4e00\u4e2astruct epoll_event \u7ed3\u6784\u6570\u7ec4&#xff0c;\u76d1\u63a7\u65f6\u4fdd\u5b58\u6240\u6709\u7684\u6d3b\u8dc3\u4e8b\u4ef6<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u4f7f\u7528hash\u8868\u7ba1\u7406\u63cf\u8ff0\u7b26fd\u4e0e\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u4e8b\u4ef6\u7ba1\u7406Channel\u5bf9\u8c61&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u903b\u8f91\u6d41\u7a0b&#xff1a;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">\u5f53\u63cf\u8ff0\u7b26\u5c31\u7eea\u4e86&#xff0c;\u901a\u8fc7\u63cf\u8ff0\u7b26\u5728hash\u8868\u4e2d\u627e\u5230\u5bf9\u5e94\u7684Channel&#xff08;\u5f97\u5230\u4e86Channel\u624d\u80fd\u77e5\u9053\u4ec0\u4e48\u4e8b\u4ef6\u5982\u4f55\u5904\u7406&#xff09;\u5f53\u63cf\u8ff0\u7b26\u5c31\u7eea\u4e86&#xff0c;\u8fd4\u56de\u5c31\u7eea\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684Channel<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u5bf9\u63cf\u8ff0\u7b26\u8fdb\u884c\u76d1\u63a7&#xff0c;\u901a\u8fc7Channel\u624d\u80fd\u77e5\u9053\u63cf\u8ff0\u7b26\u9700\u8981\u76d1\u63a7\u4ec0\u4e48\u4e8b\u4ef6<\/span><\/p>\n<\/li>\n<\/ul>\n<h3><span style=\"color:#0d0016\">5.2 Poller\u63cf\u8ff0\u7b26\u76d1\u63a7\u7c7b\u63a5\u53e3\u8bbe\u8ba1<\/span><\/h3>\n<p>#define MAX_EPOLLEVENTS 1024<br \/>\nclass Poller {<br \/>\nprivate:<br \/>\n    int _epfd;<br \/>\n    struct epoll_event _evs[MAX_EPOLLEVENTS];<br \/>\n    std::unordered_map&lt;int, Channel*&gt; _channels;<\/p>\n<p>private:<br \/>\n    \/\/ 1.\u5224\u65ad\u8981\u66f4\u65b0\u4e8b\u4ef6\u7684\u63cf\u8ff0\u7b26\u662f\u5426\u5b58\u5728,\u88ab\u76d1\u63a7<br \/>\n    bool HasChannel(Channel* channel) {}<br \/>\n    \/\/ 2.\u9488\u5bf9epoll\u76f4\u63a5\u64cd\u4f5c(\u6dfb\u52a0\u3001\u4fee\u6539\u3001\u79fb\u9664)<br \/>\n    void Update(Channel* channel, int op) {}<\/p>\n<p>public:<br \/>\n    Poller();<br \/>\n    \/\/ 1.\u6dfb\u52a0\u6216\u66f4\u65b0\u63cf\u8ff0\u7b26\u6240\u76d1\u63a7\u7684\u4e8b\u4ef6<br \/>\n    void UpdateEvent(Channel* channel);<br \/>\n    \/\/ 2.\u79fb\u9664\u63cf\u8ff0\u7b26\u7684\u76d1\u63a7<br \/>\n    void RemoveEvent(Channel* channel);<br \/>\n    \/\/ 3.\u5f00\u59cb\u76d1\u63a7&#xff0c;\u83b7\u53d6\u5c31\u7eeaChannel&#xff0c;\u8fd4\u56de\u6d3b\u8dc3\u8fde\u63a5<br \/>\n    void Poll(std::vector&lt;Channel&gt; active);<br \/>\n}; <\/p>\n<h3><span style=\"color:#0d0016\">5.3 Poller\u63cf\u8ff0\u7b26\u76d1\u63a7\u7c7b\u63a5\u53e3\u5b9e\u73b0\u4e0e\u7f16\u8bd1<\/span><\/h3>\n<p>#include &lt;unordered_map&gt;<br \/>\n#define MAX_EPOLLEVENTS 1024<br \/>\nclass Poller {<br \/>\nprivate:<br \/>\n    int _epfd;<br \/>\n    struct epoll_event _evs[MAX_EPOLLEVENTS];<br \/>\n    std::unordered_map&lt;int, Channel*&gt; _channels;<\/p>\n<p>private:<br \/>\n    \/\/ 1.\u5224\u65ad\u8981\u66f4\u65b0\u4e8b\u4ef6\u7684\u63cf\u8ff0\u7b26\u662f\u5426\u5b58\u5728<br \/>\n    bool HasChannel(Channel* channel) {<br \/>\n        auto it &#061; _channels.find(channel-&gt;Fd());<br \/>\n        if (it &#061;&#061; _channels.end())<br \/>\n            return false;<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ 2.\u9488\u5bf9epoll\u76f4\u63a5\u64cd\u4f5c(\u6dfb\u52a0\u3001\u4fee\u6539\u3001\u79fb\u9664)<br \/>\n    void Update(Channel* channel, int op) {<br \/>\n        \/\/ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);<br \/>\n        int fd &#061; channel-&gt;Fd();<br \/>\n        struct epoll_event ev;<br \/>\n        ev.data.fd &#061; fd;<br \/>\n        ev.events &#061; channel-&gt;Events();<br \/>\n        int ret &#061; epoll_ctl(_epfd, op, fd, &amp;ev);<br \/>\n        if (ret &lt; 0) {<br \/>\n            ERR_LOG(&#034;EPOLLCTL FAILED&#8230;&#034;);<br \/>\n        }<br \/>\n        return;<br \/>\n    }<\/p>\n<p>public:<br \/>\n    Poller() {<br \/>\n        _epfd &#061; epoll_create(MAX_EPOLLEVENTS);<br \/>\n        if (_epfd &lt; 0) {<br \/>\n            ERR_LOG(&#034;EPOLL CREATE FAILED&#8230;&#034;);<br \/>\n            abort(); \/\/ \u9000\u51fa\u7a0b\u5e8f<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ 1.\u6dfb\u52a0\u6216\u66f4\u65b0\u63cf\u8ff0\u7b26\u6240\u76d1\u63a7\u7684\u4e8b\u4ef6<br \/>\n    void UpdateEvent(Channel* channel) {<br \/>\n        bool ret &#061; HasChannel(channel);<br \/>\n        if (ret &#061;&#061; false) {<br \/>\n            \/\/\u4e0d\u5b58\u5728\u5219\u6dfb\u52a0<br \/>\n            _channels.insert(std::make_pair(channel-&gt;Fd(), channel));<br \/>\n            return Update(channel, EPOLL_CTL_ADD);<br \/>\n        }<br \/>\n        return Update(channel, EPOLL_CTL_MOD);<br \/>\n    }<br \/>\n    \/\/ 2.\u79fb\u9664\u63cf\u8ff0\u7b26\u7684\u76d1\u63a7<br \/>\n    void RemoveEvent(Channel* channel) {<br \/>\n        auto it &#061; _channels.find(channel-&gt;Fd());<br \/>\n        if (it !&#061; _channels.end())<br \/>\n            _channels.erase(it);<br \/>\n        Update(channel, EPOLL_CTL_DEL);<br \/>\n    }<br \/>\n    \/\/ 3.\u5f00\u59cb\u76d1\u63a7&#xff0c;\u83b7\u53d6\u5c31\u7eeaChannel&#xff0c;\u8fd4\u56de\u6d3b\u8dc3\u8fde\u63a5<br \/>\n    void Poll(std::vector&lt;Channel*&gt;* active) {<br \/>\n        \/\/    int epoll_wait(int epfd, struct epoll_event *events,<br \/>\n        \/\/                   int maxevents, int timeout);<br \/>\n        \/\/ \u963b\u585e\u76d1\u63a7&#xff0c;\u65e0\u5e8f\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4, time_out &#061; -1&#xff1a;\u963b\u585e\u76d1\u63a7<br \/>\n        int nfds &#061; epoll_wait(_epfd, _evs, MAX_EPOLLEVENTS, -1);<br \/>\n        if (nfds &lt; 0) {<br \/>\n            if (errno &#061;&#061; EINTR) {<br \/>\n                return;<br \/>\n            }<br \/>\n            ERR_LOG(&#034;EPOLL WAIT ERROR: %s&#8230;\\\\n&#034;, strerror(errno));<br \/>\n            abort(); \/\/ \u9000\u51fa\u7a0b\u5e8f<br \/>\n        }<br \/>\n        for (int i &#061; 0; i &lt; nfds; i&#043;&#043;) {<br \/>\n            auto it &#061; _channels.find(_evs[i].data.fd);<br \/>\n            assert(it !&#061; _channels.end());<br \/>\n            it-&gt;second-&gt;SetREvent(_evs[i].events); \/\/ \u8bbe\u8ba1\u5b9e\u9645\u5c31\u7eea\u4e8b\u4ef6<br \/>\n            active-&gt;push_back(it-&gt;second);<br \/>\n        }<br \/>\n    }<br \/>\n}; <\/p>\n<h3><span style=\"color:#0d0016\">5.4\u00a0Poller\u6a21\u5757\u4e0eChannel\u6a21\u5757\u6574\u5408<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u5c06Poller\u6a21\u5757\u6574\u5408\u81f3Channel&#xff0c;\u4fee\u6539Channel<\/span><\/p>\n<p>#include &lt;functional&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\nclass Poller;<br \/>\nclass Channel {<br \/>\nprivate:<br \/>\n    int _fd;<br \/>\n    Poller* _poller;<br \/>\n    uint32_t _events;  \/\/ \u5f53\u524d\u9700\u8981\u76d1\u63a7\u7684\u4e8b\u4ef6<br \/>\n    uint32_t _revents; \/\/ \u5f53\u524d\u8fde\u63a5\u89e6\u53d1\u7684\u4e8b\u4ef6<br \/>\n    using EventCallBack &#061; std::function&lt;void()&gt;;<br \/>\n    EventCallBack _read_callback;     \/\/ \u53ef\u8bfb\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    EventCallBack _write_callback;    \/\/ \u53ef\u5199\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    EventCallBack _error_callback;    \/\/ \u9519\u8bef\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    EventCallBack _close_callback;    \/\/ \u8fde\u63a5\u65ad\u5f00\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    EventCallBack _anyevent_callback; \/\/ \u4efb\u610f\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\npublic:<br \/>\n    Channel(Poller* poller, int fd)<br \/>\n        : _fd(fd), _events(0), _revents(0), _poller(poller) {}<br \/>\n    int Fd() { return _fd; }<br \/>\n    \/\/ \u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u4e4b\u540e&#xff0c;fd\u5c31\u7eea\u4e86\u7684\u4e8b\u4ef6&#xff0c;<br \/>\n    \/\/ EventLoop\u5c31\u4f1a\u901a\u8fc7\u8fd9\u4e2a\u63a5\u53e3\u628a\u5b9e\u9645\u5c31\u7eea\u7684\u4e8b\u4ef6\u8bbe\u7f6e\u8fdb\u6765&#xff0c;\u6839\u636erevents\u5224\u65ad\u4ec0\u4e48\u4e8b\u4ef6\u5c31\u7eea<br \/>\n    uint32_t Events() { return _events; } \/\/ \u83b7\u53d6\u60f3\u8981\u76d1\u63a7\u7684\u4e8b\u4ef6<\/p>\n<p>    void SetREvent(uint32_t events) { _revents &#061; events; }<br \/>\n    \/\/ \u8bbe\u7f6e\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u4f60\u4f20\u51fd\u6570&#xff0c;\u6211\u56de\u8c03<br \/>\n    void SetReadCallBack(const EventCallBack&amp; cb) { _read_callback &#061; cb; }<br \/>\n    void SetWriteCallBack(const EventCallBack&amp; cb) { _write_callback &#061; cb; }<br \/>\n    void SetErrorCallBack(const EventCallBack&amp; cb) { _error_callback &#061; cb; }<br \/>\n    void SetCloseCallBack(const EventCallBack&amp; cb) { _close_callback &#061; cb; }<br \/>\n    void SetAnyEventCallBack(const EventCallBack&amp; cb) {<br \/>\n        _anyevent_callback &#061; cb;<br \/>\n    }<br \/>\n    \/\/ \u5f53\u524d\u662f\u5426\u76d1\u63a7\u53ef\u8bfb<br \/>\n    bool Readable() { return _events &amp; EPOLLIN; }<br \/>\n    \/\/ \u5f53\u524d\u662f\u5426\u76d1\u63a7\u53ef\u5199<br \/>\n    bool Writable() { return _events &amp; EPOLLOUT; }<br \/>\n    \/\/ \u542f\u52a8\u8bfb\u4e8b\u4ef6\u76d1\u63a7 &#8211;&gt;\u6302\u5230EventLoop\u4e0a<br \/>\n    void EnableRead() {<br \/>\n        _events |&#061; EPOLLIN;<br \/>\n        Update();<br \/>\n    }<\/p>\n<p>    \/\/ \u542f\u52a8\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void EnableWrite() {<br \/>\n        _events |&#061; EPOLLOUT;<br \/>\n        Update();<br \/>\n    }<br \/>\n    \/\/ \u89e3\u9664\u53ef\u8bfb\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void DisableRead() {<br \/>\n        _events &amp;&#061; ~EPOLLIN;<br \/>\n        Update();<br \/>\n    }<br \/>\n    \/\/ \u89e3\u9664\u53ef\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void DisableWrite() {<br \/>\n        _events &amp;&#061; ~EPOLLOUT;<br \/>\n        Update();<br \/>\n    }<br \/>\n    \/\/ \u89e3\u9664\u6240\u6709\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void DisableAll() {<br \/>\n        _events &#061; 0;<br \/>\n        Update();<br \/>\n    }<br \/>\n    \/\/ \u8fd9\u91cc\u505a\u58f0\u660e&#xff0c;\u5728Poller\u7c7b\u4e4b\u540e\u5b9e\u73b0<br \/>\n    \/\/ \u79fb\u9664\u76d1\u63a7&#xff1a;\u4eceepoll\u7ea2\u9ed1\u6811\u4e0a\u76f4\u63a5\u79fb\u9664&#xff0c;\u4eceEventLoop\u4e0a\u79fb\u9664<br \/>\n    void Remove();<br \/>\n    \/\/ \u66f4\u65b0\u76d1\u63a7<br \/>\n    void Update();<\/p>\n<p>    \/\/ \u4e8b\u4ef6\u5904\u7406&#xff1a;\u4e00\u65e6\u8fde\u63a5\u89e6\u53d1\u4e86\u4e8b\u4ef6&#xff0c;\u5c31\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570&#xff0c;EventLoop\u6a21\u5757\u53ea\u7ba1\u76d1\u63a7\u89e6\u53d1\u4f55\u79cd\u4e8b\u4ef6,\u8c03\u7528Channel\u5904\u7406\u51fd\u6570<br \/>\n    \/\/ \u5982\u4f55\u5904\u7406\u7531Channel\u51b3\u5b9a<br \/>\n    void HandleEvent() {<br \/>\n        if ((_revents &amp; EPOLLIN) ||<br \/>\n            (_revents &amp; EPOLLRDHUP || _revents &amp; (EPOLLPRI))) {<br \/>\n            if (_read_callback)<br \/>\n                _read_callback();<br \/>\n            \/\/ \u65e0\u8bba\u4ec0\u4e48\u4e8b\u4ef6&#xff0c;\u90fd\u8981\u8c03\u7528\u7684\u56de\u8c03\u51fd\u6570<br \/>\n            if (_anyevent_callback)<br \/>\n                _anyevent_callback();<br \/>\n        }<br \/>\n        \/*\u6709\u53ef\u80fd\u4f1a\u51fa\u73b0\u91ca\u653e\u8fde\u63a5\u7684\u64cd\u4f5c&#xff0c;\u4e00\u6b21\u53ea\u5904\u7406\u4e00\u4e2a*\/<br \/>\n        if ((_revents &amp; EPOLLOUT)) {<br \/>\n            if (_write_callback)<br \/>\n                _write_callback();<br \/>\n            if (_anyevent_callback)<br \/>\n                _anyevent_callback(); \/\/ \u653e\u5230\u4e8b\u4ef6\u5904\u7406\u5b8c\u6bd5\u4e4b\u540e\u8c03\u7528&#xff0c;\u7528\u4e8e\u5237\u65b0\u6d3b\u8dc3\u5ea6<br \/>\n        } else if ((_revents &amp; EPOLLERR)) {<br \/>\n            if (_anyevent_callback)<br \/>\n                _anyevent_callback(); \/\/ \u51fa\u9519\u4f1a\u91ca\u653e\u8fde\u63a5&#xff0c;\u653e\u5230\u4e8b\u4ef6\u5904\u7406\u4e4b\u524d\u8c03\u7528\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03<br \/>\n            if (_error_callback)<br \/>\n                _error_callback();<br \/>\n        } else if ((_revents &amp; EPOLLHUP)) {<br \/>\n            if (_anyevent_callback)<br \/>\n                _anyevent_callback();<br \/>\n            if (_close_callback)<br \/>\n                _close_callback();<br \/>\n        }<br \/>\n    }<br \/>\n};<\/p>\n<p>\/\/\u653e\u7f6ePoller\u7c7b\u4e4b\u540e\u7684Update()\u548cRemove()<br \/>\nvoid Channel::Remove() { return _poller-&gt;RemoveEvent(this); }<br \/>\nvoid Channel::Update() { return _poller-&gt;UpdateEvent(this); } <\/p>\n<h3><span style=\"color:#0d0016\">5.5\u00a0Poller\u6a21\u5757\u4e0eChannel\u6a21\u5757\u8054\u5408\u8c03\u8bd5&#8211;\u662f\u4e0d\u5b8c\u6574\u7684<\/span><\/h3>\n<p><span style=\"color:#ed7976\">\u6ce8\u610f&#xff1a;Poller\u6a21\u5757\u662fEventLoop\u7684\u4e00\u4e2a\u5b50\u6a21\u5757&#xff0c;EventLoop\u624d\u662f\u5b9e\u9645\u4e0a\u8fdb\u884c\u4e8b\u4ef6\u7ba1\u7406\u7684\u6a21\u5757<\/span><\/p>\n<p><span style=\"color:#0d0016\">tcp_svr.cc<\/span><\/p>\n<p> #include &#034;..\/source\/server.hpp&#034;<\/p>\n<p>void HandleClose(Channel* channel) {<br \/>\n    std::cout &lt;&lt; &#034;close: &#034; &lt;&lt; channel-&gt;Fd() &lt;&lt; std::endl;<br \/>\n    channel-&gt;Remove(); \/\/ \u79fb\u9664\u76d1\u63a7<br \/>\n    delete channel;<br \/>\n}<\/p>\n<p>void HandleRead(Channel* channel) {<br \/>\n    int fd &#061; channel-&gt;Fd();<br \/>\n    char buf[1024] &#061; {0};<br \/>\n    int ret &#061; recv(fd, buf, 1023, 0);<br \/>\n    if (ret &lt;&#061; 0) {<br \/>\n        return HandleClose(channel); \/\/ \u5173\u95ed\u91ca\u653e<br \/>\n    }<br \/>\n    std::cout &lt;&lt; buf &lt;&lt; std::endl;<br \/>\n    channel-&gt;EnableWrite(); \/\/ \u542f\u52a8\u53ef\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n}<br \/>\nvoid HandleWrite(Channel* channel) {<br \/>\n    int fd &#061; channel-&gt;Fd();<br \/>\n    const char* data &#061; &#034;\u5929\u6c14\u8fd8\u4e0d\u9519&#xff01;&#034;;<br \/>\n    int ret &#061; send(fd, data, strlen(data), 0);<br \/>\n    if (ret &lt; 0) {<br \/>\n        return HandleClose(channel);<br \/>\n    }<br \/>\n    channel-&gt;DisableWrite(); \/\/ \u5173\u95ed\u5199\u76d1\u63a7<br \/>\n}<br \/>\nvoid HandleError(Channel* channel) { return HandleClose(channel); }<br \/>\nvoid HandleEvent(Channel* channel) { std::cout &lt;&lt; &#034;\u6709\u4e86\u4e00\u4e2a\u4e8b\u4ef6&#034; &lt;&lt; std::endl; }<\/p>\n<p>void Acceptor(Poller* poller, Channel* lst_channel) {<br \/>\n    int fd &#061; lst_channel-&gt;Fd();<br \/>\n    int newfd &#061; accept(fd, nullptr, nullptr);<br \/>\n    if (newfd &lt; 0) {<br \/>\n        return;<br \/>\n    }<br \/>\n    Channel* channel &#061; new Channel(poller, newfd);<br \/>\n    channel-&gt;SetReadCallBack(<br \/>\n        std::bind(HandleRead, channel)); \/\/ \u4e3a\u901a\u4fe1\u5957\u63a5\u5b57\u8bbe\u7f6e\u53ef\u8bfb\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    channel-&gt;SetWriteCallBack(<br \/>\n        std::bind(HandleWrite, channel)); \/\/ \u53ef\u5199\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    channel-&gt;SetCloseCallBack(<br \/>\n        std::bind(HandleClose, channel)); \/\/ \u5173\u95ed\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    channel-&gt;SetAnyEventCallBack(<br \/>\n        std::bind(HandleEvent, channel)); \/\/ \u4efb\u4f55\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    channel-&gt;EnableRead();                \/\/\u542f\u52a8\u8bfb\u4e8b\u4ef6\u76d1\u63a7<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    Poller poller;<br \/>\n    Socket lst_sock;<br \/>\n    lst_sock.CreateServer(8500);<br \/>\n    \/\/ \u4e3a\u76d1\u542c\u5957\u63a5\u5b57&#xff0c;\u521b\u5efa\u4e00\u4e2aChannel&#xff0c;\u8fdb\u884c\u4e8b\u4ef6\u7684\u7ba1\u7406&#xff0c;\u4ee5\u53ca\u4e8b\u4ef6\u7684\u5904\u7406<br \/>\n    Channel channel(&amp;poller, lst_sock.Fd());<br \/>\n    channel.SetReadCallBack(std::bind(<br \/>\n        Acceptor, &amp;poller,<br \/>\n        &amp;channel)); \/\/ \u56de\u8c03\u4e2d&#xff0c;\u83b7\u53d6\u65b0\u8fde\u63a5&#xff0c;\u4e3a\u65b0\u8fde\u63a5\u521b\u5efaChannel\u5e76\u6dfb\u52a0\u76d1\u63a7<br \/>\n    channel.EnableRead();<br \/>\n    while (1) {<br \/>\n        std::vector&lt;Channel*&gt; actives;<br \/>\n        poller.Poll(&amp;actives);<br \/>\n        for (auto&amp; a : actives) {<br \/>\n            a-&gt;HandleEvent();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    lst_sock.Close();<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<p><span style=\"color:#0d0016\">tcp_cli.cc<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"289\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233607-68534d67161ac.png\" width=\"430\" \/><\/p>\n<\/p>\n<p><span style=\"color:#0d0016\">\u8fd0\u884c\u7ed3\u679c&#xff1a;\u4e00\u6b21\u662f\u4e24\u4e2a\u4e8b\u4ef6&#xff08;\u8bfb\u4e8b\u4ef6\u3001\u5199\u4e8b\u4ef6&#xff09;&#xff0c;\u5ba2\u6237\u7aef\u9000\u51fa&#xff0c;\u51fa\u73b0\u6bb5\u9519\u8bef<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"310\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233607-68534d67444f8.png\" width=\"1666\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u4f7f\u7528gdb\u8fdb\u884c\u8c03\u8bd5&#xff1a;<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"182\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233607-68534d6780689.png\" width=\"327\" \/><\/p>\n<p><span style=\"color:#0d0016\">gdb .\/server &#8212;&#8211;&gt; run &#8212;&#8211;&gt; .\/client &#8212;&#8211;&gt; \u5173\u95ed\u5ba2\u6237\u7aef &#8212;&#8212;-&gt; bt&#xff08;\u67e5\u770b\u51fd\u6570\u8c03\u7528\u6808&#xff09;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u6211\u4eec\u53d1\u73b0\u5230\u95ee\u9898&#xff1a;\u89e6\u53d1\u53ef\u8bfb\u548c\u53ef\u5199\u4e8b\u4ef6\u4e4b\u540e\u90fd\u6709\u53ef\u80fd\u91ca\u653e\u8fde\u63a5&#xff0c;\u56e0\u6b64\u9700\u8981\u5148\u89e6\u53d1\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"667\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233607-68534d67931eb.png\" width=\"1347\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u73b0\u5728\u6211\u4eec\u6839\u636e\u51e0\u5f20\u56fe\u548c\u89e3\u91ca\u6765\u7406\u89e3\u8fd9\u4e2a\u4ee3\u7801\u6240\u63cf\u8ff0\u7684<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u8fc7\u7a0b&#xff1a;<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"888\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233607-68534d67de7fd.png\" width=\"649\" \/><\/p>\n<\/p>\n<li>\n<p><span style=\"color:#0d0016\">\u76d1\u542cChannel&#xff1a;<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">lst_channel&#xff1a;\u76d1\u542c\u5957\u63a5\u5b57\u7684Channel<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">SetReadCallBack(Acceptor)&#xff1a;\u8bbe\u7f6e\u8bfb\u4e8b\u4ef6\u56de\u8c03\u4e3aAcceptor\u51fd\u6570<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">EnableRead&#xff1a;\u542f\u7528\u8bfb\u4e8b\u4ef6\u76d1\u63a7<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u6dfb\u52a0\u5230Poller\u7684&lt;fd, Channel&gt;\u6620\u5c04\u4e2d<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u6700\u7ec8\u7531epoll\u76d1\u63a7\u00a0<\/span><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">Acceptor\u56de\u8c03&#xff1a;<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">\u5f53\u6709\u65b0\u8fde\u63a5\u65f6\u89e6\u53d1<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u901a\u8fc7accept()\u83b7\u53d6\u65b0\u8fde\u63a5<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u521b\u5efa\u65b0\u7684\u8fde\u63a5Channel&#xff08;con_channel&#xff09;<\/span><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u8fde\u63a5Channel&#xff1a;<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">\u4e3a\u6bcf\u4e2a\u65b0\u8fde\u63a5\u521b\u5efa\u5355\u72ec\u7684Channel<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u8bbe\u7f6e\u4e94\u79cd\u56de\u8c03\u51fd\u6570&#xff1a;<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">setreadcallback\u00a0\u2192\u00a0HandleRead&#xff08;\u8bfb\u53d6\u6570\u636e&#xff09;<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">setwritecallback\u00a0\u2192\u00a0HandleWrite&#xff08;\u5199\u6570\u636e&#xff09;<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">setclosecallback\u00a0\u2192\u00a0HandleClose&#xff08;\u5173\u95ed\u8fde\u63a5&#xff09;<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">seterrorcallback\u00a0\u2192\u00a0HandleError&#xff08;\u9519\u8bef\u5904\u7406&#xff09;<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">seteventcallback\u00a0\u2192\u00a0HandleEvent&#xff08;\u4efb\u610f\u4e8b\u4ef6&#xff09;<\/span><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u6570\u636e\u6d41\u5904\u7406&#xff08;\u5e95\u90e8&#xff09;&#xff1a;<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"277\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233614-68534d6e31c46.png\" width=\"375\" \/><\/p>\n<\/li>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">HandleRead\u6d41\u7a0b&#xff1a;<\/span><\/p>\n<li>\n<p><span style=\"color:#0d0016\">\u5f53\u5ba2\u6237\u7aef\u53d1\u9001\u6570\u636e\u65f6\u89e6\u53d1<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u8bfb\u53d6\u6570\u636e\u5e76\u6253\u5370&#xff1a;hi pupu<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u8c03\u7528EnableWrite()\u542f\u52a8\u5199\u76d1\u63a7<\/span><\/p>\n<\/li>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">HandleWrite\u6d41\u7a0b&#xff1a;<\/span><\/p>\n<li>\n<p><span style=\"color:#0d0016\">epoll\u68c0\u6d4b\u5230\u53ef\u5199\u4e8b\u4ef6\u65f6\u89e6\u53d1<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u8c03\u7528send()\u53d1\u9001\u56de\u590d\u6570\u636e&#xff1a;\u201c\u5929\u6c14\u8fd8\u4e0d\u9519&#xff01;\u201d<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u8c03\u7528DisableWrite()\u5173\u95ed\u5199\u76d1\u63a7<\/span><\/p>\n<\/li>\n<\/li>\n<\/ul>\n<h4><span style=\"color:#0d0016\">\u6838\u5fc3\u7ec4\u4ef6\u4ea4\u4e92\u4e0e\u5de5\u4f5c\u6d41\u7a0b<\/span><\/h4>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"561\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233615-68534d6f2d5bb.png\" width=\"227\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u6838\u5fc3\u5de5\u4f5c\u6d41\u7a0b<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u521d\u59cb\u5316&#xff1a;\u521b\u5efa\u76d1\u542cChannel\u5e76\u8bbe\u7f6eAcceptor\u56de\u8c03<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u4e8b\u4ef6\u76d1\u542c&#xff1a;\u901a\u8fc7epoll\u76d1\u63a7\u6240\u6709Channel<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u8fde\u63a5\u5efa\u7acb&#xff1a;Acceptor\u63a5\u6536\u65b0\u8fde\u63a5\u5e76\u521b\u5efa\u901a\u4fe1Channel<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u6570\u636e\u5904\u7406&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u8bfb\u4e8b\u4ef6 \u2192 \u8bfb\u53d6\u6570\u636e \u2192 \u542f\u7528\u5199\u76d1\u63a7<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5199\u4e8b\u4ef6 \u2192 \u53d1\u9001\u54cd\u5e94 \u2192 \u7981\u7528\u5199\u76d1\u63a7<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u8d44\u6e90\u7ba1\u7406&#xff1a;\u901a\u8fc7\u56de\u8c03\u5904\u7406\u5173\u95ed\u548c\u9519\u8bef<\/span><\/p>\n<hr \/>\n<h2><span style=\"color:#0d0016\">\u516d\u3001EventLoop\u6a21\u5757<\/span><\/h2>\n<p><span style=\"color:#ed7976\">Poller\u6a21\u5757\u662fEventLoop\u7684\u4e00\u4e2a\u5b50\u6a21\u5757&#xff0c;EventLoop\u624d\u662f\u5b9e\u9645\u4e0a\u8fdb\u884c\u4e8b\u4ef6\u7ba1\u7406\u7684\u6a21\u5757<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"458\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233616-68534d70cb6c3.png\" width=\"416\" \/><\/p>\n<h3><span style=\"color:#0d0016\">6.1 EventLoop\u6a21\u5757eventfd\u7684\u8ba4\u8bc6<\/span><\/h3>\n<p><span style=\"color:#0d0016\">eventfd&#xff1a;\u4e00\u79cd\u4e8b\u4ef6\u901a\u77e5\u673a\u5236&#xff0c;\u5934\u6587\u4ef6&#xff1a;&lt;sys\/eventfd.h&gt;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u521b\u5efa\u6587\u4ef6\u63cf\u8ff0\u7b26\u7528\u4e8e\u4e8b\u4ef6\u901a\u77e5<\/span><\/p>\n<p><span style=\"color:#0d0016\">vs\u4fe1\u53f7&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u4fe1\u53f7\u662f\u9488\u5bf9\u8fdb\u7a0b\u8fdb\u884c\u4e8b\u4ef6\u901a\u77e5&#xff0c;\u4e8b\u4ef6\u5177\u4f53\u662f\u88ab\u54ea\u4e00\u4e2a\u7ebf\u7a0b\u6240\u5904\u7406\u4e0d\u4e00\u5b9a\u3002<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">eventfd\u672c\u8d28\u5728\u5185\u6838\u91cc\u8fb9\u7ba1\u7406\u7684\u5c31\u662f\u4e00\u4e2a\u8ba1\u6570\u5668<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u521b\u5efaeventfd\u5c31\u4f1a\u5728\u5185\u6838\u4e2d\u521b\u5efa\u4e00\u4e2a\u8ba1\u6570\u5668&#xff08;\u7ed3\u6784\u4f53&#xff09;<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u6bcf\u5f53\u5411eventfd\u4e2d\u5199\u5165\u4e00\u4e2a\u6570\u503c&#8212;\u7528\u4e8e\u8868\u793a\u4e8b\u4ef6\u7684\u901a\u77e5\u6b21\u6570&#xff0c;\u53ef\u4ee5\u4f7f\u7528read\u8fdb\u884c\u6570\u636e\u7684\u8bfb\u53d6&#xff0c;\u8bfb\u53d6\u5230\u7684\u6570\u636e\u5c31\u662f\u901a\u77e5\u7684\u6b21\u6570&#xff1a;\u5047\u8bbe\u6bcf\u6b21\u7ed9eventfd\u4e2d\u5199\u51651&#xff0c;\u5c31\u8868\u793a\u901a\u77e5\u4e86\u4e00\u6b21&#xff0c;\u8fde\u7eed\u5199\u4e86\u4e09\u6b21\u4e4b\u540e&#xff0c;\u518dread\u51fa\u6765\u7684\u6570\u5b57\u5c31\u662f3&#xff0c;\u8bfb\u53d6\u4e4b\u540e\u8ba1\u6570\u6e050\u3002<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u7528\u5904&#xff1a;\u5728EventLoop\u6a21\u5757\u4e2d\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u4e8b\u4ef6\u901a\u77e5\u673a\u5236<\/span><\/p>\n<h4><span style=\"color:#0d0016\">\u7cfb\u7edf\u8c03\u7528<\/span><\/h4>\n<p>#include &lt;sys\/eventfd.h&gt;<\/p>\n<p>int eventfd(unsigned int initval, int flags); <\/p>\n<p><span style=\"color:#0d0016\">\u53c2\u6570\u8bf4\u660e&#xff1a;<\/span><\/p>\n<ul>\n<li>initval&#xff1a;\u521d\u59cb\u8ba1\u6570\u5668\u503c<\/li>\n<li>flags&#xff1a;\u63a7\u5236\u6807\u5fd7&#xff0c;\u5e38\u7528\u503c\u5305\u62ec&#xff1a;\n<ul>\n<li>EFD_CLOEXEC&#xff1a;\u6267\u884cexec\u65f6\u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u7981\u6b62\u8fdb\u7a0b\u590d\u5236&#xff0c;\u7236\u8fdb\u7a0b\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u540e\u521b\u5efa\u5b50\u8fdb\u7a0b&#xff0c;\u5b50\u8fdb\u7a0b\u4e0d\u4f1a\u590d\u5236\u7236\u8fdb\u7a0b\u6253\u5f00\u7684\u8fd9\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1a;\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u770b&#xff09;<\/li>\n<li>EFD_NONBLOCK&#xff1a;\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f<\/li>\n<li>EFD_SEMAPHORE&#xff1a;\u8bbe\u7f6e\u4e3a\u4fe1\u53f7\u91cf\u6a21\u5f0f<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u8fd4\u56de\u503c&#xff1a;\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7528\u4e8e\u64cd\u4f5c<\/span><\/p>\n<p><span style=\"color:#0d0016\">eventfd\u4e5f\u662f\u901a\u8fc7read \/ write \/ close\u8fdb\u884c\u64cd\u4f5c\u7684<\/span><\/p>\n<p><span style=\"color:#ed7976\">\u6ce8\u610f\u70b9&#xff1a;read &amp; write\u8fdb\u884cIO\u65f6&#xff0c;\u6570\u636e\u53ea\u80fd\u662f\u4e00\u4e2a8\u5b57\u8282\u6570\u636e<\/span><\/p>\n<h4><span style=\"color:#0d0016\">\u4f7f\u7528\u65b9\u6cd5&#xff1a;<\/span><\/h4>\n<p> #include &lt;iostream&gt;<br \/>\n#include &lt;stdint.h&gt;<br \/>\n#include &lt;sys\/eventfd.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\nint main() {<br \/>\n    \/\/ \u521b\u5efaeventfd<br \/>\n    int efd &#061; eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);<br \/>\n    if (efd &lt; 0) {<br \/>\n        perror(&#034;eventfd failed!!&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    uint64_t val &#061; 1;<br \/>\n    \/\/ \u5199\u5165\u4e8b\u4ef6<br \/>\n    write(efd, &amp;val, sizeof(val));<br \/>\n    write(efd, &amp;val, sizeof(val));<br \/>\n    write(efd, &amp;val, sizeof(val));<br \/>\n    \/\/ write(efd, &amp;val, sizeof(val));<br \/>\n    uint64_t res &#061; 0;<br \/>\n    \/\/ \u8bfb\u53d6\u4e8b\u4ef6<br \/>\n    read(efd, &amp;res, sizeof(res));<br \/>\n    printf(&#034;%ld\\\\n&#034;, res);<br \/>\n    close(efd);<br \/>\n    return 0;<br \/>\n} <\/p>\n<p><span style=\"color:#0d0016\">eventfd\u9700\u8981\u914d\u5408\u591a\u8def\u590d\u7528\u548cIO\u64cd\u4f5c\u624d\u80fd\u5145\u5206\u53d1\u6325\u5176\u4f18\u52bf<\/span><\/p>\n<h3>6.2 EventLoop\u6a21\u5757\u8bbe\u8ba1\u601d\u60f3<\/h3>\n<h4><span style=\"color:#0d0016\">\u529f\u80fd<\/span><\/h4>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u7ba1\u7406\u7684\u6a21\u5757&#xff08;\u5c01\u88c5\u4e86TimerQueue\u3001Poller\u6a21\u5757&#xff09;<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u8fd9\u4e2a\u6a21\u5757\u5b9e\u9645\u4e0a\u5c31\u662f\u6211\u4eec\u6240\u8bf4\u7684one thread one loop\u4e2d\u7684loop&#xff0c;\u4e5f\u5c31\u662f\u6211\u4eec\u6240\u8bf4\u7684Reactor<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u8fd9\u4e2a\u6a21\u5757\u4e0e\u7ebf\u7a0b\u662f\u4e00\u4e00\u5bf9\u5e94\u5173\u8054\u7684<\/span><\/p>\n<\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u76d1\u6d4b\u4e00\u4e2a\u8fde\u63a5\u65f6&#xff0c;\u5f53\u8fde\u63a5\u5c31\u7eea\u540e\u9700\u8981\u5904\u7406\u5bf9\u5e94\u4e8b\u4ef6\u3002\u4f46\u82e5\u8be5\u63cf\u8ff0\u7b26\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e2d\u540c\u65f6\u89e6\u53d1\u4e8b\u4ef6&#xff0c;\u5c31\u4f1a\u4ea7\u751f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u56e0\u6b64&#xff0c;\u6211\u4eec\u9700\u8981\u5c06\u8fde\u63a5\u7684\u76d1\u63a7\u3001\u4e8b\u4ef6\u5904\u7406\u53ca\u5176\u4ed6\u76f8\u5173\u64cd\u4f5c\u90fd\u7f6e\u4e8e\u540c\u4e00\u7ebf\u7a0b\u4e2d\u6267\u884c\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u8fd9\u5c31\u76f8\u5f53\u4e8e\u5c06Channel&#xff08;\u5bf9\u4e8b\u4ef6\u76d1\u63a7\u7684\u7ba1\u7406&#xff0c;\u4e8b\u4ef6\u89e6\u53d1\u7684\u5904\u7406&#xff09;\u548cPoller&#xff08;\u5c01\u88c5epoll\u76d1\u63a7\u4e8b\u4ef6&#xff09;\u5173\u8054\u5230\u4e86\u4e00\u8d77\u3002<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"270\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233617-68534d715199c.png\" width=\"787\" \/><\/p>\n<h4><span style=\"color:#0d0016\">\u610f\u4e49<\/span><\/h4>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">\u5bf9\u4e8e\u670d\u52a1\u5668\u4e2d\u7684\u6240\u6709\u4e8b\u4ef6\u90fd\u662f\u7531EventLoop\u6a21\u5757\u6765\u5b8c\u6210<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u6bcf\u4e00\u4e2aConnection\u8fde\u63a5&#xff0c;\u90fd\u4f1a\u7ed1\u5b9a\u4e00\u4e2aEventLoop\u6a21\u5757\u548c\u7ebf\u7a0b&#xff0c;\u56e0\u4e3a\u5916\u754c\u5bf9\u4e8e\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c&#xff0c;\u90fd\u662f\u8981\u653e\u5230\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u8fdb\u884c\u7684<\/span><\/p>\n<\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u601d\u60f3<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5bf9\u4e8e\u6240\u6709\u7684\u8fde\u63a5\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u8fde\u63a5\u89e6\u53d1\u4e8b\u4ef6\u540e\u8c03\u7528\u56de\u8c03\u8fdb\u884c\u5904\u7406\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u5bf9\u4e8e\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c&#xff0c;\u90fd\u8981\u653e\u5230eventloop\u5bf9\u5e94\u7684\u7ebf\u7a0b\u4e2d\u6267\u884c<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u89e3\u51b3\u65b9\u6848&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5728EventLoop\u6a21\u5757\u4e2d&#xff0c;\u6dfb\u52a0\u4e00\u4e2a\u4efb\u52a1\u961f\u5217&#xff0c;\u5bf9\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u8fdb\u884c\u4e00\u6b21\u5c01\u88c5&#xff0c;\u5c06\u5bf9\u8fde\u63a5\u7684\u64cd\u4f5c\u90fd\u4e0d\u76f4\u63a5\u6267\u884c&#xff0c;\u5f53\u505a\u4efb\u52a1\u6dfb\u52a0\u5230\u4efb\u52a1\u961f\u5217\u5f53\u4e2d\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">EventLoop\u5904\u7406\u6d41\u7a0b&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0 \u00a0 \u00a0 1.\u5728\u7ebf\u7a0b\u4e2d\u5bf9\u63cf\u8ff0\u7b26\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0 \u00a0 \u00a0 2.\u6709\u63cf\u8ff0\u7b26\u5c31\u7eea\u5219\u5bf9\u63cf\u8ff0\u7b26\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406&#xff08;\u5982\u4f55\u4fdd\u8bc1\u5904\u7406\u56de\u8c03\u51fd\u6570\u4e2d\u7684\u64cd\u4f5c\u90fd\u5728\u7ebf\u7a0b\u4e2d\u5462&#xff1a;\u5c06\u64cd\u4f5c\u90fd\u5c01\u88c5\u5230\u4efb\u52a1\u961f\u5217&#xff09;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0 \u00a0 \u00a0 3.\u6240\u6709\u7684\u5c31\u7eea\u4e8b\u4ef6\u5904\u7406\u5b8c\u4e86&#xff0c;\u8fd9\u65f6\u5019\u518d\u53bb\u5c06\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u6240\u6709\u4efb\u52a1\u4e00\u4e00\u6267\u884c<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"459\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233617-68534d71755ad.png\" width=\"952\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u6700\u540e\u4ec5\u5728\u4efb\u52a1\u961f\u5217\u5904\u52a0\u4e0a\u9501\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168<\/span><\/p>\n<h3><span style=\"color:#0d0016\">6.3 EventLoop\u6a21\u5757\u7c7b\u63a5\u53e3\u8bbe\u8ba1<\/span><\/h3>\n<p><span style=\"color:#0d0016\">1.\u4e8b\u4ef6\u76d1\u63a7<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0 \u00a0 \u00a0 \u4f7f\u7528Poller\u6a21\u5757<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0 \u00a0 \u00a0 \u6709\u4e8b\u4ef6\u5c31\u7eea\u5219\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406<\/span><\/p>\n<p><span style=\"color:#0d0016\">2.\u6267\u884c\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4efb\u52a1<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0 \u00a0 \u00a0 \u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u4efb\u52a1\u961f\u5217<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5173\u952e\u6ce8\u610f\u4e8b\u9879&#xff1a;\u5f53\u4efb\u52a1\u6267\u884c\u6d41\u7a0b\u56e0\u7b49\u5f85\u6587\u4ef6\u63cf\u8ff0\u7b26IO\u4e8b\u4ef6\u5c31\u7eea\u800c\u963b\u585e\u65f6&#xff0c;\u53ef\u80fd\u5bfc\u81f4\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u65e0\u6cd5\u6267\u884c\u3002\u4e3a\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898&#xff0c;\u9700\u8981\u5f15\u5165eventfd\u4e8b\u4ef6\u901a\u77e5\u673a\u5236&#xff0c;\u53ca\u65f6\u5524\u9192\u5904\u4e8e\u963b\u585e\u72b6\u6001\u7684\u4e8b\u4ef6\u76d1\u63a7\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5f53\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u9700\u8981\u5904\u7406\u7684\u65f6\u5019&#xff0c;\u5904\u7406\u8fc7\u7a0b\u4e2d&#xff0c;\u5982\u679c\u5bf9\u8fde\u63a5\u8981\u8fdb\u884c\u67d0\u4e9b\u64cd\u4f5c&#xff1a; \u8fd9\u4e9b\u64cd\u4f5c\u5fc5\u987b\u5728EventLoop\u5bf9\u5e94\u7684\u7ebf\u7a0b\u4e2d\u6267\u884c&#xff0c;\u4fdd\u8bc1\u5bf9\u8fde\u63a5\u7684\u5404\u9879\u64cd\u4f5c\u90fd\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/span><\/p>\n<li>\n<p><span style=\"color:#0d0016\">\u5982\u679c\u6267\u884c\u7684\u64cd\u4f5c\u672c\u5c31\u5728\u7ebf\u7a0b\u4e2d&#xff0c;\u4e0d\u9700\u8981\u5c06\u64cd\u4f5c\u538b\u5165\u961f\u5217\u4e86&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u6267\u884c<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u5982\u679c\u6267\u884c\u7684\u64cd\u4f5c\u4e0d\u518d\u7ebf\u7a0b\u4e2d&#xff0c;\u624d\u9700\u8981\u52a0\u5165\u4efb\u52a1\u6c60&#xff0c;\u7b49\u5230\u4e8b\u4ef6\u5904\u7406\u5b8c\u4e86\u7136\u540e\u6267\u884c\u4efb\u52a1<\/span><\/p>\n<\/li>\n<h4><span style=\"color:#0d0016\">\u63a5\u53e3\u8bbe\u8ba1<\/span><\/h4>\n<p>class EventLoop {<br \/>\nprivate:<br \/>\n    using Functor &#061; std::function&lt;void()&gt;;<\/p>\n<p>    std::thread::id _thread_id;<br \/>\n    int _event_fd;               \/\/ eventfd\u5524\u9192IO\u4e8b\u4ef6\u76d1\u63a7\u6709\u53ef\u80fd\u5bfc\u81f4\u7684\u963b\u585e<br \/>\n    Poller _poller;              \/\/ \u8fdb\u884c\u6240\u6709\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    std::vector&lt;Functor&gt; _tasks; \/\/ \u4efb\u52a1\u6c60<br \/>\n    std::mutex _mutex;<\/p>\n<p>private:<br \/>\n    \/\/ \u6267\u884c\u4efb\u52a1\u6c60\u4e2d\u7684\u6240\u6709\u4efb\u52a1<br \/>\n    void RunAllTask();<\/p>\n<p>public:<br \/>\n    EventLoop();<br \/>\n    \/\/ \u5224\u65ad\u5c06\u8981\u6267\u884c\u7684\u4efb\u52a1\u662f\u5426\u5904\u4e8e\u5f53\u524d\u7ebf\u7a0b\u4e2d&#xff0c;\u5982\u679c\u662f\u5219\u6267\u884c&#xff0c;\u4e0d\u662f\u5219\u538b\u5165\u961f\u5217<br \/>\n    void RunInLoop(const Functor&amp; cb);<br \/>\n    \/\/ \u5c06\u64cd\u4f5c\u538b\u5165\u4efb\u52a1\u6c60<br \/>\n    void QueueInLoop(const Functor&amp; cb);<br \/>\n    \/\/ \u5224\u65ad\u4efb\u52a1\u6240\u5728\u7684\u7ebf\u7a0b\u662f\u5426\u662f\u5728\u5f53\u524d\u7684\u7ebf\u7a0b\u4e2d\u4e0d\u65ad\u5904\u7406\u7684\u4e8b\u4ef6\u5904\u7406\u5faa\u73af<br \/>\n    bool IsInLoop();<br \/>\n    \/\/ \u4fee\u6539\/\u6dfb\u52a0\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void UpdateEvent(Channel* channel);<br \/>\n    \/\/ \u79fb\u9664\u63cf\u8ff0\u7b26\u7684\u6240\u6709\u76d1\u63a7<br \/>\n    void RemoveEvent(Channel* channel);<\/p>\n<p>    \/\/ \u542f\u52a8EventLoop\u6a21\u5757&#xff1a;\u5148\u8fdb\u884c\u4efb\u52a1\u76d1\u63a7&#xff0c;\u63a5\u4e0b\u6765\u5904\u7406\u5c31\u7eea\u4e8b\u4ef6&#xff0c;\u4e8b\u4ef6\u5904\u7406\u5b8c\u4e86&#xff0c;\u5904\u7406\u4efb\u52a1<br \/>\n    void Start();<br \/>\n}; <\/p>\n<h3><span style=\"color:#0d0016\">6.4 EventLoop\u6a21\u5757\u7c7b\u63a5\u53e3\u5b9e\u73b0\u3001\u6574\u5408\u7f16\u8bd1<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u6ce8\u610f&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u4e0e &#096;std::lock_guard&#096; \u76f8\u6bd4&#xff0c;&#096;std::unique_lock&#096; \u66f4\u52a0\u7075\u6d3b\u3002\u5b83\u5141\u8bb8\u5ef6\u8fdf\u9501\u5b9a\u548c\u89e3\u9501\u64cd\u4f5c&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u6210\u5458\u51fd\u6570\u5982 &#096;lock()&#096; \u548c &#096;unlock()&#096; \u6765\u63a7\u5236\u9501\u7684\u83b7\u53d6\u548c\u91ca\u653e\u65f6\u673a&#xff0c;\u51fa\u4f5c\u7528\u57df\u89e3\u9501\u3002\u800c &#096;std::lock_guard&#096; \u662f\u5728\u6784\u9020\u65f6\u9501\u5b9a\u4e92\u65a5\u9501&#xff0c;\u5728\u6790\u6784\u65f6\u89e3\u9501&#xff0c;\u4e0d\u80fd\u5bf9\u9501\u5b9a\u548c\u89e3\u9501\u7684\u8fc7\u7a0b\u8fdb\u884c\u66f4\u7ec6\u81f4\u7684\u63a7\u5236\u3002<\/span><\/p>\n<p> #include &lt;bits\/std_thread.h&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &lt;stdint.h&gt;<br \/>\n#include &lt;sys\/eventfd.h&gt;<br \/>\nclass EventLoop {<br \/>\nprivate:<br \/>\n    using Functor &#061; std::function&lt;void()&gt;;<\/p>\n<p>    std::thread::id _thread_id;<br \/>\n    int _event_fd; \/\/ eventfd\u5524\u9192IO\u4e8b\u4ef6\u76d1\u63a7\u6709\u53ef\u80fd\u5bfc\u81f4\u7684\u963b\u585e<br \/>\n    \/\/ Channel *_event_channel; \/\/<br \/>\n    \/\/ \u6ce8\u610fEventLoop\u6790\u6784&#xff0c;\u4f46\u662f_event_channel\u5e76\u4e0d\u4f1a\u88ab\u91ca\u653e&#xff0c;\u6240\u4ee5\u8981\u5728\u6790\u6784\u91ccdelete<br \/>\n    \/\/ \u6240\u4ee5\u4f7f\u7528unique_str\u5c06\u8be5\u6307\u9488\u7ba1\u7406\u8d77\u6765&#xff0c;EventLoop\u91ca\u653e\u7684\u65f6\u5019<br \/>\n    \/\/  &#xff0c;unique_ptr\u4e5f\u4f1a\u91ca\u653e&#xff0c;\u8fde\u5e26\u6240\u7ba1\u7406\u7684_event_channel\u4e00\u5e76\u91ca\u653e<br \/>\n    std::unique_ptr&lt;Channel&gt; _event_channel;<br \/>\n    Poller _poller;              \/\/ \u8fdb\u884c\u6240\u6709\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    std::vector&lt;Functor&gt; _tasks; \/\/ \u4efb\u52a1\u6c60<br \/>\n    std::mutex _mutex;<\/p>\n<p>private:<br \/>\n    \/\/ \u6267\u884c\u4efb\u52a1\u6c60\u4e2d\u7684\u6240\u6709\u4efb\u52a1<br \/>\n    void RunAllTask() {<br \/>\n        std::vector&lt;Functor&gt; functor;<br \/>\n        {<br \/>\n            std::unique_lock&lt;std::mutex&gt; _lock(_mutex); \/\/ \u51fa\u4f5c\u7528\u57df\u89e3\u9501<br \/>\n            _tasks.swap(functor); \/\/ functor\u4e2d\u4fdd\u5b58\u6240\u6709\u7684\u4efb\u52a1<br \/>\n        }<br \/>\n        for (auto&amp; f : functor) {<br \/>\n            f();<br \/>\n        }<br \/>\n        return;<br \/>\n    }<br \/>\n    \/\/<br \/>\n    static int CreateEventFd() {<br \/>\n        int efd &#061; eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);<br \/>\n        if (efd &lt; 0) {<br \/>\n            ERR_LOG(&#034;CREATE EVENTFD FAILED!!&#034;);<br \/>\n            abort();<br \/>\n        }<br \/>\n        return efd;<br \/>\n    }<br \/>\n    void ReadEventFd() {<\/p>\n<p>        uint64_t res &#061; 0;<br \/>\n        \/\/ \u8bfb\u53d6\u4e8b\u4ef6<br \/>\n        int ret &#061; read(_event_fd, &amp;res, sizeof(res));<br \/>\n        if (ret &lt; 0) {<br \/>\n            \/\/ EINTR:\u88ab\u4fe1\u53f7\u6253\u65ad&#xff0c;EAGAIN:\u65e0\u6570\u636e\u53ef\u8bfb<br \/>\n            if (errno &#061;&#061; EINTR || errno &#061;&#061; EAGAIN) {<br \/>\n                return;<br \/>\n            }<br \/>\n            ERR_LOG(&#034;READ EVENTFD FAILED&#034;);<br \/>\n        }<br \/>\n        return;<br \/>\n    }<br \/>\n    void WeakUpEvented() {<br \/>\n        \/\/ \u7ed9Eventfd\u5199\u5165\u6570\u636e<br \/>\n        uint64_t val &#061; 1;<br \/>\n        int ret &#061; write(_event_fd, &amp;val, sizeof(val));<br \/>\n        if (ret &lt; 0) {<br \/>\n            if (errno &#061;&#061; EINTR) {<br \/>\n                return;<br \/>\n            }<br \/>\n            ERR_LOG(&#034;READ EVENTFD FAILED&#034;);<br \/>\n        }<br \/>\n        return;<br \/>\n    }<\/p>\n<p>public:<br \/>\n    EventLoop()<br \/>\n        : _thread_id(std::this_thread::get_id()), _event_fd(CreateEventFd()),<br \/>\n          _event_channel(new Channel(this, _event_fd)) \/\/ \u6ce8\u610f_event_fd\u8981\u5148\u5b9a\u4e49<br \/>\n    {<br \/>\n        \/\/ \u7ed9eventfd\u6dfb\u52a0\u53ef\u8bfb\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570&#xff0c;\u8bfb\u53d6eventfd\u4e8b\u4ef6\u901a\u77e5\u6b21\u6570<br \/>\n        _event_channel-&gt;SetReadCallBack(<br \/>\n            std::bind(&amp;EventLoop::ReadEventFd, this));<br \/>\n        \/\/ \u542f\u52a8eventfd\u7684\u8bfb\u4e8b\u4ef6\u76d1\u63a7<br \/>\n        _event_channel-&gt;EnableRead();<br \/>\n    }<br \/>\n    \/\/ \u542f\u52a8EventLoop\u6a21\u5757&#xff1a;1.\u5148\u8fdb\u884c\u4efb\u52a1\u76d1\u63a7&#xff0c;2.\u63a5\u4e0b\u6765\u5904\u7406\u5c31\u7eea\u4e8b\u4ef6&#xff0c;\u4e8b\u4ef6\u5904\u7406\u5b8c\u4e86&#xff0c;3.\u6267\u884c\u4efb\u52a1\u6c60<br \/>\n    void Start() {<br \/>\n        \/\/ 1.\u4e8b\u4ef6\u76d1\u63a7<br \/>\n        std::vector&lt;Channel*&gt; actives;<br \/>\n        _poller.Poll(&amp;actives);<br \/>\n        for (auto&amp; channel : actives) { \/\/ 2.\u4e8b\u4ef6\u5904\u7406<br \/>\n            channel-&gt;HandleEvent();<br \/>\n        }<br \/>\n        \/\/ 3.\u4e8b\u4ef6\u5904\u7406\u5b8c\u4e86&#xff0c;\u6267\u884c\u4efb\u52a1<br \/>\n        RunAllTask();<br \/>\n    }<\/p>\n<p>    \/\/ \u5224\u65ad\u4efb\u52a1\u6240\u5728\u7684\u7ebf\u7a0b\u662f\u5426\u662f\u5728\u5f53\u524d\u7684\u7ebf\u7a0b\u4e2d\u4e0d\u65ad\u5904\u7406\u7684\u4e8b\u4ef6\u5904\u7406\u5faa\u73af<br \/>\n    bool IsInLoop() { return (_thread_id &#061;&#061; std::this_thread::get_id()); }<br \/>\n    \/\/ \u5224\u65ad\u5c06\u8981\u6267\u884c\u7684\u4efb\u52a1\u662f\u5426\u5904\u4e8e\u5f53\u524d\u7ebf\u7a0b\u4e2d&#xff0c;\u5982\u679c\u662f\u5219\u6267\u884c&#xff0c;\u4e0d\u662f\u5219\u538b\u5165\u961f\u5217<br \/>\n    void RunInLoop(const Functor&amp; cb) {<br \/>\n        if (IsInLoop()) {<br \/>\n            return cb();<br \/>\n        }<br \/>\n        return QueueInLoop(cb);<br \/>\n    }<br \/>\n    \/\/ \u5c06\u64cd\u4f5c\u538b\u5165\u4efb\u52a1\u6c60<br \/>\n    void QueueInLoop(const Functor&amp; cb) {<br \/>\n        {<br \/>\n            std::unique_lock&lt;std::mutex&gt; _lock(_mutex);<br \/>\n            _tasks.push_back(cb);<br \/>\n        }<br \/>\n        \/\/ \u5524\u9192\u6709\u53ef\u80fd\u56e0\u4e3a\u6ca1\u6709\u4e8b\u4ef6\u5c31\u7eea\u800c\u5bfc\u81f4\u7684epoll\u963b\u585e<br \/>\n        \/\/ \u5176\u5b9e\u5c31\u662f\u7ed9Eventfd\u5199\u5165\u4e00\u4e2a\u6570\u636e&#xff0c;eventfd\u5c31\u4f1a\u51fa\u53d1\u53ef\u8bfb\u4e8b\u4ef6<br \/>\n        WeakUpEvented();<br \/>\n    }<\/p>\n<p>    \/\/ \u4fee\u6539\/\u6dfb\u52a0\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void UpdateEvent(Channel* channel) { return _poller.UpdateEvent(channel); }<br \/>\n    \/\/ \u79fb\u9664\u63cf\u8ff0\u7b26\u7684\u6240\u6709\u76d1\u63a7<br \/>\n    void RemoveEvent(Channel* channel) { return _poller.RemoveEvent(channel); }<br \/>\n};<br \/>\nvoid Channel::Remove() { return _loop-&gt;RemoveEvent(this); }<br \/>\nvoid Channel::Update() { return _loop-&gt;UpdateEvent(this); } <\/p>\n<p><span style=\"color:#0d0016\">\u6ce8\u610f&#xff1a;\u5728Channel\u4e2d&#xff0c;\u5c06Poller *poller&#8212;-&gt;\u66ff\u6362\u4e3aEventLoop *loop&#xff0c;Remove\u548cUpdate\u4e2d\u4e5f\u9700\u8981\u66f4\u6539\u5bf9\u8c61_poller &#8212;&gt; _loop<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"262\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233617-68534d71a2426.png\" width=\"584\" \/><\/p>\n<h3><span style=\"color:#0d0016\">6.5\u00a0EventLoop\u6a21\u5757\u7c7b\u6574\u5408\u6d4b\u8bd5<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u6d4b\u8bd5\u7528\u4f8b\u4f9d\u65e7\u4f7f\u7528\u524d\u9762\u7684tcp_svr.cc\u548ctcp_cli.cc&#xff0c;\u4ec5\u4fee\u6539&#xff1a;\u5c06tcp_svr.cc\u4e2d\u7684Poller &#8212;&gt; EventLoop&#xff0c;poller &#8212;&gt; loop&#xff0c;\u4ee5\u53ca\u5faa\u73af\u5185\u5904\u7406\u5c31\u7eea\u4e8b\u4ef6\u6539\u4e3a&#xff1a;loop.Start()<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"348\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233618-68534d720f779.png\" width=\"516\" \/><\/p>\n<h2><span style=\"color:#0d0016\">\u4e03\u3001TimerWheel\u5b9a\u65f6\u5668\u6a21\u5757\u6574\u5408<\/span><\/h2>\n<p><span style=\"color:#0d0016\">\u5728\u6211\u7684\u4e0a\u4e00\u7bc7\u535a\u5ba2\u6709\u8be6\u7ec6\u8bb2\u89e3\u4e86\u65f6\u95f4\u8f6e\u5b9a\u65f6\u5668\u539f\u7406\u4ee5\u53ca\u5b9e\u73b0&#xff0c;\u672c\u6a21\u5757\u5bf9\u4e0a\u7bc7\u535a\u5ba2\u4e2d\u7684\u4ee3\u7801\u8fdb\u884c\u8c03\u6574&#xff0c;\u5c06timerfd.cpp &#043; timerwheel.cpp\u6574\u5408\u6210\u4e00\u4e2a\u7c7b\u81f3server.hpp\u4e2d&#xff0c;\u4ee3\u7801\u4e5f\u5728\u6211\u7684gitee\u4e2d\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">timerfd&#xff1a;\u8d1f\u8d23\u5b9e\u73b0\u5185\u6838\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4&#xff0c;\u7ed9\u8fdb\u7a0b\u89e6\u53d1\u4e00\u6b21\u5b9a&#061;\u8d85\u65f6\u4e8b\u4ef6<\/span><\/p>\n<p><span style=\"color:#0d0016\">timerwheel&#xff1a;\u8d1f\u8d23\u5b9e\u73b0\u6bcf\u6b21\u6267\u884cRunTimeTask&#xff0c;\u90fd\u53ef\u4ee5\u6267\u884c\u4e00\u6279\u5230\u671f\u7684\u5b9a\u65f6\u4efb\u52a1<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u8981\u5b9e\u73b0\u4e00\u4e2a\u5b8c\u6574\u7684\u79d2\u7ea7\u5b9a\u65f6\u5668&#xff0c;\u5c31\u9700\u8981\u5c06\u8fd9\u4e24\u4e2a\u529f\u80fd\u6574\u5408\u81f3\u4e00\u8d77&#xff0c;\u5e76\u4e14\u4e0eChannel\u8054\u7cfb\u8d77\u6765<\/span><\/p>\n<h3><span style=\"color:#0d0016\">7.1 EventLoop\u4e0eTimerWheel\u6a21\u5757\u6574\u5408\u4e0e\u7f16\u8bd1<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u8fd9\u4e00\u5904\u7684\u4ee3\u7801\u7684\u8c03\u6574\u53ef\u4ee5\u770b\u6211\u7684gitee<\/span><\/p>\n<h3><span style=\"color:#0d0016\">7.2 EventLoop\u4e0eTimerWheel\u8054\u5408\u8c03\u8bd5<\/span><\/h3>\n<p><span style=\"color:#0d0016\">tcp_svr.cpp\u4fee\u6539\u5904&#xff1a;<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"146\" src=\"2025-06-19e3sfidya44y.png\" width=\"536\" \/><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"264\" src=\"2025-06-19kt3pultupnv.png\" width=\"542\" \/><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"408\" src=\"2025-06-19tz4ywjgglw5.png\" width=\"531\" \/><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"196\" src=\"2025-06-19ujtmwyu5prw.png\" width=\"534\" \/><\/p>\n<p><span style=\"color:#0d0016\">tcp_cli.cpp<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"432\" src=\"2025-06-19qf0cyiuzzs0.png\" width=\"486\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u6211\u8fd9\u91cc\u6240\u9a8c\u8bc1\u7684\u5b9e\u9a8c\u73b0\u8c61\u662f&#xff0c;\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u53d1\u90015\u6b21\u6570\u636e&#xff08;5s&#xff09;&#xff0c;\u5c31\u6b7b\u5faa\u73af\u4e0d\u518d\u53d1\u9001\u6d88\u606f&#xff0c;\u670d\u52a1\u5668\u5c31\u4e0d\u518d\u5237\u65b0\u6d3b\u8dc3\u5ea6&#xff0c;10s\u8fc7\u540e\u5c31\u5173\u95ed\u8be5\u8fde\u63a5<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"723\" src=\"2025-06-193mucctrg3n0.png\" width=\"1633\" \/><\/p>\n<h3><span style=\"color:#0d0016\">7.3 EventLoop\u6a21\u5757\u8054\u8c03\u4e2d&#xff08;\u7b80\u5355\u670d\u52a1\u5668&#xff09;\u7684\u6a21\u5757\u6d41\u7a0b\u5173\u7cfb\u56fe<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u6839\u636e\u4ee5\u4e0b\u8fd9\u5e45\u56fe\u6240\u6807\u660e\u7684\u6b65\u9aa4\u7ed3\u5408\u6211\u4eec\u7684\u4ee3\u7801\u6765\u770b\u4fbf\u4e8e\u7406\u89e3<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"3997\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233559-68534d5fd167f.png\" width=\"4321\" \/><\/p>\n<\/p>\n<p><span style=\"color:#0d0016\">\u7ed3\u8bed&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0 \u00a0 \u00a0\u968f\u7740\u8fd9\u7bc7\u535a\u5ba2\u63a5\u8fd1\u5c3e\u58f0&#xff0c;\u6211\u8877\u5fc3\u5e0c\u671b\u6211\u6240\u5206\u4eab\u7684\u5185\u5bb9\u80fd\u4e3a\u4f60\u5e26\u6765\u4e00\u4e9b\u542f\u53d1\u548c\u5e2e\u52a9\u3002\u5b66\u4e60\u548c\u7406\u89e3\u7684\u8fc7\u7a0b\u5f80\u5f80\u5145\u6ee1\u6311\u6218&#xff0c;\u4f46\u6b63\u662f\u8fd9\u4e9b\u6311\u6218\u8ba9\u6211\u4eec\u4e0d\u65ad\u6210\u957f\u548c\u8fdb\u6b65\u3002\u6211\u5728\u51c6\u5907\u8fd9\u7bc7\u6587\u7ae0\u65f6&#xff0c;\u4e5f\u6df1\u523b\u4f53\u4f1a\u5230\u4e86\u5b66\u4e60\u4e0e\u5206\u4eab\u7684\u4e50\u8da3\u3002 \u00a0 \u00a0<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u5728\u6b64&#xff0c;\u6211\u8981\u7279\u522b\u611f\u8c22\u6bcf\u4e00\u4f4d\u9605\u8bfb\u5230\u8fd9\u91cc\u7684\u4f60\u3002\u662f\u4f60\u7684\u5173\u6ce8\u548c\u652f\u6301&#xff0c;\u7ed9\u4e88\u4e86\u6211\u6301\u7eed\u5199\u4f5c\u548c\u5206\u4eab\u7684\u52a8\u529b\u3002\u6211\u6df1\u77e5&#xff0c;\u65e0\u8bba\u6211\u5728\u67d0\u4e2a\u9886\u57df\u6709\u591a\u5c11\u89c1\u89e3&#xff0c;\u90fd\u79bb\u4e0d\u5f00\u5927\u5bb6\u7684\u9f13\u52b1\u4e0e\u6307\u6b63\u3002\u56e0\u6b64&#xff0c;\u5982\u679c\u4f60\u5728\u9605\u8bfb\u8fc7\u7a0b\u4e2d\u6709\u4efb\u4f55\u7591\u95ee\u3001\u5efa\u8bae\u6216\u662f\u53d1\u73b0\u4e86\u6587\u7ae0\u4e2d\u7684\u4e0d\u8db3\u4e4b\u5904&#xff0c;\u90fd\u6b22\u8fce\u4f60\u6177\u6168\u8d50\u6559\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0 \u00a0 \u00a0 \u4f60\u7684\u6bcf\u4e00\u6761\u53cd\u9988\u90fd\u662f\u6211\u524d\u8fdb\u8def\u4e0a\u7684\u5b9d\u8d35\u8d22\u5bcc\u3002\u540c\u65f6&#xff0c;\u6211\u4e5f\u975e\u5e38\u671f\u5f85\u80fd\u591f\u5f97\u5230\u4f60\u7684\u70b9\u8d5e\u3001\u6536\u85cf&#xff0c;\u5173\u6ce8\u8fd9\u5c06\u662f\u5bf9\u6211\u83ab\u5927\u7684\u652f\u6301\u548c\u9f13\u52b1\u3002\u5f53\u7136&#xff0c;\u6211\u66f4\u671f\u5f85\u7684\u662f\u80fd\u591f\u6301\u7eed\u4e3a\u4f60\u5e26\u6765\u6709\u4ef7\u503c\u7684\u5185\u5bb9\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb905\u6b21\uff0c\u70b9\u8d5e22\u6b21\uff0c\u6536\u85cf23\u6b21\u3002\u672c\u6587\u6458\u8981\uff1a\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u7f51\u7edc\u7f16\u7a0b\u4e2d\u5173\u952e\u6a21\u5757\u7684\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff0c\u5305\u62ecBuffer\u7f13\u51b2\u533a\u6a21\u5757\u3001Socket\u5957\u63a5\u5b57\u6a21\u5757\u3001Channel\u4e8b\u4ef6\u7ba1\u7406\u6a21\u5757\u3001Poller\u63cf\u8ff0\u7b26\u76d1\u63a7\u6a21\u5757\u3001EventLoop\u4e8b\u4ef6\u5faa\u73af\u6a21\u5757\u548cTimerWheel\u5b9a\u65f6\u5668\u6a21\u5757\u3002Buffer\u6a21\u5757\u91c7\u7528vector&lt;char&gt;\u4f5c\u4e3a\u5e95\u5c42\u5bb9\u5668\uff0c\u907f\u514dstring\u7684\\&#8217;\\\\0\\&#8217;\u622a\u65ad\u95ee\u9898\uff0c\u63d0\u4f9b\u6570\u636e\u5b58\u50a8\u548c\u7ba1\u7406\u529f\u80fd\uff1bSocket\u6a21\u5757\u5c01\u88c5\u4e86TCP\/UDP\u5957\u63a5\u5b57\u64cd\u4f5c\uff1bChannel\u548cPoller\u6a21\u5757\u5b9e\u73b0\u5bf9\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7\u7ba1\u7406\uff1bEventLoop\u6a21\u5757\u91c7\u7528one thread<\/p>\n","protected":false},"author":2,"featured_media":44441,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,43,211,44],"topic":[],"class_list":["post-44461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-linux","tag-43","tag-211","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u9879\u76ee\u3011\u4effmuduo\u5e93one thread one loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\uff08\u4e0a\uff09 - \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\/44461.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u9879\u76ee\u3011\u4effmuduo\u5e93one thread one loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\uff08\u4e0a\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb905\u6b21\uff0c\u70b9\u8d5e22\u6b21\uff0c\u6536\u85cf23\u6b21\u3002\u672c\u6587\u6458\u8981\uff1a\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u7f51\u7edc\u7f16\u7a0b\u4e2d\u5173\u952e\u6a21\u5757\u7684\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff0c\u5305\u62ecBuffer\u7f13\u51b2\u533a\u6a21\u5757\u3001Socket\u5957\u63a5\u5b57\u6a21\u5757\u3001Channel\u4e8b\u4ef6\u7ba1\u7406\u6a21\u5757\u3001Poller\u63cf\u8ff0\u7b26\u76d1\u63a7\u6a21\u5757\u3001EventLoop\u4e8b\u4ef6\u5faa\u73af\u6a21\u5757\u548cTimerWheel\u5b9a\u65f6\u5668\u6a21\u5757\u3002Buffer\u6a21\u5757\u91c7\u7528vector&lt;char&gt;\u4f5c\u4e3a\u5e95\u5c42\u5bb9\u5668\uff0c\u907f\u514dstring\u7684&#039;\\0&#039;\u622a\u65ad\u95ee\u9898\uff0c\u63d0\u4f9b\u6570\u636e\u5b58\u50a8\u548c\u7ba1\u7406\u529f\u80fd\uff1bSocket\u6a21\u5757\u5c01\u88c5\u4e86TCP\/UDP\u5957\u63a5\u5b57\u64cd\u4f5c\uff1bChannel\u548cPoller\u6a21\u5757\u5b9e\u73b0\u5bf9\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7\u7ba1\u7406\uff1bEventLoop\u6a21\u5757\u91c7\u7528one thread\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/44461.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-18T23:36:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233559-68534d5fd167f.png\" \/>\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=\"21 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44461.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/44461.html\",\"name\":\"\u3010\u9879\u76ee\u3011\u4effmuduo\u5e93one thread one loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\uff08\u4e0a\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-18T23:36:19+00:00\",\"dateModified\":\"2025-06-18T23:36:19+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/44461.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/44461.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44461.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u9879\u76ee\u3011\u4effmuduo\u5e93one thread one loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\uff08\u4e0a\uff09\"}]},{\"@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":"\u3010\u9879\u76ee\u3011\u4effmuduo\u5e93one thread one loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\uff08\u4e0a\uff09 - \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\/44461.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u9879\u76ee\u3011\u4effmuduo\u5e93one thread one loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\uff08\u4e0a\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb905\u6b21\uff0c\u70b9\u8d5e22\u6b21\uff0c\u6536\u85cf23\u6b21\u3002\u672c\u6587\u6458\u8981\uff1a\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u7f51\u7edc\u7f16\u7a0b\u4e2d\u5173\u952e\u6a21\u5757\u7684\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff0c\u5305\u62ecBuffer\u7f13\u51b2\u533a\u6a21\u5757\u3001Socket\u5957\u63a5\u5b57\u6a21\u5757\u3001Channel\u4e8b\u4ef6\u7ba1\u7406\u6a21\u5757\u3001Poller\u63cf\u8ff0\u7b26\u76d1\u63a7\u6a21\u5757\u3001EventLoop\u4e8b\u4ef6\u5faa\u73af\u6a21\u5757\u548cTimerWheel\u5b9a\u65f6\u5668\u6a21\u5757\u3002Buffer\u6a21\u5757\u91c7\u7528vector&lt;char&gt;\u4f5c\u4e3a\u5e95\u5c42\u5bb9\u5668\uff0c\u907f\u514dstring\u7684'\\0'\u622a\u65ad\u95ee\u9898\uff0c\u63d0\u4f9b\u6570\u636e\u5b58\u50a8\u548c\u7ba1\u7406\u529f\u80fd\uff1bSocket\u6a21\u5757\u5c01\u88c5\u4e86TCP\/UDP\u5957\u63a5\u5b57\u64cd\u4f5c\uff1bChannel\u548cPoller\u6a21\u5757\u5b9e\u73b0\u5bf9\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7\u7ba1\u7406\uff1bEventLoop\u6a21\u5757\u91c7\u7528one thread","og_url":"https:\/\/www.wsisp.com\/helps\/44461.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-18T23:36:19+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250618233559-68534d5fd167f.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"21 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/44461.html","url":"https:\/\/www.wsisp.com\/helps\/44461.html","name":"\u3010\u9879\u76ee\u3011\u4effmuduo\u5e93one thread one loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\uff08\u4e0a\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-18T23:36:19+00:00","dateModified":"2025-06-18T23:36:19+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/44461.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/44461.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/44461.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u9879\u76ee\u3011\u4effmuduo\u5e93one thread one loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\uff08\u4e0a\uff09"}]},{"@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\/44461","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=44461"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/44461\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/44441"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=44461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=44461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=44461"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=44461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}