{"id":114412,"date":"2026-10-09T08:26:39","date_gmt":"2026-10-09T00:26:39","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/114412.html"},"modified":"2026-10-09T08:26:39","modified_gmt":"2026-10-09T00:26:39","slug":"%e3%80%90linux%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e3%80%91%e5%9b%9b%e5%8d%81%e4%b9%9d-%e3%80%8areactor-%e6%a8%a1%e5%bc%8f%e8%af%a6%e8%a7%a3%ef%bc%9a%e5%9f%ba%e4%ba%8e-epoll-et-%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/114412.html","title":{"rendered":"\u3010Linux\u7f51\u7edc\u7f16\u7a0b\u3011\u56db\u5341\u4e5d.\u300aReactor \u6a21\u5f0f\u8be6\u89e3\uff1a\u57fa\u4e8e epoll ET \u5b9e\u73b0\u53cd\u5e94\u5806 Tcp \u670d\u52a1\u5668\u5b9e\u6218\u5f00\u53d1\u300b---\u8be6\u89e3"},"content":{"rendered":"<h2 style=\"background-color:transparent\">Reactor\u6a21\u5f0f<\/h2>\n<h3 id=\"1.1%20Reactor%E6%A8%A1%E5%BC%8F%E5%AE%9A%E4%B9%89\" style=\"background-color:transparent\">1.Reactor \u6a21\u5f0f\u5b9a\u4e49<\/h3>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">Reactor \u6a21\u5f0f&#xff08;\u53cd\u5e94\u5668\u6a21\u5f0f&#xff0c;\u4e5f\u79f0 Dispatcher \u5206\u53d1\u5668\u6a21\u5f0f&#xff09;<\/span><\/span> \u662f\u4e00\u79cd\u4e8b\u4ef6\u9a71\u52a8\u7684\u8bbe\u8ba1\u6a21\u5f0f&#xff1a; \u5b83\u901a\u8fc7 I\/O \u591a\u8def\u590d\u7528&#xff08;select \/ poll \/ epoll&#xff09;\u540c\u65f6\u76d1\u542c\u591a\u4e2a\u53e5\u67c4&#xff0c;\u5f53\u67d0\u4e2a\u53e5\u67c4\u4e0a\u7684\u4e8b\u4ef6\u5c31\u7eea\u65f6&#xff0c;\u628a\u4e8b\u4ef6\u5206\u6d3e\u7ed9\u4e8b\u5148\u6ce8\u518c\u597d\u7684\u5904\u7406\u7a0b\u5e8f&#xff08;\u56de\u8c03&#xff09;\u53bb\u5904\u7406\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"992\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002626-6ac834b2e0769.png\" width=\"2496\" \/><\/p>\n<hr \/>\n<h3>2.Reactor\u6a21\u5f0f\u7684\u89d2\u8272\u6784\u6210<\/h3>\n<table>\n<tr>\u89d2\u8272\u89e3\u91ca<\/tr>\n<tbody>\n<tr>\n<td>Handle&#xff08;\u53e5\u67c4&#xff09;<\/td>\n<td>\u7528\u4e8e\u6807\u8bc6\u4e0d\u540c\u7684\u8d44\u6e90&#xff0c;\u672c\u8d28\u5c31\u662f\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26<\/td>\n<\/tr>\n<tr>\n<td>Synchronous Event Demultiplexer&#xff08;\u540c\u6b65\u4e8b\u4ef6\u5206\u79bb\u5668&#xff09;<\/td>\n<td>\u672c\u8d28\u5c31\u662f\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528&#xff0c;\u7528\u4e8e\u7b49\u5f85\u4e8b\u4ef6\u7684\u53d1\u751f\u3002\u5bf9\u4e8e Linux \u6765\u8bf4&#xff0c;\u6307\u7684\u5c31\u662f I\/O \u591a\u8def\u590d\u7528&#xff0c;\u6bd4\u5982\u00a0select\u3001poll\u3001epoll\u00a0\u7b49<\/td>\n<\/tr>\n<tr>\n<td>Event Handler&#xff08;\u4e8b\u4ef6\u5904\u7406\u5668&#xff09;<\/td>\n<td>\u7531\u591a\u4e2a\u56de\u8c03\u65b9\u6cd5\u6784\u6210&#xff0c;\u8fd9\u4e9b\u56de\u8c03\u65b9\u6cd5\u6784\u6210\u4e86\u4e0e\u5e94\u7528\u76f8\u5173\u7684\u3001\u5bf9\u4e8e\u67d0\u4e2a\u4e8b\u4ef6\u7684\u5904\u7406\u53cd\u9988<\/td>\n<\/tr>\n<tr>\n<td>Concrete Event Handler&#xff08;\u5177\u4f53\u4e8b\u4ef6\u5904\u7406\u5668&#xff09;<\/td>\n<td>\u4e8b\u4ef6\u5904\u7406\u5668\u4e2d\u5404\u4e2a\u56de\u8c03\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0<\/td>\n<\/tr>\n<tr>\n<td>Initiation Dispatcher&#xff08;\u521d\u59cb\u5206\u53d1\u5668&#xff09;<\/td>\n<td>\u5b9e\u9645\u4e0a\u5c31\u662f\u00a0Reactor \u89d2\u8272\u3002\u5b83\u4f1a\u901a\u8fc7\u540c\u6b65\u4e8b\u4ef6\u5206\u79bb\u5668\u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f&#xff0c;\u5f53\u5bf9\u5e94\u4e8b\u4ef6\u5c31\u7eea\u65f6\u8c03\u7528\u4e8b\u4ef6\u5904\u7406\u5668&#xff0c;\u6700\u540e\u8c03\u7528\u5bf9\u5e94\u7684\u56de\u8c03\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u4e2a\u4e8b\u4ef6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>3.Reactor \u6a21\u5f0f\u7684\u5de5\u4f5c\u6d41\u7a0b<\/h3>\n<li>\u00a0\u6ce8\u518c:\u5e94\u7528\u5411\u521d\u59cb\u5206\u53d1\u5668\u6ce8\u518c\u5177\u4f53\u4e8b\u4ef6\u5904\u7406\u5668&#xff0c;\u540c\u65f6\u58f0\u660e\u5b83<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5173\u5fc3\u54ea\u4e9b\u4e8b\u4ef6\u7c7b\u578b<\/span><\/span>&#xff08;\u5982&#034;\u53ef\u8bfb&#034;&#xff09;\u3002\u6bcf\u4e2a\u4e8b\u4ef6\u5904\u7406\u5668\u90fd\u5173\u8054\u7740\u4e00\u4e2a Handle\u3002<\/li>\n<li>\u00a0\u4ea4\u51fa Handle:\u521d\u59cb\u5206\u53d1\u5668\u8981\u6c42\u6bcf\u4e2a\u4e8b\u4ef6\u5904\u7406\u5668\u63d0\u4f9b\u5b83\u5185\u90e8\u7684 Handle\u3002<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u8fd9\u4e2a Handle \u5411\u64cd\u4f5c\u7cfb\u7edf\u6807\u8bc6\u4e86\u8981\u76d1\u89c6\u7684\u8d44\u6e90<\/span><\/span>&#xff1b;\u5728 Reactor \u4e2d\u901a\u5e38\u4e00\u4e2a\u5904\u7406\u5668\u5bf9\u5e94\u4e00\u4e2a Handle&#xff0c;\u901a\u8fc7 Handle \u5373\u53ef\u53cd\u67e5\u5904\u7406\u5668\u3002<\/li>\n<li>\u00a0\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af:\u6240\u6709\u5904\u7406\u5668\u6ce8\u518c\u5b8c\u6210\u540e&#xff0c;\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\u3002\u5206\u53d1\u5668\u628a\u6240\u6709\u6ce8\u518c\u7684 Handle \u6c47\u6210\u4e00\u4efd\u76d1\u89c6\u5217\u8868&#xff0c;\u4ea4\u7ed9\u540c\u6b65\u4e8b\u4ef6\u5206\u79bb\u5668\u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f\u3002<\/li>\n<li>\u00a0\u4e8b\u4ef6\u5c31\u7eea:\u67d0\u4e2a Handle \u53d8\u4e3a Ready \u65f6&#xff0c;\u540c\u6b65\u4e8b\u4ef6\u5206\u79bb\u5668\u901a\u77e5\u521d\u59cb\u5206\u53d1\u5668&#xff08;\u5728 Linux \u4e0a\u5c31\u662f\u00a0epoll_wait\u00a0\u8fd4\u56de\u5c31\u7eea\u96c6\u5408&#xff09;\u3002<\/li>\n<li>\u00a0\u67e5\u627e\u5904\u7406\u5668:\u5206\u53d1\u5668\u4ee5 Ready \u7684 Handle \u4f5c\u4e3a key&#xff0c;\u627e\u5230\u5bf9\u5e94\u7684\u4e8b\u4ef6\u5904\u7406\u5668\u3002<\/li>\n<li>\u00a0\u56de\u8c03:\u8c03\u7528\u8be5\u5904\u7406\u5668\u4e2d\u5bf9\u5e94\u7684\u56de\u8c03\u65b9\u6cd5\u6765\u54cd\u5e94\u4e8b\u4ef6\u3002<\/li>\n<li>\u00a0\u56de\u5230:\u7ee7\u7eed\u4e0b\u4e00\u8f6e\u7b49\u5f85 \u2014\u2014\u00a0<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u8fd9\u5c31\u662f&#034;\u4e8b\u4ef6\u5faa\u73af&#034;<\/span><\/span>\u3002<\/li>\n<hr \/>\n<h3 id=\"%E4%BA%8C%E3%80%81epoll%20ET%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%88Reactor%E6%A8%A1%E5%BC%8F%EF%BC%89\">4.epoll ET \u670d\u52a1\u5668&#xff08;Reactor \u6a21\u5f0f&#xff09;<\/h3>\n<h4 style=\"background-color:transparent\">(1)\u8bbe\u8ba1\u601d\u8def<\/h4>\n<p>A. epoll ET \u670d\u52a1\u5668<\/p>\n<p><span style=\"color:#fe2c24\">\u5728\u5b9e\u73b0 ET \u6a21\u5f0f\u4e4b\u524d&#xff0c;\u5fc5\u987b\u5148\u660e\u786e\u4e09\u6761\u524d\u63d0&#xff1a;<\/span><\/p>\n<ul>\n<li>\u6240\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u5fc5\u987b\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e&#xff08;fcntl(fd, F_SETFL, O_NONBLOCK)&#xff09;&#xff1b;<\/li>\n<li>\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u65f6\u5fc5\u987b\u5faa\u73af\u8bfb\u53d6&#xff0c;\u76f4\u5230 recv \u8fd4\u56de EAGAIN&#xff1b;<\/li>\n<li>\u76d1\u542c\u5957\u63a5\u5b57\u7684\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u65f6\u5fc5\u987b\u5faa\u73af accept&#xff0c;\u76f4\u5230\u8fd4\u56de EAGAIN\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u539f\u56e0\u5728\u4e8e ET&#xff08;\u8fb9\u7f18\u89e6\u53d1&#xff09;\u53ea\u5728\u72b6\u6001&#034;\u7531\u65e0\u5230\u6709&#034;\u65f6\u901a\u77e5\u4e00\u6b21<\/span><\/span>\u3002\u672c\u6b21\u82e5\u4e0d\u628a\u6570\u636e\u8bfb\u5b8c&#xff0c;\u5185\u6838\u4e0d\u4f1a\u56e0\u4e3a&#034;\u8fd8\u6709\u5269\u4f59\u6570\u636e&#034;\u800c\u518d\u6b21\u901a\u77e5&#xff0c;\u5269\u4f59\u6570\u636e\u5c06\u6c38\u8fdc\u65e0\u6cd5\u8bfb\u5230&#xff1b;\u540c\u6837&#xff0c;\u82e5\u53ea accept \u4e00\u4e2a\u8fde\u63a5&#xff0c;\u5269\u4f59\u6392\u961f\u8fde\u63a5\u4e5f\u53ef\u80fd\u518d\u4e5f\u6536\u4e0d\u5230\u901a\u77e5\u3002<\/p>\n<ul>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u8bfb\u4e8b\u4ef6&#xff1a;<\/span><\/span>\u82e5\u662f\u76d1\u542c\u5957\u63a5\u5b57\u8bfb\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u5219\u5faa\u73af\u8c03\u7528 accept \u51fd\u6570\u83b7\u53d6\u5e95\u5c42\u8fde\u63a5&#xff0c;\u76f4\u5230\u8fd4\u56de EAGAIN&#xff1b;\u82e5\u662f\u5176\u4ed6\u5957\u63a5\u5b57\u8bfb\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u5219\u5faa\u73af\u8c03\u7528 recv \u51fd\u6570\u8bfb\u53d6\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u6570\u636e&#xff0c;\u76f4\u5230\u8fd4\u56de EAGAIN\u3002\u5176\u4e2d recv \u8fd4\u56de 0 \u8868\u793a\u5bf9\u7aef\u5df2\u5173\u95ed\u8fde\u63a5&#xff0c;\u8fd4\u56de -1 \u4e14 errno &#061;&#061; EAGAIN \u8868\u793a\u672c\u8f6e\u6570\u636e\u5df2\u8bfb\u5b8c&#xff0c;\u4e24\u79cd\u60c5\u51b5\u90fd\u5e94\u9000\u51fa\u5faa\u73af\u3002<\/li>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5199\u4e8b\u4ef6&#xff1a;<\/span><\/span>\u5199\u4e8b\u4ef6\u5c31\u7eea\u8868\u793a\u5185\u6838\u53d1\u9001\u7f13\u51b2\u533a\u6709\u7a7a\u95f4\u53ef\u5199&#xff0c;\u6b64\u65f6\u5e94\u8be5\u4ece\u5e94\u7528\u5c42\u53d1\u9001\u7f13\u51b2\u533a\u4e2d\u53d6\u51fa\u5f85\u53d1\u9001\u7684\u6570\u636e&#xff0c;\u8c03\u7528 send \u5199\u5165\u5185\u6838\u53d1\u9001\u7f13\u51b2\u533a&#xff0c;\u5e76\u5faa\u73af\u76f4\u5230\u6570\u636e\u53d1\u5b8c\u6216\u8fd4\u56de EAGAIN\u3002<\/li>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5f02\u5e38\u4e8b\u4ef6&#xff1a;<\/span><\/span>\u5f53\u67d0\u4e2a\u5957\u63a5\u5b57\u7684\u5f02\u5e38\u4e8b\u4ef6&#xff08;EPOLLERR \/ EPOLLHUP&#xff09;\u5c31\u7eea\u65f6\u4e0d\u505a\u8fc7\u591a\u5904\u7406&#xff0c;\u76f4\u63a5\u5173\u95ed\u8be5\u5957\u63a5\u5b57\u5e76\u6ce8\u9500\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u8fd9\u4e24\u4e2a\u4e8b\u4ef6\u603b\u4f1a\u88ab\u4e0a\u62a5&#xff0c;\u65e0\u9700\u5728 events \u4e2d\u4e3b\u52a8\u6ce8\u518c\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5f53 epoll ET \u670d\u52a1\u5668\u76d1\u6d4b\u5230\u67d0\u4e00\u4e8b\u4ef6\u5c31\u7eea\u540e&#xff0c;\u5c31\u4f1a\u5c06\u8be5\u4e8b\u4ef6\u4ea4\u7ed9\u5bf9\u5e94\u7684\u670d\u52a1\u5904\u7406\u7a0b\u5e8f\u8fdb\u884c\u5904\u7406<\/span><\/span><\/p>\n<hr \/>\n<p>B. Reactor \u6a21\u5f0f\u7684\u4e94\u4e2a\u89d2\u8272<\/p>\n<p>\u5728\u8fd9\u4e2a epoll ET \u670d\u52a1\u5668\u4e2d&#xff0c;Reactor \u6a21\u5f0f\u4e2d\u7684\u4e94\u4e2a\u89d2\u8272\u5bf9\u5e94\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u53e5\u67c4&#xff1a;<\/span><\/span>\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u540c\u6b65\u4e8b\u4ef6\u5206\u79bb\u5668&#xff1a;<\/span><\/span>I\/O \u591a\u8def\u590d\u7528 epoll&#xff0c;\u5373 epoll_wait \u51fd\u6570\u3002 <\/li>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4e8b\u4ef6\u5904\u7406\u5668&#xff1a;<\/span><\/span>\u8bfb\u56de\u8c03\u3001\u5199\u56de\u8c03\u548c\u5f02\u5e38\u56de\u8c03\u7684\u51fd\u6570\u7b7e\u540d&#xff0c;\u5373\u4e8b\u4ef6\u5904\u7406\u7684\u63a5\u53e3\u5b9a\u4e49\u3002<\/li>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5177\u4f53\u4e8b\u4ef6\u5904\u7406\u5668&#xff1a;<\/span><\/span>Connection \u5bf9\u8c61\u3002\u5b83\u628a\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u8be5\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u8bfb\u3001\u5199\u3001\u5f02\u5e38\u56de\u8c03\u5c01\u88c5\u5728\u4e00\u8d77&#xff0c;\u4ece\u800c\u5728\u4e8b\u4ef6\u5c31\u7eea\u65f6\u80fd\u591f\u901a\u8fc7\u6587\u4ef6\u63cf\u8ff0\u7b26\u627e\u5230\u5e76\u6267\u884c\u76f8\u5e94\u7684\u56de\u8c03\u65b9\u6cd5\u3002<\/li>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u521d\u59cb\u5206\u53d1\u5668&#xff1a;<\/span><\/span>Reactor \u7c7b\u4e2d\u7684 Dispatcher \u51fd\u6570\u3002Dispatcher \u51fd\u6570\u7684\u5de5\u4f5c\u5373\u4e3a&#xff1a;\u8c03\u7528 epoll_wait \u51fd\u6570\u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f&#xff0c;\u6709\u4e8b\u4ef6\u53d1\u751f\u540e\u5c06\u5c31\u7eea\u7684\u4e8b\u4ef6\u6d3e\u53d1\u7ed9\u5bf9\u5e94\u7684\u670d\u52a1\u5904\u7406\u7a0b\u5e8f\u5373\u53ef\u3002<\/li>\n<\/ul>\n<hr \/>\n<p>C. Connection \u7c7b<\/p>\n<p>\u5728 Reactor \u7684\u5de5\u4f5c\u6d41\u7a0b\u4e2d\u8bf4\u5230&#xff0c;\u5728\u6ce8\u518c\u4e8b\u4ef6\u5904\u7406\u5668\u65f6\u9700\u8981\u5c06\u5176\u4e0e Handle \u5173\u8054&#xff0c;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u672c\u8d28\u4e0a\u5c31\u662f\u9700\u8981\u5c06\u8bfb\u56de\u8c03\u3001\u5199\u56de\u8c03\u548c\u5f02\u5e38\u56de\u8c03\u4e0e\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u5173\u8054\u8d77\u6765<\/span><\/span>\u3002\u8fd9\u6837\u505a\u7684\u76ee\u7684\u5c31\u662f\u4e3a\u4e86\u5f53\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u4e8b\u4ef6\u5c31\u7eea\u65f6\u53ef\u4ee5\u627e\u5230\u5176\u5bf9\u5e94\u7684\u5404\u79cd\u56de\u8c03\u51fd\u6570&#xff0c;\u8fdb\u800c\u6267\u884c\u5bf9\u5e94\u7684\u56de\u8c03\u65b9\u6cd5\u6765\u5904\u7406\u8be5\u4e8b\u4ef6\u3002<\/p>\n<p>\u53ef\u4ee5\u8bbe\u8ba1\u4e00\u4e2a <span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">Connection \u7c7b<\/span><\/span>&#xff0c;\u8be5\u7c7b\u4e2d\u7684\u6210\u5458\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1b;<\/li>\n<li>\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u8bfb\u56de\u8c03\u3001\u5199\u56de\u8c03\u548c\u5f02\u5e38\u56de\u8c03&#xff1b;<\/li>\n<li><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u63a5\u6536\u7f13\u51b2\u533a\u4e0e\u53d1\u9001\u7f13\u51b2\u533a\u3002<\/span><\/span>ET \u6a21\u5f0f\u4e0b\u4e00\u6b21 recv \u53ef\u80fd\u53ea\u8bfb\u5230\u534a\u6761\u6d88\u606f&#xff0c;\u9700\u8981\u7f13\u51b2\u533a\u6512\u591f\u4e00\u6761\u5b8c\u6574\u62a5\u6587\u518d\u5904\u7406&#xff1b;send \u672a\u53d1\u9001\u5b8c\u65f6&#xff0c;\u5269\u4f59\u6570\u636e\u4e5f\u9700\u8981\u6682\u5b58&#xff0c;\u7b49\u5199\u4e8b\u4ef6\u5c31\u7eea\u540e\u518d\u7ee7\u7eed\u53d1\u9001&#xff1b;<\/li>\n<li>\u8fde\u63a5\u72b6\u6001\u7b49\u5176\u4ed6\u6210\u5458\u3002<\/li>\n<\/ul>\n<hr \/>\n<p>D. TcpServer \u7c7b<\/p>\n<p>\u5728 Reactor \u7684\u5de5\u4f5c\u6d41\u7a0b\u4e2d\u8bf4\u5230&#xff0c;\u5f53\u6240\u6709\u4e8b\u4ef6\u5904\u7406\u5668\u6ce8\u518c\u5b8c\u6bd5\u540e&#xff0c;\u4f1a\u4f7f\u7528\u540c\u6b65\u4e8b\u4ef6\u5206\u79bb\u5668\u7b49\u5f85\u8fd9\u4e9b\u4e8b\u4ef6\u53d1\u751f&#xff0c;\u5f53\u67d0\u4e2a\u4e8b\u4ef6\u5904\u7406\u5668\u7684 Handle \u53d8\u4e3a Ready \u72b6\u6001\u65f6&#xff0c;\u540c\u6b65\u4e8b\u4ef6\u5206\u79bb\u5668\u4f1a\u901a\u77e5\u521d\u59cb\u5206\u53d1\u5668&#xff0c;\u7136\u540e\u521d\u59cb\u5206\u53d1\u5668\u4f1a\u5c06 Ready \u72b6\u6001\u7684 Handle \u4f5c\u4e3a key \u6765\u5bfb\u627e\u5176\u5bf9\u5e94\u7684\u4e8b\u4ef6\u5904\u7406\u5668&#xff0c;\u5e76\u8c03\u7528\u8be5\u4e8b\u4ef6\u5904\u7406\u5668\u4e2d\u5bf9\u5e94\u7684\u56de\u8c03\u65b9\u6cd5\u6765\u54cd\u5e94\u8be5\u4e8b\u4ef6\u3002<\/p>\n<p>\u672c\u8d28\u5c31\u662f\u5f53\u4e8b\u4ef6\u6ce8\u518c\u5b8c\u6bd5\u540e&#xff0c;\u4f1a\u8c03\u7528 epoll_wait \u51fd\u6570\u6765\u7b49\u5f85\u8fd9\u4e9b\u4e8b\u4ef6\u53d1\u751f&#xff0c;\u5f53\u67d0\u4e2a\u4e8b\u4ef6\u5c31\u7eea\u65f6 epoll_wait \u51fd\u6570\u4f1a\u544a\u77e5\u8c03\u7528\u65b9&#xff0c;\u7136\u540e\u8c03\u7528\u65b9\u5c31\u6839\u636e\u5c31\u7eea\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6765\u627e\u5230\u5176\u5bf9\u5e94\u7684\u5404\u79cd\u56de\u8c03\u51fd\u6570&#xff0c;\u5e76\u8c03\u7528\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406\u3002<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5bf9\u6b64\u53ef\u4ee5\u8bbe\u8ba1\u4e00\u4e2a Reactor \u7c7b&#xff1a;<\/span><\/span><\/p>\n<p>\u8be5\u7c7b\u5f53\u4e2d\u6709\u4e00\u4e2a\u6210\u5458\u51fd\u6570 Dispatcher&#xff0c;\u5373\u521d\u59cb\u5206\u53d1\u5668&#xff0c;\u5728\u8be5\u51fd\u6570\u5185\u90e8\u4f1a\u8c03\u7528 epoll_wait \u51fd\u6570\u7b49\u5f85\u4e8b\u4ef6\u7684\u53d1\u751f&#xff0c;epoll_wait \u8fd4\u56de\u540e\u5373\u53ef\u5f97\u5230\u672c\u8f6e\u6240\u6709\u5c31\u7eea\u7684\u4e8b\u4ef6\u3002<\/p>\n<p>\u5f53\u4e8b\u4ef6\u5c31\u7eea\u540e\u9700\u8981\u6839\u636e\u5c31\u7eea\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6765\u627e\u5230\u5176\u5bf9\u5e94\u7684\u5404\u79cd\u56de\u8c03\u51fd\u6570\u3002\u7531\u4e8e\u4f1a\u5c06\u6bcf\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ca\u5176\u5bf9\u5e94\u7684\u5404\u79cd\u56de\u8c03\u90fd\u5c01\u88c5\u5230\u4e00\u4e2a Connection \u7ed3\u6784\u4e2d&#xff0c;\u6240\u4ee5\u53ef\u4ee5\u6839\u636e\u6587\u4ef6\u63cf\u8ff0\u7b26\u627e\u5230\u5176\u5bf9\u5e94\u7684 Connection \u7ed3\u6784\u3002<\/p>\n<p>\u4f7f\u7528 C&#043;&#043; STL \u4e2d\u7684 unordered_map&#xff0c;\u6765\u5efa\u7acb\u5404\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u5176\u5bf9\u5e94\u7684 Connection \u7ed3\u6784\u4e4b\u95f4\u7684\u6620\u5c04\u3002<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u8fd9\u4e2a unordered_map \u5e94\u5f53\u4f5c\u4e3a Reactor \u7c7b\u7684\u6210\u5458\u53d8\u91cf\u2014\u2014\u56e0\u4e3a&#034;\u6309\u6587\u4ef6\u63cf\u8ff0\u7b26\u67e5\u627e\u5904\u7406\u5668&#034;\u6b63\u662f\u521d\u59cb\u5206\u53d1\u5668\u81ea\u8eab\u7684\u804c\u8d23&#xff0c;\u5b83\u4e0e Dispatcher \u5fc5\u987b\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d&#xff0c;\u5426\u5219 Dispatcher \u5c06\u65e0\u6cd5\u5b8c\u6210\u4e8b\u4ef6\u5206\u6d3e\u3002<\/span><\/span><\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">Reactor \u7c7b\u4e2d\u8fd8\u9700\u8981\u63d0\u4f9b\u6210\u5458\u51fd\u6570 AddConnection&#xff0c;\u7528\u4e8e\u5411\u521d\u59cb\u5206\u53d1\u5668\u4e2d\u6ce8\u518c\u4e8b\u4ef6<\/span><\/span>&#xff1a;\u8c03\u7528 epoll_ctl \u4ee5 EPOLL_CTL_ADD \u5c06\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u52a0\u5165\u5185\u6838\u76d1\u89c6\u5217\u8868&#xff0c;\u540c\u65f6\u628a\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e Connection \u7684\u6620\u5c04\u63d2\u5165 unordered_map\u3002<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u6b64\u5916\u8fd8\u5e94\u63d0\u4f9b\u6210\u5458\u51fd\u6570 DelConnection&#xff0c;\u7528\u4e8e\u6ce8\u9500\u4e8b\u4ef6\u5e76\u91ca\u653e\u8fde\u63a5<\/span><\/span>&#xff0c;\u5176\u5185\u90e8\u9700\u8981\u4f9d\u6b21\u5b8c\u6210\u4e09\u4ef6\u4e8b&#xff1a;<\/p>\n<ul>\n<li>epoll_ctl(_epfd, EPOLL_CTL_DEL, fd, nullptr) \u4ece\u5185\u6838\u6458\u9664&#xff1b;<\/li>\n<li>close(fd) \u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1b;<\/li>\n<li>_connections.erase(fd) \u4ece\u6620\u5c04\u8868\u4e2d\u5220\u9664\u3002<\/li>\n<\/ul>\n<p>\u4e09\u8005\u7f3a\u4e00\u4e0d\u53ef&#xff0c;\u5c24\u5176\u662f\u6700\u540e\u4e00\u6b65\u2014\u2014\u82e5\u5fd8\u8bb0\u4ece\u6620\u5c04\u8868\u4e2d\u5220\u9664&#xff0c;\u5f53\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u7f16\u53f7\u88ab\u65b0\u8fde\u63a5\u590d\u7528\u65f6&#xff0c;\u4f1a\u67e5\u627e\u5230\u5df2\u7ecf\u5931\u6548\u7684\u65e7 Connection&#xff0c;\u4ece\u800c\u4ea7\u751f\u6781\u96be\u5b9a\u4f4d\u7684\u9519\u8bef\u3002<\/p>\n<hr \/>\n<p>E. epoll ET \u670d\u52a1\u5668\u7684\u5de5\u4f5c\u6d41\u7a0b<\/p>\n<p>\u00a0\u521d\u59cb\u5316&#xff1a;\u521b\u5efa\u3001\u7ed1\u5b9a\u3001\u76d1\u542c\u5957\u63a5\u5b57&#xff0c;\u5c06\u76d1\u542c\u5957\u63a5\u5b57\u8bbe\u4e3a\u975e\u963b\u585e&#xff0c;\u521b\u5efa epoll \u6a21\u578b\u3002<\/p>\n<p>\u6ce8\u518c\u76d1\u542c\u5957\u63a5\u5b57&#xff1a;\u4e3a\u76d1\u542c\u5957\u63a5\u5b57\u521b\u5efa\u5bf9\u5e94\u7684 Connection \u7ed3\u6784&#xff0c;\u8c03\u7528 Reactor \u7c7b\u7684 AddConnection \u51fd\u6570\u5c06\u5176\u52a0\u5165 epoll \u6a21\u578b&#xff0c;\u5e76\u5efa\u7acb&#034;\u76d1\u542c\u5957\u63a5\u5b57 \u2192 Connection&#034;\u7684\u6620\u5c04\u5173\u7cfb\u3002<\/p>\n<p>\u4e8b\u4ef6\u5faa\u73af&#xff1a;\u8c03\u7528 Reactor \u7c7b\u7684 Dispatcher \u51fd\u6570\u5f00\u59cb\u6d3e\u53d1\u3002Dispatcher \u5185\u90e8\u662f\u4e00\u4e2a while \u5faa\u73af&#xff0c;\u6bcf\u8f6e\u8c03\u7528 epoll_wait \u7b49\u5f85\u4e8b\u4ef6&#xff0c;\u4e8b\u4ef6\u5c31\u7eea\u540e\u6309\u6587\u4ef6\u63cf\u8ff0\u7b26\u627e\u5230\u5bf9\u5e94\u7684 Connection&#xff0c;\u8c03\u7528\u76f8\u5e94\u56de\u8c03\u5904\u7406\u3002<\/p>\n<p>\u52a8\u6001\u6ce8\u518c\u4e0e\u6ce8\u9500&#xff1a;\u5904\u7406\u8fc7\u7a0b\u4e2d\u4f1a\u4e0d\u65ad\u65b0\u589e\u548c\u79fb\u9664\u88ab\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1a;<\/p>\n<ul>\n<li>\u65b0\u8fde\u63a5\u5230\u6765 &#8211;&gt;\u00a0accept \u5faa\u73af\u5230 EAGAIN &#8211;&gt;\u00a0\u521b\u5efa Connection &#8211;&gt;\u00a0AddConnection \u6ce8\u518c&#xff1b;<\/li>\n<li>\u8fde\u63a5\u65ad\u5f00\u6216\u51fa\u9519 &#8211;&gt;\u00a0DelConnection \u6ce8\u9500&#xff08;\u4ece epoll \u6458\u9664\u3001close\u3001\u4ece\u6620\u5c04\u8868\u5220\u9664&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u9000\u51fa&#xff1a;Dispatcher \u662f\u6b7b\u5faa\u73af&#xff0c;\u9700\u7528 _stop \u6807\u5fd7\u63a7\u5236\u9000\u51fa&#xff0c;\u9000\u51fa\u540e\u7edf\u4e00\u5173\u95ed\u76d1\u542c\u5957\u63a5\u5b57\u4e0e epoll \u53e5\u67c4\u3002<\/p>\n<hr \/>\n<h4 style=\"background-color:transparent\">&#xff08;2&#xff09;Connection \u7ed3\u6784<\/h4>\n<ul>\n<li>\u00a0Connection \u7ed3\u6784\u4e2d\u9664\u4e86\u5305\u542b\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u5176\u5bf9\u5e94\u7684\u8bfb\u56de\u8c03\u3001\u5199\u56de\u8c03\u548c\u5f02\u5e38\u56de\u8c03\u5916&#xff0c;\u8fd8\u5305\u542b\u4e00\u4e2a\u8f93\u5165\u7f13\u51b2\u533a _inBuffer\u3001\u4e00\u4e2a\u8f93\u51fa\u7f13\u51b2\u533a _outBuffer \u4ee5\u53ca\u4e00\u4e2a\u56de\u6307\u6307\u9488 _svrPtr\u3002<\/li>\n<li>\u5f53\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u65f6&#xff0c;\u8c03\u7528 recv \u51fd\u6570\u8bfb\u53d6\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u6570\u636e&#xff0c;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4f46\u4e00\u6b21 recv \u65e2\u53ef\u80fd\u53ea\u8bfb\u5230\u534a\u4e2a\u62a5\u6587&#xff0c;\u4e5f\u53ef\u80fd\u8bfb\u5230\u591a\u4e2a\u62a5\u6587&#xff0c;\u56e0\u6b64\u9700\u8981\u5c06\u8bfb\u53d6\u5230\u7684\u6570\u636e\u6682\u65f6\u5b58\u653e\u5230\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684 _inBuffer \u4e2d<\/span><\/span>&#xff0c;\u5faa\u73af\u4ece\u4e2d\u5206\u79bb\u51fa\u5b8c\u6574\u7684\u62a5\u6587\u518d\u8fdb\u884c\u6570\u636e\u5904\u7406&#xff0c;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">_inBuffer \u672c\u8d28\u5c31\u662f\u7528\u6765\u89e3\u51b3\u7c98\u5305\u4e0e\u534a\u5305\u95ee\u9898\u7684\u3002<\/span><\/span><\/li>\n<li>\u5f53\u5904\u7406\u5b8c\u4e00\u4e2a\u62a5\u6587\u8bf7\u6c42\u540e&#xff0c;\u9700\u5c06\u54cd\u5e94\u6570\u636e\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef<span style=\"background-color:#ffd900\">\u3002<\/span><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u5148\u628a\u54cd\u5e94\u6570\u636e\u8ffd\u52a0\u5230 _outBuffer&#xff0c;\u518d\u5c1d\u8bd5\u8c03\u7528 send \u53d1\u9001&#xff1b;\u82e5\u5e95\u5c42 TCP \u53d1\u9001\u7f13\u51b2\u533a\u7a7a\u95f4\u4e0d\u8db3&#xff0c;\u672a\u53d1\u5b8c\u7684\u90e8\u5206\u5c31\u7559\u5728 _outBuffer \u4e2d&#xff0c;\u7b49\u5199\u4e8b\u4ef6\u5c31\u7eea\u65f6\u518d\u4f9d\u6b21\u53d1\u9001<\/span><span style=\"background-color:#ffd900\">\u3002<\/span><\/span><\/li>\n<li>Connection \u7ed3\u6784\u4e2d\u8bbe\u7f6e\u56de\u6307\u6307\u9488 _svrPtr&#xff0c;\u4fbf\u4e8e\u5feb\u901f\u627e\u5230 TcpServer \u5bf9\u8c61&#xff0c;\u56e0\u4e3a\u540e\u7eed\u9700\u8981\u6839\u636e Connection \u7ed3\u6784\u627e\u5230\u8fd9\u4e2a TcpServer \u5bf9\u8c61\u3002\u5982\u4e0a\u5c42\u4e1a\u52a1\u5904\u7406\u51fd\u6570 NetCal \u5411 _outBuffer \u9012\u4ea4\u6570\u636e\u540e&#xff0c;\u9700\u901a\u8fc7 Connection \u4e2d\u7684\u56de\u6307\u6307\u9488&#034;\u63d0\u9192&#034;\u4e0a\u5c42\u53d1\u8d77\u53d1\u9001\u3002<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u9700\u8981\u6ce8\u610f&#xff1a;\u771f\u6b63\u8981\u505a\u7684\u52a8\u4f5c\u662f&#034;\u6ce8\u518c\u5199\u4e8b\u4ef6&#034;&#xff0c;\u800c\u8fd9\u9700\u8981\u64cd\u4f5c epoll\u2014\u2014epoll \u7531 Reactor \u6301\u6709&#xff0c;\u56e0\u6b64\u8fd9\u4e00\u6b65\u5e94\u901a\u8fc7 TcpServer \u8f6c\u53d1\u7ed9 Reactor \u5b8c\u6210\u3002<\/span><\/span><\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">Connection \u7ed3\u6784\u4e2d\u9700\u63d0\u4f9b\u4e00\u4e2a\u7ba1\u7406\u56de\u8c03\u7684\u6210\u5458\u51fd\u6570&#xff0c;\u4fbf\u4e8e\u5916\u90e8\u5bf9\u56de\u8c03\u8fdb\u884c\u8bbe\u7f6e&#xff1a;<\/span><\/span><\/p>\n<p>\u00a0\u4e3a\u4e86\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c&#xff0c;\u5e38\u89c4\u7684sock\u5fc5\u987b\u662f\u8981\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u63a5\u6536\u7f13\u51b2\u533a&amp;&amp;\u53d1\u9001\u7f13\u51b2\u533a<\/p>\n<p>class Connection<br \/>\n{<br \/>\npublic:<br \/>\n    Connection(int sock &#061; -1)<br \/>\n        : _sock(sock)<br \/>\n        , _tsvr(nullptr)<br \/>\n    {}<\/p>\n<p>    void SetCallBack(func_t recv_cb, func_t send_cb, func_t except_cb)<br \/>\n    {<br \/>\n        _recv_cb &#061; recv_cb;<br \/>\n        _send_cb &#061; send_cb;<br \/>\n        _except_cb &#061; except_cb;<br \/>\n    }<\/p>\n<p>    ~Connection()<br \/>\n    {}<br \/>\n};<\/p>\n<p>private:<br \/>\n    int _listensock;<br \/>\n    int _port;<br \/>\n    Epoll _poll;<br \/>\n    std::unordered_map&lt;int, Connection *&gt; _connections;<br \/>\n    struct epoll_event *_revs;<br \/>\n    int _revs_num;<\/p>\n<p>    \/\/ \u8fd9\u4e2a\u662f\u4e0a\u5c42\u7684\u4e1a\u52a1\u5904\u7406<br \/>\n    callback_t _cb;<br \/>\n};<\/p>\n<hr \/>\n<h4 style=\"background-color:transparent\">&#xff08;3&#xff09;TcpServer \u7c7b<\/h4>\n<p>TcpServer \u7c7b\u4e2d\u6709\u4e24\u4e2a\u5173\u952e\u6210\u5458&#xff1a;<\/p>\n<ul>\n<li>\u4e00\u4e2a unordered_map&#xff0c;\u7528\u4e8e\u5efa\u7acb\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u5176\u5bf9\u5e94\u7684 Connection \u7ed3\u6784\u4e4b\u95f4\u7684\u6620\u5c04&#xff1b;<\/li>\n<li>\u4e00\u4e2a Epoll \u5bf9\u8c61\u6210\u5458 _epoll&#xff0c;\u5b83\u662f\u540c\u6b65\u4e8b\u4ef6\u5206\u79bb\u5668\u7684\u5c01\u88c5\u3002 \u7531\u4e8e _epoll \u662f\u503c\u6210\u5458&#xff0c;Epoll \u5bf9\u8c61\u4f1a\u968f TcpServer \u7684\u6784\u9020\u81ea\u52a8\u521b\u5efa\u3002<\/li>\n<\/ul>\n<p>\u5728 TcpServer \u7684\u6784\u9020\u51fd\u6570\u4e2d\u8c03\u7528 Epoll \u7c7b\u63d0\u4f9b\u7684 EpollCreate \u51fd\u6570&#xff0c;\u521b\u5efa\u5185\u6838\u4e2d\u7684 epoll \u5b9e\u4f8b&#xff0c;\u5e76\u628a epoll_create \u8fd4\u56de\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bb0\u5f55\u5230 Epoll \u5bf9\u8c61\u7684\u6210\u5458\u53d8\u91cf _epollFd \u4e2d&#xff0c;\u4fbf\u4e8e\u540e\u7eed epoll_ctl \u4e0e epoll_wait \u4f7f\u7528\u3002<\/p>\n<p>TcpServer \u5bf9\u8c61\u6790\u6784\u65f6&#xff0c;_epoll \u4f5c\u4e3a\u6210\u5458\u4f1a\u81ea\u52a8\u6790\u6784&#xff0c;\u5176\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528 close \u5173\u95ed epoll \u6a21\u578b\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u65e0\u9700\u624b\u52a8\u5904\u7406\u3002<\/p>\n<p>\u6b64\u5916&#xff0c;\u6790\u6784\u65f6\u8fd8\u9700\u8981\u91ca\u653e _connections \u4e2d\u6240\u6709\u7684 Connection \u5bf9\u8c61\u2014\u2014\u8be5\u6620\u5c04\u8868\u5b58\u653e\u7684\u662f\u88f8\u6307\u9488&#xff0c;\u4e0d\u4f1a\u81ea\u52a8\u91ca\u653e&#xff0c;\u5e94\u9010\u4e2a delete&#xff0c;\u5426\u5219\u6bcf\u5904\u7406\u4e00\u4e2a\u8fde\u63a5\u5c31\u6cc4\u6f0f\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<hr \/>\n<p>A.\u00a0\u5c01\u88c5 Epoll \u7c7b<\/p>\n<p>Epoll.hpp<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<\/p>\n<p>class Epoll<br \/>\n{<br \/>\n    const static int gnum &#061; 128;<br \/>\n    const static int gtimeout &#061; 5000;<br \/>\npublic:<br \/>\n    Epoll(int timeout &#061; gtimeout) : _timeout(timeout)<br \/>\n    {}<\/p>\n<p>    void CreateEpoll()<br \/>\n    {<br \/>\n        _epfd &#061; epoll_create(gnum);<br \/>\n        if(_epfd &lt; 0) exit(5);<br \/>\n    }<\/p>\n<p>    bool DelFromEpoll(int sock)<br \/>\n    {<br \/>\n        int n &#061; epoll_ctl(_epfd, EPOLL_CTL_DEL, sock, nullptr);<br \/>\n        return n &#061;&#061; 0;<br \/>\n    }<\/p>\n<p>    bool CtrlEpoll(int sock, uint32_t events)<br \/>\n    {<br \/>\n        events |&#061; EPOLLET;<br \/>\n        struct epoll_event ev;<br \/>\n        ev.events &#061; events;<br \/>\n        ev.data.fd &#061; sock;<br \/>\n        int n &#061; epoll_ctl(_epfd, EPOLL_CTL_MOD, sock, &amp;ev);<br \/>\n        return n &#061;&#061; 0;<br \/>\n    }<\/p>\n<p>    bool AddSockToEpoll(int sock, uint32_t events)<br \/>\n    {<br \/>\n        struct epoll_event ev;<br \/>\n        ev.events &#061; events;<br \/>\n        ev.data.fd &#061; sock;<br \/>\n        int n &#061; epoll_ctl(_epfd, EPOLL_CTL_ADD, sock, &amp;ev);<br \/>\n        return n &#061;&#061; 0;<br \/>\n    }<\/p>\n<p>    int WaitEpoll(struct epoll_event revs[], int num)<br \/>\n    {<br \/>\n        return epoll_wait(_epfd, revs, num, _timeout);<br \/>\n    }<\/p>\n<p>    ~Epoll()<br \/>\n    {}<\/p>\n<p>private:<br \/>\n    int _epfd;<br \/>\n    int _timeout;<br \/>\n};<\/p>\n<hr \/>\n<p>B.\u00a0TcpServer \u7c7b\u90e8\u5206\u4ee3\u7801<\/p>\n<p>\/\/ \u8fd9\u4e2a\u7f51\u7edc\u670d\u52a1\u5668\u575a\u51b3\u4e0d\u8981\u548c\u4e0a\u5c42\u4e1a\u52a1\u5f3a\u8026\u5408<br \/>\nclass TcpServer<br \/>\n{<br \/>\n    const static int gport &#061; 8080;<br \/>\n    const static int gnum &#061; 128;<\/p>\n<p>public:<br \/>\n    TcpServer(int port &#061; gport) : _port(port), _revs_num(gnum)<br \/>\n    {<br \/>\n        \/\/ 1. \u521b\u5efalistensock<br \/>\n        _listensock &#061; Sock::Socket();<br \/>\n        Sock::Bind(_listensock, _port);<br \/>\n        Sock::Listen(_listensock);<\/p>\n<p>        \/\/ 2. \u521b\u5efa\u591a\u8def\u8f6c\u63a5\u5bf9\u8c61<br \/>\n        _poll.CreateEpoll();<\/p>\n<p>        \/\/ 3. \u6dfb\u52a0listensock\u5230\u670d\u52a1\u5668\u4e2d<br \/>\n        AddConnection(_listensock,<br \/>\n                      std::bind(&amp;TcpServer::Accepter, this, std::placeholders::_1),<br \/>\n                      nullptr, nullptr);<\/p>\n<p>        \/\/ 4. \u6784\u5efa\u4e00\u4e2a\u83b7\u53d6\u5c31\u7eea\u4e8b\u4ef6\u7684\u7f13\u51b2\u533a<br \/>\n        _revs &#061; new struct epoll_event[_revs_num];<br \/>\n    }<\/p>\n<p>    ~TcpServer()<br \/>\n    {<br \/>\n        if (_listensock &gt;&#061; 0) close(_listensock);<br \/>\n        if (_revs) delete[] _revs;<br \/>\n    }<\/p>\n<p>private:<br \/>\n    int _listensock;<br \/>\n    int _port;<br \/>\n    Epoll _poll;<br \/>\n    std::unordered_map&lt;int, Connection *&gt; _connections;<br \/>\n    struct epoll_event *_revs;<br \/>\n    int _revs_num;<\/p>\n<p>    \/\/ \u8fd9\u4e2a\u662f\u4e0a\u5c42\u7684\u4e1a\u52a1\u5904\u7406<br \/>\n    callback_t _cb;<br \/>\n};<\/p>\n<hr \/>\n<p>a. AddConnection \u51fd\u6570<\/p>\n<p>TcpServer \u7c7b\u4e2d\u7684 AddConnection \u51fd\u6570\u7528\u4e8e\u8fdb\u884c\u4e8b\u4ef6\u6ce8\u518c\u3002<\/p>\n<p>\u5728\u6ce8\u518c\u4e8b\u4ef6\u65f6\u9700\u8981\u4f20\u5165<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u4e09\u4e2a\u56de\u8c03\u51fd\u6570<\/span><\/span>&#xff0c;\u8868\u793a\u5f53\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u4e8b\u4ef6&#xff08;\u9ed8\u8ba4\u53ea\u5173\u5fc3\u8bfb\u4e8b\u4ef6&#xff09;\u5c31\u7eea\u540e\u5e94\u8be5\u6267\u884c\u7684\u56de\u8c03\u65b9\u6cd5\u3002<\/p>\n<p>\u5728 AddConnection \u51fd\u6570\u5185\u90e8\u8981\u505a\u7684\u5c31\u662f&#xff1a;\u5c06\u5957\u63a5\u5b57\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e&#xff08;ET \u6a21\u5f0f\u7684\u8981\u6c42&#xff09;&#xff0c;\u628a\u5957\u63a5\u5b57\u548c\u56de\u8c03\u51fd\u6570\u7b49\u5c5e\u6027\u5c01\u88c5\u4e3a\u4e00\u4e2a Connection \u5bf9\u8c61&#xff0c;\u518d\u5c06\u8be5\u5957\u63a5\u5b57\u6dfb\u52a0\u5230 epoll \u6a21\u578b\u4e2d&#xff0c;\u5e76\u5efa\u7acb\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e Connection \u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb&#xff0c;\u7531 TcpServer \u7edf\u4e00\u7ba1\u7406\u3002<\/p>\n<p>\/\/ \u8fd9\u91cc\u4e13\u95e8\u9488\u5bf9\u4efb\u610fsock\u8fdb\u884c\u6dfb\u52a0TcpServer<br \/>\nvoid AddConnection(int sock, func_t recv_cb, func_t send_cb, func_t except_cb)<br \/>\n{<br \/>\n    Sock::SetNonBlock(sock);<br \/>\n    \/\/ \u9664\u4e86 _listensock&#xff0c;\u4ee5\u540e\u4f1a\u5b58\u5728\u5927\u91cf\u7684socket&#xff0c;\u6bcf\u4e00\u4e2asock\u90fd\u5fc5\u987b\u88ab\u5c01\u88c5\u6210\u4e3a\u4e00\u4e2aConnection<br \/>\n    \/\/ \u5f53\u670d\u52a1\u5668\u4e2d\u5b58\u5728\u5927\u91cf\u7684Connection\u65f6&#xff0c;TcpServer\u9700\u8981\u5c06\u6240\u6709\u7684Connection\u8981\u8fdb\u884c\u7ba1\u7406&#xff1a;\u5148\u63cf\u8ff0&#xff0c;\u518d\u7ec4\u7ec7<br \/>\n    \/\/ 1. \u6784\u5efaconn\u5bf9\u8c61&#xff0c;\u5c01\u88c5sock<br \/>\n    Connection *conn &#061; new Connection(sock);<br \/>\n    conn-&gt;SetCallBack(recv_cb, send_cb, except_cb);<br \/>\n    conn-&gt;_tsvr &#061; this;<br \/>\n    \/\/conn-&gt;_lasttimestamp &#061; time();<br \/>\n    \/\/ 2. \u6dfb\u52a0sock\u5230epoll\u4e2d<br \/>\n    _poll.AddSockToEpoll(sock, EPOLLIN | EPOLLET);   \/\/ \u4efb\u4f55\u591a\u8def\u8f6c\u63a5\u7684\u670d\u52a1\u5668&#xff0c;\u4e00\u822c\u9ed8\u8ba4\u53ea\u4f1a\u6253\u5f00\u5bf9\u8bfb\u53d6\u4e8b\u4ef6\u7684\u5173\u5fc3&#xff0c;\u5199\u5165\u4e8b\u4ef6\u4f1a\u6309\u9700\u8fdb\u884c\u6253\u5f00<br \/>\n    \/\/ 3. \u8fd8\u8981\u5c06\u5bf9\u5e94\u7684Connection*\u5bf9\u8c61\u6307\u9488\u6dfb\u52a0\u5230Connections\u6620\u5c04\u8868\u4e2d<br \/>\n    _connections.insert(std::make_pair(sock, conn));<br \/>\n}<\/p>\n<hr \/>\n<p>b. Dispatcher \u51fd\u6570&#xff08;\u521d\u59cb\u5206\u53d1\u5668&#xff09;<\/p>\n<p>TcpServer \u4e2d\u7684 Dispatcher \u51fd\u6570\u5373\u521d\u59cb\u5206\u53d1\u5668\u3002\u5176\u5185\u90e8\u662f\u4e00\u4e2a while \u5faa\u73af&#xff0c;\u6bcf\u8f6e\u8c03\u7528\u4e00\u6b21 epoll_wait \u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f\u3002<\/p>\n<p>epoll_wait \u8fd4\u56de\u5c31\u7eea\u4e8b\u4ef6\u7684\u4e2a\u6570 n&#xff0c;\u5c31\u7eea\u4e8b\u4ef6\u5b58\u653e\u5728 _revs \u6570\u7ec4\u4e2d\u3002<\/p>\n<p>\u968f\u540e\u904d\u5386\u8fd9 n \u4e2a\u4e8b\u4ef6&#xff0c;\u5bf9\u6bcf\u4e00\u4e2a\u4e8b\u4ef6\u4f9d\u6b21\u505a\u4e09\u4ef6\u4e8b&#xff1a;<\/p>\n<li>\u53d6\u51fa\u8be5\u4e8b\u4ef6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26 fd \u4e0e\u4e8b\u4ef6\u7c7b\u578b revents&#xff1b;<\/li>\n<li>\u901a\u8fc7 unordered_map \u627e\u5230\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684 Connection \u7ed3\u6784&#xff1b;<\/li>\n<li>\u6839\u636e revents \u5224\u65ad\u8be5\u8c03\u7528\u54ea\u4e2a\u56de\u8c03&#xff0c;\u5e76\u8c03\u7528\u5b83&#xff1a;<\/li>\n<ul>\n<li>\u542b EPOLLERR \/ EPOLLHUP &#8211;&gt;\u00a0\u8c03\u7528\u5f02\u5e38\u56de\u8c03&#xff08;\u5e94\u4f18\u5148\u5224\u65ad&#xff09;<\/li>\n<li>\u542b EPOLLIN &#8211;&gt;\u00a0\u8c03\u7528\u8bfb\u56de\u8c03<\/li>\n<li>\u542b EPOLLOUT &#8211;&gt;\u00a0\u8c03\u7528\u5199\u56de\u8c03 \u672c\u8f6e\u6240\u6709\u4e8b\u4ef6\u5904\u7406\u5b8c\u6bd5\u540e&#xff0c;\u56de\u5230\u5faa\u73af\u5f00\u5934\u7ee7\u7eed epoll_wait\u3002<\/li>\n<\/ul>\n<p>LoopOnce() \u2014\u2014 \u5355\u8f6e\u4e8b\u4ef6\u5904\u7406<\/p>\n<p>void LoopOnce()<br \/>\n{<br \/>\n    int n &#061; _poll.WaitEpoll(_revs, _revs_num);<br \/>\n    for (int i &#061; 0; i &lt; n; i&#043;&#043;)<br \/>\n    {<br \/>\n        int sock &#061; _revs[i].data.fd;<br \/>\n        uint32_t revents &#061; _revs[i].events;<\/p>\n<p>        \/\/ \u5c06\u6240\u6709\u7684\u5f02\u5e38\u5168\u90e8\u4ea4\u7ed9read\u6216\u8005write\u6765\u7edf\u4e00\u5904\u7406<br \/>\n        if(revents &amp; EPOLLERR)  revents |&#061; (EPOLLIN | EPOLLOUT);<br \/>\n        if(revents &amp; EPOLLHUP)  revents |&#061; (EPOLLIN | EPOLLOUT);<\/p>\n<p>        if (revents &amp; EPOLLIN)<br \/>\n        {<br \/>\n            if (IsConnectionExists(sock) &amp;&amp; _connections[sock]-&gt;_recv_cb !&#061; nullptr)<br \/>\n                _connections[sock]-&gt;_recv_cb(_connections[sock]);<br \/>\n        }<br \/>\n        if (revents &amp; EPOLLOUT)<br \/>\n        {<br \/>\n            if (IsConnectionExists(sock) &amp;&amp; _connections[sock]-&gt;_send_cb !&#061; nullptr)<br \/>\n                _connections[sock]-&gt;_send_cb(_connections[sock]);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>Dispatcher() \u2014\u2014 \u4e8b\u4ef6\u5faa\u73af<\/p>\n<p>\/\/ \u6839\u636e\u5c31\u7eea\u7684\u4e8b\u4ef6&#xff0c;\u8fdb\u884c\u7279\u5b9a\u4e8b\u4ef6\u7684\u6d3e\u53d1<br \/>\nvoid Dispatcher(callback_t cb)<br \/>\n{<br \/>\n    _cb &#061; cb;<br \/>\n    while (true)<br \/>\n    {<br \/>\n        ConnectAliveCheck();<br \/>\n        LoopOnce();   \/\/ \u5c06epoll\u5f53\u505a\u5b9a\u65f6\u5668\u6765\u4f7f\u7528<br \/>\n    }<br \/>\n}<\/p>\n<p>private \u6210\u5458&#xff08;\u4e0e\u524d\u4e00\u81f4&#xff09;<\/p>\n<p>private:<br \/>\n    int _listensock;<br \/>\n    int _port;<br \/>\n    Epoll _poll;<br \/>\n    std::unordered_map&lt;int, Connection *&gt; _connections;<br \/>\n    struct epoll_event *_revs;<br \/>\n    int _revs_num;<\/p>\n<p>    \/\/ \u8fd9\u4e2a\u662f\u4e0a\u5c42\u7684\u4e1a\u52a1\u5904\u7406<br \/>\n    callback_t _cb;<br \/>\n};<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u6ca1\u6709\u901a\u8fc7 switch \/if \u5206\u652f\u5224\u65ad epoll_wait \u7684\u8fd4\u56de\u7ed3\u679c&#xff0c;\u800c\u662f\u5229\u7528 for \u5faa\u73af\u6761\u4ef6\u672c\u8eab\u5b8c\u6210\u8fd4\u56de\u503c\u5224\u5b9a&#xff1a;<\/p>\n<ul>\n<li>epoll_wait \u8fd4\u56de -1&#xff1a;\u4ee3\u8868\u8c03\u7528\u51fa\u9519&#xff0c;\u5faa\u73af\u6761\u4ef6\u4e0d\u6210\u7acb&#xff0c;\u4e0d\u4f1a\u8fdb\u5165\u5faa\u73af\u4f53\u6267\u884c\u4e8b\u4ef6\u903b\u8f91&#xff1b;<\/li>\n<li>epoll_wait \u8fd4\u56de 0&#xff1a;\u4ee3\u8868\u7b49\u5f85\u8d85\u65f6&#xff0c;\u5faa\u73af\u6761\u4ef6\u540c\u6837\u4e0d\u6210\u7acb&#xff0c;\u8df3\u8fc7\u4e8b\u4ef6\u5904\u7406&#xff1b;<\/li>\n<li>epoll_wait \u8fd4\u56de\u5927\u4e8e 0&#xff1a;\u4ee3\u8868\u6210\u529f\u6355\u83b7\u5230\u5c31\u7eea\u4e8b\u4ef6&#xff0c;\u8fdb\u5165 for \u5faa\u73af&#xff0c;\u6267\u884c\u5bf9\u5e94\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p>\u5728\u4e8b\u4ef6\u5904\u7406\u6d41\u7a0b\u91cc&#xff0c;\u4f1a\u4f18\u5148\u5904\u7406\u5f02\u5e38\u4e8b\u4ef6&#xff0c;\u5e76\u4ea4\u7531\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\u5b8c\u6210\u5904\u7406\u3002<\/p>\n<hr \/>\n<p>c. EnableReadWrite \u51fd\u6570<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">TcpServer \u7c7b\u4e2d\u7684 EnableReadWrite \u51fd\u6570&#xff0c;\u7528\u4e8e\u52a8\u6001\u4fee\u6539\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u76d1\u542c\u4e8b\u4ef6<\/span><\/span>\u3002 \u8c03\u7528\u8be5\u51fd\u6570\u65f6\u9700\u8981\u4f20\u5165\u4e09\u4e2a\u53c2\u6570&#xff1a;<\/p>\n<ul>\n<li>\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u8868\u793a\u9700\u8981\u8bbe\u7f6e\u7684\u662f\u54ea\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u4e8b\u4ef6&#xff1b;<\/li>\n<li>\u4e24\u4e2a bool \u503c&#xff0c;\u5206\u522b\u8868\u793a\u662f\u5426\u5173\u5fc3\u8bfb\u4e8b\u4ef6\u4ee5\u53ca\u662f\u5426\u5173\u5fc3\u5199\u4e8b\u4ef6\u3002<\/li>\n<\/ul>\n<p>\u51fd\u6570\u5185\u90e8\u4f1a\u8c03\u7528 Epoll \u7c7b\u5c01\u88c5\u597d\u7684 CtrlEpoll \u51fd\u6570&#xff08;\u5373 epoll_ctl(EPOLL_CTL_MOD)&#xff09;\u4fee\u6539\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u76d1\u542c\u4e8b\u4ef6\u3002<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4e3a\u4ec0\u4e48\u9700\u8981\u8fd9\u4e2a\u51fd\u6570&#xff1f;<\/span><\/span><\/p>\n<p>\u56e0\u4e3a\u5199\u4e8b\u4ef6\u4e0d\u80fd\u957f\u671f\u6ce8\u518c&#xff1a;\u53ea\u8981\u5e95\u5c42\u53d1\u9001\u7f13\u51b2\u533a\u4e0d\u6ee1&#xff0c;\u5199\u4e8b\u4ef6\u5c31\u4f1a\u4e00\u76f4\u5904\u4e8e\u5c31\u7eea\u72b6\u6001&#xff0c;\u5bfc\u81f4 epoll_wait \u6bcf\u6b21\u90fd\u7acb\u5373\u8fd4\u56de\u3001CPU \u7a7a\u8f6c\u3002\u56e0\u6b64\u6b63\u786e\u505a\u6cd5\u662f&#034;\u6309\u9700\u5f00\u5173&#034;&#xff1a;<\/p>\n<ul>\n<li>\u6570\u636e\u53d1\u4e0d\u5b8c\u65f6 &#8211;&gt;\u00a0EnableReadWrite(fd, true, true) \u6253\u5f00\u5199\u4e8b\u4ef6&#xff1b;<\/li>\n<li>\u6570\u636e\u5168\u90e8\u53d1\u5b8c\u540e &#8211;&gt;\u00a0EnableReadWrite(fd, true, false) \u7acb\u5373\u5173\u95ed\u5199\u4e8b\u4ef6\u3002<\/li>\n<\/ul>\n<p>\u8bfb\u4e8b\u4ef6\u901a\u5e38\u662f\u957f\u671f\u5173\u5fc3\u7684&#xff08;\u8fde\u63a5\u6d3b\u7740\u5c31\u8981\u8bfb&#xff09;&#xff0c;\u6240\u4ee5\u8fd9\u4e2a\u51fd\u6570\u4e3b\u8981\u7528\u6765\u5f00\u5173\u5199\u4e8b\u4ef6\u3002<\/p>\n<p>void EnableReadWrite(Connection *conn, bool readable, bool writeable)<br \/>\n{<br \/>\n    uint32_t events &#061; ((readable ? EPOLLIN : 0) | (writeable ? EPOLLOUT : 0));<br \/>\n    bool res &#061; _poll.CtrlEpoll(conn-&gt;_sock, events);<br \/>\n    assert(res);<br \/>\n}<\/p>\n<hr \/>\n<h4>&#xff08;4&#xff09;\u56de\u8c03\u51fd\u6570<\/h4>\n<ul>\n<li>Accepter&#xff1a;\u8fde\u63a5\u4e8b\u4ef6\u89e6\u53d1\u65f6&#xff0c;\u6267\u884c\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570&#xff0c;\u83b7\u53d6\u5e95\u5c42\u5df2\u7ecf\u5efa\u7acb\u5b8c\u6210\u7684\u8fde\u63a5\u3002<\/li>\n<li>Recver&#xff1a;\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u65f6&#xff0c;\u6267\u884c\u8be5\u56de\u8c03\u51fd\u6570&#xff0c;\u8bfb\u53d6\u5ba2\u6237\u7aef\u4f20\u8f93\u7684\u6570\u636e\u5e76\u505a\u4e1a\u52a1\u5904\u7406\u3002<\/li>\n<li>Sender&#xff1a;\u5199\u4e8b\u4ef6\u5c31\u7eea\u65f6&#xff0c;\u6267\u884c\u8be5\u56de\u8c03\u51fd\u6570&#xff0c;\u5411\u5ba2\u6237\u7aef\u56de\u4f20\u54cd\u5e94\u6570\u636e\u3002<\/li>\n<li>Excepter&#xff1a;\u5f02\u5e38\u4e8b\u4ef6\u53d1\u751f\u65f6&#xff0c;\u8c03\u7528\u8be5\u56de\u8c03\u51fd\u6570&#xff0c;\u5b8c\u6210\u5404\u7c7b\u8d44\u6e90\u7684\u91ca\u653e\u5de5\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u5728\u4e3a\u6587\u4ef6\u63cf\u8ff0\u7b26\u521b\u5efa Connection \u5b9e\u4f8b\u65f6&#xff0c;\u53ef\u4ee5\u8c03\u7528 Connection \u7c7b\u7684 SetCallBack \u65b9\u6cd5&#xff0c;\u628a\u4e0a\u9762\u8fd9\u51e0\u7ec4\u56de\u8c03\u51fd\u6570\u6ce8\u518c\u5230 Connection \u5bf9\u8c61\u4e2d\u3002<\/p>\n<li>\u76d1\u542c\u5957\u63a5\u5b57\u6240\u7ed1\u5b9a\u7684 Connection \u5b9e\u4f8b&#xff0c;\u5b83\u7684_recvCb \u56de\u8c03\u4f1a\u8bbe\u7f6e\u6210 Accepter\u3002\u539f\u56e0\u662f\u76d1\u542c\u5957\u63a5\u5b57\u7684\u8bfb\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u672c\u8d28\u5c31\u4ee3\u8868\u6709\u65b0\u8fde\u63a5\u5230\u6765&#xff1b;\u76d1\u542c\u5957\u63a5\u5b57\u53ea\u9700\u8981\u5173\u6ce8\u8bfb\u4e8b\u4ef6&#xff0c;\u6240\u4ee5\u5b83\u7684_sendCb \u4e0e_exceptCb \u53ef\u4ee5\u76f4\u63a5\u7f6e\u7a7a&#xff08;nullptr&#xff09;\u3002<\/li>\n<li>\u5f53 Dispatcher \u68c0\u6d4b\u5230\u76d1\u542c\u5957\u63a5\u5b57\u7684\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u540e&#xff0c;\u5c31\u4f1a\u6267\u884c\u8be5 Connection \u5185\u7684_recvCb \u56de\u8c03&#xff0c;\u4e5f\u5c31\u662f Accepter&#xff0c;\u4ee5\u6b64\u83b7\u53d6\u65b0\u5efa\u7acb\u7684\u5e95\u5c42\u8fde\u63a5\u3002<\/li>\n<li>\u800c\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\u7684\u666e\u901a\u5957\u63a5\u5b57&#xff0c;\u5b83\u5bf9\u5e94\u7684 Connection \u5b9e\u4f8b\u4f1a\u628a_recvCb\u3001_sendCb\u3001_exceptCb \u5206\u522b\u7ed1\u5b9a\u4e3a Recver\u3001Sender\u3001Excepter\u3002<\/li>\n<li>\u4e00\u65e6 Dispatcher \u6355\u83b7\u5230\u8fd9\u7c7b\u5957\u63a5\u5b57\u7684\u5c31\u7eea\u4e8b\u4ef6&#xff0c;\u5c31\u4f1a\u6267\u884c Connection \u5bf9\u8c61\u4e0a\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u4e5f\u5c31\u662f Recver\u3001Sender\u3001Excepter\u3002<\/li>\n<hr \/>\n<p style=\"background-color:transparent\">A. Accepter<\/p>\n<p>Accepter \u56de\u8c03\u4e13\u95e8\u8d1f\u8d23\u5904\u7406\u65b0\u8fde\u63a5\u4e8b\u4ef6&#xff0c;\u6267\u884c\u903b\u8f91\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\u8c03\u7528\u5c01\u88c5\u597d\u7684 Accept \u51fd\u6570&#xff0c;\u62ff\u5230\u5e95\u5c42\u5b8c\u6210\u5efa\u7acb\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/li>\n<li>\u8c03\u7528 AddConnection \u51fd\u6570&#xff0c;\u628a\u65b0\u83b7\u53d6\u7684\u5957\u63a5\u5b57\u5305\u88c5\u6210 Connection \u5bf9\u8c61&#xff0c;\u4ea4\u7531\u670d\u52a1\u5668\u7edf\u4e00\u7ba1\u7406\u3002<\/li>\n<li>\u5b8c\u6210\u8fd9\u4e00\u6b65\u540e&#xff0c;\u5957\u63a5\u5b57\u4ee5\u53ca\u5b83\u9700\u8981\u76d1\u542c\u7684\u4e8b\u4ef6&#xff0c;\u5c31\u6ce8\u518c\u5230 Dispatcher \u4e8b\u4ef6\u5206\u53d1\u5668\u91cc\u4e86\u3002<\/li>\n<\/ul>\n<p>\u540e\u7eed Dispatcher \u505a\u4e8b\u4ef6\u6d3e\u53d1\u65f6&#xff0c;\u4f1a\u6301\u7eed\u76d1\u542c\u8fd9\u4e2a\u5957\u63a5\u5b57\u7684\u4e8b\u4ef6&#xff1b;\u4e00\u65e6\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u5c31\u81ea\u52a8\u6267\u884c\u8be5\u5957\u63a5\u5b57\u6240\u5c5e Connection \u5bf9\u8c61\u7ed1\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p>void Accepter(Connection *conn)<br \/>\n{<br \/>\n    \/\/ logMessage(DEBUG, &#034;Accepter been called&#034;);<br \/>\n    \/\/ \u4e00\u5b9a\u662flistensock\u5df2\u7ecf\u5c31\u7eea\u4e86&#xff0c;\u6b64\u6b21\u8bfb\u53d6\u4e0d\u4f1a\u963b\u585e<br \/>\n    while (true)<br \/>\n    {<br \/>\n        std::string clientip;<br \/>\n        uint16_t clientport;<br \/>\n        int accept_errno &#061; 0;<br \/>\n        \/\/ sock\u4e00\u5b9a\u662f\u975e\u5e38\u89c4\u7684IO sock<br \/>\n        int sock &#061; Sock::Accept(conn-&gt;_sock, &amp;clientip, &amp;clientport, &amp;accept_errno);<br \/>\n        if (sock &lt; 0)<br \/>\n        {<br \/>\n            if (accept_errno &#061;&#061; EAGAIN || accept_errno &#061;&#061; EWOULDBLOCK) break;<br \/>\n            else if (accept_errno &#061;&#061; EINTR) continue;   \/\/ \u6982\u7387\u975e\u5e38\u4f4e<br \/>\n            else<br \/>\n            {<br \/>\n                \/\/ accept\u5931\u8d25<br \/>\n                logMessage(WARNING, &#034;accept error, %d : %s&#034;, accept_errno, strerror(accept_errno));<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n        \/\/ \u5c06sock\u6258\u7ba1\u7ed9TcpServer<br \/>\n        if (sock &gt;&#061; 0)<br \/>\n        {<br \/>\n            AddConnection(sock, std::bind(&amp;TcpServer::Recver,  this, std::placeholders::_1),<br \/>\n                                std::bind(&amp;TcpServer::Sender,  this, std::placeholders::_1),<br \/>\n                                std::bind(&amp;TcpServer::Excepter, this, std::placeholders::_1));<br \/>\n            logMessage(DEBUG, &#034;accept client %s:%d success, add to epoll&amp;TcpServer success, sock: %d&#034;,<br \/>\n                       clientip.c_str(), clientport, sock);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8fd9\u91cc\u5b9e\u73b0\u7684\u662f ET \u6a21\u5f0f\u7684 epoll \u670d\u52a1\u5668&#xff0c;\u6240\u4ee5\u5728\u83b7\u53d6\u65b0\u8fde\u63a5\u65f6&#xff0c;\u9700\u8981\u5faa\u73af\u8c03\u7528 accept&#xff0c;\u540c\u65f6\u76d1\u542c\u5957\u63a5\u5b57\u5fc5\u987b\u914d\u7f6e\u4e3a\u975e\u963b\u585e\u3002<\/p>\n<ul>\n<li>ET \u6a21\u5f0f\u7684\u7279\u70b9\u662f&#xff1a;\u4ec5\u5f53\u5c31\u7eea\u4e8b\u4ef6\u72b6\u6001\u53d1\u751f\u53d8\u5316&#xff08;\u8fde\u63a5\u4ece\u65e0\u5230\u6709\u3001\u6570\u91cf\u7531\u5c11\u53d8\u591a&#xff09;\u624d\u4f1a\u5411\u4e0a\u901a\u77e5\u3002\u5982\u679c\u6ca1\u6709\u4e00\u6b21\u6027\u628a\u5185\u6838\u4e2d\u5168\u90e8\u5df2\u5efa\u7acb\u7684\u8fde\u63a5\u8bfb\u53d6\u5e72\u51c0&#xff0c;\u5e76\u4e14\u540e\u7eed\u4e0d\u518d\u4ea7\u751f\u65b0\u8fde\u63a5&#xff0c;\u90a3\u4e48\u5185\u6838\u91cc\u6b8b\u7559\u7684\u8fde\u63a5\u5c31\u4f1a\u6c38\u4e45\u4e22\u5931\u3002a<\/li>\n<li>\u5faa\u73af\u8c03\u7528 accept \u4f1a\u5e26\u6765\u4e00\u4e2a\u95ee\u9898&#xff1a;\u5f53\u5185\u6838\u6240\u6709\u8fde\u63a5\u90fd\u8bfb\u53d6\u5b8c\u6bd5\u540e&#xff0c;\u7ee7\u7eed\u8c03\u7528 accept \u5c31\u4f1a\u963b\u585e\u7b49\u5f85\u65b0\u8fde\u63a5\u3002\u56e0\u6b64\u76d1\u542c\u5957\u63a5\u5b57\u8981\u8bbe\u7f6e\u6210\u975e\u963b\u585e&#xff0c;\u5185\u6838\u65e0\u8fde\u63a5\u65f6 accept \u4f1a\u76f4\u63a5\u8fd4\u56de\u800c\u4e0d\u4f1a\u963b\u585e\u3002 \u540c\u65f6&#xff0c;accept \u8fd4\u56de\u7684\u65b0\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u4e5f\u9700\u8981\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e&#xff0c;\u9632\u6b62\u540e\u7eed\u5faa\u73af\u8c03\u7528 recv\u3001send \u65f6\u53d1\u751f\u963b\u585e\u3002<\/li>\n<li>\u4e0a\u8ff0\u975e\u963b\u585e\u914d\u7f6e&#xff0c;\u7edf\u4e00\u7531 AddConnection \u5185\u90e8\u7684 SetNonBlock \u51fd\u6570\u5b8c\u6210\u3002<\/li>\n<\/ul>\n<hr \/>\n<p><span style=\"color:#0d0016\">a.\u00a0\u8bbe\u7f6e\u975e\u963b\u585e<\/span><\/p>\n<p>\u8bbe\u7f6e\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a\u975e\u963b\u585e\u65f6&#xff0c;\u9700\u5148\u8c03\u7528 fcntl \u51fd\u6570\u83b7\u53d6\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u6587\u4ef6\u72b6\u6001\u6807\u8bb0&#xff0c;\u7136\u540e\u5728\u8be5\u6587\u4ef6\u72b6\u6001\u6807\u8bb0\u7684\u57fa\u7840\u4e0a\u6dfb\u52a0\u975e\u963b\u585e\u6807\u8bb0 O_NONBLOCK&#xff0c;\u6700\u540e\u8c03\u7528 fcntl \u51fd\u6570\u5bf9\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u72b6\u6001\u6807\u8bb0\u8fdb\u884c\u8bbe\u7f6e\u5373\u53ef\u3002<\/p>\n<p>static bool SetNonBlock(int sock)<br \/>\n    {<br \/>\n        int fl &#061; fcntl(sock, F_GETFL);<br \/>\n        if(fl &lt; 0) return false;<br \/>\n        fcntl(sock, F_SETFL, fl | O_NONBLOCK);<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>\u76d1\u542c\u5957\u63a5\u5b57\u5f00\u542f\u975e\u963b\u585e\u4e4b\u540e&#xff0c;\u5982\u679c\u5f53\u524d\u6ca1\u6709\u5c31\u7eea\u8fde\u63a5&#xff0c;accept \u4f1a\u4ee5\u9519\u8bef\u7684\u5f62\u5f0f\u8fd4\u56de\u3002\u6240\u4ee5\u5f53 accept \u8fd4\u56de\u503c\u5c0f\u4e8e 0 \u65f6&#xff0c;\u6211\u4eec\u9700\u8981\u8fdb\u4e00\u6b65\u5224\u65ad\u9519\u8bef\u7801&#xff1a;<\/p>\n<ul>\n<li>\u5982\u679c\u9519\u8bef\u7801\u662fEAGAIN\u6216EWOULDBLOCK&#xff0c;\u4ee3\u8868\u5185\u6838\u5df2\u7ecf\u6ca1\u6709\u5f85\u63a5\u6536\u7684\u8fde\u63a5&#xff0c;\u6240\u6709\u8fde\u63a5\u90fd\u8bfb\u53d6\u5b8c\u6210&#xff0c;\u76f4\u63a5\u8fd4\u56de 0&#xff0c;\u4ee3\u8868\u672c\u6b21 Accepter \u56de\u8c03\u6267\u884c\u6210\u529f\u3002<\/li>\n<li>\u5982\u679c\u9519\u8bef\u7801\u662fEINTR&#xff0c;\u8bf4\u660e accept \u8c03\u7528\u8fc7\u7a0b\u88ab\u7cfb\u7edf\u4fe1\u53f7\u6253\u65ad&#xff0c;\u9700\u8981\u7ee7\u7eed\u5faa\u73af\u8c03\u7528 accept \u5c1d\u8bd5\u83b7\u53d6\u8fde\u63a5\u3002<\/li>\n<li>\u5176\u4f59\u9519\u8bef\u7801\u5219\u4ee3\u8868 accept \u53d1\u751f\u4e86\u771f\u6b63\u7684\u5f02\u5e38&#xff0c;\u8fd4\u56de &#8211; 1&#xff0c;\u8868\u793a\u672c\u6b21 Accepter \u56de\u8c03\u6267\u884c\u5931\u8d25\u3002<\/li>\n<\/ul>\n<hr \/>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">accept\u3001recv\u3001send \u8fd9\u7c7b IO \u7cfb\u7edf\u8c03\u7528\u4e3a\u4ec0\u4e48\u4f1a\u88ab\u4fe1\u53f7\u4e2d\u65ad&#xff1f;<\/span><\/span><\/p>\n<p>\u5f53 IO \u7cfb\u7edf\u8c03\u7528\u51fa\u9519\u8fd4\u56de&#xff0c;\u5e76\u4e14\u9519\u8bef\u7801\u88ab\u7f6e\u4e3aEINTR&#xff0c;\u5c31\u4ee3\u8868\u672c\u6b21 IO \u8bfb\u5199\u64cd\u4f5c\u5728\u5b8c\u6210\u524d\u88ab\u4fe1\u53f7\u6253\u65ad\u3002\u7b80\u5355\u6765\u8bf4&#xff1a;\u7cfb\u7edf\u8c03\u7528\u9677\u5165\u5185\u6838\u4e4b\u540e\u3001\u8fd8\u6ca1\u8fd4\u56de\u7528\u6237\u6001\u4e4b\u524d&#xff0c;\u5185\u6838\u6536\u5230\u4e86\u4fe1\u53f7\u3002<\/p>\n<p>\u5185\u6838\u51c6\u5907\u4ece\u5185\u6838\u6001\u5207\u56de\u7528\u6237\u6001\u524d&#xff0c;\u4f1a\u68c0\u67e5\u672a\u51b3\u4fe1\u53f7\u96c6&#xff08;pending \u4f4d\u56fe&#xff09;\u3002\u5982\u679c\u5b58\u5728\u8fd8\u672a\u5904\u7406\u7684\u4fe1\u53f7&#xff0c;\u5185\u6838\u5c31\u4f1a\u4f18\u5148\u53bb\u5904\u7406\u8fd9\u4e2a\u4fe1\u53f7\u3002<\/p>\n<p>EINTR\u5c5e\u4e8e\u4e00\u79cd\u7279\u6b8a\u573a\u666f\u3002<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">IO \u64cd\u4f5c\u5206\u4e3a\u7b49\u5f85\u5c31\u7eea\u548c\u6570\u636e\u62f7\u8d1d\u4e24\u4e2a\u9636\u6bb5<\/span><\/span>&#xff0c;\u5176\u4e2d\u7b49\u5f85\u9636\u6bb5\u8017\u65f6\u901a\u5e38\u66f4\u957f&#xff0c;\u6b64\u65f6\u8fdb\u7a0b\u5904\u4e8e\u963b\u585e\u7b49\u5f85\u7684\u95f2\u7f6e\u72b6\u6001\u3002\u5982\u679c\u5728\u8fd9\u4e2a\u7b49\u5f85\u9636\u6bb5\u6536\u5230\u4fe1\u53f7&#xff0c;\u5185\u6838\u4f1a\u7acb\u523b\u6682\u505c\u672c\u6b21 IO \u8c03\u7528&#xff0c;\u8f6c\u800c\u53bb\u5904\u7406\u4fe1\u53f7&#xff0c;\u4e8e\u662f\u5c31\u4ea7\u751f\u4e86EINTR\u4e2d\u65ad\u9519\u8bef\u3002<\/p>\n<hr \/>\n<p><span style=\"color:#0d0016\">b. \u5199\u4e8b\u4ef6\u6309\u9700\u5f00\u542f<\/span><\/p>\n<p>Accepter \u63a5\u6536\u65b0\u8fde\u63a5\u5957\u63a5\u5b57&#xff0c;\u5e76\u628a\u5b83\u6ce8\u518c\u5230 Dispatcher \u65f6&#xff0c;\u4ec5\u6ce8\u518cEPOLLIN\u4e0eEPOLLET&#xff0c;\u4e5f\u5c31\u662f\u53ea\u8ba9 epoll \u76d1\u542c\u8be5\u5957\u63a5\u5b57\u7684\u8bfb\u4e8b\u4ef6\u3002<\/p>\n<p>\u4e0d\u6ce8\u518c\u5199\u4e8b\u4ef6\u7684\u539f\u56e0\u5f88\u7b80\u5355&#xff1a;\u6b64\u65f6\u6682\u65e0\u5f85\u53d1\u9001\u6570\u636e&#xff0c;\u6ca1\u5fc5\u8981\u8ba9 epoll \u6301\u7eed\u76d1\u542c\u5199\u5c31\u7eea\u4e8b\u4ef6\u3002\u901a\u5e38\u8bfb\u4e8b\u4ef6\u9ed8\u8ba4\u5f00\u542f&#xff0c;\u5199\u4e8b\u4ef6\u91c7\u7528\u6309\u9700\u5f00\u542f\u7684\u7b56\u7565&#xff1a;\u53ea\u6709\u5b58\u5728\u5f85\u53d1\u9001\u6570\u636e\u65f6&#xff0c;\u624d\u5411 epoll \u6ce8\u518c\u5199\u4e8b\u4ef6&#xff1b;\u4e00\u65e6\u6240\u6709\u6570\u636e\u53d1\u9001\u5b8c\u6210&#xff0c;\u5c31\u7acb\u523b\u5173\u95ed\u8be5\u5957\u63a5\u5b57\u7684\u5199\u4e8b\u4ef6\u76d1\u542c\u3002<\/p>\n<hr \/>\n<p>B. Recver<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">Recver \u56de\u8c03\u8d1f\u8d23\u5904\u7406\u8bfb\u4e8b\u4ef6&#xff0c;\u6267\u884c\u6d41\u7a0b\u5982\u4e0b<\/span><\/span>&#xff1a; \u5faa\u73af\u8c03\u7528 recv \u51fd\u6570\u8bfb\u53d6\u6570\u636e&#xff0c;\u5e76\u628a\u8bfb\u5230\u7684\u6570\u636e\u5b58\u5165\u5f53\u524d\u5957\u63a5\u5b57\u5bf9\u5e94\u7684 Connection \u5bf9\u8c61\u7684_inBuffer\u7f13\u51b2\u533a\u3002 \u5bf9_inBuffer\u5185\u7684\u6570\u636e\u505a\u62a5\u6587\u89e3\u6790\u62c6\u5206&#xff0c;\u53d6\u51fa\u5b8c\u6574\u62a5\u6587&#xff0c;\u672a\u8bfb\u5b8c\u7684\u6b8b\u7559\u6570\u636e\u7ee7\u7eed\u4fdd\u7559\u5728\u7f13\u51b2\u533a\u4e2d\u3002 \u8c03\u7528\u4e1a\u52a1\u5904\u7406\u51fd\u6570&#xff0c;\u5904\u7406\u89e3\u6790\u51fa\u6765\u7684\u5b8c\u6574\u62a5\u6587\u3002<\/p>\n<p>\u5f53 recv \u8fd4\u56de\u503c\u5c0f\u4e8e 0 \u65f6&#xff0c;\u9700\u8981\u5224\u65ad\u9519\u8bef\u7801&#xff1a;<\/p>\n<ul>\n<li>\u9519\u8bef\u7801\u4e3aEAGAIN\u6216EWOULDBLOCK&#xff1a;\u5185\u6838\u7f13\u51b2\u533a\u6570\u636e\u5df2\u7ecf\u5168\u90e8\u8bfb\u53d6\u5b8c\u6210&#xff1b;<\/li>\n<li>\u9519\u8bef\u7801\u4e3aEINTR&#xff1a;\u672c\u6b21\u8bfb\u53d6\u88ab\u4fe1\u53f7\u4e2d\u65ad&#xff0c;\u9700\u8981\u7ee7\u7eed\u5faa\u73af\u8c03\u7528 recv \u8bfb\u53d6\u6570\u636e&#xff1b;<\/li>\n<li>\u5176\u4f59\u60c5\u51b5&#xff1a;\u4ee3\u8868\u8bfb\u64cd\u4f5c\u53d1\u751f\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p>\u4e00\u65e6\u8bfb\u64cd\u4f5c\u51fa\u9519&#xff0c;\u76f4\u63a5\u89e6\u53d1\u8be5\u5957\u63a5\u5b57\u7ed1\u5b9a\u7684_exceptCb\u5f02\u5e38\u56de\u8c03&#xff0c;\u5728\u5f02\u5e38\u56de\u8c03\u5185\u90e8\u5173\u95ed\u5957\u63a5\u5b57\u3002<\/p>\n<hr \/>\n<p>a. \u62a5\u6587\u5207\u5272<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u62a5\u6587\u5207\u5272\u7684\u6838\u5fc3\u76ee\u7684\u662f\u89e3\u51b3 TCP \u7c98\u5305\u95ee\u9898&#xff0c;\u800c\u7c98\u5305\u7684\u5904\u7406\u65b9\u5f0f\u548c\u81ea\u5b9a\u4e49\u534f\u8bae\u5f3a\u76f8\u5173<\/span><\/span>\u3002 \u6211\u4eec\u9700\u8981\u6309\u7167\u534f\u8bae\u89c4\u5219\u62c6\u5206\u62a5\u6587&#xff0c;\u4f8b\u5982 UDP \u5e38\u91c7\u7528\u5b9a\u957f\u62a5\u5934\u52a0\u81ea\u63cf\u8ff0\u5b57\u6bb5\u7684\u65b9\u6848\u5b9e\u73b0\u62a5\u6587\u5206\u79bb\u3002 \u672c\u6587\u91cd\u70b9\u5728\u4e8e\u6f14\u793a\u5b8c\u6574\u7684\u6570\u636e\u5904\u7406\u6d41\u7a0b&#xff0c;\u4e3a\u964d\u4f4e\u590d\u6742\u5ea6&#xff0c;\u4e0d\u8bbe\u8ba1\u590d\u6742\u534f\u8bae&#xff0c;\u76f4\u63a5\u4f7f\u7528\u5b57\u7b26X\u4f5c\u4e3a\u62a5\u6587\u5206\u9694\u6807\u8bb0&#xff0c;\u6bcf\u4e00\u6761\u62a5\u6587\u7684\u672b\u5c3e\u90fd\u4ee5X\u6807\u8bc6\u62a5\u6587\u7ed3\u675f\u3002<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u62a5\u6587\u5207\u5272\u903b\u8f91\u5c31\u662f\u4ee5X\u4f5c\u4e3a\u5206\u9694\u7b26&#xff0c;\u5bf9_inBuffer\u5185\u7684\u6570\u636e\u8fdb\u884c\u62c6\u5206<\/span><\/span>\u3002 SplitMessage\u51fd\u6570\u8d1f\u8d23\u5b8c\u6210\u8fd9\u4e2a\u5de5\u4f5c&#xff1a;\u628a_inBuffer\u4e2d\u5207\u5206\u51fa\u7684\u5b8c\u6574\u62a5\u6587\u5b58\u5165 vector \u5bb9\u5668&#xff1b;\u90a3\u4e9b\u4e0d\u8db3\u4ee5\u7ec4\u6210\u5b8c\u6574\u62a5\u6587\u7684\u5269\u4f59\u6570\u636e&#xff0c;\u5219\u7ee7\u7eed\u4fdd\u7559\u5728_inBuffer\u7f13\u51b2\u533a\u91cc\u3002<\/p>\n<p>\/\/ \u8981\u628a\u4f20\u5165\u8fdb\u6765\u7684\u7f13\u51b2\u533a\u8fdb\u884c\u5207\u5206<br \/>\n\/\/ 1. buffer\u88ab\u5207\u8d70\u7684&#xff0c;\u540c\u65f6\u4e5f\u8981\u5c06\u5176\u4ecebuffer\u4e2d\u79fb\u9664<br \/>\n\/\/ 2. \u53ef\u80fd\u4f1a\u5b58\u5728\u591a\u4e2a\u62a5\u6587&#xff0c;\u591a\u4e2a\u62a5\u6587\u4f9d\u6b21\u653e\u5165out<br \/>\n\/\/ buffer: \u8f93\u5165\u8f93\u51fa\u578b\u53c2\u6570  out: \u8f93\u51fa\u578b\u53c2\u6570<br \/>\nvoid SpliteMessage(std::string &amp;buffer, std::vector&lt;std::string&gt; *out)<br \/>\n{<br \/>\n    while (true)<br \/>\n    {<br \/>\n        auto pos &#061; buffer.find(SEP);<br \/>\n        if (std::string::npos &#061;&#061; pos) break;<br \/>\n        std::string message &#061; buffer.substr(0, pos);<br \/>\n        buffer.erase(0, pos &#043; SEP_LEN);<br \/>\n        out-&gt;push_back(message);<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<p>b. \u4e1a\u52a1\u5904\u7406\u51fd\u6570<\/p>\n<p>\u5bf9\u5206\u5272\u5f97\u5230\u7684\u5b8c\u6574\u62a5\u6587\u6267\u884c\u53cd\u5e8f\u5217\u5316\u64cd\u4f5c\u3002 \u6267\u884c\u4e1a\u52a1\u903b\u8f91\u3002 \u4e1a\u52a1\u5904\u7406\u5b8c\u6210\u540e&#xff0c;\u7ec4\u88c5\u751f\u6210\u54cd\u5e94\u62a5\u6587\u3002 \u628a\u7ec4\u88c5\u597d\u7684\u54cd\u5e94\u62a5\u6587\u5199\u5165\u5f53\u524d Connection \u5bf9\u8c61\u7684_outBuffer\u8f93\u51fa\u7f13\u51b2\u533a&#xff0c;\u540c\u65f6\u5f00\u542f\u5957\u63a5\u5b57\u7684\u5199\u4e8b\u4ef6\u76d1\u542c\u3002<\/p>\n<p>\u540e\u7eed Dispatcher \u8fdb\u884c\u4e8b\u4ef6\u5206\u53d1\u65f6&#xff0c;\u5c31\u4f1a\u76d1\u542c\u8be5\u5957\u63a5\u5b57\u7684\u5199\u4e8b\u4ef6&#xff1b;\u4e00\u65e6\u5199\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u5c31\u4f1a\u89e6\u53d1 Connection \u7ed1\u5b9a\u7684\u5199\u56de\u8c03\u51fd\u6570&#xff0c;\u628a_outBuffer\u7f13\u51b2\u533a\u91cc\u7684\u54cd\u5e94\u6570\u636e\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>void NetCal(Connection *conn, std::string &amp;request)<br \/>\n{<br \/>\n    logMessage(DEBUG, &#034;NetCal been called, get request: %s&#034;, request.c_str());<\/p>\n<p>    \/\/ 1. \u53cd\u5e8f\u5217\u5316<br \/>\n    Request req;<br \/>\n    if(!req.Deserialized(request)) return;<\/p>\n<p>    \/\/ 2. \u4e1a\u52a1\u5904\u7406<br \/>\n    Response resp &#061; calculator(req);<\/p>\n<p>    \/\/ 3. \u5148\u5e8f\u5217\u5316&#xff0c;\u518d\u6784\u5efa\u5e94\u7b54<br \/>\n    std::string sendstr &#061; resp.Serialize();<br \/>\n    sendstr &#061; Encode(sendstr);<\/p>\n<p>    \/\/ 4. \u4ea4\u7ed9\u670d\u52a1\u5668conn<br \/>\n    conn-&gt;_outbuffer &#043;&#061; sendstr;<\/p>\n<p>    \/\/ 5. \u60f3\u529e\u6cd5\u8ba9\u5e95\u5c42\u7684TcpServer&#xff0c;\u8ba9\u5b83\u5f00\u59cb\u53d1\u9001<br \/>\n    \/\/ \/\/ a. \u9700\u8981\u6709\u5b8c\u6574\u7684\u53d1\u9001\u903b\u8f91<br \/>\n    \/\/ \/\/ b. \u89e6\u53d1\u53d1\u9001\u7684\u52a8\u4f5c&#xff0c;\u4e00\u65e6\u5f00\u542fEPOLLOUT&#xff0c;epoll\u4f1a\u81ea\u52a8\u7acb\u9a6c\u89e6\u53d1\u4e00\u6b21\u53d1\u9001\u4e8b\u4ef6\u5c31\u7eea&#xff0c;<br \/>\n    \/\/ \/\/    \u5982\u679c\u540e\u7eed\u4fdd\u6301\u53d1\u9001\u7684\u5f00\u542f&#xff0c;epoll\u4f1a\u4e00\u76f4\u53d1\u9001<br \/>\n    conn-&gt;_tsvr-&gt;EnableReadWrite(conn, true, true);<br \/>\n}<\/p>\n<hr \/>\n<p>c.\u00a0\u534f\u8bae\u5b9a\u5236<\/p>\n<p>\u534f\u8bae\u90e8\u5206&#xff1a;Encode \/ Request \/ Response<\/p>\n<p>std::string Encode(std::string &amp;s)<br \/>\n{<br \/>\n    return s &#043; SEP;<br \/>\n}<\/p>\n<p>class Request<br \/>\n{<br \/>\npublic:<br \/>\n    std::string Serialize()<br \/>\n    {<br \/>\n        std::string str;<br \/>\n        str &#061; std::to_string(x_);<br \/>\n        str &#043;&#061; SPACE;<br \/>\n        str &#043;&#061; op_;<br \/>\n        str &#043;&#061; SPACE;<br \/>\n        str &#043;&#061; std::to_string(y_);<br \/>\n        return str;<br \/>\n    }<br \/>\n    bool Deserialized(const std::string &amp;str)<br \/>\n    {<br \/>\n        std::size_t left &#061; str.find(SPACE);<br \/>\n        if (left &#061;&#061; std::string::npos) return false;<br \/>\n        std::size_t right &#061; str.rfind(SPACE);<br \/>\n        if (right &#061;&#061; std::string::npos) return false;<br \/>\n        x_ &#061; atoi(str.substr(0, left).c_str());<br \/>\n        y_ &#061; atoi(str.substr(right &#043; SPACE_LEN).c_str());<br \/>\n        if (left &#043; SPACE_LEN &gt; str.size()) return false;<br \/>\n        else op_ &#061; str[left &#043; SPACE_LEN];<br \/>\n        return true;<br \/>\n    }<br \/>\npublic:<br \/>\n    Request(){}<br \/>\n    Request(int x, int y, char op)<br \/>\n        : x_(x)<br \/>\n        , y_(y)<br \/>\n        , op_(op)<br \/>\n    {}<br \/>\n    ~Request(){}<br \/>\npublic:<br \/>\n    int x_;<br \/>\n    int y_;<br \/>\n    char op_; \/\/ &#039;&#043;&#039;, &#039;-&#039;, &#039;*&#039;, &#039;\/&#039;, &#039;%&#039;<br \/>\n};<\/p>\n<p>class Response<br \/>\n{<br \/>\npublic:<br \/>\n    std::string Serialize()<br \/>\n    {<br \/>\n        std::string s;<br \/>\n        s &#061; std::to_string(code_);<br \/>\n        s &#043;&#061; SPACE;<br \/>\n        s &#043;&#061; std::to_string(result_);<br \/>\n        return s;<br \/>\n    }<br \/>\n    bool Deserialized(const std::string &amp;s)<br \/>\n    {<br \/>\n        std::size_t pos &#061; s.find(SPACE);<br \/>\n        if (pos &#061;&#061; std::string::npos) return false;<br \/>\n        code_ &#061; atoi(s.substr(0, pos).c_str());<br \/>\n        result_ &#061; atoi(s.substr(pos &#043; SPACE_LEN).c_str());<br \/>\n        return true;<br \/>\n    }<br \/>\npublic:<br \/>\n    Response(){}<br \/>\n    Response(int result, int code)<br \/>\n        : result_(result)<br \/>\n        , code_(code)<br \/>\n    {}<br \/>\n    ~Response(){}<br \/>\npublic:<br \/>\n    int result_; \/\/ \u8ba1\u7b97\u7ed3\u679c<br \/>\n    int code_;   \/\/ \u8ba1\u7b97\u7ed3\u679c\u7684\u72b6\u6001\u7801<br \/>\n};<\/p>\n<p>\u4ee3\u7801\u89e3\u6790<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5b9e\u73b0\u7b80\u6613\u7684\u8bf7\u6c42\u3001\u54cd\u5e94\u62a5\u6587\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\u903b\u8f91&#xff0c;\u914d\u5408\u524d\u9762 Reactor \u6d88\u606f\u961f\u5217\u7684\u62a5\u6587\u5207\u5272\u6a21\u5757\u4f7f\u7528\u3002<\/p>\n<p><span style=\"color:#fe2c24\">1.Encode \u51fd\u6570&#xff1a;<\/span>\u5728\u5e8f\u5217\u5316\u540e\u7684\u62a5\u6587\u672b\u5c3e\u8ffd\u52a0SEP\u5206\u9694\u7b26&#xff0c;\u6807\u8bb0\u62a5\u6587\u8fb9\u754c&#xff0c;\u7528\u4e8eSplitMessage\u5207\u5272\u62a5\u6587&#xff0c;\u89e3\u51b3 TCP \u7c98\u5305\u3002<\/p>\n<p><span style=\"color:#fe2c24\">2.Request \u7c7b&#xff1a;<\/span>\u5c01\u88c5\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u56db\u5219\u8fd0\u7b97\u8bf7\u6c42&#xff0c;\u6210\u5458\u5305\u542b\u4e24\u4e2a\u64cd\u4f5c\u6570 x_\u3001y_\u548c\u8fd0\u7b97\u7b26 op_\u3002<\/p>\n<ul>\n<li>Serialize&#xff1a;\u628a\u5bf9\u8c61\u6210\u5458\u62fc\u63a5\u6210x op y\u683c\u5f0f\u5b57\u7b26\u4e32&#xff0c;\u5b8c\u6210\u5e8f\u5217\u5316&#xff0c;\u7528\u4e8e\u7f51\u7edc\u53d1\u9001\u3002<\/li>\n<li>Deserialized&#xff1a;\u63a5\u6536\u62a5\u6587\u5b57\u7b26\u4e32&#xff0c;\u5229\u7528find\u548crfind\u67e5\u627e\u7a7a\u683c&#xff0c;\u62c6\u5206\u89e3\u6790\u51fa\u4e24\u4e2a\u6570\u5b57\u548c\u8fd0\u7b97\u7b26&#xff0c;\u586b\u5145\u5230\u5bf9\u8c61\u6210\u5458&#xff0c;\u89e3\u6790\u5931\u8d25\u8fd4\u56de false\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\">3.Response \u7c7b&#xff1a;<\/span>\u5c01\u88c5\u670d\u52a1\u5668\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u7684\u5e94\u7b54\u6570\u636e&#xff0c;\u5305\u542b\u8fd0\u7b97\u7ed3\u679c result_\u4e0e\u72b6\u6001\u7801 code_\u3002<\/p>\n<ul>\n<li>Serialize&#xff1a;\u5c06\u72b6\u6001\u7801\u3001\u8fd0\u7b97\u7ed3\u679c\u62fc\u63a5\u4e3acode result\u5b57\u7b26\u4e32&#xff0c;\u5e8f\u5217\u5316\u8f93\u51fa\u3002<\/li>\n<li>Deserialized&#xff1a;\u67e5\u627e\u7a7a\u683c\u5206\u5272\u5b57\u7b26\u4e32&#xff0c;\u89e3\u6790\u5f97\u5230\u72b6\u6001\u7801\u548c\u7ed3\u679c&#xff0c;\u8fd8\u539f\u54cd\u5e94\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u6574\u4f53\u4e1a\u52a1\u94fe\u8def&#xff1a;Recver \u8bfb\u53d6\u6570\u636e &#8211;&gt;\u00a0SplitMessage \u5207\u51fa\u5b8c\u6574\u62a5\u6587 &#8211;&gt;\u00a0Request \u53cd\u5e8f\u5217\u5316 &#8211;&gt;\u00a0\u4e1a\u52a1\u8ba1\u7b97 &#8211;&gt;\u00a0Response \u5bf9\u8c61\u7ec4\u88c5 &#8211;&gt;\u00a0Response \u5e8f\u5217\u5316 &#8211;&gt;\u00a0Encode \u8ffd\u52a0\u62a5\u6587\u5206\u9694\u7b26 &#8211;&gt;\u00a0\u5199\u5165\u8f93\u51fa\u7f13\u51b2\u533a_outBuffer&#xff0c;\u5f00\u542f\u5199\u4e8b\u4ef6 &#8211;&gt;\u00a0Dispatcher \u89e6\u53d1 Sender \u56de\u8c03\u53d1\u9001\u6570\u636e\u3002<\/p>\n<hr \/>\n<p><span style=\"color:#0d0016\">C. Sender<\/span><\/p>\n<ul>\n<li>\u5faa\u73af\u8c03\u7528 send \u51fd\u6570\u53d1\u9001\u6570\u636e&#xff0c;\u5e76\u5c06\u53d1\u9001\u51fa\u53bb\u7684\u6570\u636e\u4ece\u8be5\u5957\u63a5\u5b57\u5bf9\u5e94 Connection \u7ed3\u6784\u7684 _outBuffer \u4e2d\u5220\u9664\u3002<\/li>\n<li>\u82e5\u5faa\u73af\u8c03\u7528 send \u51fd\u6570\u540e\u8be5\u5957\u63a5\u5b57\u5bf9\u5e94\u7684 _outBuffer \u4e2d\u7684\u6570\u636e\u88ab\u5168\u90e8\u53d1\u9001&#xff0c;\u6b64\u65f6\u5c31\u9700\u8981\u5c06\u8be5\u5957\u63a5\u5b57\u5bf9\u5e94\u7684\u5199\u4e8b\u4ef6\u5173\u95ed&#xff0c;\u56e0\u4e3a\u5df2\u6ca1\u6709\u8981\u53d1\u9001\u7684\u6570\u636e\u4e86&#xff0c;\u82e5 _outBuffer \u4e2d\u7684\u6570\u636e\u8fd8\u6709\u5269\u4f59&#xff0c;\u90a3\u4e48\u8be5\u5957\u63a5\u5b57\u5bf9\u5e94\u7684\u5199\u4e8b\u4ef6\u5c31\u5e94\u7ee7\u7eed\u6253\u5f00\u3002<\/li>\n<\/ul>\n<p>\/\/ \u6700\u5f00\u59cb\u65f6&#xff0c;conn\u662f\u6ca1\u6709\u88ab\u89e6\u53d1\u7684<br \/>\nvoid Sender(Connection *conn)<br \/>\n{<br \/>\n    while(true)<br \/>\n    {<br \/>\n        ssize_t n &#061; send(conn-&gt;_sock, conn-&gt;_outbuffer.c_str(), conn-&gt;_outbuffer.size(), 0);<br \/>\n        if(n &gt; 0)<br \/>\n        {<br \/>\n            conn-&gt;_outbuffer.erase(0, n);<br \/>\n            if(conn-&gt;_outbuffer.empty()) break;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            if(errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK) break;<br \/>\n            else if(errno &#061;&#061; EINTR) continue;<br \/>\n            else<br \/>\n            {<br \/>\n                logMessage(ERROR, &#034;send error, %d : %s&#034;, errno, strerror(errno));<br \/>\n                conn-&gt;_except_cb(conn);<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u867d\u7136\u4e0d\u80fd\u786e\u5b9a\u662f\u5426\u53d1\u5b8c\u4e86&#xff0c;\u4f46\u53ef\u4ee5\u4fdd\u8bc1&#xff1a;\u5982\u679c\u6ca1\u6709\u51fa\u9519&#xff0c;\u8981\u4e48\u53d1\u5b8c\u4e86&#xff0c;\u8981\u4e48\u53d1\u9001\u6761\u4ef6\u4e0d\u6ee1\u8db3&#xff0c;\u4e0b\u6b21\u518d\u53d1\u9001<br \/>\n    if(conn-&gt;_outbuffer.empty()) EnableReadWrite(conn, true, false);<br \/>\n    else EnableReadWrite(conn, true, true);<br \/>\n}<\/p>\n<p>Sender \u662f\u5199\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570&#xff0c;\u8d1f\u8d23\u5c06_outbuffer\u7f13\u51b2\u533a\u7684\u6570\u636e\u901a\u8fc7\u975e\u963b\u585e send \u5faa\u73af\u53d1\u9001\u3002\u53d1\u9001\u6210\u529f\u5219\u4ece\u7f13\u51b2\u533a\u79fb\u9664\u5df2\u53d1\u9001\u5b57\u8282&#xff1b;\u9047\u5230EAGAIN\/EWOULDBLOCK\u4ee3\u8868\u5185\u6838\u7f13\u51b2\u533a\u5df2\u6ee1&#xff0c;\u505c\u6b62\u53d1\u9001\u7b49\u5f85\u4e0b\u6b21\u5199\u4e8b\u4ef6&#xff1b;EINTR\u4ee3\u8868\u88ab\u4fe1\u53f7\u4e2d\u65ad&#xff0c;\u7ee7\u7eed\u91cd\u8bd5\u3002\u53d1\u751f\u5176\u4ed6\u9519\u8bef\u65f6\u89e6\u53d1\u5f02\u5e38\u56de\u8c03\u5173\u95ed\u8fde\u63a5\u3002\u51fd\u6570\u672b\u5c3e\u6839\u636e\u7f13\u51b2\u533a\u662f\u5426\u4e3a\u7a7a&#xff0c;\u52a8\u6001\u8c03\u6574 epoll \u76d1\u542c&#xff1a;\u7f13\u51b2\u533a\u4e3a\u7a7a\u5c31\u5173\u95ed\u5199\u4e8b\u4ef6&#xff0c;\u9632\u6b62\u7a7a\u8f6e\u8be2&#xff1b;\u7f13\u51b2\u533a\u8fd8\u6709\u6570\u636e&#xff0c;\u5219\u7ee7\u7eed\u76d1\u542c\u5199\u4e8b\u4ef6&#xff0c;\u7b49\u5f85\u4e0b\u6b21\u5c31\u7eea\u7ee7\u7eed\u53d1\u9001\u3002<\/p>\n<hr \/>\n<p>D.\u00a0Excepter<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u5f02\u5e38\u4e8b\u4ef6\u5c31\u7eea\u7684\u5957\u63a5\u5b57\u4e0d\u505a\u8fc7\u591a\u5904\u7406&#xff0c;\u8c03\u7528 close \u51fd\u6570\u5c06\u8be5\u5957\u63a5\u5b57\u5173\u95ed\u5373\u53ef\u3002<\/li>\n<li>\u4f46\u5728\u5173\u95ed\u8be5\u5957\u63a5\u5b57\u524d&#xff0c;\u9700\u5148\u5c06\u8be5\u5957\u63a5\u5b57\u4ece epoll \u6a21\u578b\u4e2d\u5220\u9664&#xff0c;\u5e76\u53d6\u6d88\u8be5\u5957\u63a5\u5b57\u4e0e\u5176\u5bf9\u5e94\u7684 Connection \u7ed3\u6784\u7684\u6620\u5c04\u5173\u7cfb\u3002<\/li>\n<li>\u91ca\u653e Connection \u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>void Excepter(Connection *conn)<br \/>\n{<br \/>\n    if(!IsConnectionExists(conn-&gt;_sock)) return;<br \/>\n    \/\/ 1. \u4eceepoll\u4e2d\u79fb\u9664<br \/>\n    bool res &#061; _poll.DelFromEpoll(conn-&gt;_sock);<br \/>\n    assert(res); \/\/ \u8981\u8fdb\u884c\u5224\u65ad<br \/>\n    \/\/ 2. \u4eceunorder_map\u4e2d\u79fb\u9664<br \/>\n    _connections.erase(conn-&gt;_sock);<br \/>\n    \/\/ 3. close(sock);<br \/>\n    close(conn-&gt;_sock);<br \/>\n    \/\/ 4. delete conn;<br \/>\n    delete conn;<\/p>\n<p>    logMessage(DEBUG, &#034;Excepter \u56de\u6536\u5b8c\u6bd5&#xff0c;\u6240\u6709\u7684\u5f02\u5e38\u60c5\u51b5&#034;);<br \/>\n}<\/p>\n<p>\u4ee3\u7801\u89e3\u6790&#xff1a;<\/p>\n<ul>\n<li>Excepter \u662f\u5f02\u5e38\u56de\u8c03\u51fd\u6570&#xff0c;\u5f53\u8fde\u63a5\u53d1\u751f\u8bfb\u5199\u9519\u8bef\u65f6&#xff0c;\u7528\u6765\u5b89\u5168\u56de\u6536\u8fde\u63a5\u8d44\u6e90\u3002<\/li>\n<li>\u5148\u5224\u65ad\u8fde\u63a5\u662f\u5426\u8fd8\u5b58\u5728&#xff0c;\u5982\u679c\u4e0d\u5b58\u5728\u76f4\u63a5\u8fd4\u56de&#xff0c;\u9632\u6b62\u91cd\u590d\u56de\u6536\u3002<\/li>\n<li>\u5c06\u5957\u63a5\u5b57\u4ece epoll \u5b9e\u4f8b\u4e2d\u5220\u9664&#xff0c;epoll \u4e0d\u518d\u76d1\u542c\u8be5 fd \u4e0a\u7684\u4e8b\u4ef6&#xff1b;assert\u7528\u6765\u8c03\u8bd5\u9636\u6bb5\u6821\u9a8c\u5220\u9664\u662f\u5426\u6210\u529f\u3002<\/li>\n<li>\u4ece\u4fdd\u5b58\u6240\u6709\u8fde\u63a5\u7684unordered_map&#xff08;_connections&#xff09;\u4e2d\u64e6\u9664\u8fd9\u6761\u8fde\u63a5\u8bb0\u5f55\u3002<\/li>\n<li>\u5173\u95ed socket \u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u91ca\u653e\u64cd\u4f5c\u7cfb\u7edf\u5c42\u9762\u7684\u5957\u63a5\u5b57\u8d44\u6e90\u3002<\/li>\n<li>delete conn\u91ca\u653e\u5806\u4e0a\u521b\u5efa\u7684 Connection \u5bf9\u8c61\u5185\u5b58&#xff0c;\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<li>\u6253\u5370\u8c03\u8bd5\u65e5\u5fd7&#xff0c;\u6807\u8bb0\u672c\u6b21\u8fde\u63a5\u8d44\u6e90\u56de\u6536\u5b8c\u6210\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u4f5c\u7528&#xff1a;\u4e0d\u7ba1\u662f\u8bfb\u5f02\u5e38\u3001\u5199\u5f02\u5e38\u8fd8\u662f\u5bf9\u7aef\u5173\u95ed\u8fde\u63a5&#xff0c;\u90fd\u4f1a\u8d70\u5230\u8fd9\u4e2a\u56de\u8c03&#xff0c;\u7edf\u4e00\u5b8c\u6210\u8d44\u6e90\u6e05\u7406&#xff0c;\u907f\u514d fd \u6cc4\u9732\u3001\u5185\u5b58\u6cc4\u9732\u3002<\/span><\/span><\/p>\n<hr \/>\n<h4>&#xff08;5&#xff09;Socket \u5957\u63a5\u5b57<\/h4>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cerrno&gt;<br \/>\n#include &lt;cassert&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;ctype.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<\/p>\n<p>class Sock<br \/>\n{<br \/>\nprivate:<br \/>\n    const static int gbacklog &#061; 10;<br \/>\npublic:<br \/>\n    Sock() {}<br \/>\n    static int Socket()<br \/>\n    {<br \/>\n        int listensock &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n        if (listensock &lt; 0)<br \/>\n        {<br \/>\n            exit(2);<br \/>\n        }<br \/>\n        int opt &#061; 1;<br \/>\n        setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &amp;opt, sizeof(opt));<br \/>\n        return listensock;<br \/>\n    }<\/p>\n<p>    static void Bind(int sock, uint16_t port, std::string ip &#061; &#034;0.0.0.0&#034;)<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        inet_pton(AF_INET, ip.c_str(), &amp;local.sin_addr);<br \/>\n        if (bind(sock, (struct sockaddr *)&amp;local, sizeof(local)) &lt; 0)<br \/>\n        {<br \/>\n            exit(3);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    static void Listen(int sock)<br \/>\n    {<br \/>\n        if (listen(sock, gbacklog) &lt; 0)<br \/>\n        {<br \/>\n            exit(4);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    static int Accept(int listensock, std::string *ip, uint16_t *port, int *accept_errno)<br \/>\n    {<br \/>\n        struct sockaddr_in src;<br \/>\n        socklen_t len &#061; sizeof(src);<br \/>\n        *accept_errno &#061; 0;<br \/>\n        int servicesock &#061; accept(listensock, (struct sockaddr *)&amp;src, &amp;len);<br \/>\n        if (servicesock &lt; 0)<br \/>\n        {<br \/>\n            *accept_errno &#061; errno;<br \/>\n            return -1;<br \/>\n        }<br \/>\n        if(port) *port &#061; ntohs(src.sin_port);<br \/>\n        if(ip) *ip &#061; inet_ntoa(src.sin_addr);<br \/>\n        return servicesock;<br \/>\n    }<\/p>\n<p>    static bool Connect(int sock, const std::string &amp;server_ip, const uint16_t &amp;server_port)<br \/>\n    {<br \/>\n        struct sockaddr_in server;<br \/>\n        memset(&amp;server, 0, sizeof(server));<br \/>\n        server.sin_family &#061; AF_INET;<br \/>\n        server.sin_port &#061; htons(server_port);<br \/>\n        server.sin_addr.s_addr &#061; inet_addr(server_ip.c_str());<br \/>\n        if(connect(sock, (struct sockaddr*)&amp;server, sizeof(server)) &#061;&#061; 0) return true;<br \/>\n        else return false;<br \/>\n    }<\/p>\n<p>    static bool SetNonBlock(int sock)<br \/>\n    {<br \/>\n        int fl &#061; fcntl(sock, F_GETFL);<br \/>\n        if(fl &lt; 0) return false;<br \/>\n        fcntl(sock, F_SETFL, fl | O_NONBLOCK);<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    ~Sock() {}<br \/>\n};<\/p>\n<p>sock\u662f\u5c01\u88c5 socket \u57fa\u7840\u64cd\u4f5c\u7684\u5de5\u5177\u7c7b&#xff0c;\u5168\u90e8\u4e3a\u9759\u6001\u65b9\u6cd5&#xff0c;\u7edf\u4e00\u5c01\u88c5 TCP \u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u7840\u63a5\u53e3&#xff0c;\u63d0\u4f9b\u521b\u5efa\u5957\u63a5\u5b57\u3001\u7ed1\u5b9a\u3001\u76d1\u542c\u3001\u63a5\u53d7\u8fde\u63a5\u3001\u5ba2\u6237\u7aef\u8fde\u63a5\u3001\u8bbe\u7f6e\u975e\u963b\u585e\u7b49\u80fd\u529b\u3002<\/p>\n<ul>\n<li>Socket()&#xff1a;\u521b\u5efa TCP \u76d1\u542c\u5957\u63a5\u5b57&#xff0c;\u5e76\u4e14\u8bbe\u7f6e\u7aef\u53e3\u590d\u7528\u4e0e\u5730\u5740\u590d\u7528&#xff0c;\u670d\u52a1\u7aef\u91cd\u542f\u53ef\u4ee5\u5feb\u901f\u590d\u7528\u7aef\u53e3\u3002\u5931\u8d25\u76f4\u63a5\u9000\u51fa\u7a0b\u5e8f\u3002<\/li>\n<li>Bind()&#xff1a;\u586b\u5145\u672c\u673a\u5730\u5740\u7ed3\u6784\u4f53&#xff0c;\u8c03\u7528 bind \u5c06 socket \u7ed1\u5b9a\u5230\u6307\u5b9a IP \u4e0e\u7aef\u53e3\u3002\u9ed8\u8ba4\u7ed1\u5b9a0.0.0.0&#xff0c;\u76d1\u542c\u672c\u673a\u6240\u6709\u7f51\u5361\u3002<\/li>\n<li>Listen()&#xff1a;\u5f00\u542f\u76d1\u542c&#xff0c;gbacklog \u4e3a 10&#xff0c;\u8bbe\u7f6e TCP \u534a\u8fde\u63a5\u961f\u5217\u957f\u5ea6\u3002<\/li>\n<li>Accept()&#xff1a;\u963b\u585e\u63a5\u53d7\u65b0\u8fde\u63a5&#xff0c;\u8f93\u51fa\u5bf9\u7aef IP\u3001\u7aef\u53e3&#xff0c;\u540c\u65f6\u5e26\u56de accept \u8c03\u7528\u7684\u9519\u8bef\u7801&#xff1b;\u8fd4\u56de\u65b0\u8fde\u63a5\u7684 socket \u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n<li>Connect()&#xff1a;\u5ba2\u6237\u7aef\u4f7f\u7528&#xff0c;\u5411\u6307\u5b9a IP \u7aef\u53e3\u53d1\u8d77 TCP \u8fde\u63a5&#xff0c;\u8fde\u63a5\u6210\u529f\u8fd4\u56de true\u3002<\/li>\n<li>SetNonBlock()&#xff1a;\u901a\u8fc7fcntl\u628a\u6307\u5b9a socket \u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f&#xff0c;\u8fd9\u662f Reactor \u9ad8\u5e76\u53d1\u6a21\u578b\u7684\u57fa\u7840\u3002<\/li>\n<\/ul>\n<p>\u8bf4\u660e&#xff1a;\u8fd9\u4e2a\u5de5\u5177\u7c7b\u662f\u6574\u4e2a Reactor \u6d88\u606f\u961f\u5217\u5e95\u5c42\u7f51\u7edc\u57fa\u7840&#xff0c;Sock::SetNonBlock\u628a\u6240\u6709\u8fde\u63a5 socket \u8bbe\u7f6e\u975e\u963b\u585e&#xff0c;\u914d\u5408 epoll &#043; \u56db\u4e2a\u56de\u8c03\u51fd\u6570&#xff08;Accepter\u3001Recver\u3001Sender\u3001Excepter&#xff09;\u5b9e\u73b0\u4e8b\u4ef6\u9a71\u52a8\u670d\u52a1\u5668<\/p>\n<hr \/>\n<h4>&#xff08;6&#xff09;\u4ee3\u7801\u6f14\u793a<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"2385\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002629-6ac834b540278.png\" width=\"1045\" \/><\/p>\n<hr \/>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1716\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002630-6ac834b65c089.png\" width=\"980\" \/><\/p>\n<hr \/>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1253\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002631-6ac834b7152f7.png\" width=\"889\" \/><\/p>\n<hr \/>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1459\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002631-6ac834b77bbf7.png\" width=\"796\" \/><\/p>\n<hr \/>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"3028\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002632-6ac834b80c2aa.png\" width=\"882\" \/><\/p>\n<hr \/>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1328\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002634-6ac834ba1bc8b.png\" width=\"921\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"6020\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002634-6ac834ba9a4ea.png\" width=\"1238\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"235\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002636-6ac834bce9979.png\" width=\"740\" \/><\/p>\n<hr \/>\n<h4 style=\"background-color:transparent\">&#xff08;7&#xff09;\u670d\u52a1\u5668\u6d4b\u8bd5<\/h4>\n<p>\u5ba2\u6237\u7aef\u5efa\u7acb\u8fde\u63a5\u540e&#xff0c;\u670d\u52a1\u7aef\u4f1a\u4e3a\u8be5\u8fde\u63a5\u5206\u914d 5 \u53f7\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;4 \u53f7\u6587\u4ef6\u63cf\u8ff0\u7b26\u7531 epoll \u5360\u7528\u3002\u5ba2\u6237\u7aef\u53ef\u5411\u670d\u52a1\u7aef\u63d0\u4ea4\u591a\u9879\u7b80\u5355\u8ba1\u7b97\u4efb\u52a1&#xff0c;\u5148\u8fdb\u884c\u8fd0\u884c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"245\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002637-6ac834bd05001.png\" width=\"1760\" \/><\/p>\n<p>\u8fd9\u4e2a\u662f\u4e00\u6761\u6761\u7684\u53d1\u9001&#xff0c;\u4f46\u5728\u771f\u5b9e\u7684\u4e1a\u52a1\u573a\u666f\u4e2d&#xff0c;\u80af\u5b9a\u5f88\u591a\u6761\u6570\u636e\u4e00\u8d77\u53d1\u9001&#xff0c;\u6211\u4eec\u7ee7\u7eed\u6765\u6f14\u793a&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"799\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002637-6ac834bd35b74.png\" width=\"1834\" \/><\/p>\n<p>\u4e00\u6b21\u53d1\u5f88\u5bf9\u6761&#xff0c;\u6f14\u793a\u7c98\u5305\u95ee\u9898&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"551\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002637-6ac834bdee704.png\" width=\"1800\" \/><\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u4f1a\u53d1\u73b09&#043;\u8fd9\u4e2a\u8ba1\u7b97\u8868\u8fbe\u5f0f\u5b50\u6ca1\u6709\u663e\u793a\u51fa\u6765<\/p>\n<p>\u65e5\u5fd7\u53ef\u4ee5\u770b\u5230&#xff0c;\u670d\u52a1\u7aef\u7b2c\u4e00\u6b21 recv \u53ea\u8bfb\u5230 1 \u6761\u62a5\u6587&#xff08;1 &#043; 1&#xff09;&#xff0c;\u7b2c\u4e8c\u6b21\u5374\u4e00\u53e3\u6c14\u8bfb\u5230\u4e86 2 \u6761&#xff08;3 * 8 \u548c 8 \/ 0&#xff09;\u2014\u2014\u8fd9\u5c31\u662f&#034;\u7c98\u5305&#034;\u3002\u6839\u672c\u539f\u56e0\u5728\u4e8e TCP \u662f\u5b57\u8282\u6d41&#xff0c;\u4e0d\u4fdd\u8bc1\u6d88\u606f\u8fb9\u754c&#xff1a;recv \u4e00\u6b21\u80fd\u8bfb\u5230\u591a\u5c11&#xff0c;\u53ea\u53d6\u51b3\u4e8e\u8c03\u7528\u5b83\u7684\u90a3\u4e00\u523b\u5185\u6838\u63a5\u6536\u7f13\u51b2\u533a\u91cc\u79ef\u4e86\u591a\u5c11\u5b57\u8282&#xff0c;\u4e0e\u53d1\u9001\u65b9\u53d1\u4e86\u51e0\u6b21\u65e0\u5173\u3002\u800c\u670d\u52a1\u7aef\u5e76\u6ca1\u6709\u628a\u8fd9\u4e24\u6761\u5f53\u6210\u4e00\u6761\u5904\u7406\u2014\u2014_inbuffer \u5148\u628a\u6570\u636e\u6512\u4e0b\u6765&#xff0c;SpliteMessage \u518d\u6309 \\\\r\\\\n \u9010\u6761\u5207\u5206&#xff0c;\u6700\u7ec8 NetCal \u88ab\u51c6\u786e\u8c03\u7528\u4e86 2 \u6b21\u3002\u8fd9\u6b63\u662f _inbuffer &#043; \u5206\u9694\u7b26\u5faa\u73af\u5207\u5206\u5b58\u5728\u7684\u610f\u4e49&#xff1a;<span style=\"color:#fe2c24\"><span style=\"background-color:#ffd900\">\u7f13\u51b2\u533a\u8d1f\u8d23&#034;\u6512&#034;&#xff0c;\u5faa\u73af\u8d1f\u8d23&#034;\u5207&#034;<\/span><\/span>\u3002<\/p>\n<hr \/>\n<p>\u7531\u4e8e\u4f7f\u7528\u4e86\u591a\u8def\u8f6c\u63a5\u6280\u672f&#xff0c;\u867d\u7136 epoll \u670d\u52a1\u5668\u662f\u4e00\u4e2a\u5355\u8fdb\u7a0b\u7684\u670d\u52a1\u5668&#xff0c;\u4f46\u5374\u53ef\u540c\u65f6\u4e3a\u591a\u4e2a\u5ba2\u6237\u7aef\u63d0\u4f9b\u670d\u52a1\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"741\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002638-6ac834be5acd7.png\" width=\"1805\" \/><\/p>\n<hr \/>\n<h4>&#xff08;8&#xff09;\u603b\u7ed3<\/h4>\n<p>\u57fa\u4e8e IO \u591a\u8def\u8f6c\u63a5&#xff0c;\u5f53\u68c0\u6d4b\u5230\u4e8b\u4ef6\u5c31\u7eea\u540e&#xff0c;\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u6267\u884c\u4e1a\u52a1\u903b\u8f91\u7684\u7f16\u7a0b\u6a21\u578b&#xff0c;\u53eb\u505a<span style=\"color:#fe2c24\">\u53cd\u5e94\u5806&#xff08;Reactor&#xff09;\u6a21\u5f0f<\/span>\u3002\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684 TcpServer \u5c31\u662f\u4e00\u4e2a\u53cd\u5e94\u5806\u5b9e\u4f8b&#xff0c;\u800c\u6bcf\u4e00\u4e2a Connection \u5bf9\u8c61\u4ee3\u8868\u4e00\u4e2a\u4e8b\u4ef6\u3002\u6bcf\u4e2a\u4e8b\u4ef6\u5185\u90e8\u5305\u542b&#xff1a;<\/p>\n<ul>\n<li>\u6587\u4ef6\u63cf\u8ff0\u7b26<\/li>\n<li>\u72ec\u7acb\u7f13\u51b2\u533a<\/li>\n<li>\u56de\u8c03\u51fd\u6570<\/li>\n<li>\u6307\u5411\u53cd\u5e94\u5806\u7684\u6307\u9488<\/li>\n<\/ul>\n<p>\u53cd\u5e94\u5806\u5185\u90e8\u5b58\u5728\u4e8b\u4ef6\u5206\u53d1\u51fd\u6570&#xff0c;\u4e00\u65e6 epoll \u76d1\u6d4b\u5230\u67d0\u4e2a\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u5206\u53d1\u51fd\u6570\u5c31\u4f1a\u89e6\u53d1\u8be5\u4e8b\u4ef6\u7ed1\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u6267\u884c\u3002<\/p>\n<hr \/>\n<p>\u7279\u6027<\/p>\n<ul>\n<li>\u5355\u8fdb\u7a0b\u6a21\u578b&#xff1a;\u7531\u540c\u4e00\u4e2a\u8fdb\u7a0b\u5b8c\u6210\u4e8b\u4ef6\u5206\u53d1\u4e0e IO \u8bfb\u5199\u64cd\u4f5c;<\/li>\n<li>\u534a\u540c\u6b65\u534a\u5f02\u6b65&#xff1a;\u5f02\u6b65\u4f53\u73b0\u5728\u4e8b\u4ef6\u7684\u5230\u6765\u662f\u968f\u673a\u3001\u4e0d\u53ef\u9884\u6d4b\u7684&#xff1b;<\/li>\n<li>\u540c\u6b65\u4f53\u73b0\u5728 IO \u64cd\u4f5c\u7531\u5f53\u524d\u7ebf\u7a0b\u76f4\u63a5\u6267\u884c\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Reactor\u6a21\u5f0f1.Reactor \u6a21\u5f0f\u5b9a\u4e49Reactor \u6a21\u5f0f&#xff08;\u53cd\u5e94\u5668\u6a21\u5f0f&#xff0c;\u4e5f\u79f0 Dispatcher \u5206\u53d1\u5668\u6a21\u5f0f&#xff09; \u662f\u4e00\u79cd\u4e8b\u4ef6\u9a71\u52a8\u7684\u8bbe\u8ba1\u6a21\u5f0f&#xff1a; \u5b83\u901a\u8fc7 I\/O \u591a\u8def\u590d\u7528&#xff08;select \/ poll \/ epoll&#xff09;\u540c\u65f6\u76d1\u542c\u591a\u4e2a\u53e5\u67c4&#xff0c;\u5f53\u67d0\u4e2a\u53e5\u67c4\u4e0a\u7684\u4e8b\u4ef6\u5c31\u7eea\u65f6&#xff0c;\u628a\u4e8b\u4ef6\u5206\u6d3e\u7ed9\u4e8b\u5148\u6ce8\u518c\u597d\u7684\u5904\u7406\u7a0b\u5e8f&#xff08;\u56de\u8c03&#xff09;\u53bb\u5904\u7406\u30022.Reactor\u6a21\u5f0f\u7684\u89d2\u8272<\/p>\n","protected":false},"author":2,"featured_media":114399,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[13028,13029,55,3884,58],"topic":[],"class_list":["post-114412","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-reactor","tag-tcpserver","tag-c","tag-epoll","tag-linux"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Linux\u7f51\u7edc\u7f16\u7a0b\u3011\u56db\u5341\u4e5d.\u300aReactor \u6a21\u5f0f\u8be6\u89e3\uff1a\u57fa\u4e8e epoll ET \u5b9e\u73b0\u53cd\u5e94\u5806 Tcp \u670d\u52a1\u5668\u5b9e\u6218\u5f00\u53d1\u300b-\u8be6\u89e3 - \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\/114412.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Linux\u7f51\u7edc\u7f16\u7a0b\u3011\u56db\u5341\u4e5d.\u300aReactor \u6a21\u5f0f\u8be6\u89e3\uff1a\u57fa\u4e8e epoll ET \u5b9e\u73b0\u53cd\u5e94\u5806 Tcp \u670d\u52a1\u5668\u5b9e\u6218\u5f00\u53d1\u300b-\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Reactor\u6a21\u5f0f1.Reactor \u6a21\u5f0f\u5b9a\u4e49Reactor \u6a21\u5f0f&#xff08;\u53cd\u5e94\u5668\u6a21\u5f0f&#xff0c;\u4e5f\u79f0 Dispatcher \u5206\u53d1\u5668\u6a21\u5f0f&#xff09; \u662f\u4e00\u79cd\u4e8b\u4ef6\u9a71\u52a8\u7684\u8bbe\u8ba1\u6a21\u5f0f&#xff1a; \u5b83\u901a\u8fc7 I\/O \u591a\u8def\u590d\u7528&#xff08;select \/ poll \/ epoll&#xff09;\u540c\u65f6\u76d1\u542c\u591a\u4e2a\u53e5\u67c4&#xff0c;\u5f53\u67d0\u4e2a\u53e5\u67c4\u4e0a\u7684\u4e8b\u4ef6\u5c31\u7eea\u65f6&#xff0c;\u628a\u4e8b\u4ef6\u5206\u6d3e\u7ed9\u4e8b\u5148\u6ce8\u518c\u597d\u7684\u5904\u7406\u7a0b\u5e8f&#xff08;\u56de\u8c03&#xff09;\u53bb\u5904\u7406\u30022.Reactor\u6a21\u5f0f\u7684\u89d2\u8272\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/114412.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-09T00:26:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002626-6ac834b2e0769.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=\"15 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/114412.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/114412.html\",\"name\":\"\u3010Linux\u7f51\u7edc\u7f16\u7a0b\u3011\u56db\u5341\u4e5d.\u300aReactor \u6a21\u5f0f\u8be6\u89e3\uff1a\u57fa\u4e8e epoll ET \u5b9e\u73b0\u53cd\u5e94\u5806 Tcp \u670d\u52a1\u5668\u5b9e\u6218\u5f00\u53d1\u300b-\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-09T00:26:39+00:00\",\"dateModified\":\"2026-10-09T00:26:39+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/114412.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/114412.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/114412.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Linux\u7f51\u7edc\u7f16\u7a0b\u3011\u56db\u5341\u4e5d.\u300aReactor \u6a21\u5f0f\u8be6\u89e3\uff1a\u57fa\u4e8e epoll ET \u5b9e\u73b0\u53cd\u5e94\u5806 Tcp \u670d\u52a1\u5668\u5b9e\u6218\u5f00\u53d1\u300b---\u8be6\u89e3\"}]},{\"@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":"\u3010Linux\u7f51\u7edc\u7f16\u7a0b\u3011\u56db\u5341\u4e5d.\u300aReactor \u6a21\u5f0f\u8be6\u89e3\uff1a\u57fa\u4e8e epoll ET \u5b9e\u73b0\u53cd\u5e94\u5806 Tcp \u670d\u52a1\u5668\u5b9e\u6218\u5f00\u53d1\u300b-\u8be6\u89e3 - \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\/114412.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u7f51\u7edc\u7f16\u7a0b\u3011\u56db\u5341\u4e5d.\u300aReactor \u6a21\u5f0f\u8be6\u89e3\uff1a\u57fa\u4e8e epoll ET \u5b9e\u73b0\u53cd\u5e94\u5806 Tcp \u670d\u52a1\u5668\u5b9e\u6218\u5f00\u53d1\u300b-\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Reactor\u6a21\u5f0f1.Reactor \u6a21\u5f0f\u5b9a\u4e49Reactor \u6a21\u5f0f&#xff08;\u53cd\u5e94\u5668\u6a21\u5f0f&#xff0c;\u4e5f\u79f0 Dispatcher \u5206\u53d1\u5668\u6a21\u5f0f&#xff09; \u662f\u4e00\u79cd\u4e8b\u4ef6\u9a71\u52a8\u7684\u8bbe\u8ba1\u6a21\u5f0f&#xff1a; \u5b83\u901a\u8fc7 I\/O \u591a\u8def\u590d\u7528&#xff08;select \/ poll \/ epoll&#xff09;\u540c\u65f6\u76d1\u542c\u591a\u4e2a\u53e5\u67c4&#xff0c;\u5f53\u67d0\u4e2a\u53e5\u67c4\u4e0a\u7684\u4e8b\u4ef6\u5c31\u7eea\u65f6&#xff0c;\u628a\u4e8b\u4ef6\u5206\u6d3e\u7ed9\u4e8b\u5148\u6ce8\u518c\u597d\u7684\u5904\u7406\u7a0b\u5e8f&#xff08;\u56de\u8c03&#xff09;\u53bb\u5904\u7406\u30022.Reactor\u6a21\u5f0f\u7684\u89d2\u8272","og_url":"https:\/\/www.wsisp.com\/helps\/114412.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-09T00:26:39+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261009002626-6ac834b2e0769.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"15 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/114412.html","url":"https:\/\/www.wsisp.com\/helps\/114412.html","name":"\u3010Linux\u7f51\u7edc\u7f16\u7a0b\u3011\u56db\u5341\u4e5d.\u300aReactor \u6a21\u5f0f\u8be6\u89e3\uff1a\u57fa\u4e8e epoll ET \u5b9e\u73b0\u53cd\u5e94\u5806 Tcp \u670d\u52a1\u5668\u5b9e\u6218\u5f00\u53d1\u300b-\u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-09T00:26:39+00:00","dateModified":"2026-10-09T00:26:39+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/114412.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/114412.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/114412.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Linux\u7f51\u7edc\u7f16\u7a0b\u3011\u56db\u5341\u4e5d.\u300aReactor \u6a21\u5f0f\u8be6\u89e3\uff1a\u57fa\u4e8e epoll ET \u5b9e\u73b0\u53cd\u5e94\u5806 Tcp \u670d\u52a1\u5668\u5b9e\u6218\u5f00\u53d1\u300b---\u8be6\u89e3"}]},{"@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\/114412","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=114412"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/114412\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/114399"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=114412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=114412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=114412"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=114412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}