{"id":85705,"date":"2026-07-27T21:24:26","date_gmt":"2026-07-27T13:24:26","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/85705.html"},"modified":"2026-07-27T21:24:26","modified_gmt":"2026-07-27T13:24:26","slug":"%e4%bb%8e%e9%9b%b6%e6%89%8b%e5%86%99%e9%ab%98%e6%80%a7%e8%83%bd-c-tcp-%e6%9c%8d%e5%8a%a1%e5%99%a8%e6%a1%86%e6%9e%b6%ef%bc%88%e4%b8%83%ef%bc%89%ef%bc%9achannel%e6%a8%a1%e5%9d%97%e5%92%8cpoller","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/85705.html","title":{"rendered":"\u4ece\u96f6\u624b\u5199\u9ad8\u6027\u80fd C++ TCP \u670d\u52a1\u5668\u6846\u67b6\uff08\u4e03\uff09\uff1aChannel\u6a21\u5757\u548cPoller\u6a21\u5757\u5b9e\u73b0"},"content":{"rendered":"<h2>\u4ece\u96f6\u624b\u5199\u9ad8\u6027\u80fd C&#043;&#043; TCP \u670d\u52a1\u5668\u6846\u67b6&#xff08;\u4e03&#xff09;&#xff1a;Channel\u6a21\u5757\u548cPoller\u6a21\u5757\u5b9e\u73b0<\/h2>\n<h3>\u4e00\u3001\u524d\u7f6e\u77e5\u8bc6<\/h3>\n<h4>\u00a0 \u00a0 \u00a0 \u00a0 \u4ecb\u7ecd\u00a0epoll\u00a0<\/h4>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 epoll \u662f Linux \u5185\u6838\u63d0\u4f9b\u7684\u4e00\u79cd\u53ef\u6269\u5c55\u7684 I\/O \u4e8b\u4ef6\u901a\u77e5\u673a\u5236&#xff0c;\u4e13\u95e8\u7528\u4e8e\u9ad8\u6548\u5730\u76d1\u63a7\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5982 socket\u3001\u7ba1\u9053\u3001\u7ec8\u7aef\u7b49&#xff09;&#xff0c;\u7b49\u5f85\u5176\u4e2d\u7684\u67d0\u4e9b\u53d8\u4e3a\u53ef\u8bfb\u3001\u53ef\u5199\u6216\u53d1\u751f\u5f02\u5e38\u3002\u5b83\u662f\u4f20\u7edf select \u548c poll \u7684\u66ff\u4ee3\u65b9\u6848&#xff0c;\u8bbe\u8ba1\u4e0a\u89e3\u51b3\u4e86\u540e\u4e24\u8005\u5728\u8fde\u63a5\u6570\u4e0a\u5347\u65f6\u6027\u80fd\u7ebf\u6027\u4e0b\u964d\u7684\u95ee\u9898\u3002<\/p>\n<h4>\u00a0 \u00a0 \u00a0 \u00a0 \u4e3a\u4ec0\u4e48\u4f7f\u7528 epoll&#xff1f;<\/h4>\n<ul>\n<li>select\/poll \u6bcf\u6b21\u8c03\u7528\u90fd\u9700\u8981\u5c06\u6574\u4e2a\u88ab\u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408\u4ece\u7528\u6237\u6001\u62f7\u8d1d\u5230\u5185\u6838\u6001&#xff0c;\u8fd4\u56de\u65f6\u518d\u6b21\u62f7\u8d1d\u56de\u6765\u3002<\/li>\n<li>\u5185\u6838\u901a\u8fc7\u8f6e\u8be2\u6240\u6709\u63cf\u8ff0\u7b26\u6765\u68c0\u67e5\u4e8b\u4ef6&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\u3002<\/li>\n<li>\u652f\u6301\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u6709\u4e0a\u9650&#xff0c;select \u901a\u5e38\u53d7 FD_SETSIZE \u9650\u5236&#xff0c;poll \u867d\u7136\u6ca1\u6709\u786c\u9650\u5236\u4f46\u540c\u6837\u5b58\u5728\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ul>\n<h4>\u00a0 \u00a0 \u00a0 \u00a0 epoll \u7684\u4f18\u52bf<\/h4>\n<ul>\n<li>\u4f7f\u7528\u5185\u6838\u4e8b\u4ef6\u8868&#xff08;eventpoll \u5bf9\u8c61&#xff09;&#xff0c;\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ea\u9700\u6ce8\u518c\u4e00\u6b21&#xff0c;\u65e0\u9700\u6bcf\u6b21\u91cd\u590d\u4f20\u9012\u3002<\/li>\n<li>\u91c7\u7528\u56de\u8c03\u673a\u5236&#xff0c;\u5f53\u63cf\u8ff0\u7b26\u5c31\u7eea\u65f6\u5185\u6838\u4f1a\u5c06\u4e8b\u4ef6\u653e\u5165\u5c31\u7eea\u94fe\u8868&#xff0c;epoll_wait \u53ea\u8fd4\u56de\u5c31\u7eea\u4e8b\u4ef6&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(1)\u3002<\/li>\n<li>\u6ca1\u6709\u6700\u5927\u63cf\u8ff0\u7b26\u6570\u7684\u786c\u9650\u5236&#xff08;\u4ec5\u53d7\u7cfb\u7edf\u5168\u5c40\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u548c\u5185\u5b58\u9650\u5236&#xff09;\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e8c\u3001Channel\u6a21\u5757<\/h3>\n<h4>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e3a\u4ec0\u4e48\u9700\u8981 Channel<\/h4>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5728\u4e0a\u4e00\u8282\u4e2d&#xff0c;\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86 Socket \u7c7b\u7684\u5c01\u88c5&#xff0c;\u5b83\u80fd\u591f\u521b\u5efa\u5957\u63a5\u5b57\u3001\u7ed1\u5b9a\u5730\u5740\u3001\u76d1\u542c \/ \u8fde\u63a5&#xff0c;\u4ee5\u53ca\u8fdb\u884c\u963b\u585e\u6216\u975e\u963b\u585e\u7684\u8bfb\u5199\u3002\u4f46\u4ec5\u6709\u88f8\u7684 Socket\u00a0\u8fd8\u8fdc\u8fdc\u4e0d\u591f\u2014\u2014\u6211\u4eec\u7684\u76ee\u6807\u662f\u6784\u5efa\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684 Reactor \u7f51\u7edc\u6846\u67b6&#xff0c;\u6838\u5fc3\u5c31\u5728\u4e8e&#xff1a;\u8ba9 Poller\u76d1\u63a7\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5e76\u5728\u5c31\u7eea\u65f6\u56de\u8c03\u5bf9\u5e94\u7684\u5904\u7406\u51fd\u6570\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8fd9\u5c31\u9700\u8981\u4e00\u4e2a\u6865\u6881&#xff0c;\u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u76d1\u63a7\u7684\u4e8b\u4ef6\u3001\u5c31\u7eea\u540e\u56de\u8c03\u7684\u51fd\u6570\u8fd9\u4e09\u8005\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002\u8fd9\u4e2a\u6865\u6881\u5c31\u662f Channel \u6a21\u5757\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Channel \u5e76\u4e0d\u62e5\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u751f\u547d\u5468\u671f&#xff0c;\u5b83\u53ea\u662f\u5bf9\u4e00\u4e2a\u5df2\u6253\u5f00\u7684 fd \u8fdb\u884c\u4e8b\u4ef6\u7ba1\u7406\u3002\u901a\u5e38\u4e00\u4e2a TCP \u8fde\u63a5&#xff08;Connection&#xff09;\u4f1a\u6301\u6709\u4e00\u4e2a Socket \u548c\u4e00\u4e2a Channel&#xff0c;Socket\u00a0\u8d1f\u8d23\u8bfb\u5199&#xff0c;Channel\u00a0\u8d1f\u8d23\u544a\u8bc9 Poller &#xff1a;\u201c\u6211\u5bf9\u8fd9\u4e2a fd \u7684\u54ea\u4e9b\u4e8b\u4ef6\u611f\u5174\u8da3\u201d&#xff0c;\u5e76\u5728\u4e8b\u4ef6\u5c31\u7eea\u65f6\u8c03\u7528Connection\u63d0\u4f9b\u7684\u4e1a\u52a1\u56de\u8c03\u3002\u8fd9\u4e2aConnection\u4f1a\u5728\u4e4b\u540e\u8fdb\u884c\u7f16\u5199\u3002<\/p>\n<h4>\u76f8\u5173\u529f\u80fd&#xff1a;<\/h4>\n<li>\n<p>\u4e8b\u4ef6\u7ba1\u7406&#xff1a;<br \/>\n\u63cf\u8ff0\u7b26\u662f\u5426\u53ef\u8bfb<br \/>\n\u63cf\u8ff0\u7b26\u662f\u5426\u53ef\u5199<br \/>\n\u5bf9\u63cf\u8ff0\u7b26\u76d1\u63a7\u53ef\u8bfb<br \/>\n\u5bf9\u63cf\u8ff0\u7b26\u76d1\u63a7\u53ef\u5199<br \/>\n\u89e3\u9664\u53ef\u8bfb\u4e8b\u4ef6\u76d1\u63a7<br \/>\n\u89e3\u9664\u53ef\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n\u89e3\u9664\u6240\u6709\u4e8b\u4ef6\u76d1\u63a7<\/p>\n<\/li>\n<li>\n<p>\u4e8b\u4ef6\u89e6\u53d1\u540e\u7684\u5904\u7406\u7ba1\u7406<br \/>\n\u9700\u8981\u5904\u7406\u7684\u4e8b\u4ef6&#xff1a;\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u6302\u65ad\u3001\u9519\u8bef\u4ee5\u53ca\u4efb\u610f\u4e8b\u4ef6\u5904\u7406\u7684\u56de\u8c03\u51fd\u6570<\/p>\n<\/li>\n<h4>\u4ecb\u7ecd\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u6302\u65ad\u3001\u9519\u8bef\u4ee5\u53ca\u4efb\u610f\u4e8b\u4ef6\u5904\u7406<\/h4>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u9700\u8981\u6ce8\u610f&#xff0c;\u8fd9\u4e2aChannel\u6a21\u5757\u662f\u975e\u5e38\u62bd\u8c61\u7684&#xff0c;\u662f\u73b0\u5728\u5199\u7684\u6a21\u5757\u4e2d\u6700\u96be\u61c2\u7684&#xff0c;\u53ef\u4ee5\u6df1\u5ea6\u7406\u89e3\u56de\u8c03\u51fd\u6570\u673a\u5236&#xff0c;\u53bb\u89e3\u8026\u5e95\u5c42\u673a\u5236\u4e0e\u4e0a\u5c42\u4e1a\u52a1&#xff0c;\u7528\u56de\u8c03\u6784\u5efa\u4e8b\u4ef6\u9a71\u52a8\u7684\u5f02\u6b65\u6d41\u7a0b\u3002\u56e0\u6b64\u6211\u4eec\u5728\u8fd9\u7406\u89e3\u4e0b\u8fd9\u4e9b\u56de\u8c03\u51fd\u6570\u5728\u4ec0\u4e48\u65f6\u5019\u4f1a\u88ab\u56de\u8c03\u5904\u7406&#xff1a;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/p>\n<h5>1. \u4efb\u610f\u4e8b\u4ef6\u56de\u8c03 _event_callback<\/h5>\n<ul>\n<li>\n<p>\u4f55\u65f6\u6267\u884c&#xff1a;\u53ea\u8981 HandleEvent \u88ab\u8c03\u7528&#xff0c;\u5e76\u4e14\u8fdb\u5165\u4e86\u4efb\u4f55\u4e00\u4e2a\u6709\u6548\u4e8b\u4ef6\u5206\u652f&#xff08;\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u9519\u8bef\u6216\u6302\u65ad&#xff09;&#xff0c;\u5728\u5177\u4f53\u7684\u8bfb\u5199\u56de\u8c03\u4e4b\u524d&#xff0c;\u5b83\u5c31\u4f1a\u88ab\u9996\u5148\u6267\u884c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5178\u578b\u573a\u666f&#xff1a;\u6bcf\u6b21\u8fde\u63a5\u6709\u201c\u52a8\u9759\u201d\u65f6\u90fd\u4f1a\u89e6\u53d1\u3002\u9002\u5408\u7528\u6765\u66f4\u65b0\u8fde\u63a5\u7684\u6700\u540e\u6d3b\u8dc3\u65f6\u95f4\u3001\u7edf\u8ba1\u4e8b\u4ef6\u6b21\u6570\u3001\u6253\u5370\u8c03\u8bd5\u4fe1\u606f\u7b49\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>2. \u53ef\u8bfb\u56de\u8c03 _read_callback<\/h5>\n<ul>\n<li>\n<p>\u4f55\u65f6\u6267\u884c&#xff1a;\u5f53 _revents\u00a0\u4e2d\u5305\u542b\u00a0EPOLLIN\u3001EPOLLRDHUP&#xff08;\u5bf9\u7aef\u5173\u95ed\u5199\u7aef&#xff09;\u6216\u00a0EPOLLPRI&#xff08;\u5e26\u5916\u6570\u636e&#xff09;\u65f6\u3002<\/p>\n<\/li>\n<li>\n<p>\u89e6\u53d1\u6761\u4ef6&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5185\u6838 socket \u63a5\u6536\u7f13\u51b2\u533a\u4e2d\u6709\u6570\u636e&#xff0c;\u53ef\u4ee5\u65e0\u963b\u585e\u5730\u8c03\u7528 recv \u8bfb\u53d6\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u7aef\u8c03\u7528\u4e86 close()\u00a0\u00a0\u6216 shutdown&#xff0c;\u672c\u7aef\u4f1a\u6536\u5230\u00a0EPOLLRDHUP&#xff08;\u9700\u5728 _events \u4e2d\u663e\u5f0f\u8bbe\u7f6e\u76d1\u63a7&#xff09;\u6216\u00a0EPOLLIN&#xff08;\u8bfb\u8fd4\u56de 0&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u6536\u5230 TCP \u5e26\u5916\u6570\u636e&#xff08;\u7d27\u6025\u6307\u9488&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5>3. \u53ef\u5199\u56de\u8c03 _write_callback<\/h5>\n<ul>\n<li>\n<p>\u4f55\u65f6\u6267\u884c&#xff1a;\u5f53\u00a0_revents\u00a0\u00a0\u4e2d\u5305\u542b\u00a0EPOLLOUT\u00a0\u65f6\u3002<\/p>\n<\/li>\n<li>\n<p>\u89e6\u53d1\u6761\u4ef6&#xff1a;\u5185\u6838 socket \u53d1\u9001\u7f13\u51b2\u533a\u4ece\u201c\u6ee1\u201d\u53d8\u4e3a\u201c\u6709\u53ef\u7528\u7a7a\u95f4\u201d&#xff08;\u8fb9\u7f18\u89e6\u53d1&#xff09;\u6216\u4e00\u76f4\u6709\u7a7a\u95f4&#xff08;\u6c34\u5e73\u89e6\u53d1&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>4. \u9519\u8bef\u56de\u8c03 _error_callback<\/h5>\n<ul>\n<li>\n<p>\u4f55\u65f6\u6267\u884c&#xff1a;\u5f53\u00a0_revents\u00a0\u00a0\u4e2d\u5305\u542b\u00a0EPOLLERR\u00a0\u65f6\u3002<\/p>\n<\/li>\n<li>\n<p>\u89e6\u53d1\u6761\u4ef6&#xff1a;socket \u53d1\u751f\u4e86\u5f02\u6b65\u9519\u8bef&#xff08;\u5982 RST \u54cd\u5e94\u3001ICMP \u9519\u8bef\u7b49&#xff09;\u3002\u8fd9\u4e2a\u4e8b\u4ef6\u65e0\u9700\u7528\u6237\u4e3b\u52a8\u8bbe\u7f6e&#xff0c;\u5185\u6838\u4f1a\u81ea\u52a8\u76d1\u542c\u5e76\u8fd4\u56de\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>5. \u5173\u95ed\u56de\u8c03\u00a0_close_callback<\/h5>\n<ul>\n<li>\n<p>\u4f55\u65f6\u6267\u884c&#xff1a;\u5f53\u00a0_revents\u00a0\u00a0\u4e2d\u5305\u542b\u00a0EPOLLHUP\u00a0\u65f6\u3002<\/p>\n<\/li>\n<li>\n<p>\u89e6\u53d1\u6761\u4ef6&#xff1a;\u8fde\u63a5\u6302\u8d77&#xff0c;\u5bf9\u7aef\u8fdb\u7a0b\u5d29\u6e83\u3001\u8c03\u7528 close()\u3001\u6216\u672c\u7aef shutdown \u5199\u7aef\u7b49\u5bfc\u81f4\u8bfb\u5199\u901a\u9053\u5b8c\u5168\u5173\u95ed\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>\u76f8\u5173\u63a5\u53e3&#xff1a;<\/h4>\n<li>\u542f\u7528 \/ \u7981\u7528\u4e8b\u4ef6&#xff1a;EnableRead()\u3001EnableWrite()\u3001DisableRead()\u3001DisableWrite()\u3001DisableAll()\u3002<\/li>\n<li>\u8bbe\u7f6e\u56de\u8c03&#xff1a;\u4e00\u7cfb\u5217 SetXxxCallBack \u65b9\u6cd5\u3002<\/li>\n<li>\u4e8b\u4ef6\u5206\u6d3e&#xff1a;HandleEvent()&#xff0c;\u6839\u636e _revents \u8c03\u7528\u76f8\u5e94\u7684\u56de\u8c03\u3002<\/li>\n<li>\u4e0e Poller \u4ea4\u4e92&#xff1a;Update() \u5c06\u5f53\u524d _events \u540c\u6b65\u5230 epoll&#xff1b;Remove() \u4ece epoll \u4e2d\u79fb\u9664\u3002<\/li>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7531\u4e8e\u8003\u8651\u5230\u5927\u5bb6\u7b2c\u4e00\u6b21\u5199\u8fd9\u4e2a Channel \u6a21\u5757&#xff0c;\u6211\u4eec\u662f\u5148\u4e0d\u548c Poller \u6a21\u5757\u8fdb\u884c\u4ea4\u4e92&#xff0c;\u5230\u4e0b\u9762 Poller \u6a21\u5757\u5199\u5b8c\u4e4b\u540e&#xff0c;\u5728\u4fee\u6539 Channel \u4ee3\u7801&#xff0c;\u7136\u540e\u5b8c\u6210\u8054\u5408\u8c03\u8bd5<\/p>\n<h4>\u7f16\u5199\u4ee3\u7801&#xff1a;\u00a0 \u00a0 \u00a0 \u00a0<\/h4>\n<p>#ifndef __CHANNEL__<br \/>\n#define __CHANNEL__<\/p>\n<p>#include &lt;functional&gt;<br \/>\n#include &lt;cstdint&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<\/p>\n<p>namespace xxhh<br \/>\n{<br \/>\n    class Poller;<\/p>\n<p>    class Channel : public std::enable_shared_from_this&lt;Channel&gt;<br \/>\n    {<br \/>\n    public:<br \/>\n        using EventCallBack &#061; std::function&lt;void(std::shared_ptr&lt;Channel&gt;)&gt;;<\/p>\n<p>        \/\/ \u6784\u9020\u51fd\u6570&#xff1a;\u9700\u8981 Poller \u6307\u9488&#xff0c;\u7528\u4e8e\u540e\u7eed Update\/Remove<br \/>\n        Channel(Poller *poller, int fd)<br \/>\n            : _sockfd(fd), _poll(poller), _events(0), _revents(0) {}<\/p>\n<p>        int Fd() const { return _sockfd; }<br \/>\n        uint32_t Events() const { return _events; }<br \/>\n        void SetREvents(uint32_t events) { _revents &#061; events; }<\/p>\n<p>        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 SetEventCallBack(const EventCallBack &amp;cb) { _event_callback &#061; cb; }<\/p>\n<p>        bool ReadAble() const { return (_events &amp; EPOLLIN); }<br \/>\n        bool WriteAble() const { return (_events &amp; EPOLLOUT); }<\/p>\n<p>        void EnableRead()<br \/>\n        {<br \/>\n            _events |&#061; EPOLLIN;<br \/>\n            Update();<br \/>\n        }<br \/>\n        void EnableWrite()<br \/>\n        {<br \/>\n            _events |&#061; EPOLLOUT;<br \/>\n            Update();<br \/>\n        }<br \/>\n        void DisableRead()<br \/>\n        {<br \/>\n            _events &amp;&#061; ~EPOLLIN;<br \/>\n            Update();<br \/>\n        }<br \/>\n        void DisableWrite()<br \/>\n        {<br \/>\n            _events &amp;&#061; ~EPOLLOUT;<br \/>\n            Update();<br \/>\n        }<\/p>\n<p>        void Remove(); \/\/ \u5b9e\u73b0\u89c1 poller.hpp \u5c3e\u90e8<br \/>\n        void Update(); \/\/ \u5b9e\u73b0\u89c1 poller.hpp \u5c3e\u90e8<\/p>\n<p>        void HandleEvent()<br \/>\n        {<br \/>\n            auto self &#061; shared_from_this(); \/\/ \u4fdd\u8bc1\u5728 HandleEvent \u5168\u7a0b\u5bf9\u8c61\u4e0d\u9500\u6bc1<\/p>\n<p>            if ((_revents &amp; EPOLLIN) || (_revents &amp; EPOLLRDHUP) || (_revents &amp; EPOLLPRI))<br \/>\n            {<br \/>\n                if (_read_callback)<br \/>\n                    _read_callback(self);<br \/>\n            }<br \/>\n            else if (_revents &amp; EPOLLOUT)<br \/>\n            {<br \/>\n                if (_write_callback)<br \/>\n                    _write_callback(self);<br \/>\n            }<br \/>\n            else if (_revents &amp; EPOLLERR)<br \/>\n            {<br \/>\n                if (_error_callback)<br \/>\n                    _error_callback(self);<br \/>\n            }<br \/>\n            else if (_revents &amp; EPOLLHUP)<br \/>\n            {<br \/>\n                if (_close_callback)<br \/>\n                    _close_callback(self);<br \/>\n            }<br \/>\n            if (_event_callback)<br \/>\n                _event_callback(self);<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        int _sockfd;<br \/>\n        Poller *_poll;<br \/>\n        uint32_t _events;<br \/>\n        uint32_t _revents;<br \/>\n        EventCallBack _read_callback;<br \/>\n        EventCallBack _write_callback;<br \/>\n        EventCallBack _error_callback;<br \/>\n        EventCallBack _close_callback;<br \/>\n        EventCallBack _event_callback;<br \/>\n    };<br \/>\n}<br \/>\n#endif<\/p>\n<h4>\u4e3a\u4ec0\u4e48 Channel \u8981\u4f7f\u7528 shared_ptr<\/h4>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5728\u5f02\u6b65\u7f51\u7edc\u5e93\u4e2d&#xff0c;Channel \u7684\u751f\u547d\u5468\u671f\u4e0d\u660e\u786e&#xff1a;\u5b83\u53ef\u80fd\u88ab Poller\u3001TcpConnection\u3001TcpServer \u7b49\u591a\u4e2a\u7ec4\u4ef6\u5171\u540c\u6301\u6709&#xff0c;\u800c\u4e14\u4e8b\u4ef6\u56de\u8c03&#xff08;\u5982\u00a0_read_callback&#xff09;\u5185\u90e8\u6709\u53ef\u80fd\u89e6\u53d1\u00a0delete this\u00a0\u6216\u8ba9\u6700\u540e\u4e00\u4e2a\u00a0shared_ptr\u00a0\u5f15\u7528\u6d88\u5931\u3002\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5982\u679c\u4f7f\u7528\u88f8\u6307\u9488&#xff0c;\u4e00\u65e6\u5728\u00a0HandleEvent\u00a0\u6267\u884c\u8fc7\u7a0b\u4e2d\u5bf9\u8c61\u88ab\u9500\u6bc1&#xff0c;\u540e\u7eed\u7684\u4ee3\u7801\u5c31\u4f1a\u8bbf\u95ee\u91ce\u6307\u9488&#xff0c;\u5bfc\u81f4\u5d29\u6e83\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0std::enable_shared_from_this\u00a0\u914d\u5408\u00a0shared_from_this()\u00a0\u53ef\u4ee5\u4fdd\u8bc1&#xff1a;\u5728\u00a0HandleEvent\u00a0\u4f5c\u7528\u57df\u5185&#xff0c;\u65e0\u8bba\u56de\u8c03\u51fd\u6570\u505a\u4e86\u4ec0\u4e48&#xff08;\u6bd4\u5982\u8c03\u7528\u00a0Remove\u00a0\u6216\u5173\u95ed\u8fde\u63a5&#xff09;&#xff0c;self\u00a0\u8fd9\u4e2a\u4e34\u65f6\u00a0shared_ptr\u00a0\u90fd\u4f1a\u5ef6\u957f\u00a0Channel\u00a0\u7684\u751f\u547d\u5468\u671f&#xff0c;\u76f4\u5230\u00a0HandleEvent\u00a0\u51fd\u6570\u8fd4\u56de\u3002\u8fd9\u662f\u5f02\u6b65\u56de\u8c03\u573a\u666f\u4e0b\u9632\u6b62 use-after-free \u7684\u6807\u51c6\u505a\u6cd5\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u53e6\u5916&#xff0c;Channel\u00a0\u5e76\u4e0d\u62e5\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5b83\u7684\u6240\u6709\u6743\u9700\u8981\u5171\u4eab&#xff08;\u6bd4\u5982 TcpConnection \u548c EventLoop \u90fd\u53ef\u80fd\u6301\u6709&#xff09;&#xff0c;shared_ptr\u00a0\u6b63\u597d\u8868\u8fbe\u8fd9\u79cd\u5171\u4eab\u6240\u6709\u6743\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u56e0\u6b64&#xff0c;\u9009\u62e9\u00a0shared_ptr\u00a0\u662f\u51fa\u4e8e\u5b89\u5168\u6027\u548c\u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\u7684\u53cc\u91cd\u8003\u8651\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u5c01\u88c5 Poller \u6a21\u5757<\/h3>\n<p>\u00a0 \u00a0 \u00a0\u00a0 \u00a0 \u4e0a\u9762\u6211\u4eec\u4ecb\u7ecd\u4e86 epoll \u7684\u4f5c\u7528\u548c\u4f18\u52bf&#xff0c;\u4e0b\u9762\u6211\u4eec\u8981\u4ecb\u7ecd\u76f8\u5173\u7684\u63a5\u53e3&#xff0c;\u5728\u5b8c\u6210\u4ee3\u7801\u7684\u7f16\u5199<\/p>\n<ul><\/ul>\n<h4>\u00a0 \u00a0 \u00a0 \u00a0 \u76f8\u5173\u63a5\u53e3<\/h4>\n<h5>\u00a0 \u00a0 \u00a0 \u00a0 1. \u521b\u5efa epoll \u5b9e\u4f8b<\/h5>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int epoll_create(int size);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0size \u53c2\u6570\u5386\u53f2\u4e0a\u662f\u7ed9\u5185\u6838\u7684\u4e00\u4e2a\u201c\u63d0\u793a\u201d&#xff0c;\u8868\u793a\u671f\u671b\u901a\u8fc7\u8fd9\u4e2a epoll \u5b9e\u4f8b\u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u3002\u5185\u6838\u80fd\u6839\u636e\u8fd9\u4e2a\u63d0\u793a\u63d0\u524d\u5206\u914d\u6070\u5f53\u5927\u5c0f\u7684\u5185\u90e8\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4f46\u5728\u00a0Linux 2.6.8 \u4e4b\u540e\u7684\u5185\u6838\u4e2d&#xff0c;\u8be5\u53c2\u6570\u5df2\u88ab\u5b8c\u5168\u5ffd\u7565&#xff0c;\u53ea\u8981\u4f20\u5165\u4e00\u4e2a\u5927\u4e8e 0\u00a0\u7684\u6574\u6570\u5373\u53ef\u3002\u5185\u6838\u4f1a\u6309\u9700\u52a8\u6001\u8c03\u6574\u5185\u5b58&#xff0c;\u4e0d\u518d\u4f9d\u8d56\u8be5\u503c\u505a\u9884\u5206\u914d\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u6210\u529f\u8fd4\u56de\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5931\u8d25\u8fd4\u56de -1 \u5e76\u8bbe\u7f6e errno\u3002<\/p>\n<h5>\u00a0 \u00a0 \u00a0 \u00a0 2.\u00a0\u7ba1\u7406\u88ab\u76d1\u63a7\u7684\u63cf\u8ff0\u7b26\u4e0e\u4e8b\u4ef6<\/h5>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0\u76f8\u5173\u53c2\u6570&#xff1a;<\/p>\n<ul>\n<li>epfd&#xff1a;\u7531 epoll_create \u8fd4\u56de\u7684 epoll \u5b9e\u4f8b\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n<li>op&#xff1a;\u64cd\u4f5c\u7c7b\u578b&#xff0c;\u53ef\u53d6\u4ee5\u4e0b\u5b8f&#xff1a;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0EPOLL_CTL_ADD&#xff1a;\u5411 epoll \u5b9e\u4f8b\u6ce8\u518c\u65b0\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26 fd&#xff0c;\u5e76\u5c06\u4e8b\u4ef6 event \u5173\u8054\u5230\u5b83\u3002<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0EPOLL_CTL_MOD&#xff1a;\u4fee\u6539\u5df2\u6ce8\u518c\u7684 fd \u5bf9\u5e94\u7684\u4e8b\u4ef6\u3002<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0EPOLL_CTL_DEL&#xff1a;\u4ece epoll \u5b9e\u4f8b\u4e2d\u79fb\u9664 fd\u3002\u6b64\u65f6 event \u53c2\u6570\u53ef\u8bbe\u4e3a NULL\u3002<\/li>\n<li>fd&#xff1a;\u5f85\u64cd\u4f5c\u7684\u76ee\u6807\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n<li>event&#xff1a;\u6307\u5411 struct epoll_event \u7684\u6307\u9488&#xff0c;\u63cf\u8ff0\u9700\u8981\u76d1\u542c\u7684\u4e8b\u4ef6\u7c7b\u578b\u53ca\u7528\u6237\u6570\u636e\u3002<\/li>\n<li>\n<p>\u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de 0&#xff0c;\u5931\u8d25\u8fd4\u56de -1 \u5e76\u8bbe\u7f6e errno&#xff08;\u5982 EBADF\u3001EEXIST\u3001EINVAL \u7b49&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>typedef union epoll_data {<\/p>\n<p>\u00a0 \u00a0 void \u00a0 \u00a0*ptr;<\/p>\n<p>\u00a0 \u00a0 int \u00a0 \u00a0 \u00a0fd;<\/p>\n<p>\u00a0 \u00a0 uint32_t u32;<\/p>\n<p>\u00a0 \u00a0 uint64_t u64;<\/p>\n<p>} epoll_data_t;<\/p>\n<\/p>\n<p>struct epoll_event {<\/p>\n<p>\u00a0 \u00a0 uint32_t \u00a0 \u00a0 events; \u00a0 \/* Epoll events *\/<\/p>\n<p>\u00a0 \u00a0 epoll_data_t data; \u00a0 \u00a0 \/* User data variable *\/<\/p>\n<p>};<\/p>\n<p>events&#xff1a;\u4f4d\u63a9\u7801&#xff0c;\u53ef\u7531\u4ee5\u4e0b\u6807\u5fd7\u7ec4\u5408&#xff1a;<\/p>\n<ul>\n<li>EPOLLIN&#xff1a;\u6570\u636e\u53ef\u8bfb&#xff08;\u5305\u62ec\u5bf9\u7aef\u6b63\u5e38\u5173\u95ed\u65f6\u7684 FIN&#xff09;\u3002<\/li>\n<li>EPOLLOUT&#xff1a;\u6570\u636e\u53ef\u5199&#xff08;\u4f8b\u5982 socket \u53d1\u9001\u7f13\u51b2\u533a\u672a\u6ee1&#xff09;\u3002<\/li>\n<li>EPOLLRDHUP&#xff1a;\u5bf9\u7aef\u5173\u95ed\u8fde\u63a5\u6216\u534a\u5173\u95ed\u5199\u7aef\u3002<\/li>\n<li>EPOLLPRI&#xff1a;\u6709\u7d27\u6025\u6570\u636e\u53ef\u8bfb&#xff08;\u5e26\u5916\u6570\u636e&#xff09;\u3002<\/li>\n<li>EPOLLERR&#xff1a;\u53d1\u751f\u9519\u8bef\u3002\u6b64\u4e8b\u4ef6\u65e0\u9700\u663e\u5f0f\u8bbe\u7f6e&#xff0c;\u5185\u6838\u4f1a\u81ea\u52a8\u76d1\u542c\u5e76\u8fd4\u56de\u3002<\/li>\n<li>EPOLLHUP&#xff1a;\u6302\u8d77\u3002\u8868\u793a\u8bfb\u5199\u901a\u9053\u88ab\u5173\u95ed&#xff0c;\u901a\u5e38\u65e0\u9700\u663e\u5f0f\u8bbe\u7f6e\u3002<\/li>\n<li>EPOLLET&#xff1a;\u4f7f\u7528\u8fb9\u7f18\u89e6\u53d1&#xff08;Edge Triggered&#xff09;\u6a21\u5f0f&#xff0c;\u9ed8\u8ba4\u662f\u6c34\u5e73\u89e6\u53d1\u3002<\/li>\n<li>EPOLLONESHOT&#xff1a;\u89e6\u53d1\u4e00\u6b21\u4e8b\u4ef6\u540e&#xff0c;\u8be5\u63cf\u8ff0\u7b26\u88ab\u81ea\u52a8\u7981\u7528&#xff0c;\u9700\u901a\u8fc7 epoll_ctl \u91cd\u65b0\u6ce8\u518c\u624d\u80fd\u518d\u6b21\u83b7\u5f97\u901a\u77e5\u3002\u7528\u4e8e\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u907f\u514d\u7ade\u6001\u3002<\/li>\n<li>EPOLLWAKEUP&#xff08;\u4e0d\u5e38\u89c1&#xff09;&#xff1a;\u786e\u4fdd\u7cfb\u7edf\u4e0d\u4f1a\u56e0\u8be5\u4e8b\u4ef6\u5904\u7406\u800c\u8fdb\u5165\u6302\u8d77\u72b6\u6001\u3002<\/li>\n<li>EPOLLEXCLUSIVE&#xff1a;\u72ec\u5360\u5524\u9192\u6a21\u5f0f&#xff0c;\u591a\u4e2a epoll \u5b9e\u4f8b\u76d1\u63a7\u540c\u4e00\u63cf\u8ff0\u7b26\u65f6\u53ef\u907f\u514d\u60ca\u7fa4\u3002<\/li>\n<\/ul>\n<h5>\u00a0 \u00a0 \u00a0 \u00a0 3.\u00a0\u7b49\u5f85 I\/O \u4e8b\u4ef6<\/h5>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int epoll_wait(int epfd, struct epoll_event *events,int maxevents, int timeout);<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u76f8\u5173\u53c2\u6570&#xff1a;<\/p>\n<ul>\n<li>epfd&#xff1a;epoll \u5b9e\u4f8b\u63cf\u8ff0\u7b26\u3002<\/li>\n<li>events&#xff1a;\u8f93\u51fa\u53c2\u6570&#xff0c;\u5185\u6838\u5c06\u5c31\u7eea\u7684\u4e8b\u4ef6\u6570\u7ec4\u590d\u5236\u5230\u8be5\u7f13\u51b2\u533a\u3002<\/li>\n<li>maxevents&#xff1a;events \u6570\u7ec4\u80fd\u5bb9\u7eb3\u7684\u6700\u5927\u4e8b\u4ef6\u6570&#xff0c;\u5fc5\u987b\u5927\u4e8e 0\u3002<\/li>\n<li>timeout&#xff1a;\u7b49\u5f85\u8d85\u65f6&#xff08;\u6beb\u79d2&#xff09;\u3002-1 \u8868\u793a\u65e0\u9650\u963b\u585e&#xff0c;0 \u8868\u793a\u7acb\u5373\u8fd4\u56de&#xff08;\u975e\u963b\u585e&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8fd4\u56de\u503c&#xff1a;<\/p>\n<ul>\n<li>\u5927\u4e8e 0&#xff1a;\u8fd4\u56de\u5c31\u7eea\u4e8b\u4ef6\u6570&#xff0c;events \u6570\u7ec4\u5df2\u586b\u5145\u5bf9\u5e94\u6570\u91cf\u7684 struct epoll_event\u3002<\/li>\n<li>0&#xff1a;\u8d85\u65f6\u65f6\u95f4\u8017\u5c3d&#xff0c;\u6ca1\u6709\u4efb\u4f55\u4e8b\u4ef6\u5c31\u7eea\u3002<\/li>\n<li>-1&#xff1a;\u51fa\u9519&#xff0c;errno \u88ab\u8bbe\u7f6e<\/li>\n<\/ul>\n<h4>\u00a0 \u00a0 \u00a0 \u00a0 \u4ecb\u7ecd\u6c34\u5e73\u89e6\u53d1\u4e0e\u8fb9\u7f18\u89e6\u53d1<\/h4>\n<p>epoll \u652f\u6301\u4e24\u79cd\u89e6\u53d1\u65b9\u5f0f&#xff0c;\u901a\u8fc7 epoll_event.events \u4e2d\u7684 EPOLLET \u6807\u5fd7\u63a7\u5236\u3002<\/p>\n<ul>\n<li>\u6c34\u5e73\u89e6\u53d1&#xff08;Level Triggered&#xff0c;LT&#xff0c;\u9ed8\u8ba4&#xff09;<\/li>\n<\/ul>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u53ea\u8981\u6587\u4ef6\u63cf\u8ff0\u7b26\u5904\u4e8e\u5c31\u7eea\u72b6\u6001&#xff0c;\u4f8b\u5982\u63a5\u6536\u7f13\u51b2\u533a\u4ecd\u6709\u6570\u636e\u672a\u8bfb\u5b8c&#xff0c;\u6bcf\u6b21\u8c03\u7528 epoll_wait \u90fd\u4f1a\u91cd\u590d\u901a\u77e5&#xff0c;\u76f4\u5230\u6570\u636e\u88ab\u8bfb\u8d70\u3002\u8fd9\u4e0e select\/poll \u884c\u4e3a\u4e00\u81f4&#xff0c;\u7f16\u7a0b\u7b80\u5355\u3001\u4e0d\u6613\u4e22\u4e8b\u4ef6\u3002<\/p>\n<ul>\n<li>\u8fb9\u7f18\u89e6\u53d1&#xff08;Edge Triggered&#xff0c;ET&#xff09;<\/li>\n<\/ul>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4ec5\u5728\u63cf\u8ff0\u7b26\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\u624d\u901a\u77e5\u4e00\u6b21&#xff0c;\u4f8b\u5982\u4ece\u672a\u5c31\u7eea\u53d8\u4e3a\u5c31\u7eea\u3002\u4e00\u65e6\u4e8b\u4ef6\u88ab\u901a\u77e5&#xff0c;\u5373\u4f7f\u63cf\u8ff0\u7b26\u4ecd\u5c31\u7eea&#xff0c;\u53ea\u8981\u72b6\u6001\u6ca1\u6709\u518d\u53d8\u5316\u5c31\u4e0d\u4f1a\u518d\u6536\u5230\u901a\u77e5\u3002\u8fd9\u8981\u6c42&#xff1a;<\/p>\n<ul>\n<li>\u6587\u4ef6\u63cf\u8ff0\u7b26\u5fc5\u987b\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e&#xff08;O_NONBLOCK&#xff09;&#xff0c;\u5426\u5219\u8bfb\u5199\u53ef\u80fd\u963b\u585e\u5bfc\u81f4\u6574\u4e2a\u670d\u52a1\u505c\u6ede\u3002<\/li>\n<li>\u6bcf\u6b21\u89e6\u53d1\u540e\u5fc5\u987b\u5faa\u73af\u8bfb\u5199&#xff0c;\u76f4\u5230\u8fd4\u56de EAGAIN&#xff0c;\u786e\u4fdd\u5c06\u7f13\u51b2\u533a\u6570\u636e\u5f7b\u5e95\u5904\u7406\u5e72\u51c0&#xff0c;\u5426\u5219\u4e0d\u4f1a\u518d\u83b7\u5f97\u65b0\u901a\u77e5&#xff0c;\u53ef\u80fd\u9020\u6210\u4e8b\u4ef6\u4e22\u5931\u4e0e\u6c38\u4e45\u9965\u997f\u3002<\/li>\n<li>\u7f16\u7a0b\u590d\u6742\u5ea6\u66f4\u9ad8&#xff0c;\u4f46\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u53ef\u51cf\u5c11\u7cfb\u7edf\u8c03\u7528\u6b21\u6570\u548c\u91cd\u590d\u901a\u77e5\u5f00\u9500\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u6211\u4eec\u5c06\u4f1a\u5c06\u4e0a\u9762\u7684\u63a5\u53e3\u8fdb\u884c\u5c01\u88c5&#xff1a;<\/p>\n<h4>\u76f8\u5173\u6210\u5458&#xff1a;<\/h4>\n<p>1. \u5fc5\u987b\u62e5\u6709\u4e00\u4e2a epoll \u7684\u64cd\u4f5c\u53e5\u67c4 \u00a0<\/p>\n<p>2. \u62e5\u6709\u4e00\u4e2a struct epoll_event \u7ed3\u6784\u6570\u7ec4&#xff0c;\u76d1\u63a7\u65f6\u4fdd\u5b58\u6240\u6709\u7684\u6d3b\u8dc3\u4e8b\u4ef6 \u00a0<\/p>\n<p>3. \u4f7f\u7528 hash \u8868\u7ba1\u7406\u63cf\u8ff0\u7b26\u4e0e\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u4e8b\u4ef6\u7ba1\u7406 Channel \u5bf9\u8c61 \u00a0<\/p>\n<h4>\u76f8\u5173\u63a5\u53e3&#xff1a;<\/h4>\n<p>1. \u6dfb\u52a0 \/ \u4fee\u6539\u63cf\u8ff0\u7684\u4e8b\u4ef6\u76d1\u63a7&#xff08;\u4e0d\u5b58\u5728\u5219\u6dfb\u52a0&#xff0c;\u5b58\u5728\u5c31\u4fee\u6539&#xff09; \u00a0<\/p>\n<p>2. \u79fb\u9664\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7<\/p>\n<h4>\u00a0 \u00a0 \u00a0 \u00a0 \u4ee3\u7801\u7f16\u5199&#xff1a;<\/h4>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4e3a\u4e86\u8ba9 Poller \u6a21\u5757\u4f7f\u7528 Channel \u7684\u76f8\u5173\u5185\u5bb9&#xff0c;\u6211\u4eec\u9700\u8981\u5bf9\u4e0a\u9762 Channel \u7684\u5185\u5bb9\u8fdb\u884c\u8865\u5145&#xff1a;<\/p>\n<p>#ifndef __SOCKET__<br \/>\n#define __SOCKET__<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &#034;log.hpp&#034;<br \/>\nnamespace xxhh<br \/>\n{<br \/>\n#define DEFAULT_SOCKET_FD -1<br \/>\n#define MAX_LISTEN 1024<br \/>\n    class Socket<br \/>\n    {<br \/>\n    public:<br \/>\n        Socket() : _sockfd(DEFAULT_SOCKET_FD) {}<br \/>\n        ~Socket() { Close(); };<br \/>\n        Socket(int sockfd) : _sockfd(sockfd) {}<br \/>\n        int Fd() { return _sockfd; }<br \/>\n        \/\/ \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n        bool CreateSocket()<br \/>\n        {<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            {<br \/>\n                ERR_LOG(&#034;Create Socket Failed!&#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        {<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, const struct sockaddr *addr,socklen_t addrlen);<br \/>\n            int ret &#061; bind(_sockfd, (struct sockaddr *)&amp;addr, len);<br \/>\n            if (ret &lt; 0)<br \/>\n            {<br \/>\n                ERR_LOG(&#034;Bind Address Failed!,errno: %d (%s)&#034;, errno, strerror(errno));<br \/>\n                return false;<br \/>\n            }<br \/>\n            return true;<br \/>\n        }<br \/>\n        \/\/ \u5f00\u59cb\u76d1\u542c<br \/>\n        bool Listen(int backlog &#061; MAX_LISTEN)<br \/>\n        {<br \/>\n            \/\/  int listen(int sockfd, int backlog);<br \/>\n            int ret &#061; listen(_sockfd, backlog);<br \/>\n            if (ret &lt; 0)<br \/>\n            {<br \/>\n                ERR_LOG(&#034;Socket Listen fail&#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        {<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, const struct sockaddr *addr,socklen_t addrlen);<br \/>\n            int ret &#061; connect(_sockfd, (struct sockaddr *)&amp;addr, len);<br \/>\n            if (ret &lt; 0)<br \/>\n            {<br \/>\n                ERR_LOG(&#034;Connect Server Failed!&#034;);<br \/>\n                return false;<br \/>\n            }<br \/>\n            return true;<br \/>\n        }<br \/>\n        \/\/ \u63a5\u53d7\u65b0\u94fe\u63a5<br \/>\n        int Accept()<br \/>\n        {<br \/>\n            int ret &#061; accept(_sockfd, nullptr, nullptr);<br \/>\n            if (ret &lt; 0)<br \/>\n            {<br \/>\n                ERR_LOG(&#034;Socket Accept Failed!&#034;);<br \/>\n                return -1;<br \/>\n            }<br \/>\n            return ret;<br \/>\n        }<br \/>\n        \/\/ \u63a5\u6536\u6570\u636e<br \/>\n        ssize_t Recv(void *buf, size_t len, int flag &#061; 0)<br \/>\n        {<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;&#061; 0)<br \/>\n            {<br \/>\n                \/\/ EAGAIN: \u8868\u660e\u5f53\u524d socket \u7684\u63a5\u6536\u7f13\u51b2\u533a\u4e2d\u6ca1\u6709\u6570\u636e&#xff0c;\u5728\u975e\u963b\u585e\u7684\u60c5\u51b5\u4e0b\u624d\u4f1a\u51fa\u73b0\u8fd9\u4e2a\u9519\u8bef<br \/>\n                \/\/ EINTR: \u8868\u793a\u5f53\u524d socket \u7684\u963b\u585e\u7b49\u5f85&#xff0c;\u88ab\u4fe1\u53f7\u6253\u65ad\u4e86<br \/>\n                if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EINTR)<br \/>\n                {<br \/>\n                    return 0;<br \/>\n                }<br \/>\n                ERR_LOG(&#034;Socket Recv Failed!&#034;);<br \/>\n                return -1;<br \/>\n            }<br \/>\n            return ret;<br \/>\n        }<br \/>\n        ssize_t NonBlockRecv(void *buf, size_t len)<br \/>\n        {<br \/>\n            return Recv(buf, len, MSG_DONTWAIT);<br \/>\n        }<br \/>\n        \/\/ \u53d1\u9001\u6570\u636e<br \/>\n        ssize_t Send(const void *buf, size_t len, int flag &#061; 0)<br \/>\n        {<br \/>\n            \/\/  ssize_t send(int sockfd, const void *buf, size_t len, int flags);<br \/>\n            int ret &#061; send(_sockfd, buf, len, flag);<br \/>\n            if (ret &lt;&#061; 0)<br \/>\n            {<br \/>\n                \/\/ EAGAIN: \u8868\u660e\u5f53\u524d socket \u7684\u63a5\u6536\u7f13\u51b2\u533a\u4e2d\u6ca1\u6709\u6570\u636e&#xff0c;\u5728\u975e\u963b\u585e\u7684\u60c5\u51b5\u4e0b\u624d\u4f1a\u51fa\u73b0\u8fd9\u4e2a\u9519\u8bef<br \/>\n                \/\/ EINTR: \u8868\u793a\u5f53\u524d socket \u7684\u963b\u585e\u7b49\u5f85&#xff0c;\u88ab\u4fe1\u53f7\u6253\u65ad\u4e86<br \/>\n                if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EINTR)<br \/>\n                {<br \/>\n                    return 0;<br \/>\n                }<br \/>\n                ERR_LOG(&#034;Socket Send Failed!&#034;);<br \/>\n                return -1;<br \/>\n            }<br \/>\n            return ret; \/\/ \u5b9e\u9645\u53d1\u9001\u7684\u6570\u636e\u957f\u5ea6<br \/>\n        }<br \/>\n        ssize_t NonBlockSend(void *buf, size_t len)<br \/>\n        {<br \/>\n            return Send(buf, len, MSG_DONTWAIT);<br \/>\n        }<\/p>\n<p>        \/\/ \u5173\u95ed\u5957\u63a5\u5b57<br \/>\n        void Close()<br \/>\n        {<br \/>\n            if (_sockfd !&#061; -1)<br \/>\n            {<br \/>\n                close(_sockfd);<br \/>\n                _sockfd &#061; -1;<br \/>\n            }<br \/>\n        }<br \/>\n        \/\/ \u8bbe\u7f6e\u5957\u63a5\u5b57\u963b\u585e\u5c5e\u6027 &#8212; \u975e\u963b\u585e<br \/>\n        void NonBlock()<br \/>\n        {<br \/>\n            \/\/ int fcntl(int fd, int cmd, &#8230; \/* arg *\/ );<br \/>\n            int flag &#061; fcntl(_sockfd, F_GETFL, 0); \/\/ FL -&gt; \u6587\u4ef6\u72b6\u6001\u6807\u5fd7<br \/>\n            fcntl(_sockfd, F_SETFL, flag | O_NONBLOCK);<br \/>\n        }<\/p>\n<p>        \/\/ \u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u5668\u8fde\u63a5<br \/>\n        bool CreateServer(uint16_t port, const std::string &amp;ip &#061; &#034;0.0.0.0&#034;, bool block_flag &#061; false)<br \/>\n        {<br \/>\n            \/\/ 1. \u8bbe\u7f6e\u5957\u63a5\u5b57 2. \u542f\u52a8\u5730\u5740\u590d\u7528 3. \u8bbe\u7f6e\u975e\u963b\u585e  4. \u7ed1\u5b9a\u5730\u5740 5. \u5f00\u59cb\u76d1\u542c<br \/>\n            if (CreateSocket() &#061;&#061; false)<br \/>\n                return false;<br \/>\n            ReuseAddress();<br \/>\n            if (block_flag)<br \/>\n                NonBlock();<br \/>\n            if (Bind(ip, port) &#061;&#061; false)<br \/>\n                return false;<br \/>\n            if (Listen() &#061;&#061; false)<br \/>\n                return false;<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        {<br \/>\n            \/\/ 1. \u521b\u5efa\u5957\u63a5\u5b57  2. \u6267\u884c\u8fde\u63a5\u670d\u52a1\u5668<br \/>\n            if (CreateSocket() &#061;&#061; false)<br \/>\n                return false;<br \/>\n            if (Connect(ip, port) &#061;&#061; false)<br \/>\n                return false;<br \/>\n            return true;<br \/>\n        }<br \/>\n        \/\/ \u8bbe\u7f6e\u5957\u63a5\u5b57\u9009\u9879 &#8212; \u5f00\u542f\u5730\u5740\u590d\u7528<br \/>\n        \/\/ SO_REUSEADDR: \u89e3\u51b3\u5730\u5740\u7aef\u53e3\u91cd\u7528<br \/>\n        \/\/ SO_REUSEPORT: \u5141\u8bb8\u591a\u4e2a\u5957\u63a5\u5b57\u5b8c\u5168\u7ed1\u5b9a\u5230\u540c\u4e00\u4e2a\u7aef\u53e3&#xff0c;\u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861<br \/>\n        void ReuseAddress()<br \/>\n        {<br \/>\n            \/\/ \u8bbe\u7f6e\u5957\u63a5\u5b57&#xff08;socket&#xff09;\u7684\u9009\u9879<br \/>\n            \/\/  int setsockopt(int sockfd, int level, int optname,const void *optval, socklen_t optlen);<br \/>\n            \/\/ level&#xff1a;\u9009\u9879\u5b9a\u4e49\u7684\u534f\u8bae\u5c42  optname&#xff1a;\u5177\u4f53\u9009\u9879\u540d<br \/>\n            \/\/ SOL_SOCKET&#xff08;Socket Level&#xff09;\u8868\u793a\u9009\u9879\u5c5e\u4e8e \u5957\u63a5\u5b57\u5c42&#xff08;\u901a\u7528 socket \u5c42&#xff09;<br \/>\n            \/\/ level &#061; IPPROTO_TCP&#xff0c;\u5219\u53ea\u80fd\u4f7f\u7528 TCP_* \u5f00\u5934\u7684\u9009\u9879&#xff08;\u5982 TCP_NODELAY&#xff09;&#xff1b;<br \/>\n            \/\/ \u82e5 level &#061; IPPROTO_IP&#xff0c;\u5219\u4f7f\u7528 IP_* \u9009\u9879&#xff08;\u5982 IP_TTL&#xff09;\u3002<br \/>\n            int val &#061; 1;<br \/>\n            setsockopt(_sockfd, SOL_SOCKET, SO_REUSEADDR, (void *)&amp;val, sizeof(int));<br \/>\n            val &#061; 1;<br \/>\n            setsockopt(_sockfd, SOL_SOCKET, SO_REUSEPORT, (void *)&amp;val, sizeof(int));<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        int _sockfd;<br \/>\n    };<\/p>\n<p>}<\/p>\n<p>#endif<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 Channel&#xff1a;<\/p>\n<p>#ifndef __CHANNEL__<br \/>\n#define __CHANNEL__<\/p>\n<p>#include &lt;functional&gt;<br \/>\n#include &lt;cstdint&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<\/p>\n<p>namespace xxhh<br \/>\n{<br \/>\n    class Poller;<\/p>\n<p>    class Channel : public std::enable_shared_from_this&lt;Channel&gt;<br \/>\n    {<br \/>\n    public:<br \/>\n        using EventCallBack &#061; std::function&lt;void(std::shared_ptr&lt;Channel&gt;)&gt;;<\/p>\n<p>        \/\/ \u6784\u9020\u51fd\u6570&#xff1a;\u9700\u8981 Poller \u6307\u9488&#xff0c;\u7528\u4e8e\u540e\u7eed Update\/Remove<br \/>\n        Channel(Poller *poller, int fd)<br \/>\n            : _sockfd(fd), _poll(poller), _events(0), _revents(0) {}<\/p>\n<p>        int Fd() const { return _sockfd; }<br \/>\n        uint32_t Events() const { return _events; }<br \/>\n        void SetREvents(uint32_t events) { _revents &#061; events; }<\/p>\n<p>        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 SetEventCallBack(const EventCallBack &amp;cb) { _event_callback &#061; cb; }<\/p>\n<p>        bool ReadAble() const { return (_events &amp; EPOLLIN); }<br \/>\n        bool WriteAble() const { return (_events &amp; EPOLLOUT); }<\/p>\n<p>        void EnableRead()<br \/>\n        {<br \/>\n            _events |&#061; EPOLLIN;<br \/>\n            Update();<br \/>\n        }<br \/>\n        void EnableWrite()<br \/>\n        {<br \/>\n            _events |&#061; EPOLLOUT;<br \/>\n            Update();<br \/>\n        }<br \/>\n        void DisableRead()<br \/>\n        {<br \/>\n            _events &amp;&#061; ~EPOLLIN;<br \/>\n            Update();<br \/>\n        }<br \/>\n        void DisableWrite()<br \/>\n        {<br \/>\n            _events &amp;&#061; ~EPOLLOUT;<br \/>\n            Update();<br \/>\n        }<\/p>\n<p>        void Remove(); \/\/ \u5b9e\u73b0\u89c1 poller.hpp \u5c3e\u90e8<br \/>\n        void Update(); \/\/ \u5b9e\u73b0\u89c1 poller.hpp \u5c3e\u90e8<\/p>\n<p>        void HandleEvent()<br \/>\n        {<br \/>\n            auto self &#061; shared_from_this(); \/\/ \u4fdd\u8bc1\u5728 HandleEvent \u5168\u7a0b\u5bf9\u8c61\u4e0d\u9500\u6bc1<\/p>\n<p>            if ((_revents &amp; EPOLLIN) || (_revents &amp; EPOLLRDHUP) || (_revents &amp; EPOLLPRI))<br \/>\n            {<br \/>\n                if (_read_callback)<br \/>\n                    _read_callback(self);<br \/>\n            }<br \/>\n            else if (_revents &amp; EPOLLOUT)<br \/>\n            {<br \/>\n                if (_write_callback)<br \/>\n                    _write_callback(self);<br \/>\n            }<br \/>\n            else if (_revents &amp; EPOLLERR)<br \/>\n            {<br \/>\n                if (_error_callback)<br \/>\n                    _error_callback(self);<br \/>\n            }<br \/>\n            else if (_revents &amp; EPOLLHUP)<br \/>\n            {<br \/>\n                if (_close_callback)<br \/>\n                    _close_callback(self);<br \/>\n            }<br \/>\n            if (_event_callback)<br \/>\n                _event_callback(self);<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        int _sockfd;<br \/>\n        Poller *_poll;<br \/>\n        uint32_t _events;<br \/>\n        uint32_t _revents;<br \/>\n        EventCallBack _read_callback;<br \/>\n        EventCallBack _write_callback;<br \/>\n        EventCallBack _error_callback;<br \/>\n        EventCallBack _close_callback;<br \/>\n        EventCallBack _event_callback;<br \/>\n    };<br \/>\n}<br \/>\n#endif<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 Poller&#xff1a;<\/p>\n<p>#ifndef __POLLER__<br \/>\n#define __POLLER__<\/p>\n<p>#include &lt;unordered_map&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cassert&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &#034;log.hpp&#034;<br \/>\n#include &#034;channel.hpp&#034;<\/p>\n<p>namespace xxhh<br \/>\n{<br \/>\n#define MAX_EPOLLEVENTS 1024<\/p>\n<p>    class Poller<br \/>\n    {<br \/>\n    private:<br \/>\n        void Update(std::shared_ptr&lt;Channel&gt; channel, int op)<br \/>\n        {<br \/>\n            int fd &#061; channel-&gt;Fd();<br \/>\n            struct epoll_event ev;<br \/>\n            ev.events &#061; channel-&gt;Events();<br \/>\n            ev.data.fd &#061; fd;<br \/>\n            int ret &#061; epoll_ctl(_epfd, op, fd, &amp;ev);<br \/>\n            if (ret &lt; 0)<br \/>\n            {<br \/>\n                switch (op)<br \/>\n                {<br \/>\n                case EPOLL_CTL_ADD:<br \/>\n                    ERR_LOG(&#034;EPOLL_CTL_ADD Failed!&#034;);<br \/>\n                    break;<br \/>\n                case EPOLL_CTL_DEL:<br \/>\n                    ERR_LOG(&#034;EPOLL_CTL_DEL Failed!&#034;);<br \/>\n                    break;<br \/>\n                case EPOLL_CTL_MOD:<br \/>\n                    ERR_LOG(&#034;EPOLL_CTL_MOD Failed!&#034;);<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n        }<\/p>\n<p>        bool IsExitChannel(int fd) const<br \/>\n        {<br \/>\n            return _channels.find(fd) !&#061; _channels.end();<br \/>\n        }<\/p>\n<p>    public:<br \/>\n        Poller()<br \/>\n        {<br \/>\n            _epfd &#061; epoll_create(MAX_EPOLLEVENTS);<br \/>\n            if (_epfd &lt; 0)<br \/>\n            {<br \/>\n                ERR_LOG(&#034;Poller Create Error!&#034;);<br \/>\n                abort();<br \/>\n            }<br \/>\n        }<\/p>\n<p>        ~Poller() { close(_epfd); }<\/p>\n<p>        void UpdateEvent(std::shared_ptr&lt;Channel&gt; channel)<br \/>\n        {<br \/>\n            int fd &#061; channel-&gt;Fd();<br \/>\n            if (!IsExitChannel(fd))<br \/>\n            {<br \/>\n                _channels[fd] &#061; channel;<br \/>\n                Update(channel, EPOLL_CTL_ADD);<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                Update(channel, EPOLL_CTL_MOD);<br \/>\n            }<br \/>\n        }<\/p>\n<p>        void RemoveEvent(std::shared_ptr&lt;Channel&gt; channel)<br \/>\n        {<br \/>\n            int fd &#061; channel-&gt;Fd();<br \/>\n            auto it &#061; _channels.find(fd);<br \/>\n            if (it !&#061; _channels.end())<br \/>\n            {<br \/>\n                _channels.erase(it);<br \/>\n            }<br \/>\n            Update(channel, EPOLL_CTL_DEL);<br \/>\n        }<\/p>\n<p>        void Poll(std::vector&lt;std::shared_ptr&lt;Channel&gt;&gt; *active)<br \/>\n        {<br \/>\n            int nfds &#061; epoll_wait(_epfd, _evs, MAX_EPOLLEVENTS, -1);<br \/>\n            if (nfds &lt; 0)<br \/>\n            {<br \/>\n                if (errno &#061;&#061; EINTR)<br \/>\n                    return;<br \/>\n                ERR_LOG(&#034;Epoll Wait Error: %s\\\\n&#034;, strerror(errno));<br \/>\n                abort();<br \/>\n            }<br \/>\n            for (int i &#061; 0; i &lt; nfds; &#043;&#043;i)<br \/>\n            {<br \/>\n                int fd &#061; _evs[i].data.fd;<br \/>\n                auto it &#061; _channels.find(fd);<br \/>\n                assert(it !&#061; _channels.end());<br \/>\n                it-&gt;second-&gt;SetREvents(_evs[i].events);<br \/>\n                active-&gt;push_back(it-&gt;second);<br \/>\n            }<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        int _epfd;<br \/>\n        struct epoll_event _evs[MAX_EPOLLEVENTS];<br \/>\n        std::unordered_map&lt;int, std::shared_ptr&lt;Channel&gt;&gt; _channels;<br \/>\n    };<br \/>\n}<\/p>\n<p>void xxhh::Channel::Remove() { _poll-&gt;RemoveEvent(shared_from_this()); }<br \/>\nvoid xxhh::Channel::Update() { _poll-&gt;UpdateEvent(shared_from_this()); }<\/p>\n<p>#endif<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u7f16\u5199\u6d4b\u8bd5\u4ee3\u7801<\/h3>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4e0b\u9762\u7684\u6d4b\u8bd5\u4ee3\u7801&#xff0c;\u5176\u5b9e\u5c31\u662f\u6211\u4eec Reactor \u7684\u96cf\u5f62\u4e86&#xff0c;\u5927\u5bb6\u9700\u8981\u4ed4\u7ec6\u53bb\u770b&#xff0c;\u91cc\u9762\u7684\u4e00\u4e9b\u5185\u5bb9&#xff0c;\u6211\u4eec\u4f1a\u5728\u540e\u9762\u8fdb\u884c\u5c01\u88c5\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7406\u8bba\u8bb2\u5b8c&#xff0c;\u4ee3\u7801\u5199\u5b8c&#xff0c;\u73b0\u5728\u6211\u4eec\u6765\u9a8c\u8bc1\u4e00\u4e0b Channel &#043; Poller \u662f\u5426\u771f\u7684\u80fd\u5de5\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684 echo \u670d\u52a1\u5668 \u6d4b\u8bd5\u7a0b\u5e8f&#xff0c;\u5b83\u4f7f\u7528\u4e86\u6211\u4eec\u521a\u521a\u5c01\u88c5\u7684 Socket\u3001Poller \u548c Channel\u3002<\/p>\n<h4>\u670d\u52a1\u7aef\u4ee3\u7801(server.cc)<\/h4>\n<p>#include &#034;..\/source\/socket.hpp&#034;<br \/>\n#include &#034;..\/source\/poller.hpp&#034;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;iostream&gt;<\/p>\n<p>using namespace xxhh;<\/p>\n<p>void HandleClose(std::shared_ptr&lt;Channel&gt; channel)<br \/>\n{<br \/>\n    std::cout &lt;&lt; &#034;close: &#034; &lt;&lt; channel-&gt;Fd() &lt;&lt; std::endl;<br \/>\n    channel-&gt;Remove(); \/\/ \u4ec5\u4ece epoll \u79fb\u9664&#xff0c;\u4e0d delete<br \/>\n    \/\/ channel \u7684 shared_ptr \u4f1a\u81ea\u52a8\u91ca\u653e&#xff0c;\u65e0\u9700\u989d\u5916\u64cd\u4f5c<br \/>\n}<\/p>\n<p>void HandleRead(std::shared_ptr&lt;Channel&gt; channel)<br \/>\n{<br \/>\n    int fd &#061; channel-&gt;Fd();<br \/>\n    char buf[1024] &#061; {0};<br \/>\n    int ret &#061; recv(fd, buf, sizeof(buf) &#8211; 1, 0);<br \/>\n    if (ret &lt;&#061; 0)<br \/>\n    {<br \/>\n        HandleClose(channel);<br \/>\n        return;<br \/>\n    }<br \/>\n    std::cout &lt;&lt; &#034;recv: &#034; &lt;&lt; buf &lt;&lt; std::endl;<br \/>\n    channel-&gt;EnableWrite(); \/\/ \u542f\u52a8\u53ef\u5199\u4e8b\u4ef6<br \/>\n}<\/p>\n<p>void HandleWrite(std::shared_ptr&lt;Channel&gt; channel)<br \/>\n{<br \/>\n    int fd &#061; channel-&gt;Fd();<br \/>\n    const char *msg &#061; &#034;\u5929\u6c14\u597d&#034;;<br \/>\n    int ret &#061; send(fd, msg, strlen(msg), 0);<br \/>\n    if (ret &lt; 0)<br \/>\n    {<br \/>\n        HandleClose(channel);<br \/>\n        return;<br \/>\n    }<br \/>\n    channel-&gt;DisableWrite(); \/\/ \u5199\u5b8c\u5173\u95ed\u5199\u76d1\u63a7<br \/>\n}<\/p>\n<p>void HandleError(std::shared_ptr&lt;Channel&gt; channel)<br \/>\n{<br \/>\n    HandleClose(channel);<br \/>\n}<\/p>\n<p>void HandleEvent(std::shared_ptr&lt;Channel&gt; channel)<br \/>\n{<br \/>\n    std::cout &lt;&lt; &#034;\u6709\u4e86\u4e00\u4e2a\u4e8b\u60c5&#034; &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>void Acceptor(Poller *poll, std::shared_ptr&lt;Channel&gt; listen_channel)<br \/>\n{<br \/>\n    int fd &#061; listen_channel-&gt;Fd();<br \/>\n    int newFd &#061; accept(fd, nullptr, nullptr);<br \/>\n    if (newFd &lt; 0)<br \/>\n        return;<\/p>\n<p>    auto channel &#061; std::make_shared&lt;Channel&gt;(poll, newFd);<br \/>\n    channel-&gt;SetReadCallBack(HandleRead);<br \/>\n    channel-&gt;SetWriteCallBack(HandleWrite);<br \/>\n    channel-&gt;SetCloseCallBack(HandleClose);<br \/>\n    channel-&gt;SetErrorCallBack(HandleError);<br \/>\n    channel-&gt;SetEventCallBack(HandleEvent);<br \/>\n    channel-&gt;EnableRead(); \/\/ \u5f00\u59cb\u76d1\u63a7\u65b0\u8fde\u63a5\u7684\u8bfb\u4e8b\u4ef6<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    Socket tcpSocket;<br \/>\n    tcpSocket.CreateServer(8080);<\/p>\n<p>    Poller poll;<br \/>\n    auto listen_channel &#061; std::make_shared&lt;Channel&gt;(&amp;poll, tcpSocket.Fd());<br \/>\n    listen_channel-&gt;SetReadCallBack(std::bind(Acceptor, &amp;poll, listen_channel));<br \/>\n    listen_channel-&gt;EnableRead();<\/p>\n<p>    while (1)<br \/>\n    {<br \/>\n        std::vector&lt;std::shared_ptr&lt;Channel&gt;&gt; active;<br \/>\n        poll.Poll(&amp;active);<br \/>\n        for (auto &amp;ch : active)<br \/>\n        {<br \/>\n            ch-&gt;HandleEvent();<br \/>\n        }<br \/>\n    }<br \/>\n    tcpSocket.Close();<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u5ba2\u6237\u7aef\u4ee3\u7801(client.cc)<\/h4>\n<p>#include &#034;..\/source\/socket.hpp&#034;<br \/>\nusing namespace xxhh;<br \/>\nint main()<br \/>\n{<br \/>\n    Socket clientSock;<br \/>\n    clientSock.CreateClient(8080, &#034;127.0.0.1&#034;);<br \/>\n    while (1)<br \/>\n    {<br \/>\n        std::string str &#061; &#034;Hello world&#034;;<br \/>\n        clientSock.Send((void *)str.c_str(), str.size());<br \/>\n        char buf[1024] &#061; {0};<br \/>\n        clientSock.Recv(buf, 1023);<br \/>\n        DBG_LOG(&#034;%s&#034;, buf);<br \/>\n        sleep(1);<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ece\u96f6\u624b\u5199\u9ad8\u6027\u80fd C TCP \u670d\u52a1\u5668\u6846\u67b6&#xff08;\u4e03&#xff09;&#xff1a;Channel\u6a21\u5757\u548cPoller\u6a21\u5757\u5b9e\u73b0\u4e00\u3001\u524d\u7f6e\u77e5\u8bc6\u4ecb\u7ecd\u00a0epoll\u00a0epoll \u662f Linux \u5185\u6838\u63d0\u4f9b\u7684\u4e00\u79cd\u53ef\u6269\u5c55\u7684 I\/O \u4e8b\u4ef6\u901a\u77e5\u673a\u5236&#xff0c;\u4e13\u95e8\u7528\u4e8e\u9ad8\u6548\u5730\u76d1\u63a7\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5982 socket\u3001\u7ba1\u9053\u3001\u7ec8\u7aef\u7b49&#xff09;&#xff0c;\u7b49\u5f85\u5176\u4e2d\u7684\u67d0\u4e9b\u53d8\u4e3a\u53ef\u8bfb\u3001\u53ef\u5199\u6216\u53d1\u751f\u5f02\u5e38\u3002\u5b83\u662f\u4f20\u7edf select \u548c poll \u7684\u66ff\u4ee3\u65b9\u6848&#xff0c;\u8bbe\u8ba1\u4e0a\u89e3\u51b3\u4e86\u540e\u4e24\u8005\u5728<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,205,43],"topic":[],"class_list":["post-85705","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-linux","tag-tcp-ip","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4ece\u96f6\u624b\u5199\u9ad8\u6027\u80fd C++ TCP \u670d\u52a1\u5668\u6846\u67b6\uff08\u4e03\uff09\uff1aChannel\u6a21\u5757\u548cPoller\u6a21\u5757\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/85705.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece\u96f6\u624b\u5199\u9ad8\u6027\u80fd C++ TCP \u670d\u52a1\u5668\u6846\u67b6\uff08\u4e03\uff09\uff1aChannel\u6a21\u5757\u548cPoller\u6a21\u5757\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4ece\u96f6\u624b\u5199\u9ad8\u6027\u80fd C TCP \u670d\u52a1\u5668\u6846\u67b6&#xff08;\u4e03&#xff09;&#xff1a;Channel\u6a21\u5757\u548cPoller\u6a21\u5757\u5b9e\u73b0\u4e00\u3001\u524d\u7f6e\u77e5\u8bc6\u4ecb\u7ecd\u00a0epoll\u00a0epoll \u662f Linux \u5185\u6838\u63d0\u4f9b\u7684\u4e00\u79cd\u53ef\u6269\u5c55\u7684 I\/O \u4e8b\u4ef6\u901a\u77e5\u673a\u5236&#xff0c;\u4e13\u95e8\u7528\u4e8e\u9ad8\u6548\u5730\u76d1\u63a7\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5982 socket\u3001\u7ba1\u9053\u3001\u7ec8\u7aef\u7b49&#xff09;&#xff0c;\u7b49\u5f85\u5176\u4e2d\u7684\u67d0\u4e9b\u53d8\u4e3a\u53ef\u8bfb\u3001\u53ef\u5199\u6216\u53d1\u751f\u5f02\u5e38\u3002\u5b83\u662f\u4f20\u7edf select \u548c poll \u7684\u66ff\u4ee3\u65b9\u6848&#xff0c;\u8bbe\u8ba1\u4e0a\u89e3\u51b3\u4e86\u540e\u4e24\u8005\u5728\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/85705.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-27T13:24:26+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/85705.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/85705.html\",\"name\":\"\u4ece\u96f6\u624b\u5199\u9ad8\u6027\u80fd C++ TCP \u670d\u52a1\u5668\u6846\u67b6\uff08\u4e03\uff09\uff1aChannel\u6a21\u5757\u548cPoller\u6a21\u5757\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-27T13:24:26+00:00\",\"dateModified\":\"2026-07-27T13:24:26+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/85705.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/85705.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/85705.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece\u96f6\u624b\u5199\u9ad8\u6027\u80fd C++ TCP \u670d\u52a1\u5668\u6846\u67b6\uff08\u4e03\uff09\uff1aChannel\u6a21\u5757\u548cPoller\u6a21\u5757\u5b9e\u73b0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4ece\u96f6\u624b\u5199\u9ad8\u6027\u80fd C++ TCP \u670d\u52a1\u5668\u6846\u67b6\uff08\u4e03\uff09\uff1aChannel\u6a21\u5757\u548cPoller\u6a21\u5757\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/85705.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece\u96f6\u624b\u5199\u9ad8\u6027\u80fd C++ TCP \u670d\u52a1\u5668\u6846\u67b6\uff08\u4e03\uff09\uff1aChannel\u6a21\u5757\u548cPoller\u6a21\u5757\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4ece\u96f6\u624b\u5199\u9ad8\u6027\u80fd C TCP \u670d\u52a1\u5668\u6846\u67b6&#xff08;\u4e03&#xff09;&#xff1a;Channel\u6a21\u5757\u548cPoller\u6a21\u5757\u5b9e\u73b0\u4e00\u3001\u524d\u7f6e\u77e5\u8bc6\u4ecb\u7ecd\u00a0epoll\u00a0epoll \u662f Linux \u5185\u6838\u63d0\u4f9b\u7684\u4e00\u79cd\u53ef\u6269\u5c55\u7684 I\/O \u4e8b\u4ef6\u901a\u77e5\u673a\u5236&#xff0c;\u4e13\u95e8\u7528\u4e8e\u9ad8\u6548\u5730\u76d1\u63a7\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5982 socket\u3001\u7ba1\u9053\u3001\u7ec8\u7aef\u7b49&#xff09;&#xff0c;\u7b49\u5f85\u5176\u4e2d\u7684\u67d0\u4e9b\u53d8\u4e3a\u53ef\u8bfb\u3001\u53ef\u5199\u6216\u53d1\u751f\u5f02\u5e38\u3002\u5b83\u662f\u4f20\u7edf select \u548c poll \u7684\u66ff\u4ee3\u65b9\u6848&#xff0c;\u8bbe\u8ba1\u4e0a\u89e3\u51b3\u4e86\u540e\u4e24\u8005\u5728","og_url":"https:\/\/www.wsisp.com\/helps\/85705.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-27T13:24:26+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"13 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/85705.html","url":"https:\/\/www.wsisp.com\/helps\/85705.html","name":"\u4ece\u96f6\u624b\u5199\u9ad8\u6027\u80fd C++ TCP \u670d\u52a1\u5668\u6846\u67b6\uff08\u4e03\uff09\uff1aChannel\u6a21\u5757\u548cPoller\u6a21\u5757\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-27T13:24:26+00:00","dateModified":"2026-07-27T13:24:26+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/85705.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/85705.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/85705.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece\u96f6\u624b\u5199\u9ad8\u6027\u80fd C++ TCP \u670d\u52a1\u5668\u6846\u67b6\uff08\u4e03\uff09\uff1aChannel\u6a21\u5757\u548cPoller\u6a21\u5757\u5b9e\u73b0"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/85705","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=85705"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/85705\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=85705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=85705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=85705"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=85705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}