{"id":44567,"date":"2025-06-19T15:25:44","date_gmt":"2025-06-19T07:25:44","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/44567.html"},"modified":"2025-06-19T15:25:44","modified_gmt":"2025-06-19T07:25:44","slug":"%e4%bb%bfmuduo%e5%ba%93%e5%ae%9e%e7%8e%b0%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\/44567.html","title":{"rendered":"\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668"},"content":{"rendered":"<p><span style=\"color:#511b78\">1.\u5b9e\u73b0\u76ee\u6807<\/span><\/p>\n<p>\u4effmuduo\u5e93One Thread One Loop\u5f0f\u4e3b\u4eceReactor\u6a21\u578b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668&#xff1a;<\/p>\n<p>\u901a\u8fc7\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7684\u7ec4\u4ef6&#xff0c;\u53ef\u4ee5\u5feb\u901f\u5b9e\u73b0\u4e00\u4e2a\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7684\u642d\u5efa&#xff0c;\u5e76\u4e14&#xff0c;\u901a\u8fc7\u7ec4\u5185\u4e0d\u540c\u5e94\u7528\u5c42\u534f\u8bae\u7684\u652f\u6301&#xff0c;\u53ef\u4ee5\u5feb\u901f\u5b8c\u6210\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u7684\u642d\u5efa&#xff0c;\u7531\u4e8e\u8981\u5b9e\u73b0\u7684\u662f\u4e00\u4e2a\u670d\u52a1\u5668&#xff0c;\u6240\u4ee5\u5e76\u4e0d\u6d89\u53ca\u5b9e\u9645\u7684\u4e1a\u52a1\u4ee3\u7801\u3002<\/p>\n<p>Http\u670d\u52a1\u5668&#xff1a;<\/p>\n<p>Http\u534f\u8bae\u662f\u4f4d\u4e8e\u5e94\u7528\u5c42\u7684\u4e00\u4e2a\u534f\u8bae&#xff0c;\u5168\u79f0\u4e3a\u4e00\u4e2a\u8d85\u6587\u672c\u4f20\u8f93\u534f\u8bae&#xff0c;\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u57fa\u4e8e\u8bf7\u6c42\u54cd\u5e94\u7684\u534f\u8bae&#xff0c;\u8fd0\u884c\u5728Tcp\u4e4b\u4e0a\u7684\u534f\u8bae&#xff0c;\u5b83\u662f\u4e0d\u5b89\u5168\u7684\u534f\u8bae\u3002<\/p>\n<p>Reactor\u6a21\u5f0f&#xff1a;<\/p>\n<p>Reactor \u6a21\u5f0f&#xff0c;\u662f\u6307\u901a\u8fc7\u4e00\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u540c\u65f6\u4f20\u9012\u7ed9\u670d\u52a1\u5668\u8fdb\u884c\u8bf7\u6c42\u5904\u7406\u65f6\u7684\u4e8b\u4ef6\u9a71\u52a8\u5904\u7406\u6a21\u5f0f\u3002 \u670d\u52a1\u7aef\u7a0b\u5e8f\u5904\u7406\u4f20\u5165\u591a\u8def\u8bf7\u6c42&#xff0c;\u5e76\u5c06\u5b83\u4eec\u540c\u6b65\u5206\u6d3e\u7ed9\u8bf7\u6c42\u5bf9\u5e94\u7684\u5904\u7406\u7ebf\u7a0b&#xff0c;Reactor \u6a21\u5f0f\u4e5f\u53ebDispatcher \u6a21\u5f0f&#xff0c;\u4e5f\u53ef\u4ee5\u53eb\u505a\u53d1\u5e03\u8005\u6a21\u5f0f\u3002<\/p>\n<p>\u5206\u7c7b&#xff1a;<\/p>\n<p>\u5355Reacto\u6a21\u5f0f&#xff1a;<\/p>\n<p>\u5355I\/O\u591a\u8def\u590d\u7528&#043;\u4e1a\u52a1\u5904\u7406&#xff0c;\u4f18\u70b9\u4e3a\u6240\u6709\u7684\u64cd\u4f5c\u90fd\u5728\u4e00\u4e2a\u7ebf\u7a0b\u5185\u6267\u884c&#xff0c;\u4e0d\u5b58\u5728\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff0c;\u6ca1\u6709\u6b7b\u9501\u95ee\u9898&#xff0c;\u4ee3\u7801\u5bb9\u6613\u7f16\u5199&#xff0c;\u7f3a\u70b9\u4e3a\u65e0\u6cd5\u5229\u7528cup\u591a\u6838\u6709\u6548\u8d44\u6e90&#xff0c;\u8d44\u6e90\u6d6a\u8d39\u4e25\u91cd&#xff0c;\u5bb9\u6613\u8fbe\u5230\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<p>\u5355Reactor\u591a\u7ebf\u7a0b&#xff1a;<\/p>\n<p>\u5355I\/O\u591a\u8def\u8f6c\u63a5&#043;\u7ebf\u7a0b\u6c60&#043;\u4e1a\u52a1\u5904\u7406&#xff0c;\u4f18\u70b9\u4e3a\u53ef\u4ee5\u5145\u5206\u53d1\u6325cpu\u591a\u6838\u8d44\u6e90&#xff0c;\u7f3a\u70b9\u4e3a\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u6bd4\u8f83\u9ebb\u70e6&#xff0c;\u5355\u4e2aReactor\u627f\u62c5\u4e86\u6240\u6709\u7684\u4e8b\u4ef6\u7684\u76d1\u542c\u548c\u54cd\u5e94&#xff0c;\u5728\u5355\u7ebf\u7a0b\u4e4b\u4e0b&#xff0c;\u9ad8\u5e76\u53d1\u6210\u4e3a\u4e86\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<p>\u591aReactor\u591a\u7ebf\u7a0b\u6a21\u5f0f&#xff1a;<\/p>\n<p>\u591a\u4e2aI\/O\u591a\u8def\u8f6c\u63a5&#043;\u7ebf\u7a0b\u6c60&#043;\u4e1a\u52a1\u5904\u7406&#xff0c;\u5145\u5206\u5229\u7528CPU\u591a\u6838\u8d44\u6e90&#xff0c;\u4e3b\u4eceReactor\u5404\u53f8\u5176\u804c&#xff0c;\u5728\u4e3bReactor\u4e2d\u5904\u7406\u65b0\u8fde\u63a5\u8bf7\u6c42\u4e8b\u4ef6&#xff0c;\u6709\u65b0\u8fde\u63a5\u5230\u6765\u5219\u5206\u53d1\u5230\u5b50Reactor\u4e2d\u76d1\u63a7&#xff0c;\u5728\u5b50Reactor\u4e2d\u8fdb\u884c\u5ba2\u6237\u7aef\u901a\u4fe1\u76d1\u63a7&#xff0c;\u6709\u4e8b\u4ef6\u89e6\u53d1&#xff0c;\u5219\u63a5\u6536\u6570\u636e\u5206\u53d1\u7ed9Worker\u7ebf\u7a0b\u6c60\u3002<\/p>\n<p>\u53ef\u4ee5\u5145\u5206\u5229\u7528CPU\u8d44\u6e90&#xff0c;\u5c06\u76f8\u5e94\u4ea4\u7ed9\u5b50Reactor\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u76ee\u6807\u5b9a\u4f4d<\/p>\n<p>One Thread One Loop\u4e3b\u4eceReactor\u6a21\u578b\u9ad8\u5e76\u53d1\u670d\u52a1\u5668<\/p>\n<p>\u54b1\u4eec\u5b9e\u73b0\u7684\u662f\u4e3b\u4eceReactor\u6a21\u578b\u670d\u52a1\u5668&#xff0c;\u4e5f\u5c31\u662f\u4e3bReactor\u7ebf\u7a0b\u4ec5\u4ec5\u76d1\u63a7\u76d1\u542c\u63cf\u8ff0\u7b26&#xff0c;\u83b7\u53d6\u65b0\u5efa\u8fde\u63a5&#xff0c;\u4fdd\u8bc1\u83b7\u53d6\u65b0\u8fde\u63a5\u7684\u9ad8\u6548\u6027&#xff0c;\u63d0\u9ad8\u670d\u52a1\u5668\u7684\u5e76\u53d1\u6027\u80fd&#xff0c;\u4e3bReactor\u83b7\u53d6\u5230\u65b0\u94fe\u63a5\u4e4b\u540e&#xff0c;\u53d1\u5e03\u5230\u5b50Reactor\u8fdb\u884c\u901a\u4fe1\u4e8b\u4ef6\u76d1\u63a7&#xff0c;\u5b50Reactor\u8fdb\u884c\u5bf9\u884c\u7684\u76d1\u63a7\u5404\u81ea\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u589e\u5220\u67e5\u6539\u4ee5\u53ca\u4e0a\u5c42\u7684\u4e1a\u52a1\u5904\u7406\u3002<\/p>\n<p>One Thread One Loop\u7684\u601d\u60f3\u5c31\u662f\u628a\u6240\u6709\u7684\u64cd\u4f5c\u90fd\u653e\u5230\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u8fdb\u884c&#xff0c;\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u5e94\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u7684\u5faa\u73af\u3002 \u5f53\u524d\u5b9e\u73b0\u4e2d&#xff0c;\u56e0\u4e3a\u5e76\u4e0d\u786e\u5b9a\u7ec4\u4ef6\u4f7f\u7528\u8005\u7684\u4f7f\u7528\u610f\u5411&#xff0c;\u56e0\u6b64\u5e76\u4e0d\u63d0\u4f9b\u4e1a\u52a1\u5c42\u5de5\u4f5c\u7ebf\u7a0b\u6c60\u7684\u5b9e\u73b0&#xff0c;\u53ea\u5b9e\u73b0\u4e3b\u4eceReactor&#xff0c;\u800cWorker\u5de5\u4f5c\u7ebf\u7a0b\u6c60&#xff0c;\u53ef\u7531\u7ec4\u4ef6\u5e93\u7684\u4f7f\u7528\u8005\u7684\u9700\u8981\u81ea\u884c\u51b3\u5b9a\u662f\u5426\u4f7f\u7528\u548c\u5b9e\u73b0\u3002<\/p>\n<p>\u529f\u80fd\u6a21\u5757\u5212\u5206&#xff1a;<\/p>\n<p>\u57fa\u4e8e\u4ee5\u4e0a\u7684\u7406\u89e3&#xff0c;\u6211\u4eec\u8981\u5b9e\u73b0\u7684\u662f\u4e00\u4e2a\u5e26\u6709\u534f\u8bae\u652f\u6301\u7684Reactor\u6a21\u578b\u9ad8\u6027\u80fd\u670d\u52a1\u5668&#xff0c;\u56e0\u6b64\u5c06\u6574\u4e2a\u9879\u76ee\u7684\u5b9e\u73b0\u5212\u5206\u4e3a\u4e24\u4e2a\u5927\u7684\u6a21\u5757&#xff1a; \u2022 SERVER\u6a21\u5757&#xff1a;\u5b9e\u73b0Reactor\u6a21\u578b\u7684TCP\u670d\u52a1\u5668&#xff1b; \u2022 \u534f\u8bae\u6a21\u5757&#xff1a;\u5bf9\u5f53\u524d\u7684Reactor\u6a21\u578b\u670d\u52a1\u5668\u63d0\u4f9b\u5e94\u7528\u5c42\u534f\u8bae\u652f\u6301\u3002<\/p>\n<p>SERVER\u6a21\u5757&#xff1a;<\/p>\n<p>SERVER\u6a21\u5757&#xff1a;SERVER\u6a21\u5757\u5c31\u662f\u5c06\u6240\u6709\u7684\u8fde\u63a5\u4ee5\u53ca\u7ebf\u7a0b\u8fdb\u884c\u7ba1\u7406&#xff0c;\u5728\u5408\u9002\u7684\u65f6\u5019\u505a\u5408\u9002\u7684\u4e8b\u60c5&#xff0c;\u5b8c\u6210\u670d\u52a1\u5668\u7684\u5b9e\u73b0\u3002\u5177\u4f53\u7684\u7ba1\u7406\u5206\u4e3a\u4e09\u4e2a\u6a21\u5757&#xff1a;<\/p>\n<p>\u76d1\u542c\u8fde\u63a5\u7ba1\u7406&#xff1a;\u5bf9\u76d1\u542c\u94fe\u63a5\u8fdb\u884c\u901a\u4fe1\u7ba1\u7406\u3002<\/p>\n<p>\u901a\u4fe1\u8fde\u63a5\u7ba1\u7406&#xff1a;\u5bf9\u6b63\u5728\u901a\u4fe1\u7684\u94fe\u63a5\u8fdb\u884c\u7ba1\u7406\u3002<\/p>\n<p>\u8d85\u65f6\u8fde\u63a5\u7ba1\u7406&#xff1a;\u5bf9\u5df2\u7ecf\u8d85\u65f6\u7684\u94fe\u63a5\u8fdb\u884c\u7ba1\u7406\u3002<\/p>\n<p>\u57fa\u4e8e\u4ee5\u4e0a\u601d\u60f3&#xff0c;\u53c8\u53ef\u4ee5\u5212\u5206\u4e3a\u591a\u4e2a\u6a21\u5757&#xff1a;<\/p>\n<\/p>\n<p>Buffer \u6a21\u5757&#xff1a;<\/p>\n<p>Boffer\u6a21\u5757&#xff0c;\u662f\u4e00\u4e2a\u7f13\u51b2\u533a\u6a21\u5757&#xff0c;\u7528\u4e8e\u901a\u4fe1\u8fc7\u7a0b\u4e2d\u63a5\u6536\u548c\u53d1\u9001\u4fe1\u606f\u7684\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p>Socket \u6a21\u5757&#xff1a;<\/p>\n<p>Socket\u6a21\u5757&#xff0c;\u4e3b\u8981\u5bf9\u521b\u5efaSocket\u505a\u4e00\u4e2a\u5c01\u88c5&#xff0c;\u5b9e\u73b0\u7f51\u7edc\u901a\u4fe1\u7684\u5404\u9879\u529f\u80fd\u3002<\/p>\n<\/p>\n<p>Channel\u6a21\u5757&#xff1a;<\/p>\n<p>Channel\u6a21\u5757&#xff0c;\u4e3b\u8981\u5bf9\u6587\u4ef6\u63cf\u8ff0\u7b26\u9700\u8981\u8fdb\u884c\u7684I\/O\u4e8b\u4ef6\u7ba1\u7406\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\u884cIO\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<\/p>\n<\/p>\n<p>Connection \u6a21\u5757&#xff1a;<\/p>\n<p>Connection\u6a21\u5757\u662f\u5bf9Buffer\u6a21\u5757&#xff0c;Socket\u6a21\u5757&#xff0c;Channel\u6a21\u5757\u7684\u4e00\u4e2a\u6574\u4f53\u5c01\u88c5&#xff0c;\u5b9e\u73b0\u4e86\u5bf9\u4e00\u4e2a\u901a\u4fe1\u5957\u63a5\u5b57\u7684\u6574\u4f53\u7684\u7ba1\u7406&#xff0c;\u6bcf\u4e00\u4e2a\u8fdb\u884c\u6570\u636e\u901a\u4fe1\u7684\u5957\u63a5\u5b57&#xff08;\u4e5f\u5c31\u662faccept\u83b7\u53d6\u5230\u7684\u65b0\u8fde\u63a5&#xff09;\u90fd\u4f1a\u4f7f\u7528Connection\u8fdb\u884c\u7ba1\u7406\u3002<\/p>\n<p>Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e09\u4e2a\u7531\u7ec4\u4ef6\u4f7f\u7528\u8005\u4f20\u5165\u7684\u56de\u8c03\u51fd\u6570&#xff1a;\u8fde\u63a5\u5efa\u7acb\u5b8c\u6210\u56de\u8c03&#xff0c;\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u65b0\u6570\u636e\u56de\u8c03&#xff0c;\u5173\u95ed\u56de\u8c03\u3002<\/p>\n<ul>\n<li>Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e24\u4e2a\u7ec4\u4ef6\u4f7f\u7528\u8005\u63d0\u4f9b\u7684\u63a5\u53e3&#xff1a;\u6570\u636e\u53d1\u9001\u63a5\u53e3&#xff0c;\u8fde\u63a5\u5173\u95ed\u63a5\u53e3<\/li>\n<li>Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e24\u4e2a\u7528\u6237\u6001\u7f13\u51b2\u533a&#xff1a;\u7528\u6237\u6001\u63a5\u6536\u7f13\u51b2\u533a&#xff0c;\u7528\u6237\u6001\u53d1\u9001\u7f13\u51b2\u533a<\/li>\n<li>Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aSocket\u5bf9\u8c61&#xff1a;\u5b8c\u6210\u63cf\u8ff0\u7b26\u9762\u5411\u7cfb\u7edf\u7684IO\u64cd\u4f5c<\/li>\n<li>Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aChannel\u5bf9\u8c61&#xff1a;\u5b8c\u6210\u63cf\u8ff0\u7b26IO\u4e8b\u4ef6\u5c31\u7eea\u7684\u5904\u7406<\/li>\n<\/ul>\n<p>\u5177\u4f53\u5904\u7406\u6d41\u7a0b\u5982\u4e0b&#xff1a; 1. \u5b9e\u73b0\u5411Channel\u63d0\u4f9b\u53ef\u8bfb&#xff0c;\u53ef\u5199&#xff0c;\u9519\u8bef\u7b49\u4e0d\u540c\u4e8b\u4ef6\u7684IO\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570&#xff0c;\u7136\u540e\u5c06Channel\u548c\u5bf9\u5e94\u7684\u63cf\u8ff0\u7b26\u6dfb\u52a0\u5230Poller\u4e8b\u4ef6\u76d1\u63a7\u4e2d\u3002 2. \u5f53\u63cf\u8ff0\u7b26\u5728Poller\u6a21\u5757\u4e2d\u5c31\u7eea\u4e86IO\u53ef\u8bfb\u4e8b\u4ef6&#xff0c;\u5219\u8c03\u7528\u63cf\u8ff0\u7b26\u5bf9\u5e94Channel\u4e2d\u4fdd\u5b58\u7684\u8bfb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570&#xff0c;\u8fdb\u884c\u6570\u636e\u8bfb\u53d6&#xff0c;\u5c06socket\u63a5\u6536\u7f13\u51b2\u533a\u5168\u90e8\u8bfb\u53d6\u5230Connection\u7ba1\u7406\u7684\u7528\u6237\u6001\u63a5\u6536\u7f13\u51b2\u533a\u4e2d\u3002\u7136\u540e\u8c03\u7528\u7531\u7ec4\u4ef6\u4f7f\u7528\u8005\u4f20\u5165\u7684\u65b0\u6570\u636e\u5230\u6765\u56de\u8c03\u51fd\u6570\u8fdb\u884c\u5904\u7406\u3002 3. \u7ec4\u4ef6\u4f7f\u7528\u8005\u8fdb\u884c\u6570\u636e\u7684\u4e1a\u52a1\u5904\u7406\u5b8c\u6bd5\u540e&#xff0c;\u901a\u8fc7Connection\u5411\u4f7f\u7528\u8005\u63d0\u4f9b\u7684\u6570\u636e\u53d1\u9001\u63a5\u53e3&#xff0c;\u5c06\u6570\u636e\u5199\u5165Connection\u7684\u53d1\u9001\u7f13\u51b2\u533a\u4e2d\u3002 4. \u542f\u52a8\u63cf\u8ff0\u7b26\u5728Poll\u6a21\u5757\u4e2d\u7684IO\u5199\u4e8b\u4ef6\u76d1\u63a7&#xff0c;\u5c31\u7eea\u540e&#xff0c;\u8c03\u7528Channel\u4e2d\u4fdd\u5b58\u7684\u5199\u4e8b\u4ef6\u5904\u7406\u51fd\u6570&#xff0c;\u5c06\u53d1\u9001\u7f13\u51b2\u533a\u4e2d\u7684\u6570\u636e\u901a\u8fc7Socket\u8fdb\u884c\u9762\u5411\u7cfb\u7edf\u7684\u5b9e\u9645\u6570\u636e\u53d1\u9001\u3002<\/p>\n<\/p>\n<p>Accept \u6a21\u5757&#xff1a;<\/p>\n<p>Acceptor\u6a21\u5757\u662f\u5bf9Socket\u6a21\u5757&#xff0c;Channel\u6a21\u5757\u7684\u4e00\u4e2a\u6574\u4f53\u5c01\u88c5&#xff0c;\u5b9e\u73b0\u4e86\u5bf9\u4e00\u4e2a\u76d1\u542c\u5957\u63a5\u5b57\u7684\u6574\u4f53\u7684\u7ba1\u7406\u3002<\/p>\n<ul>\n<li>Acceptor\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aSocket\u5bf9\u8c61&#xff1a;\u5b9e\u73b0\u76d1\u542c\u5957\u63a5\u5b57\u7684\u64cd\u4f5c\u3002<\/li>\n<li>\u00a0Acceptor\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aChannel\u5bf9\u8c61&#xff1a;\u5b9e\u73b0\u76d1\u542c\u5957\u63a5\u5b57IO\u4e8b\u4ef6\u5c31\u7eea\u7684\u5904\u7406\u3002<\/li>\n<\/ul>\n<p>\u5177\u4f53\u6d41\u7a0b&#xff1a;<\/p>\n<p>1.\u5b9e\u73b0\u5411Channel\u63d0\u4f9b\u53ef\u8bfb\u4e8b\u4ef6\u7684IO\u4e8b\u4ef6\u5904\u7406\u56de\u8c03\u51fd\u6570&#xff0c;\u51fd\u6570\u7684\u529f\u80fd\u5176\u5b9e\u4e5f\u5c31\u662f\u83b7\u53d6\u65b0\u8fde\u63a5 2. \u4e3a\u65b0\u8fde\u63a5\u6784\u5efa\u4e00\u4e2aConnection\u5bf9\u8c61\u51fa\u6765\u3002<\/p>\n<\/p>\n<p>TimerQueue\u6a21\u5757&#xff1a; TimerQueue\u6a21\u5757\u662f\u5b9e\u73b0\u56fa\u5b9a\u65f6\u95f4\u5b9a\u65f6\u4efb\u52a1\u7684\u6a21\u5757&#xff0c;\u53ef\u4ee5\u7406\u89e3\u5c31\u662f\u8981\u7ed9\u5b9a\u65f6\u4efb\u52a1\u7ba1\u7406\u5668&#xff0c;\u5411\u5b9a\u65f6\u4efb\u52a1\u7ba1\u7406\u5668\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u4efb\u52a1&#xff0c;\u4efb\u52a1\u5c06\u5728\u56fa\u5b9a\u65f6\u95f4\u540e\u88ab\u6267\u884c&#xff0c;\u540c\u65f6\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5237\u65b0\u5b9a\u65f6\u4efb\u52a1\u6765\u5ef6\u8fdf\u4efb\u52a1\u7684\u6267\u884c\u3002 \u8fd9\u4e2a\u6a21\u5757\u4e3b\u8981\u662f\u5bf9Connection\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406&#xff0c;\u5bf9\u975e\u6d3b\u8dc3\u8fde\u63a5\u8fdb\u884c\u8d85\u65f6\u540e\u7684\u91ca\u653e\u529f\u80fd\u3002<\/p>\n<p>TimerQueue\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2atimerfd&#xff1a;linux\u7cfb\u7edf\u63d0\u4f9b\u7684\u5b9a\u65f6\u5668\u3002 TimerQueue\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aChannel\u5bf9\u8c61&#xff1a;\u5b9e\u73b0\u5bf9timerfd\u7684IO\u65f6\u95f4\u5c31\u7eea\u56de\u8c03\u5904\u7406\u3002<\/p>\n<\/p>\n<p>Poller\u6a21\u5757&#xff1a;<\/p>\n<p>Poller\u6a21\u5757\u662f\u5bf9epoll\u8fdb\u884c\u5c01\u88c5\u7684\u4e00\u4e2a\u6a21\u5757&#xff0c;\u4e3b\u8981\u5b9e\u73b0epoll\u7684IO\u4e8b\u4ef6\u6dfb\u52a0&#xff0c;\u4fee\u6539&#xff0c;\u79fb\u9664&#xff0c;\u83b7\u53d6\u6d3b\u8dc3\u8fde\u63a5\u529f\u80fd\u3002<\/p>\n<\/p>\n<p>EverlLoop \u6a21\u5757&#xff1a;<\/p>\n<p>EventLoop\u6a21\u5757\u53ef\u4ee5\u7406\u89e3\u5c31\u662f\u6211\u4eec\u4e0a\u8fb9\u6240\u8bf4\u7684Reactor\u6a21\u5757&#xff0c;\u5b83\u662f\u5bf9Poller\u6a21\u5757&#xff0c;TimerQueue\u6a21\u5757&#xff0c;Socket\u6a21\u5757\u7684\u4e00\u4e2a\u6574\u4f53\u5c01\u88c5&#xff0c;\u8fdb\u884c\u6240\u6709\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7\u3002<\/p>\n<p>EventLoop\u6a21\u5757\u5fc5\u7136\u662f\u4e00\u4e2a\u5bf9\u8c61\u5bf9\u5e94\u4e00\u4e2a\u7ebf\u7a0b\u7684\u6a21\u5757&#xff0c;\u7ebf\u7a0b\u5185\u90e8\u7684\u76ee\u7684\u5c31\u662f\u8fd0\u884cEventLoop\u7684\u542f\u52a8\u51fd\u6570\u3002 EventLoop\u6a21\u5757\u4e3a\u4e86\u4fdd\u8bc1\u6574\u4e2a\u670d\u52a1\u5668\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff0c;\u56e0\u6b64\u8981\u6c42\u4f7f\u7528\u8005\u5bf9\u4e8eConnection\u7684\u6240\u6709\u64cd\u4f5c\u4e00\u5b9a\u8981\u5728\u5176\u5bf9\u5e94\u7684EventLoop\u7ebf\u7a0b\u5185\u5b8c\u6210&#xff0c;\u4e0d\u80fd\u5728\u5176\u4ed6\u7ebf\u7a0b\u4e2d\u8fdb\u884c&#xff08;\u6bd4\u5982\u7ec4\u4ef6\u4f7f\u7528\u8005\u4f7f\u7528Connection\u53d1\u9001\u6570\u636e&#xff0c;\u4ee5\u53ca\u5173\u95ed\u8fde\u63a5\u8fd9\u79cd\u64cd\u4f5c&#xff09;\u3002<\/p>\n<ul>\n<li>EventLoop\u6a21\u5757\u4fdd\u8bc1\u81ea\u5df1\u5185\u90e8\u6240\u76d1\u63a7\u7684\u6240\u6709\u63cf\u8ff0\u7b26&#xff0c;\u90fd\u8981\u662f\u6d3b\u8dc3\u8fde\u63a5&#xff0c;\u975e\u6d3b\u8dc3\u8fde\u63a5\u5c31\u8981\u53ca\u65f6\u91ca\u653e\u907f\u514d\u8d44\u6e90\u6d6a\u8d39\u3002<\/li>\n<li>EventLoop\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aeventfd&#xff1a;eventfd\u5176\u5b9e\u5c31\u662flinux\u5185\u6838\u63d0\u4f9b\u7684\u4e00\u4e2a\u4e8b\u4ef6fd&#xff0c;\u4e13\u95e8\u7528\u4e8e\u4e8b\u4ef6\u901a\u77e5\u3002<\/li>\n<li>EventLoop\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aPoller\u5bf9\u8c61&#xff1a;\u7528\u4e8e\u8fdb\u884c\u63cf\u8ff0\u7b26\u7684IO\u4e8b\u4ef6\u76d1\u63a7\u3002<\/li>\n<li>EventLoop\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aTimerQueue\u5bf9\u8c61&#xff1a;\u7528\u4e8e\u8fdb\u884c\u5b9a\u65f6\u4efb\u52a1\u7684\u7ba1\u7406\u3002<\/li>\n<li>EventLoop\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aPendingTask\u961f\u5217&#xff1a;\u7ec4\u4ef6\u4f7f\u7528\u8005\u5c06\u5bf9Connection\u8fdb\u884c\u7684\u6240\u6709\u64cd\u4f5c&#xff0c;\u90fd\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\u4e2d&#xff0c;\u7531EventLoop\u6a21\u5757\u8fdb\u884c\u7ba1\u7406&#xff0c;\u5e76\u5728EventLoop\u5bf9\u5e94\u7684\u7ebf\u7a0b\u4e2d\u8fdb\u884c\u6267\u884c\u3002<\/li>\n<li>\u6bcf\u4e00\u4e2aConnection\u5bf9\u8c61\u90fd\u4f1a\u7ed1\u5b9a\u5230\u4e00\u4e2aEventLoop\u4e0a&#xff0c;\u8fd9\u6837\u80fd\u4fdd\u8bc1\u5bf9\u8fd9\u4e2a\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u662f\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u5b8c\u6210\u7684\u3002<\/li>\n<\/ul>\n<p>\u5177\u4f53\u6d41\u7a0b&#xff1a;<\/p>\n<ul>\n<li>\u901a\u8fc7Poller\u6a21\u5757\u5bf9\u5f53\u524d\u6a21\u5757\u7ba1\u7406\u5185\u7684\u6240\u6709\u63cf\u8ff0\u7b26\u8fdb\u884cIO\u4e8b\u4ef6\u76d1\u63a7&#xff0c;\u6709\u63cf\u8ff0\u7b26\u4e8b\u4ef6\u5c31\u7eea\u540e&#xff0c;\u901a\u8fc7\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684Channel\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406\u3002<\/li>\n<li>\u6240\u6709\u5c31\u7eea\u7684\u63cf\u8ff0\u7b26IO\u4e8b\u4ef6\u5904\u7406\u5b8c\u6bd5\u540e&#xff0c;\u5bf9\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u6240\u6709\u64cd\u4f5c\u987a\u5e8f\u8fdb\u884c\u6267\u884c\u3002<\/li>\n<li>\u7531\u4e8eepoll\u7684\u4e8b\u4ef6\u76d1\u63a7&#xff0c;\u6709\u53ef\u80fd\u4f1a\u56e0\u4e3a\u6ca1\u6709\u4e8b\u4ef6\u5230\u6765\u800c\u6301\u7eed\u963b\u585e&#xff0c;\u5bfc\u81f4\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u4e0d\u80fd\u53ca\u65f6\u5f97\u5230\u6267\u884c&#xff0c;\u56e0\u6b64\u521b\u5efa\u4e86eventfd&#xff0c;\u6dfb\u52a0\u5230Poller\u7684\u4e8b\u4ef6\u76d1\u63a7\u4e2d&#xff0c;\u7528\u4e8e\u5b9e\u73b0\u6bcf\u6b21\u5411\u4efb\u52a1\u961f\u5217\u6dfb\u52a0\u4efb\u52a1\u7684\u65f6\u5019&#xff0c;\u901a\u8fc7\u5411eventfd\u5199\u5165\u6570\u636e\u6765\u5524\u9192epoll\u7684\u963b\u585e\u3002<\/li>\n<\/ul>\n<p>TcpServer\u6a21\u5757&#xff1a;<\/p>\n<ul>\n<li>\u8fd9\u4e2a\u6a21\u5757\u662f\u4e00\u4e2a\u6574\u4f53Tcp\u670d\u52a1\u5668\u6a21\u5757\u7684\u5c01\u88c5&#xff0c;\u5185\u90e8\u5c01\u88c5\u4e86Acceptor\u6a21\u5757EventLoopThrea-dpool\u6a21\u5757\u3002<\/li>\n<li>TcpServer\u4e2d\u5305\u542b\u6709\u4e00\u4e2aEventLoop\u5bf9\u8c61&#xff1a;\u4ee5\u5907\u5728\u8d85\u8f7b\u91cf\u4f7f\u7528\u573a\u666f\u4e2d\u4e0d\u9700\u8981EventLoop\u7ebf\u7a0b\u6c60&#xff0c;\u53ea\u9700\u8981\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u5b8c\u6210\u6240\u6709\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/li>\n<li>TcpServer\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aEventLoopThreadPool\u5bf9\u8c61&#xff1a;\u5176\u5b9e\u5c31\u662fEventLoop\u7ebf\u7a0b\u6c60&#xff0c;\u4e5f\u5c31\u662f\u5b50Reactor\u7ebf\u7a0b\u6c60\u3002<\/li>\n<li>TcpServer\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aAcceptor\u5bf9\u8c61&#xff1a;\u4e00\u4e2aTcpServer\u670d\u52a1\u5668&#xff0c;\u5fc5\u7136\u5bf9\u5e94\u6709\u4e00\u4e2a\u76d1\u542c\u5957\u63a5\u5b57&#xff0c;\u80fd\u591f\u5b8c\u6210\u83b7\u53d6\u5ba2\u6237\u7aef\u65b0\u8fde\u63a5&#xff0c;\u5e76\u5904\u7406\u7684\u4efb\u52a1\u3002<\/li>\n<li>TcpServer\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2astd::shared_ptr&lt;Connection&gt;\u7684hash\u8868&#xff1a;\u4fdd\u5b58\u4e86\u6240\u6709\u7684\u65b0\u5efa\u8fde\u63a5\u5bf9\u5e94\u7684Connection&#xff0c;\u6ce8\u610f&#xff0c;\u6240\u6709\u7684Connection\u4f7f\u7528shared_ptr\u8fdb\u884c\u7ba1\u7406&#xff0c;\u8fd9\u6837\u80fd\u591f\u4fdd\u8bc1\u5728hash\u8868\u4e2d\u5220\u9664\u4e86Connection\u4fe1\u606f\u540e&#xff0c;\u5728shared_ptr\u8ba1\u6570\u5668\u4e3a0\u7684\u60c5\u51b5\u4e0b\u5b8c\u6210\u5bf9Connection\u8d44\u6e90\u7684\u91ca\u653e\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u5177\u4f53\u6d41\u7a0b&#xff1a;<\/p>\n<p>1. \u5728\u5b9e\u4f8b\u5316TcpServer\u5bf9\u8c61\u8fc7\u7a0b\u4e2d&#xff0c;\u5b8c\u6210BaseLoop\u7684\u8bbe\u7f6e&#xff0c;Acceptor\u5bf9\u8c61\u7684\u5b9e\u4f8b\u5316&#xff0c;\u4ee5\u53caEventLoop\u7ebf\u7a0b\u6c60\u7684\u5b9e\u4f8b\u5316&#xff0c;\u4ee5\u53castd::shared_ptr&lt;Connection&gt;\u7684hash\u8868\u7684\u5b9e\u4f8b\u5316\u3002 2. \u4e3aAcceptor\u5bf9\u8c61\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570&#xff1a;\u83b7\u53d6\u5230\u65b0\u8fde\u63a5\u540e&#xff0c;\u4e3a\u65b0\u8fde\u63a5\u6784\u5efaConnection\u5bf9\u8c61&#xff0c;\u8bbe\u7f6e Connection\u7684\u5404\u9879\u56de\u8c03&#xff0c;\u5e76\u4f7f\u7528shared_ptr\u8fdb\u884c\u7ba1\u7406&#xff0c;\u5e76\u6dfb\u52a0\u5230hash\u8868\u4e2d\u8fdb\u884c\u7ba1\u7406&#xff0c;\u5e76\u4e3a Connection\u9009\u62e9\u4e00\u4e2aEventLoop\u7ebf\u7a0b&#xff0c;\u4e3aConnection\u6dfb\u52a0\u4e00\u4e2a\u5b9a\u65f6\u9500\u6bc1\u4efb\u52a1&#xff0c;\u4e3aConnection\u6dfb\u52a0\u4e8b\u4ef6\u76d1\u63a7&#xff0c; 3. \u542f\u52a8BaseLoop\u3002<\/p>\n<p>SERVER\u6a21\u5f0f\u56fe&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1200\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619072541-6853bb758958f.png\" width=\"1200\" \/><\/p>\n<\/p>\n<p>Http\u534f\u8bae\u6a21\u5f0f&#xff1a;<\/p>\n<p>Http\u534f\u8bae\u6a21\u5f0f&#xff1a;HTTP\u534f\u8bae\u6a21\u5757\u7528\u4e8e\u5bf9\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u6a21\u5757\u8fdb\u884c\u534f\u8bae\u652f\u6301&#xff0c;\u57fa\u4e8e\u63d0\u4f9b\u7684\u534f\u8bae\u652f\u6301\u80fd\u591f\u66f4\u65b9\u4fbf\u7684\u5b8c\u6210\u6307\u5b9a\u534f\u8bae\u670d\u52a1\u5668\u7684\u642d\u5efa\u3002\u800cHTTP\u534f\u8bae\u652f\u6301\u6a21\u5757\u7684\u5b9e\u73b0&#xff0c;\u53ef\u4ee5\u7ec6\u5206\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6a21\u5757\u3002<\/p>\n<\/p>\n<p>Util\u6a21\u5757&#xff1a;<\/p>\n<p>\u8fd9\u4e2a\u6a21\u5757\u662f\u4e00\u4e2a\u5de5\u5177\u6a21\u5757&#xff0c;\u4e3b\u8981\u63d0\u4f9bHTTP\u534f\u8bae\u6a21\u5757\u6240\u7528\u5230\u7684\u4e00\u4e9b\u5de5\u5177\u51fd\u6570&#xff0c;\u6bd4\u5982url\u7f16\u89e3\u7801&#xff0c;\u6587\u4ef6\u8bfb\u5199&#8230;.\u7b49\u3002<\/p>\n<\/p>\n<p>HttpRequest\u6a21\u5757&#xff1a; \u8fd9\u4e2a\u6a21\u5757\u662fHTTP\u8bf7\u6c42\u6570\u636e\u6a21\u5757&#xff0c;\u7528\u4e8e\u4fdd\u5b58HTTP\u8bf7\u6c42\u6570\u636e\u88ab\u89e3\u6790\u540e\u7684\u5404\u9879\u8bf7\u6c42\u5143\u7d20\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p>HttpResponse\u6a21\u5757&#xff1a;<\/p>\n<p>\u8fd9\u4e2a\u6a21\u5757\u662fHTTP\u54cd\u5e94\u6570\u636e\u6a21\u5757&#xff0c;\u7528\u4e8e\u4e1a\u52a1\u5904\u7406\u540e\u8bbe\u7f6e\u5e76\u4fdd\u5b58HTTP\u54cd\u5e94\u6570\u636e\u7684\u7684\u5404\u9879\u5143\u7d20\u4fe1\u606f&#xff0c;\u6700\u7ec8\u4f1a\u88ab\u6309\u7167HTTP\u534f\u8bae\u54cd\u5e94\u683c\u5f0f\u7ec4\u7ec7\u6210\u4e3a\u54cd\u5e94\u4fe1\u606f\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<\/p>\n<p>HttpContext\u6a21\u5757&#xff1a; \u8fd9\u4e2a\u6a21\u5757\u662f\u4e00\u4e2aHTTP\u8bf7\u6c42\u63a5\u6536\u7684\u4e0a\u4e0b\u6587\u6a21\u5757&#xff0c;\u4e3b\u8981\u662f\u4e3a\u4e86\u9632\u6b62\u5728\u4e00\u6b21\u63a5\u6536\u7684\u6570\u636e\u4e2d&#xff0c;\u4e0d\u662f\u4e00\u4e2a\u5b8c\u6574\u7684HTTP\u8bf7\u6c42&#xff0c;\u5219\u89e3\u6790\u8fc7\u7a0b\u5e76\u672a\u5b8c\u6210&#xff0c;\u65e0\u6cd5\u8fdb\u884c\u5b8c\u6574\u7684\u8bf7\u6c42\u5904\u7406&#xff0c;\u9700\u8981\u5728\u4e0b\u6b21\u63a5\u6536\u5230\u65b0\u6570\u636e\u540e\u7ee7\u7eed\u6839\u636e\u4e0a\u4e0b\u6587\u8fdb\u884c\u89e3\u6790&#xff0c;\u6700\u7ec8\u5f97\u5230\u4e00\u4e2aHttpRequest\u8bf7\u6c42\u4fe1\u606f\u5bf9\u8c61&#xff0c;\u56e0\u6b64\u5728\u8bf7\u6c42\u6570\u636e\u7684\u63a5\u6536\u4ee5\u53ca\u89e3\u6790\u90e8\u5206\u9700\u8981\u4e00\u4e2a\u4e0a\u4e0b\u6587\u6765\u8fdb\u884c\u63a7\u5236\u63a5\u6536\u548c\u5904\u7406\u8282\u594f\u3002<\/p>\n<\/p>\n<p>HttpServer\u6a21\u5757&#xff1a; \u8fd9\u4e2a\u6a21\u5757\u662f\u6700\u7ec8\u7ed9\u7ec4\u4ef6\u4f7f\u7528\u8005\u63d0\u4f9b\u7684HTTP\u670d\u52a1\u5668\u6a21\u5757\u4e86&#xff0c;\u7528\u4e8e\u4ee5\u7b80\u5355\u7684\u63a5\u53e3\u5b9e\u73b0HTTP\u670d\u52a1\u5668\u7684\u642d\u5efa\u3002 HttpServer\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aTcpServer\u5bf9\u8c61&#xff1a;TcpServer\u5bf9\u8c61\u5b9e\u73b0\u670d\u52a1\u5668\u7684\u642d\u5efa HttpServer\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e24\u4e2a\u63d0\u4f9b\u7ed9TcpServer\u5bf9\u8c61\u7684\u63a5\u53e3&#xff1a;\u8fde\u63a5\u5efa\u7acb\u6210\u529f\u8bbe\u7f6e\u4e0a\u4e0b\u6587\u63a5\u53e3&#xff0c;\u6570\u636e\u5904\u7406\u63a5\u53e3\u3002 HttpServer\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2ahash-map\u8868\u5b58\u50a8\u8bf7\u6c42\u4e0e\u5904\u7406\u51fd\u6570\u7684\u6620\u5c04\u8868&#xff1a;\u7ec4\u4ef6\u4f7f\u7528\u8005\u5411 HttpServer\u8bbe\u7f6e\u54ea\u4e9b\u8bf7\u6c42\u5e94\u8be5\u4f7f\u7528\u54ea\u4e9b\u51fd\u6570\u8fdb\u884c\u5904\u7406&#xff0c;\u7b49TcpServer\u6536\u5230\u5bf9\u5e94\u7684\u8bf7\u6c42\u5c31\u4f1a\u4f7f\u7528\u5bf9\u5e94\u7684\u51fd\u6570\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>2.\u4ee3\u7801\u5b9e\u73b0<\/p>\n<p>2.1\u00a0 SERVER\u670d\u52a1\u5668\u6a21\u5757\u5b9e\u73b0&#xff1a;<\/p>\n<p>1.Buffer \u6a21\u5757\u5b9e\u73b0&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"223\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619072543-6853bb7764d37.png\" width=\"1200\" \/><\/p>\n<p>\u603b\u7f13\u51b2\u533a\u5927\u5c0f\u662f\u4e00\u5b9a\u7684&#xff0c;\u5f00\u59cb\u7684\u65f6\u5019readbuff\u7b49\u4e8ewritebuff,\u4e3a0&#xff0c;\u5f53\u5199\u5165\u6570\u636e\u7684\u65f6\u5019&#xff0c;\u5199\u5927\u5c0f\u4e3a\u6570\u5267\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u7684\u540e\u4e00\u4e2a\u4f4d\u7f6e\u5f00\u59cb&#xff0c;\u5bf9\u4f9d\u7136\u4ece\u6570\u636e\u5f00\u5934\u5f00\u59cb\u8bfb&#xff0c;\u8bfb\u5b8c\u4e4b\u540e&#xff0c;\u6211\u4eec\u4e0d\u7528\u5220\u9664&#xff0c;\u5f53\u518d\u6b21\u5199\u5165\u7684\u65f6\u5019&#xff0c;\u5199\u5165\u5230\u4e0a\u4e00\u4e2a\u6570\u636e\u7684\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;\u8bfb\u7684\u5927\u5c0f\u7b49\u4e8e\u5199\u7684\u5927\u5c0f\u3002\u5199\u7ee7\u7eed\u5f80\u540e\u8d70\u3002\u5982\u679c\u603b\u4f53\u7a7a\u95f4\u4e0d\u591f&#xff0c;\u76f4\u63a5\u6269\u5bb9,\u9700\u8981\u5b9e\u73b0\u7684\u51fd\u6570\u63a5\u53e3\u6bd4\u8f83\u591a\u3002<\/p>\n<p>\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0&#xff1a;<\/p>\n<p>#define BUFFER_DEFAULT_SIZE 1024<br \/>\nclass Buffer<br \/>\n{<br \/>\nprivate:<br \/>\n    std::vector&lt;char&gt; _buffer;\/\/\u603b\u7684\u7f13\u51b2\u533a<br \/>\n    uint64_t _readbuff;\/\/\u8bfb\u7f13\u51b2\u533a\u5927\u5c0f<br \/>\n    uint64_t _writerbuff;\/\/\u5199\u7f13\u51b2\u533a\u5927\u5c0f<br \/>\npublic:<br \/>\n    Buffer() : _readbuff(0), _writerbuff(0), _buffer(BUFFER_DEFAULT_SIZE) {}\/\/\u521d\u59cb\u5316<br \/>\n    \/\/ \u8d77\u59cb\u5730\u5740<br \/>\n    char* Begin()<br \/>\n    {<br \/>\n        return &amp;(*_buffer.begin());<br \/>\n    }<br \/>\n    \/\/ \u5199\u7684\u8d77\u59cb\u5730\u5740<br \/>\n    char* GetWritePos()<br \/>\n    {<br \/>\n        return Begin() &#043; _writerbuff;<br \/>\n    }<br \/>\n    \/\/ \u8bfb\u7684\u8d77\u59cb\u5730\u5740<br \/>\n    char* GetReadPos()<br \/>\n    {<br \/>\n        return Begin() &#043; _readbuff;<br \/>\n    }<br \/>\n    \/\/ \u83b7\u53d6\u7f13\u51b2\u533a\u672b\u5c3e\u7a7a\u95f2\u7a7a\u95f4\u5927\u5c0f&#8211;\u5199\u504f\u79fb\u4e4b\u540e\u7684\u7a7a\u95f2\u7a7a\u95f4, \u603b\u4f53\u7a7a\u95f4\u5927\u5c0f\u51cf\u53bb\u5199\u504f\u79fb<br \/>\n    uint64_t TailIndexSize()<br \/>\n    {<br \/>\n        return _buffer.size() &#8211; _writerbuff;<br \/>\n    }<br \/>\n    \/\/ \u83b7\u53d6\u7f13\u51b2\u533a\u8d77\u59cb\u7a7a\u95f2\u7a7a\u95f4\u5927\u5c0f&#8211;\u8bfb\u504f\u79fb\u4e4b\u524d\u7684\u7a7a\u95f2\u7a7a\u95f4<br \/>\n    uint64_t HeadIndexSize()<br \/>\n    {<br \/>\n        return _readbuff;<br \/>\n    }<br \/>\n    \/\/ \u83b7\u53d6\u53ef\u8bfb\u6570\u636e\u5927\u5c0f &#061; \u5199\u504f\u79fb &#8211; \u8bfb\u504f\u79fb<br \/>\n    uint64_t ReadIndexSize()<br \/>\n    {<br \/>\n        return _writerbuff &#8211; _readbuff;<br \/>\n    }<br \/>\n    \/\/ \u5c06\u8bfb\u504f\u79fb\u5411\u540e\u79fb\u52a8size\u5927\u5c0f<br \/>\n    void MoveReadOff(uint64_t size)<br \/>\n    {<br \/>\n        if (size &#061;&#061; 0) return;<br \/>\n        \/\/ \u5411\u540e\u79fb\u52a8\u7684\u5927\u5c0f&#xff0c;\u5fc5\u987b\u5c0f\u4e8e\u53ef\u8bfb\u6570\u636e\u5927\u5c0f<br \/>\n        assert(size &lt;&#061; ReadIndexSize());<br \/>\n        _readbuff &#043;&#061; size;<br \/>\n    }<br \/>\n    \/\/ \u5c06\u5199\u504f\u79fb\u5411\u540e\u79fb\u52a8<br \/>\n    void MoveWriterOff(uint64_t size)<br \/>\n    {<br \/>\n        if (size &#061;&#061; 0) return;<br \/>\n        assert(size &lt;&#061; TailIndexSize());<br \/>\n        _writerbuff &#043;&#061; size;<br \/>\n    }<br \/>\n    \/\/\u786e\u4fdd\u6709\u8db3\u591f\u7684\u7a7a\u95f4\u8fdb\u884c\u5199<br \/>\n    void EnsureWriteSpace(uint64_t size)<br \/>\n    {<br \/>\n        \/\/ \u672b\u5c3e\u7a7a\u95f4\u8db3\u591f&#xff0c;\u76f4\u63a5\u8fd4\u56de<br \/>\n        if (TailIndexSize() &gt;&#061; size) return;<br \/>\n        if (TailIndexSize() &#043; HeadIndexSize() &gt;&#061; size)<br \/>\n        {<br \/>\n            uint64_t rsz &#061; ReadIndexSize();\/\/\u5199\u7a7a\u95f4\u7684\u5927\u5c0f<br \/>\n            std::copy(GetReadPos(), GetReadPos() &#043; rsz, Begin());\/\/\u76f4\u63a5\u590d\u5236<br \/>\n            _readbuff &#061; 0;\/\/ \u5c06\u8bfb\u504f\u79fb\u5f520<br \/>\n            _writerbuff &#061; rsz; \/\/ \u5c06\u5199\u4f4d\u7f6e\u7f6e\u4e3a\u53ef\u8bfb\u6570\u636e\u5927\u5c0f&#xff0c; \u56e0\u4e3a\u5f53\u524d\u7684\u53ef\u8bfb\u6570\u636e\u5927\u5c0f\u5c31\u662f\u5199\u504f\u79fb\u91cf<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            \/\/ \u603b\u4f53\u7a7a\u95f4\u4e0d\u591f&#xff0c;\u5219\u9700\u8981\u6269\u5bb9&#xff0c;\u4e0d\u79fb\u52a8\u6570\u636e&#xff0c;\u76f4\u63a5\u7ed9\u5199\u504f\u79fb\u4e4b\u540e\u6269\u5bb9\u8db3\u591f\u7a7a\u95f4\u5373\u53ef<br \/>\n            DBG_LOG(&#034;RESIZE %ld&#034;, _writerbuff &#043; size);\/\/\u6269\u5bb9<br \/>\n            _buffer.resize(_writerbuff &#043; size);<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u5199\u6570\u636e<br \/>\n    void Write(const void* data, uint64_t size)<br \/>\n    {<br \/>\n        if (size &#061;&#061; 0) return;<br \/>\n        EnsureWriteSpace(size);<br \/>\n        const char* wd &#061; (const char* )data;<br \/>\n        std::copy(wd, wd &#043; size, GetWritePos());<br \/>\n    }<br \/>\n    \/\/ \u5c3e\u63d2<br \/>\n    void WritePush(const void* data, uint64_t size)<br \/>\n    {<br \/>\n        Write(data, size);<br \/>\n        MoveWriterOff(size);<br \/>\n    }<br \/>\n    \/\/\u5199string<br \/>\n    void WriteString(const std::string&amp; data)<br \/>\n    {<br \/>\n        return Write(data.c_str(), data.size());<br \/>\n    }<br \/>\n    \/\/\u5199\u6570\u636e\u5e76\u63d2\u5165<br \/>\n    void WriteStringAndPush(const std::string &amp;data)<br \/>\n    {<br \/>\n        WriteString(data);<br \/>\n        MoveWriterOff(data.size());<br \/>\n    }<br \/>\n    void WriteBuffer(Buffer &amp;data)<br \/>\n    {<br \/>\n        return Write(data.GetReadPos(), data.ReadIndexSize());<br \/>\n    }<br \/>\n    void WriteBufferAndPush(Buffer &amp;data)<br \/>\n    {<br \/>\n        WriteBuffer(data);<br \/>\n        MoveWriterOff(data.ReadIndexSize());<br \/>\n    }<br \/>\n    \/\/ \u8bfb\u53d6\u6570\u636e<br \/>\n    void Read(void *buf, uint64_t size)<br \/>\n    {<br \/>\n        \/\/ \u8981\u6c42\u8981\u83b7\u53d6\u7684\u6570\u636e\u5927\u5c0f\u5fc5\u987b\u5c0f\u4e8e\u53ef\u8bfb\u6570\u636e\u5927\u5c0f<br \/>\n        assert(size &lt;&#061; ReadIndexSize());<br \/>\n        std::copy(GetReadPos(), GetReadPos() &#043; size, (char *)buf);<br \/>\n    }<br \/>\n    \/\/\u8bfb\u5e76\u5220\u9664<br \/>\n    void ReadAndPop(void *buf, uint64_t size)<br \/>\n    {<br \/>\n        Read(buf, size);<br \/>\n        MoveReadOff(size);<br \/>\n    }<br \/>\n    \/\/\u8bfb<br \/>\n    std::string ReadAsString(uint64_t size)<br \/>\n    {<br \/>\n        \/\/ \u8981\u6c42\u8981\u83b7\u53d6\u7684\u6570\u636e\u5927\u5c0f\u5fc5\u987b\u5c0f\u4e8e\u53ef\u8bfb\u6570\u636e\u5927\u5c0f<br \/>\n        assert(size &lt;&#061; ReadIndexSize());<br \/>\n        std::string str;<br \/>\n        str.resize(size);<br \/>\n        Read(&amp;str[0], size);<br \/>\n        return str;<br \/>\n    }<br \/>\n    \/\/\u8bfb<br \/>\n    std::string ReadAsStringAndPop(uint64_t size)<br \/>\n    {<br \/>\n        assert(size &lt;&#061; ReadIndexSize());<br \/>\n        std::string str &#061; ReadAsString(size);<br \/>\n        MoveReadOff(size);<br \/>\n        return str;<br \/>\n    }<br \/>\n    \/\/\u67e5\u627eCRLF<br \/>\n    char* FindCRLF()<br \/>\n    {<br \/>\n        char* res &#061; (char *)memchr(GetReadPos(), &#039;\\\\n&#039;, ReadIndexSize());\/\/\u8fd4\u56de\u627e\u5230\u7684\u5730\u5740<br \/>\n        return res;<br \/>\n    }<br \/>\n    std::string GetLine()<br \/>\n    {<br \/>\n        char *pos &#061; FindCRLF();<br \/>\n        if (pos &#061;&#061; NULL) return &#034;&#034;;<br \/>\n        \/\/ &#043;1\u662f\u4e3a\u4e86\u628a\u6362\u884c\u5b57\u7b26\u4e5f\u53d6\u51fa\u6765\u3002<br \/>\n        return ReadAsString(pos &#8211; GetReadPos() &#043; 1);<br \/>\n    }<br \/>\n    std::string GetLineAndPop()<br \/>\n    {<br \/>\n        std::string str &#061; GetLine();<br \/>\n        MoveReadOff(str.size());<br \/>\n        return str;<br \/>\n    }<br \/>\n    \/\/ \u6e05\u7a7a\u7f13\u51b2\u533a<br \/>\n    void Clear()<br \/>\n    {<br \/>\n        \/\/ \u53ea\u9700\u8981\u5c06\u504f\u79fb\u91cf\u5f520\u5373\u53ef<br \/>\n        _readbuff &#061; 0;<br \/>\n        _writerbuff &#061; 0;<br \/>\n    }<br \/>\n}; <\/p>\n<p>2.Socket\u5b9e\u73b0&#xff1a;<\/p>\n<p>Socket\u5957\u63a5\u5b57\u7684\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355&#xff0c;\u5c31\u662f\u5c06\u7f51\u7edc\u51fd\u6570\u63a5\u53e3\u8fdb\u884c\u5c01\u88c5&#xff0c;Socket\u7684\u4e3b\u8981\u529f\u80fd&#xff1a;\u521b\u5efa\u5957\u63a5\u5b57&#xff0c;\u7ed1\u5b9a\u5730\u5740\u548c\u7aef\u53e3\u53f7&#xff0c;\u76d1\u542c\u662f\u5426\u6709\u94fe\u63a5&#xff0c;\u670d\u52a1\u7aef\u83b7\u53d6\u65b0\u94fe\u63a5&#xff0c;\u5ba2\u6237\u7aef\u53d1\u8d77\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u63a5\u6536\u6570\u636e&#xff0c;\u53d1\u9001\u6570\u636e&#xff0c;\u5173\u95ed\u5957\u63a5\u5b57&#xff0c;\u7aef\u53e3\u53f7\u662f\u5426\u91cd\u7528&#xff0c;\u8bbe\u4e3a\u975e\u963b\u585e\u7b49\u3002<\/p>\n<p>\u4ee3\u7801\u63a5\u53e3&#xff1a;<\/p>\n<p>#define MAX_LISTEN 1024<br \/>\nclass Socket<br \/>\n{<br \/>\nprivate:<br \/>\n    int _socket;<br \/>\npublic:<br \/>\n    Socket() : _socket(-1){}<br \/>\n    Socket(int socket) : _socket(socket){}<br \/>\n    ~Socket() { Close(); }<br \/>\n    int Fd() {return _socket;}<br \/>\n    \/\/ \u521b\u5efasocket<br \/>\n    bool Create()<br \/>\n    {<br \/>\n        _socket &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n        if (_socket &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;CREATE SOCKET FAILED!!&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u7ed1\u5b9a\u5730\u5740\u4fe1\u606f<br \/>\n    bool Bind(const std::string &amp;ip, uint16_t &amp;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 len &#061; sizeof(local);<br \/>\n        if (bind(_socket, (struct sockaddr *)&amp;local, len) &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;BIND ADDRESS FAILED!&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u76d1\u542c\u5730\u5740<br \/>\n    bool Listen(int backlog &#061; MAX_LISTEN)<br \/>\n    {<br \/>\n        if (listen(_socket, backlog) &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;SOCKET LISTEN FAILED!&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        else return true;<br \/>\n    }<br \/>\n    \/\/ \u8bf7\u6c42\u8fde\u63a5<br \/>\n    bool Connect(const std::string&amp; ip,const uint16_t&amp; 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 len &#061; sizeof(local);<br \/>\n        if (connect(_socket, (struct sockaddr *)&amp;local, len) &lt; 0) \/\/ \u9519\u8bef<br \/>\n        {<br \/>\n            ERR_LOG(&#034;BIND ADDRESS FAILED!&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u83b7\u53d6\u65b0\u94fe\u63a5<br \/>\n    int Accept()<br \/>\n    {<br \/>\n        int ret &#061; accept(_socket, NULL, NULL);<br \/>\n        if (ret &lt; 0)<br \/>\n        {<br \/>\n            ERR_LOG(&#034;BIND ADDRESS FAILED!&#034;);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return ret;<br \/>\n    }<br \/>\n    \/\/ \u8bfb\u53d6\u6570\u636e<br \/>\n    bool Recv(char *buf, ssize_t size, int flag &#061; 0)<br \/>\n    {<br \/>\n        int n &#061; recv(_socket, buf, size, flag);<br \/>\n        if (n &lt;&#061; 0)<br \/>\n        {<br \/>\n            if (errno &#061;&#061; EAGAIN || n &#061;&#061; EINTR) return 0; \/\/ \u8fd9\u6b21\u6ca1\u6709\u6570\u636e<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u975e\u963b\u585e\u8bfb\u53d6<br \/>\n    bool NoneBlockRecv(char *buf, ssize_t size)<br \/>\n    {<br \/>\n        if (size &#061;&#061; 0) return 0;<br \/>\n        Recv(buf, size, MSG_DONTWAIT); \/\/ MSG_DONTWAIT\u8868\u793a\u5f53\u524d\u4e3a\u975e\u963b\u585e<br \/>\n    }<br \/>\n    \/\/ \u53d1\u9001<br \/>\n    bool Send(const char* buf, ssize_t size, int flag &#061; 0)<br \/>\n    {<br \/>\n        int n &#061; send(_socket, buf, size, flag);<br \/>\n        if (n &lt;&#061; 0)<br \/>\n        {<br \/>\n            if (errno &#061;&#061; EAGAIN || n &#061;&#061; EINTR) return 0; \/\/ \u8fd9\u6b21\u6ca1\u6709\u6570\u636e<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u975e\u963b\u585e\u53d1\u9001<br \/>\n    bool NoneBlockSend(char *buf, size_t size)<br \/>\n    {<br \/>\n        if (size &#061;&#061; 0) return 0;<br \/>\n        Send(buf, size, MSG_DONTWAIT);<br \/>\n    }<br \/>\n    \/\/ \u5173\u95ed<br \/>\n    void Close()<br \/>\n    {<br \/>\n        if (_socket &gt;&#061; 0)<br \/>\n        {<br \/>\n            close(_socket);<br \/>\n            _socket &#061; -1;<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u521b\u5efa\u670d\u52a1\u6bb5\u7aef<br \/>\n    bool CreateServer(uint16_t &amp;port, const std::string &amp;ip &#061; &#034;0.0.0.0&#034;, bool block_flag &#061; false)<br \/>\n    {<br \/>\n        if (Create() &#061;&#061; false) return false;<br \/>\n        if (block_flag) NoneBlock(); \/\/ \u8bbe\u4e3a\u975e\u963b\u585e\u72b6\u6001<br \/>\n        if (Bind(ip, port) &#061;&#061; false) return false;<br \/>\n        if (Listen() &#061;&#061; false) return false;<br \/>\n        ReuseAddress();<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u521b\u5efa\u5ba2\u6237\u7aef<br \/>\n    bool CreateClient(const uint16_t&amp; port, const std::string&amp; ip &#061; &#034;0.0.0.0&#034;)<br \/>\n    {<br \/>\n        \/\/ 1. \u521b\u5efa\u5957\u63a5\u5b57&#xff0c;2.\u6307\u5411\u8fde\u63a5\u670d\u52a1\u5668<br \/>\n        if (Create() &#061;&#061; false) return false;<br \/>\n        if (Connect(ip, port) &#061;&#061; false) return false;<br \/>\n        return true;<br \/>\n    }<br \/>\n    \/\/ \u5f00\u542f\u5730\u5740\u7aef\u53e3\u91cd\u7528<br \/>\n    void ReuseAddress()<br \/>\n    {<br \/>\n        int val &#061; 1;<br \/>\n        setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (void *)&amp;val, sizeof(int));<br \/>\n        val &#061; 1;<br \/>\n        setsockopt(_socket, SOL_SOCKET, SO_REUSEPORT, (void *)&amp;val, sizeof(int));<br \/>\n    }<br \/>\n    \/\/ \u975e\u963b\u585e<br \/>\n    void NoneBlock()<br \/>\n    {<br \/>\n        int flag &#061; fcntl(_socket, F_GETFL, 0);<br \/>\n        fcntl(_socket, F_SETFL, flag | O_NONBLOCK);<br \/>\n    }<br \/>\n}; <\/p>\n<p>3.Channel\u6a21\u5757&#xff1a;<\/p>\n<p>Chennal \u6a21\u5757\u662f\u5bf9\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u5c01\u88c5&#xff0c;\u73b0\u5bf9\u63cf\u8ff0\u7b26\u53ef\u8bfb&#xff0c;\u53ef\u5199&#xff0c;\u9519\u8bef\u2026\u4e8b\u4ef6\u7684\u7ba1\u7406\u64cd\u4f5c&#xff0c;\u4ee5\u53caPoller\u6a21\u5757\u5bf9\u63cf\u8ff0\u7b26\u8fdb\u884cIO\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<\/p>\n<p>\u529f\u80fd&#xff1a;\u5bf9\u4e00\u4e2a\u63cf\u8ff0\u7b26\u8fdb\u884c\u76d1\u63a7\u65f6\u95f4\u7ba1\u7406&#xff0c;\u610f\u4e49&#xff0c;\u5bf9\u4e8e\u63cf\u8ff0\u7b26\u7684\u72b6\u6001\u66f4\u5bb9\u6613\u7ef4\u62a4&#xff0c;\u5bf9\u4e4b\u540e\u7684\u64cd\u4f5c\u6d41\u7a0b\u66f4\u52a0\u65b9\u4fbf&#xff0c;\u529f\u80fd\u8bbe\u8ba1&#xff1a;\u63cf\u8ff0\u7b26\u662f\u5426\u53ef\u8bfb&#xff0c;\u662f\u5426\u53ef\u5199&#xff0c;\u76d1\u63a7\u53ef\u8bfb&#xff0c;\u76d1\u63a7\u53ef\u5199&#xff0c;\u89e3\u9664\u53ef\u8bfb&#xff0c;\u89e3\u9664\u53ef\u5199&#xff0c;\u89e3\u9664\u6240\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>\u4ee3\u7801\u6846\u67b6&#xff1a;<\/p>\n<p>class Channel {<\/p>\n<p>        private:<br \/>\n                int _fd;<br \/>\n                uint32_t events;  \/\/ \u5f53\u524d\u9700\u8981\u76d1\u63a7\u7684\u4e8b\u4ef6<br \/>\n                uint32_t revents; \/\/ \u5f53\u524d\u8fde\u63a5\u89e6\u53d1\u7684\u4e8b\u4ef6<br \/>\n                using eventCallback &#061; std::function&lt;void()&gt;&gt;;<br \/>\n                eventCallback _read_callback;  \/\/ \u53ef\u8bfb\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n                eventCallback _error_callback; \/\/ \u53ef\u5199\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n                eventCallback _close_callback; \/\/ \u8fde\u63a5\u5173\u95ed\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n                eventCallback _event_callback; \/\/ \u4efb\u610f\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n                eventCallback _write_callback; \/\/ \u53ef\u5199\u4e8b\u4ef6\u88ab\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n        public:<br \/>\n                Channel(int fd) : fd(_fd) {}<br \/>\n                int Fd() {return _fd ;}<br \/>\n                void setReadCallback(const eventCallback &amp;cb);\/\/\u8bbe\u7f6e\u8bfb\u76d1\u63a7<br \/>\n                void setWriteCallback(const eventCallback &amp;cb);\/\/\u5199\u76d1\u63a7<br \/>\n                void setErrorCallback(const eventCallback &amp;cb);\/\/\u9519\u8bef\u76d1\u63a7<br \/>\n                void setCloseCallback(const eventCallback &amp;cb);\/\/\u5173\u95ed\u76d1\u63a7<br \/>\n                void setEventCallback(const eventCallback &amp;cb);\/\/\u4efb\u610f\u65f6\u95f4\u76d1\u63a7<br \/>\n                bool readAble();   \/\/ \u5f53\u524d\u662f\u5426\u53ef\u8bfb<br \/>\n                bool writeAble();  \/\/ \u5f53\u524d\u662f\u5426\u53ef\u5199<br \/>\n                void enableRead(); \/\/ \u542f\u52a8\u8bfb\u4e8b\u4ef6\u76d1\u63a7<br \/>\n                void enableWrite(); \/\/ \u542f\u52a8\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n                void disableRead();     \/\/ \u5173\u95ed\u8bfb\u4e8b\u4ef6\u76d1\u63a7<br \/>\n                void disableWrite(); \/\/ \u5173\u95ed\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n                void disableAll();  \/\/ \u5173\u95ed\u6240\u6709\u4e8b\u4ef6\u76d1\u63a7<br \/>\n                void Remove();  \/\/\u79fb\u9664\u76d1\u63a7<br \/>\n                void handleEvent(); \/\/ \u4e8b\u4ef6\u5904\u7406&#xff0c;\u4e00\u65e6\u89e6\u53d1\u4e86\u67d0\u4e2a\u4e8b\u4ef6&#xff0c;\u5c31\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570&#xff01;<\/p>\n<p>};<\/p>\n<p>4.Poller\u6a21\u5757<\/p>\n<p>\u63cf\u8ff0\u7b26\u4e8b\u4ef6\u76d1\u63a7\u6a21\u5757&#xff0c;\u5bf9\u4efb\u610f\u63cf\u8ff0\u7b26\u8fdb\u884c\u65f6\u95f4\u76d1\u63a7&#xff0c;\u5c31\u662f\u5bf9epoll\u51fd\u6570\u5f97\u5c01\u88c5&#xff0c;\u65f6\u671f\u53d8\u5f97\u66f4\u6709\u610f\u4e49\u3002<\/p>\n<p>\u529f\u80fd\u8bbe\u8ba1&#xff1a;\u6dfb\u52a0\u4e8b\u4ef6&#xff0c;\u4fee\u6539\u4e8b\u4ef6&#xff0c;\u79fb\u9664\u4e8b\u4ef6&#xff0c;\u53d6\u6d88\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n<p>\u5c01\u88c5\u601d\u60f3&#xff1a; 1. \u5fc5\u987b\u62e5\u6709\u4e00\u4e2aepoll\u7684\u64cd\u4f5c\u53e5\u67c4 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a02. \u62e5\u6709\u4e00\u4e2astruct epoll_event \u7ed3\u6784\u6570\u7ec4&#xff0c;\u76d1\u63a7\u4fdd\u5b58\u6240\u6709\u7684\u6d3b\u8dc3\u4e8b\u4ef6&#xff01; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a03. \u4f7f\u7528hash\u8868\u7ba1\u7406\u63cf\u8ff0\u7b26\u4e0e\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u4e8b\u4ef6\u7ba1\u7406Channnel\u5bf9\u8c61&#xff01; \u903b\u8f91\u6d41\u7a0b&#xff1a; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a01. \u5bf9\u63cf\u8ff0\u7b26\u8fdb\u884c\u76d1\u63a7&#xff0c;\u901a\u8fc7Channnel\u624d\u80fd\u77e5\u9053\u63cf\u8ff0\u7b26\u76d1\u63a7\u4ec0\u4e48\u4e8b\u4ef6 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a02. \u5f53\u63cf\u8ff0\u7b26\u5c31\u7eea\u4e86&#xff0c;\u901a\u8fc7\u63cf\u8ff0\u7b26\u5728hash\u8868\u4e2d\u627e\u5230\u5bf9\u5e94\u7684Channel&#xff08;\u5f97\u5230\u4e86Channel\u624d\u77e5\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u9053\u4ec0\u4e48\u4e8b\u4ef6\u5982\u4f55\u5904\u7406&#xff09;\u5f53\u63cf\u8ff0\u7b26\u5c31\u7eea\u4e86&#xff0c;\u8fd4\u56de\u5c31\u7eea\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684Channel\u3002<\/p>\n<p>\u4ee3\u7801\u6846\u67b6<\/p>\n<p>\u6846\u67b6&#xff1a;<br \/>\nclass Poller {<br \/>\nprivate:<br \/>\n        int _epfd;<br \/>\n        struct epoll_event_evs[xxx];<br \/>\n        std::unordered_map&lt;int,Channel*&gt; mp;<br \/>\nprivate:<br \/>\n        \/\/ 1. \u5224\u65ad\u8981\u66f4\u65b0\u4e8b\u4ef6\u7684\u63cf\u8ff0\u7b26\u662f\u5426\u5b58\u5728<br \/>\n        \/\/ 2. \u9488\u5bf9epoll\u76f4\u63a5\u64cd\u4f5c&#xff08;\u6dfb\u52a0&#xff0c;\u4fee\u6539&#xff0c;\u79fb\u9664&#xff09;<br \/>\npublic:<br \/>\n        \/\/ 1. \u6dfb\u52a0\u6216\u8005\u66f4\u65b0\u63cf\u8ff0\u7b26\u6240\u76d1\u63a7\u7684\u4e8b\u4ef6<br \/>\n        void Update(Channel* channel);<br \/>\n        \/\/ 2. \u79fb\u9664\u63cf\u8ff0\u7b26\u6240\u76d1\u63a7\u7684\u4e8b\u4ef6<br \/>\n        void Remove(Channel* )<br \/>\n        \/\/ 3. \u5f00\u59cb\u76d1\u63a7&#xff0c;\u83b7\u53d6\u5c31\u7eeaChannel<br \/>\n};<br \/>\n*\/<br \/>\n\/*<\/p>\n<p>5.EventLoop\u6a21\u5757<\/p>\n<p>\u8fd9\u4e2a\u6a21\u5757\u548c\u7ebf\u7a0b\u662f\u4e00\u4e00\u5bf9\u5e94\u7684&#xff01; \u76d1\u542c\u4e86\u4e00\u4e2a\u94fe\u63a5&#xff0c;\u5982\u679c\u8fd9\u4e2a\u8fde\u63a5\u4e00\u65e6\u5c31\u7eea&#xff0c;\u5c31\u8981\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406&#xff01; \u4f46\u662f\u5982\u679c\u8fd9\u4e2a\u63cf\u8ff0\u7b26&#xff0c;\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e2d\u90fd\u89e6\u53d1\u4e86\u4e86\u4e8b\u4ef6&#xff0c;\u8fdb\u884c\u5904\u7406&#xff0c;\u5c31\u4f1a\u5b58\u5728\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff01; \u56e0\u6b64\u6211\u4eec\u9700\u8981\u5c06\u4e00\u4e2a\u94fe\u63a5\u7684\u4e8b\u4ef6\u76d1\u63a7&#xff0c; \u4ee5\u53ca\u8fde\u63a5\u4e8b\u4ef6\u5904\u7406&#xff0c;\u4ee5\u53ca\u5176\u4ed6\u64cd\u4f5c\u90fd\u653e\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e2d&#xff01; \u5982\u4f55\u4fdd\u8bc1\u4e00\u4e2a\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u5728eventloop\u5bf9\u5e94\u7684\u7ebf\u7a0b\u4e2d&#xff01; \u7ed9eventLOOP\u6a21\u5757\u4e2d&#xff0c;\u90fd\u6dfb\u52a0\u4e00\u4e2a\u4efb\u52a1\u961f\u5217&#xff01; \u5bf9\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c&#xff0c;\u90fd\u8fdb\u884c\u4e00\u6b21\u5c01\u88c5&#xff0c;\u5c06\u5bf9\u8fde\u63a5\u7684\u64cd\u4f5c\u5f53\u4f5c\u4efb\u52a1\u90fd\u6dfb\u52a0\u5230\u4efb\u52a1\u961f\u5217\u4e2d&#xff01;<\/p>\n<p>\u529f\u80fd&#xff1a;\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u7684\u6a21\u5757&#xff0c;\u4e00\u4e2a\u6a21\u5757\u5bf9\u5e94\u4e00\u4e2a\u7ebf\u7a0b\u3002\u610f\u4e49&#xff0c;\u6240\u6709\u7684\u7ebf\u7a0b\u90fd\u5728\u8fd9\u4e2a\u6a21\u5757\u4e2d\u5b8c\u6210&#xff0c;\u529f\u80fd\u8bbe\u8ba1&#xff0c;\u5c06\u4e00\u4e2a\u4efb\u52a1\u6dfb\u52a0\u5230\u4efb\u52a1\u961f\u5217\u4e2d&#xff0c;\u5b9a\u65f6\u4efb\u52a1\u7684\u6dfb\u52a0&#xff0c;\u53d6\u6d88&#xff0c;\u5237\u65b0\u3002<\/p>\n<p>\u4e8b\u4ef6\u529f\u80fd&#xff1a;<\/p>\n<li>\u4e8b\u4ef6\u76d1\u63a7<\/li>\n<li>\u4f7f\u7528Poller\u6a21\u5757<\/li>\n<li>\u6709\u4e8b\u4ef6\u5c31\u7eea\u5219\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406&#xff01;<\/li>\n<li>\u6267\u884c\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4efb\u52a1&#xff01;<\/li>\n<li>\u6ce8\u610f\u70b9&#xff1a;<\/li>\n<li>\u56e0\u4e3a\u6709\u53ef\u80fd\u56e0\u4e3a\u7b49\u5f85\u63cf\u8ff0\u7b26IO\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u6267\u884c\u6d41\u6d41\u7a0b\u963b\u585e&#xff0c;\u8fd9\u4e2a\u65f6\u5019\u4efb\u52a1\u5bf9\u7acb\u4e2d\u7684\u4efb\u52a1\u5f97\u4e0d\u5230\u6267\u884c&#xff01;<\/li>\n<li>\u56e0\u6b64\u5f97\u6709\u4e00\u4e2a\u4e8b\u4ef6\u901a\u77e5\u7684\u4e1c\u897f&#xff0c;\u80fd\u591f\u5524\u9192\u4e8b\u4ef6\u76d1\u63a7\u7684\u963b\u585e&#xff01;<\/li>\n<li>\u5f53\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u9700\u8981\u5904\u7406\u7684\u65f6\u5019&#xff0c;\u5904\u7406\u8fc7\u7a0b\u4e2d&#xff0c;\u5982\u679c\u5bf9\u8fde\u63a5\u8981\u8fdb\u884c\u67d0\u4e9b\u64cd\u4f5c&#xff01;<\/li>\n<li>\u8fd9\u4e9b\u64cd\u4f5c\u5fc5\u987b\u8981\u5728Eventloop\u5bf9\u5e94\u7684\u7ebf\u7a0b\u4e2d\u8fdb\u884c&#xff0c;\u4fdd\u8bc1\u5bf9\u8fde\u63a5\u7684\u5404\u9879\u64cd\u4f5c\u90fd\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/li>\n<li>\u5982\u679c\u6267\u884c\u7684\u64cd\u4f5c\u5c31\u5728\u672c\u7ebf\u7a0b\u4e2d&#xff0c;\u4e0d\u9700\u8981\u5c06\u64cd\u4f5c\u538b\u5165\u961f\u5217\u4e86&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u6267\u884c&#xff01;<\/li>\n<li>\u5982\u679c\u6267\u884c\u7684\u64cd\u4f5c\u4e0d\u5728\u7ebf\u7a0b\u4e2d&#xff0c;\u624d\u9700\u8981\u52a0\u5165\u4efb\u52a1\u6c60&#xff0c;\u7b49\u5230\u4e8b\u4ef6\u5904\u7406\u5b8c\u4e86\u4e4b\u540e\u5c31\u884c\u6267\u884c\u4efb\u52a1&#xff01;<\/li>\n<p>\u8bbe\u8ba1\u6846\u67b6&#xff1a;<\/p>\n<p> class Eventloop {<br \/>\nprivate:<br \/>\n        std::thread::id _thread_id; \/\/ \u7ebf\u7a0bID<br \/>\n        int _event_fd \/\/ eventfd \u5524\u9192IO\u4e8b\u4ef6\u76d1\u63a7\u6709\u53ef\u80fd\u7684\u963b\u585e&#xff01;&#xff01;&#xff01;<br \/>\n        Poller _poller; \/\/ \u8fdb\u884c\u6240\u6709\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7<br \/>\n        using Functor &#061; std::function&lt;void()&gt;;<br \/>\n        std::vector&lt;Functor&gt; _task; \/\/ \u4efb\u52a1\u6c60<br \/>\n        std::mutex _mutex; \/\/ \u5b9e\u73b0\u4efb\u52a1\u6c60\u64cd\u4f5c\u7684\u7ebf\u7a0b\u5b89\u5168&#xff01;&#xff01;&#xff01;<br \/>\npublic:<br \/>\n        void runAllTask();<br \/>\npublic:<br \/>\n        Eventloop();<br \/>\n        void runInLoop(const Functor&amp;cb); \/\/ \u5224\u65ad\u5c06\u8981\u6267\u884c\u7684\u4efb\u52a1\u662f\u5426\u5904\u4e8e\u5f53\u524d\u7ebf\u7a0b\u4e2d&#xff0c;\u5982\u679c\u662f\u5219\u6267\u884c&#xff0c;\u4e0d\u662f\u5219\u538b\u5165\u961f\u5217\u3002<br \/>\n        void queueInLoop(const Functor&amp;cb);  \/\/ \u5c06\u64cd\u4f5c\u538b\u5165\u4efb\u52a1\u6c60&#xff01;<br \/>\n        bool isInLoop(); \/\/\u6c38\u8fdc\u5224\u65ad\u5f53\u524d\u7ebf\u7a0b\u662f\u5426\u662fEventLoop\u6240\u5bf9\u5e94\u7684\u7ebf\u7a0b<br \/>\n        void updateEvent(Channel* channel); \/\/ \u6dfb\u52a0\/\u4fee\u6539\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7<br \/>\n        void removeEvent(Channel* channel);  \/\/ \u79fb\u9664\u63cf\u8ff0\u7b26\u7684\u76d1\u63a7<br \/>\n        void Start(); \/\/ \u4efb\u52a1\u76d1\u63a7\u5b8c\u6bd5\u8fdb\u884c\u5904\u7406\u4efb\u52a1&#xff01; \u4e09\u6b65\u8d70&#xff1a;\u4e8b\u4ef6\u76d1\u63a7-\u300b\u5c31\u7eea\u4e8b\u4ef6\u5904\u7406-\u300b\u6267\u884c\u4efb\u52a1<\/p>\n<p>};<\/p>\n<p>6.Connection\u6a21\u5757<\/p>\n<p>Connection\u6a21\u5757\u662f\u5bf9Buffer\u6a21\u5757&#xff0c;Socket\u6a21\u5757&#xff0c;Channel\u6a21\u5757\u7684\u2f00\u4e2a\u6574\u4f53\u5c01\u88c5&#xff0c;\u5b9e\u73b0\u4e86\u5bf9\u4e00\u4e2a\u901a\u4fe1\u5957\u63a5\u5b57\u7684\u6574\u4f53\u7684\u7ba1\u7406&#xff0c;\u6bcf\u4e00\u4e2a\u8fdb\u884c\u6570\u636e\u901a\u4fe1\u7684\u5957\u63a5\u5b57&#xff08;\u4e5f\u5c31\u662faccept\u83b7\u53d6\u5230\u7684\u65b0\u8fde\u63a5&#xff09;\u90fd\u4f1a\u4f7f\u7528 Connection\u8fdb\u884c\u7ba1\u7406\u3002 \u2022 Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e09\u4e2a\u7531\u7ec4\u4ef6\u4f7f\u7528\u8005\u4f20\u5165\u7684\u56de\u8c03\u51fd\u6570&#xff1a;\u8fde\u63a5\u5efa\u7acb\u5b8c\u6210\u56de\u8c03&#xff0c;\u4e8b\u4ef6\u56de\u8c03&#xff0c; \u65b0\u6570\u636e\u56de\u8c03&#xff0c;\u5173\u95ed\u56de\u8c03\u3002 \u2022 Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e24\u4e2a\u7ec4\u4ef6\u4f7f\u7528\u8005\u63d0\u4f9b\u7684\u63a5\u53e3&#xff1a;\u6570\u636e\u53d1\u9001\u63a5\u53e3&#xff0c;\u8fde\u63a5\u5173\u95ed\u63a5\u53e3 \u2022 Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e24\u4e2a\u7528\u6237\u6001\u7f13\u51b2\u533a&#xff1a;\u7528\u6237\u6001\u63a5\u6536\u7f13\u51b2\u533a&#xff0c;\u7528\u6237\u6001\u53d1\u9001\u7f13\u51b2\u533a \u2022 Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u2f00\u4e2aSocket\u5bf9\u8c61&#xff1a;\u5b8c\u6210\u63cf\u8ff0\u7b26\u9762\u5411\u7cfb\u7edf\u7684IO\u64cd\u4f5c \u2022 Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u2f00\u4e2aChannel\u5bf9\u8c61&#xff1a;\u5b8c\u6210\u63cf\u8ff0\u7b26IO\u4e8b\u4ef6\u5c31\u7eea\u7684\u5904\u7406<\/p>\n<p>\u8fd9\u662f\u5bf9\u901a\u4fe1\u5957\u63a5\u5b57\u8fdb\u884c\u901a\u4fe1\u7ba1\u7406\u7684\u4e00\u4e2a\u6a21\u5757&#xff0c;\u5bf9\u4e00\u4e2a\u8fde\u63a5\u7684\u64cd\u4f5c\u90fd\u662f\u901a\u8fc7\u8fd9\u4e2a\u6a21\u5757\u6765\u5b9e\u73b0\u7684\u3002\u8fd9\u5404\u6a21\u5757\u672c\u7701\u5e76\u4e0d\u662f\u4e00\u4e2a\u5355\u72ec\u7684\u529f\u80fd\u6a21\u5757&#xff0c;\u662f\u4e00\u4e2a\u8fde\u63a5\u7ba1\u7406\u7684\u6a21\u5757\u3002<\/p>\n<p>Connection\u6a21\u5757&#xff0c;\u4e00\u4e2a\u8fde\u63a5\u6709\u4efb\u4f55\u7684\u4e8b\u4ef6\u600e\u4e48\u5904\u7406\u90fd\u662f\u6709\u8fd9\u4e2a\u6a21\u5757\u6765\u8fdb\u884c\u5904\u7406\u7684&#xff0c;\u56e0\u4e3a\u7ec4\u4ef6\u7684\u8bbe\u8ba1\u4e5f\u4e0d\u77e5\u9053\u4f7f\u7528\u8005\u8981\u5982\u4f55\u5904\u7406\u4e8b\u4ef6&#xff0c;\u56e0\u6b64\u53ea\u80fd\u662f\u63d0\u4f9b\u4e00\u4e9b\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570\u7531\u4f7f\u7528\u8005\u8bbe\u7f6e\u3002<\/p>\n<p>\u8bbe\u8ba1\u6846\u67b6&#xff1a;<\/p>\n<p>DISCONECTED &#8212; \u8fde\u63a5\u5173\u95ed\u72b6\u6001&#xff1b;   CONNECTING &#8212; \u8fde\u63a5\u5efa\u7acb\u6210\u529f-\u5f85\u5904\u7406\u72b6\u6001<br \/>\n\/\/CONNECTED &#8212; \u8fde\u63a5\u5efa\u7acb\u5b8c\u6210&#xff0c;\u5404\u79cd\u8bbe\u7f6e\u5df2\u5b8c\u6210&#xff0c;\u53ef\u4ee5\u901a\u4fe1\u7684\u72b6\u6001&#xff1b;  DISCONNECTING &#8212; \u5f85\u5173\u95ed\u72b6\u6001<br \/>\ntype enum {<br \/>\n        \/\/ \u8fde\u63a5\u5173\u95ed&#xff1b;<br \/>\n        \/\/ \u8fde\u63a5\u5efa\u7acb\u6210\u529f \u2014\u2014 \u5f85\u5904\u7406\u72b6\u6001&#xff1b;<br \/>\n        \/\/ \u8fde\u63a5\u8bbe\u7acb\u5b8c\u6210&#xff0c;\u53ef\u4ee5\u901a\u4fe1&#xff1b;<br \/>\n        \/\/ \u5f85\u5173\u95ed\u72b6\u6001&#xff1b;<br \/>\n        DISCONECTED,CONNECTING,CONNECTED,DISCONECTING} ConnStatu;<br \/>\nusing PreConnection &#061; std::shared_ptr&lt;Connection&gt;;<br \/>\nclass Connection {<br \/>\n        private:<br \/>\n                uint64_t _conn_id; \/\/\u8fde\u63a5\u7684\u552f\u4e00ID&#xff0c;\u4fbf\u4e8e\u8fde\u63a5\u7684\u7ba1\u7406\u548c\u67e5\u627e<br \/>\n                bool _enable_inactive_release;  \/\/ \u8fde\u63a5\u662f\u5426\u542f\u52a8\u975e\u6d3b\u8dc3\u9500\u6bc1\u7684\u5224\u65ad\u6807\u5fd7&#xff0c;\u9ed8\u8ba4\u4e3afalse<br \/>\n                int _sockfd; \/\/ \u8fde\u63a5\u5173\u8054\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n                ConnStatu _statu; \/\/<br \/>\n                Socket _socket; \/\/ \u5957\u63a5\u5b57\u64cd\u4f5c\u7ba1\u7406<br \/>\n                Channel _channel;  \/\/ \u8fde\u63a5\u4e8c\u70b9\u4e8b\u4ef6\u7ba1\u7406<br \/>\n                Buffer _in_buffer; \/\/ \u8f93\u5165\u7f13\u51b2\u533a \u2014\u2014 \u5b58\u653e\u4ecesocket\u4e2d\u8bfb\u5230\u7684\u6570\u636e<br \/>\n                buffer _out_buffer; \/\/ \u8f93\u51fa\u7f13\u51b2\u533a \u2014\u2014  \u53d1\u9001\u7ed9\u5bf9\u7aef\u7684\u662f\u6570\u636e&#xff0c;\u7b49\u5230\u63cf\u8ff0\u7b26\u4e8b\u4ef6\u53ef\u5199&#xff0c;\u518d\u53d1&#xff01;<br \/>\n                Any _context; \/\/ \u8bf7\u6c42\u7684\u63a5\u53d7\u5904\u7406\u4e0a\u4e0b\u6587<br \/>\n                \/*\u8fd9\u56db\u4e2a\u56de\u8c03\u51fd\u6570&#xff0c;\u662f\u8ba9\u670d\u52a1\u5668\u6a21\u5757\u6765\u8bbe\u7f6e\u7684&#xff08;\u5176\u5b9e\u670d\u52a1\u5668\u6a21\u5757\u7684\u5904\u7406\u56de\u8c03\u4e5f\u662f\u7ec4\u4ef6\u4f7f\u7528\u8005\u8bbe\u7f6e\u7684&#xff09;*\/<br \/>\n                \/*\u6362\u53e5\u8bdd\u8bf4&#xff0c;\u8fd9\u51e0\u4e2a\u56de\u8c03\u90fd\u662f\u7ec4\u4ef6\u4f7f\u7528\u8005\u4f7f\u7528\u7684*\/<br \/>\n                using ConnectCallback &#061; std::function&lt;void(const PreConnection&amp;)&gt;;<br \/>\n                using MessageCallback &#061; std::function&lt;void(const PtrConnection&amp;, Buffer *)&gt;;<br \/>\n                using ClosedCallback &#061; std::function&lt;void(const PtrConnection&amp;)&gt;;<br \/>\n                using AnyEventCallback &#061; std::function&lt;void(const PtrConnection&amp;)&gt;;<br \/>\n                ConnectedCallback _connected_callback;<br \/>\n                MessageCallback _message_callback;<br \/>\n                ClosedCallback _closed_callback;<br \/>\n                AnyEventCallback _event_callback;<br \/>\n                \/*\u7ec4\u4ef6\u5185\u7684\u8fde\u63a5\u5173\u95ed\u56de\u8c03&#8211;\u7ec4\u4ef6\u5185\u8bbe\u7f6e\u7684&#xff0c;\u56e0\u4e3a\u670d\u52a1\u5668\u7ec4\u4ef6\u5185\u4f1a\u628a\u6240\u6709\u7684\u8fde\u63a5\u7ba1\u7406\u8d77\u6765&#xff0c;\u4e00\u65e6\u67d0\u4e2a\u8fde\u63a5\u8981\u5173\u95ed*\/<br \/>\n                \/*\u5c31\u5e94\u8be5\u4ece\u7ba1\u7406\u7684\u5730\u65b9\u79fb\u9664\u6389\u81ea\u5df1\u7684\u4fe1\u606f*\/<br \/>\n                ClosedCallback _server_closed_callback;<br \/>\n        private:<br \/>\n        \/\/ \/*\u4e94\u4e2achannel\u7684\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570*\/<br \/>\n        \/\/\u63cf\u8ff0\u7b26\u53ef\u8bfb\u4e8b\u4ef6\u89e6\u53d1\u540e\u8c03\u7528\u7684\u51fd\u6570&#xff0c;\u63a5\u6536socket\u6570\u636e\u653e\u5230\u63a5\u6536\u7f13\u51b2\u533a\u4e2d&#xff0c;\u7136\u540e\u8c03\u7528_message_callback<br \/>\n                void HandleRead() {<br \/>\n                }<br \/>\n                void HandleRead() {<br \/>\n                }<br \/>\n                void HandleClose() {<br \/>\n                }<br \/>\n                void HandleError() {<br \/>\n                }<br \/>\n                \/\/\u63cf\u8ff0\u7b26\u89e6\u53d1\u4efb\u610f\u4e8b\u4ef6: 1. \u5237\u65b0\u8fde\u63a5\u7684\u6d3b\u8dc3\u5ea6&#8211;\u5ef6\u8fdf\u5b9a\u65f6\u9500\u6bc1\u4efb\u52a1&#xff1b;  2. \u8c03\u7528\u7ec4\u4ef6\u4f7f\u7528\u8005\u7684\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03<br \/>\n                void HandleEvent() {<br \/>\n                }<br \/>\n                \/\/\u8fde\u63a5\u83b7\u53d6\u4e4b\u540e&#xff0c;\u6240\u5904\u7684\u72b6\u6001\u4e0b\u8981\u8fdb\u884c\u5404\u79cd\u8bbe\u7f6e&#xff08;\u542f\u52a8\u8bfb\u76d1\u63a7,\u8c03\u7528\u56de\u8c03\u51fd\u6570&#xff09;<br \/>\n                void EstablishedInLoop() {<br \/>\n                }<br \/>\n                \/\/\u8fd9\u4e2a\u63a5\u53e3\u624d\u662f\u5b9e\u9645\u7684\u91ca\u653e\u63a5\u53e3<br \/>\n                void ReleaseInLoop() {<br \/>\n                }<br \/>\n                \/\/\u8fd9\u4e2a\u63a5\u53e3\u5e76\u4e0d\u662f\u5b9e\u9645\u7684\u53d1\u9001\u63a5\u53e3&#xff0c;\u800c\u53ea\u662f\u628a\u6570\u636e\u653e\u5230\u4e86\u53d1\u9001\u7f13\u51b2\u533a&#xff0c;\u542f\u52a8\u4e86\u53ef\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n                void SendInLoop(Buffer &amp;buf) {<br \/>\n                }<br \/>\n                \/\/\u8fd9\u4e2a\u5173\u95ed\u64cd\u4f5c\u5e76\u975e\u5b9e\u9645\u7684\u8fde\u63a5\u91ca\u653e\u64cd\u4f5c&#xff0c;\u9700\u8981\u5224\u65ad\u8fd8\u6709\u6ca1\u6709\u6570\u636e\u5f85\u5904\u7406&#xff0c;\u5f85\u53d1\u9001<br \/>\n                void ShutdownInLoop() {<br \/>\n                }<br \/>\n                \/\/\u542f\u52a8\u975e\u6d3b\u8dc3\u8fde\u63a5\u8d85\u65f6\u91ca\u653e\u89c4\u5219<br \/>\n                void EnableInactiveReleaseInLoop(int sec) {<br \/>\n                }<br \/>\n                void CancelInactiveReleaseInLoop() {<br \/>\n                }<br \/>\n                void UpgradeInLoop(const Any &amp;context,<br \/>\n                        const ConnectedCallback &amp;conn,<br \/>\n                        const MessageCallback &amp;msg,<br \/>\n                        const ClosedCallback &amp;closed,<br \/>\n                        const AnyEventCallback &amp;event) {<br \/>\n                        _context &#061; context;<br \/>\n                        _connected_callback &#061; conn;<br \/>\n                        _message_callback &#061; msg;<br \/>\n                        _closed_callback &#061; closed;<br \/>\n                        _event_callback &#061; event;<br \/>\n                }<br \/>\n        public:<br \/>\n                Connection(EventLoop* loop,uint64_t _conn_id,int sockfd) :  _sockfd(sockfd),<br \/>\n                        _enable_inactive_release(false), _loop(loop), _statu(CONNECTING), _socket(_sockfd), _channel(loop, _sockfd) {<br \/>\n                        _channel.SetCloseCallback(std::bind(&amp;Connection::HandleClose, this));<br \/>\n                        _channel.SetEventCallback(std::bind(&amp;Connection::HandleEvent, this));<br \/>\n                        _channel.SetReadCallback(std::bind(&amp;Connection::HandleRead, this));<br \/>\n                        _channel.SetWriteCallback(std::bind(&amp;Connection::HandleWrite, this));<br \/>\n                        _channel.SetErrorCallback(std::bind(&amp;Connection::HandleError, this));<br \/>\n                }<br \/>\n                 ~Connection() { DBG_LOG(&#034;RELEASE CONNECTION:%p&#034;, this); }<br \/>\n                  \/\/\u83b7\u53d6\u7ba1\u7406\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n                 int Fd() {return _sockfd; }<br \/>\n                 \/\/ \u83b7\u53d6\u8fde\u63a5ID<br \/>\n                 int Id() {return _conn_id; }<br \/>\n                 \/\/ \u662f\u5426\u5904\u4e8eCONNECTED\u72b6\u6001<br \/>\n                 bool Connected() { return (_statu &#061;&#061; CONNECTED); }<br \/>\n                 \/\/\u8bbe\u7f6e\u4e0a\u4e0b\u6587&#8211;\u8fde\u63a5\u5efa\u7acb\u5b8c\u6210\u65f6\u8fdb\u884c\u8c03\u7528<br \/>\n                void SetContext(const Any &amp;context) { _context &#061; context; }<br \/>\n                 \/\/\u83b7\u53d6\u4e0a\u4e0b\u6587&#xff0c;\u8fd4\u56de\u7684\u662f\u6307\u9488<br \/>\n                Any *GetContext() { return &amp;_context; }<br \/>\n                void SetConnectedCallback(const ConnectedCallback&amp;cb) { _connected_callback &#061; cb; }<br \/>\n                void SetMessageCallback(const MessageCallback&amp;cb) { _message_callback &#061; cb; }<br \/>\n                void SetClosedCallback(const ClosedCallback&amp;cb) { _closed_callback &#061; cb; }<br \/>\n                void SetAnyEventCallback(const AnyEventCallback&amp;cb) { _event_callback &#061; cb; }<br \/>\n                void SetSrvClosedCallback(const ClosedCallback&amp;cb) { _server_closed_callback &#061; cb; }<br \/>\n                \/\/\u8fde\u63a5\u5efa\u7acb\u5c31\u7eea\u540e&#xff0c;\u8fdb\u884cchannel\u56de\u8c03\u8bbe\u7f6e&#xff0c;\u542f\u52a8\u8bfb\u76d1\u63a7&#xff0c;\u8c03\u7528_connected_callback<br \/>\n                void Established() {}<br \/>\n                _loop-&gt;RunInLoop(std::bind(&amp;Connection::EstablishedInLoop, this));<br \/>\n                \/\/ \u53d1\u9001\u6570\u636e&#xff0c;\u5c06\u6570\u636e\u653e\u5230\u53d1\u9001\u7f13\u51b2\u533a&#xff0c;\u542f\u52a8\u5199\u4e8b\u4ef6\u76d1\u63a7<br \/>\n                void Send(const char *data, size_t len) {}<br \/>\n                    \/\/\u63d0\u4f9b\u7ed9\u7ec4\u4ef6\u4f7f\u7528\u8005\u7684\u5173\u95ed\u63a5\u53e3&#8211;\u5e76\u4e0d\u5b9e\u9645\u5173\u95ed&#xff0c;\u9700\u8981\u5224\u65ad\u6709\u6ca1\u6709\u6570\u636e\u5f85\u5904\u7406<br \/>\n                void Shutdown() {}<br \/>\n                void Release() {}<br \/>\n                \/\/\u542f\u52a8\u975e\u6d3b\u8dc3\u9500\u6bc1&#xff0c;\u5e76\u5b9a\u4e49\u591a\u957f\u65f6\u95f4\u65e0\u901a\u4fe1\u5c31\u662f\u975e\u6d3b\u8dc3&#xff0c;\u6dfb\u52a0\u5b9a\u65f6\u4efb\u52a1<br \/>\n                void EnableInactiveRelease(int sec) { }<br \/>\n                 \/\/\u53d6\u6d88\u975e\u6d3b\u8dc3\u9500\u6bc1<br \/>\n                void CancelInactiveRelease() {}<br \/>\n                void Upgrade(const Any &amp;context, const ConnectedCallback &amp;conn, const MessageCallback &amp;msg,<br \/>\n                        const ClosedCallback &amp;closed, const AnyEventCallback &amp;event) {<br \/>\n                        _loop-&gt;AssertInLoop();<br \/>\n                        _loop-&gt;RunInLoop(std::bind(&amp;Connection::UpgradeInLoop, this, context, conn, msg, closed, event));<br \/>\n        }<\/p>\n<p> };<\/p>\n<p>7.Acception\u6a21\u5757<\/p>\n<p>\u5bf9\u901a\u4fe1\u8fde\u63a5\u505a\u6574\u4f53\u7ba1\u7406\u7684\u4e00\u4e2a\u6a21\u5757&#xff0c;\u5bf9\u4e00\u4e2a\u901a\u4fe1\u8fde\u63a5\u7684\u6a21\u5757\u90fd\u662f\u901a\u8fc7\u8fd9\u4e2a\u6a21\u5757\u6765\u8fdb\u884c\u7684\u3002\u5b9e\u73b0\u4e86\u5bf9\u5957\u63a5\u5b57\u6574\u4f53\u7684\u7ba1\u7406\u3002<\/p>\n<p>\u610f\u4e49&#xff1a;\u5f53\u83b7\u53d6\u4e86\u4e00\u4e2a\u65b0\u5efa\u8fde\u63a5\u7684\u63cf\u8ff0\u7b26\u540e&#xff0c;\u9700\u8981\u4e3a\u8fd9\u4e2a\u901a\u4fe1\u8fde\u63a5&#xff0c;\u5c01\u88c5\u4e00\u4e2aconnection\u5bf9\u8c61&#xff0c;\u8bbe\u7f6e\u4e0d\u540c\u56de\u8c03\u3002 \u6ce8\u610f&#xff1a;\u56e0\u4e3aAcceptor\u6a21\u5757\u672c\u8eab\u5e76\u4e0d\u77e5\u9053\u4e00\u4e2a\u94fe\u63a5\u4ea7\u751f\u4e86\u67d0\u4e2a\u4e8b\u4ef6\u8be5\u5982\u4f55\u5904\u7406&#xff0c;\u56e0\u6b64\u83b7\u53d6\u4e00\u4e2a\u901a\u4fe1\u8fde\u63a5\u540e&#xff0c;Connection\u7684\u5c01\u88c5&#xff0c;\u4ee5\u53ca\u4e8b\u4ef6\u56de\u8c03\u7684\u8bbe\u7f6e\u90fd\u5e94\u8be5\u7531\u670d\u52a1\u5668\u6a21\u5757\u6765\u8fdb\u884c&#xff01;<\/p>\n<p>\u8bbe\u8ba1\u6846\u67b6&#xff1a;<\/p>\n<p>\/*<br \/>\nAcceptor \u6a21\u5757&#xff0c;\u5bf9\u76d1\u542c\u5957\u63a5\u5b57\u8fdb\u884c\u7ba1\u7406&#xff01;<br \/>\n        1. \u521b\u5efa\u4e00\u4e2a\u76d1\u542c\u5957\u63a5\u5b57<br \/>\n        2. \u542f\u52a8\u8bfb\u4e8b\u4ef6\u76d1\u63a7<br \/>\n        3. \u4e8b\u4ef6\u89e6\u53d1\u540e&#xff0c;\u83b7\u53d6\u65b0\u8fde\u63a5<br \/>\n        4. \u8c03\u7528\u65b0\u8fde\u63a5\u83b7\u53d6\u6210\u529f\u540e\u7684\u56de\u8c03\u51fd\u6570&#xff01;<br \/>\n                4. \u4e3a\u65b0\u8fde\u63a5\u521b\u5efaConnection\u8fdb\u884c\u7ba1\u7406&#xff08;\u8fd9\u4e00\u6b65\u4e0d\u662fAcceptor\u6a21\u5757\u64cd\u4f5c&#xff0c;\u5e94\u8be5\u662f\u670d\u52a1\u5668\u6a21\u5757&#xff09;<br \/>\n        \u56e0\u4e3aAcceptor\u6a21\u5757\u53ea\u8fdb\u884c\u76d1\u542c\u8fde\u63a5\u7684\u7ba1\u7406&#xff0c;\u56e0\u6b64\u83b7\u53d6\u5230\u65b0\u8fde\u63a5\u7684\u63cf\u8ff0\u7b26\u540e&#xff0c;\u5bf9\u4e8e\u65b0\u8fde\u63a5\u63cf\u8ff0\u7b26\u5982\u4f55\u5904\u7406\u5e76\u4e0d\u5173\u5fc3!<br \/>\n        \u5bf9\u4e8e\u65b0\u8fde\u63a5\u5982\u4f55\u5904\u7406&#xff0c;\u5e94\u8be5\u662f\u670d\u52a1\u5668\u6a21\u5757\u5173\u5fc3\u7ba1\u7406&#xff01;<br \/>\n        \u670d\u52a1\u5668\u6a21\u5757&#xff0c;\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5bf9\u4e8e\u65b0\u8fde\u63a5\u63cf\u8ff0\u7b26\u5904\u7406\u7684\u51fd\u6570&#xff0c;\u5c06\u8fd9\u4e2a\u51fd\u6570\u8bbe\u7f6e\u7ed9Acceptor\u6a21\u5757\u7684\u56de\u8c03\u51fd\u6570&#xff01;<br \/>\n*\/<\/p>\n<p>8.LoopThread\u6a21\u5757<\/p>\n<p>\u76ee\u6807&#xff1a;\u5c06eventloop\u6a21\u5757\u548c\u7ebf\u7a0b\u6574\u5408\u8d77\u6765&#xff01; eventloop \u548c \u7ebf\u7a0b\u662f\u4e00\u4e00\u5bf9\u5e94\u7684&#xff01; eventloop \u6a21\u5757\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61&#xff0c;\u5728\u6784\u9020\u7684\u65f6\u5019\u5c31\u4f1a\u521d\u59cb\u5316&#xff01; _thread_id; \u800c\u540e\u9762\u5f53\u8fd0\u884c\u4e00\u4e2a\u64cd\u4f5c\u7684\u65f6\u5019\u5224\u65ad\u662f\u5426\u8fd0\u884c\u5728eventloop\u6240\u5bf9\u5e94\u7684\u7ebf\u7a0b\u4e2d&#xff0c;\u5c31\u662f\u5c06\u7ebf\u7a0bID\u4e0eEventLoop\u6a21\u5757\u4e2d\u7684thread_id \u8fdb\u884c\u4e00\u4e2a\u6bd4\u8f83&#xff0c;\u76f8\u540c\u5c31\u8868\u793a\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u4e0d\u540c\u5c31\u8868\u793a\u5f53\u524d\u8fd0\u884c\u7ebf\u7a0b\u5e76\u4e0d\u662feventloop\u7ebf\u7a0b&#xff01; eventloop \u6a21\u5757\u5728\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u65f6\u5019&#xff0c;\u5fc5\u987b\u5728\u7ebf\u7a0b\u5185\u90e8&#xff01; eventloop \u5b9e\u4f8b\u5316\u5bf9\u8c61\u4f1a\u8bbe\u7f6e\u81ea\u5df1\u7684 thread_id; \u5982\u679c\u6211\u4eec\u5148\u521b\u5efa\u4e86\u591a\u4e2a eventloop \u5bf9\u8c61&#xff0c;\u7136\u540e\u521b\u5efa\u4e86\u591a\u4e2a\u7ebf\u7a0b&#xff0c;\u5c06\u5404\u81ea\u7684\u7ebf\u7a0bid&#xff0c;\u91cd\u65b0\u7ed9 eventloop \u8fdb\u884c\u8bbe\u7f6e&#xff01; \u5b58\u5728\u95ee\u9898&#xff1a;\u5728\u6784\u9020 eventloop\u5bf9\u8c61&#xff0c;\u5230\u8bbe\u7f6e\u65b0\u7684 thread_id \u671f\u95f4\u5c06\u662f\u4e0d\u53ef\u63a7\u7684&#xff01; \u56e0\u6b64&#xff0c;\u5fc5\u987b\u5148\u521b\u5efa\u7ebf\u7a0b&#xff0c;\u7136\u540e\u5728\u7ebf\u7a0b\u7684\u5165\u53e3\u51fd\u6570\u4e2d&#xff0c;\u53bb\u5b9e\u4f8b\u5316 eventloop \u5bf9\u8c61&#xff01; \u6784\u9020\u4e00\u4e2a\u65b0\u7684\u6a21\u5757&#xff1a;LoopThread\u3002<\/p>\n<p>class LoopThread {<br \/>\n    private:<br \/>\n        \/*\u7528\u4e8e\u5b9e\u73b0_loop\u83b7\u53d6\u7684\u540c\u6b65\u5173\u7cfb&#xff0c;\u907f\u514d\u7ebf\u7a0b\u521b\u5efa\u4e86&#xff0c;\u4f46\u662f_loop\u8fd8\u6ca1\u6709\u5b9e\u4f8b\u5316\u4e4b\u524d\u53bb\u83b7\u53d6_loop*\/<br \/>\n        std::mutex _mutex;          \/\/ \u4e92\u65a5\u9501<br \/>\n        std::condition_variable _cond;   \/\/ \u6761\u4ef6\u53d8\u91cf<br \/>\n        EventLoop *_loop;       \/\/ EventLoop\u6307\u9488\u53d8\u91cf&#xff0c;\u8fd9\u4e2a\u5bf9\u8c61\u9700\u8981\u5728\u7ebf\u7a0b\u5185\u5b9e\u4f8b\u5316<br \/>\n        std::thread _thread;    \/\/ EventLoop\u5bf9\u5e94\u7684\u7ebf\u7a0b<br \/>\n    private:<br \/>\n        \/*\u5b9e\u4f8b\u5316 EventLoop \u5bf9\u8c61&#xff0c;\u5524\u9192_cond\u4e0a\u6709\u53ef\u80fd\u963b\u585e\u7684\u7ebf\u7a0b&#xff0c;\u5e76\u4e14\u5f00\u59cb\u8fd0\u884cEventLoop\u6a21\u5757\u7684\u529f\u80fd*\/<br \/>\n        void ThreadEntry() {<br \/>\n            EventLoop loop;<br \/>\n            {<br \/>\n                std::unique_lock&lt;std::mutex&gt; lock(_mutex);\/\/\u52a0\u9501<br \/>\n                _loop &#061; &amp;loop;<br \/>\n                _cond.notify_all();<br \/>\n            }<br \/>\n            loop.Start();<br \/>\n        }<br \/>\n    public:<br \/>\n        \/*\u521b\u5efa\u7ebf\u7a0b&#xff0c;\u8bbe\u5b9a\u7ebf\u7a0b\u5165\u53e3\u51fd\u6570*\/<br \/>\n        LoopThread():_loop(NULL), _thread(std::thread(&amp;LoopThread::ThreadEntry, this)) {}<br \/>\n        \/*\u8fd4\u56de\u5f53\u524d\u7ebf\u7a0b\u5173\u8054\u7684EventLoop\u5bf9\u8c61\u6307\u9488*\/<br \/>\n        EventLoop *GetLoop() {<br \/>\n            EventLoop *loop &#061; NULL;<br \/>\n            {<br \/>\n                std::unique_lock&lt;std::mutex&gt; lock(_mutex);\/\/\u52a0\u9501<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        }<br \/>\n};<\/p>\n<p>9.LoopThreadPool\u6a21\u5757<\/p>\n<p>\u7ebf\u7a0b\u6570\u91cf\u53ef\u914d\u7f6e&#xff0c;\u8c03\u8282\u7ebf\u7a0b\u6570\u91cf&#xff0c;\u5bf9\u7ebf\u7a0b\u6570\u91cf\u8fdb\u884c\u7ba1\u7406\u3002\u63d0\u4f9b\u7ebf\u7a0b\u5206\u914d\u7684\u529f\u80fd\u3002<\/p>\n<p>1.\u7ebf\u7a0b\u6570\u91cf\u53ef\u914d\u7f6e&#xff08;0\u6216\u591a\u4e2a&#xff09; \u6ce8\u610f\u4e8b\u9879&#xff1a;\u5728\u670d\u52a1\u5668\u4e2d&#xff0c;\u4e3b\u4eceReactor\u6a21\u578b\u662f \u4e3b\u7ebf\u7a0b\u53ea\u8d1f\u8d23\u65b0\u8fde\u63a5\u83b7\u53d6&#xff0c;\u4e1b\u4e66\u7ebf\u7a0b\u8d1f\u8d23\u65b0\u8fde\u63a5\u7684\u4e8b\u4ef6\u76d1\u63a7\u4ee5\u53ca\u5904\u7406!\u56e0\u6b64\u5f53\u524d\u7684\u7ebf\u7a0b\u6c60&#xff0c;\u6709\u53ef\u80fd\u4ece\u5c5e\u7ebf\u7a0b\u4f1a\u6570\u91cf\u4e3a0&#xff0c;\u4e5f\u5c31\u662f\u5b9e\u73b0\u5355 Reactor\u670d\u52a1\u5668&#xff0c;\u4e00\u4e2a\u7ebf\u7a0b\u53ca\u8d1f\u8d23\u83b7\u53d6\u8fde\u63a5\u4ee5\u53ca\u8fde\u63a5\u7684\u5904\u7406&#xff01; 2. \u5bf9\u6240\u6709\u7684\u7ebf\u7a0b\u8fdb\u884c\u7ba1\u7406&#xff0c;\u5176\u5b9e\u4e5f\u5c31\u662f\u7ba1\u74060\u4e2a\u6216\u591a\u4e2aLoopThread\u5bf9\u8c61&#xff01; 3. \u63d0\u4f9b\u7ebf\u7a0b\u5206\u914d\u7684\u529f\u80fd&#xff01; 4.\u5f53\u4e3b\u7ebf\u7a0b\u83b7\u53d6\u4e86\u4e00\u4e2a\u94fe\u63a5&#xff0c;\u9700\u8981\u5c06\u65b0\u7684\u7ebf\u7a0b\u6302\u5230\u4ece\u5c5e\u7ebf\u7a0b\u4e0a\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u4ee5\u53ca\u7ba1\u7406&#xff01; 5.\u5047\u8bbe0\u4e2a\u4ece\u5c5e\u7ebf\u7a0b&#xff0c;\u5219\u76f4\u63a5\u5206\u914d\u7ed9\u4e3b\u7ebf\u7a0b\u7684EventLoop&#xff0c;\u8fdb\u884c\u5904\u7406&#xff01; 6.\u5047\u8bbe\u6709\u591a\u4e2a\u4e1b\u4e66\u7ebf\u7a0b&#xff0c;\u5219\u91c7\u7528RR\u8f6e\u8f6c&#xff01;&#xff08;\u5c06\u5bf9\u5e94\u7ebf\u7a0b\u7684EventLoop\u83b7\u53d6\u5230&#xff0c;\u8bbe\u7f6e\u7ed9\u5bf9\u5e94\u7684Connection)\u3002<\/p>\n<p>\u8bbe\u8ba1\u6846\u67b6&#xff1a;<\/p>\n<p>class LoopThreadPool {<br \/>\n    private:<br \/>\n        int _thread_count;<br \/>\n        int _next_idx;<br \/>\n        EventLoop *_baseloop;<br \/>\n        std::vector&lt;LoopThread*&gt; _threads;<br \/>\n        std::vector&lt;EventLoop *&gt; _loops;<br \/>\n    public:<br \/>\n        LoopThreadPool(EventLoop *baseloop):_thread_count(0), _next_idx(0), _baseloop(baseloop) {}<br \/>\n        void SetThreadCount(int count) { _thread_count &#061; count; }<br \/>\n        void Create() {<br \/>\n            if (_thread_count &gt; 0) {<br \/>\n                _threads.resize(_thread_count);<br \/>\n                _loops.resize(_thread_count);<br \/>\n                for (int i &#061; 0; i &lt; _thread_count; i&#043;&#043;) {<br \/>\n                    _threads[i] &#061; new LoopThread();<br \/>\n                    _loops[i] &#061; _threads[i]-&gt;GetLoop();<br \/>\n                }<br \/>\n            }<br \/>\n            return ;<br \/>\n        }<br \/>\n        EventLoop *NextLoop() {<br \/>\n            if (_thread_count &#061;&#061; 0) {<br \/>\n                return _baseloop;<br \/>\n            }<br \/>\n            _next_idx &#061; (_next_idx &#043; 1) % _thread_count;<br \/>\n            return _loops[_next_idx];<br \/>\n        }<br \/>\n};<\/p>\n<p>10.TcpServer\u6a21\u5757<\/p>\n<p>\u5bf9\u4e4b\u524d\u6240\u6709\u7684\u6a21\u5757\u8fdb\u884c\u6574\u5408&#xff0c;\u5b8c\u6210\u4e00\u4e2a\u670d\u52a1\u5668\u7684\u642d\u5efa\u3002<\/p>\n<p>\u5b9e\u73b0\u601d\u60f3&#xff1a;<\/p>\n<p>1.\u7ba1\u7406<\/p>\n<li>Acceptor\u5bf9\u8c61&#xff0c;\u521b\u5efa\u4e00\u4e2a\u76d1\u542c\u5957\u63a5\u5b57&#xff01;<\/li>\n<li>EventLoop \u5bf9\u8c61&#xff0c;baseloop\u5bf9\u8c61&#xff0c;\u5b9e\u73b0\u5bf9\u76d1\u542c\u5957\u63a5\u5b57\u7684\u4e8b\u4ef6\u76d1\u63a7&#xff01;<\/li>\n<li>std::vector conns,\u5b9e\u73b0\u5bf9\u65b0\u5efa\u8fde\u63a5\u7684\u7ba1\u7406&#xff01;<\/li>\n<li>EventLoopPool \u5bf9\u8c61&#xff0c;\u521b\u5efaloop\u7ebf\u7a0b\u6c60&#xff0c;\u5bf9\u65b0\u5efa\u8fde\u63a5\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u548c\u5904\u7406&#xff01;<\/li>\n<p>2.\u6d41\u7a0b<\/p>\n<li>\u5728TcpServer\u4e2d\u5b9e\u4f8b\u4e00\u4e2aAcceptor\u5bf9\u8c61&#xff0c;\u4ee5\u53ca\u4e00\u4e2aEventLoop \u5bf9\u8c61&#xff08;baseloop)<\/li>\n<li>\u5c06Acceptor \u6302\u5728baseloop \u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7<\/li>\n<li>\u4e00\u65e6Acceptor \u5bf9\u8c61\u5c31\u7eea\u4e86\u53ef\u8bfb\u4e8b\u4ef6&#xff0c;\u5219\u6267\u884c\u65f6\u95f4\u56de\u8c03\u51fd\u6570\u83b7\u53d6\u65b0\u5efa\u8fde\u63a5&#xff01;<\/li>\n<li>\u5bf9\u65b0\u8fde\u63a5&#xff0c;\u521b\u9020\u4e00\u4e2a Connection \u8fdb\u884c\u7ba1\u7406&#xff01;<\/li>\n<li>\u5bf9\u65b0\u8fde\u63a5\u5bf9\u5e94\u7684 Connection \u8bbe\u7f6e\u529f\u80fd\u56de\u8c03 &#xff08;\u8fde\u63a5\u5b8c\u6210\u56de\u8c03&#xff0c;\u6d88\u606f\u56de\u8c03&#xff0c;\u5173\u95ed\u56de\u8c03&#xff0c;\u4efb\u610f\u4e8b\u4ef6\u76d1\u63a7&#xff01;&#xff09;<\/li>\n<li>\u542f\u52a8Connettion \u7684\u975e\u6d3b\u8dc3\u94fe\u63a5\u7684\u8d85\u65f6\u9500\u6bc1\u529f\u80fd.<\/li>\n<li>\u5c06\u65b0\u8fde\u63a5\u5bf9\u5e94\u7684Connection \u6302\u5230 LoopThreadPool \u4e2d\u7684\u4e1b\u4e66\u7ebf\u7a0b\u5bf9\u5e94\u7684Eventloop \u4e2d\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7&#xff01;<\/li>\n<li>\u4e00\u65e6Connection\u5bf9\u5e94\u7684\u94fe\u63a5\u5c31\u7eea\u4e86\u53ef\u8bfb\u4e8b\u4ef6&#xff0c;\u5219\u8fd9\u4e2a\u65f6\u5019\u6267\u884c\u8bfb\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570&#xff0c;\u8bfb\u53d6\u6570\u636e&#xff0c;\u8bfb\u53d6\u5b8c\u6bd5\u540e\u8c03\u7528TcpServer\u8bbe\u7f6e\u7684\u6d88\u606f\u56de\u8c03&#xff01;<\/li>\n<p>&#xff08;\u4e09&#xff09;\u529f\u80fd\u8bbe\u8ba1<\/p>\n<li>\u8bbe\u7f6e\u4ece\u5c5e\u7ebf\u7a0b\u6c60\u6570\u91cf&#xff01;<\/li>\n<li>\u542f\u52a8\u670d\u52a1\u5668<\/li>\n<li>\u8bbe\u7f6e\u5404\u79cd\u56de\u8c03\u51fd\u6570&#xff01;&#xff08;\u8fde\u63a5\u5efa\u7acb\u5b8c\u6210&#xff0c;\u6d88\u606f&#xff0c;\u5173\u95ed&#xff0c;\u4efb\u610f&#xff09; \u7528\u6237\u8bbe\u7f6e\u7ed9TcpServer TcpServer\u8bbe\u7f6e\u83b7\u53d6\u7684\u65b0\u8fde\u63a5&#xff01;<\/li>\n<li>\u662f\u5426\u542f\u52a8\u975e\u6d3b\u8dc3\u8fde\u63a5\u8d85\u65f6\u9500\u6bc1\u529f\u80fd<\/li>\n<li>\u6dfb\u52a0\u4efb\u52a1&#xff01;<\/li>\n<p>\u8bbe\u8ba1\u6846\u67b6&#xff1a;<\/p>\n<p>class TcpServer {<br \/>\n    private:<br \/>\n        uint64_t _next_id;      \/\/\u8fd9\u662f\u4e00\u4e2a\u81ea\u52a8\u589e\u957f\u7684\u8fde\u63a5ID&#xff0c;<br \/>\n        int _port;<br \/>\n        int _timeout;           \/\/\u8fd9\u662f\u975e\u6d3b\u8dc3\u8fde\u63a5\u7684\u7edf\u8ba1\u65f6\u95f4&#8212;\u591a\u957f\u65f6\u95f4\u65e0\u901a\u4fe1\u5c31\u662f\u975e\u6d3b\u8dc3\u8fde\u63a5<br \/>\n        bool _enable_inactive_release;\/\/\u662f\u5426\u542f\u52a8\u4e86\u975e\u6d3b\u8dc3\u8fde\u63a5\u8d85\u65f6\u9500\u6bc1\u7684\u5224\u65ad\u6807\u5fd7<br \/>\n        EventLoop _baseloop;    \/\/\u8fd9\u662f\u4e3b\u7ebf\u7a0b\u7684EventLoop\u5bf9\u8c61&#xff0c;\u8d1f\u8d23\u76d1\u542c\u4e8b\u4ef6\u7684\u5904\u7406<br \/>\n        Acceptor _acceptor;    \/\/\u8fd9\u662f\u76d1\u542c\u5957\u63a5\u5b57\u7684\u7ba1\u7406\u5bf9\u8c61<br \/>\n        LoopThreadPool _pool;   \/\/\u8fd9\u662f\u4ece\u5c5eEventLoop\u7ebf\u7a0b\u6c60<br \/>\n        std::unordered_map&lt;uint64_t, PtrConnection&gt; _conns;\/\/\u4fdd\u5b58\u7ba1\u7406\u6240\u6709\u8fde\u63a5\u5bf9\u5e94\u7684shared_ptr\u5bf9\u8c61<\/p>\n<p>        using ConnectedCallback &#061; std::function&lt;void(const PtrConnection&amp;)&gt;;<br \/>\n        using MessageCallback &#061; std::function&lt;void(const PtrConnection&amp;, Buffer *)&gt;;<br \/>\n        using ClosedCallback &#061; std::function&lt;void(const PtrConnection&amp;)&gt;;<br \/>\n        using AnyEventCallback &#061; std::function&lt;void(const PtrConnection&amp;)&gt;;<br \/>\n        using Functor &#061; std::function&lt;void()&gt;;<br \/>\n        ConnectedCallback _connected_callback;<br \/>\n        MessageCallback _message_callback;<br \/>\n        ClosedCallback _closed_callback;<br \/>\n        AnyEventCallback _event_callback;<br \/>\n    private:<br \/>\n        void RunAfterInLoop(const Functor &amp;task, int delay) {<\/p>\n<p>        }<br \/>\n        \/\/\u4e3a\u65b0\u8fde\u63a5\u6784\u9020\u4e00\u4e2aConnection\u8fdb\u884c\u7ba1\u7406<br \/>\n        void NewConnection(int fd) {<\/p>\n<p>        }<br \/>\n        void RemoveConnectionInLoop(const PtrConnection &amp;conn) {<\/p>\n<p>        }<br \/>\n        \/\/\u4ece\u7ba1\u7406Connection\u7684_conns\u4e2d\u79fb\u9664\u8fde\u63a5\u4fe1\u606f<br \/>\n        void RemoveConnection(const PtrConnection &amp;conn) {<\/p>\n<p>        }<br \/>\n    public:<br \/>\n        TcpServer(int port):<br \/>\n            _port(port),<br \/>\n            _next_id(0),<br \/>\n            _enable_inactive_release(false),<br \/>\n            _acceptor(&amp;_baseloop, port),<br \/>\n            _pool(&amp;_baseloop) {<br \/>\n            _acceptor.SetAcceptCallback(std::bind(&amp;TcpServer::NewConnection, this, std::placeholders::_1));<\/p>\n<p>        }<br \/>\n        void SetThreadCount(int count) { <\/p>\n<p>        }<br \/>\n        void SetConnectedCallback(const ConnectedCallback&amp;cb) { <\/p>\n<p>        }<br \/>\n        void SetMessageCallback(const MessageCallback&amp;cb) { <\/p>\n<p>        }<br \/>\n        void SetClosedCallback(const ClosedCallback&amp;cb) {<\/p>\n<p>        }<br \/>\n        void SetAnyEventCallback(const AnyEventCallback&amp;cb) { <\/p>\n<p>        }<br \/>\n        void EnableInactiveRelease(int timeout) { <\/p>\n<p>        }<br \/>\n        \/\/\u7528\u4e8e\u6dfb\u52a0\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1<br \/>\n        void RunAfter(const Functor &amp;task, int delay) {<\/p>\n<p>        }<br \/>\n        void Start() { <\/p>\n<p>        }<br \/>\n};<\/p>\n<p>2.2 Http\u6a21\u5757\u5b9e\u73b0<\/p>\n<p>Http\u6a21\u5757\u662f\u5904\u4e8e\u5e94\u7528\u5c42\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u534f\u8bae&#xff0c;\u5305\u542b\u4e94\u90e8\u5206&#xff0c;\u6bcf\u90e8\u5206\u6709\u4e0d\u540c\u7684\u5185\u5bb9\u3002<\/p>\n<p>1.Util\u7684\u5904\u7406<\/p>\n<p>\u76ee\u7684&#xff1a;\u76ee\u7684:\u5b9e\u73b0\u4e00\u4e9b\u5de5\u5177\u63a5\u53e3&#xff0c;\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9&#xff0c;\u5411\u6587\u4ef6\u5199\u5165\u5185\u5bb9&#xff0c;URL\u7f16\u7801&#xff0c;URL\u89e3\u7801&#xff0c;\u901a\u8fc7HTTP\u72b6\u6001\u7801\u83b7\u53d6\u63cf\u8ff0\u4fe1\u606f&#xff0c;\u901a\u8fc7\u6587\u4ef6\u540e\u7f00\u540d\u83b7\u53d6mime&#xff0c;\u5224\u65ad\u4e00\u4e2a\u6587\u4ef6\u662f\u4e0d\u662f\u76ee\u5f55&#xff0c;\u5224\u65ad\u4e00\u4e2a\u6587&#xff0c;\u662f\u5426\u662f\u4e00\u4e2a\u666e\u901a\u6587\u4ef6&#xff0c;HTTP\u8d44\u6e90\u8def\u5f84\u6709\u6548\u6027\u5224\u65ad<\/p>\n<p>\u6846\u67b6\u8bbe\u8ba1&#xff1a;<\/p>\n<p>class Util {<br \/>\n    public:<br \/>\n        \/\/ \u5b57\u7b26\u4e32\u5206\u5272\u51fd\u6570<br \/>\n        size_t Spilt();<br \/>\n        \/\/ \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<br \/>\n        static bool ReadFile() {<\/p>\n<p>        }<br \/>\n        \/\/ \u5411\u6587\u4ef6\u5199\u5165\u5185\u5bb9<br \/>\n        static bool  WriteFile();<br \/>\n        \/\/ URL\u7f16\u7801<br \/>\n        static bool UrlEncode();<br \/>\n        \/\/ URL\u89e3\u7801<br \/>\n        static bool UrlDecode();<br \/>\n        \/\/ \u901a\u8fc7HTTP\u72b6\u6001\u7801\u83b7\u53d6\u63cf\u8ff0\u4fe1\u606f<br \/>\n        static std::string StatusDesc();<br \/>\n        \/\/ \u6839\u636e\u6587\u4ef6\u540e\u7f00\u540d\u83b7\u53d6\u6587\u4ef6MINE<br \/>\n        static std::string ExtMine();<br \/>\n        \/\/ \u5224\u65ad\u4e00\u4e2a\u6587\u4ef6\u662f\u4e0d\u662f\u76ee\u5f55<br \/>\n        static bool IsDirectory();<br \/>\n        \/\/\u5224\u65ad\u4e00\u4e2a\u6587\u4ef6\u662f\u5426\u662f\u4e00\u4e2a\u666e\u901a\u6587\u4ef6<br \/>\n        static bool IsRegular();<br \/>\n        \/\/HTTP\u8d44\u6e90\u8def\u5f84\u6709\u6548\u6027\u5224\u65ad<br \/>\n        static bool VaildPath();<br \/>\n};<\/p>\n<p>2.HttpRequest<\/p>\n<p>\u76ee\u7684&#xff1a;\u5b58\u50a8Http\u8bf7\u6c42\u4fe1\u606f\u7684&#xff0c;\u63a5\u6536\u5230\u4e00\u4e2a\u6570\u636e&#xff0c;\u6309\u7167HTTP\u8bf7\u6c42\u683c\u5f0f\u8fdb\u884c\u89e3\u6790&#xff0c;\u5f97\u5230\u5404\u4e2a\u5173\u952e\u8981\u7d20\u653e\u5230Request\u4e2d\u3002<\/p>\n<p>\u529f\u80fd&#xff1a;<\/p>\n<ul>\n<li>HttpRequest\u6a21\u5757<\/li>\n<li>\u5b58\u50a8HTTP\u8bf7\u6c42\u4fe1\u606f<\/li>\n<li>\u63a5\u6536\u5230\u4e00\u4e2a\u6570\u636e&#xff0c;\u6309\u7167HTTP\u8bf7\u6c42\u683c\u5f0f\u8fdb\u884c\u89e3\u6790&#xff0c;\u5f97\u5230\u5404\u4e2a\u5173\u952e\u8981\u7d20\u653e\u5230Request\u4e2d<\/li>\n<li>HttpResponse\u6a21\u5757<\/li>\n<li>\u5b58\u50a8HTTP\u54cd\u5e94\u4fe1\u606f<\/li>\n<li>\u8fdb\u884c\u4e1a\u52a1\u5904\u7406\u7684\u540c\u65f6&#xff0c;\u8ba9\u4f7f\u7528\u8005\u5411Response\u4e2d\u586b\u5145\u54cd\u5e94\u8981\u7d20&#xff0c;\u5b8c\u6bd5\u540e&#xff0c;\u5c06\u5176\u7ec4\u7ec7\u6210HTTP\u54cd\u5e94\u683c\u5f0f\u7684\u6570\u636e&#xff0c;\u53d1\u7ed9\u5ba2\u6237\u7aef\u3002<\/li>\n<\/ul>\n<p>class HttpRequest {<br \/>\n    public:<br \/>\n        std::string _method;      \/\/\u8bf7\u6c42\u65b9\u6cd5<br \/>\n        std::string _path;        \/\/\u8d44\u6e90\u8def\u5f84<br \/>\n        std::string _version;     \/\/\u534f\u8bae\u7248\u672c<br \/>\n        std::string _body;        \/\/\u8bf7\u6c42\u6b63\u6587<br \/>\n        std::smatch _matches;     \/\/\u8d44\u6e90\u8def\u5f84\u7684\u6b63\u5219\u63d0\u53d6\u6570\u636e<br \/>\n        std::unordered_map&lt;std::string, std::string&gt; _headers;  \/\/\u5934\u90e8\u5b57\u6bb5<br \/>\n        std::unordered_map&lt;std::string, std::string&gt; _params;   \/\/\u67e5\u8be2\u5b57\u7b26\u4e32<br \/>\n    public:<br \/>\n        HttpRequest():_version(&#034;HTTP\/1.1&#034;) {}<br \/>\n        void ReSet() {<br \/>\n            _method.clear();<br \/>\n            _path.clear();<br \/>\n            _version &#061; &#034;HTTP\/1.1&#034;;<br \/>\n            _body.clear();<br \/>\n            std::smatch match;<br \/>\n            _matches.swap(match);<br \/>\n            _headers.clear();<br \/>\n            _params.clear();<br \/>\n        }<br \/>\n        \/\/\u63d2\u5165\u5934\u90e8\u5b57\u6bb5<br \/>\n        void SetHeader(const std::string &amp;key, const std::string &amp;val) {<br \/>\n            _headers.insert(std::make_pair(key, val));<br \/>\n        }<br \/>\n        \/\/\u5224\u65ad\u662f\u5426\u5b58\u5728\u6307\u5b9a\u5934\u90e8\u5b57\u6bb5<br \/>\n        bool HasHeader(const std::string &amp;key) const {<br \/>\n            auto it &#061; _headers.find(key);<br \/>\n            if (it &#061;&#061; _headers.end()) {<br \/>\n                return false;<br \/>\n            }<br \/>\n            return true;<br \/>\n        }<br \/>\n        \/\/\u83b7\u53d6\u6307\u5b9a\u5934\u90e8\u5b57\u6bb5\u7684\u503c<br \/>\n        std::string GetHeader(const std::string &amp;key) const {<br \/>\n            auto it &#061; _headers.find(key);<br \/>\n            if (it &#061;&#061; _headers.end()) {<br \/>\n                return &#034;&#034;;<br \/>\n            }<br \/>\n            return it-&gt;second;<br \/>\n        }<br \/>\n        \/\/\u63d2\u5165\u67e5\u8be2\u5b57\u7b26\u4e32<br \/>\n        void SetParam(const std::string &amp;key, const std::string &amp;val) {<br \/>\n            _params.insert(std::make_pair(key, val));<br \/>\n        }<br \/>\n        \/\/\u5224\u65ad\u662f\u5426\u6709\u67d0\u4e2a\u6307\u5b9a\u7684\u67e5\u8be2\u5b57\u7b26\u4e32<br \/>\n        bool HasParam(const std::string &amp;key) const {<br \/>\n            auto it &#061; _params.find(key);<br \/>\n            if (it &#061;&#061; _params.end()) {<br \/>\n                return false;<br \/>\n            }<br \/>\n            return true;<br \/>\n        }<br \/>\n        \/\/\u83b7\u53d6\u6307\u5b9a\u7684\u67e5\u8be2\u5b57\u7b26\u4e32<br \/>\n        std::string GetParam(const std::string &amp;key) const {<br \/>\n            auto it &#061; _params.find(key);<br \/>\n            if (it &#061;&#061; _params.end()) {<br \/>\n                return &#034;&#034;;<br \/>\n            }<br \/>\n            return it-&gt;second;<br \/>\n        }<br \/>\n        \/\/\u83b7\u53d6\u6b63\u6587\u957f\u5ea6<br \/>\n        size_t ContentLength() const {<br \/>\n            \/\/ Content-Length: 1234\\\\r\\\\n<br \/>\n            bool ret &#061; HasHeader(&#034;Content-Length&#034;);<br \/>\n            if (ret &#061;&#061; false) {<br \/>\n                return 0;<br \/>\n            }<br \/>\n            std::string clen &#061; GetHeader(&#034;Content-Length&#034;);<br \/>\n            return std::stol(clen);<br \/>\n        }<br \/>\n        \/\/\u5224\u65ad\u662f\u5426\u662f\u77ed\u94fe\u63a5<br \/>\n        bool Close() const {<br \/>\n            \/\/ \u6ca1\u6709Connection\u5b57\u6bb5&#xff0c;\u6216\u8005\u6709Connection\u4f46\u662f\u503c\u662fclose&#xff0c;\u5219\u90fd\u662f\u77ed\u94fe\u63a5&#xff0c;\u5426\u5219\u5c31\u662f\u957f\u8fde\u63a5<br \/>\n            if (HasHeader(&#034;Connection&#034;) &#061;&#061; true &amp;&amp; GetHeader(&#034;Connection&#034;) &#061;&#061; &#034;keep-alive&#034;) {<br \/>\n                return false;<br \/>\n            }<br \/>\n            return true;<br \/>\n        }<br \/>\n};<\/p>\n<p>3.Http<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb737\u6b21\uff0c\u70b9\u8d5e7\u6b21\uff0c\u6536\u85cf28\u6b21\u3002\u603b\u7f13\u51b2\u533a\u5927\u5c0f\u662f\u4e00\u5b9a\u7684\uff0c\u5f00\u59cb\u7684\u65f6\u5019readbuff\u7b49\u4e8ewritebuff,\u4e3a0\uff0c\u5f53\u5199\u5165\u6570\u636e\u7684\u65f6\u5019\uff0c\u5199\u5927\u5c0f\u4e3a\u6570\u5267\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u7684\u540e\u4e00\u4e2a\u4f4d\u7f6e\u5f00\u59cb\uff0c\u5bf9\u4f9d\u7136\u4ece\u6570\u636e\u5f00\u5934\u5f00\u59cb\u8bfb\uff0c\u8bfb\u5b8c\u4e4b\u540e\uff0c\u6211\u4eec\u4e0d\u7528\u5220\u9664\uff0c\u5f53\u518d\u6b21\u5199\u5165\u7684\u65f6\u5019\uff0c\u5199\u5165\u5230\u4e0a\u4e00\u4e2a\u6570\u636e\u7684\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u8bfb\u7684\u5927\u5c0f\u7b49\u4e8e\u5199\u7684\u5927\u5c0f\u3002\u529f\u80fd\uff1a\u5bf9\u4e00\u4e2a\u63cf\u8ff0\u7b26\u8fdb\u884c\u76d1\u63a7\u65f6\u95f4\u7ba1\u7406\uff0c\u610f\u4e49\uff0c\u5bf9\u4e8e\u63cf\u8ff0\u7b26\u7684\u72b6\u6001\u66f4\u5bb9\u6613\u7ef4\u62a4\uff0c\u5bf9\u4e4b\u540e\u7684\u64cd\u4f5c\u6d41\u7a0b\u66f4\u52a0\u65b9\u4fbf\uff0c\u529f\u80fd\u8bbe\u8ba1\uff1a\u63cf\u8ff0\u7b26\u662f\u5426\u53ef\u8bfb\uff0c\u662f\u5426\u53ef\u5199\uff0c\u76d1\u63a7\u53ef\u8bfb\uff0c\u76d1\u63a7\u53ef\u5199\uff0c\u89e3\u9664\u53ef\u8bfb\uff0c\u89e3\u9664\u53ef\u5199\uff0c\u89e3\u9664\u6240\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n","protected":false},"author":2,"featured_media":44565,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[43,44],"topic":[],"class_list":["post-44567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4effmuduo\u5e93\u5b9e\u73b0\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\/44567.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb737\u6b21\uff0c\u70b9\u8d5e7\u6b21\uff0c\u6536\u85cf28\u6b21\u3002\u603b\u7f13\u51b2\u533a\u5927\u5c0f\u662f\u4e00\u5b9a\u7684\uff0c\u5f00\u59cb\u7684\u65f6\u5019readbuff\u7b49\u4e8ewritebuff,\u4e3a0\uff0c\u5f53\u5199\u5165\u6570\u636e\u7684\u65f6\u5019\uff0c\u5199\u5927\u5c0f\u4e3a\u6570\u5267\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u7684\u540e\u4e00\u4e2a\u4f4d\u7f6e\u5f00\u59cb\uff0c\u5bf9\u4f9d\u7136\u4ece\u6570\u636e\u5f00\u5934\u5f00\u59cb\u8bfb\uff0c\u8bfb\u5b8c\u4e4b\u540e\uff0c\u6211\u4eec\u4e0d\u7528\u5220\u9664\uff0c\u5f53\u518d\u6b21\u5199\u5165\u7684\u65f6\u5019\uff0c\u5199\u5165\u5230\u4e0a\u4e00\u4e2a\u6570\u636e\u7684\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u8bfb\u7684\u5927\u5c0f\u7b49\u4e8e\u5199\u7684\u5927\u5c0f\u3002\u529f\u80fd\uff1a\u5bf9\u4e00\u4e2a\u63cf\u8ff0\u7b26\u8fdb\u884c\u76d1\u63a7\u65f6\u95f4\u7ba1\u7406\uff0c\u610f\u4e49\uff0c\u5bf9\u4e8e\u63cf\u8ff0\u7b26\u7684\u72b6\u6001\u66f4\u5bb9\u6613\u7ef4\u62a4\uff0c\u5bf9\u4e4b\u540e\u7684\u64cd\u4f5c\u6d41\u7a0b\u66f4\u52a0\u65b9\u4fbf\uff0c\u529f\u80fd\u8bbe\u8ba1\uff1a\u63cf\u8ff0\u7b26\u662f\u5426\u53ef\u8bfb\uff0c\u662f\u5426\u53ef\u5199\uff0c\u76d1\u63a7\u53ef\u8bfb\uff0c\u76d1\u63a7\u53ef\u5199\uff0c\u89e3\u9664\u53ef\u8bfb\uff0c\u89e3\u9664\u53ef\u5199\uff0c\u89e3\u9664\u6240\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/44567.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-19T07:25:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619072541-6853bb758958f.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=\"17 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44567.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/44567.html\",\"name\":\"\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-19T07:25:44+00:00\",\"dateModified\":\"2025-06-19T07:25:44+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/44567.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/44567.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44567.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4effmuduo\u5e93\u5b9e\u73b0\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":"\u4effmuduo\u5e93\u5b9e\u73b0\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\/44567.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb737\u6b21\uff0c\u70b9\u8d5e7\u6b21\uff0c\u6536\u85cf28\u6b21\u3002\u603b\u7f13\u51b2\u533a\u5927\u5c0f\u662f\u4e00\u5b9a\u7684\uff0c\u5f00\u59cb\u7684\u65f6\u5019readbuff\u7b49\u4e8ewritebuff,\u4e3a0\uff0c\u5f53\u5199\u5165\u6570\u636e\u7684\u65f6\u5019\uff0c\u5199\u5927\u5c0f\u4e3a\u6570\u5267\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u7684\u540e\u4e00\u4e2a\u4f4d\u7f6e\u5f00\u59cb\uff0c\u5bf9\u4f9d\u7136\u4ece\u6570\u636e\u5f00\u5934\u5f00\u59cb\u8bfb\uff0c\u8bfb\u5b8c\u4e4b\u540e\uff0c\u6211\u4eec\u4e0d\u7528\u5220\u9664\uff0c\u5f53\u518d\u6b21\u5199\u5165\u7684\u65f6\u5019\uff0c\u5199\u5165\u5230\u4e0a\u4e00\u4e2a\u6570\u636e\u7684\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u8bfb\u7684\u5927\u5c0f\u7b49\u4e8e\u5199\u7684\u5927\u5c0f\u3002\u529f\u80fd\uff1a\u5bf9\u4e00\u4e2a\u63cf\u8ff0\u7b26\u8fdb\u884c\u76d1\u63a7\u65f6\u95f4\u7ba1\u7406\uff0c\u610f\u4e49\uff0c\u5bf9\u4e8e\u63cf\u8ff0\u7b26\u7684\u72b6\u6001\u66f4\u5bb9\u6613\u7ef4\u62a4\uff0c\u5bf9\u4e4b\u540e\u7684\u64cd\u4f5c\u6d41\u7a0b\u66f4\u52a0\u65b9\u4fbf\uff0c\u529f\u80fd\u8bbe\u8ba1\uff1a\u63cf\u8ff0\u7b26\u662f\u5426\u53ef\u8bfb\uff0c\u662f\u5426\u53ef\u5199\uff0c\u76d1\u63a7\u53ef\u8bfb\uff0c\u76d1\u63a7\u53ef\u5199\uff0c\u89e3\u9664\u53ef\u8bfb\uff0c\u89e3\u9664\u53ef\u5199\uff0c\u89e3\u9664\u6240\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/44567.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-19T07:25:44+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619072541-6853bb758958f.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"17 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/44567.html","url":"https:\/\/www.wsisp.com\/helps\/44567.html","name":"\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-19T07:25:44+00:00","dateModified":"2025-06-19T07:25:44+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/44567.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/44567.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/44567.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4effmuduo\u5e93\u5b9e\u73b0\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\/44567","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=44567"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/44567\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/44565"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=44567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=44567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=44567"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=44567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}