{"id":15315,"date":"2025-04-18T21:52:58","date_gmt":"2025-04-18T13:52:58","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/15315.html"},"modified":"2025-04-18T21:52:58","modified_gmt":"2025-04-18T13:52:58","slug":"%ef%bc%9c%e9%a1%b9%e7%9b%ae%ef%bc%9e-%e4%b8%bb%e4%bb%8ereactor%e6%a8%a1%e5%9e%8b%e7%9a%84%e9%ab%98%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/15315.html","title":{"rendered":"\uff1c\u9879\u76ee\uff1e \u4e3b\u4eceReactor\u6a21\u578b\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"Reactor-toc\" style=\"margin-left:40px\">Reactor<\/p>\n<p id=\"%E6%A6%82%E5%BF%B5-toc\" style=\"margin-left:80px\">\u6982\u5ff5<\/p>\n<p id=\"%E5%88%86%E7%B1%BB-toc\" style=\"margin-left:80px\">\u5206\u7c7b<\/p>\n<p id=\"%E5%8D%95Reactor%E5%8D%95%E7%BA%BF%E7%A8%8B-toc\" style=\"margin-left:120px\">\u5355Reactor\u5355\u7ebf\u7a0b<\/p>\n<p id=\"%E5%8D%95Reactor%E5%A4%9A%E7%BA%BF%E7%A8%8B-toc\" style=\"margin-left:120px\">\u5355Reactor\u591a\u7ebf\u7a0b<\/p>\n<p id=\"%E5%A4%9AReactor%E5%A4%9A%E7%BA%BF%E7%A8%8B-toc\" style=\"margin-left:120px\">\u591aReactor\u591a\u7ebf\u7a0b<\/p>\n<p id=\"%E9%A1%B9%E7%9B%AE%E4%BB%8B%E7%BB%8D%C2%A0-toc\" style=\"margin-left:40px\">\u9879\u76ee\u4ecb\u7ecd\u00a0<\/p>\n<p id=\"%E9%A1%B9%E7%9B%AE%E8%A7%84%E5%88%92-toc\" style=\"margin-left:80px\">\u9879\u76ee\u89c4\u5212<\/p>\n<p id=\"%E6%A8%A1%E5%9D%97%E5%85%B3%E7%B3%BB-toc\" style=\"margin-left:80px\">\u6a21\u5757\u5173\u7cfb<\/p>\n<p id=\"%E5%AE%9E%E7%8E%B0%C2%A0-toc\" style=\"margin-left:40px\">\u5b9e\u73b0\u00a0<\/p>\n<p id=\"%E6%97%B6%E9%97%B4%E8%BD%AE%E5%AE%9A%E6%97%B6%E5%99%A8-toc\" style=\"margin-left:80px\">TimerWheel &#8212; \u65f6\u95f4\u8f6e\u5b9a\u65f6\u5668<\/p>\n<p id=\"%E5%AE%9A%E6%97%B6%E5%99%A8%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8-toc\" style=\"margin-left:120px\">\u5b9a\u65f6\u5668\u7cfb\u7edf\u8c03\u7528<\/p>\n<p id=\"%E6%97%B6%E9%97%B4%E8%BD%AE%E8%AE%BE%E8%AE%A1-toc\" style=\"margin-left:120px\">\u65f6\u95f4\u8f6e\u8bbe\u8ba1<\/p>\n<p id=\"%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F-toc\" style=\"margin-left:80px\">\u901a\u7528\u7c7b\u578bAny\u00a0<\/p>\n<p id=\"Buffer-toc\" style=\"margin-left:80px\">Buffer<\/p>\n<p id=\"Socket-toc\" style=\"margin-left:80px\">Socket<\/p>\n<p id=\"Channel-toc\" style=\"margin-left:80px\">Channel<\/p>\n<p id=\"Poller-toc\" style=\"margin-left:80px\">Poller<\/p>\n<p id=\"EventLoop%EF%BC%88%E6%A0%B8%E5%BF%83%EF%BC%89-toc\" style=\"margin-left:80px\">EventLoop&#xff08;\u6838\u5fc3&#xff09;<\/p>\n<p id=\"eventfd%C2%A0-toc\" style=\"margin-left:120px\">eventfd\u00a0<\/p>\n<p id=\"%E8%AE%BE%E8%AE%A1%E6%80%9D%E8%B7%AF-toc\" style=\"margin-left:120px\">\u8bbe\u8ba1\u601d\u8def<\/p>\n<p id=\"Connection-toc\" style=\"margin-left:80px\">Connection<\/p>\n<p id=\"Accept-toc\" style=\"margin-left:80px\">Accept<\/p>\n<p id=\"LoopThread-toc\" style=\"margin-left:80px\">LoopThread<\/p>\n<p id=\"LoopThreadPool-toc\" style=\"margin-left:80px\">LoopThreadPool<\/p>\n<p id=\"TcpServer-toc\" style=\"margin-left:80px\">TcpServer<\/p>\n<p id=\"%E2%80%8B%E7%BC%96%E8%BE%91%C2%A0EchoServer-toc\" style=\"margin-left:40px\">EchoServer<\/p>\n<p id=\"%C2%A0%E6%B5%8B%E8%AF%95-toc\" style=\"margin-left:80px\">\u6d4b\u8bd5<\/p>\n<p id=\"%E6%80%A7%E8%83%BD%E6%B5%8B%E8%AF%95-toc\" style=\"margin-left:80px\">\u6027\u80fd\u6d4b\u8bd5<\/p>\n<p id=\"HTTPServer-toc\" style=\"margin-left:40px\">HTTPServer<\/p>\n<hr \/>\n<h3 id=\"Reactor\">Reactor<\/h3>\n<h4 id=\"%E6%A6%82%E5%BF%B5\">\u6982\u5ff5<\/h4>\n<p><span style=\"color:#7b7f82\">Reactor \u6a21\u5f0f\u662f\u4e00\u79cd\u4e8b\u4ef6\u9a71\u52a8\u7684\u8bbe\u8ba1\u6a21\u5f0f&#xff0c;\u5e7f\u6cdb\u5e94\u7528\u4e8e\u9ad8\u5e76\u53d1\u3001\u5f02\u6b65I\/O\u573a\u666f\u4e0b\u7684\u7cfb\u7edf\u8bbe\u8ba1\u3002\u5b83\u901a\u8fc7\u5c06\u8bf7\u6c42\u5206\u53d1\u5230\u9002\u5f53\u7684\u5904\u7406\u7a0b\u5e8f\u6765\u7ba1\u7406\u8f93\u5165\/\u8f93\u51fa\u64cd\u4f5c&#xff0c;\u5e76\u4f7f\u5f97\u5355\u7ebf\u7a0b\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u7684\u8fde\u63a5\u3002<\/span><\/p>\n<p>\u6838\u5fc3\u7ec4\u4ef6<\/p>\n<p><span style=\"color:#7b7f82\">Reactor (\u53cd\u5e94\u5668)<\/span><\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u8d1f\u8d23\u76d1\u542c\u5e76\u63a5\u6536\u6240\u6709\u7c7b\u578b\u7684\u4e8b\u4ef6&#xff08;\u5982\u8bfb\u5c31\u7eea\u3001\u5199\u5c31\u7eea\u7b49&#xff09;&#xff0c;\u7136\u540e\u5c06\u5176\u5206\u6d3e\u7ed9\u76f8\u5e94\u7684\u5904\u7406\u5668\u8fdb\u884c\u5904\u7406\u3002\u5b83\u662f\u6574\u4e2a\u6a21\u5f0f\u7684\u6838\u5fc3\u534f\u8c03\u8005\u3002<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#7b7f82\">Handlers (\u4e8b\u4ef6\u5904\u7406\u5668)<\/span><\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u6bcf\u79cd\u5177\u4f53\u7684\u4e8b\u4ef6\u90fd\u6709\u5bf9\u5e94\u7684Handler\u8d1f\u8d23\u5b9e\u9645\u4e1a\u52a1\u903b\u8f91\u7684\u6267\u884c\u3002\u4f8b\u5982&#xff0c;\u201c\u5ba2\u6237\u7aef\u6570\u636e\u5230\u8fbe\u201d\u7684\u4e8b\u4ef6\u7531\u4e13\u95e8\u7684\u6570\u636e\u8bfb\u53d6Handler\u5b8c\u6210\u4efb\u52a1&#xff1b;\u800c\u201c\u6570\u636e\u53d1\u9001\u5b8c\u6bd5\u201d\u5219\u4ea4\u7531\u53e6\u4e00\u7ec4Handler\u53bb\u6e05\u7406\u8d44\u6e90\u6216\u505a\u5176\u4ed6\u540e\u7eed\u5de5\u4f5c\u3002\u00a0<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#7b7f82\">Demultiplexer (\u591a\u8def\u590d\u7528\u5668)<\/span><\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u8fd9\u662f\u4e00\u4e2a\u62bd\u8c61\u7684\u6982\u5ff5&#xff0c;\u5728\u64cd\u4f5c\u7cfb\u7edf\u5c42\u9762\u901a\u5e38\u6307\u4ee3\u50cfselect(), poll()\u00a0\u6216 Linux \u7279\u6709\u7684 epoll()\u00a0\u7b49 API \u3002\u5b83\u7684\u4f5c\u7528\u662f\u4ece\u4f17\u591a\u6587\u4ef6\u63cf\u8ff0\u7b26\u91cc\u7b5b\u9009\u51fa\u90a3\u4e9b\u5f53\u524d\u5df2\u7ecf\u51c6\u5907\u597d\u53ef\u4ee5\u8fdb\u884c I\/O \u64cd\u4f5c\u7684\u5bf9\u8c61\u96c6\u5408\u4ea4\u7ed9\u4e0a\u5c42\u5e94\u7528\u7ee7\u7eed\u52a0\u5de5\u3002<\/span><\/li>\n<\/ul>\n<p>\u5de5\u4f5c\u6d41\u7a0b<\/p>\n<p><span style=\"color:#7b7f82\">\u5f53\u4e00\u4e2a\u65b0\u4e8b\u4ef6\u53d1\u751f\u65f6&#xff0c;\u6bd4\u5982\u6709\u65b0\u7684\u7f51\u7edc\u8fde\u63a5\u8fdb\u6765\u6216\u8005\u662f\u67d0\u4e2a\u5957\u63a5\u5b57\u53d8\u5f97\u53ef\u8bfb\u4e86&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">Demultiplexer\u4f1a\u68c0\u6d4b\u5230\u8fd9\u4e2a\u53d8\u5316\u5e76\u5c06\u76f8\u5173\u4fe1\u606f\u901a\u77e5\u7ed9Reactor&#xff1b;<\/span><\/li>\n<li><span style=\"color:#7b7f82\">Reactor\u6839\u636e\u9884\u5148\u8bbe\u5b9a\u597d\u7684\u89c4\u5219\u627e\u5230\u5339\u914d\u6b64\u7c7b\u578b\u4e8b\u4ef6\u7684\u90a3\u4e2a\u7279\u5b9ahandler\u5b9e\u4f8b&#xff1b;<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u6700\u7ec8\u628a\u63a7\u5236\u6743\u8f6c\u79fb\u8fc7\u53bb\u8ba9\u90a3\u4e2a handler \u6267\u884c\u5176\u804c\u8d23\u8303\u56f4\u5185\u7684\u4e8b\u52a1\u3002<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#7b7f82\">\u8fd9\u79cd\u67b6\u6784\u975e\u5e38\u9002\u7528\u4e8e\u9700\u8981\u540c\u65f6\u76d1\u63a7\u5927\u91cf\u72ec\u7acb\u6765\u6e90\u7684\u60c5\u51b5\u4e4b\u4e0b&#xff0c;\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u5927\u91cf\u7684\u963b\u585e\u7b49\u5f85\u65f6\u95f4\u6d6a\u8d39\u6389CPU\u5468\u671f\u6570\u76ee\u7684\u60c5\u51b5\u51fa\u73b0\u3002<\/span><\/p>\n<h4 id=\"%E5%88%86%E7%B1%BB\">\u5206\u7c7b<\/h4>\n<h5 id=\"%E5%8D%95Reactor%E5%8D%95%E7%BA%BF%E7%A8%8B\">\u5355Reactor\u5355\u7ebf\u7a0b<\/h5>\n<p><span style=\"color:#7b7f82\">\u4e00\u4e2a\u5355\u72ec\u7684\u5de5\u4f5c\u7ebf\u7a0b\u914d\u5408\u4e00\u4e2a\u53cd\u5e94\u5668&#xff08;Reactor&#xff09;&#xff0c;\u7ba1\u7406\u6240\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u8bf7\u6c42\u3002<\/span><\/p>\n<li><span style=\"color:#646a73\">\u901a\u8fc7IO\u591a\u8def\u590d\u2f64\u6a21\u578b\u8fdb\u2f8f\u5ba2\u2f3e\u7aef\u8bf7\u6c42\u76d1\u63a7 <\/span><\/li>\n<li><span style=\"color:#646a73\">\u89e6\u53d1\u4e8b\u4ef6\u540e&#xff0c;\u8fdb\u2f8f\u4e8b\u4ef6\u5904\u7406 <\/span>\n<li><span style=\"color:#646a73\">\u5982\u679c\u662f\u65b0\u5efa\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5219\u83b7\u53d6\u65b0\u5efa\u8fde\u63a5&#xff0c;\u5e76\u6dfb\u52a0\u2f84\u591a\u8def\u590d\u2f64\u6a21\u578b\u8fdb\u2f8f\u4e8b\u4ef6\u76d1\u63a7\u3002 <\/span><\/li>\n<li><span style=\"color:#646a73\">\u5982\u679c\u662f\u6570\u636e\u901a\u4fe1\u8bf7\u6c42&#xff0c;\u5219\u8fdb\u2f8f\u5bf9\u5e94\u6570\u636e\u5904\u7406&#xff08;\u63a5\u6536\u6570\u636e&#xff0c;\u5904\u7406\u6570\u636e&#xff0c;\u53d1\u9001\u54cd\u5e94&#xff09;<\/span><\/li>\n<\/li>\n<p>\u5728\u8fd9\u4e2a\u6a21\u578b\u91cc&#xff0c;\u6240\u6709\u7684I\/O\u64cd\u4f5c\u90fd\u662f\u975e\u963b\u585e(non-blocking)\u5f62\u5f0f\u5b8c\u6210&#xff0c;\u5e76\u4f9d\u8d56\u4e8e\u5e95\u5c42\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u4e8b\u4ef6\u901a\u77e5\u529f\u80fd(\u5982select\u3001poll\u6216epoll\u7b49&#xff0c;\u6211\u4eec\u4f7f\u7528\u7684\u662fepoll)&#xff0c;\u5f53\u67d0\u4e2a\u5957\u63a5\u5b57(socket)\u51c6\u5907\u5c31\u7eea\u65f6\u4f1a\u89e6\u53d1\u5bf9\u5e94\u7684\u8bfb\u53d6(read) \/ \u5199\u5165(write)\u56de\u8c03\u51fd\u6570\u6765\u5b9e\u9645\u5904\u7406\u6570\u636e\u4ea4\u6362\u4efb\u52a1\u3002\u7531\u4e8e\u53ea\u5b58\u5728\u552f\u4e00\u7684\u4e00\u6761\u6267\u884c\u8def\u5f84\u8d1f\u8d23\u63a5\u6536\u65b0\u6765\u7684\u94fe\u63a5\u4ee5\u53ca\u670d\u52a1\u4e8e\u73b0\u5b58\u6d3b\u8dc3\u8fde\u63a5\u4e0a\u7684\u4ea4\u4e92\u52a8\u4f5c\u3002<\/p>\n<p><span style=\"color:#7b7f82\">\u4f18\u70b9&#xff1a;\u8be5\u67b6\u6784\u76f8\u5bf9\u7b80\u5355\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4&#xff0c;\u540c\u65f6\u4e5f\u80fd\u4fdd\u8bc1\u8f83\u9ad8\u7684\u6027\u80fd\u6c34\u5e73&#xff0c;\u7279\u522b\u662f\u5728\u9ad8\u8d1f\u8f7d\u6761\u4ef6\u4e0b\u8868\u73b0\u5c24\u4e3a\u7a81\u51fa&#xff0c;\u56e0\u4e3a\u907f\u514d\u4e86\u9891\u7e41\u521b\u5efa\u9500\u6bc1\u7ebf\u7a0b\u5e26\u6765\u7684\u5f00\u9500&#xff0c;\u4e5f\u65e0\u9700\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u7f3a\u70b9&#xff1a;\u5728\u9762\u5bf9\u8ba1\u7b97\u5bc6\u96c6\u578b\u4e1a\u52a1\u573a\u666f\u4e0b\u5b83\u7684\u52a3\u52bf\u4fbf\u663e\u73b0\u51fa\u6765\u4e86\u2014\u2014\u5982\u679c\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684\u4efb\u52a1\u8017\u7528\u4e86\u8fc7\u591aCPU\u8d44\u6e90&#xff0c;\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5176\u4ed6\u7b49\u5f85\u670d\u52a1\u961f\u5217\u4e2d\u7684\u9879\u76ee\u5ef6\u8fdf\u54cd\u5e94\u65f6\u95f4\u8fc7\u957f&#xff0c;\u5e76\u4e14\u65e0\u6cd5\u53ca\u65f6\u83b7\u53d6\u65b0\u8fde\u63a5&#xff0c;\u7528\u6237\u4f53\u9a8c\u5dee&#xff1b;\u6b64\u5916\u5bf9\u4e8e\u591a\u6838\u5904\u7406\u5668\u73af\u5883\u800c\u8a00\u65e0\u6cd5\u5145\u5206\u5229\u7528\u786c\u4ef6\u5e76\u884c\u8fd0\u7b97\u80fd\u529b\u4e5f\u662f\u5176\u56fa\u6709\u7f3a\u9677\u4e4b\u4e00\u3002<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"407\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135254-68025936d6853.png\" width=\"850\" \/><\/p>\n<h5 id=\"%E5%8D%95Reactor%E5%A4%9A%E7%BA%BF%E7%A8%8B\">\u5355Reactor\u591a\u7ebf\u7a0b<\/h5>\n<li><span style=\"color:#646a73\">Reactor\u7ebf\u7a0b\u901a\u8fc7I\/O\u591a\u8def\u590d\u2f64\u6a21\u578b\u8fdb\u2f8f\u5ba2\u2f3e\u7aef\u8bf7\u6c42\u76d1\u63a7 <\/span><\/li>\n<li><span style=\"color:#646a73\">\u89e6\u53d1\u4e8b\u4ef6\u540e&#xff0c;\u8fdb\u2f8f\u4e8b\u4ef6\u5904\u7406 <\/span>\n<li><span style=\"color:#646a73\">\u5982\u679c\u662f\u65b0\u5efa\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5219\u83b7\u53d6\u65b0\u5efa\u8fde\u63a5&#xff0c;\u5e76\u6dfb\u52a0\u2f84\u591a\u8def\u590d\u2f64\u6a21\u578b\u8fdb\u2f8f\u4e8b\u4ef6\u76d1\u63a7\u3002 <\/span><\/li>\n<li><span style=\"color:#646a73\">\u5982\u679c\u662f\u6570\u636e\u901a\u4fe1\u8bf7\u6c42&#xff0c;\u5219\u63a5\u6536\u6570\u636e\u540e\u5206\u53d1\u7ed9Worker\u7ebf\u7a0b\u6c60\u8fdb\u2f8f\u4e1a\u52a1\u5904\u7406\u3002 <\/span><\/li>\n<li><span style=\"color:#646a73\">\u2f2f\u4f5c\u7ebf\u7a0b\u5904\u7406\u5b8c\u6bd5\u540e&#xff0c;\u5c06\u54cd\u5e94\u4ea4\u7ed9Reactor\u7ebf\u7a0b\u8fdb\u2f8f\u6570\u636e\u54cd\u5e94<\/span><\/li>\n<\/li>\n<p><span style=\"color:#7b7f82\">\u8fd9\u662f\u6700\u57fa\u7840\u7684\u4e00\u79cd\u5f62\u5f0f&#xff0c;\u4e3b\u7ebf\u7a0b\u8d1f\u8d23\u76d1\u542c\u6240\u6709\u7684\u4e8b\u4ef6\u6e90&#xff08;\u5982Socket\u8fde\u63a5&#xff09;&#xff0c;\u5f53\u6709\u4e8b\u4ef6\u5230\u8fbe\u65f6\u5c06\u5176\u5206\u53d1\u5230\u9884\u5148\u521b\u5efa\u597d\u7684\u4e00\u7ec4\u5de5\u4f5c\u8005\u7ebf\u7a0b\u6c60\u4e2d\u53bb\u5b9e\u9645\u5904\u7406\u4e1a\u52a1\u903b\u8f91\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u4f18\u70b9&#xff1a;\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u5145\u5206\u5229\u7528\u73b0\u4ee3CPU\u591a\u6838\u4f18\u52bf&#xff0c;\u5e76\u884c\u5316\u5730\u5b8c\u6210\u590d\u6742\u7684\u8ba1\u7b97\u4efb\u52a1\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u7f3a\u70b9&#xff1a;\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b&#xff0c;\u5355\u4e00\u7684Reactor\u5373\u4f7f\u4e0d\u8fdb\u884c\u4e1a\u52a1\u5904\u7406&#xff0c;\u4f46\u662f\u4e5f\u53ef\u80fd\u6765\u4e0d\u53ca\u5904\u7406\u5927\u91cf\u65b0\u8fde\u63a5&#xff0c;\u5bfc\u81f4\u5ef6\u8fdf\u589e\u52a0\u3002\u867d\u7136IO\u64cd\u4f5c\u662f\u7ebf\u7a0b\u6c60\u5904\u7406\u7684&#xff0c;\u4f46\u662f\u5355Reactor\u5904\u7406\u80fd\u529b\u53d7\u9650\u4e8e\u4e3b\u7ebf\u7a0b&#xff0c;\u5f53\u6709\u5927\u91cf\u7684\u4e8b\u4ef6\u9700\u8981\u5206\u53d1\u65f6&#xff0c;\u4e3b\u7ebf\u7a0b\u53ef\u80fd\u65e0\u6cd5\u53ca\u65f6\u5904\u7406&#xff0c;\u5bfc\u81f4\u4e8b\u4ef6\u79ef\u538b\u3002\u8fd9\u65f6\u5019\u7ebf\u7a0b\u6c60\u7684\u7ebf\u7a0b\u53ef\u80fd\u90fd\u5904\u4e8e\u7a7a\u95f2\u72b6\u6001&#xff0c;\u800c\u4e3b\u7ebf\u7a0b\u5374\u5fd9\u4e0d\u8fc7\u6765&#xff0c;\u7cfb\u7edf\u6574\u4f53\u7684\u541e\u5410\u91cf\u5c31\u88ab\u9650\u5236\u4e86\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u53e6\u5916&#xff0c;\u7ebf\u7a0b\u4e4b\u95f4\u7684\u5207\u6362\u5f00\u9500\u4e5f\u662f\u4e00\u4e2a\u95ee\u9898\u3002\u867d\u7136\u5de5\u4f5c\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u5904\u7406\u591a\u4e2a\u4efb\u52a1&#xff0c;\u4f46\u7ebf\u7a0b\u6570\u91cf\u589e\u591a\u7684\u8bdd&#xff0c;\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u6210\u672c\u4e5f\u4f1a\u4e0a\u5347&#xff0c;\u5c24\u5176\u662f\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b&#xff0c;\u9891\u7e41\u7684\u7ebf\u7a0b\u5207\u6362\u53ef\u80fd\u5bfc\u81f4CPU\u8d44\u6e90\u88ab\u5927\u91cf\u6d88\u8017&#xff0c;\u53cd\u800c\u964d\u4f4e\u4e86\u6548\u7387\u3002<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"580\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135255-6802593708cf8.png\" width=\"1096\" \/><\/p>\n<\/p>\n<h5 id=\"%E5%A4%9AReactor%E5%A4%9A%E7%BA%BF%E7%A8%8B\">\u591aReactor\u591a\u7ebf\u7a0b<\/h5>\n<li><span style=\"color:#646a73\">\u5728\u4e3bReactor\u4e2d\u5904\u7406\u65b0\u8fde\u63a5\u8bf7\u6c42\u4e8b\u4ef6&#xff0c;\u6709\u65b0\u8fde\u63a5\u5230\u6765\u5219\u5206\u53d1\u5230\u2f26Reactor\u4e2d\u76d1\u63a7 <\/span><\/li>\n<li><span style=\"color:#646a73\">\u5728\u2f26Reactor\u4e2d\u8fdb\u2f8f\u5ba2\u2f3e\u7aef\u901a\u4fe1\u76d1\u63a7&#xff0c;\u6709\u4e8b\u4ef6\u89e6\u53d1&#xff0c;\u5219\u63a5\u6536\u6570\u636e\u5206\u53d1\u7ed9Worker\u7ebf\u7a0b\u6c60 <\/span><\/li>\n<li><span style=\"color:#646a73\">Worker\u7ebf\u7a0b\u6c60\u5206\u914d\u72ec\u2f74\u7684\u7ebf\u7a0b\u8fdb\u2f8f\u5177\u4f53\u7684\u4e1a\u52a1\u5904\u7406 <\/span>\n<li><span style=\"color:#646a73\">\u2f2f\u4f5c\u7ebf\u7a0b\u5904\u7406\u5b8c\u6bd5\u540e&#xff0c;\u5c06\u54cd\u5e94\u4ea4\u7ed9\u2f26Reactor\u7ebf\u7a0b\u8fdb\u2f8f\u6570\u636e\u54cd\u5e94<\/span><\/li>\n<\/li>\n<p><span style=\"color:#7b7f82\">\u76f8\u8f83\u4e8e\u7b2c\u4e00\u79cd\u7ed3\u6784&#xff0c;\u6b64\u7248\u672c\u5f15\u5165\u4e86\u4e13\u95e8\u7528\u4e8e\u63a5\u6536\u65b0\u8bf7\u6c42\u7684\u201cAcceptor\u201d\u89d2\u8272\u4ee5\u53ca\u82e5\u5e72\u4e2a\u72ec\u7acb\u8fd0\u884c\u7684\u5b50\u7ea7Reactors\u5b9e\u4f8b\u3002\u6bcf\u4e2a\u6b21\u7ea7\u5355\u5143\u5404\u81ea\u7ef4\u62a4\u7740\u4e00\u90e8\u5206\u5df2\u7ecf\u5efa\u7acb\u8d77\u6765\u7684\u4f1a\u8bdd\u94fe\u63a5\u5e76\u5bf9\u5176\u53d1\u751f\u7684\u5404\u7c7b\u64cd\u4f5c\u505a\u51fa\u53cd\u5e94&#xff1b;\u540c\u65f6\u4e3a\u4e86\u5747\u8861\u8d1f\u8f7d\u538b\u529b\u8fd8\u53ef\u80fd\u6d89\u53ca\u5230\u52a8\u6001\u8c03\u6574\u5206\u914d\u6bd4\u4f8b\u7b49\u7b56\u7565\u4f18\u5316\u63aa\u65bd\u3002\u8fd9\u79cd\u5206\u79bb\u673a\u5236\u4e0d\u4ec5\u589e\u5f3a\u4e86\u53ef\u4f38\u7f29\u6027\u548c\u7a33\u5b9a\u6027\u800c\u4e14\u4e5f\u4fbf\u4e8e\u6a21\u5757\u5316\u7684\u7ba1\u7406\u4e0e\u6269\u5c55\u5347\u7ea7\u7b49\u5de5\u4f5c\u5f00\u5c55\u5b9e\u65bd\u3002<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"473\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135255-680259374d98e.png\" width=\"1065\" \/><\/p>\n<p>\u6ce8\u610f&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u5e76\u4e0d\u610f\u5473\u7740\u7ebf\u7a0b\u8d8a\u591a&#xff0c;\u670d\u52a1\u5668\u6548\u7387\u8d8a\u9ad8&#xff0c;\u56e0\u4e3a\u7ebf\u7a0b\u8d8a\u591a&#xff0c;CPU\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u7684\u5207\u6362\u5c31\u4f1a\u8d8a\u591a&#xff0c;\u9501\u8d44\u6e90\u7ade\u4e89\u8d8a\u6fc0\u70c8&#xff0c;\u4e32\u884c\u591a\u4e86&#xff0c;\u5e76\u53d1\u6548\u7387\u964d\u4f4e&#xff0c;\u5e76\u4e14CPU\u4f1a\u56e0\u4e3a\u5927\u91cf\u7684\u7ebf\u7a0b\u5207\u6362\u53cd\u800c\u5bfc\u81f4\u6574\u4f53\u6548\u7387\u964d\u4f4e<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u6240\u4ee5\u6709\u4e9b\u8bbe\u8ba1\u662f\u5c06\u4e1a\u52a1\u7ebf\u7a0b\u53bb\u9664&#xff0c;\u878d\u5408\u5230\u4ece\u5c5e\u7684Reactor\u7684IO\u4e8b\u4ef6\u76d1\u63a7\u7ebf\u7a0b\u4e2d&#xff0c;\u5373\u4ece\u5c5e\u7684Reactor\u9700\u8981IO\u4e8b\u4ef6\u76d1\u63a7\u3001IO\u64cd\u4f5c\u3001\u4e1a\u52a1\u5904\u7406\u4e09\u4e2a\u4efb\u52a1<\/span><\/li>\n<\/ul>\n<h3 id=\"%E9%A1%B9%E7%9B%AE%E4%BB%8B%E7%BB%8D%C2%A0\">\u9879\u76ee\u4ecb\u7ecd\u00a0<\/h3>\n<p>\u672c\u9879\u76ee\u4e2d&#xff0c;\u91c7\u7528\u7684\u662f\u591aReactor\u591a\u7ebf\u7a0b\u6a21\u5f0f&#xff0c;\u4e00\u4e2aReactor\u5bf9\u5e94\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u4e3bReactor\u53ea\u8d1f\u8d23\u65b0\u8fde\u63a5\u7684\u83b7\u53d6&#xff0c;\u591a\u4e2a\u4ece\u5c5eReactor\u8fdb\u884c\u8fde\u63a5\u7684IO\u4e8b\u4ef6\u76d1\u63a7\u4e0e\u4e1a\u52a1\u5904\u7406&#xff0c;\u6ca1\u6709\u91c7\u53d6\u7ebf\u7a0b\u6c60\u5904\u7406IO\u4e8b\u4ef6&#xff0c;\u4e3b\u8981\u662f\u8003\u8651\u7ebf\u7a0b\u6570\u91cf\u8fc7\u591a\u53cd\u800c\u5bfc\u81f4CPU\u8ba1\u7b97\u6548\u7387\u964d\u4f4e&#xff0c;\u9891\u7e41\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u3002<\/p>\n<h4 id=\"%E9%A1%B9%E7%9B%AE%E8%A7%84%E5%88%92\">\u9879\u76ee\u89c4\u5212<\/h4>\n<p><span style=\"color:#7b7f82\">\u6574\u4e2a\u9879\u76ee\u5212\u5206\u4e3a\u4e24\u4e2a\u5927\u6a21\u5757&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">Server\u6a21\u5757&#xff1a;\u5b9e\u73b0\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u7684\u4e3b\u4eceReactor\u6a21\u578b\u7684TCP\u670d\u52a1\u5668\u3002<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u534f\u8bae\u6a21\u5757&#xff1a;\u5bf9\u5f53\u524d\u7684Reactor\u670d\u52a1\u5668\u63d0\u4f9b\u5e94\u7528\u5c42\u534f\u8bae\u652f\u6301<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#7b7f82\">\u5176\u4e2dServer\u6a21\u5757\u6700\u5173\u952e&#xff0c;\u5728\u8fd9\u4e2a\u6a21\u5757\u4e2d&#xff0c;\u4e3b\u8981\u662f\u8981\u5bf9\u4e8e\u6240\u6709\u7684\u94fe\u63a5\u4ee5\u53ca\u7ebf\u7a0b\u8fdb\u884c\u7ba1\u7406&#xff0c;\u7ba1\u7406\u7684\u4e3b\u8981\u5185\u5bb9\u6709\u4e0b\u9762\u7684\u4e09\u4e2a\u65b9\u9762<\/span><\/p>\n<li><span style=\"color:#7b7f82\">\u76d1\u542c\u8fde\u63a5\u7ba1\u7406&#xff1a;\u6709\u65b0\u7684\u8fde\u63a5\u5230\u6765\u65f6&#xff0c;\u8981\u5bf9\u4e8e\u65b0\u7684\u8fde\u63a5\u60c5\u51b5\u8fdb\u884c\u7ba1\u7406<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u901a\u4fe1\u8fde\u63a5\u7ba1\u7406&#xff1a;\u5bf9\u4e8e\u901a\u4fe1\u8fde\u63a5\u8981\u8fdb\u884c\u7ba1\u7406<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u8d85\u65f6\u8fde\u63a5\u7ba1\u7406&#xff1a;\u5f53\u6709\u8fde\u63a5\u5904\u4e8e\u8d85\u65f6\u72b6\u6001\u65f6&#xff0c;\u8981\u5bf9\u4e8e\u8fd9\u4e9b\u5df2\u7ecf\u8d85\u65f6\u7684\u8fde\u63a5\u8fdb\u884c\u7ba1\u7406<\/span><\/li>\n<p><span style=\"color:#7b7f82\">\u6240\u4ee5\u57fa\u4e8e\u4e0a\u9762\u7684\u8fd9\u4e09\u4e2a\u4e3b\u8981\u7684\u7ba1\u7406\u65b9\u5f0f&#xff0c;\u53c8\u53ef\u4ee5\u6784\u5efa\u51fa\u4e0b\u9762\u7684\u4e00\u4e9b\u66f4\u52a0\u7ec6\u81f4\u5316\u7684\u6a21\u5757<\/span><\/p>\n<p><span style=\"color:#7b7f82\">Buffer\u6a21\u5757&#xff1a;\u7f13\u51b2\u533a\u6a21\u5757\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u529f\u80fd&#xff1a;\u7528\u4e8e\u5b9e\u73b0C\/S\u901a\u4fe1\u4e2d\u670d\u52a1\u7aef\u7684\u7528\u6237\u6001\u7684\u63a5\u6536\u7f13\u51b2\u533a\u548c\u53d1\u9001\u7f13\u51b2\u533a\u529f\u80fd\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u610f\u4e49&#xff1a;\u5728\u5b9e\u9645\u7684TCP\u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1\u7684\u65f6\u5019&#xff0c;\u6709\u53ef\u80fd\u4f1a\u51fa\u73b0\u8bfb\u53d6\u4e0a\u6765\u7684\u6570\u636e\u5e76\u4e0d\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u62a5\u6587&#xff0c;\u5728\u53d1\u9001\u7684\u65f6\u5019\u53ef\u80fdTCP\u7684\u53d1\u9001\u7f13\u51b2\u533a\u5df2\u6ee1&#xff0c;\u6240\u4ee5\u8981\u5bf9\u4e8e\u8fd9\u4e9b\u6ca1\u6709\u5b8c\u5168\u5c31\u7eea\u7684\u6570\u636e\u8fdb\u884c\u4e00\u4e2a\u6682\u65f6\u7684\u7f13\u5b58\u7ba1\u7406\u7684\u8fc7\u7a0b&#xff0c;\u6240\u4ee5\u8981\u5355\u72ec\u8bbe\u8ba1\u51fa\u4e00\u4e2aBuffer\u6a21\u5757&#xff0c;\u4e3b\u8981\u7684\u529f\u80fd\u5c31\u662f\u8981\u5b9e\u73b0\u901a\u4fe1\u5957\u63a5\u5b57\u7684\u7528\u6237\u6001\u7f13\u51b2\u533a<\/span><\/p>\n<p><span style=\"color:#7b7f82\">Socket\u6a21\u5757&#xff1a;\u5957\u63a5\u5b57\u6a21\u5757<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u529f\u80fd&#xff1a;\u5bf9\u5957\u63a5\u5b57\u64cd\u4f5c\u505a\u5c01\u88c5\u7684\u6a21\u5757&#xff0c;\u6709socket\u3001bind\u3001listen\u3001connect\u3001accept\u3001recv\u3001send\u3001\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u7aef\u8fde\u63a5\u3001\u521b\u5efa\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u7b49\u63a5\u53e3\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u610f\u4e49&#xff1a;\u4f7f\u5f97\u7a0b\u5e8f\u4e2d\u5bf9\u5957\u63a5\u5b57\u7684\u5404\u9879\u64cd\u4f5c\u66f4\u52a0\u7b80\u4fbf\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">Channel\u6a21\u5757<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u529f\u80fd&#xff1a;\u5bf9\u4e00\u4e2a\u63cf\u8ff0\u7b26\u8fdb\u884cIO\u4e8b\u4ef6\u76d1\u63a7\u7ba1\u7406\u7684\u6a21\u5757&#xff0c;\u5b9e\u73b0\u5bf9\u63cf\u8ff0\u7b26\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u9519\u8bef\u3001\u7b49\u4e8b\u4ef6\u7684\u7ba1\u7406\u64cd\u4f5c&#xff0c;\u5f53\u4e8b\u4ef6\u5c31\u7eea\u540e&#xff0c;\u5bf9\u5c31\u7eea\u7684\u76d1\u63a7\u4e8b\u4ef6\u7684\u5904\u7406\u4e5f\u662f\u7531Channel\u6a21\u5757\u6765\u8d1f\u8d23\u7684\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\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\u7b80\u4fbf&#xff08;\u4e8b\u4ef6\u5c31\u7eea\u540e\u76f4\u63a5\u8c03\u7528Channel\u7c7b\u7684HandlerEvent\u51fd\u6570\u5373\u53ef&#xff09;<\/span><\/p>\n<p><span style=\"color:#7b7f82\">Poller\u6a21\u5757<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u529f\u80fd&#xff1a;\u5bf9epoll\u7cfb\u7edf\u8c03\u7528\u505a\u5c01\u88c5\u7684\u6a21\u5757&#xff0c;\u5bf9\u5916\u63d0\u4f9b\u5bf9epoll\u7684IO\u4e8b\u4ef6\u76d1\u63a7\u7684\u6dfb\u52a0\u3001\u4fee\u6539\u3001\u79fb\u9664\u3001\u83b7\u53d6\u5c31\u7eea\u4e8b\u4ef6\u7684\u63a5\u53e3\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u610f\u4e49&#xff1a;\u4f7f\u6211\u4eec\u5728\u5bf9\u63cf\u8ff0\u7b26\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u7684\u64cd\u4f5c\u66f4\u52a0\u7b80\u5355\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">TimerWheel\u6a21\u5757&#xff1a;\u5b9a\u65f6\u5668\u6a21\u5757<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u529f\u80fd&#xff1a;\u5411\u5b9a\u65f6\u5668\u6dfb\u52a0\u4e00\u4e2a\u4efb\u52a1\u540e&#xff0c;\u8be5\u4efb\u52a1\u4f1a\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u540e\u88ab\u6267\u884c&#xff0c;\u540c\u65f6\u4e5f\u53ef\u4ee5\u5237\u65b0\u5b9a\u65f6\u4efb\u52a1\u6765\u5ef6\u8fdf\u4efb\u52a1\u7684\u6267\u884c\u4e8b\u4ef6\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u8bbe\u8ba1&#xff1a;\u6211\u4eec\u91c7\u7528\u65f6\u95f4\u8f6e\u601d\u60f3&#xff0c;\u5373\u521b\u5efa\u4e00\u4e2a60\u4e2a\u5143\u7d20\u5927\u5c0f\u7684\u6570\u7ec4&#xff08;\u8868\u793a\u30100&#xff0c; 59\u3011\u79d2&#xff09;&#xff0c;\u5e76\u5b9a\u4e49\u4e00\u4e2a\u79d2\u9488\u53d8\u91cf&#xff08;\u4e0b\u6807\u521d\u59cb\u6307\u54110&#xff09;&#xff0c;\u8be5\u79d2\u9488\u4f1a\u4e00\u79d2\u949f\u79fb\u52a8\u4e00\u4e2a\u5143\u7d20&#xff0c;\u79fb\u52a8\u5230\u54ea\u91cc\u5c31\u91ca\u653e\u54ea\u91cc\u7684\u4efb\u52a1<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u610f\u4e49&#xff1a;\u8fd9\u4e2a\u6a21\u5757\u662f\u5bf9Connection\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406&#xff0c;\u4e00\u4e2a\u8fde\u63a5\u5728\u89c4\u5b9a\u7684\u6807\u51c6\u65f6\u95f4\u5185\u6ca1\u53d1\u751f\u4efb\u4f55\u65f6\u95f4&#xff0c;\u5219\u8be5\u8fde\u63a5\u65f6\u975e\u6d3b\u8dc3\u8fde\u63a5&#xff0c;\u670d\u52a1\u5668\u6ca1\u5fc5\u8981\u7ef4\u62a4\u4e00\u4e2a\u975e\u6d3b\u8dc3\u8fde\u63a5&#xff0c;\u8fd9\u4f1a\u6d6a\u8d39\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u6240\u4ee5\u8d85\u65f6\u540e\u4f1a\u81ea\u52a8\u91ca\u653e\u8fde\u63a5\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">EventLoop\u6a21\u5757<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u529f\u80fd&#xff1a;\u8fdb\u884c\u8fde\u63a5\u7ba1\u7406\u7684\u6574\u5408\u6a21\u5757&#xff08;\u5bf9\u8fde\u63a5\u7684\u4e8b\u4ef6\u76d1\u63a7\u4ee5\u53ca\u8d85\u65f6\u4efb\u52a1\u7684\u7ba1\u7406\u90fd\u662f\u7531\u8be5\u6a21\u5757\u63d0\u4f9b\u63a5\u53e3&#xff09;&#xff0c;\u4e5f\u5c31\u662f\u6211\u4eec\u6240\u8bf4\u7684one thread one eventloop\u4e2d\u7684loop&#xff0c;\u5373\u4ece\u5c5eReactor&#xff0c;\u8fd9\u4e2a\u6a21\u5757\u4e0e\u7ebf\u7a0b\u4e00\u4e00\u5bf9\u5e94\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u8bbe\u8ba1&#xff1a;EventLoop\u4e2d\u4f1a\u7ec4\u5408\u4e00\u4e2a\u5b9a\u65f6\u5668\u6a21\u5757\u3001Poller\u6a21\u5757&#xff0c;\u7528\u6765\u5bf9\u63cf\u8ff0\u7b26\u8fdb\u884cIO\u4e8b\u4ef6\u76d1\u63a7\u64cd\u4f5c\u3001\u5b9a\u65f6\u4efb\u52a1\u64cd\u4f5c&#xff0c;\u5e76\u4e14\u5728\u6a21\u5757\u4e2d\u8fd8\u4f1a\u8bbe\u7f6e\u4e00\u4e2a\u4efb\u52a1\u961f\u5217&#xff0c;\u8be5\u4efb\u52a1\u961f\u5217\u4fdd\u8bc1\u4e00\u4e2a\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u5e94\u8be5\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5185\u90e8\u6267\u884c&#xff0c;\u6240\u4ee5\u5bf9\u4e8e\u9700\u8981\u5728\u7ebf\u7a0b\u5185\u90e8\u6267\u884c\u7684\u51fd\u6570&#xff0c;\u6211\u4eec\u90fd\u5c01\u88c5\u4e3a\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;push\u5230EventLoop\u7684\u4efb\u52a1\u961f\u5217\u4e2d&#xff0c;\u8fd9\u6837\u5c31\u4fdd\u8bc1\u4e86\u5bf9\u4e00\u4e2a\u94fe\u63a5\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5185\u90e8\u6267\u884c\u8d39&#xff0c;\u907f\u514d\u4e86\u7ebf\u7a0b\u5b89\u5168\u800c\u4e0d\u5f97\u4e3a\u6bcf\u4e00\u4e2a\u8fde\u63a5\u52a0\u9501\u6700\u7ec8\u5bfc\u81f4\u6548\u7387\u6781\u4f4e\u7684\u95ee\u9898\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u7531\u4e8e\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4eba\u7269\u90fd\u662f\u5728\u4e8b\u4ef6\u5c31\u7eea\u540e\u4ea7\u751f\u7684&#xff0c;\u6240\u4ee5\u5982\u679cepoll\u4e2d\u6ca1\u6709\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u90a3\u4e48\u4ece\u5c5eReactor\u6267\u884c\u6d41\u5c31\u4f1a\u963b\u585e\u5728epoll_wait\u4e2d&#xff0c;\u5bfc\u81f4\u4efb\u52a1\u961f\u5217\u7684\u4efb\u52a1\u8fdf\u8fdf\u5f97\u4e0d\u5230\u6267\u884c&#xff0c;\u4e3a\u4e86\u907f\u514d\u53d1\u751f\u8fd9\u4e2a\u95ee\u9898&#xff0c;\u6211\u4eec\u53c8\u521b\u5efa\u4e86\u4e00\u4e2aeventfd&#xff0c;\u5c06eventfd\u7684\u8bfb\u4e8b\u4ef6\u4e5f\u52a0\u5165\u4e8b\u4ef6\u76d1\u63a7\u4e2d&#xff0c;\u4e00\u65e6\u6211\u4eec\u5411\u4efb\u52a1\u961f\u5217\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u4efb\u52a1&#xff0c;\u90a3\u4e48\u6211\u4eec\u5c31\u9700\u8981\u5411eventfd\u4e2d\u5199\u5165\u4e00\u4e2a\u6570\u636e&#xff0c;\u6b64\u65f6epoll_wait\u81f3\u5c11\u4f1a\u56e0\u4e3aeventfd\u7684\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u800c\u8fd4\u56de\u5c31\u7eea\u4e8b\u4ef6&#xff0c;\u5728\u6267\u884c\u5b8c\u5c31\u7eea\u4e8b\u4ef6\u540e&#xff0c;\u5c31\u53ef\u4ee5\u6267\u884c\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u4e86\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u610f\u4e49&#xff1a;\u5bf9\u4e8e\u670d\u52a1\u5668\u7684\u6240\u6709\u4e8b\u4ef6\u90fd\u662f\u7531EventLoop\u6a21\u5757\u5b8c\u6210\u7684&#xff0c;\u6bcf\u4e00\u4e2a\u65b0\u83b7\u53d6\u4e0a\u6765\u7684\u8fde\u63a5\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2aConnection\u5bf9\u8c61&#xff0c;\u6bcf\u4e2aConnection\u5bf9\u8c61\u90fd\u4f1a\u7ed1\u5b9a\u4e00\u4e2aEventLoop\u6a21\u5757\u548c\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff0c;\u56e0\u4e3a\u4e00\u4e2a\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u53ea\u5728\u4e00\u4e2a\u7ebf\u7a0b\u5185\u6267\u884c\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">Any\u6a21\u5757<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u529f\u80fd&#xff1a;\u7528\u4e8e\u9002\u914d\u6240\u6709\u4e0a\u5c42\u534f\u8bae&#xff0c;\u4e0d\u540c\u7684\u534f\u8bae\u5bf9\u5e94\u7684\u4e0a\u4e0b\u6587\u7c7b\u578b\u4e0d\u540c&#xff0c;\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u63a5\u53e3\u4f9b\u4e0a\u5c42\u7528\u6237\u4fee\u6539\u652f\u6301\u7684\u534f\u8bae<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u610f\u4e49&#xff1a;\u4f9b\u4e0a\u5c42\u7528\u6237\u4fee\u6539\u652f\u6301\u7684\u534f\u8bae<\/span><\/p>\n<p><span style=\"color:#7b7f82\">Connection\u6a21\u5757<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u529f\u80fd&#xff1a;\u5bf9\u4e00\u4e2a\u901a\u4fe1\u8fde\u63a5\u6574\u4f53\u7ba1\u7406\u7684\u6a21\u5757&#xff0c;\u5bf9\u4e00\u4e2a\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u662f\u901a\u8fc7\u8fd9\u4e2a\u6a21\u5757\u8fdb\u884c\u7684&#xff0c;Connection\u7c7b\u5305\u542b\u4e00\u4e2aEventLoop\u7684\u6307\u9488\u5bf9\u8c61&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u8be5\u6307\u9488\u5bf9\u8fde\u63a5\u8fdb\u884c\u76d1\u63a7\u7ba1\u7406\u3001\u5b9a\u65f6\u4efb\u52a1\u7ba1\u7406\u3002\u5728Connection\u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6&#xff0c;\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4e3a\u5bf9\u6bcf\u4e00\u4e2a\u8fde\u63a5\u8bbe\u7f6e\u8bfb\u3001\u5199\u3001\u9519\u8bef\u3001\u5173\u95ed\u3001\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u610f\u4e49&#xff1a;\u662f\u5bf9\u4e00\u4e2a\u5df2\u7ecf\u88ab\u83b7\u53d6\u4e0a\u6765\u7684\u8fde\u63a5\u505a\u7ba1\u7406\u7684\u6a21\u5757&#xff0c;\u589e\u52a0\u94fe\u63a5\u64cd\u4f5c\u7684\u7075\u6d3b\u548c\u4fbf\u6377\u6027\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">Acceptor\u6a21\u5757<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u529f\u80fd&#xff1a;\u5bf9\u76d1\u542c\u5957\u63a5\u5b57\u505a\u7ba1\u7406\u7684\u6a21\u5757<\/span>\u00a0<\/p>\n<p><span style=\"color:#7b7f82\">\u610f\u4e49&#xff1a;\u5f53\u83b7\u53d6\u4e86\u4e00\u4e2a\u65b0\u8fde\u63a5\u7684\u63cf\u8ff0\u7b26\u4e4b\u540e&#xff0c;\u9700\u8981\u4e3a\u8be5\u901a\u4fe1\u8fde\u63a5\u5c01\u88c5\u4e00\u4e2aConnection\u5bf9\u8c61&#xff0c;\u4e3a\u8be5\u8fde\u63a5\u8bbe\u7f6e\u5404\u79cd\u56de\u8c03\u51fd\u6570&#xff08;\u8bfb\u3001\u5199\u3001\u9519\u8bef\u3001\u4efb\u610f\u3001\u591a\u4e2a\u9636\u6bb5\u56de\u8c03\u51fd\u6570&#xff09;\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">Acceptor\u6a21\u5757\u672c\u8eab\u5e76\u4e0d\u77e5\u9053\u4e00\u4e2a\u8fde\u63a5\u4ea7\u751f\u4e86\u67d0\u4e2a\u4e8b\u4ef6\u540e\u8be5\u5982\u4f55\u5904\u7406&#xff0c;\u56e0\u6b64\u83b7\u53d6\u4e00\u4e2a\u901a\u4fe1\u8fde\u63a5\u540e&#xff0c;Connection\u5bf9\u8c61\u7684\u521b\u5efa\u4ee5\u53ca\u5404\u4e2a\u9636\u6bb5\u56de\u8c03\u51fd\u6570\u8bbe\u7f6e\u90fd\u662f\u5728\u670d\u52a1\u5668\u6a21\u5757\u8fdb\u884c\u7684\u3002<\/span><\/p>\n<\/p>\n<h4 id=\"%E6%A8%A1%E5%9D%97%E5%85%B3%E7%B3%BB\">\u6a21\u5757\u5173\u7cfb<\/h4>\n<p><span style=\"color:#7b7f82\">\u9879\u76ee\u89c4\u5212\u4e2d\u5c55\u793a\u4e86\u5404\u4e2a\u6a21\u5757\u7684\u57fa\u672c\u529f\u80fd\u548c\u5927\u81f4\u8bbe\u8ba1&#xff0c;\u4f46\u662f\u6574\u4f53\u6765\u8bf4\u8fd8\u662f\u601d\u7ef4\u8f83\u4e71&#xff0c;\u6240\u4ee5\u4e0b\u9762\u6211\u7528\u51e0\u5f20\u56fe\u6765\u628a\u8fd9\u4e9b\u6a21\u5757\u4e4b\u95f4\u7684\u5173\u7cfb\u6784\u5efa\u51fa\u6765\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">Connection\u6a21\u5757\u7684\u903b\u8f91\u56fe<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"642\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135255-68025937892fd.png\" width=\"1311\" \/><\/p>\n<p><span style=\"color:#7b7f82\">Connection\u901a\u4fe1\u6a21\u5757\u56fe\u4e2d\u5305\u542b\u56db\u4e2a\u5927\u6a21\u5757&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">Buffer\u7f13\u51b2\u533a\u6a21\u5757<\/span><\/li>\n<li><span style=\"color:#7b7f82\">Socket\u5957\u63a5\u5b57\u6a21\u5757<\/span><\/li>\n<li><span style=\"color:#7b7f82\">Channel\u63cf\u8ff0\u7b26\u4e8b\u4ef6\u7ba1\u7406\u6a21\u5757<\/span><\/li>\n<li><span style=\"color:#7b7f82\">TcpServer\u8bbe\u7f6e\u7684\u56db\u4e2a\u9636\u6bb5\u56de\u8c03\u51fd\u6570<\/span><\/li>\n<\/ul>\n<p>Connection\u901a\u8fc7\u7ec4\u5408Buffer\u7c7b\u3001Socket\u7c7b\u4ee5\u53caChannel\u7c7b\u5bf9\u8c61&#xff0c;\u8bbe\u8ba1\u51fa\u8fde\u63a5\u7684\u8bfb\u56de\u8c03\u3001\u5199\u56de\u8c03\u3001\u9519\u8bef\u56de\u8c03\u3001\u5173\u95ed\u56de\u8c03\u4ee5\u53ca\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u5e76\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5bf9Channel\u7684\u4e94\u4e2a\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570\u8fdb\u884cbind\u8bbe\u7f6e&#xff0c;\u4ee5\u8fbe\u5230\u6bcf\u4e00\u4e2aConnection\u5bf9\u8c61\u5728\u88ab\u5b9e\u4f8b\u5316\u4e4b\u540e&#xff0c;\u90fd\u6709\u5bf9\u5e94\u7684\u4e8b\u4ef6\u5c31\u7eea\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p>\u5176\u4e2dSocket\u5728\u4e94\u4e2a\u56de\u8c03\u51fd\u6570\u7684\u5b9e\u73b0\u4e2d\u53d1\u6325\u4e86\u4eceTCP\u63a5\u6536\u7f13\u51b2\u533a\u8bfb\u6570\u636e\u3001\u5411TCP\u53d1\u9001\u7f13\u51b2\u533a\u5199\u6570\u636e\u3001\u5173\u95ed\u5957\u63a5\u5b57\u8fd9\u4e09\u4e2a\u4f5c\u7528\u3002<\/p>\n<p>Channel\u53d1\u6325\u4e86\u5f53\u9700\u8981\u5411TCP\u53d1\u9001\u7f13\u51b2\u533a\u5199\u5165\u6570\u636e\u65f6\u76d1\u63a7\u8fde\u63a5\u7684\u5199\u4e8b\u4ef6&#xff0c;\u6570\u636e\u5199\u5b8c\u4e4b\u540e\u53d6\u6d88\u76d1\u63a7\u63cf\u8ff0\u7b26\u7684\u5199\u4e8b\u4ef6&#xff0c;\u4ee5\u53ca\u5f53\u8fde\u63a5\u5173\u95ed\u65f6\u79fb\u9664\u8fde\u63a5\u7684\u4e8b\u4ef6\u76d1\u63a7\u8fd9\u51e0\u4e2a\u4f5c\u7528\u3002<\/p>\n<p>Buffer\u5219\u8d77\u5230\u4e86\u7f13\u51b2\u6570\u636e\u7684\u4f5c\u7528&#xff0c;\u56e0\u4e3a\u4eceTCP\u63a5\u6536\u7f13\u51b2\u533a\u8bfb\u6570\u636e\u65f6\u8bfb\u4e0a\u6765\u7684\u5e76\u4e0d\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u62a5\u6587&#xff0c;\u6240\u4ee5\u6b64\u65f6\u9700\u8981\u7528\u6237\u7f13\u5b58&#xff0c;\u5f53\u53d1\u9001\u6570\u636e\u65f6TCP\u7684\u53d1\u9001\u7f13\u51b2\u533a\u53ef\u80fd\u5df2\u7ecf\u6ee1\u4e86&#xff0c;\u6b64\u65f6\u5199\u6570\u636e\u5c31\u4f1a\u963b\u585e&#xff0c;\u6240\u4ee5\u4e5f\u9700\u8981\u7f13\u51b2\u533a\u7b49\u5f85\u5199\u4e8b\u4ef6\u5c31\u7eea\u3002<\/p>\n<p>\u5e76\u4e14Connection\u7c7b\u4e2d\u8fd8\u6709EventLoop\u6307\u9488\u6307\u5411\u4e00\u4e2aEventLoop&#xff0c;\u6240\u4ee5Channel\u5bf9\u8c61\u5bf9\u5916\u63d0\u4f9b\u8fde\u63a5\u7684\u76d1\u63a7\u63a5\u53e3\u5b9e\u9645\u4e0a\u5c31\u662f\u901a\u8fc7EventLoop\u7ec4\u5408\u7684Poller\u7c7b\u5bf9\u8c61\u5b9e\u73b0\u7684&#xff0c;\u56e0\u4e3a\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u5e94\u4e00\u4e2aEventLoop&#xff0c;\u4e00\u4e2aEventLoop\u5bf9\u5e94\u4e00\u4e2a\u8ba1\u65f6\u5668\u6a21\u5757\u548c\u4e00\u4e2aepoll&#xff0c;\u5982\u679c\u4e00\u4e2a\u8fde\u63a5\u5c5e\u4e8e\u8be5EventLoop&#xff0c;\u5219\u8be5\u8fde\u63a5\u7684\u4e8b\u4ef6\u76d1\u63a7\u5c31\u5e94\u8be5\u88ab\u8bbe\u7f6e\u5728EventLoop\u7684epoll\u4e2d\u3002\u6240\u4ee5Connection\u5bf9\u5916\u63d0\u4f9b\u7684\u4e8b\u4ef6\u76d1\u63a7\u64cd\u4f5c\u3001\u5b9a\u65f6\u4efb\u52a1\u64cd\u4f5c\u63a5\u53e3\u90fd\u662f\u901a\u8fc7EventLoop\u6307\u9488\u95f4\u63a5\u8c03\u7528\u7684Poller\u63a5\u53e3\u3001TimerWheel\u63a5\u53e3&#xff0c;\u4f8b\u5982\u542f\u52a8\u3001\u53d6\u6d88\u975e\u6d3b\u8dc3\u8fde\u63a5\u9500\u6bc1&#xff0c;\u5237\u65b0\u6d3b\u8dc3\u5ea6\u8fd9\u51e0\u4e2a\u63a5\u53e3\u51fd\u6570\u3002<\/p>\n<p>Acceptor\u6a21\u5757\u903b\u8f91\u56fe\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"548\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135255-68025937da70b.png\" width=\"819\" \/><\/p>\n<p><span style=\"color:null\">\u00a0\u5bf9\u4e8eAcceptor\u7c7b\u6765\u8bf4&#xff0c;\u6211\u4eec\u9700\u8981\u5355\u72ec\u4e3a\u5176\u8bbe\u7f6e \u4e00\u4e2a\u8bfb\u56de\u8c03\u51fd\u6570&#xff0c;\u56e0\u4e3a\u8fd9\u662f\u4e00\u4e2a\u76d1\u542c\u8fde\u63a5&#xff0c;\u5e76\u4e0d\u662f\u4e00\u4e2a\u901a\u4fe1\u8fde\u63a5&#xff0c;\u5bf9\u4e8e\u901a\u4fe1\u8fde\u63a5\u6240\u8bbe\u7f6e\u7684\u8bfb\u56de\u8c03\u662f\u5c06TCP\u63a5\u6536\u7f13\u51b2\u533a\u6570\u636e\u62f7\u8d1d\u5230inbuffer\u4e2d&#xff0c;\u800clisten\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u5e94\u8be5\u8c03\u7528accept\u4ece\u5168\u8fde\u63a5\u961f\u5217\u83b7\u53d6\u65b0\u8fde\u63a5&#xff0c;\u4e3a\u65b0\u8fde\u63a5\u521b\u5efastruct file\u7b49\u5185\u6838\u6570\u636e\u7ed3\u6784\u5206\u914d\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u6240\u4ee5Acceptor\u7c7b\u9700\u8981\u7ec4\u5408Channel\u6765\u7ba1\u7406listen\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bfb\u4e8b\u4ef6&#xff0c;\u9700\u8981\u7ec4\u5408Socket\u7c7b\u6765\u8c03\u7528accept\u63a5\u53e3\u3002<\/span><\/p>\n<p><span style=\"color:null\">\u4f46\u662fAcceptor\u6a21\u5757\u672c\u8eab\u5e76\u4e0d\u77e5\u9053\u76d1\u542c\u8fde\u63a5\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u540e\u8be5\u5982\u4f55\u5904\u7406&#xff0c;\u56e0\u6b64\u83b7\u53d6\u4e00\u4e2a\u901a\u4fe1\u8fde\u63a5\u540e&#xff0c;Connection\u5bf9\u8c61\u7684\u521b\u5efa\u4ee5\u53ca\u5404\u4e2a\u9636\u6bb5\u56de\u8c03\u51fd\u6570\u8bbe\u7f6e\u90fd\u662f\u5728\u670d\u52a1\u5668\u6a21\u5757\u8fdb\u884c\u7684&#xff0c;\u6240\u4ee5\u9700\u8981TcpServer\u6a21\u5757\u4f20\u5165OnConnected\u56de\u8c03\u51fd\u6570\u8fdb\u884c\u8bbe\u7f6e\u540e\u7eed\u52a8\u4f5c\u3002<\/span><\/p>\n<p>EventLoop\u6a21\u5757\u903b\u8f91\u56fe\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"796\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135256-6802593817284.png\" width=\"702\" \/><\/p>\n<\/p>\n<p>\u5728\u7b2c\u4e00\u4e2aConnection\u7684\u903b\u8f91\u56fe\u4e2d\u6211\u4eec\u5df2\u7ecf\u8bb2\u5230\u4e86EventLoop\u7684\u7ec4\u5408\u5173\u7cfb&#xff1a;\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u5e94\u4e00\u4e2aEventLoop&#xff0c;\u4e00\u4e2aEventLoop\u5bf9\u5e94\u4e00\u4e2a\u8ba1\u65f6\u5668\u6a21\u5757\u548c\u4e00\u4e2aepoll&#xff0c;\u5404\u4e2a\u4ece\u5c5eReactor\u4e92\u4e0d\u5e72\u6270&#xff0c;\u907f\u514d\u4e86\u56e0\u7ebf\u7a0b\u5b89\u5168\u800c\u4e0d\u5f97\u4e0d\u52a0\u9501&#xff0c;\u4ece\u800c\u5bfc\u81f4\u6574\u4f53\u6548\u7387\u964d\u4f4e\u95ee\u9898\u3002<\/p>\n<p>Channel\u5305\u542b\u4e86EventLoop\u6307\u9488&#xff0c;Connection\u4e5f\u5305\u542b\u4e86EventLoop\u6307\u9488&#xff0c;\u5e76\u4e14Connection\u53c8\u7ec4\u5408\u4e86Channel\u5bf9\u8c61\u7ba1\u7406Connection\u5bf9\u5e94\u8fde\u63a5\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7\u4efb\u52a1&#xff0c;Connection\u53c8\u5bf9Channel\u8bbe\u7f6e\u4e86\u4e94\u4e2a\u56de\u8c03\u51fd\u6570&#xff0c;\u6240\u4ee5Channel\u4e5f\u76f8\u5f53\u4e8e\u56de\u6307\u4e86Connection&#xff0c;\u81f3\u6b64<\/p>\n<p>Channel &#xff1a;Connection &#xff1a;EventLoop &#061; m\u00a0: m\u00a0: n\u00a0 (m &gt;&gt; n)<\/p>\n<p>&#xff08;m\u6307\u901a\u4fe1\u8fde\u63a5\u7684\u6570\u91cf&#xff0c;n\u6307\u4ece\u5c5eReactor\u7684\u6570\u91cf&#xff09;<\/p>\n<h3 id=\"%E5%AE%9E%E7%8E%B0%C2%A0\">\u5b9e\u73b0\u00a0<\/h3>\n<h4 id=\"%E6%97%B6%E9%97%B4%E8%BD%AE%E5%AE%9A%E6%97%B6%E5%99%A8\">TimerWheel &#8212; \u65f6\u95f4\u8f6e\u5b9a\u65f6\u5668<\/h4>\n<h5 id=\"%E5%AE%9A%E6%97%B6%E5%99%A8%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8\">\u5b9a\u65f6\u5668\u7cfb\u7edf\u8c03\u7528<\/h5>\n<p><span style=\"color:#7b7f82\">\u5728\u8fdb\u884c<\/span>TimerWheel <span style=\"color:#7b7f82\">\u6a21\u5757\u4e2d&#xff0c;\u9700\u8981\u7528\u5230\u4e00\u4e2a\u5b9a\u65f6\u5668\u7684\u6982\u5ff5&#xff0c;\u800c\u5728\u4e4b\u524d\u7684\u5185\u5bb9\u4e2d\u6ca1\u6709\u5bf9\u4e8e\u8fd9\u90e8\u5206\u5185\u5bb9\u8fdb\u884c\u603b\u7ed3&#xff0c;\u56e0\u6b64\u8fd9\u91cc\u5bf9\u4e8e\u8fd9\u4e2a\u5b9a\u65f6\u5668\u7684\u5185\u5bb9\u8fdb\u884c\u5b66\u4e60&#xff0c;\u9996\u5148\u8ba4\u8bc6\u4e00\u4e0b\u5b9a\u65f6\u5668\u7684\u76f8\u5173\u63a5\u53e3\u4fe1\u606f&#xff1a;<\/span><\/p>\n<p>#include &lt;sys\/timerfd.h&gt;<br \/>\nint timerfd_create(int clockid, int flags);<br \/>\nint timerfd_settime(int fd, int flags, const struct itimerspec *new_value, struct itimerspec *old_value);<\/p>\n<p>\/\/ \u53c2\u6570\u4e2d\u5305\u542b\u7684\u7ed3\u6784\u4f53<br \/>\nstruct timespec {<br \/>\n    time_t tv_sec;                \/* Seconds *\/<br \/>\n    long   tv_nsec;               \/* Nanoseconds *\/<br \/>\n};<\/p>\n<p>struct itimerspec {<br \/>\n    struct timespec it_interval;  \/* Interval for periodic timer *\/<br \/>\n    struct timespec it_value;     \/* Initial expiration *\/<br \/>\n};<\/p>\n<p>timerfd_create\u51fd\u6570&#xff0c;\u8fd9\u4e2a\u51fd\u6570\u7684\u529f\u80fd\u662f\u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u5668<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u5bf9\u4e8e\u7b2c\u4e00\u4e2a\u53c2\u6570clockid\u6765\u8bf4&#xff0c;\u6709\u4e24\u4e2a\u9009\u9879&#xff1a;<\/span>\n<ul>\n<li><span style=\"color:#7b7f82\">CLOCK_REALTIME&#xff1a;\u8868\u793a\u7684\u662f\u4ee5\u7cfb\u7edf\u7684\u65f6\u95f4\u4e3a\u57fa\u51c6\u503c&#xff0c;\u8fd9\u662f\u4e0d\u51c6\u786e\u7684&#xff0c;\u56e0\u4e3a\u5982\u679c\u7cfb\u7edf\u7684\u65f6\u95f4\u51fa\u73b0\u4e86\u95ee\u9898\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u5176\u4ed6\u7684\u60c5\u51b5\u51fa\u73b0<\/span><\/li>\n<li><span style=\"color:#7b7f82\">CLOCK_MONOTONIC&#xff1a;\u8868\u793a\u7684\u662f\u4ee5\u7cfb\u7edf\u542f\u52a8\u65f6\u95f4\u8fdb\u884c\u9012\u589e\u7684\u4e00\u4e2a\u57fa\u51c6\u503c&#xff0c;\u4e5f\u5c31\u662f\u8bf4\u8fd9\u4e2a\u65f6\u95f4\u662f\u4e0d\u4f1a\u968f\u7740\u7cfb\u7edf\u65f6\u95f4\u7684\u6539\u53d8\u800c\u8fdb\u884c\u6539\u53d8\u7684<\/span><\/li>\n<\/ul>\n<\/li>\n<li><span style=\"color:#7b7f82\">\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662fflag&#xff0c;\u4e5f\u5c31\u662f\u6240\u8c13\u7684\u6807\u8bb0\u4f4d&#xff0c;\u8fd9\u91cc\u6211\u4eec\u9009\u62e9\u662f0\u8868\u793a\u7684\u662f\u963b\u585e\u64cd\u4f5c<\/span><\/li>\n<\/ul>\n<p>\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u56e0\u4e3aLinux\u4e0b\u4e00\u5207\u7686\u6587\u4ef6&#xff0c;\u6240\u4ee5\u5bf9\u4e8e\u8fd9\u4e2a\u51fd\u6570\u6765\u8bf4\u5176\u5b9e\u5c31\u662f\u6253\u5f00\u4e86\u4e00\u4e2a\u6587\u4ef6&#xff0c;\u5bf9\u4e8e\u8fd9\u4e2a\u5b9a\u65f6\u5668\u7684\u64cd\u4f5c\u5c31\u662f\u5bf9\u4e8e\u8fd9\u4e2a\u6587\u4ef6\u7684\u64cd\u4f5c&#xff0c;\u5b9a\u65f6\u5668\u7684\u539f\u7406\u5176\u5b9e\u5c31\u662f\u5728\u5b9a\u65f6\u5668\u7684\u8d85\u65f6\u65f6\u95f4\u4e4b\u540e&#xff0c;\u7cfb\u7edf\u4f1a\u7ed9\u8fd9\u4e2a\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u6587\u4ef6\u5b9a\u65f6\u5668\u5f53\u4e2d\u5199\u5165\u4e00\u4e2a8\u5b57\u8282\u7684\u6570\u636e&#xff0c;\u5f53\u521b\u5efa\u4e86\u8fd9\u4e2a\u5b9a\u65f6\u5668\u4e4b\u540e&#xff0c;\u5047\u8bbe\u5b9a\u65f6\u5668\u4e2d\u521b\u5efa\u7684\u8d85\u65f6\u65f6\u95f4\u662f3\u79d2&#xff0c;\u90a3\u4e48\u5c31\u610f\u5473\u7740\u6bcf3\u79d2\u5c31\u7b97\u662f\u4e00\u6b21\u8d85\u65f6&#xff0c;\u90a3\u4e48\u4ece\u542f\u52a8\u5f00\u59cb&#xff0c;\u6bcf\u96943\u79d2&#xff0c;\u7cfb\u7edf\u5c31\u4f1a\u7ed9\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u6587\u4ef6\u5f53\u4e2d\u5199\u5165\u4e00\u4e2a1&#xff0c;\u8868\u793a\u7684\u662f\u4ece\u4e0a\u4e00\u6b21\u8bfb\u53d6\u5230\u73b0\u5728\u8d85\u65f6\u4e861\u6b21&#xff0c;\u5047\u8bbe\u572830s\u4e4b\u540e\u624d\u8bfb\u53d6\u6570\u636e&#xff0c;\u90a3\u4e48\u4f1a\u8bfb\u4e0a\u6765\u7684\u6570\u636e\u662f10&#xff0c;\u8868\u793a\u7684\u662f\u4ece\u4e0a\u4e00\u6b21\u8bfb\u53d6\u5230\u73b0\u5728\u5b9e\u8df5\u8d85\u51fa\u9650\u5236\u4e8610\u6b21<\/p>\n<p>timerfd_settime\u51fd\u6570&#xff0c;\u542f\u52a8\u5b9a\u65f6\u5668<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7b2c\u4e00\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c&#xff0c;\u8fd9\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u5176\u5b9e\u4e5f\u662f\u521b\u5efa\u7684\u5b9a\u65f6\u5668\u7684\u6807\u8bc6\u7b26<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u7b2c\u4e8c\u4e2a\u6807\u8bb0\u4f4d\u8868\u793a\u7684\u662f\u4f7f\u7528\u7684\u662f\u76f8\u5bf9\u65f6\u95f4&#xff0c;\u9ed8\u8ba4\u7ed90<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u540e\u9762\u7684\u4e24\u4e2a\u53c2\u6570\u4e5f\u5f88\u597d\u7406\u89e3&#xff0c;\u8868\u793a\u7684\u662f\u65b0\u7684\u65f6\u95f4\u548c\u65e7\u7684\u65f6\u95f4&#xff0c;\u4e0d\u9700\u8981\u5c31\u7f6e\u7a7a\u5373\u53ef<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#7b7f82\">\u4e0b\u9762\u5199\u4e00\u4efd\u5b9e\u4f8b\u4ee3\u7801&#xff1a;<\/span><\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/timerfd.h&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u5668<br \/>\n    int timerfd &#061; timerfd_create(CLOCK_MONOTONIC, 0);<\/p>\n<p>    struct itimerspec itm;<br \/>\n    \/\/ \u8bbe\u7f6e\u7b2c\u4e00\u6b21\u8d85\u65f6\u7684\u65f6\u95f4<br \/>\n    itm.it_value.tv_sec &#061; 1;<br \/>\n    itm.it_value.tv_nsec &#061; 0;<br \/>\n    \/\/ \u8bbe\u7f6e\u7b2c\u4e00\u6b21\u8d85\u65f6\u540e&#xff0c;\u6bcf\u9694\u591a\u957f\u65f6\u95f4\u8d85\u65f6\u4e00\u6b21<br \/>\n    itm.it_interval.tv_sec &#061; 1;<br \/>\n    itm.it_interval.tv_nsec &#061; 0;<br \/>\n    timerfd_settime(timerfd, 0, &amp;itm, nullptr);<\/p>\n<p>    while(true)<br \/>\n    {<br \/>\n        uint64_t tmp;<br \/>\n        int ret &#061; read(timerfd, &amp;tmp, sizeof(tmp));<br \/>\n        if(ret &lt; 0)<br \/>\n            return -1;<br \/>\n        cout &lt;&lt; &#034;\u8d85\u65f6\u7684\u6b21\u6570&#xff1a;&#034; &lt;&lt; tmp &lt;&lt; endl;<br \/>\n        sleep(3);<br \/>\n    }<br \/>\n    close(timerfd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50&#xff0c;\u5c31\u662f\u4e00\u4e2a\u5b9a\u65f6\u5668\u7684\u4f7f\u7528\u5b9e\u4f8b&#xff0c;\u501f\u52a9\u8fd9\u4e2a\u4f7f\u7528\u7684\u4f8b\u5b50\u5c31\u53ef\u4ee5\u8fdb\u884c\u5224\u65ad\u51fa\u6bcf\u96941\u79d2\u8d85\u65f6\u5668\u5c31\u4f1a\u8d85\u65f6\u4e00\u6b21&#xff0c;\u7136\u540e\u5411\u6587\u4ef6\u4e2d\u5199\u5165\u4e00\u4e2a1&#xff0c;\u6211\u4eec\u8fd9\u91ccsleep\u4e863\u79d2&#xff0c;\u6240\u4ee5\u540e\u9762\u7684\u8bfb\u53d6\u6570\u636e\u4e2d\u90fd\u662f\u8d85\u65f6\u4e863\u6b21\u00a0\u3002<\/p>\n<h5 id=\"%E6%97%B6%E9%97%B4%E8%BD%AE%E8%AE%BE%E8%AE%A1\">\u65f6\u95f4\u8f6e\u8bbe\u8ba1<\/h5>\n<p>\u5728\u4e0a\u8ff0\u7684\u4f8b\u5b50\u5f53\u4e2d&#xff0c;\u5b58\u5728\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684\u95ee\u9898&#xff0c;\u6bcf\u6b21\u8d85\u65f6\u90fd\u8981\u628a\u6240\u6709\u7684\u8fde\u63a5\u904d\u5386\u4e00\u6b21&#xff0c;\u8fd9\u6837\u7684\u6548\u7387\u662f\u6bd4\u8f83\u5e95\u4e0b\u7684&#xff0c;\u6240\u4ee5\u884d\u751f\u51fa\u4e86\u4e00\u4e2a\u65b0\u7684\u65b9\u6848&#xff0c;\u65f6\u95f4\u8f6e&#xff1a;<\/p>\n<p>\u73b0\u5728\u5b9a\u4e49\u4e00\u4e2a\u4e8c\u7ef4\u52a8\u6001\u6570\u7ec4&#xff08;vector&lt;vector&lt;Task&gt;&gt;&#xff09;&#xff0c;\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u79d2\u9488\u4e0b\u6807&#xff0c;\u79d2\u9488\u6307\u5411\u7684\u662f\u6570\u7ec4\u7684\u8d77\u59cb\u4f4d\u7f6e&#xff0c;\u8fd9\u4e2a\u79d2\u9488\u6bcf\u79d2\u949f\u5411\u540e\u8d70\u4e00\u6b65&#xff0c;\u8d70\u5230\u54ea\u91cc\u5c31\u4ee3\u8868\u54ea\u91cc\u7684\u4efb\u52a1\u9700\u8981\u88ab\u6267\u884c\u4e86&#xff0c;\u90a3\u4e48\u5982\u679c\u60f3\u8981\u5b9a\u4e00\u4e2a3\u79d2\u4e4b\u540e\u7684\u4efb\u52a1&#xff0c;\u90a3\u4e48\u53ea\u9700\u8981\u5c06\u4efb\u52a1\u6dfb\u52a0\u5230\u5f53\u524d\u79d2\u9488\u7684\u540e\u4e09\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e&#xff0c;\u8ba9\u5176\u6bcf\u79d2\u949f\u8d70\u4e00\u683c&#xff0c;\u90a3\u4e48\u8d70\u5230\u5bf9\u5e94\u7684\u4f4d\u7f6e\u5c31\u53ef\u4ee5\u6267\u884c\u5bf9\u5e94\u4f4d\u7f6e\u7684\u4efb\u52a1\u5373\u53ef\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"404\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135256-6802593837e06.png\" width=\"714\" \/><\/p>\n<p>\u4f46\u662f\u65f6\u95f4\u8f6e\u7684\u7c7b\u578b\u8bbe\u8ba1\u4f9d\u65e7\u4e0d\u5408\u9002<\/p>\n<p><span style=\"color:#7b7f82\">\u5982\u679cvector&lt;vector&lt;Task&gt;&gt;\u53ea\u5b58\u653eTask&#xff0c;\u90a3\u4e48\u79d2\u9488\u8d70\u5230\u54ea\u91cc\u5c31\u6267\u884c\u54ea\u91cc\u7684\u4efb\u52a1&#xff0c;\u8fd9\u770b\u8d77\u6765\u5e76\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898&#xff0c;\u4f46\u662f\u5173\u952e\u70b9\u5728\u4e8e\u5982\u4f55\u5237\u65b0\u4efb\u52a1&#xff1f;<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u5982\u679c\u8981\u5c06\u65e7\u7684\u8d85\u65f6\u4efb\u52a1\u201c\u62ff\u51fa\u6765\u201d\u5e76\u6839\u636e\u5f53\u524d\u79d2\u9488\u4f4d\u7f6e\u5411\u540e\u504f\u79fb\u8d85\u65f6\u65f6\u95f4&#xff0c;\u7136\u540e\u63d2\u5165&#xff0c;\u770b\u4f3c\u7b80\u5355\u4f46\u5b9e\u9645\u5f88\u96be\u64cd\u4f5c<\/span><\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u5982\u4f55\u77e5\u9053\u65e7\u7684\u8d85\u65f6\u4efb\u52a1\u5728\u54ea\u91cc&#xff1f;\u6211\u4eec\u662f\u6839\u636e\u79d2\u9488\u7684\u4f4d\u7f6epush_back\u5230\u79d2\u9488\u6240\u5904\u7684\u4e00\u4e2avector\u4e2d&#xff0c;\u800c\u79d2\u9488\u4e00\u76f4\u5728\u79fb\u52a8&#xff08;\u4e0d\u80fd\u523b\u821f\u6c42\u5251\u5427&#xff09;<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u77e5\u9053\u4e86\u5728\u54ea\u4e2avector\u4e2d&#xff0c;\u90a3\u4e48\u600e\u4e48\u627e\u5230\u54ea\u4e2a\u5143\u7d20\u662f\u6211\u4eec\u7684\u5b9a\u65f6\u4efb\u52a1&#xff1f;\u5927\u5bb6\u7c7b\u578b\u90fd\u76f8\u540c&#xff0c;\u90fd\u662f\u4e00\u4e2a\u6ca1\u6709\u53c2\u6570\u7684\u53ef\u8c03\u7528\u5bf9\u8c61<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#7b7f82\">\u5373\u4f7f\u4e0a\u9762\u4e24\u4e2a\u95ee\u9898\u90fd\u80fd\u5b9e\u73b0&#xff0c;\u90a3\u4e5f\u5b9a\u7136\u9700\u8981\u4f7f\u7528\u66f4\u591a\u7684\u5bb9\u5668\u3001\u5185\u5b58\u6765\u8f85\u52a9&#xff0c;\u4f46\u662f\u6211\u4eec\u6709\u4e00\u4e2a\u66f4\u5de7\u5999\u7684\u65b9\u6cd5&#xff1a;\u5b58\u653eshared_ptr\u667a\u80fd\u6307\u9488 \u00a0vector&lt;vector&lt;std::shared_ptr&lt;TimerTask&gt;&gt;&gt;<\/span><\/p>\n<p>\u5b58\u653e\u4e00\u4e2a\u7ba1\u7406\u8d85\u65f6\u4efb\u52a1\u5bf9\u8c61\u8d44\u6e90\u7684shared_ptr\u667a\u80fd\u6307\u9488&#xff0c;\u8fd9\u662f\u56e0\u4e3a\u5982\u679c\u4e00\u4e2a\u975e\u6d3b\u8dc3\u7684\u8fde\u63a5\u5728\u5373\u5c06\u8d85\u65f6\u65f6&#xff0c;\u7a81\u7136\u4e8b\u4ef6\u5c31\u7eea\u4e86&#xff0c;\u90a3\u4e48\u8be5\u8fde\u63a5\u7684\u6d3b\u8dc3\u5ea6\u5e94\u8be5\u5237\u65b0&#xff0c;\u90a3\u4e48\u4e3a\u4e86\u5b9e\u73b0\u5237\u65b0\u529f\u80fd&#xff0c;\u6211\u4eec\u4f7f\u7528\u4e86shared_ptr\u667a\u80fd\u6307\u9488\u7684\u5f15\u7528&#xff0c;\u5982\u679c\u6709\u4e24\u4e2ashared_ptr\u5171\u540c\u7ba1\u7406\u4e00\u4e2a\u8d44\u6e90&#xff0c;\u90a3\u4e48\u53ea\u6709\u4e24\u4e2ashared_ptr\u90fd\u7ed3\u675f\u751f\u547d&#xff0c;\u6700\u7ec8\u624d\u4f1adelete\u7ba1\u7406\u7684\u8d44\u6e90&#xff0c;\u5e76\u8c03\u7528\u7ba1\u7406\u8d44\u6e90\u7684\u6790\u6784\u51fd\u6570\u3002<\/p>\n<p>\u6211\u4eec\u5c06\u8d85\u65f6\u540e\u53d1\u751f\u7684\u4e8b\u4ef6\u653e\u5728\u4e86TimerTask\u7684\u6790\u6784\u51fd\u6570\u4e2d&#xff0c;\u5c06\u4efb\u52a1\u7684\u6267\u884c\u4e0eTimerTask\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u8fdb\u884c\u7ed1\u5b9a&#xff0c;\u4e00\u65e6TimerTask\u751f\u547d\u5468\u671f\u7ed3\u675f&#xff0c;\u90a3\u4e48\u81ea\u52a8\u8c03\u7528\u6790\u6784\u51fd\u6570&#xff0c;\u6240\u4ee5\u5728\u6790\u6784\u7684\u65f6\u5019\u5c31\u4f1a\u8c03\u7528\u6211\u4eec\u7684\u8d85\u65f6\u4e8b\u4ef6&#xff0c;\u6240\u4ee5\u53ef\u4ee5\u5de7\u5999\u7684\u4e0eshared_ptr\u7ed3\u5408&#xff0c;\u5b9e\u73b0\u4efb\u52a1\u7684\u5237\u65b0\u529f\u80fd&#xff0c;\u53ea\u9700\u8981\u5c06\u4e4b\u524d\u7684shared_ptr\u62f7\u8d1d\u4e00\u4e2ashared_ptr&#xff0c;\u518d\u6dfb\u52a0\u5230\u4e00\u4e2a\u65b0\u7684\u8d85\u65f6\u65f6\u95f4\u4e2d&#xff0c;\u90a3\u4e48\u7b2c\u4e00\u4e2ashared_ptr\u9500\u6bc1\u65f6\u7531\u4e8e\u5f15\u7528\u8ba1\u6570\u4e0d\u4e3a0&#xff0c;\u5e76\u4e0d\u4f1a\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<p>\u5173\u952e\u70b9&#xff1a;\u4e3a\u4e86\u80fd\u5728\u4e4b\u540e\u5237\u65b0\u65f6&#xff0c;\u627e\u5230\u4e0a\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4\u7684shared_ptr\u667a\u80fd\u6307\u9488&#xff0c;\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u5bb9\u5668\u6765\u5b58\u50a8\u4e8b\u4ef6\u5bf9\u5e94\u7684shared_ptr&#xff0c;\u8fd9\u91cc\u53c8\u6709\u4e00\u4e2a\u5173\u952e&#xff0c;\u6211\u4eec\u4e0d\u80fd\u5728\u5bb9\u5668\u4e2d\u4f7f\u7528shared_ptr\u505a\u7ba1\u7406&#xff0c;\u56e0\u4e3a\u8fd9\u4f1a\u589e\u52a0\u5f15\u7528\u8ba1\u6570&#xff01;\u6240\u4ee5\u9700\u8981\u4f7f\u7528weak_ptr&#xff0c;weak_ptr\u53ef\u4ee5\u4f7f\u7528\u8d44\u6e90\u4f46\u662f\u4e0d\u4f1a\u589e\u52a0\u5f15\u7528\u8ba1\u6570<\/p>\n<p>\u6700\u540e\u7684clear\u4e5f\u662f\u4e00\u4e2a\u5173\u952e\u70b9&#xff0c;clear\u4f1a\u5220\u9664\u5bb9\u5668\u5185\u6240\u6709\u5143\u7d20&#xff0c;\u5220\u9664\u7684\u539f\u7406\u662fvector\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u8f7d\u51fd\u6570\u5728\u8d4b\u503c\u4e4b\u524d\u4f1adelete\u91ca\u653e\u81ea\u8eab\u7684\u8d44\u6e90&#xff0c;\u8fd9\u5c31\u4f1a\u8c03\u7528\u6570\u636eshared_ptr\u7684\u6790\u6784\u51fd\u6570&#xff08;\u5185\u90e8\u518d\u5224\u65ad&#8211;\u5f15\u7528\u8ba1\u6570\u662f\u5426\u4e3a0&#xff09;&#xff0c;\u4ece\u800cdeleteTimerTask&#xff0c;\u8c03\u7528TimerTask\u7684\u6790\u6784\u51fd\u6570&#xff0c;\u8fdb\u800c\u6267\u884c\u6211\u4eec\u671f\u5f85\u7684\u8d85\u65f6\u6267\u884c\u4efb\u52a1\u3002<\/p>\n<p>\u5b9e\u73b0<\/p>\n<p>TimerTask&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u9700\u8981\u5bf9\u5916\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3&#xff0c;\u7528\u6765\u8bbe\u7f6e\u6790\u6784\u65f6\u5e94\u8be5\u56de\u8c03\u7684\u51fd\u6570\u3002<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u56e0\u4e3aTimerWheel\u4e2dtimers\u54c8\u5e0c\u8868\u8868\u793a\u4e00\u4e2a\u4efb\u52a1\u662f\u5426\u8fd8\u5728\u7b49\u5f85\u8d85\u65f6&#xff0c;\u5982\u679cTimerTask\u8c03\u7528\u4e86\u6790\u6784\u51fd\u6570&#xff0c;\u5c31\u8868\u660e\u6ca1\u6709shared_ptr\u7ba1\u7406TImerTask\u4e86&#xff0c;\u6240\u4ee5\u4e5f\u9700\u8981\u5c06timers\u4e2d\u8bb0\u5f55\u7684\u4fe1\u606f\u5220\u9664&#xff0c;\u6211\u4eec\u5c06\u5220\u9664\u8fd9\u4e00\u64cd\u4f5c\u4e5f\u653e\u5230TImerTask\u7684\u6790\u6784\u51fd\u6570\u4e2d\u5904\u7406\u3002<\/span><\/li>\n<\/ul>\n<p>TimerWheel&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">capacity\u6765\u6307\u660e\u65f6\u95f4\u8f6e\u7684\u5927\u5c0f&#xff0c;\u5373\u6700\u5927\u53ef\u4ee5\u8bbe\u7f6e\u7684\u8d85\u65f6\u65f6\u95f4&#xff0c;\u6211\u4eec\u9ed8\u8ba4\u4e3a60&#xff0c;\u56e0\u4e3a\u4e00\u4e2a\u8fde\u63a5\u4e00\u822c\u6700\u591a\u975e\u6d3b\u8dc360s&#xff0c;\u6211\u4eec\u5c31\u9700\u8981\u91ca\u653e\u8fde\u63a5\u4e86&#xff0c;\u5426\u5219\u5e72\u8017\u7740\u8d44\u6e90\u4e0d\u4f7f\u7528&#xff0c;\u964d\u4f4e\u670d\u52a1\u5668\u6548\u7387\u3002<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4e00\u4e2aEventLoop\u5305\u542b\u4e00\u4e2a\u8ba1\u65f6\u5668\u6a21\u5757&#xff0c;\u6240\u4ee5EventLoop\u5bf9\u5916\u63d0\u4f9b\u7684\u8ba1\u65f6\u5668\u63a5\u53e3\u5b9e\u9645\u4e0a\u662f\u95f4\u63a5\u8c03\u7528\u7684\u8ba1\u65f6\u5668\u6a21\u5757\u7684\u63a5\u53e3<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4e3a\u4e86\u4fdd\u8bc11s\u949f\u6211\u4eec\u7684\u79d2\u9488\u8fd0\u52a8\u4e00\u6b21&#xff0c;\u6211\u4eec\u4f7f\u7528\u5185\u6838\u7684timerfd\u6765\u63a7\u5236&#xff0c;\u76d1\u63a7timerfd\u7684\u8bfb\u4e8b\u4ef6<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4e3a\u4e86\u7ba1\u7406timerfd\u6211\u4eec\u9700\u8981\u4e00\u4e2achannel\u5bf9\u8c61<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5728\u6784\u9020\u51fd\u6570\u4e2d&#xff0c;\u76f4\u63a5bind\u8bbe\u7f6etimerfd\u7684\u8bfb\u56de\u8c03\u4e3a\u7c7b\u5185\u7684OnTime\u6210\u5458\u51fd\u6570&#xff0c;\u5e76\u76d1\u63a7\u5b83\u7684\u8bfb\u4e8b\u4ef6<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u6211\u4eec\u5728TcpServer\u4e2d\u4f1a\u4f7f\u7528id\u552f\u4e00\u6807\u8bc6\u4e00\u4e2a\u8fde\u63a5&#xff0c;\u6240\u4ee5\u6211\u4eec\u5728\u5b9a\u65f6\u5668\u4f7f\u7528\u65f6\u53ea\u9700\u8981\u4f20\u5165\u8be5\u8fde\u63a5\u7684id\u3001\u5ef6\u8fdf\u65f6\u95f4\u4ee5\u53ca\u8d85\u65f6\u4efb\u52a1\u5373\u53ef<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5bf9\u5916\u63d0\u4f9b\u5bf9conn_id\u7684\u6dfb\u52a0\u8d85\u65f6\u4efb\u52a1\u3001\u5237\u65b0\u8d85\u65f6\u4efb\u52a1\u3001\u5220\u9664\u8d85\u65f6\u4efb\u52a1\u3001\u5224\u65ad\u8d85\u65f6\u4efb\u52a1\u662f\u5426\u5b58\u5728\u56db\u4e2a\u63a5\u53e3<\/span><\/li>\n<\/ul>\n<p>\u5982\u4f55\u5b9e\u73b01s\u949f\u6211\u4eec\u7684\u6307\u9488\u540e\u79fb\u4e00\u4f4d&#xff0c;\u4e5f\u5c31\u662f\u6267\u884c\u4e00\u6b21Run\u51fd\u6570&#xff1f;<\/p>\n<p>\u663e\u7136\u4e0d\u80fd\u5199\u4e00\u4e2a\u6b7b\u5faa\u73af&#xff0c;Run\u4e4b\u540esleep 1s\u3002\u8fd9\u4e00\u65b9\u9762\u4f1a\u963b\u585e\u6267\u884c\u6d41&#xff0c;\u53e6\u4e00\u65b9\u9762\u5e76\u4e0d\u80fd\u4fdd\u8bc1\u5176\u4ed6\u64cd\u4f5c\u4ee5\u53caRun\u8017\u65f6\u4e3a0\u3002<\/p>\n<p>\u8fd9\u91cc\u5c31\u9700\u8981\u4f7f\u7528\u5230\u6211\u4eec\u4e0a\u9762\u8bb2\u89e3\u7684 tiemrfd&#xff0c;\u6211\u4eec\u5c06create\u4e00\u4e2atimerfd&#xff0c;\u8bbe\u7f6e\u5185\u6838\u6bcf1s\u5411timerfd\u5199\u5165\u4e00\u4e2a8\u5b57\u8282\u5927\u5c0f\u76841&#xff0c;\u5e76\u901a\u8fc7channel\u5bf9\u8c61\u56de\u6307\u7684EventLoop&#xff0c;\u95f4\u63a5\u8c03\u7528EventLoop\u4e2d\u7684Poller\u63a5\u53e3&#xff0c;\u5c06tiemrfd\u7684\u8bfb\u4e8b\u4ef6\u653e\u5230epoll\u4e2d\u8fdb\u884c\u76d1\u63a7&#xff0c;\u90a3\u4e48\u4e00\u65e6timerfd\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u4e86&#xff0c;\u5c31\u8868\u660e\u8ddd\u79bb\u4e0a\u4e00\u6b21\u5df2\u7ecf\u8fc7\u53bb\u4e861s\u6216\u591as&#xff0c;\u4f1a\u8fc7\u53bb\u591as\u8fd9\u662f\u56e0\u4e3a\u4e00\u4e2aEventLoop\u7684epoll\u4f1a\u76d1\u63a7\u5927\u91cf\u4e8b\u4ef6&#xff08;\u4e00\u822c\u4e00\u4e2a\u4ece\u5c5eReactor\u80fd\u652f\u63011w &#8211; 10w\u7684\u5e76\u53d1\u91cf&#xff09;&#xff0c;\u6240\u4ee5timerfd\u7684\u5c31\u7eea\u4e8b\u4ef6\u53ef\u80fd\u5728\u5f88\u9760\u540e\u7684\u4f4d\u7f6e&#xff0c;\u90a3\u4e48\u4ece\u5c5eReactor\u6267\u884c\u524d\u9762\u7684\u5c31\u7eea\u4e8b\u4ef6\u5e76\u8fdb\u884c\u4e1a\u52a1\u5904\u7406\u53ef\u80fd\u4f1a\u8017\u65f6\u591a\u79d2&#xff0c;\u4f46\u662f\u6ca1\u6709\u5173\u7cfb&#xff0c;\u56e0\u4e3a\u5185\u6838\u6bcf1s\u90fd\u4f1a\u5411timerfd\u5199\u5165\u4e00\u4e2a8\u5b57\u8282\u76841&#xff0c;\u6240\u4ee5\u53ea\u8981\u5728timerfd\u7684\u8bfb\u56de\u8c03\u4e2d\u6839\u636e\u8bfb\u5230\u7684timer\u6b21\u6570&#xff0c;\u6765\u51b3\u5b9a\u79d2\u9488\u79fb\u52a8\u51e0\u6b65\u5373\u53ef\u3002<\/p>\n<p>using TaskFunc &#061; std::function&lt;void()&gt;;<br \/>\nusing ReleaseFunc &#061; std::function&lt;void()&gt;;<br \/>\n\/\/ \u6bcf\u4e00\u4e2a\u8d85\u65f6\u4efb\u52a1<br \/>\nclass TimerTask<br \/>\n{<br \/>\nprivate:<br \/>\n    uint64_t _id;           \/\/ \u6807\u8bc6\u4efb\u52a1id<br \/>\n    uint32_t _timeout;      \/\/ \u4efb\u52a1\u9700\u8981\u8bbe\u7f6e\u7684\u5ef6\u8fdf\u4e8b\u4ef6<br \/>\n    TaskFunc _task_cb;      \/\/ \u5f53\u8d85\u65f6\u540e\u9700\u8981\u505a\u7684\u4efb\u52a1&#xff0c;void()\u7c7b\u578b\u51fd\u6570&#xff0c;\u6240\u4ee5\u53c2\u6570\u7531\u7528\u6237\u81ea\u5df1bind\u4f20\u5165<br \/>\n    ReleaseFunc _release;   \/\/ \u5f53\u8be5\u4efb\u52a1\u88ab\u6267\u884c\u65f6&#xff0c;\u9700\u8981\u5c06TimerWheel\u4e2d\u7684_timers\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u6211\u8fd9\u4e2a\u4efb\u52a1&#xff0c;\u8868\u660e\u6211\u4efb\u52a1\u5b8c\u6210\u4e86&#xff0c;\u4e0d\u5b58\u5728\u4e86<br \/>\n    bool _iscancel;         \/\/ \u662f\u5426\u53d6\u6d88\u4e86\u8be5\u4efb\u52a1&#xff0c;false \u8868\u793a\u4efb\u52a1\u6ca1\u6709\u88ab\u53d6\u6d88&#xff0c;true\u8868\u793a\u4efb\u52a1\u88ab\u53d6\u6d88\u4e86<br \/>\npublic:<br \/>\n    TimerTask(uint64_t id, uint32_t timeout, const TaskFunc &amp;cb) : _id(id), _timeout(timeout), _task_cb(cb), _iscancel(false)<br \/>\n    {}<br \/>\n    ~TimerTask()<br \/>\n    {<br \/>\n        if (_iscancel &#061;&#061; false)<br \/>\n            _task_cb();<br \/>\n        _release();<br \/>\n    }<br \/>\n    \/\/ \u5bf9\u5916\u63d0\u4f9b\u8bbe\u7f6erelease\u56de\u8c03\u51fd\u6570\u7684\u63a5\u53e3<br \/>\n    void SetRelease(const ReleaseFunc&amp; cb) { _release &#061; cb;}<br \/>\n    void Cancel() { _iscancel &#061; true; }<br \/>\n    uint32_t GetTimeout() { return _timeout; }<br \/>\n};<\/p>\n<p>\/\/ shared_ptr\u5c31\u662f\u8bbe\u8ba1\u7cbe\u9ad3&#xff0c;\u53ef\u4ee5\u8ba9\u8ba1\u65f6\u7684\u4e8b\u4ef6\u5237\u65b0<br \/>\nclass TimerWheel<br \/>\n{<br \/>\nprivate:<br \/>\n    int _tick;                                                      \/\/ \u79d2\u9488<br \/>\n    int _capacity;                                                  \/\/ \u63a7\u5236\u65f6\u95f4\u8f6e\u7684\u957f\u5ea6&#xff0c;\u79d2\u5355\u4f4d<br \/>\n    std::vector&lt;std::vector&lt;std::shared_ptr&lt;TimerTask&gt;&gt;&gt; _wheel;    \/\/ \u65f6\u95f4\u8f6e<br \/>\n    std::unordered_map&lt;uint64_t, std::weak_ptr&lt;TimerTask&gt;&gt; _timers; \/\/ \u7528\u6765\u5bfb\u627e\u4e4b\u524d\u7684shared_ptr&#xff0c;\u4f46\u662f\u9700\u8981\u7528weak_ptr\u63a5\u6536&#xff0c;\u4e0d\u80fd\u8ba9_timers\u53c2\u4e0e\u5f15\u7528\u8ba1\u6570<\/p>\n<p>    int _timerfd;                               \/\/ \u5b9a\u65f6\u5668\u63cf\u8ff0\u7b26<br \/>\n    EventLoop* _loop;                           \/\/ \u56de\u6307\u5230EventLoop&#xff0c;\u4f7f\u7528EventLoop\u7684\u63a5\u53e3\u8bbe\u7f6e\u76d1\u542c\u4e8b\u4ef6<br \/>\n    std::unique_ptr&lt;Channel&gt; _timer_channel;    \/\/ \u7ba1\u7406timerfd\u7684\u8fde\u63a5\u4e8b\u4ef6<br \/>\nprivate:<br \/>\n    void TimerErase(uint64_t id)<br \/>\n    {<br \/>\n        auto it &#061; _timers.find(id);<br \/>\n        if (it &#061;&#061; _timers.end())<br \/>\n            return;<br \/>\n        _timers.erase(it);<br \/>\n    }<br \/>\n    static int CreateTimerfd()<br \/>\n    {<br \/>\n        \/\/ \u521b\u5efa<br \/>\n        int timerfd &#061; timerfd_create(CLOCK_MONOTONIC, 0);<br \/>\n        if (timerfd &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;timerfd create error!&#034;);<br \/>\n            abort();<br \/>\n        }<br \/>\n        struct itimerspec itime;<br \/>\n        \/\/ \u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4<br \/>\n        itime.it_value.tv_sec &#061; 1;<br \/>\n        itime.it_value.tv_nsec &#061; 0;<br \/>\n        \/\/ \u8bbe\u7f6e\u8d85\u65f6\u540e\u7684\u4e0b\u4e00\u6b21\u8d85\u65f6\u65f6\u95f4<br \/>\n        itime.it_interval.tv_sec &#061; 1;<br \/>\n        itime.it_interval.tv_nsec &#061; 0;<br \/>\n        \/\/ \u8bbe\u7f6e\u8fdb\u5185\u6838<br \/>\n        timerfd_settime(timerfd, 0, &amp;itime, nullptr);<\/p>\n<p>        return timerfd;<br \/>\n    }<br \/>\n    \/\/ \u5f53timerfd\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u4e86&#xff0c;\u5c31\u628a\u6570\u636e\u5835\u4e0a\u6765&#xff0c;\u4e0d\u7136\u7684\u8bddepoll\u4f1a\u4e00\u76f4\u5c31\u7eea<br \/>\n    int ReadTimerfd()<br \/>\n    {<br \/>\n        uint64_t times;<br \/>\n        int ret &#061; read(_timerfd, &amp;times, sizeof(times));<br \/>\n        if (ret &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;timerfd read error!&#034;);<br \/>\n            abort();<br \/>\n        }<br \/>\n        return times;<br \/>\n    }<br \/>\n    \/\/ \u5c06\u79d2\u9488\u6bcf\u79d2\u52a0\u4e00&#xff0c;\u5982\u679c\u6709\u4efb\u52a1\u5c31\u6267\u884c&#xff0c;\u5b9e\u9645\u52a8\u4f5c\u5c31\u662fclear<br \/>\n    void Run()<br \/>\n    {<br \/>\n        _wheel[_tick].clear();              \/\/ \u91ca\u653e\u7a7a\u95f4\u610f\u5473\u7740shared_ptr\u88ab\u8c03\u7528\u6790\u6784\u51fd\u6570&#xff0c;\u542f\u52a8\u5bf9\u5e94\u4efb\u52a1<br \/>\n        _tick &#061; (_tick &#043; 1) % _capacity;    \/\/ \u79d2\u9488\u5f80\u540e\u8d70<br \/>\n    }<br \/>\n    \/\/ 1s\u65f6\u95f4\u5230\u55bd&#xff01;\u5185\u6838\u4f1a\u5411timerfd\u5199\u5165\u4e00\u4e2a1&#xff0c;\u6240\u4ee5\u6b64\u65f6epoll\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u8fdb\u884c\u79d2\u9488 &#043; 1<br \/>\n    void OnTime()<br \/>\n    {<br \/>\n        \/\/ \u6bcf\u6b21\u65f6\u95f4\u5230\u4e86&#xff0c;\u628a\u6570\u636e\u8bfb\u4e00\u4e0b&#xff0c;\u5982\u679c\u4e0d\u8bfb\u7684\u8bdd&#xff0c;epoll\u4f1a\u4e00\u76f4\u5c31\u7eea<br \/>\n        int times &#061; ReadTimerfd();<br \/>\n        \/\/ \u7136\u540e\u518d\u6267\u884c\u8d85\u65f6\u4efb\u52a1&#xff0c;\u79d2\u9488\u540e\u79fb<br \/>\n\/*<br \/>\n   \u56e0\u4e3a\u6709\u53ef\u80fd\u4e00\u4e2a\u8fde\u63a5\u7684\u4e1a\u52a1\u5904\u7406\u65f6\u95f4\u5f88\u957f&#xff0c;\u4f8b\u598230s&#xff0c;\u90a3\u4e48\u5728\u8fd930s\u5185\u79d2\u9488\u5e94\u8be5\u8d7030\u6b21&#xff0c;\u4f46\u662f<br \/>\n   \u7531\u4e8e\u4ece\u5c5e\u7ebf\u7a0b\u4e00\u76f4\u5728\u6267\u884c\u4e1a\u52a1\u5904\u7406&#xff0c;\u800c\u6ca1\u7a7a\u53bb\u8bfb\u53d6timerfd&#xff0c;\u6240\u4ee5\u5f53\u771f\u6b63\u79d2\u9488\u5728\u79fb\u52a8\u5e76\u6267\u884c<br \/>\n   \u8d85\u65f6\u4efb\u52a1\u65f6&#xff0c;\u9700\u8981\u77e5\u9053\u5185\u6838\u901a\u77e5\u4e86\u51e0\u6b21&#xff0c;\u901a\u77e5\u4e86\u51e0\u6b21\u5c31\u8868\u660e\u8fc7\u53bb\u4e86\u51e0\u79d2&#xff0c;\u4e5f\u5c31\u610f\u5473\u7740Run\u51e0\u6b21<br \/>\n*\/<br \/>\n        for (int i &#061; 0; i &lt; times; i&#043;&#043;)<br \/>\n        {<br \/>\n            Run();<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u5411\u65f6\u95f4\u8f6e\u4e2d\u6dfb\u52a0\u8d85\u65f6\u4e8b\u4ef6<br \/>\n    void AddTimerTaskInLoop(uint64_t id, uint32_t delay, const TaskFunc&amp; cb)<br \/>\n    {<br \/>\n        std::shared_ptr&lt;TimerTask&gt; pt(new TimerTask(id, delay, cb));<br \/>\n        pt-&gt;SetRelease(std::bind(&amp;TimerWheel::TimerErase, this, id));<br \/>\n        int pos &#061; (_tick &#043; delay) % _capacity;<\/p>\n<p>        _wheel[pos].push_back(pt);<br \/>\n        _timers[id] &#061; std::weak_ptr&lt;TimerTask&gt;(pt);<br \/>\n    }<br \/>\n    \/\/ \u5237\u65b0\u8d85\u65f6\u65f6\u95f4<br \/>\n    void ReflushTimerTaskInLoop(uint64_t id)<br \/>\n    {<br \/>\n        \/\/ \u5148\u5224\u65ad\u4e4b\u524d\u5728_timers\u4e2d\u6709\u6ca1\u6709\u8bb0\u5f55<br \/>\n        auto it &#061; _timers.find(id);<br \/>\n        if (it &#061;&#061; _timers.end())<br \/>\n            return;<br \/>\n        \/\/ \u6709\u8bb0\u5f55\u5219\u83b7\u53d6\u4e0a\u4e00\u6b21\u8ba1\u65f6\u4efb\u52a1\u7684shared_ptr<br \/>\n        std::shared_ptr&lt;TimerTask&gt; pt &#061; it-&gt;second.lock();     \/\/ \u83b7\u53d6\u54c8\u5e0c\u8868\u4e2d\u5b58\u653e\u7684weak_ptr&#xff0c;\u4f7f\u7528lock\u51fd\u6570\u83b7\u53d6weak_ptr\u5171\u4eab\u7684shared_ptr\u5bf9\u8c61<br \/>\n        \/\/ \u66f4\u65b0\u8d85\u65f6\u65f6\u95f4&#xff0c;\u653e\u5230\u65b0\u7684\u65f6\u95f4\u8f6e\u5185<br \/>\n        int pos &#061; (_tick &#043; pt-&gt;GetTimeout()) % _capacity;<br \/>\n        _wheel[pos].push_back(pt);<br \/>\n    }<br \/>\n    \/* \u53d6\u6d88\u4e00\u4e2aTimerTask<br \/>\n    \u6ce8\u610f&#xff1a;\u4e0d\u80fd\u76f4\u63a5\u4ece\u65f6\u95f4\u8f6e\u4e2d\u5220\u9664&#xff0c;\u56e0\u4e3a\u5220\u9664\u64cd\u4f5c\u5c31\u610f\u5473\u7740shared_ptr\u81ea\u52a8\u8c03\u7528\u6790\u6784\u51fd\u6570&#xff0c;\u5c31\u626d\u66f2\u4e86\u6211\u4eec\u7684\u610f\u613f&#xff0c;\u53d8\u4e3a\u4e86\u63d0\u524d\u6267\u884c\u8d85\u65f6\u65f6\u95f4&#xff0c;\u800c\u4e0d\u662f\u53d6\u6d88\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4<br \/>\n    \u6240\u4ee5&#xff0c;\u6211\u4eec\u8981\u5728TimerTask\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5b57\u6bb5iscancel&#xff0c;\u8868\u660e\u8be5\u4e8b\u4ef6\u662f\u5426\u88ab\u53d6\u6d88\u4e86&#xff0c;\u5982\u679c\u88ab\u53d6\u6d88\u4e86&#xff0c;\u5c31\u4e0d\u8981\u5728\u6790\u6784\u51fd\u6570\u4e2d\u6267\u884c\u65b9\u6cd5*\/<br \/>\n    void CancelTimerTaskInLoop(uint64_t id)<br \/>\n    {<br \/>\n        auto it &#061; _timers.find(id);<br \/>\n        if (it &#061;&#061; _timers.end()) return;<br \/>\n        std::shared_ptr&lt;TimerTask&gt; pt &#061; it-&gt;second.lock();<br \/>\n        if (pt) pt-&gt;Cancel();<br \/>\n    }<br \/>\npublic:<br \/>\n    TimerWheel(EventLoop* loop)<br \/>\n        : _tick(0)<br \/>\n        , _capacity(60)<br \/>\n        , _wheel(_capacity)<br \/>\n        , _loop(loop)<br \/>\n        , _timerfd(CreateTimerfd())<br \/>\n        , _timer_channel(new Channel(_loop, _timerfd))<br \/>\n    {<br \/>\n        \/\/ \u5c06tiemrfd\u52a0\u5165\u4e8b\u4ef6\u76d1\u542c&#xff0c;\u56e0\u4e3a\u5185\u6838\u6bcf1\u79d2\u949f\u4f7ftimerfd\u8d85\u65f6\u4e00\u6b21&#xff0c;\u5373\u4e8b\u4ef6\u5c31\u7eea\u4e00\u6b21&#xff0c;\u6240\u4ee5\u5c31\u8c03\u7528\u6211\u4eec\u7ed1\u5b9a\u7684\u56de\u8c03\u51fd\u6570OnTimer&#xff0c;\u6267\u884c\u6240\u6709\u8d85\u65f6\u4efb\u52a1\u5e76\u5c06\u79d2\u9488&#043;1<br \/>\n        _timer_channel-&gt;SetReadCallback(std::bind(&amp;TimerWheel::OnTime, this));<br \/>\n        _timer_channel-&gt;EnableRead();<br \/>\n    }<\/p>\n<p>    \/*\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u64cd\u4f5c\u6210\u5458timers\u54c8\u5e0c\u8868&#xff0c;\u90a3\u4e48\u4e00\u5b9a\u4f1a\u5f15\u53d1\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff0c;\u6211\u4eec\u5f53\u7136\u53ef\u4ee5\u52a0\u9501&#xff0c;\u4f46\u662f\u52a0\u9501\u4f1a\u5bfc\u81f4\u6548\u7387\u964d\u4f4e&#xff0c;\u6211\u4eec\u6b64\u65f6\u91c7\u7528\u7a7a\u95f4\u6362\u65f6\u95f4\u7684\u65b9\u5f0f&#xff0c;<br \/>\n    \u4e5f\u5c06\u5b9a\u65f6\u5668\u7684\u6240\u6709\u64cd\u4f5c\u4efb\u52a1\u90fd\u653e\u5230\u540c\u4e00\u4e2aEventLoop\u7684\u4efb\u52a1\u961f\u5217\u4e2d&#xff0c;\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u4e32\u884c\u6267\u884c\u4efb\u52a1\u4e0d\u4f1a\u5f15\u53d1\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898*\/<br \/>\n    \/*\u56e0\u4e3a\u4e0b\u9762\u4e09\u4e2a\u51fd\u6570\u7528\u5230\u4e86_loop\u7684\u6210\u5458\u51fd\u6570&#xff0c;\u6240\u4ee5\u9700\u8981\u5728EventLoop\u7c7b\u7684\u4e0b\u9762\u8fdb\u884c\u7c7b\u5916\u5b9a\u4e49*\/<br \/>\n    void AddTimerTask(uint64_t id, uint32_t delay, const TaskFunc&amp; cb);<br \/>\n    void ReflushTimerTask(uint64_t id);<br \/>\n    void CancelTimerTask(uint64_t id);<\/p>\n<p>    \/\/ \u8be5\u5b9a\u65f6\u4efb\u52a1\u662f\u5426\u5b58\u5728<br \/>\n    bool HasTimer(uint64_t id)<br \/>\n    {<br \/>\n        auto it &#061; _timers.find(id);<br \/>\n        if (it &#061;&#061; _timers.end())<br \/>\n            return false;<br \/>\n        return true;<br \/>\n    }<br \/>\n}; <\/p>\n<h4 id=\"%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F\">\u901a\u7528\u7c7b\u578bAny\u00a0<\/h4>\n<p>\u5bf9\u4e8eConnection\u6765\u8bf4&#xff0c;\u5b83\u7684\u5de5\u4f5c\u4efb\u52a1\u4e4b\u4e00\u662f\u8981\u5bf9\u4e8e\u8fde\u63a5\u8fdb\u884c\u7ba1\u7406&#xff0c;\u90a3\u4e48\u5c31\u610f\u5473\u7740\u8fd9\u4e2a\u6a21\u5757\u662f\u4f1a\u6d89\u53ca\u5230\u5bf9\u4e8e\u5e94\u7528\u5c42\u534f\u8bae\u7684\u5904\u7406\u7684&#xff0c;\u56e0\u6b64\u5728Connection\u4e2d\u8981\u8bbe\u7f6e\u534f\u8bae\u5904\u7406\u7684\u4e0a\u4e0b\u6587\u6765\u63a7\u5236\u5904\u7406\u8282\u594f<\/p>\n<p>\u5e94\u7528\u5c42\u7684\u534f\u8bae\u662f\u6709\u5f88\u591a\u7684&#xff0c;\u5e73\u65f6\u4f7f\u7528\u6700\u591a\u7684\u662fhttp\u534f\u8bae&#xff0c;\u4e0d\u8fc7\u4e5f\u4f1a\u6709\u4f8b\u5982ftp\u534f\u8bae\u8fd9\u6837\u7684\u5b58\u5728&#xff0c;\u800c\u4e3a\u4e86\u4f7f\u5f97\u672c\u9879\u76ee\u53ef\u4ee5\u652f\u6301\u7684\u534f\u8bae\u8db3\u591f\u591a&#xff0c;\u90a3\u4e48\u5c31\u610f\u5473\u7740\u4e0d\u80fd\u56fa\u5b9a\u5199\u6b7b\u7c7b\u578b&#xff0c;\u800c\u662f\u53ef\u4ee5\u5b58\u50a8\u4efb\u610f\u534f\u8bae\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f&#xff0c;\u56e0\u6b64\u5c31\u9700\u8981\u8bbe\u8ba1\u4e00\u4e2a\u901a\u7528\u7c7b\u578b\u6765\u4fdd\u5b58\u5404\u79cd\u4e0d\u540c\u7684\u6570\u636e<\/p>\n<p>\u6211\u4eec\u60f3\u8981\u505a\u6210\u7684\u6548\u679c\u662f&#xff0c;\u8fd9\u4e2aAny\u7c7b&#xff0c;\u53ef\u4ee5\u63a5\u53d7\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e&#xff0c;\u4f8b\u5982\u6709\u8fd9\u6837\u7684\u7528\u6cd5&#xff1a;<\/p>\n<p>Any a;<br \/>\na &#061; 10;<br \/>\na &#061; &#034;abc&#034;;<br \/>\na &#061; 12.34;<br \/>\n&#8230;<\/p>\n<p>\u90a3\u8be5\u5982\u4f55\u8bbe\u8ba1\u8fd9\u4e2a\u901a\u7528\u7c7b\u578bAny&#xff1f;<\/p>\n<p><span style=\"color:#7b7f82\">\u8fd9\u91cc\u53c2\u8003\u4e86\u4e00\u79cd\u5d4c\u5957\u7c7b\u578b&#xff0c;\u5728\u4e00\u4e2a\u7c7b\u4e2d\u5d4c\u5957\u5b58\u5728\u4e00\u4e2a\u65b0\u7684\u7c7b&#xff0c;\u5728\u8fd9\u4e2a\u7c7b\u4e2d\u5b58\u5728\u6a21\u677f&#xff0c;\u800c\u8fdb\u800c\u5bf9\u4e8e\u7c7b\u8fdb\u884c\u5904\u7406\u00a0\u00a0<\/span><\/p>\n<p>class Any<br \/>\n{<br \/>\nprivate:<br \/>\n    class holder<br \/>\n    {<br \/>\n        \/\/ &#8230;<br \/>\n    };<br \/>\n    template &lt;class T&gt;<br \/>\n    class placeholder : public holder<br \/>\n    {<br \/>\n        T _val;<br \/>\n    };<br \/>\n    holder *_content;<br \/>\n};<\/p>\n<p><span style=\"color:#7b7f82\">\u5728\u8fd9\u4e2aAny\u7c7b\u4e2d&#xff0c;\u6210\u5458\u4fdd\u5b58\u7684\u662fholder\u7c7b\u7684\u6307\u9488&#xff0c;\u5f53Any\u7c7b\u5bb9\u5668\u9700\u8981\u4fdd\u5b58\u4e00\u4e2a\u6570\u636e\u7684\u65f6\u5019&#xff0c;\u53ea\u9700\u8981\u901a\u8fc7placeholder\u5b50\u7c7b\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7279\u5b9a\u7c7b\u578b\u7684\u5b50\u7c7b\u5bf9\u8c61\u51fa\u6765&#xff0c;\u8ba9\u8fd9\u4e2a\u5b50\u7c7b\u5bf9\u8c61\u4fdd\u5b58\u6570\u636e\u5373\u53ef&#xff0c;\u5177\u4f53\u539f\u7406\u4e3a\u591a\u6001&#xff1a;<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"357\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135256-6802593850740.png\" width=\"787\" \/><\/p>\n<p><span style=\"color:#7b7f82\">Any\u4fdd\u5b58\u7684\u662f\u4e00\u4e2a\u7236\u7c7b\u6307\u9488&#xff0c;\u90a3\u4e48\u5b50\u7c7b\u91cd\u5199\u7236\u7c7b\u7684\u865a\u51fd\u6570&#xff0c;\u901a\u8fc7\u7236\u7c7b\u6307\u9488\u8c03\u7528\u865a\u51fd\u6570\u65f6\u5c31\u4f1a\u5b9e\u73b0\u52a8\u6001\u7684\u591a\u6001&#xff0c;\u8fd9\u5c31\u662fAny\u7684\u539f\u7406\u3002\u00a0<\/span><\/p>\n<p>\u5b9e\u73b0\u00a0<\/p>\n<p>\u00a0\u6ce8\u610f&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u62f7\u8d1d\u6784\u9020\u3001\u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u8f7d\u7684\u73b0\u4ee3\u5199\u6cd5<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u62f7\u8d1d\u65f6\u9700\u8981\u501f\u52a9placeholder\u63d0\u4f9b\u7684clone\u65b9\u6cd5&#xff0c;\u56e0\u4e3a\u62f7\u8d1d\u6784\u9020\u7684\u5176\u5b9e\u662fplaceholder<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5bf9\u5916\u63d0\u4f9bget\u65b9\u6cd5&#xff0c;\u83b7\u53d6placeholder\u7684\u5bf9\u8c61&#xff0c;\u5373\u534f\u8bae\u4e0a\u4e0b\u6587<\/span><\/li>\n<\/ul>\n<p>class Any<br \/>\n{<br \/>\nprivate:<br \/>\n    class holder<br \/>\n    {<br \/>\n        public:<br \/>\n            virtual ~holder() {}<br \/>\n            virtual const std::type_info&amp; type() &#061; 0;<br \/>\n            virtual holder *clone() &#061; 0;<br \/>\n    };<br \/>\n    template&lt;class T&gt;<br \/>\n    class placeholder: public holder<br \/>\n    {<br \/>\n        public:<br \/>\n            placeholder(const T &amp;val): _val(val) {}<br \/>\n            \/\/ \u83b7\u53d6\u5b50\u7c7b\u5bf9\u8c61\u4fdd\u5b58\u7684\u6570\u636e\u7c7b\u578b<br \/>\n            virtual const std::type_info&amp; type() { return typeid(T); }<br \/>\n            \/\/ \u9488\u5bf9\u5f53\u524d\u7684\u5bf9\u8c61\u81ea\u8eab&#xff0c;\u514b\u9686\u51fa\u4e00\u4e2a\u65b0\u7684\u5b50\u7c7b\u5bf9\u8c61<br \/>\n            virtual holder *clone() { return new placeholder(_val); }<br \/>\n        public:<br \/>\n            T _val;<br \/>\n    };<br \/>\n    holder *_content;<br \/>\npublic:<br \/>\n    Any():_content(NULL) {}<br \/>\n    template&lt;class T&gt;<br \/>\n    Any(const T &amp;val):_content(new placeholder&lt;T&gt;(val)) {}<br \/>\n    Any(const Any &amp;other):_content(other._content ? other._content-&gt;clone() : nullptr) {}<br \/>\n    ~Any() { delete _content; }<\/p>\n<p>    Any &amp;swap(Any &amp;other)<br \/>\n    {<br \/>\n        std::swap(_content, other._content);<br \/>\n        return *this;<br \/>\n    }<\/p>\n<p>    \/\/ \u8fd4\u56de\u5b50\u7c7b\u5bf9\u8c61\u4fdd\u5b58\u7684\u6570\u636e\u7684\u6307\u9488<br \/>\n    template&lt;class T&gt;<br \/>\n    T *get()<br \/>\n    {<br \/>\n        \/\/\u60f3\u8981\u83b7\u53d6\u7684\u6570\u636e\u7c7b\u578b&#xff0c;\u5fc5\u987b\u548c\u4fdd\u5b58\u7684\u6570\u636e\u7c7b\u578b\u4e00\u81f4<br \/>\n        assert(typeid(T) &#061;&#061; _content-&gt;type());<br \/>\n        return &amp;((placeholder&lt;T&gt;*)_content)-&gt;_val;<br \/>\n    }<br \/>\n    \/\/\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d\u51fd\u6570<br \/>\n    template&lt;class T&gt;<br \/>\n    Any&amp; operator&#061;(const T &amp;val)<br \/>\n    {<br \/>\n        \/\/\u4e3aval\u6784\u9020\u4e00\u4e2a\u4e34\u65f6\u7684\u901a\u7528\u5bb9\u5668&#xff0c;\u7136\u540e\u4e0e\u5f53\u524d\u5bb9\u5668\u81ea\u8eab\u8fdb\u884c\u6307\u9488\u4ea4\u6362&#xff0c;\u4e34\u65f6\u5bf9\u8c61\u91ca\u653e\u7684\u65f6\u5019&#xff0c;\u539f\u5148\u4fdd\u5b58\u7684\u6570\u636e\u4e5f\u5c31\u88ab\u91ca\u653e<br \/>\n        Any(val).swap(*this);<br \/>\n        return *this;<br \/>\n    }<br \/>\n    Any&amp; operator&#061;(const Any &amp;other)<br \/>\n    {<br \/>\n        Any(other).swap(*this);<br \/>\n        return *this;<br \/>\n    }<br \/>\n}; <\/p>\n<p>\u6d4b\u8bd5&#xff1a;\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"584\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135256-680259387448a.png\" width=\"635\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"133\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135256-680259388f94e.png\" width=\"390\" \/><\/p>\n<h4 id=\"Buffer\" style=\"background-color:transparent\">Buffer<\/h4>\n<p><span style=\"color:#7b7f82\">\u4f5c\u4e3a\u7f13\u51b2\u533a\u6a21\u5757&#xff0c;Buffer\u662f\u5b9e\u73b0\u901a\u4fe1\u7528\u6237\u7684\u63a5\u6536\u7f13\u51b2\u533a\u548c\u53d1\u9001\u7f13\u51b2\u533a\u57df\u7684\u529f\u80fd\u3002\u6211\u4eec\u91c7\u7528\u7684\u662fvector\u5bb9\u5668\u6765\u5b9e\u73b0Buffer&#xff0c;\u5e76\u6ca1\u6709\u91c7\u7528stirng&#xff0c;\u56e0\u4e3a\u6211\u4eec\u4f1a\u9891\u7e41\u7684\u5728Buffer\u4e2d\u5b58\u653e\u6570\u636e&#xff0c;\u53d6\u51fa\u6570\u636e&#xff0c;\u5982\u679c\u4f7f\u7528string\u7684substr&#xff0c;\u90a3\u4e48\u5c31\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u6570\u7ec4\u79fb\u52a8&#xff0c;\u8fd9\u662f\u964d\u4f4e\u6548\u7387\u7684&#xff0c;\u867d\u7136vector\u6dfb\u52a0\u6216\u5220\u9664\u4e00\u90e8\u5206\u6570\u636e\u4e5f\u4f1a\u53d1\u751f\u5927\u91cf\u7684\u6570\u7ec4\u5143\u7d20\u79fb\u52a8\u6784&#xff0c;\u4f46\u662f\u6211\u4eec\u4f1a\u4f7f\u7528\u8bfb\u3001\u5199\u4e0b\u6807\u7ef4\u62a4\u4e00\u4e2a\u53ef\u8bfb\u7a7a\u95f4&#xff0c;\u53d6\u51fa\u6570\u636e\u662f\u53ea\u9700\u8981\u79fb\u52a8\u8bfb\u4e0b\u6807\u5373\u53ef&#xff0c;string\u66f4\u504f\u5411\u4e8e\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c&#xff0c;\u800c\u5bf9\u4e8e\u7f13\u51b2\u533a\u6765\u8bf4\u7528\u6570\u7ec4\u6765\u5b9e\u73b0\u66f4\u4e3a\u5408\u9002\u3002<\/span><\/p>\n<p>Buffer\u8d1f\u8d23\u4e24\u4e2a\u529f\u80fd&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u5199\u5165\u6570\u636e&#xff0c;\u4ece\u5199\u4e0b\u6807\u5f00\u59cb&#xff0c;\u5c06\u9700\u8981\u5199\u5165\u8f93\u5165\u7f13\u51b2\u533a\u7684\u6570\u636e\u62f7\u8d1d\u5230\u5199\u4e0b\u6807\u5f00\u59cb\u7684\u7a7a\u95f4\u4e2d<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u8bfb\u53d6\u6570\u636e&#xff0c;\u4ece\u8bfb\u4e0b\u6807\u5f00\u59cb&#xff0c;\u5c06\u7528\u6237\u9700\u8981\u7684\u6307\u5b9a\u957f\u5ea6\u7684\u6570\u636e\u8fd4\u56de&#xff0c;\u524d\u63d0\u662f\u8bfb\u4e0b\u6807\u4e0d\u80fd\u8d85\u8fc7\u5199\u4e0b\u6807<\/span><\/li>\n<\/ul>\n<p>\u6210\u5458\u53d8\u91cf&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">vector<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u8bfb\u4e0b\u6807<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5199\u4e0b\u6807<\/span><\/li>\n<\/ul>\n<p>\u5bf9\u5916\u63a5\u53e3&#xff1a;<\/p>\n<p><span style=\"color:#7b7f82\">\u56e0\u4e3a\u6211\u4eec\u5bf9\u5df2\u7ecf\u65e0\u6548\u7684\u6570\u636e\u5e76\u4e0d\u91c7\u53d6\u5220\u9664\u64cd\u4f5c&#xff0c;\u5f53\u6709\u65b0\u6570\u636e\u5230\u6765\u540e\u76f4\u63a5\u8986\u76d6\u5199\u5165\u5373\u53ef&#xff0c;\u6240\u4ee5\u6211\u4eec\u7ef4\u62a4\u7684\u4e24\u4e2a\u4e0b\u6807\u4f1a\u5728vector\u4e2d\u5212\u5206\u51fa\u4e09\u4e2a\u533a\u57df&#xff1a;<\/span><\/p>\n<p>[0, read_pos)\u00a0 [read_pos, write_pos)\u00a0 [write_pos, vector.size() &#8211; 1] <\/p>\n<p><span style=\"color:#7b7f82\">\u4ece\u7f13\u51b2\u533a\u8bfb\u53d6\u6570\u636e\u65f6\u4ece\u8bfb\u4e0b\u6807\u5f00\u59cb\u8bfb&#xff0c;\u5411\u7f13\u51b2\u533a\u5199\u6570\u636e\u65f6\u4ece\u5199\u4e0b\u6807\u5f00\u59cb\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u5411\u7f13\u51b2\u533a\u5199\u6570\u636e\u65f6&#xff0c;\u5c3e\u90e8\u7684\u7a7a\u95f2\u7a7a\u95f4\u53ef\u80fd\u4e0d\u8db3&#xff0c;\u6b64\u65f6\u9700\u8981\u5224\u65ad\u5c3e\u90e8\u7a7a\u95f2\u7a7a\u95f4\u52a0\u4e0a\u5934\u90e8\u7684\u7a7a\u95f2\u7a7a\u95f4\u662f\u5426\u6ee1\u8db3\u5199\u5165\u7684\u6570\u636e\u5927\u5c0f\u9700\u6c42&#xff0c;\u5982\u679c\u6ee1\u8db3&#xff0c;\u5219\u5c06\u8bfb\u4e0b\u6807\u5230\u5199\u4e0b\u6807\u5185\u7684\u6570\u636e\u79fb\u52a8\u81f30\u4e0b\u6807&#xff0c;\u7136\u540e\u5199\u5165\u6570\u636e\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u5982\u679c\u5934\u90e8\u7a7a\u95f4\u7a7a\u95f4&#043;\u5c3e\u90e8\u7a7a\u95f2\u7a7a\u95f4\u4e0d\u80fd\u6ee1\u8db3\u5199\u5165\u6570\u636e\u7684\u5927\u5c0f&#xff0c;\u5219\u4e0d\u79fb\u52a8\u6570\u636e&#xff0c;\u5b57\u8282resize\u6269\u5bb9vector&#xff0c;\u8fd9\u6837\u5c31\u907f\u514d\u4e86\u9891\u7e41\u79fb\u52a8\u6570\u7ec4\u5143\u7d20\u7684\u6548\u7387\u95ee\u9898\u3002<\/span><\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u83b7\u53d6\u5f53\u524d\u5199\u4f4d\u7f6e\u5730\u5740\u4ee5\u53caconst\u7248\u672c<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u83b7\u53d6\u5f53\u524d\u8bfb\u4f4d\u7f6e\u5730\u5740\u4ee5\u53caconst\u7248\u672c<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5c3e\u90e8\u7a7a\u95f2\u7a7a\u95f4\u5927\u5c0f<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5934\u90e8\u7a7a\u95f2\u7a7a\u95f4\u5927\u5c0f<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u83b7\u53d6\u200b\u200b\u53ef\u5199\u6570\u636e\u5927\u5c0f<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u83b7\u53d6\u53ef\u8bfb\u6570\u636e\u5927\u5c0f<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u8bfb\u4e0b\u6807\u540e\u79fb<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5199\u4e0b\u6807\u540e\u79fb<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u6269\u5bb9\u51fd\u6570<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4ece\u7f13\u51b2\u533a\u8bfb\u6570\u636e<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5411\u7f13\u51b2\u533a\u5199\u6570\u636e<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u83b7\u53d6\u4e00\u6574\u884c\u6570\u636e\u3002\u65b9\u4fbf\u540e\u671f\u5904\u7406\u534f\u8bae<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u6e05\u7406\u529f\u80fd<\/span><\/li>\n<\/ul>\n<p>\u5b9e\u73b0<\/p>\n<p>class Buffer<br \/>\n{<br \/>\n    static const int default_size &#061; 1024;<\/p>\n<p>private:<br \/>\n    std::vector&lt;char&gt; _buffer; \/\/ \u7f13\u51b2\u533a<br \/>\n    uint64_t _write_pos;       \/\/ \u5199\u4e0b\u6807<br \/>\n    uint64_t _read_pos;        \/\/ \u8bfb\u4e0b\u6807<\/p>\n<p>public:<br \/>\n    Buffer() : _buffer(default_size), _write_pos(0), _read_pos(0) {}<br \/>\n    void Clear() { _write_pos &#061; _read_pos &#061; 0; }<br \/>\n    \/\/ \u8fd4\u56de\u5199\u4e0b\u6807\u7a7a\u95f4\u5730\u5740<br \/>\n    char *WritePostion() { return &amp;*(_buffer.begin() &#043; _write_pos); }<br \/>\n    \/\/ \u8fd4\u56de\u8bfb\u4e0b\u6807\u7a7a\u95f4\u5730\u5740<br \/>\n    char *ReadPosition() { return &amp;*(_buffer.begin() &#043; _read_pos); }<br \/>\n    \/\/ const\u7248\u672c\u8fd4\u56de\u5199\u4e0b\u6807\u7a7a\u95f4\u5730\u5740<br \/>\n    const char *WritePosition() const { return &amp;*(_buffer.begin() &#043; _write_pos); }<br \/>\n    \/\/ const\u7248\u672c\u8fd4\u56de\u8bfb\u4e0b\u6807\u7a7a\u95f4\u5730\u5740<br \/>\n    const char *ReadPosition() const { return &amp;*(_buffer.begin() &#043; _read_pos); }<br \/>\n    \/\/ \u5c3e\u90e8\u7a7a\u95f2\u5927\u5c0f<br \/>\n    uint64_t TailSize() const { return _buffer.size() &#8211; _write_pos; }<br \/>\n    \/\/ \u5934\u90e8\u7a7a\u95f2\u7a7a\u95f4<br \/>\n    uint64_t HeadSize() const { return _read_pos; }<br \/>\n    \/\/ \u83b7\u53d6\u53ef\u5199\u6570\u636e\u5927\u5c0f<br \/>\n    uint64_t WriteAbleSize() const { return TailSize() &#043; HeadSize(); }<br \/>\n    \/\/ \u83b7\u53d6\u53ef\u8bfb\u6570\u636e\u5927\u5c0f<br \/>\n    uint64_t ReadAbleSize() const { return _write_pos &#8211; _read_pos; }<br \/>\n    \/\/ \u8bfb\u4e0b\u6807\u540e\u79fb<br \/>\n    void MoveReadPos(uint64_t len)<br \/>\n    {<br \/>\n        \/\/ \u8fd9\u91cc\u53ea\u8fdb\u884c\u8bfb\u4e0b\u6807\u79fb\u52a8&#xff0c;\u81f3\u4e8e\u7a7a\u95f4\u662f\u5426\u8db3\u591f\u8ba9Read\u51fd\u6570\u81ea\u884c\u5224\u65ad<br \/>\n        assert(len &lt;&#061; ReadAbleSize());<br \/>\n        _read_pos &#043;&#061; len;<br \/>\n    }<br \/>\n    \/\/ \u5199\u4e0b\u6807\u540e\u79fb<br \/>\n    void MoveWritePos(uint64_t len)<br \/>\n    {<br \/>\n        \/\/ \u8fd9\u91cc\u53ea\u8fdb\u884c\u5199\u4e0b\u6807\u79fb\u52a8&#xff0c;\u81f3\u4e8e\u7a7a\u95f4\u662f\u5426\u8db3\u591f\u8ba9Read\u51fd\u6570\u81ea\u884c\u5224\u65ad&#xff0c;\u7a0b\u5e8f\u5230\u4e86\u8fd9\u91cc\u7a7a\u95f4\u5fc5\u7136\u8db3\u591f<br \/>\n        assert(len &lt;&#061; TailSize());<br \/>\n        _write_pos &#043;&#061; len;<br \/>\n    }<br \/>\n    \/\/ \u6269\u5bb9\u51fd\u6570<br \/>\n    void Reserve(uint64_t len)<br \/>\n    {<br \/>\n        if (len &lt;&#061; TailSize())<br \/>\n            return;<br \/>\n        else if (len &lt;&#061; WriteAbleSize())<br \/>\n        {<br \/>\n            uint64_t size &#061; ReadAbleSize();<br \/>\n            \/\/ \u5c06\u6240\u6709\u6570\u636e\u5411\u524d\u79fb\u52a8<br \/>\n            std::copy(_buffer.begin() &#043; _read_pos, _buffer.begin() &#043; _write_pos, _buffer.begin());<br \/>\n            _read_pos &#061; 0;<br \/>\n            _write_pos &#061; size;<br \/>\n        }<br \/>\n        else<br \/>\n            _buffer.resize(_write_pos &#043; len);<br \/>\n    }<br \/>\n    \/\/ \u4ece\u7f13\u51b2\u533a\u8bfb\u6570\u636e<br \/>\n    void Read(void *buf, uint64_t len)<br \/>\n    {<br \/>\n        \/\/ \u5224\u65ad\u662f\u5426\u6709\u8db3\u591f\u6570\u636e\u53ef\u8bfb<br \/>\n        assert(len &lt;&#061; ReadAbleSize());<br \/>\n        std::copy(ReadPosition(), ReadPosition() &#043; len, static_cast&lt;char*&gt;(buf));<br \/>\n        \/\/ \u66f4\u65b0\u8bfb\u4e0b\u6807<br \/>\n        MoveReadPos(len);<br \/>\n    }<br \/>\n    \/\/ \u91cd\u8f7dRead<br \/>\n    std::string Read(uint64_t len)<br \/>\n    {<br \/>\n        assert(len &lt;&#061; ReadAbleSize());<br \/>\n        std::string str(len, &#039;\\\\0&#039;);<br \/>\n        \/\/ \u6ce8\u610f&#xff1a;\u4e0d\u80fd\u76f4\u63a5\u7528c_str()&#xff0c;\u56e0\u4e3a\u8fd9\u662fconst char*&#xff0c;\u4e0d\u5141\u8bb8\u901a\u8fc7\u6307\u9488\u4fee\u6539\u5185\u5bb9<br \/>\n        Read(&amp;str[0], len);<br \/>\n        \/\/ std::cout &lt;&lt; &#034;&#043;&#043;&#043;&#043;&#043;&#043;&#043;&#043;&#034; &lt;&lt; str &lt;&lt; std::endl;<br \/>\n        return str;<br \/>\n    }<br \/>\n    \/\/ \u83b7\u53d6\u4e00\u6574\u884c\u6570\u636e<br \/>\n    std::string Getline()<br \/>\n    {<br \/>\n        std::string tmp;<br \/>\n        uint64_t i &#061; _read_pos;<br \/>\n        for (; i &lt; _write_pos; &#043;&#043;i)<br \/>\n        {<br \/>\n            if (_buffer[i] !&#061; &#039;\\\\n&#039;) tmp &#043;&#061; _buffer[i];<br \/>\n            else break;<br \/>\n        }<br \/>\n        \/\/ \u627e\u4e0d\u5230\\\\n&#xff0c;\u8868\u660e\u6ca1\u6709\u5b8c\u6574\u7684\u4e00\u884c\u6570\u636e&#xff0c;\u5219\u8fd4\u56de\u7a7a<br \/>\n        if (i &#061;&#061; _write_pos)<br \/>\n            return &#034;&#034;;<br \/>\n        \/\/ \u5426\u5219\u8fd4\u56de\u4e00\u6574\u884c\u6570\u636e&#xff0c;\u5305\u62ec\\\\n<br \/>\n        tmp &#043;&#061; _buffer[i&#043;&#043;];<br \/>\n        MoveReadPos(i &#8211; _read_pos);<br \/>\n        return tmp;<\/p>\n<p>    }<br \/>\n    \/\/ \u5411\u7f13\u51b2\u533a\u5199\u6570\u636e<br \/>\n    void Write(const void *data, uint64_t len)<br \/>\n    {<br \/>\n        if (len &#061;&#061; 0) return;<br \/>\n        if (!data) return;<br \/>\n        Reserve(len);<br \/>\n        std::copy(static_cast&lt;const char*&gt;(data), static_cast&lt;const char*&gt;(data) &#043; len, WritePostion());<br \/>\n        MoveWritePos(len);<br \/>\n    }<br \/>\n    \/\/ \u91cd\u8f7dstring\u7c7b\u578b\u7684write\u51fd\u6570<br \/>\n    void Write(const std::string &amp;str)<br \/>\n    {<br \/>\n        Write(str.c_str(), str.size());<br \/>\n    }<br \/>\n    \/\/ \u91cd\u8f7d\u672c\u7c7b\u7c7b\u578b\u7684write\u51fd\u6570<br \/>\n    void Write(const Buffer &amp;buffer)<br \/>\n    {<br \/>\n        Write(buffer.ReadPosition(), buffer.ReadAbleSize());<br \/>\n    }<br \/>\n}; <\/p>\n<h4 id=\"Socket\">Socket<\/h4>\n<p>\u5bf9socket\u7cfb\u7edf\u8c03\u7528\u7684\u5c01\u88c5\u6a21\u5757&#xff0c;\u6ca1\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684&#xff0c;\u6211\u4eec\u5df2\u7ecf\u5728\u7f51\u7edc\u90e8\u5206\u8054\u7cfb\u5f88\u591a\u6b21\u4e86<\/p>\n<\/p>\n<p>\u00a0\u5bf9\u5916\u63a5\u53e3&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">Create\u521b\u5efa\u5957\u63a5\u5b57<\/span><\/li>\n<li><span style=\"color:#7b7f82\">Bind\u7ed1\u5b9a\u5957\u63a5\u5b57<\/span><\/li>\n<li><span style=\"color:#7b7f82\">Listen\u76d1\u542c\u5957\u63a5\u5b57<\/span><\/li>\n<li><span style=\"color:#7b7f82\">Connect\u5ba2\u6237\u7aef\u53d1\u8d77\u8fde\u63a5<\/span><\/li>\n<li><span style=\"color:#7b7f82\">Accept\u670d\u52a1\u7aef\u4ece\u5168\u8fde\u63a5\u961f\u5217\u83b7\u53d6\u65b0\u8fde\u63a5<\/span><\/li>\n<li><span style=\"color:#7b7f82\">Recv\u4eceTCP\u7684\u63a5\u6536\u7f13\u51b2\u533a\u8bfb\u53d6\u6570\u636e<\/span><\/li>\n<li><span style=\"color:#7b7f82\">Send\u5411TCP\u7684\u53d1\u9001\u7f13\u51b2\u533a\u5199\u5165\u6570\u636e<\/span><\/li>\n<li><span style=\"color:#7b7f82\">CreateServer\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u7aef\u8fde\u63a5<\/span><\/li>\n<li><span style=\"color:#7b7f82\">CreateClient\u521b\u5efa\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5<\/span><\/li>\n<li><span style=\"color:#7b7f82\">NonBlock\u8bbe\u7f6e\u5957\u63a5\u5b57\u975e\u963b\u585e<\/span><\/li>\n<li><span style=\"color:#7b7f82\">ReuseAddr\u8bbe\u7f6e\u7aef\u53e3\u590d\u7528&#xff0c;\u907f\u514d\u56e0\u670d\u52a1\u5668\u5d29\u6e83&#xff0c;\u5bfc\u81f4\u670d\u52a1\u5668\u662f\u5148\u6325\u624b\u7684\u4e00\u65b9&#xff0c;\u4ece\u800c\u8fdb\u5165TimeWait\u72b6\u6001&#xff0c;\u6b64\u65f6\u4f1a\u4fdd\u7559TCP\u8fde\u63a5\u7684\u56db\u5143\u7ec4&#xff0c;\u7ef4\u6301\u4e24\u4e2aMSL\u65f6\u95f4&#xff0c;\u5982\u679c\u7acb\u5373\u91cd\u542f\u670d\u52a1\u5668\u8fdb\u7a0b\u4f1a\u5bfc\u81f4\u7aef\u53e3\u7ed1\u5b9a\u5931\u8d25&#xff0c;\u6240\u4ee5\u6211\u4eec\u8bbe\u7f6e\u7aef\u53e3\u590d\u7528&#xff0c;\u53ef\u4ee5\u4f7f\u670d\u52a1\u5668\u8fdb\u7a0b\u9000\u51fa\u540e\u4f9d\u7136\u80fd\u7acb\u5373\u91cd\u542f<\/span><\/li>\n<\/ul>\n<p>\u5b9e\u73b0<\/p>\n<p>class Socket<br \/>\n{<br \/>\n    \/\/ \u5168\u8fde\u63a5\u961f\u5217\u6700\u5927\u503c<br \/>\n    static const int backlog &#061; 1024;<br \/>\nprivate:<br \/>\n    int _sockfd;    \/\/ \u5ba2\u6237\u7aef&#xff1a;\u8fde\u63a5\u5957\u63a5\u5b57\u3002   \u670d\u52a1\u7aef&#xff1a;\u76d1\u542c\u5957\u63a5\u5b57<br \/>\npublic:<br \/>\n    Socket() : _sockfd(-1) {}<br \/>\n    Socket(int sockfd) : _sockfd(sockfd) {}<br \/>\n    ~Socket() { Close(); }<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    int Fd() { return _sockfd; }<br \/>\n    \/\/ \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n    bool Create()<br \/>\n    {<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 Error!&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u670d\u52a1\u7aefbind\u5957\u63a5\u5b57&#xff0c;\u5ba2\u6237\u7aef\u4e0d\u9700\u8981\u624b\u52a8bind&#xff0c;\u907f\u514dapp\u4e4b\u95f4\u7aef\u53e3\u51b2\u7a81&#xff0c;\u4ee5\u53ca\u9632\u6b62app\u975e\u6cd5\u7ed1\u5b9a\u591a\u4e2a\u7aef\u53e3\u53f7<br \/>\n    bool Bind(const std::string&amp; ip, uint16_t port)<br \/>\n    {<br \/>\n        struct sockaddr_in local;<br \/>\n        memset(&amp;local, 0, sizeof(local));<br \/>\n        local.sin_family &#061; AF_INET;<br \/>\n        local.sin_port &#061; htons(port);<br \/>\n        local.sin_addr.s_addr &#061; inet_addr(ip.c_str());<br \/>\n        socklen_t addrlen &#061; sizeof(local);<br \/>\n        if (bind(_sockfd, reinterpret_cast&lt;const struct sockaddr*&gt;(&amp;local), addrlen) &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;Bind socket Error!&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u670d\u52a1\u5668\u5f00\u542f\u5957\u63a5\u5b57\u76d1\u542c\u72b6\u6001&#xff0c;\u76d1\u542c\u662f\u5426\u6709\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u6210\u529f\u5219\u653e\u5165\u5168\u8fde\u63a5\u961f\u5217<br \/>\n    bool Listen()<br \/>\n    {<br \/>\n        if (listen(_sockfd, backlog) &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;Listen socket Error!&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u670d\u52a1\u7aef\u4ece\u5168\u8fde\u63a5\u961f\u5217\u53d6\u5efa\u7acb\u6210\u529f\u7684\u8fde\u63a5<br \/>\n    int Accept()<br \/>\n    {<br \/>\n        int newfd &#061; accept(_sockfd, nullptr, nullptr);<br \/>\n        if (newfd &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;Accept Error!&#034;);<br \/>\n            return -1;<br \/>\n        }<br \/>\n        return newfd;<br \/>\n    }<br \/>\n    \/\/ \u5ba2\u6237\u7aef\u5411 ip:port \u53d1\u8d77TCP\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5185\u6838\u81ea\u52a8\u5b8c\u6210\u4e09\u6b21\u63e1\u624b<br \/>\n    bool Connect(const std::string ip, uint16_t port)<br \/>\n    {<br \/>\n        struct sockaddr_in addr;<br \/>\n        memset(&amp;addr, 0, sizeof(addr));<\/p>\n<p>        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(addr);<\/p>\n<p>        int n &#061; connect(_sockfd, reinterpret_cast&lt;const struct sockaddr*&gt;(&amp;addr), len);<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;Connect Error!&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u63a5\u6536\u6570\u636e\u3002\u540c\u6837\u8981\u6307\u660efd<br \/>\n    ssize_t Recv(void* buf, size_t len, int flag &#061; 0)<br \/>\n    {<br \/>\n        ssize_t n &#061; recv(_sockfd, buf, len, flag);<br \/>\n        if (n &lt;&#061; 0)<br \/>\n        {<br \/>\n            \/\/ n &#061;&#061; 0\u8868\u793a\u5199\u7aef\u5173\u95ed&#xff0c;n &lt; 0\u5219\u9519\u8bef\u7801\u88ab\u8bbe\u7f6e<br \/>\n            if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK || errno &#061;&#061; EINTR)<br \/>\n                return 0;<br \/>\n            ERR_LOG(&#034;socket recv error!&#034;);<br \/>\n            return -1;<br \/>\n        }<br \/>\n        return n;<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\u3002\u8fd9\u91cc\u6709\u95ee\u9898&#xff1a;\u5982\u679c\u662f\u670d\u52a1\u7aef&#xff0c;\u8981\u6307\u660e\u5957\u63a5\u5b57fd&#xff01;<br \/>\n    ssize_t Send(const void* buf, size_t len, int flag &#061; 0)<br \/>\n    {<br \/>\n        ssize_t n &#061; send(_sockfd, buf, len, flag);<br \/>\n        if (n &lt;&#061; 0)<br \/>\n        {<br \/>\n            if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK || errno &#061;&#061; EINTR)<br \/>\n                return 0;<br \/>\n            ERR_LOG(&#034;socket send error!&#034;);<br \/>\n            return -1;<br \/>\n        }<\/p>\n<p>        return n;<br \/>\n    }<br \/>\n    ssize_t NonBlockSend(const void* buf, size_t len)<br \/>\n    {<br \/>\n        if (len &#061;&#061; 0) return 0;<br \/>\n        return Send(buf, len, MSG_DONTWAIT);<br \/>\n    }<br \/>\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u7aef\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. \u521b\u5efa\u5957\u63a5\u5b57 2. \u8bbe\u7f6e\u5730\u5740\u3001\u7aef\u53e3\u590d\u7528  3. \u7ed1\u5b9a\u5957\u63a5\u5b57  4. \u76d1\u542c\u5957\u63a5\u5b57<br \/>\n        if (!Create() || !ReuseAddr() || !Bind(ip, port) || !Listen())<br \/>\n            return false;<br \/>\n        \/\/ \u662f\u5426\u8bbe\u7f6e\u5957\u63a5\u5b57\u975e\u963b\u585e&#xff0c;\u5373Reactor\u7684ET\u6216LT\u6a21\u5f0f<br \/>\n        if (block_flag) NonBlock();<\/p>\n<p>        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, bool block_flag &#061; false)<br \/>\n    {<br \/>\n        if (!Create() || !Connect(ip, port))<br \/>\n            return false;<br \/>\n        if (block_flag) NonBlock();<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u8bbe\u7f6e\u5957\u63a5\u5b57\u4e3a\u975e\u963b\u585e<br \/>\n    bool NonBlock()<br \/>\n    {<br \/>\n        int flag &#061; fcntl(_sockfd, F_GETFL);<br \/>\n        if (flag &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;SetNonBlock Error!&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        fcntl(_sockfd, F_SETFL, flag | O_NONBLOCK);<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u8bbe\u7f6e\u7aef\u53e3\u590d\u7528&#xff0c;\u907f\u514d\u56e0\u670d\u52a1\u7aef\u4e3b\u52a8\u65ad\u5f00\u8fde\u63a5\u8fdb\u5165TIME_WAIT\u72b6\u6001\u800c\u5728\u77ed\u65f6\u95f4\u5185\u65e0\u6cd5\u542f\u52a8\u670d\u52a1<br \/>\n    bool ReuseAddr()<br \/>\n    {<br \/>\n        int opt &#061; 1;<br \/>\n        if (setsockopt(_sockfd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &amp;opt, sizeof(opt)) &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;Setsockopt Error!&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n}; <\/p>\n<p>\u00a0<\/p>\n<p>\u6d4b\u8bd5<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"427\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135256-68025938a112a.png\" width=\"553\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"443\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135256-68025938b9a83.png\" width=\"553\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"245\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135256-68025938d2bd2.png\" width=\"972\" \/><\/p>\n<\/p>\n<h4 id=\"Channel\" style=\"background-color:transparent\">Channel<\/h4>\n<p><span style=\"color:#7b7f82\">\u6211\u4eec\u5728\u9879\u76ee\u89c4\u5212\u4e2d\u5df2\u7ecf\u8bb2\u89e3&#xff0c;Channel\u662f\u7528\u6765\u8d1f\u8d23\u5bf9\u4e00\u4e2a\u63cf\u8ff0\u7b26IO\u4e8b\u4ef6\u7ba1\u7406\u7684\u6a21\u5757&#xff0c;\u5b9e\u73b0\u5bf9\u63cf\u8ff0\u7b26\u53ef\u8bfb&#xff0c;\u53ef\u5199&#xff0c;\u9519\u8bef&#8230;\u4e8b\u4ef6\u7684\u7ba1\u7406\u64cd\u4f5c&#xff0c;\u4ee5\u53caPoller\u6a21\u5757\u5bf9\u63cf\u8ff0\u7b26\u8fdb\u2f8fIO\u4e8b\u4ef6\u76d1\u63a7\u5c31\u7eea\u540e&#xff0c;\u6839\u636e\u4e0d\u540c\u7684\u4e8b\u4ef6&#xff0c;\u56de\u8c03\u4e0d\u540c\u7684\u5904\u7406\u51fd\u6570\u529f\u80fd\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u5f53epoll\u4e2d\u4e8b\u4ef6\u5c31\u7eea\u4e86&#xff0c;\u5c31\u6267\u884cChannel\u4e2d\u63d0\u4f9b\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570&#xff0c;\u6839\u636e\u76f8\u5e94\u7684\u5c31\u7eea\u4e8b\u4ef6\u6765\u8c03\u7528\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u5bf9\u5916\u63a5\u53e3&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u76d1\u63a7\u8bfb\u4e8b\u4ef6<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u76d1\u63a7\u5199\u4e8b\u4ef6<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5173\u95ed\u8bfb\u4e8b\u4ef6\u76d1\u63a7<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5173\u95ed\u5199\u4e8b\u4ef6\u76d1\u63a7<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5728epoll\u4e2d\u53d6\u6d88fd\u7684\u4e8b\u4ef6\u76d1\u63a7<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u8bbe\u7f6e\u4e94\u4e2a\u56de\u8c03\u51fd\u6570<\/span><\/li>\n<li><span style=\"color:#7b7f82\">HandlerEvent\u4e8b\u4ef6\u5c31\u7eea\u5904\u7406\u51fd\u6570<\/span><\/li>\n<\/ul>\n<p>\u5b9e\u73b0<\/p>\n<p>\u7ec6\u8282&#xff1a;<span style=\"color:#7b7f82\">\u4e00\u4e2a\u8fde\u63a5\u53ea\u8981\u89e6\u53d1\u4e86\u5c31\u7eea\u4e8b\u4ef6&#xff0c;\u90a3\u4e48\u6211\u4eec\u5c31\u5e94\u8be5\u8c03\u7528\u4e00\u6b21\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570&#xff0c;\u5728\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570\u4e2d&#xff0c;\u6211\u4eec\u4f1a\u4e3a\u8be5\u8fde\u63a5\u5237\u65b0\u6d3b\u8dc3\u5ea6&#xff0c;\u5e76\u8c03\u7528\u4e0a\u5c42\u8bbe\u7f6e\u7684\u9636\u6bb5\u56de\u8c03\u51fd\u6570&#xff08;\u56e0\u4e3a\u53ef\u80fd\u4e0a\u5c42\u4e5f\u9700\u8981\u8fd9\u4e2a\u4e1a\u52a1\u573a\u666f&#xff09;<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u90a3\u4e48\u5982\u679c fd \u5bf9\u5e94\u7684\u8fde\u63a5\u51fa\u73b0\u4e86\u9519\u8bef&#xff0c;\u4f8b\u5982\u5bf9\u7aef\u8fde\u63a5\u5f02\u5e38\u5173\u95ed&#xff0c;\u6211\u4eecrecv\u3001send\u51fa\u9519\u4e86&#xff0c;\u8fd9\u65f6\u5728Channel\u7684HandlerEvent\u4e2d&#xff0c;\u6211\u4eec\u5e94\u8be5\u8c03\u7528\u8be5\u8fde\u63a5\u7684 close \u56de\u8c03\u51fd\u6570&#xff0c;\u5173\u95ed\u8fd9\u4e2a\u5f02\u5e38\u8fde\u63a5&#xff0c;\u4f46\u662f\u53c8\u56e0\u4e3a\u5728\u4efb\u610f\u4e8b\u4ef6\u89e6\u53d1\u4e4b\u540e&#xff0c;\u6211\u4eec\u90fd\u8981\u8c03\u7528\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570&#xff0c;\u6240\u4ee5\u5148\u5173\u95ed\u8fde\u63a5\u518d\u8c03\u7528\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u4f1a\u53d1\u751f\u6bb5\u9519\u8bef&#xff0c;\u56e0\u4e3a\u8fde\u63a5\u5df2\u7ecf\u5173\u95ed&#xff0c;Connection\u5bf9\u8c61\u5df2\u7ecf\u9500\u6bc1&#xff0c;\u800c\u6211\u4eec\u662f\u5728Connection\u4e2d\u4e3aChannel\u7ed1\u5b9a\u7684\u51fd\u6570&#xff0c;\u8fde\u63a5\u5173\u95ed\u610f\u5473\u7740\u7a0b\u5e8f\u4f1a\u5728HandlerEvent\u51fd\u6570\u4e2d\u89e3\u5f15\u7528\u7a7a\u6307\u9488\u5e76\u8c03\u7528\u56de\u8c03\u51fd\u6570\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898&#xff0c;\u6211\u4eec\u5f53\u7136\u53ef\u4ee5\u5148\u6267\u884c\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u518d\u53bb\u6267\u884c\u5173\u95ed\u8fde\u63a5\u56de\u8c03&#xff0c;\u4f46\u662f\u7531\u4e8e\u540e\u9762\u4e5f\u5f15\u51fa\u4e86\u4e00\u4e2a\u95ee\u9898&#xff08;Connection\u7684Release\u51fd\u6570\u5f15\u53d1\u7684\u95ee\u9898&#xff09;&#xff0c;\u6240\u4ee5\u6211\u4eec\u5b9e\u9645\u7684close\u5e76\u4e0d\u4f1a\u76f4\u63a5\u5173\u95ed\u8fde\u63a5&#xff0c;\u800c\u662f\u4f1a\u7b49\u5f85\u5c31\u7eea\u4e8b\u4ef6\u5904\u7406\u5b8c\u6bd5\u518d\u91ca\u653e\u8fde\u63a5&#xff0c;\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u7edf\u4e00\u7684\u5c06\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u653e\u5728\u6700\u540e<\/span><\/p>\n<\/p>\n<p>class Poller;<br \/>\nclass EventLoop;<br \/>\nclass Channel<br \/>\n{<br \/>\n    using EventCallBack &#061; std::function&lt;void()&gt;;<br \/>\nprivate:<br \/>\n    EventLoop* _loop;   \/\/ \u56de\u6307Poller\u7c7b&#xff0c;\u4ece\u800c\u4f7f\u7528EventLoop\u4e2d\u5c01\u88c5\u7684epoll\u63a5\u53e3\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7<\/p>\n<p>    int _fd;            \/\/ \u5173\u5fc3\u4e8b\u4ef6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26fd<br \/>\n    uint32_t _events;   \/\/ \u5173\u5fc3\u7684\u4e8b\u4ef6<br \/>\n    uint32_t _revents;  \/\/ \u5c31\u7eea\u7684\u4e8b\u4ef6<\/p>\n<p>    EventCallBack _read_cb;     \/\/ \u53ef\u8bfb\u4e8b\u4ef6\u89e6\u53d1\u540e\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570  EPOLLIN<br \/>\n    EventCallBack _write_cb;    \/\/ \u53ef\u5199\u4e8b\u4ef6\u89e6\u53d1\u540e\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570  EPOLLOUT<br \/>\n    EventCallBack _error_cb;    \/\/ \u9519\u8bef\u4e8b\u4ef6\u89e6\u53d1\u540e\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570  EPOLLERR<br \/>\n    EventCallBack _close_cb;    \/\/ \u8fde\u63a5\u65ad\u5f00\u4e8b\u4ef6\u89e6\u53d1\u540e\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570  EPOLLHUP<br \/>\n    EventCallBack _event_cb;    \/\/ \u4efb\u610f\u4e8b\u4ef6\u89e6\u53d1\u540e\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570<br \/>\npublic:<br \/>\n    Channel(EventLoop* loop, int fd) : _fd(fd), _events(0), _revents(0), _loop(loop) {}<br \/>\n    int Fd() { return _fd; }<br \/>\n    \/\/ \u5916\u90e8\u83b7\u53d6\u5173\u5fc3\u7684\u4e8b\u4ef6<br \/>\n    uint32_t GetEvents() { return _events; }<br \/>\n    \/\/ \u5916\u90e8\u8bbe\u7f6e\u5c31\u7eea\u4e8b\u4ef6<br \/>\n    void SetRevents(uint32_t revents) { _revents &#061; revents; }<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u56de\u8c03\u51fd\u6570<br \/>\n    void SetReadCallback(const EventCallBack&amp; cb) { _read_cb &#061; cb; }<br \/>\n    void SetWriteCallback(const EventCallBack&amp; cb) { _write_cb &#061; cb; }<br \/>\n    void SetErrorCallback(const EventCallBack&amp; cb) { _error_cb &#061; cb; }<br \/>\n    void SetCloseCallback(const EventCallBack&amp; cb) { _close_cb &#061; cb; }<br \/>\n    void SetEventCallback(const EventCallBack&amp; cb) { _event_cb &#061; cb; }<\/p>\n<p>    \/\/ \u662f\u5426\u76d1\u63a7\u4e86fd\u7684\u8bfb\u3001\u5199\u4e8b\u4ef6<br \/>\n    bool ReadAble() { return _events &amp; EPOLLIN; }<br \/>\n    bool WriteAble() { return _events &amp; EPOLLOUT; }<\/p>\n<p>    \/\/ \u76d1\u63a7\u8bfb\u4e8b\u4ef6<br \/>\n    void EnableRead() { _events |&#061; EPOLLIN; Update(); }<br \/>\n    \/\/ \u76d1\u63a7\u5199\u4e8b\u4ef6<br \/>\n    void EnableWrite() { _events |&#061; EPOLLOUT; Update(); }<br \/>\n    \/\/ \u5173\u95ed\u8bfb\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void DisableRead() { _events &amp;&#061; ~EPOLLIN; Update(); }<br \/>\n    \/\/ \u5173\u95ed\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void DisableWrite() { _events &amp;&#061; ~EPOLLOUT; Update(); }<br \/>\n    \/\/ \u5173\u95ed\u76d1\u63a7fd\u7684\u5168\u90e8\u4e8b\u4ef6<br \/>\n    void DisableAll() { _events &#061; 0; Update(); }<\/p>\n<p>    \/\/ \u5728 epoll \u4e2d\u6dfb\u52a0\u3001\u4fee\u6539\u3001\u79fb\u9664 _fd \u5bf9\u5e94\u7684\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    \/\/ \u7531\u4e8e\u4f7f\u7528\u5230\u4e86EventLoop\u7c7b\u4e2d\u5c01\u88c5\u7684Poller\u7c7b\u4e2d\u7684\u51fd\u6570&#xff0c;\u800cEventLoop\u7c7b\u5728\u4e0b\u9762\u624d\u5b9a\u4e49&#xff0c;\u6240\u4ee5\u9700\u8981\u5c06\u4e0b\u9762\u4e24\u4e2a\u51fd\u6570\u7684\u5b9a\u4e49\u653e\u5728EventLoop\u7c7b\u4e0b\u9762<br \/>\n    void Remove();<br \/>\n    void Update();<\/p>\n<p>    \/\/ \u4e8b\u4ef6\u5206\u914d\u5668&#xff0c;\u4e00\u65e6\u89e6\u53d1\u4e86\u4e8b\u4ef6&#xff0c;\u5c31\u6267\u884cHandlerEvent&#xff0c;\u5177\u4f53\u8c03\u7528\u54ea\u4e2a\u51fd\u6570\u7531HandlerEvent\u51b3\u7b56<br \/>\n    void HandlerEvent()<br \/>\n    {<br \/>\n        \/\/ EPOLLIN &#8212; \u8bfb\u4e8b\u4ef6\u5c31\u7eea    EPOLLHUP &#8212; \u8be5\u8fde\u63a5\u5173\u95ed    EPOLLPRI &#8212; \u643a\u5e26\u5e26\u5916\u6570\u636e\u7684\u9ad8\u4f18\u5148\u7ea7\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u8fd9\u4e09\u8005\u90fd\u9700\u8981\u8fdb\u884c\u8bfb\u56de\u8c03<br \/>\n        if ((_revents &amp; EPOLLIN) || (_revents &amp; EPOLLRDHUP) || (_revents &amp; EPOLLPRI))<br \/>\n        {<br \/>\n            if (_read_cb) _read_cb();<br \/>\n        }<br \/>\n        \/\/ \u6709\u53ef\u80fd\u91ca\u653e\u8fde\u63a5\u7684\u64cd\u4f5c&#xff0c;\u4e00\u6b21\u53ea\u80fd\u6267\u884c\u4e00\u6b21&#xff0c;\u907f\u514d\u9519\u8bef\u53d1\u751f<br \/>\n        if (_revents &amp; EPOLLOUT)<br \/>\n        {<br \/>\n            if (_write_cb) _write_cb();<br \/>\n        }<br \/>\n        else if (_revents &amp; EPOLLERR)   \/\/ EPOLLERR \u8868\u793a\u9519\u8bef<br \/>\n        {<br \/>\n            \/\/ \u5f02\u5e38\u7684\u60c5\u51b5\u8fde\u63a5\u3001\u5957\u63a5\u5b57\u90fd\u4f1a\u5173\u95ed&#xff0c;\u6240\u4ee5\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u9700\u8981\u5728\u5f02\u5e38\u5904\u7406\u4e4b\u524d\u8c03\u7528<br \/>\n            if (_error_cb) _error_cb();<br \/>\n        }<br \/>\n        else if (_revents &amp; EPOLLHUP)   \/\/ EPOLLHUP \u8868\u793a\u8fde\u63a5\u8fd8\u672a\u5efa\u7acb<br \/>\n        {<br \/>\n            if (_close_cb) _close_cb();<br \/>\n        }<br \/>\n        \/\/ \u4efb\u610f\u4e8b\u4ef6\u56de\u8c03<br \/>\n        if (_event_cb) _event_cb();<br \/>\n    }<br \/>\n}; <\/p>\n<h4 id=\"Poller\" style=\"background-color:transparent\">Poller<\/h4>\n<p><span style=\"color:#7b7f82\">\u540cSocket\u6a21\u5757\u4e00\u6837&#xff0c;\u662f\u5bf9epoll\u4e09\u4e2a\u7cfb\u7edf\u8c03\u7528\u7684\u4e00\u4e2a\u5c01\u88c5&#xff0c;\u4fbf\u4e8e\u6211\u4eec\u540e\u7eed\u7a0b\u5e8f\u4e2d\u7684\u8c03\u7528\u3002<\/span><\/p>\n<p>\u6210\u5458&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">_channels&#xff0c;\u4e00\u4e2a\u7ef4\u62a4epoll\u6240\u76d1\u63a7\u7684\u6240\u6709\u4e8b\u4ef6\u7684\u54c8\u5e0c\u8868&#xff0c;\u7ba1\u7406\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u5bf9\u5e94\u4e8b\u4ef6\u7ba1\u7406\u7684\u7684Channel\u5bf9\u8c61<\/span><\/li>\n<li><span style=\"color:#7b7f82\">epfd&#xff0c;\u4fdd\u5b58\u521b\u5efaepoll\u8fd4\u56de\u5230\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5373epoll\u7684\u64cd\u4f5c\u53e5\u67c4<\/span><\/li>\n<li><span style=\"color:#7b7f82\">evs\u6570\u7ec4&#xff0c;\u7528\u6765\u4fdd\u5b58\u5df2\u7ecf\u5c31\u7eea\u7684\u4e8b\u4ef6\u7684epoll_event\u7ed3\u6784\u4f53<\/span><\/li>\n<\/ul>\n<p>\u5bf9\u5916\u63a5\u53e3&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u6dfb\u52a0\u6216\u66f4\u65b0\u5bf9\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u79fb\u9664\u5bf9\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7<\/span><\/li>\n<\/ul>\n<p>\u00a0\u903b\u8f91\u6d41\u7a0b&#xff1a;<\/p>\n<li><span style=\"color:#7b7f82\">\u5bf9\u63cf\u8ff0\u7b26\u8fdb\u884c\u76d1\u63a7&#xff0c;\u4f46\u662f\u9700\u8981\u76d1\u63a7\u63cf\u8ff0\u7b26\u7684\u4ec0\u4e48\u4e8b\u4ef6&#xff0c;\u7531Channel\u6765\u63d0\u4f9b<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5f53\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u5c31\u7eea\u4e4b\u540e&#xff0c;\u5e94\u8be5\u8c03\u7528\u63cf\u8ff0\u7b26\u7684\u54ea\u4e2a\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u4e5f\u7531Channel\u6765\u8d1f\u8d23<\/span><\/li>\n<p>class Poller<br \/>\n{<br \/>\n    static const int num &#061; 1024;<br \/>\nprivate:<br \/>\n    int _epfd;                      \/\/ epoll\u7684fd<br \/>\n    struct epoll_event _evs[num];   \/\/ \u7528\u4e8e\u63a5\u6536\u5c31\u7eea\u4e8b\u4ef6\u7684\u6570\u7ec4<br \/>\n    std::unordered_map&lt;int, Channel*&gt; _channels; \/\/ fd\u4e0e\u5bf9\u5e94Channel\u6620\u5c04<br \/>\nprivate:<br \/>\n    \/\/ \u5bf9epoll\u7684\u76f4\u63a5\u64cd\u4f5c<br \/>\n    void Update(Channel* channel, int op)<br \/>\n    {<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;GetEvents();<\/p>\n<p>        int ret &#061; epoll_ctl(_epfd, op, fd, &amp;ev);<br \/>\n        if (ret &lt; 0)<br \/>\n            ERR_LOG(&#034;epoll_ctl error!&#034;);<\/p>\n<p>    }<br \/>\n    \/\/ \u5224\u65ad\u4e00\u4e2aChannel\u662f\u5426\u5df2\u7ecf\u6dfb\u52a0\u5230\u4e86\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    bool HasChannel(Channel* channel)<br \/>\n    {<br \/>\n        auto it &#061; _channels.find(channel-&gt;Fd());<br \/>\n        if (it &#061;&#061; _channels.end()) return false;<br \/>\n        return true;<br \/>\n    }<br \/>\npublic:<br \/>\n    Poller() : _epfd(-1)<br \/>\n    {<br \/>\n        _epfd &#061; epoll_create(num);<br \/>\n        if (_epfd &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;epoll_create error!&#034;);<br \/>\n            abort();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u6dfb\u52a0\u6216\u4fee\u6539\u4e8b\u4ef6<br \/>\n    void UpdateEvent(Channel* channel)<br \/>\n    {<br \/>\n        auto it &#061; _channels.find(channel-&gt;Fd());<br \/>\n        if (it &#061;&#061; _channels.end())<br \/>\n        {<br \/>\n            \/\/ \u6ca1\u6709\u627e\u5230\u5c31\u662f\u6dfb\u52a0\u4e8b\u4ef6<br \/>\n            int fd &#061; channel-&gt;Fd();<br \/>\n            _channels[fd] &#061; channel;<br \/>\n            Update(channel, EPOLL_CTL_ADD);<br \/>\n        }<br \/>\n        else Update(channel, EPOLL_CTL_MOD);    \/\/ \u5426\u5219\u6dfb\u52a0\u4e8b\u4ef6<br \/>\n    }<br \/>\n    \/\/ \u79fb\u9664\u76d1\u63a7<br \/>\n    void RemoveEvent(Channel* channel)<br \/>\n    {<br \/>\n        auto it &#061; _channels.find(channel-&gt;Fd());<br \/>\n        if (it !&#061; _channels.end())<br \/>\n        {<br \/>\n            epoll_ctl(_epfd, EPOLL_CTL_DEL, channel-&gt;Fd(), nullptr);<br \/>\n            _channels.erase(it);<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u5f00\u59cb\u76d1\u63a7&#xff0c;\u8fd4\u56de\u6d3b\u8dc3\u8fde\u63a5<br \/>\n    void Poll(std::vector&lt;Channel*&gt;* active)<br \/>\n    {<br \/>\n        int n &#061; epoll_wait(_epfd, _evs, num, -1);<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            if (errno &#061;&#061; EINTR)<br \/>\n                return;<\/p>\n<p>            ERR_LOG(&#034;epoll wait error!, error: %s&#034;, strerror(errno));<br \/>\n            abort();<br \/>\n        }<\/p>\n<p>        \/\/ \u5c06\u5c31\u7eea\u7684channel*\u8fd4\u56de<br \/>\n        for (int i &#061; 0; i &lt; n; i&#043;&#043;)<br \/>\n        {<br \/>\n            auto it &#061; _channels.find(_evs[i].data.fd);<br \/>\n            assert(it !&#061; _channels.end());<\/p>\n<p>            it-&gt;second-&gt;SetRevents(_evs[i].events);<br \/>\n            active-&gt;push_back(it-&gt;second);<br \/>\n        }<br \/>\n    }<br \/>\n}; <\/p>\n<\/p>\n<h4 id=\"EventLoop%EF%BC%88%E6%A0%B8%E5%BF%83%EF%BC%89\" style=\"background-color:transparent\">EventLoop&#xff08;\u6838\u5fc3&#xff09;<\/h4>\n<p>EventLoop\u662f\u6211\u4eec\u670d\u52a1\u5668\u6a21\u5757\u7684\u7edf\u7b79\u7ba1\u7406\u6a21\u5757&#xff0c;\u56e0\u4e3a\u6211\u4eec\u6240\u8bf4\u7684 one loop per thread \u6216 one thread one EventLoop&#xff0c;\u4e2d\u7684loop\u6307\u7684\u5c31\u662fEventLoop&#xff0c;\u4e3bReactor\u3001\u4ece\u5c5eReactor\u90fd\u9700\u8981\u72ec\u7acb\u7684\u4e00\u4e2aEventLoop\u3002<\/p>\n<h5 id=\"eventfd%C2%A0\">eventfd\u00a0<\/h5>\n<p>\u5728\u5bf9\u4e8eEventLoop\u6a21\u5757\u7684\u5b66\u4e60\u524d&#xff0c;\u8981\u5148\u770b\u4e00\u4e0beventfd\u8fd9\u4e2a\u51fd\u6570&#xff0c;\u5b83\u7684\u6838\u5fc3\u529f\u80fd\u5c31\u662f\u4e00\u79cd\u4e8b\u4ef6\u7684\u901a\u77e5\u673a\u5236&#xff0c;\u7b80\u5355\u6765\u8bf4&#xff0c;\u5f53\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u7684\u65f6\u5019&#xff0c;\u5c31\u4f1a\u5728\u5185\u6838\u5f53\u4e2d\u7ba1\u7406\u4e00\u4e2a\u8ba1\u6570\u5668&#xff0c;\u6bcf\u5f53\u5411eventfd\u5f53\u4e2d\u5199\u5165\u4e00\u4e2a\u6570\u503c&#xff0c;\u8868\u793a\u7684\u5c31\u662f\u4e8b\u4ef6\u901a\u77e5\u7684\u6b21\u6570&#xff0c;\u4e4b\u540e\u53ef\u4ee5\u4f7f\u7528read\u6765\u5bf9\u4e8e\u6570\u636e\u8fdb\u884c\u8bfb\u53d6&#xff0c;\u8bfb\u53d6\u5230\u7684\u6570\u636e\u5c31\u662f\u901a\u77e5\u7684\u6b21\u6570<\/p>\n<p>\u5047\u8bbe\u6bcf\u6b21\u7ed9eventfd\u5199\u4e00\u4e2a1&#xff0c;\u90a3\u4e48\u5c31\u8868\u793a\u901a\u77e5\u4e86\u4e00\u6b21&#xff0c;\u901a\u77e5\u4e09\u6b21\u4e4b\u540e\u518d\u8fdb\u884c\u8bfb\u53d6&#xff0c;\u6b64\u65f6\u8bfb\u53d6\u51fa\u6765\u7684\u5c31\u662f3&#xff0c;\u8bfb\u53d6\u4e86\u4e4b\u540e\u8fd9\u4e2a\u8ba1\u6570\u5668\u5c31\u4f1a\u53d8\u62100<\/p>\n<p>eventfd\u7684\u5e94\u7528\u573a\u666f\u5728\u672c\u9879\u76ee\u4e2d&#xff0c;\u662f\u7528\u4e8eEventLoop\u6a21\u5757\u4e2d\u5b9e\u73b0\u7ebf\u7a0b\u4e4b\u95f4\u7684\u4e8b\u4ef6\u901a\u77e5\u529f\u80fd\u7684<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"183\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135256-68025938f0c07.png\" width=\"535\" \/><\/p>\n<p>\u8fd4\u56de\u503c\u00a0<\/p>\n<p>\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u00a0<\/p>\n<p>\u53c2\u6570\u00a0<\/p>\n<p><span style=\"color:null\">initval<\/span><\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u8ba1\u6570\u521d\u503c\u00a0<\/span><\/li>\n<\/ul>\n<p>flags<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">EFD_CLOEXEC&#xff0c;\u7981\u6b62\u8fdb\u7a0b\u590d\u5236<\/span><\/li>\n<li><span style=\"color:#7b7f82\">EFD_NONBLOCK&#xff0c;\u542f\u52a8\u975e\u963b\u585e\u5c5e\u6027<\/span><\/li>\n<\/ul>\n<p>\u8bfb\u53d6\u64cd\u4f5c\u4e5f\u90fd\u662f\u4f7f\u7528read\u3001write&#xff0c;\u56e0\u4e3a\u4e00\u5207\u7686\u6587\u4ef6&#xff0c;\u4f46\u662f\u6ce8\u610f\u5728\u8bfb\u5199\u7684\u65f6\u5019\u8981\u8bfb\u51998\u5b57\u8282\u5927\u5c0f\u00a0<\/p>\n<\/p>\n<h5 id=\"%E8%AE%BE%E8%AE%A1%E6%80%9D%E8%B7%AF\" style=\"background-color:transparent\">\u8bbe\u8ba1\u601d\u8def<\/h5>\n<p><span style=\"color:null\">EventLoop\u7ec4\u5408\u4e86\u4e00\u4e2aPoller\u548c\u4e00\u4e2a\u8ba1\u65f6\u5668&#xff0c;\u5e76\u4e14EventLoop\u4e0e\u7ebf\u7a0b\u4e00\u4e00\u5bf9\u5e94\u3002<\/span><\/p>\n<p>\u4e3a\u4ec0\u4e48 \u4e00\u4e2a\u7ebf\u7a0b\u8981\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684EventLoop&#xff1f;<\/p>\n<p><span style=\"color:#7b7f82\">\u5982\u679c\u6211\u4eec\u4e0d\u5c06EventLoop\u4e0e\u4e00\u4e2a\u7ebf\u7a0b\u4e00\u4e00\u5bf9\u5e94&#xff0c;\u90a3\u4e48\u4e00\u4e2a\u8fde\u63a5\u653e\u5728EventLoop\u7684Poller\u4e2d\u76d1\u63a7&#xff0c;\u4e00\u65e6\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u90a3\u4e48\u5916\u63a5\u7684\u7ebf\u7a0b\u6c60\u53ef\u80fd\u4f1a\u4e89\u62a2\u7740\u5904\u7406\u8be5\u5c31\u7eea\u4e8b\u4ef6&#xff0c;\u90a3\u4e48\u8fd9\u5c31\u5f15\u53d1\u4e86\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff0c;\u6211\u4eec\u4e0d\u5f97\u4e0d\u8fdb\u884c\u52a0\u9501&#xff0c;\u800c\u52a0\u9501\u5c31\u610f\u5473\u7740\u670d\u52a1\u5668\u6574\u4f53\u6548\u7387\u7684\u964d\u4f4e&#xff0c;\u6bcf\u4e00\u4e2a\u8fde\u63a5\u90fd\u52a0\u4e00\u4e2a\u9501\u8fd9\u662f\u4e0d\u73b0\u5b9e\u7684&#xff0c;\u4e00\u53f0\u767e\u4e07\u7ea7\u5e76\u53d1\u91cf\u7684\u670d\u52a1\u5668&#xff0c;\u5982\u679c\u4e3a\u6bcf\u4e00\u4e2a\u8fde\u63a5\u90fd\u52a0\u9501&#xff0c;\u8fd9\u4e2a\u6548\u7387\u548c\u5185\u5b58\u6d88\u8017\u662f\u5f88\u5e9e\u5927\u7684\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u56e0\u6b64\u54e6\u6211\u4eec\u9700\u8981\u5c06\u4e00\u4e2a\u8fde\u63a5\u7684\u4e8b\u4ef6\u76d1\u63a7\u3001\u8fde\u63a5\u7684\u4e8b\u4ef6\u5904\u7406\u4ee5\u53ca\u5176\u4ed6\u64cd\u4f5c\u90fd\u653e\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u8fdb\u884c&#xff0c;\u8fd9\u6837\u5c31\u907f\u514d\u4e86\u56e0\u52a0\u9501\u5bfc\u81f4\u7684\u6548\u7387\u95ee\u9898\u3002<\/span><\/p>\n<p>\u5982\u4f55\u4fdd\u8bc1\u4e00\u4e2a\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u5728EventLoop\u5bf9\u5e94\u7684\u7ebf\u7a0b\u4e2d&#xff1f;\u00a0<\/p>\n<p><span style=\"color:#7b7f82\">\u4e3aEventLoop\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;\u5373\u5bf9\u8fde\u63a5\u7684\u64cd\u4f5c\u5e76\u4e0d\u76f4\u63a5\u6267\u884c&#xff0c;\u800c\u662f\u5f53\u505a\u4efb\u52a1\u6dfb\u52a0\u5230\u4efb\u52a1\u961f\u5217\u4e2d\u3002<\/span><\/p>\n<p>\u65e2\u7136\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u5e94\u4e00\u4e2aConnection&#xff0c;\u90a3\u4e48\u8be5\u7ebf\u7a0b\u4e0d\u5c31\u662f\u4f1a\u4e32\u884c\u7684\u6267\u884cepoll\u8fd4\u56de\u7684\u5c31\u7eea\u4e8b\u4ef6\u5417&#xff1f;\u6211\u8fd9\u4e2a\u8fde\u63a5\u4e5f\u6ca1\u6709\u653e\u5728\u5176\u4ed6\u7ebf\u7a0b\u7684EventLoop\u4e2d\u76d1\u63a7\u554a&#xff0c;\u4e3a\u4ec0\u4e48\u4f1a\u5f15\u53d1\u5bb3\u6015\u591a\u7ebf\u7a0b\u4e89\u62a2\u641e\u4e00\u4e2a\u4efb\u52a1\u961f\u5217&#xff0c;\u4fdd\u8bc1\u4e00\u4e2a\u8fde\u63a5\u7684\u6240\u6709\u4e8b\u4ef6\u90fd\u662f\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5185\u6267\u884c&#xff1f;<\/p>\n<p><span style=\"color:#7b7f82\">\u5728muduo\u7f51\u7edc\u5e93\u4e2d&#xff0c;EventLoop\u5f15\u5165\u4efb\u52a1\u961f\u5217\u7684\u4e3b\u8981\u76ee\u7684\u662f\u4e3a\u4e86\u652f\u6301\u66f4\u590d\u6742\u7684\u5e76\u53d1\u573a\u666f\u4ee5\u53ca\u89e3\u8026\u4e1a\u52a1\u903b\u8f91\u4e0e\u4e8b\u4ef6\u5faa\u73af\u672c\u8eab\u3002\u867d\u7136\u5728\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u578b\u4e0b&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u786e\u5b9e\u53ea\u5904\u7406\u5c5e\u4e8e\u5b83\u7684\u90a3\u4e9b\u8fde\u63a5&#xff08;\u5373\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u5e94\u82e5\u5e72\u4e2aConnection\u5bf9\u8c61&#xff09;&#xff0c;\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u4ee5\u4e0b\u51e0\u4e2a\u539f\u56e0\u4fc3\u4f7f\u9700\u8981\u5b9e\u73b0\u4efb\u52a1\u961f\u5217&#xff1a;<\/span><\/p>\n<li><span style=\"color:#7b7f82\">\u8de8\u7ebf\u7a0b\u64cd\u4f5c&#xff1a;\u5c3d\u7ba1\u5f53\u524d\u8fde\u63a5\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684 EventLoop \u548c\u5176\u6240\u5728\u7684\u7ebf\u7a0b&#xff0c;\u4f46\u662f\u53ef\u80fd\u5b58\u5728\u4ece\u5176\u4ed6\u7ebf\u7a0b\u63d0\u4ea4\u7684\u4efb\u52a1\u5e0c\u671b\u5f71\u54cd\u8be5\u8fde\u63a5\u7684\u884c\u4e3a\u3002\u4f8b\u5982&#xff0c;\u67d0\u4e9b\u63a7\u5236\u6307\u4ee4\u53ef\u80fd\u6765\u81ea\u53e6\u4e00\u4e2a\u7ba1\u7406\u7ebf\u7a0b\u800c\u4e0d\u662f\u76f4\u63a5\u7531 IO \u4e8b\u4ef6\u89e6\u53d1\u3002<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5ef6\u540e\u6267\u884c&#xff1a;\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5c06\u4e00\u4e9b\u975e\u7d27\u6025\u7684\u64cd\u4f5c\u63a8\u8fdf\u5230\u4e3b\u4e8b\u4ef6\u5faa\u73af\u4e2d\u53bb\u5b8c\u6210&#xff0c;\u8fd9\u53ef\u4ee5\u907f\u514d\u963b\u585e\u5173\u952e\u8def\u5f84\u4e0a\u7684\u5de5\u4f5c\u3002\u901a\u8fc7\u628a\u4efb\u52a1\u52a0\u5165\u961f\u5217\u4ea4\u7ed9 eventloop \u6765\u7edf\u4e00\u8c03\u5ea6&#xff0c;\u5c31\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4fdd\u8bc1\u987a\u5e8f\u4e00\u81f4\u6027&#xff1a;\u5982\u679c\u540c\u4e00\u8fde\u63a5\u4ea7\u751f\u4e86\u591a\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u4efb\u52a1(\u5982\u8bfb\u53d6\u5b8c\u6210\u540e\u5199\u56de\u54cd\u5e94)&#xff0c;\u5229\u7528\u4efb\u52a1\u961f\u5217\u53ef\u4ee5\u5e2e\u52a9\u4fdd\u6301\u8fd9\u4e9b\u4efb\u52a1\u6309\u7167\u6b63\u786e\u7684\u987a\u5e8f\u88ab\u6267\u884c\u800c\u65e0\u9700\u62c5\u5fc3\u4e71\u5e8f\u95ee\u9898\u3002<\/span><\/li>\n<p><span style=\"color:null\">\u56e0\u6b64\u5373\u4fbf\u662f\u5728\u5355\u4e00\u7ebf\u7a0b\u73af\u5883\u4e0b\u8fd0\u884c\u67d0\u6761\u8fde\u63a5\u7684\u6240\u6709\u56de\u8c03\u51fd\u6570&#xff0c;\u4f7f\u7528\u4efb\u52a1\u961f\u5217\u4ecd\u7136\u80fd\u63d0\u4f9b\u7075\u6d3b\u6027\u548c\u66f4\u597d\u7684\u7ed3\u6784\u5316\u8bbe\u8ba1\u601d\u8def\u3002<\/span>\u6240\u4ee5\u53ef\u4ee5\u8bf4 RunInLoop \u662f\u56f4\u7ed5\u7740\u591a\u7ebf\u7a0b\u534f\u4f5c\u53ca\u5185\u90e8\u4efb\u52a1\u8c03\u5ea6\u8fd9\u4e00\u76ee\u6807\u5c55\u5f00\u7684\u91cd\u8981\u529f\u80fd\u4e4b\u4e00\u3002<\/p>\n<p>EventLoop\u5904\u7406\u6d41\u7a0b<\/p>\n<li>\u5728\u7ebf\u7a0b\u4e2d\u5bf9\u63cf\u8ff0\u7b26\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7<\/li>\n<li>\u4e8b\u4ef6\u5c31\u7eea\u5219\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406\u00a0<\/li>\n<li>\u6240\u6709\u5c31\u7eea\u4e8b\u4ef6\u5904\u7406\u5b8c\u6bd5\u540e\u518d\u5904\u7406\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4e8b\u4ef6<\/li>\n<p>\u6ce8\u610f&#xff1a;\u56e0\u4e3a\u5904\u7406\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u662f\u5728\u5c31\u7eea\u4e8b\u4ef6\u4e4b\u540e\u7684&#xff0c;\u4e5f\u5c31\u4f1a\u5f15\u53d1\u4e00\u4e2a\u95ee\u9898&#xff0c;\u5982\u679cepoll\u4e2d\u6ca1\u6709\u5c31\u7eea\u4e8b\u4ef6\u800c\u963b\u585e\u5728epoll_wait&#xff0c;\u90a3\u4e48\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u5c31\u5f97\u4e0d\u5230\u6267\u884c&#xff0c;\u6240\u4ee5\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u901a\u77e5\u673a\u5236&#xff0c;\u6765\u5524\u9192\u4e8b\u4ef6\u76d1\u63a7\u7684\u963b\u585e&#xff0c;\u5373eventfd\u3002<\/p>\n<p><span style=\"color:#7b7f82\">\u6211\u4eec\u8fd8\u53ef\u4ee5\u6269\u5c55\u4ece\u5c5eReactor&#xff0c;\u5bf9\u4ece\u5c5eReactor\u5916\u63a5\u7ebf\u7a0b\u6c60&#xff0c;\u5c06\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u6765\u6267\u884c&#xff0c;\u6240\u4ee5\u4e3a\u4e86\u65e5\u540e\u65b9\u4fbf\u63a5\u5165\u7ebf\u7a0b\u6c60&#xff0c;\u6211\u4eec\u5bf9task\u8fdb\u884c\u52a0\u9501\u4fdd\u62a4&#xff0c;\u5f53\u9700\u8981\u8bbf\u95ee\u4efb\u52a1\u961f\u5217\u65f6&#xff0c;\u76f4\u63a5swap\u4e00\u4e2a\u65b0\u7684\u4efb\u52a1\u961f\u5217&#xff0c;\u8fd9\u6837\u5c31\u907f\u514d\u4e86\u6bcf\u6b21\u53d6\u4efb\u52a1\u90fd\u8981\u52a0\u9501\u7684\u65f6\u95f4\u6d88\u8017\u3002<\/span><\/p>\n<p>\u5b9e\u73b0<\/p>\n<p>\u6ce8\u610f&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u5728EventLoop\u4e0e\u5b9a\u65f6\u5668\u6574\u5408\u65f6&#xff0c;\u9700\u8981\u6ce8\u610f\u5b9a\u65f6\u5668\u4e2d\u9700\u8981\u4f7f\u7528EventLoop\u4e2d\u7684RunInLoop\u51fd\u6570&#xff0c;\u6240\u4ee5\u5373\u4f7f\u5b9a\u65f6\u5668\u7c7b\u5b9e\u73b0\u5728EventLoop\u7c7b\u4e4b\u524d&#xff0c;\u6211\u4eec\u4e5f\u5fc5\u987b\u5c06\u6d89\u53ca\u5230\u4f7f\u7528EventLoop\u4e2d\u7684RunInLoop\u51fd\u6570\u7684\u51fd\u6570\u653e\u5728EventLoop\u5b9a\u4e4b\u540e\u5b9a\u4e49\u3002\u00a0<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u6784\u9020\u51fd\u6570\u5185\u6dfb\u52a0\u5bf9eventfd\u7684\u8bfb\u4e8b\u4ef6\u76d1\u63a7&#xff0c;\u4e00\u65e6\u6211\u4eec\u5411\u4efb\u52a1\u961f\u5217push\u4efb\u52a1\u540e&#xff0c;\u5c31\u5411eventfd\u5199\u5165\u4e00\u4e2a\u6570\u636e&#xff0c;\u8fd9\u6837\u5c31\u907f\u514d\u4e86epoll\u963b\u585e\u95ee\u9898<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5728\u52a0\u9501\u65f6&#xff0c;\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528unique_ptr\u667a\u80fd\u6307\u9488\u7ba1\u7406\u9501&#xff0c;\u5e76\u914d\u5408{}\u9650\u5236\u667a\u80fd\u6307\u9488\u7684\u751f\u547d\u5468\u671f&#xff0c;\u4ece\u800c\u8fbe\u5230\u51fa\u4e86\u4f5c\u7528\u57df\u9501\u81ea\u52a8\u91ca\u653e\u7684\u64cd\u4f5c<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5bf9\u5916\u63d0\u4f9bStart\u51fd\u6570&#xff0c;\u542f\u52a8\u4ece\u5c5eReactor\u7684\u5de5\u4f5c&#xff0c;\u51fd\u6570\u5185\u65e0\u9650\u5faa\u73af\u4e09\u4e2a\u6d41\u7a0b&#xff1a;<\/span>\n<ul>\n<li><span style=\"color:#7b7f82\">\u5728\u7ebf\u7a0b\u4e2d\u5bf9\u63cf\u8ff0\u7b26\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4e8b\u4ef6\u5c31\u7eea\u5219\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406\u00a0<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u6240\u6709\u5c31\u7eea\u4e8b\u4ef6\u5904\u7406\u5b8c\u6bd5\u540e\u518d\u5904\u7406\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4e8b\u4ef6<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>class EventLoop<br \/>\n{<br \/>\n    using func &#061; std::function&lt;void()&gt;;<br \/>\nprivate:<br \/>\n    std::thread::id _thread_id;          \/\/ \u6807\u8bb0\u5f53\u5148\u7ebf\u7a0b\u7684id&#xff0c;\u4ee5\u4fbf\u4e8e\u540e\u7eed\u533a\u5206\u4e00\u4e2a\u8fde\u63a5\u7684\u64cd\u4f5c\u662f\u5426\u5728\u540c\u4e00\u4e2aEventLoop\u4e2d<br \/>\n    int _eventfd;                        \/\/ eventfd&#xff0c;\u5f53epoll\u963b\u585e\u65f6&#xff0c;\u5411eventfd\u5199\u5165\u6570\u636e&#xff0c;\u6b64\u65f6epoll\u56e0\u4e8b\u4ef6\u5c31\u7eea\u800c\u8fd4\u56de<br \/>\n    std::unique_ptr&lt;Channel&gt; _event_channel;   \/\/ \u7ba1\u7406eventfd\u8fde\u63a5\u7684\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    Poller _poller;                      \/\/ \u7ba1\u7406\u6240\u6709fd\u7684\u4e8b\u4ef6\u76d1\u63a7&#xff0c;\u4e00\u4e2aEventLoop\u5bf9\u5e94\u4e00\u4e2a\u7ebf\u7a0b\u3001\u5bf9\u5e94\u4e00\u4e2aepoll<\/p>\n<p>    std::vector&lt;func&gt; _tasks;            \/\/ \u4efb\u52a1\u961f\u5217&#xff0c;\u8ba9\u6240\u6709\u7684\u5bf9\u540c\u4e00\u4e2a\u8fde\u63a5\u7684\u64cd\u4f5c\u653e\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5185\u90e8<br \/>\n    std::mutex _mutex;                   \/\/ \u5bf9\u4efb\u52a1\u961f\u5217\u52a0\u9501<br \/>\n    TimerWheel _timer_wheel;             \/\/ \u5b9a\u65f6\u5668\u6a21\u5757<\/p>\n<p>public:<br \/>\n    \/\/ \u6267\u884c_tasks\u4e2d\u6240\u6709\u4efb\u52a1<br \/>\n    void RunTasks()<br \/>\n    {<br \/>\n        std::vector&lt;func&gt; t;<br \/>\n        {<br \/>\n            std::unique_lock&lt;std::mutex&gt; _lock(_mutex);<br \/>\n            _tasks.swap(t);<br \/>\n        }<br \/>\n        for (auto&amp; f : t)<br \/>\n        {<br \/>\n            f();<br \/>\n        }<br \/>\n    }<br \/>\n    static int CreateEventfd()<br \/>\n    {<br \/>\n        int efd &#061; eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);<br \/>\n        if (efd &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;create eventfd error!&#034;);<br \/>\n            abort();<br \/>\n        }<br \/>\n        return efd;<br \/>\n    }<br \/>\n    \/\/ eventfd\u7684\u53ef\u8bfb\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570<br \/>\n    void ReadEventfd()<br \/>\n    {<br \/>\n        uint64_t val &#061; 0;<br \/>\n        int n &#061; read(_eventfd, &amp;val, sizeof(val));<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            if (errno &#061;&#061; EINTR || errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK)<br \/>\n                return;<br \/>\n            ERR_LOG(&#034;read eventfd error!&#034;);<br \/>\n            abort();<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u5411eventfd\u6587\u4ef6\u5199\u5165\u6570\u636e&#xff0c;\u4f7f\u5f97eventfd\u7684\u8bfb\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u4ece\u800c\u4f7fepoll\u8131\u79bb\u963b\u585e<br \/>\n    void WakeUpEventfd()<br \/>\n    {<br \/>\n        uint64_t val &#061; 1;<br \/>\n        int n &#061; write(_eventfd, &amp;val, sizeof(val));<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            if (errno &#061;&#061; EINTR)<br \/>\n                return;<br \/>\n            ERR_LOG(&#034;write eventfd error!&#034;);<br \/>\n            abort();<br \/>\n        }<br \/>\n    }<br \/>\npublic:<br \/>\n    EventLoop()<br \/>\n        :_eventfd(CreateEventfd())<br \/>\n        ,_event_channel(new Channel(this, _eventfd))<br \/>\n        ,_thread_id(std::this_thread::get_id())<br \/>\n        , _timer_wheel(this)<br \/>\n    {<br \/>\n        \/\/ \u8bbe\u7f6eeventfd\u7684\u53ef\u8bfb\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570&#xff0c;\u5e76\u5f00\u59cb\u76d1\u542ceventfd\u8bfb\u4e8b\u4ef6<br \/>\n        _event_channel-&gt;SetReadCallback(std::bind(&amp;EventLoop::ReadEventfd, this));<br \/>\n        _event_channel-&gt;EnableRead();<br \/>\n    }<br \/>\n    void Start()<br \/>\n    {<br \/>\n        while (true)<br \/>\n        {<br \/>\n            \/\/ 1. \u4e8b\u4ef6\u76d1\u63a7<br \/>\n            std::vector&lt;Channel*&gt; actives;<br \/>\n            _poller.Poll(&amp;actives);<\/p>\n<p>            \/\/ 2. \u4e8b\u4ef6\u5904\u7406<br \/>\n            for (auto&amp; channel : actives)<br \/>\n            {<br \/>\n                channel-&gt;HandlerEvent();<br \/>\n            }<\/p>\n<p>            \/\/ 3. \u6267\u884c\u4efb\u52a1<br \/>\n            RunTasks();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u5224\u65ad\u5f53\u524d\u7ebf\u7a0b\u662f\u5426\u662fEventLoop\u5bf9\u5e94\u7684\u7ebf\u7a0b<br \/>\n    bool IsInLoop()<br \/>\n    {<br \/>\n        return _thread_id &#061;&#061; std::this_thread::get_id();<br \/>\n    }<\/p>\n<p>    void AssertInLoop()<br \/>\n    {<br \/>\n        assert(_thread_id &#061;&#061; std::this_thread::get_id());<br \/>\n    }<\/p>\n<p>    \/\/ \u5224\u65ad\u662f\u5426\u6267\u884c\u7684\u4efb\u52a1\u662f\u5904\u4e8e\u5f53\u524d\u7ebf\u7a0b\u4e2d&#xff0c;\u5982\u679c\u662f\u5219\u6267\u884c&#xff0c;\u4e0d\u662f\u5219\u653e\u5165\u4efb\u52a1\u961f\u5217<br \/>\n    void RunInLoop(const func&amp; cb)<br \/>\n    {<br \/>\n        if (IsInLoop())<br \/>\n            cb();<br \/>\n        else<br \/>\n            QueueInLoop(cb);<br \/>\n    }<\/p>\n<p>    \/\/ \u5c06\u5bf9\u8fde\u63a5\u7684\u64cd\u4f5c\u653e\u5165\u4efb\u52a1\u961f\u5217<br \/>\n    void QueueInLoop(const func&amp; cb)<br \/>\n    {<br \/>\n        \/\/ \u52a0\u9501&#xff0c;\u7136\u540e\u5c06\u56de\u8c03\u51fd\u6570\u653e\u5165\u4efb\u52a1\u961f\u5217<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\u6b64\u65f6\u53ef\u80fd\u963b\u585e\u7684epoll&#xff0c;\u5176\u5b9e\u5c31\u662f\u5411eventfd\u5199\u5165\u6570\u636e&#xff0c;\u4ece\u800c\u4f7f\u5f97eventfd\u7684\u8bfb\u4e8b\u4ef6\u5c31\u7eea<br \/>\n        WakeUpEventfd();<br \/>\n    }<\/p>\n<p>    \/\/ \u6dfb\u52a0\u3001\u4fee\u6539\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void UpdateEvent(Channel* channel)<br \/>\n    {<br \/>\n        _poller.UpdateEvent(channel);<br \/>\n    }<\/p>\n<p>    \/\/ \u79fb\u9664\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void RemoveEvent(Channel* channel)<br \/>\n    {<br \/>\n        _poller.RemoveEvent(channel);<br \/>\n    }<\/p>\n<p>    \/\/ \u5bf9\u5916\u63d0\u4f9b\u5b9a\u65f6\u4efb\u52a1\u7684\u6dfb\u52a0\u3001\u5237\u65b0\u3001\u5220\u9664\u64cd\u4f5c<br \/>\n    void AddTimerTask(uint64_t id, uint32_t delay, const TaskFunc&amp; cb)<br \/>\n    {<br \/>\n        _timer_wheel.AddTimerTask(id, delay, cb);<br \/>\n    }<br \/>\n    void ReflushTimerTask(uint64_t id)<br \/>\n    {<br \/>\n        _timer_wheel.ReflushTimerTask(id);<br \/>\n    }<br \/>\n    void TimerCancel(uint64_t id)<br \/>\n    {<br \/>\n        _timer_wheel.CancelTimerTask(id);<br \/>\n    }<br \/>\n    \/* \u662f\u5426\u5b58\u5728\u67d0\u4e2a\u5b9a\u65f6\u4efb\u52a1&#xff0c;\u8be5\u51fd\u6570\u5b58\u5728\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff0c;\u56e0\u4e3a\u5982\u679c\u591a\u7ebf\u7a0b\u8c03\u7528&#xff0c;<br \/>\n    \u53ef\u80fd\u4f1a\u5f15\u53d1\u6570\u636e\u4e0d\u4e00\u81f4\u95ee\u9898&#xff0c;\u6240\u4ee5\u53ea\u80fd\u5728\u6a21\u5757\u5185&#xff0c;\u5728\u5bf9\u5e94\u7684EventLoop\u7ebf\u7a0b\u5185\u6267\u884c*\/<br \/>\n    bool HasTimer(uint64_t id)<br \/>\n    {<br \/>\n        return _timer_wheel.HasTimer(id);<br \/>\n    }<br \/>\n}; <\/p>\n<\/p>\n<p><span style=\"color:#7b7f82\">\u8c03\u7528Channel\u7684EnableRead\u6d41\u7a0b&#xff1a;<\/span><\/p>\n<li><span style=\"color:#7b7f82\">\u5148\u8c03\u7528Channel\u5185\u90e8\u7684<\/span><span style=\"color:#7b7f82\">EnableRead<\/span><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"46\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135257-680259391048e.png\" width=\"758\" \/><\/li>\n<li><span style=\"color:#7b7f82\">\u7136\u540e\u56de\u8c03Update\u51fd\u6570<\/span><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"31\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135257-680259391ff2f.png\" width=\"603\" \/><\/li>\n<li><span style=\"color:#7b7f82\">Update\u5185\u53c8\u56de\u8c03\u4e86EventLoop\u7c7b\u7684\u6210\u5458\u51fd\u6570UpdateEvent\u51fd\u6570&#xff0c;\u5e76\u4f20\u5165channel\u6307\u9488<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"127\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135257-680259392faa4.png\" width=\"455\" \/><\/span><\/li>\n<li><span style=\"color:#7b7f82\">EventLoop\u7c7b\u7684\u6210\u5458\u51fd\u6570UpdateEvent\u51fd\u6570\u53c8\u56de\u8c03\u4e86\u6210\u5458\u53d8\u91cfPoller\u7684\u6210\u5458\u51fd\u6570UpdateEvent<\/span><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"294\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135257-6802593941435.png\" width=\"704\" \/><\/li>\n<li><span style=\"color:#7b7f82\">\u81f3\u6b64\u6211\u4eec\u5c31\u5c06\u4e00\u4e2afd\u7684\u76d1\u63a7\u4e8b\u4ef6\u653e\u5165\u4e86epoll\u4e2d<\/span><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"285\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135257-6802593957625.png\" width=\"475\" \/><\/li>\n<p><span style=\"color:#7b7f82\">\u5b9e\u9645\u4e0a\u662fChannel\u56de\u6307\u4e86EventLoop&#xff0c;EventLoop\u7ec4\u5408\u4e86Poller&#xff0c;\u6240\u4ee5Channel\u53ef\u4ee5\u6839\u636eEventLoop\u627e\u5230Poller&#xff0c;\u95f4\u63a5\u7684\u8c03\u7528Poller\u7684\u63a5\u53e3\u5c06Channel\u7ba1\u7406\u7684\u4e8b\u4ef6\u6dfb\u52a0\u7684epoll\u7684\u76d1\u63a7\u4e2d<\/span><\/p>\n<p>\u6dfb\u52a0\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\u00a0<\/p>\n<p>\u5b9e\u9645\u4e0a\u5c31\u662f\u5c06\u4e00\u4e2a\u51fd\u6570bind\u5230\u5b9a\u65f6\u5668\u8d85\u65f6\u65f6\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\u4e0a\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"385\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135257-680259396cc46.png\" width=\"719\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"109\" src=\"2025-04-18jgf2lhioyo3.png\" width=\"646\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"110\" src=\"2025-04-18xd2owtgcs2r.png\" width=\"878\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"224\" src=\"2025-04-18dn5co3hbg4l.png\" width=\"749\" \/><\/p>\n<h4 id=\"Connection\" style=\"background-color:transparent\">Connection<\/h4>\n<p>\u662f\u5bf9\u4e00\u4e2a\u901a\u4fe1\u8fde\u63a5\u7684\u5168\u65b9\u4f4d\u7ba1\u7406&#xff0c;\u6211\u4eec\u5bf9\u901a\u4fe1\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u662f\u901a\u8fc7\u8fd9\u4e2a\u6574\u5408\u4e86\u5404\u4e2a\u5c0f\u6a21\u5757\u540e\u7684Connection\u6a21\u5757\u63d0\u4f9b\u7684<\/p>\n<p>\u6210\u5458&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u5957\u63a5\u5b57\u7ba1\u7406&#xff0c;\u80fd\u591f\u8fdb\u884c\u5957\u63a5\u5b57\u7684\u64cd\u4f5c<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u8fde\u63a5\u4e8b\u4ef6\u7684\u5c31\u7eea\u540e\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u9519\u8bef\u3001\u6302\u65ad\u3001\u4efb\u610f\u4e8b\u4ef6\u5171\u4e94\u79cd\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u7f13\u51b2\u533a\u7ba1\u7406&#xff0c;\u4fbf\u4e8esocket\u6570\u636e\u7684\u63a5\u6536\u548c\u53d1\u9001<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u534f\u8bae\u4e0a\u4e0b\u6587\u7684\u7ba1\u7406&#xff0c;\u8bb0\u5f55\u8bf7\u6c42\u6570\u636e\u7684\u5904\u7406\u8fc7\u7a0b&#xff0c;\u53cd\u5e8f\u5217\u5316<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u56de\u8c03\u51fd\u6570\u7684\u8bbe\u7f6e<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u9636\u6bb5\u5904\u7406\u51fd\u6570\u5171\u56db\u4e2a<\/span>\n<ul>\n<li><span style=\"color:#7b7f82\">\u670d\u52a1\u7aef\u4eceTCP\u63a5\u6536\u7f13\u51b2\u533a\u6536\u5230\u6570\u636e\u540e&#xff0c;\u6570\u636e\u8be5\u5982\u4f55\u5904\u7406&#xff0c;\u9700\u8981\u7528\u6237\u51b3\u5b9a&#xff0c;\u56e0\u6b64\u5fc5\u987b\u6709\u4e1a\u52a1\u5904\u7406\u56de\u8c03\u51fd\u6570<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4e00\u4e2a\u8fde\u63a5\u5efa\u7acb\u6210\u529f\u540e&#xff0c;\u7528\u6237\u53ef\u4ee5\u8bbe\u5b9a\u4e00\u4e9b\u884c\u4e3a&#xff0c;\u56e0\u6b64\u6709\u8fde\u63a5\u5efa\u7acb\u6210\u529f\u7684\u56de\u8c03\u51fd\u6570<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4e00\u4e2a\u8fde\u63a5\u5173\u95ed\u524d&#xff0c;\u7528\u6237\u53ef\u4ee5\u8bbe\u5b9a\u4e00\u4e9b\u884c\u4e3a&#xff0c;\u56e0\u6b64\u6709\u8fde\u63a5\u5173\u95ed\u7684\u56de\u8c03\u51fd\u6570<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4efb\u610f\u4e8b\u4ef6\u7684\u4ea7\u751f\u540e&#xff0c;\u7528\u6237\u53ef\u4ee5\u8bbe\u5b9a\u4e00\u4e9b\u884c\u4e3a&#xff0c;\u56e0\u6b64\u6709\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u529f\u80fd&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u53d1\u9001\u6570\u636e&#xff0c;\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u76f4\u63a5\u53d1\u9001\u6570\u636e&#xff0c;\u800c\u662f\u628a\u6570\u636e\u5199\u5165\u5230\u53d1\u9001\u7f13\u51b2\u533a\u4e2d&#xff0c;\u7136\u540e\u542f\u52a8\u5199\u4e8b\u4ef6\u76d1\u63a7<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5173\u95ed\u8fde\u63a5&#xff0c;\u5b9e\u9645\u4e0a\u4e5f\u4e0d\u662f\u76f4\u63a5\u5173\u95ed\u8fde\u63a5&#xff0c;\u800c\u662f\u5728\u91ca\u653e\u8fde\u63a5\u4e4b\u524d&#xff0c;\u770b\u770b\u8f93\u5165\u8f93\u51fa\u7f13\u51b2\u533a\u662f\u5426\u8fd8\u6709\u6570\u636e\u5f85\u5904\u7406<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u542f\u52a8\u3001\u53d6\u6d88\u975e\u6d3b\u8dc3\u8fde\u63a5\u7684\u8d85\u65f6\u9500\u6bc1\u529f\u80fd&#xff0c;\u56e0\u6b64\u9700\u8981\u4e00\u4e2abool\u503c\u6765\u8868\u660e\u8fde\u63a5\u662f\u5426\u542f\u52a8\u975e\u6d3b\u8dc3\u9500\u6bc1\u529f\u80fd<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u534f\u8bae\u5207\u6362&#xff0c;\u4e00\u4e2a\u8fde\u63a5\u6536\u5230\u6570\u636e\u540e\u8be5\u5982\u4f55\u8fdb\u884c\u4e1a\u52a1\u5904\u7406&#xff0c;\u53d6\u51b3\u4e8e\u4e0a\u4e0b\u6587\u4ee5\u53ca\u6570\u636e\u7684\u4e1a\u52a1\u5904\u7406\u56de\u8c03\u51fd\u6570<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#7b7f82\">Connection\u662f\u5bf9\u8fde\u63a5\u7ba1\u7406\u7684\u6a21\u5757&#xff0c;\u5bf9\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u662f\u901a\u8fc7\u8fd9\u4e2a\u6a21\u5757\u5b8c\u6210\u7684\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u573a\u666f&#xff1a;\u5f53\u5bf9\u8fde\u63a5\u8fdb\u884c\u591a\u7ebf\u7a0b\u64cd\u4f5c\u65f6&#xff0c;\u53ef\u80fd\u4f1a\u51fa\u73b0\u8fde\u63a5\u5df2\u7ecf\u88ab\u91ca\u653e&#xff0c;\u4f46\u662f\u540e\u7eed\u53c8\u5bf9\u8fde\u63a5\u8fdb\u884c\u4e86\u64cd\u4f5c&#xff0c;\u5bfc\u81f4\u5185\u5b58\u8bbf\u95ee\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u7a7a\u95f4&#xff0c;\u6700\u7ec8\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u89e3\u51b3\u65b9\u6848&#xff1a;\u4f7f\u7528\u667a\u80fd\u6307\u9488shared_ptr\u5bf9Connection\u5bf9\u8c61\u8fdb\u884c\u7ba1\u7406&#xff0c;\u8fd9\u6837\u5c31\u80fd\u4fdd\u8bc1\u4efb\u610f\u4e00\u4e2a\u5730\u65b9\u5bf9Connection\u5bf9\u8c61\u64cd\u4f5c\u65f6&#xff0c;\u8be5\u5bf9\u8c61\u4f9d\u65e7\u5b58\u5728&#xff0c;\u5373\u4f7f\u5176\u4ed6\u5730\u65b9\u91ca\u653e\u4e86\u4ed6\u7684\u667a\u80fd\u6307\u9488<\/span><\/p>\n<p>\u5b9e\u73b0<\/p>\n<p>\u6ce8\u610f\u70b9&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u9700\u8981\u7ec4\u5408\u6211\u4eec\u524d\u9762\u7f16\u5199\u7684Socket\u3001Buffer\u3001Channel&#xff08;\u9700\u8981\u56de\u6307EventLoop&#xff09;\u6a21\u5757&#xff0c;\u6700\u7ec8\u5b9e\u73b0\u4e94\u4e2a\u901a\u4fe1\u8fde\u63a5\u7684\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570\u00a0<\/span>\n<ul>\n<li><span style=\"color:#7b7f82\">\u8bfb\u56de\u8c03&#xff1a;\u4eceTCP\u63a5\u6536\u7f13\u51b2\u533a\u8bfb\u53d6\u6570\u636e&#xff0c;\u5199\u5165\u5230\u6211\u4eec\u7684inbuffer\u7f13\u51b2\u533a&#xff0c;\u5982\u679cinbuffer\u4e2d\u6709\u6570\u636e\u5219\u8c03\u7528\u4e0a\u5c42\u8bbe\u7f6e\u7684\u4e1a\u52a1\u5904\u7406\u51fd\u6570<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5199\u56de\u8c03&#xff1a;\u4e00\u65e6\u8c03\u7528\u5199\u56de\u8c03\u5c31\u610f\u5473\u7740\u53ef\u4ee5\u5411TCP\u53d1\u9001\u7f13\u51b2\u533a\u5199\u6570\u636e\u4e86&#xff0c;\u6240\u4ee5\u6211\u4eec\u76f4\u63a5\u5199\u5165\u5373\u53ef&#xff0c;\u5982\u679c\u5199\u5165\u5931\u8d25\u4e86\u5219\u9700\u8981\u5173\u95ed\u8fde\u63a5&#xff0c;\u4f46\u662f\u5728\u5173\u95ed\u8fde\u63a5\u4e4b\u524d\u9700\u8981\u5224\u65ad\u53d1\u9001\u7f13\u51b2\u533a\u662f\u5426\u8fd8\u6709\u6570\u636e&#xff0c;\u5982\u679c\u6709\u6570\u636e&#xff0c;\u5219\u518d\u6267\u884c\u4e00\u6b21\u4e1a\u52a1\u5904\u7406\u51fd\u6570&#xff0c;\u7136\u540e\u8fdb\u884c\u5173\u95ed\u3002\u5982\u679c\u5199\u5165\u6210\u529f&#xff0c;\u5219\u5224\u65adoutbuffer\u662f\u5426\u8fd8\u6709\u6570\u636e&#xff0c;\u5982\u679c\u6ca1\u6709\u5c31\u53d6\u6d88\u5199\u4e8b\u4ef6\u76d1\u63a7&#xff0c;\u5982\u679c\u8fd8\u5904\u4e8e\u5173\u95ed\u8fde\u63a5\u72b6\u6001&#xff0c;\u5219\u5173\u95ed\u8fde\u63a5\u4e0d\u5728\u5224\u65ad\u53d1\u9001\u7f13\u51b2\u533a\u662f\u5426\u8fd8\u6709\u6570\u636e\u4e86\u3002<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u6302\u65ad&#xff1a;\u5224\u65ad\u4e00\u4e0binbuffer\u662f\u5426\u8fd8\u6709\u6570\u636e&#xff0c;\u5982\u679c\u6709\u5219\u8c03\u7528\u4e00\u6b21\u4e1a\u52a1\u5904\u7406\u51fd\u6570&#xff0c;\u7136\u540e\u5173\u95ed\u8fde\u63a5\u3002<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u9519\u8bef&#xff1a;\u76f4\u63a5\u590d\u7528\u6302\u65ad\u56de\u8c03&#xff0c;\u56e0\u4e3a\u4e24\u8005\u64cd\u4f5c\u76f8\u540c<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4efb\u610f\u56de\u8c03&#xff1a;\u5982\u679c\u5173\u5fc3\u8fde\u63a5\u7684\u6d3b\u8dc3\u5ea6\u5219\u5237\u65b0\u8d85\u65f6\u4efb\u52a1&#xff0c;\u7136\u540e\u8c03\u7528\u7ec4\u4ef6\u4f7f\u7528\u8005\u63d0\u4f9b\u7684\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570<\/span><\/li>\n<\/ul>\n<\/li>\n<li><span style=\"color:#7b7f82\">\u5728\u6784\u9020\u51fd\u6570\u5185\u76f4\u63a5\u5bf9Channel\u8fdb\u884c\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570\u7684bind&#xff0c;\u4ee5\u8fbe\u5230\u53ea\u8981\u5b9e\u4f8b\u5316\u4e00\u4e2aConnection\u5bf9\u8c61&#xff0c;\u8be5\u5bf9\u8c61\u5c31\u6709\u76f8\u5e94\u7684\u4e94\u4e2a\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u542f\u52a8\u975e\u6d3b\u8dc3\u8fde\u63a5\u9500\u6bc1\u529f\u80fd&#xff0c;\u5b9e\u9645\u4e0a\u5c31\u662f\u5c06Connection\u4e2d\u7684Release\u51fd\u6570\u4f5c\u4e3a\u4e86\u8d85\u65f6\u4efb\u52a1<\/span><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"241\" src=\"2025-04-18suc30bmn2vi.png\" width=\"864\" \/><\/li>\n<\/ul>\n<p><span style=\"color:#7b7f82\">\u7279\u522b\u6ce8\u610f&#xff1a;<\/span>\u00a0<\/p>\n<p>\u56e0\u4e3a\u4e00\u4e2a\u4ece\u5c5eReactor\u5bf9\u5e94\u4e00\u4e2a\u7ebf\u7a0b\u3001\u4e00\u4e2aEventLoop&#xff0c;EventLoop\u4e2d\u53c8\u542b\u6709\u4e00\u4e2a\u72ec\u7acb\u7684epoll&#xff0c;\u7ebf\u7a0b\u8fd9\u4e2a\u6267\u884c\u6d41\u4f1a\u5faa\u73af\u5f0f\u7684\u8c03\u7528EventLoop\u7684Start\u51fd\u6570\u8fdb\u884c\u4e09\u4e2a\u64cd\u4f5c\u7684\u65e0\u9650\u5faa\u73af&#xff0c;\u662f\u4e32\u884c\u6267\u884c\u7684\u3002\u5f53\u670d\u52a1\u5668\u8fbe\u5230\u4e86\u4e00\u4e2a\u74f6\u9888&#xff0c;\u5728\u4e00\u6b21\u4e1a\u52a1\u5904\u7406\u4e2d\u82b1\u8d39\u4e86\u5927\u91cf\u65f6\u95f4&#xff0c;\u90a3\u4e48\u8fd9\u5c31\u4f1a\u5bfc\u81f4\u5728\u4ed6\u4e4b\u540e\u7684\u5c31\u7eea\u4e8b\u4ef6\u76f4\u63a5\u8d85\u65f6&#xff0c;\u8d85\u65f6\u4e86\u90a3\u4e48\u5b9a\u65f6\u5668\u76f4\u63a5\u6267\u884c\u8d85\u65f6\u4efb\u52a1Release\u51fd\u6570&#xff0c;\u79fb\u9664\u4e8b\u4ef6\u76d1\u63a7\u3001\u5173\u95ed\u5957\u63a5\u5b57&#xff0c;\u5982\u679c\u6709\u540e\u7eed\u7684\u5b9a\u65f6\u4efb\u52a1\u5219\u53d6\u6d88\u3001\u8c03\u7528\u4e0a\u5c42\u7684\u5173\u95ed\u56de\u8c03\u51fd\u6570&#xff08;TcpServer\u4e2d\u8bbe\u7f6e\u7684RemoveConnection\u51fd\u6570&#xff0c;\u4f1adeleteConnection\u5bf9\u8c61&#xff09;&#xff0c;\u6240\u4ee5\u5728\u6267\u884c\u540e\u9762\u7684\u5c31\u7eea\u4efb\u52a1\u65f6\u5c31\u4f1a\u56e0\u4e3aConnection\u5bf9\u8c61\u5df2\u7ecf\u9500\u6bc1\u800c\u5bfc\u81f4\u8bbf\u95ee\u975e\u6cd5\u7a7a\u95f4\u5bfc\u81f4\u6bb5\u9519\u8bef\u3002<\/p>\n<p>\u867d\u7136\u662f\u670d\u52a1\u5668\u6027\u80fd\u9020\u6210\u7684\u4e00\u4e2a\u4e1a\u52a1\u5904\u7406\u4e8b\u4ef6\u8fc7\u957f\u800c\u5bfc\u81f4\u62d6\u7d2f\u4e86\u5176\u4ed6\u8fde\u63a5&#xff0c;\u5bfc\u81f4\u5176\u4ed6\u8fde\u63a5\u4e5f\u8d85\u8fc7\u4e86\u8d85\u65f6\u65f6\u95f4&#xff0c;\u4f46\u8fd9\u662f\u670d\u52a1\u5668\u6027\u80fd\u7684\u95ee\u9898&#xff0c;\u8ddf\u8ba1\u65f6\u5668\u65e0\u5173&#xff0c;\u8ba1\u65f6\u5668\u53ea\u8d1f\u8d23\u8d85\u65f6\u9500\u6bc1&#xff0c;\u8fd9\u662f\u4e00\u79cd\u89e3\u8026\u5408\u3002<\/p>\n<p>\u4f46\u662f\u4e3a\u4e86\u907f\u514d\u8ba1\u65f6\u5668\u76f4\u63a5\u6267\u884c\u8d85\u65f6\u4efb\u52a1&#xff0c;\u5173\u95ed\u8fde\u63a5\u5bfc\u81f4\u672c\u8f6eepoll\u540e\u7eed\u7684\u5c31\u7eea\u4e8b\u4ef6\u5728\u6267\u884c\u65f6Connection\u5bf9\u8c61\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86&#xff0c;\u6240\u4ee5\u6211\u4eec\u8981\u5c06Release\u4e5f\u5c01\u88c5\u4e3a\u4e00\u4e2a\u4efb\u52a1\u538b\u5165\u4ece\u5c5e\u7ebf\u7a0b\u7684\u4efb\u52a1\u961f\u5217\u3002<\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u5e94\u8be5\u80fd\u4f53\u4f1a\u4e3a\u4ec0\u4e48ReleaseInLoop\u4e2d\u9700\u8981\u53d6\u6d88\u5b9a\u65f6\u5668\u4e2d\u8be5\u8fde\u63a5\u5bf9\u5e94\u7684\u8d85\u65f6\u4efb\u52a1&#xff0c;\u56e0\u4e3a\u4f1a\u6709\u8fd9\u6837\u4e00\u4e2a\u98ce\u9669&#xff0c;\u8be5\u8fde\u63a5\u7684\u9500\u6bc1\u4efb\u52a1\u5df2\u7ecf\u88ab\u538b\u5165\u4efb\u52a1\u961f\u5217&#xff0c;\u4f46\u662f\u672c\u8f6eepoll\u540e\u7eed\u7684\u5c31\u7eea\u4e8b\u4ef6\u5728\u6267\u884c\u540e\u4f1a\u5237\u65b0\u5b9a\u65f6\u4efb\u52a1&#xff0c;\u8fd9\u4f1a\u5bfc\u81f4\u4e0b\u4e00\u6b21\u79d2\u9488\u8d70\u5230\u8be5\u5b9a\u65f6\u4efb\u52a1\u5904\u65f6&#xff0c;\u8981\u6267\u884c\u8d85\u65f6\u4efb\u52a1\u91ca\u653e\u8fde\u63a5\u65f6&#xff0c;Connection\u5bf9\u8c61\u5df2\u7ecf\u4e0d\u5728\u4e86&#xff0c;\u6240\u4ee5\u5728ReleaseInLoop\u4e2d\u6211\u4eec\u9700\u8981\u5224\u65ad\u5b9a\u65f6\u5668\u4e2d\u662f\u5426\u8fd8\u6709\u6211\u4eec\u7684\u4efb\u52a1&#xff0c;\u5982\u679c\u6709\u5219\u53d6\u6d88\u3002<\/p>\n<p>typedef enum<br \/>\n{<br \/>\n    Disconnected,   \/\/ \u8fde\u63a5\u5173\u95ed\u72b6\u6001<br \/>\n    Connecting,     \/\/ \u8fde\u63a5\u5efa\u7acb\u6210\u529f&#xff0c;\u5f85\u5904\u7406\u72b6\u6001<br \/>\n    Connected,      \/\/ \u8fde\u63a5\u5efa\u7acb\u5b8c\u6210&#xff0c;\u5404\u79cd\u8bbe\u7f6e\u5df2\u5b8c\u6210&#xff0c;\u53ef\u4ee5\u901a\u4fe1<br \/>\n    Disconnecting   \/\/ \u5f85\u5173\u95ed\u72b6\u6001<br \/>\n}ConnStatus;<\/p>\n<p>class Connection : public std::enable_shared_from_this&lt;Connection&gt;<br \/>\n{<br \/>\n\/*\u4f7f\u7528\u667a\u80fd\u6307\u9488shared_ptr\u5bf9Connection\u5bf9\u8c61\u8fdb\u884c\u7ba1\u7406&#xff0c;\u8fd9\u6837\u5c31\u80fd\u4fdd\u8bc1\u4efb\u610f\u4e00\u4e2a\u5730\u65b9\u5bf9*\/<br \/>\n\/*Connection\u5bf9\u8c61\u64cd\u4f5c\u65f6&#xff0c;\u8be5\u5bf9\u8c61\u4f9d\u65e7\u5b58\u5728&#xff0c;\u5373\u4f7f\u5176\u4ed6\u5730\u65b9\u91ca\u653e\u4e86\u4ed6\u7684\u667a\u80fd\u6307\u9488*\/<br \/>\n    using ConnectionCallback &#061; std::function&lt;void(const std::shared_ptr&lt;Connection&gt;&amp;)&gt;;<br \/>\n    using MessageCallback &#061; std::function&lt;void(const std::shared_ptr&lt;Connection&gt;&amp;, Buffer*)&gt;;<br \/>\n    using CloseCallback &#061; std::function&lt;void(const std::shared_ptr&lt;Connection&gt;&amp;)&gt;;<br \/>\n    using AnyEventCallback &#061; std::function&lt;void(const std::shared_ptr&lt;Connection&gt;&amp;)&gt;;<br \/>\nprivate:<br \/>\n    uint64_t _conn_id;          \/\/ \u552f\u4e00\u6807\u8bc6\u4e00\u4e2a\u8fde\u63a5\u7684ID<br \/>\n    int _sockfd;                \/\/ \u8fde\u63a5\u5bf9\u5e94\u7684fd<\/p>\n<p>    bool _is_inactive_release;  \/\/ \u8fde\u63a5\u662f\u5426\u542f\u52a8\u975e\u6d3b\u8dc3\u9500\u6bc1\u6807\u5fd7\u4f4d&#xff0c;\u9ed8\u8ba4false, \u56e0\u4e3a\u4e0a\u5c42\u53ef\u80fd\u6709\u957f\u8fde\u63a5\u7684\u9700\u6c42<br \/>\n    ConnStatus _status;         \/\/ \u8fde\u63a5\u5904\u4e8e\u4f55\u79cd\u72b6\u6001<\/p>\n<p>    EventLoop* _loop;           \/\/ \u56de\u6307EventLoop&#xff0c;\u8ba9\u6240\u6709\u64cd\u4f5c\u90fd\u5728loop\u5bf9\u5e94\u7684\u7ebf\u7a0b\u4e0a\u8fdb\u884c\u64cd\u4f5c&#xff0c;\u907f\u514d\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<br \/>\n    Channel _channel;           \/\/ \u5bf9\u8fde\u63a5\u7684\u4e8b\u4ef6\u7ba1\u7406<br \/>\n    Socket _socket;             \/\/ \u5957\u63a5\u5b57\u7ba1\u7406<br \/>\n    Any _context;               \/\/ \u4e0a\u5c42\u662f\u4f55\u79cd\u534f\u8bae<\/p>\n<p>    Buffer _inbuffer;           \/\/ \u8f93\u5165\u7f13\u51b2\u533a<br \/>\n    Buffer _outbuffer;          \/\/ \u8f93\u51fa\u7f13\u51b2\u533a<\/p>\n<p>    \/\/ \u67d0\u4e2a\u9636\u6bb5\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u662f\u4e0a\u5c42\u9700\u8981\u7684\u4e1a\u52a1\u5904\u7406\u51fd\u6570&#xff0c;\u662f\u4e0a\u5c42\u5728\u670d\u52a1\u5668\u6a21\u5757\u8bbe\u7f6e\u7684<br \/>\n    ConnectionCallback _conn_cb;\/\/ \u8fde\u63a5\u5efa\u7acb\u540e&#xff0c;\u4e0a\u5c42\u9700\u8981\u7684\u4e1a\u52a1\u5904\u7406\u51fd\u6570<br \/>\n    MessageCallback _msg_cb;    \/\/ \u63a5\u6536\u6570\u636e\u540e&#xff0c;\u4e0a\u5c42\u9700\u8981\u7684\u4e1a\u52a1\u5904\u7406\u51fd\u6570<br \/>\n    CloseCallback _close_cb;    \/\/ \u8fde\u63a5\u5173\u95ed\u540e&#xff0c;\u4e0a\u5c42\u9700\u8981\u7684\u4e1a\u52a1\u5904\u7406\u51fd\u6570<br \/>\n    AnyEventCallback _event_cb; \/\/ \u4efb\u610f\u4e8b\u4ef6\u89e6\u53d1&#xff0c;\u4e0a\u5c42\u9700\u8981\u7684\u4e1a\u52a1\u5904\u7406\u51fd\u6570<br \/>\n    \/\/ \u56e0\u4e3a\u5728\u540e\u9762\u7684\u670d\u52a1\u5668\u6a21\u5757\u4f1a\u4f7f\u7528shared_ptr\u4fdd\u5b58\u6240\u6709\u8fde\u63a5\u8bb0\u5f55&#xff0c;\u6240\u4ee5\u4e00\u65e6\u6709\u8fde\u63a5\u9700\u8981\u5173\u95ed<br \/>\n    \/\/ \u90a3\u4e48\u5c31\u5e94\u8be5\u4ece\u7ba1\u7406\u7684\u5730\u65b9\u79fb\u9664\u81ea\u5df1\u7684\u4fe1\u606f&#xff0c;\u7c7b\u4f3c\u8ba1\u65f6\u5668\u6a21\u5757\u90a3\u91cc\u7684TimerTask\u7684Release\u56de\u8c03<br \/>\n    CloseCallback _server_close_cb;<br \/>\nprivate:<br \/>\n    \/\/ \u8fde\u63a5\u4e8b\u4ef6\u5c31\u7eea\u540e&#xff0c;\u56de\u8c03\u7684\u4e94\u4e2a\u51fd\u6570<br \/>\n    void HandlerRead()<br \/>\n    {<br \/>\n        char buffer[65536];<br \/>\n        ssize_t n &#061; _socket.NonBlockRecv(buffer, 65535);<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            \/\/ \u8fd4\u56de\u503c\u5c0f\u4e8e0&#xff0c;\u8868\u660e\u4e00\u5b9a\u662f\u51fa\u73b0\u4e86\u8fde\u63a5\u5927\u95ee\u9898&#xff0c;\u6ca1\u5fc5\u8981\u4fdd\u7559\u8fde\u63a5\u4e86<br \/>\n            \/\/ \u4f46\u662f\u4e0d\u80fd\u76f4\u63a5\u5173\u95ed\u8fde\u63a5&#xff0c;\u56e0\u4e3ainbuffer\u3001outbuffer\u53ef\u80fd\u8fd8\u6709\u6570\u636e&#xff0c;\u9700\u8981\u5904\u7406\u5b8c\u518d\u5173\u95ed\u8fde\u63a5<br \/>\n            return ShutdownInLoop();<br \/>\n        }<br \/>\n        \/\/ \u653e\u5165\u7f13\u51b2\u533a<br \/>\n        _inbuffer.Write(buffer, n);<\/p>\n<p>        \/\/ \u7f13\u51b2\u533a\u6709\u6570\u636e&#xff0c;\u5219\u8c03\u7528MessageCallback\u8fdb\u884c\u4e1a\u52a1\u5904\u7406<br \/>\n        if (_inbuffer.ReadAbleSize() &gt; 0)<br \/>\n        {<br \/>\n            \/\/ shared_from_this &#8212; \u4ece\u5f53\u524d\u5bf9\u8c61\u83b7\u53d6\u81ea\u8eab\u7684shared_ptr&#xff0c;\u9700\u8981\u7ee7\u627f\u4e00\u4e2a\u6a21\u677f\u7c7b<br \/>\n            return _msg_cb(shared_from_this(), &amp;_inbuffer);<br \/>\n        }<\/p>\n<p>    }<br \/>\n    \/\/ \u89e6\u53d1\u5199\u4e8b\u4ef6<br \/>\n    void HandlerWrite()<br \/>\n    {<br \/>\n        ssize_t n &#061; _socket.NonBlockSend(_outbuffer.ReadPosition(), _outbuffer.ReadAbleSize());<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            \/\/ \u8fd4\u56de\u503c\u5c0f\u4e8e0&#xff0c;\u8868\u660e\u4e00\u5b9a\u662f\u51fa\u73b0\u4e86\u8fde\u63a5\u5927\u95ee\u9898&#xff0c;\u6ca1\u5fc5\u8981\u4fdd\u7559\u8fde\u63a5\u4e86&#xff0c;<br \/>\n            \/\/ \u6700\u540e\u518d\u770b\u4e00\u6b21inbuffer\u662f\u5426\u8fd8\u6709\u6570\u636e<br \/>\n            if (_inbuffer.ReadAbleSize() &gt; 0)<br \/>\n                _msg_cb(shared_from_this(), &amp;_inbuffer);<\/p>\n<p>            \/\/ \u8fdb\u884c\u5b9e\u9645\u7684\u5173\u95ed&#xff0c;\u4e0d\u518d\u5224\u65ad\u53d1\u9001\u7f13\u51b2\u533a\u662f\u5426\u8fd8\u6709\u6570\u636e\u4e86<br \/>\n            return ReleaseInLoop();<\/p>\n<p>        }<br \/>\n        \/\/ outbuffer\u7684\u8bfb\u6307\u9488\u79fb\u52a8<br \/>\n        _outbuffer.MoveReadPos(n);<\/p>\n<p>        if (_outbuffer.ReadAbleSize() &#061;&#061; 0)<br \/>\n        {<br \/>\n            \/\/ \u5982\u679coutbuffer\u6ca1\u6709\u6570\u636e\u4e86&#xff0c;\u90a3\u5c31\u5173\u95ed\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n            _channel.DisableWrite();<br \/>\n            \/\/ \u5982\u679c\u6b64\u65f6\u8fd8\u662f\u8fde\u63a5\u5173\u95ed\u72b6\u6001&#xff0c;\u90a3\u628a\u94fe\u63a5\u4e5f\u5173\u95ed\u4e86<br \/>\n            if (_status &#061;&#061; Disconnecting)<br \/>\n                return Release();<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u89e6\u53d1\u6302\u65ad\u4e8b\u4ef6<br \/>\n    void HandlerClose()<br \/>\n    {<br \/>\n        \/\/ \u4e00\u65e6\u8fde\u63a5\u6302\u65ad\u4e86&#xff0c;\u5957\u63a5\u5b57\u4ec0\u4e48\u90fd\u5e72\u4e0d\u4e86\u4e86&#xff0c;\u56e0\u6b64\u6709\u6570\u636e\u5f85\u5904\u7406\u5c31\u5904\u7406\u4e00\u4e0b<br \/>\n        if (_inbuffer.ReadAbleSize() &gt; 0)<br \/>\n            _msg_cb(shared_from_this(), &amp;_inbuffer);<\/p>\n<p>        \/\/ \u8fdb\u884c\u5b9e\u9645\u7684\u5173\u95ed&#xff0c;\u4e0d\u518d\u5224\u65ad\u53d1\u9001\u7f13\u51b2\u533a\u662f\u5426\u8fd8\u6709\u6570\u636e\u4e86<br \/>\n        Release();<br \/>\n    }<br \/>\n    \/\/ \u89e6\u53d1\u51fa\u9519\u4e8b\u4ef6<br \/>\n    void HandlerError()<br \/>\n    {<br \/>\n        HandlerClose();<br \/>\n    }<br \/>\n    \/\/ \u89e6\u53d1\u4efb\u610f\u4e8b\u4ef6&#xff1a;1. \u5237\u65b0\u8fde\u63a5\u7684\u6d3b\u8dc3\u5ea6 2. \u8c03\u7528\u7ec4\u4ef6\u4f7f\u7528\u8005\u63d0\u4f9b\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    void HandlerEvent()<br \/>\n    {<br \/>\n        \/\/ \u5982\u679c\u5173\u5fc3\u8fde\u63a5\u6d3b\u8dc3\u5ea6\u5219\u5237\u65b0\u6d3b\u8dc3\u5ea6<br \/>\n        if (_is_inactive_release)<br \/>\n            _loop-&gt;ReflushTimerTask(_conn_id);<br \/>\n        if (_event_cb)<br \/>\n            _event_cb(shared_from_this());<br \/>\n    }<\/p>\n<p>\/*\u4e3a\u4e86\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u6240\u4ee5\u5c06\u5bf9\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u5c01\u88c5\u4e3a\u4e00\u4e2a\u4efb\u52a1&#xff0c;*\/<br \/>\n\/*push\u5230loop\u7684\u4efb\u52a1\u961f\u5217\u4e2d&#xff0c;\u8ba9loop\u5bf9\u5e94\u7684\u7ebf\u7a0b\u4e32\u884c\u7684\u6267\u884c\u4efb\u52a1*\/<br \/>\n    void EstablishedInLoop()<br \/>\n    {<br \/>\n        \/\/ 1. \u4fee\u6539\u8fde\u63a5\u72b6\u6001, \u5f53\u524d\u51fd\u6570\u6267\u884c\u5b8c\u5219\u8fde\u63a5\u8fdb\u5165\u5df2\u5b8c\u6210\u8fde\u63a5\u72b6\u6001<br \/>\n        assert(_status &#061;&#061; Connecting);<br \/>\n        _status &#061; Connected;<br \/>\n        \/\/ 2. \u542f\u52a8\u8bfb\u4e8b\u4ef6\u76d1\u63a7<br \/>\n        _channel.EnableRead();<br \/>\n        \/\/ 3. \u8c03\u7528\u56de\u8c03\u51fd\u6570<br \/>\n        if (_conn_cb) _conn_cb(shared_from_this());<br \/>\n    }<\/p>\n<p>    void ReleaseInLoop()<br \/>\n    {<br \/>\n        \/\/ 1. \u4fee\u6539\u8fde\u63a5\u72b6\u6001<br \/>\n        _status &#061; Disconnected;<br \/>\n        \/\/ 2. \u79fb\u9664\u8fde\u63a5\u7684\u4e8b\u4ef6\u76d1\u63a7<br \/>\n        _channel.Remove();<br \/>\n        \/\/ 3. \u5173\u95ed\u63cf\u8ff0<br \/>\n        _socket.Close();<br \/>\n        \/\/ 4. \u5982\u679c\u5b9a\u65f6\u5668\u4e2d\u8fd8\u6709\u6211\u4eec\u7684\u5b9a\u65f6\u9500\u6bc1\u4efb\u52a1&#xff0c;\u5219\u53d6\u6d88\u4efb\u52a1<br \/>\n        if (_loop-&gt;HasTimer(_conn_id))<br \/>\n            CancelInactiveReleaseInLoop();<br \/>\n        \/\/ 5. \u8c03\u7528\u5173\u95ed\u56de\u8c03\u51fd\u6570&#xff0c;\u5982\u679c\u5148\u8c03\u7528\u79fb\u9664\u670d\u52a1\u5668\u7ba1\u7406\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u90a3\u4e48\u518d\u53bb\u8c03\u7528_close_cb\u4f1a\u8bbf\u95ee\u5df2\u7ecf\u91ca\u653e\u7684\u7a7a\u95f4&#xff0c;\u6240\u4ee5\u5148\u8c03\u7528\u5173\u95ed\u56de\u8c03\u51fd\u6570<br \/>\n        if (_close_cb) _close_cb(shared_from_this());<br \/>\n        if (_server_close_cb) _server_close_cb(shared_from_this());<br \/>\n    }<br \/>\n    \/\/ \u5c06\u6570\u636e\u653e\u5230\u7f13\u51b2\u533a&#xff0c;\u542f\u52a8\u53ef\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void SendInLoop(Buffer&amp; buffer)<br \/>\n    {<br \/>\n        if (_status &#061;&#061; Disconnected) return;<\/p>\n<p>        _outbuffer.Write(buffer);<br \/>\n        if (!_channel.WriteAble()) _channel.EnableWrite();<br \/>\n    }<br \/>\n    \/\/ \u5e76\u975e\u5b9e\u9645\u7684\u8fde\u63a5\u91ca\u653e\u64cd\u4f5c&#xff0c;\u9700\u8981\u5224\u65ad\u7f13\u51b2\u533a\u662f\u5426\u8fd8\u6709\u6b8b\u7559<br \/>\n    void ShutdownInLoop()<br \/>\n    {<br \/>\n        \/\/ \u8bbe\u7f6e\u4e3a\u534a\u5173\u95ed\u72b6\u6001<br \/>\n        _status &#061; Disconnecting;<br \/>\n        if (_inbuffer.ReadAbleSize() &gt; 0)<br \/>\n        {<br \/>\n            if (_msg_cb)<br \/>\n                _msg_cb(shared_from_this(), &amp;_inbuffer);<br \/>\n        }<\/p>\n<p>        \/\/ \u8981\u4e48\u5199\u5165\u6570\u636e\u7684\u65f6\u5019\u51fa\u9519\u5173\u95ed&#xff0c;\u8981\u4e48\u5c31\u662f\u6ca1\u6709\u5f85\u53d1\u9001\u7684\u6570\u636e&#xff0c;\u76f4\u63a5\u5173\u95ed<br \/>\n        if (_outbuffer.ReadAbleSize() &gt; 0)<br \/>\n        {<br \/>\n            if (!_channel.WriteAble())<br \/>\n                _channel.EnableWrite();<br \/>\n        }<br \/>\n        if (_outbuffer.ReadAbleSize() &#061;&#061; 0)<br \/>\n            Release();<br \/>\n    }<\/p>\n<p>    void EnableInactiveReleaseInLoop(int time)<br \/>\n    {<br \/>\n        \/\/ 1. \u5c06_is_inactive_release \u7f6e\u4e3a true<br \/>\n        _is_inactive_release &#061; true;<br \/>\n        \/\/ 2. \u5982\u679c\u5f53\u524d\u5b9a\u65f6\u9500\u6bc1\u4efb\u52a1\u5df2\u7ecf\u5b58\u5728&#xff0c;\u5219\u5237\u65b0\u4efb\u52a1<br \/>\n        if (_loop-&gt;HasTimer(_conn_id))<br \/>\n            _loop-&gt;ReflushTimerTask(_conn_id);<br \/>\n        \/\/ 3. \u5982\u679c\u4e0d\u5b58\u5728&#xff0c;\u5219\u65b0\u589e\u5b9a\u65f6\u4efb\u52a1<br \/>\n        else<br \/>\n            _loop-&gt;AddTimerTask(_conn_id, time, std::bind(&amp;Connection::Release, this));<br \/>\n    }<\/p>\n<p>    void CancelInactiveReleaseInLoop()<br \/>\n    {<br \/>\n        _is_inactive_release &#061; false;<br \/>\n        if (_loop-&gt;HasTimer(_conn_id))<br \/>\n            _loop-&gt;TimerCancel(_conn_id);<br \/>\n    }<\/p>\n<p>    void UpgradeInLoop(const Any&amp; context, const ConnectionCallback&amp; conn_cb, const MessageCallback&amp; msg_cb,<br \/>\n        const CloseCallback&amp; close_cb, const AnyEventCallback&amp; event_cb)<br \/>\n    {<br \/>\n        _context &#061; context;<br \/>\n        _conn_cb &#061; conn_cb;<br \/>\n        _msg_cb &#061; msg_cb;<br \/>\n        _close_cb &#061; close_cb;<br \/>\n        _event_cb &#061; event_cb;<br \/>\n    }<br \/>\npublic:<br \/>\n    Connection(EventLoop* loop, uint64_t conn_id, int sockfd)<br \/>\n        : _sockfd(sockfd)<br \/>\n        , _socket(sockfd)<br \/>\n        , _conn_id(conn_id)<br \/>\n        , _loop(loop)<br \/>\n        , _is_inactive_release(false)<br \/>\n        , _status(Connecting)   \/\/ \u867d\u7136\u5185\u6838\u4e09\u6b21\u63e1\u624b\u4e86&#xff0c;\u4f46\u662f\u6211\u4eec\u8fd8\u6ca1\u6709\u5bf9\u8fde\u63a5\u8fdb\u884c\u5404\u79cd\u56de\u8c03\u8bbe\u7f6e&#xff0c;\u6240\u4ee5\u6211\u4eec\u4efb\u52a1\u5904\u4e8e\u8fde\u63a5\u5efa\u7acb\u72b6\u6001<br \/>\n        , _channel(loop, _sockfd)<br \/>\n    {<br \/>\n        _channel.SetReadCallback(std::bind(&amp;Connection::HandlerRead, this));<br \/>\n        _channel.SetWriteCallback(std::bind(&amp;Connection::HandlerWrite, this));<br \/>\n        _channel.SetCloseCallback(std::bind(&amp;Connection::HandlerClose, this));<br \/>\n        _channel.SetErrorCallback(std::bind(&amp;Connection::HandlerError, this));<br \/>\n        _channel.SetEventCallback(std::bind(&amp;Connection::HandlerEvent, this));<br \/>\n        \/\/ \u4e0d\u80fd\u5728\u8fd9\u91cc\u76f4\u63a5\u76d1\u63a7\u53ef\u8bfb\u4e8b\u4ef6&#xff0c;\u4f1a\u56e0\u4e3a\u5b9a\u65f6\u4efb\u52a1\u51fa\u95ee\u9898<br \/>\n    }<br \/>\n    ~Connection() { DBG_LOG(&#034;release connection: %p&#034;, this); }<br \/>\n    int Fd() { return _sockfd; }<br \/>\n    uint64_t id() { return _conn_id; }<br \/>\n    \/\/ \u8fde\u63a5\u662f\u5426\u5904\u4e8eConnected\u72b6\u6001<br \/>\n    bool status() { return _status &#061;&#061; Connected; }<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u3001\u83b7\u53d6\u4e0a\u4e0b\u6587<br \/>\n    void SetContext(const Any&amp; context) { _context &#061; context; }<br \/>\n    Any* GetContext() { return &amp;_context; }<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u9636\u6bb5\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    void SetConnectedCallback(const ConnectionCallback&amp; cb) { _conn_cb &#061; cb; }<br \/>\n    void SetMessageCallback(const MessageCallback&amp; cb) { _msg_cb &#061; cb; }<br \/>\n    void SetClosedCallback(const CloseCallback&amp; cb) { _close_cb &#061; cb; }<br \/>\n    void SetAnyEventCallback(const AnyEventCallback&amp; cb) { _event_cb &#061; cb; }<br \/>\n    void SetSvrClosedCallback(const AnyEventCallback&amp; cb) { _server_close_cb &#061; cb; }<\/p>\n<p>    \/\/ \u8fde\u63a5\u83b7\u53d6\u540e&#xff0c;\u5bf9\u8fde\u63a5\u7684\u5404\u79cd\u8bbe\u7f6e\u56de\u8c03\u3001\u542f\u52a8\u4e8b\u4ef6\u76d1\u63a7<br \/>\n    void Established()<br \/>\n    {<br \/>\n        _loop-&gt;RunInLoop(std::bind(&amp;Connection::EstablishedInLoop, this));<br \/>\n    }<br \/>\n    \/\/ \u5c06\u6570\u636e\u5199\u5165\u53d1\u9001\u7f13\u51b2\u533a&#xff0c;\u7136\u540e\u542f\u52a8\u5199\u4e8b\u4ef6\u76d1\u542c<br \/>\n    void Send(const char* data, size_t len)<br \/>\n    {<br \/>\n        \/\/ \u56e0\u4e3a\u5916\u754c\u4f20\u5165\u7684data\u53ef\u80fd\u662f\u4e00\u4e2a\u4e34\u65f6\u7a7a\u95f4&#xff0c;\u800c\u6211\u4eec\u5c06send\u4efb\u52a1push\u5230\u4e86\u4efb\u52a1\u6c60&#xff0c;\u8be5\u4efb\u52a1\u5728\u6267\u884c\u524ddata\u7a7a\u95f4\u53ef\u80fd\u5c31\u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86<br \/>\n        \/\/ \u56e0\u6b64\u6211\u4eec\u9700\u8981\u6784\u5efa\u4e00\u4e2a\u53d8\u91cf\u4fdd\u5b58data\u6570\u636e<\/p>\n<p>        Buffer buf;<br \/>\n        buf.Write(data, len);<br \/>\n        _loop-&gt;RunInLoop(std::bind(&amp;Connection::SendInLoop, this, std::move(buf)));<br \/>\n    }<br \/>\n    \/\/ \u63d0\u4f9b\u7ed9\u7ec4\u4ef6\u4f7f\u7528\u8005\u7684\u63a5\u53e3&#xff0c;\u5e76\u4e0d\u662f\u771f\u6b63\u5173\u95ed\u8fde\u63a5&#xff0c;\u9700\u8981\u68c0\u67e5\u7f13\u51b2\u533a\u662f\u5426\u8fd8\u6709\u6570\u636e<br \/>\n    void Shutdown()<br \/>\n    {<br \/>\n        _loop-&gt;RunInLoop(std::bind(&amp;Connection::ShutdownInLoop, this));<br \/>\n    }<br \/>\n    void Release()<br \/>\n    {<br \/>\n        _loop-&gt;QueueInLoop(std::bind(&amp;Connection::ReleaseInLoop, this));<br \/>\n    }<br \/>\n    \/\/ \u542f\u52a8\u975e\u6d3b\u8dc3\u8fde\u63a5\u7684\u8d85\u65f6\u81ea\u52a8\u9500\u6bc1&#xff0c;\u4f20\u5165\u8d85\u65f6\u65f6\u95f4time<br \/>\n    void EnableInactiveRelease(int time)<br \/>\n    {<br \/>\n        _loop-&gt;RunInLoop(std::bind(&amp;Connection::EnableInactiveReleaseInLoop, this, time));<br \/>\n    }<br \/>\n    \/\/ \u53d6\u6d88\u975e\u6d3b\u8dc3\u8fde\u63a5\u7684\u8d85\u65f6\u81ea\u52a8\u9500\u6bc1<br \/>\n    void CancelInactiveRelease()<br \/>\n    {<br \/>\n        _loop-&gt;RunInLoop(std::bind(&amp;Connection::CancelInactiveReleaseInLoop, this));<br \/>\n    }<br \/>\n    \/\/ \u534f\u8bae\u5207\u6362 &#8211;&gt; \u91cd\u7f6e\u4e0a\u4e0b\u6587\u3001\u9636\u6bb5\u56de\u8c03\u51fd\u6570 &#8212; \u975e\u7ebf\u7a0b\u5b89\u5168<br \/>\n    void Upgrade(const Any&amp; context, const ConnectionCallback&amp; conn_cb, const MessageCallback&amp; msg_cb,<br \/>\n        const CloseCallback&amp; close_cb, const AnyEventCallback&amp; event_cb)<br \/>\n    {<br \/>\n        _loop-&gt;AssertInLoop();<br \/>\n        _loop-&gt;RunInLoop(std::bind(&amp;Connection::UpgradeInLoop, this, context, conn_cb, msg_cb, close_cb, event_cb));<br \/>\n    }<br \/>\n}; <\/p>\n<\/p>\n<h4 id=\"Accept\" style=\"background-color:transparent\">Accept<\/h4>\n<p>\u8fd9\u4e2a\u6a21\u5757\u7684\u610f\u4e49\u4e3b\u8981\u662f\u5bf9\u4e8e\u76d1\u542c\u5957\u63a5\u5b57\u8fdb\u884c\u7ba1\u7406&#xff0c;\u4e3b\u8981\u7684\u529f\u80fd\u662f&#xff1a;<\/p>\n<li><span style=\"color:#7b7f82\">\u521b\u5efa\u4e00\u4e2a\u76d1\u542c\u5957\u63a5\u5b57<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u542f\u52a8\u8bfb\u4e8b\u4ef6\u76d1\u63a7<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4e8b\u4ef6\u89e6\u53d1\u540e&#xff0c;\u83b7\u53d6\u65b0\u8fde\u63a5<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u8c03\u7528\u65b0\u8fde\u63a5\u83b7\u53d6\u6210\u529f\u540e\u7684\u56de\u8c03\u51fd\u6570<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4e3a\u65b0\u8fde\u63a5\u521b\u5efaConnection\u8fdb\u884c\u7ba1\u7406<\/span><\/li>\n<p>\u5bf9\u4e8e\u65b0\u8fde\u63a5\u5982\u4f55\u5904\u7406\u5e94\u8be5\u662f\u670d\u52a1\u5668\u6a21\u5757\u6765\u7ba1\u7406\u7684&#xff0c;\u5373\u8bbe\u7f6e\u7ed9listenfd\u7684\u8bfb\u56de\u8c03\u51fd\u6570\u7528\u4e8e\u5904\u7406\u65b0\u8fde\u63a5\u3002\u56e0\u4e3a\u4e3bReactor\u662f\u4e13\u95e8\u8d1f\u8d23\u83b7\u53d6\u65b0\u8fde\u63a5\u7684&#xff0c;\u4e00\u4e2aReactor\u5bf9\u5e94\u4e00\u4e2aEventLoop&#xff0c;\u6240\u4ee5\u4e3b\u7ebf\u7a0b\u4f5c\u4e3a\u4e3bReactor\u7684\u4e13\u5c5e\u7ebf\u7a0b&#xff0c;\u5fc5\u7136\u9700\u8981\u4e00\u4e2aEventLoop\u5bf9\u8c61\u652f\u6301listen\u7684\u5404\u79cd\u64cd\u4f5c&#xff0c;\u6240\u4ee5\u9700\u8981\u5916\u5356\u4f20\u5165EventLoop\u6307\u9488<\/p>\n<p>class Acceptor<br \/>\n{<br \/>\n    using AcceptCallback &#061; std::function&lt;void(int)&gt;;<br \/>\nprivate:<br \/>\n    Socket _socket;             \/\/ \u76d1\u542c\u5957\u63a5\u5b57\u7ba1\u7406<br \/>\n    EventLoop* _loop;           \/\/ \u5bf9\u76d1\u542c\u5957\u63a5\u5b57\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u7ba1\u7406<br \/>\n    Channel _channel;           \/\/ \u76d1\u542c\u5957\u63a5\u5b57\u4e8b\u4ef6\u7ba1\u7406<br \/>\n    AcceptCallback _accept_cb;  \/\/ \u76d1\u542c\u5230\u65b0\u8fde\u63a5\u65f6\u7684\u56de\u8c03\u51fd\u6570<br \/>\nprivate:<br \/>\n    \/\/ \u76d1\u542c\u5230\u65b0\u8fde\u63a5\u65f6\u7684\u8bfb\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570<br \/>\n    void HandlerRead()<br \/>\n    {<br \/>\n        int newfd &#061; _socket.Accept();<br \/>\n        if (newfd &lt; 0) return;<br \/>\n        if (_accept_cb) _accept_cb(newfd);<br \/>\n    }<br \/>\n    int CreateServer(int port)<br \/>\n    {<br \/>\n        bool ret &#061; _socket.CreateServer(port);<br \/>\n        assert(ret &#061;&#061; true);<br \/>\n        return _socket.Fd();<br \/>\n    }<br \/>\npublic:<br \/>\n    \/*\u6ce8\u610f&#xff1a;\u542f\u52a8\u8bfb\u4e8b\u4ef6\u76d1\u63a7\u4e0d\u80fd\u653e\u5728\u6784\u9020\u51fd\u6570\u5185&#xff01;\u56e0\u4e3a\u5982\u679c\u6784\u9020\u51fd\u6570\u542f\u52a8\u4e86\u8bfb\u4e8b\u4ef6\u76d1\u63a7\u4e4b\u540e&#xff0c;\u7acb\u9a6c\u6765\u4e86\u4e00\u4e2a\u8fde\u63a5*\/<br \/>\n    \/*\u90a3\u4e48\u6b64\u65f6\u6211\u4eec\u7684AcceptCallback\u8fd8\u6ca1\u6709\u8bbe\u7f6e&#xff0c;\u6240\u4ee5\u8c03\u7528HandlerRead\u65f6\u4e0d\u4f1a\u8c03\u7528_accept_cb&#xff0c;\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4*\/<br \/>\n    \/*\u65b0\u8fde\u63a5\u5f97\u4e0d\u5230\u670d\u52a1&#xff0c;\u5e76\u4e14newfd\u6cc4\u9732&#xff01;*\/<br \/>\n    Acceptor(EventLoop* loop, uint16_t port)<br \/>\n        : _loop(loop)<br \/>\n        , _channel(loop, _socket.Fd())<br \/>\n        , _socket(CreateServer(port))<br \/>\n    {<br \/>\n        _channel.SetReadCallback(std::bind(&amp;Acceptor::HandlerRead, this));<br \/>\n    }<br \/>\n    void SetAcceptCallback(const AcceptCallback&amp; cb) { _accept_cb &#061; cb; }<br \/>\n    void Listen() { _channel.EnableRead(); }<br \/>\n}; <\/p>\n<h4 id=\"LoopThread\">LoopThread<\/h4>\n<p>\u8be5\u6a21\u5757\u7684\u529f\u80fd\u4e3b\u8981\u662f\u6765\u628aEventLoop\u6a21\u5757\u548c\u7ebf\u7a0b\u7ed3\u5408\u5728\u4e00\u8d77&#xff0c;\u8981\u5f62\u6210\u7684\u6700\u7ec8\u6548\u679c\u662f&#xff0c;\u8ba9EventLoop\u548c\u7ebf\u7a0b\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\u3002\u5728EventLoop\u6a21\u5757\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61&#xff0c;\u5728\u6784\u9020\u7684\u65f6\u5019\u5c31\u4f1a\u521d\u59cb\u5316\u7ebf\u7a0b\u7684id&#xff0c;\u800c\u5f53\u540e\u9762\u9700\u8981\u8fd0\u884c\u4e00\u4e2a\u64cd\u4f5c\u7684\u65f6\u5019&#xff0c;\u5c31\u5224\u65ad\u662f\u5426\u8fd0\u884c\u5728EventLoop\u6a21\u5757\u5bf9\u5e94\u7684\u7ebf\u7a0b\u4e2d&#xff0c;\u5982\u679c\u662f\u5c31\u4ee3\u8868\u662f\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u4e0d\u662f\u5c31\u4ee3\u8868\u5f53\u524d\u8fd0\u884c\u7684\u7ebf\u7a0b\u4e0d\u662fEventLoop\u7ebf\u7a0b<\/p>\n<p>\u4e0d\u80fd\u76f4\u63a5\u5728\u4e3b\u7ebf\u7a0b\u5185\u5b9e\u4f8b\u5316\u4e09\u4e2aEventLoop&#xff0c;\u56e0\u4e3a\u6b64\u65f6EventLoop\u5185\u7684thread_id\u90fd\u4f1a\u88ab\u8bbe\u7f6e\u4e3a\u4e3b\u7ebf\u7a0b\u7684ID&#xff0c;\u6240\u4ee5\u5fc5\u987b\u5728\u6bcf\u4e2a\u65b0\u7ebf\u7a0b\u5185\u90e8\u8fdb\u884c\u5b9e\u4f8b\u5316EventLoop&#xff0c;\u5e76\u4e14\u5728\u6784\u9020EventLoop\u5bf9\u8c61\u5230\u8bbe\u7f6e\u65b0\u7684\u7ebf\u7a0bid\u8fd9\u4e2a\u671f\u95f4\u662f\u4e0d\u53ef\u63a7\u7684<\/p>\n<p>\u6240\u4ee5\u5c31\u8981\u6784\u9020\u4e00\u4e2a\u65b0\u7684\u6a21\u5757&#xff0c;LoopThread&#xff0c;\u8fd9\u4e2a\u6a21\u5757\u7684\u610f\u4e49\u5c31\u662f\u628aEventLoop\u548c\u7ebf\u7a0b\u653e\u5230\u4e00\u5757&#xff0c;\u4e3b\u8981\u7684\u601d\u8def\u5c31\u662f\u521b\u5efa\u7ebf\u7a0b&#xff0c;\u5728\u7ebf\u7a0b\u4e2d\u5b9e\u4f8b\u5316\u4e00\u4e2aEventLoop\u5bf9\u8c61&#xff0c;\u8fd9\u6837\u53ef\u4ee5\u5411\u5916\u90e8\u8fd4\u56de\u4e00\u4e2a\u5b9e\u4f8b\u5316\u7684EventLoop<\/p>\n<p>\u6d41\u7a0b&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u521b\u5efa\u7ebf\u7a0b<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5728\u7ebf\u7a0b\u4e2d\u5b9e\u4f8b\u5316EventLoop\u5bf9\u8c61<\/span><\/li>\n<\/ul>\n<p>\u63a5\u53e3&#xff1a;\u00a0<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u8fd4\u56de\u5b9e\u4f8b\u5316\u7684EventLoop<\/span>\u00a0<\/li>\n<\/ul>\n<p>\u5b9e\u73b0\u00a0<\/p>\n<p>class LoopThread<br \/>\n{<br \/>\nprivate:<br \/>\n    std::thread _thread;            \/\/ \u4e00\u4e2aEventLoop\u5bf9\u5e94\u4e00\u4e2a\u7ebf\u7a0b<br \/>\n    EventLoop* _loop;               \/\/ \u4ece\u5c5eReactor\u7ba1\u7406\u6240\u6709fd\u4e8b\u4ef6<br \/>\n    std::mutex _mutex;              \/\/ \u4e3a\u4e86\u4fdd\u8bc1\u83b7\u53d6_loop\u4e4b\u524d&#xff0c;loop\u5df2\u7ecf\u88ab\u521d\u59cb\u5316&#xff0c;\u6240\u4ee5\u9700\u8981\u7ebf\u7a0b\u9501<br \/>\n    std::condition_variable _cond;  \/\/ \u7ebf\u7a0b\u540c\u6b65\u63a7\u5236\u83b7\u53d6\u5df2\u7ecf\u521d\u59cb\u5316\u7684_loop<br \/>\nprivate:<br \/>\n    \/\/ \u7ebf\u7a0b\u51fd\u6570\u6267\u884c\u4f8b\u7a0b<br \/>\n    void ThreadEntry()<br \/>\n    {<br \/>\n        \/\/ \u5b9e\u529b\u5316EventLoop<br \/>\n        EventLoop loop;<br \/>\n        {<br \/>\n            \/\/ \u521d\u59cb\u5316EventLoop\u7684\u65f6\u5019&#xff0c;\u5c31\u522b\u6253\u65ad&#xff0c;\u6240\u4ee5\u52a0\u9501<br \/>\n            std::unique_lock&lt;std::mutex&gt; lock(_mutex);<br \/>\n            _loop &#061; &amp;loop;<br \/>\n            \/\/ \u5524\u9192\u53ef\u80fd\u65e9\u5df2\u7ecf\u8c03\u7528GetLoop\u800c\u7b49\u5f85\u7684\u6267\u884c\u6d41<br \/>\n            _cond.notify_all();<br \/>\n        }<br \/>\n        loop.Start();<br \/>\n    }<br \/>\npublic:<br \/>\n    LoopThread()<br \/>\n        :_thread(std::thread(&amp;LoopThread::ThreadEntry, this))<br \/>\n        , _loop(nullptr)<br \/>\n    {}<\/p>\n<p>    \/\/ \u5916\u90e8\u83b7\u53d6EventLoop*<br \/>\n    EventLoop* GetLoop()<br \/>\n    {<br \/>\n        EventLoop* loop &#061; nullptr;<br \/>\n        {<br \/>\n            std::unique_lock&lt;std::mutex&gt; lock(_mutex);<br \/>\n            \/\/ \u5982\u679c_loop\u4e3a\u7a7a&#xff0c;\u5219\u7b49\u5f85<br \/>\n            _cond.wait(lock, [&amp;](){ return _loop !&#061; NULL; });\/\/loop\u4e3aNULL\u5c31\u4e00\u76f4\u963b\u585e<br \/>\n            loop &#061; _loop;<br \/>\n        }<br \/>\n        return loop;<br \/>\n    }<\/p>\n<p>}; <\/p>\n<p>\u00a0<\/p>\n<p>\u7ec6\u8282&#xff1a;\u00a0<\/p>\n<li><span style=\"color:#7b7f82\">\u5728\u7ebf\u7a0b\u5185\u521d\u59cb\u5316EventLoop<\/span><\/li>\n<li><span style=\"color:#7b7f82\">GetLoop\u51fd\u6570&#xff0c;\u5728\u5916\u90e8\u53ef\u4ee5\u83b7\u53d6\u5230EventLoop\u5bf9\u8c61&#xff0c;\u65b9\u4fbf\u65b0\u8fde\u63a5\u6307\u5411EventLoop<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u6709\u53ef\u80fd\u7ebf\u7a0b\u521b\u5efa\u4e86&#xff0c;\u4f46\u662fEventLoop\u8fd8\u6ca1\u6709\u5b9e\u4f8b\u5316&#xff0c;\u4f46\u662f\u5916\u90e8\u8c03\u7528\u4e86GetLoop\u51fd\u6570&#xff0c;\u6b64\u65f6\u4f1a\u83b7\u5f97nullptr&#xff0c;\u8fd9\u5c31\u8868\u660e\u8be5\u8fde\u63a5\u4f1a\u5f97\u4e0d\u5230\u670d\u52a1&#xff0c;\u6240\u4ee5\u6211\u4eec\u9700\u8981\u52a0\u9501&#xff0c;\u5728\u83b7\u53d6Loop\u4e4b\u524d\u5fc5\u987b\u7b49\u5f85\u521d\u59cb\u5316\u3002<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u6211\u4eec\u4e0d\u4f7f\u7528new\u521b\u5efa\u5806\u4e0a\u7684EventLoop&#xff0c;\u800c\u662f\u5728\u7ebf\u7a0b\u6267\u884c\u7684\u4f8b\u7a0b\u4e2d\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5c40\u90e8\u5bf9\u8c61&#xff0c;\u8fd9\u6837\u5c31\u4f1a\u4f7fEventLoop\u751f\u547d\u5468\u671f\u4e0eThreadLoop\u7ed1\u5b9a&#xff0c;\u56e0\u4e3a\u5728\u7ebf\u7a0b\u6267\u884c\u7684\u4f8b\u7a0b\u4e2d&#xff0c;\u6211\u4eec\u4f1a\u6267\u884cloop\u7684start\u51fd\u6570&#xff0c;\u8fd9\u4e2astart\u662f\u4e00\u4e2a\u6b7b\u5faa\u73af&#xff0c;\u4e00\u65e6\u51fa\u9519\u4e86\u90a3\u4e48\u5c31\u662f\u4ece\u5c5ereactor\u53d6\u6d88\u4e86&#xff0c;\u76f8\u5e94\u7684EventLoop\u4e5f\u5e94\u8be5\u9500\u6bc1<\/span><\/li>\n<\/p>\n<\/p>\n<h4 id=\"LoopThreadPool\">LoopThreadPool<\/h4>\n<p><span style=\"color:#7b7f82\">\u4e3bReactor\u5c31\u662f\u6211\u4eec\u7684\u4e3b\u7ebf\u7a0b&#xff0c;\u53ea\u8d1f\u8d23\u8fde\u63a5\u7684\u83b7\u53d6\u3002\u4ece\u5c5eReactor\u8d1f\u8d23\u65b0\u8fde\u63a5\u7684\u4e8b\u4ef6\u76d1\u63a7\u53ca\u5904\u7406\u3002<\/span><\/p>\n<p><span style=\"color:#7b7f82\">\u8bbe\u8ba1\u601d\u60f3&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u9488\u5bf9LoopThread\u8bbe\u8ba1\u4e00\u4e2a\u7ebf\u7a0b\u6c60&#xff0c;\u4fbf\u4e8e\u4f7f\u7528\u8005\u5bf9\u7ebf\u7a0b\u7684\u63a7\u5236<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5bf9\u6240\u6709\u7684LoopThread\u8fdb\u884c\u7ba1\u7406\u53ca\u5206\u914d<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#7b7f82\">\u529f\u80fd&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u7ebf\u7a0b\u6570\u91cf\u53ef\u914d\u7f6e<\/span>\n<ul>\n<li><span style=\"color:#7b7f82\">\u6839\u636e\u7528\u6237\u4f20\u9012\u7684\u53c2\u6570\u51b3\u5b9a\u7ebf\u7a0b\u7684\u6570\u91cf&#xff0c;\u56e0\u6b64\u4ece\u5c5e\u7ebf\u7a0b\u6570\u91cf\u53ef\u80fd\u4e3a0&#xff0c;\u4e5f\u5c31\u662f\u5355Reactor\u6a21\u5f0f\u7684\u670d\u52a1\u5668&#xff0c;\u6b64\u65f6\u8868\u793a\u670d\u52a1\u5668\u662f\u8f7b\u91cf\u7ea7\u7684&#xff0c;\u4e3b\u7ebf\u7a0b\u65e2\u8d1f\u8d23\u83b7\u53d6\u8fde\u63a5&#xff0c;\u53c8\u8d1f\u8d23\u4e8b\u4ef6\u7684\u5904\u7406\u3002<\/span><\/li>\n<\/ul>\n<\/li>\n<li><span style=\"color:#7b7f82\">\u5bf9\u6240\u6709\u7ebf\u7a0b\u8fdb\u884c\u7ba1\u7406&#xff0c;\u5c31\u662f\u7ba1\u7406 \u30100&#xff0c;n\u3011 \u4e2a LoopThread \u5bf9\u8c61<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u63d0\u4f9b\u7ebf\u7a0b\u5206\u914d\u7684\u529f\u80fd<\/span>\n<ul>\n<li><span style=\"color:#7b7f82\">\u5982\u679c\u67090\u4e2a\u4ece\u5c5e\u7ebf\u7a0b&#xff0c;\u5219\u76f4\u63a5\u5206\u914d\u7ed9\u4e3b\u7ebf\u7a0b\u7684EventLoop\u8fdb\u884c\u5904\u7406\u3002<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5982\u679c\u6709n\u4e2a\u4ece\u5c5e\u7ebf\u7a0b&#xff0c;\u5219\u91c7\u7528\u8f6e\u8f6c\u7684\u601d\u60f3&#xff0c;\u5bf9\u65b0\u8fde\u63a5Connection\u8fdb\u884c\u4ece\u5c5eReactor\u7ebf\u7a0b\u5206\u914d&#xff0c;\u5c31\u662f\u83b7\u53d6\u4eceLoopThread\u4e2d\u83b7\u53d6\u8f6e\u8f6c\u5230\u7684\u7ebf\u7a0b\u7684EventLoop\u5730\u5740&#xff0c;\u5c06\u5176\u8bbe\u7f6e\u7ed9\u65b0\u8fde\u63a5\u7684Connection\u3002<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u00a0\u5b9e\u73b0<\/p>\n<p>class LoopThreadPool<br \/>\n{<br \/>\nprivate:<br \/>\n    int _thread_count;                \/\/ \u4ece\u5c5e\u7ebf\u7a0b\u7684\u6570\u91cf<br \/>\n    int _next_idx;                    \/\/ \u7528\u4e8e\u63a7\u5236\u8f6e\u8f6c\u7684\u4e0b\u6807<br \/>\n    EventLoop* _baseloop;             \/\/ \u5f53\u4ece\u5c5e\u7ebf\u7a0b\u6570\u91cf\u5927\u4e8e0&#xff0c;\u5219baseloop\u53ea\u8d1f\u8d23\u83b7\u53d6\u65b0\u8fde\u63a5\u3002\u5982\u679c\u7b49\u4e8e0&#xff0c;\u5219\u65e2\u8d1f\u8d23\u83b7\u53d6\u65b0\u8fde\u63a5&#xff0c;\u53c8\u8d1f\u8d23\u4e8b\u4ef6\u5904\u7406<br \/>\n    std::vector&lt;LoopThread*&gt; _threads;\/\/ \u4fdd\u5b58\u5404\u4e2a\u4ece\u5c5e\u7ebf\u7a0b<br \/>\n    std::vector&lt;EventLoop*&gt; _loops;   \/\/ \u4fdd\u5b58\u5404\u4e2a\u7ebf\u7a0b\u7684EventLoop\u6307\u9488<br \/>\npublic:<br \/>\n    LoopThreadPool(EventLoop* baseloop)<br \/>\n      : _baseloop(baseloop)<br \/>\n      , _thread_count(0)<br \/>\n      , _next_idx(0)<br \/>\n    {}<br \/>\n    void SetCount(int count) { _thread_count &#061; count; }<br \/>\n    void Create()<br \/>\n    {<br \/>\n        if (_thread_count &gt; 0)<br \/>\n        {<br \/>\n            _threads.resize(_thread_count);<br \/>\n            _loops.resize(_thread_count);<br \/>\n            for (int i &#061; 0; i &lt; _thread_count; &#043;&#043;i)<br \/>\n            {<br \/>\n              \/\/ \u521b\u5efa\u7ebf\u7a0b\u540e&#xff0c;\u5728\u7ebf\u7a0b\u6267\u884c\u7684\u4f8b\u7a0b\u5185\u90e8\u5b9e\u4f8b\u5316EventLoop&#xff0c;\u6240\u4ee5\u5982\u679c\u963b\u585e\u4e86&#xff0c;\u90a3\u4e48\u4e0d\u4f1a\u6267\u884c\u5230\u4e0b\u4e00\u6761\u8bed\u53e5\u83b7\u53d6\u4e00\u4e2a\u7a7a\u7684EventLoop<br \/>\n              _threads[i] &#061; new LoopThread();<br \/>\n              _loops[i] &#061; _threads[i]-&gt;GetLoop();<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    EventLoop* NextLoop()<br \/>\n    {<br \/>\n        if (_thread_count &#061;&#061; 0)<br \/>\n            return _baseloop;<br \/>\n        _next_idx &#061; (_next_idx &#043; 1) % _thread_count;<br \/>\n        return _loops[_next_idx];<br \/>\n    }<br \/>\n}; <\/p>\n<p>\u00a0<\/p>\n<h4 id=\"TcpServer\">TcpServer<\/h4>\n<p>\u6574\u5408\u6240\u6709\u6a21\u5757&#xff0c;\u65b9\u4fbf\u4e0a\u5c42\u76f4\u63a5\u4f7f\u7528\u3002\u4e0d\u9700\u8981\u7528\u6237\u624b\u52a8\u521b\u5efaEventLoop\u5bf9\u8c61baseloop&#xff0c;\u4e5f\u4e0d\u9700\u8981\u521b\u5efaLoopThreadPool&#xff0c;\u53ea\u9700\u8981\u4f20\u5165\u7aef\u53e3\u53f7\u3001\u4ece\u5c5e\u7ebf\u7a0b\u6c60\u7684\u6570\u91cf<\/p>\n<p>\u7ba1\u7406&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">Acceptor\u5bf9\u8c61&#xff0c;\u521b\u5efa\u4e00\u4e2a\u76d1\u542c\u5957\u63a5\u5b57<\/span><\/li>\n<li><span style=\"color:#7b7f82\">EventLoop\u5bf9\u8c61&#xff0c;\u5373baseloop&#xff0c;\u5b9e\u73b0\u5bf9\u76d1\u542c\u5957\u63a5\u5b57\u7684\u4e8b\u4ef6\u76d1\u63a7<\/span><\/li>\n<li><span style=\"color:#7b7f82\">std::unordered_map&lt;uint64_t, std::shared_ptr&lt;Connection&gt;&gt; _conns&#xff0c;\u5b9e\u73b0\u5bf9\u6240\u6709\u65b0\u5efa\u8fde\u63a5\u7684\u7ba1\u7406<\/span><\/li>\n<li><span style=\"color:#7b7f82\">LoopThreadPool\u5bf9\u8c61&#xff0c;\u521b\u5efaloop\u7ebf\u7a0b\u6c60&#xff0c;\u5bf9\u65b0\u5efa\u8fde\u63a5\u8f6e\u8f6c\u5206\u914dEventLoop&#xff0c;\u5b9e\u73b0\u4ece\u5c5eReactor\u7684\u8d1f\u8f7d\u5747\u8861<\/span><\/li>\n<\/ul>\n<p>\u529f\u80fd&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#7b7f82\">\u8bbe\u7f6e\u4ece\u5c5e\u7ebf\u7a0b\u6c60\u4e2d\u7ebf\u7a0b\u7684\u6570\u91cf<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u542f\u52a8\u670d\u52a1\u5668<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u8bbe\u7f6e\u5404\u79cd\u9636\u6bb5\u56de\u8c03\u51fd\u6570&#xff08;\u8fde\u63a5\u5efa\u7acb\u5b8c\u6210\u3001\u4e1a\u52a1\u5904\u7406\u3001\u5173\u95ed\u3001\u4efb\u610f&#xff09;&#xff0c;\u662f\u7528\u6237\u8bbe\u7f6e\u7ed9TcpServer&#xff0c;TcpServer\u518d\u8bbe\u7f6e\u7ed9\u65b0\u83b7\u53d6\u8fde\u63a5\u7684Connection<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u662f\u5426\u542f\u52a8\u975e\u6d3b\u8dc3\u8fde\u63a5\u8d85\u65f6\u9500\u6bc1\u529f\u80fd<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u6dfb\u52a0\u5b9a\u65f6\u4efb\u52a1\u529f\u80fd\u00a0<\/span><\/li>\n<\/ul>\n<p>\u00a0\u6d41\u7a0b&#xff1a;<\/p>\n<li><span style=\"color:#7b7f82\">\u5728TcpServer\u4e2d\u5b9e\u4f8b\u5316\u4e00\u4e2aAcceptor\u5bf9\u8c61&#xff0c;\u4ee5\u53ca\u4e00\u4e2aEventLoop\u5bf9\u8c61&#xff08;baseloop&#xff09;<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5c06Acceptor\u6302\u5230baseloop\u4e0a\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4e00\u65e6Acceptor\u5bf9\u8c61\u5373listenfd\u8bfb\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u5219\u6267\u884c\u8bfb\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570\u83b7\u53d6\u65b0\u8fde\u63a5<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5bf9\u65b0\u8fde\u63a5\u521b\u5efa\u4e00\u4e2aConnection\u5bf9\u8c61\u8fdb\u884c\u7edf\u7b79\u7ba1\u7406<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u5c06\u65b0\u8fde\u63a5\u5bf9\u5e94\u7684Connection\u6302\u5230LoopThreadPool\u8fd4\u56de\u7684EventLoop\u4e0a\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u7ba1\u7406<\/span><\/li>\n<li><span style=\"color:#7b7f82\">\u4e00\u65e6Connection\u5bf9\u5e94\u7684\u8fde\u63a5\u5c31\u7eea\u4e86\u53ef\u8bfb\u4e8b\u4ef6&#xff0c;\u5219\u6267\u884c\u53ef\u8bfb\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u8bfb\u53d6\u6570\u636e\u7136\u540e\u518d\u8c03\u4f63TcpServer\u8bbe\u7f6e\u7684\u9636\u6bb5\u56de\u8c03\u51fd\u6570<\/span><\/li>\n<\/p>\n<p>\u00a0\u5b9e\u73b0<\/p>\n<p>class TcpServer<br \/>\n{<br \/>\nprivate:<br \/>\n    uint64_t _conn_id;          \/\/ \u552f\u4e00\u6807\u8bc6\u8fde\u63a5\u7684id&#xff0c;\u81ea\u52a8\u589e\u957f<br \/>\n    int _timeout;               \/\/ \u975e\u6d3b\u8dc3\u8fde\u63a5\u7684\u8d85\u65f6\u65f6\u95f4<br \/>\n    bool _is_inactive_release;  \/\/ \u662f\u5426\u542f\u52a8\u975e\u6d3b\u8dc3\u9500\u6bc1\u529f\u80fd<br \/>\n    int _port;                  \/\/ \u670d\u52a1\u5668\u8981\u7ed1\u5b9a\u7684\u7aef\u53e3\u53f7<br \/>\n    EventLoop _baseloop;        \/\/ \u76d1\u542c\u5957\u63a5\u5b57\u5bf9\u5e94\u7684EventLoop<br \/>\n    Acceptor _acceptor;         \/\/ \u76d1\u542c\u5957\u63a5\u5b57\u7ba1\u7406<br \/>\n    LoopThreadPool _threadpool; \/\/ \u4ece\u5c5e\u7ebf\u7a0b\u7ba1\u7406<br \/>\n    std::unordered_map&lt;uint64_t, std::shared_ptr&lt;Connection&gt;&gt; _conns; \/\/ shared_ptr\u4fdd\u5b58\u6240\u6709\u8fde\u63a5<\/p>\n<p>    using ConnectedCallback &#061; std::function&lt;void(const std::shared_ptr&lt;Connection&gt;&amp;)&gt;;<br \/>\n    using MessageCallback &#061; std::function&lt;void(const std::shared_ptr&lt;Connection&gt;&amp;, Buffer*)&gt;;<br \/>\n    using CloseCallback &#061; std::function&lt;void(const std::shared_ptr&lt;Connection&gt;&amp;)&gt;;<br \/>\n    using AnyEventCallback &#061; std::function&lt;void(const std::shared_ptr&lt;Connection&gt;&amp;)&gt;;<br \/>\n    ConnectedCallback _conn_cb;\/\/ \u8fde\u63a5\u5efa\u7acb\u540e&#xff0c;\u4e0a\u5c42\u9700\u8981\u7684\u4e1a\u52a1\u5904\u7406\u51fd\u6570<br \/>\n    MessageCallback _msg_cb;    \/\/ \u63a5\u6536\u6570\u636e\u540e&#xff0c;\u4e0a\u5c42\u9700\u8981\u7684\u4e1a\u52a1\u5904\u7406\u51fd\u6570<br \/>\n    CloseCallback _close_cb;    \/\/ \u8fde\u63a5\u5173\u95ed\u540e&#xff0c;\u4e0a\u5c42\u9700\u8981\u7684\u4e1a\u52a1\u5904\u7406\u51fd\u6570<br \/>\n    AnyEventCallback _event_cb; \/\/ \u4efb\u610f\u4e8b\u4ef6\u89e6\u53d1&#xff0c;\u4e0a\u5c42\u9700\u8981\u7684\u4e1a\u52a1\u5904\u7406\u51fd\u6570<br \/>\nprivate:<br \/>\n    \/\/ \u76d1\u542c\u5957\u63a5\u5b57\u7684\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u56de\u8c03<br \/>\n    void NewConnection(int fd)<br \/>\n    {<br \/>\n        _conn_id&#043;&#043;;<br \/>\n        std::shared_ptr&lt;Connection&gt; conn(new Connection(_threadpool.NextLoop(), _conn_id, fd));<\/p>\n<p>        conn-&gt;SetConnectedCallback(_conn_cb);  \/\/ \u8fde\u63a5\u5efa\u7acb\u6210\u529f\u65f6&#xff0c;\u4e0a\u5c42\u4e1a\u52a1\u5904\u7406\u7684\u56de\u8c03\u51fd\u6570<br \/>\n        conn-&gt;SetMessageCallback(_msg_cb);      \/\/ \u7f13\u51b2\u533a\u6709\u6570\u636e\u5c31\u7eea&#xff0c;\u4e0a\u5c42\u4e1a\u52a1\u5904\u7406\u7684\u56de\u8c03\u51fd\u6570<br \/>\n        conn-&gt;SetClosedCallback(_close_cb);      \/\/ \u8fde\u63a5\u5173\u95ed\u65f6&#xff0c;\u9700\u8981\u5c06_conns\u4e2d\u5bf9Connection\u8bb0\u5f55shared_ptr\u5220\u9664\u7684\u7684\u56de\u8c03\u51fd\u6570<br \/>\n        conn-&gt;SetAnyEventCallback(_event_cb);<br \/>\n        conn-&gt;SetSvrClosedCallback(std::bind(&amp;TcpServer::RemoveConnection, this, std::placeholders::_1));<\/p>\n<p>        \/\/ \u662f\u5426\u542f\u52a8\u975e\u6d3b\u8dc3\u9500\u6bc1<br \/>\n        if (_is_inactive_release)<br \/>\n            conn-&gt;EnableInactiveRelease(_timeout);<\/p>\n<p>        \/\/ \u5c31\u7eea\u521d\u59cb\u5316<br \/>\n        conn-&gt;Established();<br \/>\n        _conns.insert(std::make_pair(_conn_id, conn));<br \/>\n    }<br \/>\n    void RemoveConnectionInLoop(const std::shared_ptr&lt;Connection&gt;&amp; conn)<br \/>\n    {<br \/>\n        if (!conn) return;<br \/>\n        auto it &#061; _conns.find(conn-&gt;id());<br \/>\n        if (it !&#061; _conns.end()) {<br \/>\n            _conns.erase(it);<br \/>\n        }<br \/>\n    }<br \/>\n    void RunAfterInLoop(const TaskFunc&amp; task, int delay)<br \/>\n    {<br \/>\n        _conn_id&#043;&#043;;<br \/>\n        _baseloop.AddTimerTask(_conn_id, delay, task);<br \/>\n    }<br \/>\n    \/\/ \u4ece_connections\u4e2d\u79fb\u9664\u5bf9\u5e94\u94fe\u63a5\u7684shared_ptr<br \/>\n    void RemoveConnection(const std::shared_ptr&lt;Connection&gt;&amp; conn)<br \/>\n    {<br \/>\n        _baseloop.RunInLoop(std::bind(&amp;TcpServer::RemoveConnectionInLoop, this, conn));<br \/>\n    }<br \/>\npublic:<br \/>\n    TcpServer(int port)<br \/>\n        : _port(port)<br \/>\n        , _conn_id(0)<br \/>\n        , _timeout(0)<br \/>\n        , _is_inactive_release(false)<br \/>\n        , _acceptor(&amp;_baseloop, port)<br \/>\n        , _threadpool(&amp;_baseloop)<br \/>\n    {<br \/>\n        \/\/ \u4e0d\u80fd\u76f4\u63a5\u5728\u8fd9\u91ccCreate&#xff0c;\u56e0\u4e3a\u6211\u4eec\u8fd8\u6ca1\u6709\u8bbe\u7f6ethreadcount&#xff0c;\u6240\u4ee5\u9ed8\u8ba4\u4e3a0&#xff0c;\u8fd4\u56deEventLoop\u65f6\u4f1a\u51fa\u9519&#xff0c;\u5e94\u8be5\u653e\u5728Start\u4e2d<br \/>\n        \/\/ _threadpool.Create();   \/\/ \u521b\u5efa\u7ebf\u7a0b\u6c60\u4e2d\u7684\u4ece\u5c5e\u7ebf\u7a0b<br \/>\n        _acceptor.SetAcceptCallback(std::bind(&amp;TcpServer::NewConnection, this, std::placeholders::_1));<br \/>\n        _acceptor.Listen();     \/\/ \u5c06\u76d1\u542c\u5957\u63a5\u5b57\u6302\u5230baseloop\u4e0a<br \/>\n    }<br \/>\n    \/\/ \u8bbe\u7f6e\u4ece\u5c5e\u7ebf\u7a0b\u6570\u91cf<br \/>\n    void SetThreadCount(int count) { _threadpool.SetCount(count); }<\/p>\n<p>    \/\/ \u7528\u4e8e\u542f\u52a8\u975e\u6d3b\u8dc3\u8fde\u63a5\u7684\u9500\u6bc1\u529f\u80fd<br \/>\n    void EnableInactiveRelease(int timeout)<br \/>\n    {<br \/>\n        _is_inactive_release &#061; true;<br \/>\n        _timeout &#061; timeout;<br \/>\n    }<\/p>\n<p>    \/\/ \u7528\u4e8e\u6dfb\u52a0\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1<br \/>\n    void RunAfter(const TaskFunc&amp; cb, int delay)<br \/>\n    {<br \/>\n        _baseloop.RunInLoop(std::bind(&amp;TcpServer::RunAfterInLoop, this, cb, delay));<br \/>\n    }<\/p>\n<p>    void Start()<br \/>\n    {<br \/>\n        \/\/ \u6ce8\u610f&#xff01;&#xff01;&#xff01;\u5728\u6ca1\u6709\u8bbe\u7f6ethreadcount\u524d&#xff0c;\u4e0d\u80fd\u8c03\u7528Create<br \/>\n        _threadpool.Create();<br \/>\n        _baseloop.Start();<br \/>\n    }<\/p>\n<p>    void SetConnectedCallback(const ConnectedCallback&amp; cb) { _conn_cb &#061; cb; }<br \/>\n    void SetMessageCallback(const MessageCallback&amp; cb) { _msg_cb &#061; cb; }<br \/>\n    void SetClosedCallback(const CloseCallback&amp; cb) { _close_cb &#061; cb; }<br \/>\n    void SetAnyEventCallback(const AnyEventCallback&amp; cb) { _event_cb &#061; cb; }<br \/>\n}; <\/p>\n<h3 id=\"%E2%80%8B%E7%BC%96%E8%BE%91%C2%A0EchoServer\" style=\"background-color:transparent\">EchoServer<\/h3>\n<p>\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86\u6240\u6709\u7684\u6a21\u5757\u5b9e\u73b0&#xff0c;\u73b0\u5728\u901a\u8fc7\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56de\u54cd\u670d\u52a1\u5668\u6765\u89c2\u5bdf\u6574\u4f53\u6548\u679c<\/p>\n<p>EchoServer.hpp\u00a0<\/p>\n<p>#include &#034;..\/Server.hpp&#034;<\/p>\n<p>class EchoServer<br \/>\n{<br \/>\nprivate:<br \/>\n    TcpServer _tcpsvr;<br \/>\nprivate:<br \/>\n    void OnConnected(const std::shared_ptr&lt;Connection&gt;&amp; conn)<br \/>\n    {<br \/>\n        DBG_LOG(&#034;new connection: %p&#034;, conn.get());<br \/>\n    }<\/p>\n<p>    void OnMessage(const std::shared_ptr&lt;Connection&gt;&amp; conn, Buffer *buf)<br \/>\n    {<br \/>\n        if (!conn || !buf) return;<br \/>\n        if (buf-&gt;ReadAbleSize() &#061;&#061; 0) return;<\/p>\n<p>        DBG_LOG(&#034;%s&#034;, buf-&gt;ReadPosition());<br \/>\n        conn-&gt;Send(buf-&gt;ReadPosition(), buf-&gt;ReadAbleSize());<br \/>\n        buf-&gt;MoveReadPos(buf-&gt;ReadAbleSize());<\/p>\n<p>        conn-&gt;Shutdown();<br \/>\n    }<\/p>\n<p>    void OnClosed(const std::shared_ptr&lt;Connection&gt;&amp; conn)<br \/>\n    {<br \/>\n        DBG_LOG(&#034;Connection Destory&#034;);<br \/>\n    }<br \/>\npublic:<br \/>\n    EchoServer(int port)<br \/>\n        : _tcpsvr(port)<br \/>\n    {<br \/>\n        _tcpsvr.SetThreadCount(3);<br \/>\n        _tcpsvr.EnableInactiveRelease(10);<\/p>\n<p>        _tcpsvr.SetConnectedCallback(std::bind(&amp;EchoServer::OnConnected, this, std::placeholders::_1));<br \/>\n        _tcpsvr.SetMessageCallback(std::bind(&amp;EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));<br \/>\n        _tcpsvr.SetClosedCallback(std::bind(&amp;EchoServer::OnClosed, this, std::placeholders::_1));<br \/>\n    }<br \/>\n    void Start()<br \/>\n    {<br \/>\n        _tcpsvr.Start();<br \/>\n    }<br \/>\n};<\/p>\n<p>main.cc<\/p>\n<p>#include &#034;echo.hpp&#034;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    EchoServer echo(8080);<br \/>\n    echo.Start();<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>client.cc<\/p>\n<p>#include &#034;..\/Server.hpp&#034;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    Socket client;<br \/>\n    client.CreateClient(8080, &#034;127.0.0.1&#034;);<\/p>\n<p>    int cnt &#061; 5;<br \/>\n    while (cnt&#8211;)<br \/>\n    {<br \/>\n        \/\/ \u53d1<br \/>\n        std::string str &#061; &#034;hello Linux&#034;;<br \/>\n        client.Send(str.c_str(), str.size());<\/p>\n<p>        \/\/ \u6536<br \/>\n        char buffer[1024] &#061; {0};<br \/>\n        client.Recv(buffer, sizeof(buffer) &#8211; 1);<br \/>\n        DBG_LOG(&#034;%s&#034;, buffer);<\/p>\n<p>        sleep(1);<br \/>\n    }<br \/>\n    while(true) sleep(1);<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u00a0<\/p>\n<h4 id=\"%C2%A0%E6%B5%8B%E8%AF%95\">\u00a0\u6d4b\u8bd5<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"181\" src=\"2025-04-18ox5birynxr3.png\" width=\"1168\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u5ba2\u6237\u7aef\u53d1\u9001\u4e865\u6b21\u6570\u636e\u540e\u505c\u6b62\u53d1\u9001&#xff0c;\u670d\u52a1\u7aef\u6536\u5230\u4e86\u4e94\u6b21\u6570\u636e\u7136\u540e\u56de\u663e\u4e94\u6b21&#xff0c;\u5ba2\u6237\u7aef\u4e5f\u6536\u5230\u5e76\u6253\u5370\u4e86\u51fa\u6765&#xff0c;\u5e76\u4e14\u5728\u8d85\u65f6\u65f6\u95f410s\u8fc7\u540e\u670d\u52a1\u7aef\u4e3b\u52a8\u5173\u95ed\u4e86\u8fde\u63a5\u3002<\/p>\n<h4 id=\"%E6%80%A7%E8%83%BD%E6%B5%8B%E8%AF%95\" style=\"background-color:transparent\">\u6027\u80fd\u6d4b\u8bd5<\/h4>\n<p>WebBench\u5de5\u5177<\/p>\n<p>WebBench\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u7f51\u7ad9\u538b\u529b\u6d4b\u8bd5\u5de5\u5177&#xff0c;\u7531\u7ea2\u5e3d\u516c\u53f8\u5f00\u53d1\u5e76\u7ef4\u62a4\u3002\u5b83\u4e3b\u8981\u7528\u4e8e\u8bc4\u4f30HTTP\u670d\u52a1\u5668\u5728\u9ad8\u8d1f\u8f7d\u60c5\u51b5\u4e0b\u7684\u6027\u80fd\u8868\u73b0\u3002\u901a\u8fc7\u6a21\u62df\u5927\u91cf\u7684\u5e76\u53d1\u7528\u6237\u8bbf\u95ee\u8bf7\u6c42&#xff0c;\u53ef\u4ee5\u5bf9\u76ee\u6807\u7ad9\u70b9\u7684\u538b\u529b\u627f\u53d7\u80fd\u529b\u3001\u54cd\u5e94\u65f6\u95f4\u548c\u541e\u5410\u91cf\u7b49\u6307\u6807\u8fdb\u884c\u5168\u9762\u5206\u6790\u3002<\/p>\n<p>WebBench\u901a\u8fc7\u5efa\u7acb\u5927\u91cf\u7684\u5ba2\u6237\u7aef\u5230\u670d\u52a1\u5668\u7aef\u4e4b\u95f4\u7684\u5e76\u884cTCP\u8fde\u63a5\u6765\u53d1\u8d77\u8bf7\u6c42\u3002\u8fd9\u6a21\u4eff\u4e86\u771f\u5b9e\u73af\u5883\u4e2d\u4f17\u591a\u7528\u6237\u7684\u540c\u65f6\u5728\u7ebf\u573a\u666f\u3002<\/p>\n<p>\u4e00\u65e6\u5efa\u7acb\u4e86\u8db3\u591f\u7684\u8fde\u63a5\u6570\u4e4b\u540e&#xff0c;WebBench\u5c06\u5f00\u59cb\u4e0d\u65ad\u5730\u5411\u6307\u5b9a\u7684\u76ee\u6807URL\u5730\u5740\u53d1\u51faGET\u6216\u5176\u4ed6\u7c7b\u578b\u7684HTTP\u67e5\u8be2\u6307\u4ee4&#xff08;\u53d6\u51b3\u4e8e\u914d\u7f6e\u8bbe\u5b9a&#xff09;&#xff0c;\u76f4\u5230\u8fbe\u5230\u9884\u8bbe\u7684\u6700\u5927\u6b21\u6570\u6216\u6301\u7eed\u65f6\u95f4\u4e3a\u6b62\u3002<\/p>\n<p>\u6d4b\u8bd5<\/p>\n<p>\u7531\u4e8e\u4e91\u670d\u52a1\u5668\u7684\u5e26\u5bbd\u5f88\u4f4e&#xff0c;\u6240\u4ee5\u5982\u679c\u662f\u4e0d\u540c\u4e91\u670d\u52a1\u5668\u4e4b\u95f4\u8fdb\u884c\u9ad8\u5e76\u53d1\u8bf7\u6c42&#xff0c;\u90a3\u4e48\u6548\u7387\u5f88\u4f4e&#xff0c;\u6240\u4ee5\u6211\u4eec\u4f7f\u7528\u4e86\u672c\u5730\u73af\u56de&#xff0c;\u672c\u5730\u8bf7\u6c42&#xff0c;\u8fd9\u6837\u5c31\u5ffd\u7565\u4e86\u5e26\u5bbd\u5f71\u54cd&#xff0c;\u4f46\u662f\u53c8\u6709\u4e00\u4e2a\u95ee\u9898&#xff0c;\u672c\u5730\u670d\u52a1\u5668\u65e2\u54cd\u5e94&#xff0c;\u53c8\u591a\u8fdb\u7a0b\u8bf7\u6c42&#xff0c;\u4ed6\u4eec\u4f1a\u4e89\u593aCPU&#xff0c;\u4f1a\u964d\u4f4e\u670d\u52a1\u5668\u6548\u7387&#xff0c;\u4e5f\u4e0d\u5408\u7406&#xff0c;\u6240\u4ee5\u6211\u4eec\u5206\u522b\u5728\u4e91\u670d\u52a1\u5668\u4e0a\u6d4b\u8bd5\u4e00\u6b21&#xff0c;\u5728\u865a\u62df\u673a\u4e0a\u6d4b\u8bd5\u4e00\u6b21<\/p>\n<p>\u4e91\u670d\u52a1\u5668\u914d\u7f6e\u4e3a2\u68381G&#xff0c;\u6211\u4eec\u5728\u4e91\u670d\u52a1\u5668\u4e0a\u540c\u65f6\u8fd0\u884c\u670d\u52a1\u7aef\u4e0ewebbench\u8fdb\u884c4000\u5e76\u53d1\u91cf\u6d4b\u8bd5<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"275\" src=\"2025-04-18gbraohr3j5a.png\" width=\"818\" \/><\/p>\n<p>\u865a\u62df\u673a\u914d\u7f6e\u4e3a4\u68384G&#xff0c;\u6211\u4eec\u5728\u865a\u62df\u673a\u4e0a\u540c\u65f6\u8fd0\u884c\u670d\u52a1\u7aef\u4e0ewebbench\u8fdb\u884c5000\u5e76\u53d1\u91cf\u6d4b\u8bd5<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"314\" src=\"2025-04-18nxjk1vybxrs.png\" width=\"633\" \/><\/p>\n<\/p>\n<\/p>\n<\/p>\n<h3 id=\"HTTPServer\">HTTPServer<\/h3>\n<p><span style=\"color:#7b7f82\">\u5bf9\u4e8e\u8fd9\u4e2a\u6a21\u5757&#xff0c;\u6211\u5c06\u4f1a\u5728\u53e6\u4e00\u7bc7\u6587\u7ae0\u4e2d\u8bb2\u89e3\u5e76\u6d4b\u8bd5<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf29\u6b21\u3002\u4e3b\u4eceReactor\u9ad8\u5e76\u53d1\u9879\u76ee_\u4e3b\u4ecereactor<\/p>\n","protected":false},"author":2,"featured_media":15295,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,43,191,185],"topic":[],"class_list":["post-15315","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-43","tag-191","tag-185"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\uff1c\u9879\u76ee\uff1e \u4e3b\u4eceReactor\u6a21\u578b\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \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\/15315.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\uff1c\u9879\u76ee\uff1e \u4e3b\u4eceReactor\u6a21\u578b\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf29\u6b21\u3002\u4e3b\u4eceReactor\u9ad8\u5e76\u53d1\u9879\u76ee_\u4e3b\u4ecereactor\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/15315.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T13:52:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135254-68025936d6853.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=\"34 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/15315.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/15315.html\",\"name\":\"\uff1c\u9879\u76ee\uff1e \u4e3b\u4eceReactor\u6a21\u578b\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T13:52:58+00:00\",\"dateModified\":\"2025-04-18T13:52:58+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/15315.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/15315.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/15315.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\uff1c\u9879\u76ee\uff1e \u4e3b\u4eceReactor\u6a21\u578b\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\"}]},{\"@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":"\uff1c\u9879\u76ee\uff1e \u4e3b\u4eceReactor\u6a21\u578b\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \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\/15315.html","og_locale":"zh_CN","og_type":"article","og_title":"\uff1c\u9879\u76ee\uff1e \u4e3b\u4eceReactor\u6a21\u578b\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf29\u6b21\u3002\u4e3b\u4eceReactor\u9ad8\u5e76\u53d1\u9879\u76ee_\u4e3b\u4ecereactor","og_url":"https:\/\/www.wsisp.com\/helps\/15315.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T13:52:58+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418135254-68025936d6853.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"34 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/15315.html","url":"https:\/\/www.wsisp.com\/helps\/15315.html","name":"\uff1c\u9879\u76ee\uff1e \u4e3b\u4eceReactor\u6a21\u578b\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T13:52:58+00:00","dateModified":"2025-04-18T13:52:58+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/15315.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/15315.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/15315.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\uff1c\u9879\u76ee\uff1e \u4e3b\u4eceReactor\u6a21\u578b\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668"}]},{"@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\/15315","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=15315"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/15315\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/15295"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=15315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=15315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=15315"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=15315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}