{"id":2738,"date":"2024-12-11T11:02:00","date_gmt":"2024-12-11T03:02:00","guid":{"rendered":"https:\/\/guapicoding.com\/?p=2738"},"modified":"2025-03-17T10:26:00","modified_gmt":"2025-03-17T02:26:00","slug":"%e9%87%8d%e6%9e%84-muduo-%e7%bd%91%e7%bb%9c%e5%ba%93","status":"publish","type":"post","link":"https:\/\/guapicoding.com\/?p=2738","title":{"rendered":"\u91cd\u6784 muduo \u7f51\u7edc\u5e93"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li><strong>\u9ad8\u5e76\u53d1\u975e\u963b\u585e\u7f51\u7edc\u5e93<\/strong><br>\u91c7\u7528\u591a<code>Reactor<\/code>\u591a\u7ebf\u7a0b\u6a21\u578b\uff0c\u5b9e\u73b0\u9ad8\u5e76\u53d1\u975e\u963b\u585e\u7684\u7f51\u7edc\u5e93\u3002<\/li>\n\n\n\n<li><strong>\u667a\u80fd\u6307\u9488\u9632\u6b62\u60ac\u7a7a\u6307\u9488<\/strong><br><code>TcpConnection<\/code>\u7ee7\u627f\u81ea<code>enable_shared_from_this<\/code>\uff0c\u5176\u76ee\u7684\u662f\u9632\u6b62\u5728\u4e0d\u8be5\u88ab\u91ca\u653e\u5bf9\u8c61\u7684\u5730\u65b9\u91ca\u653e\u5bf9\u8c61\uff0c\u5bfc\u81f4\u60ac\u7a7a\u6307\u9488\u7684\u4ea7\u751f\u3002<br>\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u7528\u6237\u53ef\u80fd\u5728\u5904\u7406<code>OnMessage<\/code>\u4e8b\u4ef6\u65f6\u5220\u9664\u5bf9\u8c61\uff0c\u786e\u4fdd<code>TcpConnection<\/code>\u4ee5\u6b63\u786e\u65b9\u5f0f\u91ca\u653e\u3002<\/li>\n\n\n\n<li><strong>\u5524\u9192\u673a\u5236<\/strong><br><code>EventLoop<\/code>\u4e2d\u4f7f\u7528\u4e86<code>eventfd<\/code>\u6765\u8c03\u7528<code>wakeup()<\/code>\uff0c\u8ba9<code>mainloop<\/code>\u5524\u9192<code>subloop<\/code>\u7684<code>epoll_wait<\/code>\u963b\u585e\u3002<\/li>\n\n\n\n<li><strong>\u4e00\u81f4\u6027\u54c8\u5e0c\u8f6e\u8be2\u7b97\u6cd5<\/strong><br>\u65b0\u589e<code>ConsistenHash<\/code>\u5934\u6587\u4ef6\uff0c\u91c7\u7528\u4e00\u81f4\u6027\u54c8\u5e0c\u8f6e\u8be2\u7b97\u6cd5\uff0c\u5c06<code>EventLoop<\/code>\u5408\u7406\u5206\u53d1\u7ed9\u6bcf\u4e00\u4e2a<code>TcpConnection<\/code>\u5bf9\u8c61\u3002<br>\u6b64\u5916\uff0c\u652f\u6301\u81ea\u5b9a\u4e49\u54c8\u5e0c\u51fd\u6570\uff0c\u6ee1\u8db3\u9ad8\u5e76\u53d1\u9700\u6c42\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u865a\u62df\u8282\u70b9\u6570\u91cf\u4e0d\u80fd\u8fc7\u5c11\u3002<\/li>\n\n\n\n<li><strong>\u7ebf\u7a0b\u521b\u5efa\u6709\u5e8f\u6027<\/strong><br>\u5728<code>Thread<\/code>\u4e2d\u901a\u8fc7<code>C++ lambda<\/code>\u8868\u8fbe\u5f0f\u4ee5\u53ca\u4fe1\u53f7\u91cf\u673a\u5236\uff0c\u4fdd\u8bc1\u7ebf\u7a0b\u521b\u5efa\u7684\u6709\u5e8f\u6027\uff0c\u786e\u4fdd\u7ebf\u7a0b\u6b63\u5e38\u521b\u5efa\u540e\u518d\u6267\u884c\u7ebf\u7a0b\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong>\u975e\u963b\u585e\u6838\u5fc3\u7f13\u51b2\u533a<\/strong><br><code>Buffer.*<\/code>\u662f<code>muduo<\/code>\u7f51\u7edc\u5e93\u975e\u963b\u585e\u7684\u6838\u5fc3\u6a21\u5757\u3002\u5f53\u89e6\u53d1\u76f8\u5e94\u7684\u8bfb\u5199\u4e8b\u4ef6\u65f6\uff0c\u5185\u6838\u7f13\u51b2\u533a\u53ef\u80fd\u6ca1\u6709\u8db3\u591f\u7a7a\u95f4\u4e00\u6b21\u6027\u53d1\u9001\u6570\u636e\uff0c\u6b64\u65f6\u6709\u4e24\u79cd\u9009\u62e9\uff1a\n<ul class=\"wp-block-list\">\n<li>\u7b2c\u4e00\u79cd\u662f\u5c06\u5176\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\uff0c\u4f46\u53ef\u80fd\u9020\u6210 CPU \u5fd9\u7b49\u5f85\uff1b<\/li>\n\n\n\n<li>\u7b2c\u4e8c\u79cd\u662f\u963b\u585e\u7b49\u5f85\u5185\u6838\u7f13\u51b2\u533a\u6709\u7a7a\u95f4\u518d\u53d1\u9001\uff0c\u4f46\u6548\u7387\u4f4e\u4e0b\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0c<code>Buffer<\/code>\u6a21\u5757\u5c06\u591a\u4f59\u6570\u636e\u5b58\u50a8\u5728\u7528\u6237\u7f13\u51b2\u533a\uff0c\u5e76\u6ce8\u518c\u76f8\u5e94\u7684\u8bfb\u5199\u4e8b\u4ef6\u76d1\u542c\uff0c\u5f85\u4e8b\u4ef6\u518d\u6b21\u89e6\u53d1\u65f6\u7edf\u4e00\u53d1\u9001\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u7075\u6d3b\u7684\u65e5\u5fd7\u6a21\u5757<\/strong><br><code>Logger<\/code>\u652f\u6301\u8bbe\u7f6e\u65e5\u5fd7\u7b49\u7ea7\u3002\u5728\u8c03\u8bd5\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u5f00\u542f<code>DEBUG<\/code>\u6a21\u5f0f\u6253\u5370\u65e5\u5fd7\uff1b\u800c\u5728\u670d\u52a1\u5668\u8fd0\u884c\u65f6\uff0c\u4e3a\u4e86\u51cf\u5c11\u65e5\u5fd7\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\uff0c\u53ef\u5173\u95ed<code>DEBUG<\/code>\u76f8\u5173\u65e5\u5fd7\u8f93\u51fa\u3002<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"alignright size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/guapicoding.com\/wp-content\/uploads\/2024\/12\/image-1024x729.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"729\" data-original=\"https:\/\/guapicoding.com\/wp-content\/uploads\/2024\/12\/image-1024x729.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-2741\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\"><em>\u56fe\u4e3a\u591a\u7ebf\u7a0b\u591a Reactor \u6846\u67b6\u56fe<\/em><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/guapicoding.com\/wp-content\/uploads\/2024\/12\/1741786943540.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"912\" height=\"647\" data-original=\"https:\/\/guapicoding.com\/wp-content\/uploads\/2024\/12\/1741786943540.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-2760\"  sizes=\"auto, (max-width: 912px) 100vw, 912px\" \/><\/div><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/guapicoding.com\/wp-content\/uploads\/2024\/12\/1741787001573-1.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"816\" height=\"624\" data-original=\"https:\/\/guapicoding.com\/wp-content\/uploads\/2024\/12\/1741787001573-1.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-2762\" style=\"width:840px;height:auto\"  sizes=\"auto, (max-width: 816px) 100vw, 816px\" \/><\/div><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\"><em>\u56fe\u4e3a muduo \u6846\u67b6\u56fe<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6982\u89c8<\/h2>\n\n\n\n<p>\u6bcf\u4e00\u4e2a EventLoop \u5bf9\u8c61\u90fd\u7ed1\u5b9a\u4e86\u4e00\u4e2a\u7ebf\u7a0b\uff08\u4e00\u5bf9\u4e00\u7ed1\u5b9a\uff09\uff0c\u5373 One Loop Per Thread\u3002\u8fd9\u79cd\u8fd0\u884c\u6a21\u5f0f\u662f muduo \u5e93\u7684\u7279\u8272\uff0c\u5145\u5206\u5229\u7528\u591a\u6838 cpu \u7684\u80fd\u529b\uff0c\u6bcf\u4e00\u4e2a\u6838\u7684\u7ebf\u7a0b\u8d1f\u8d23\u5faa\u73af\u76d1\u542c\u4e00\u7ec4\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u96c6\u5408\u3002<\/p>\n\n\n\n<p>EventLoop \u8d77\u5230\u4e00\u4e2a\u9a71\u52a8\u5faa\u73af\u7684\u529f\u80fd\uff0cPoller \u8d1f\u8d23\u4ece\u4e8b\u4ef6\u76d1\u542c\u5668\u4e0a\u83b7\u53d6\u76d1\u542c\u7ed3\u679c\uff0c\u800c Channel \u5219\u8d1f\u8d23\u5c06 fd \u53ca\u5176\u611f\u5174\u8da3\u7684\u4e8b\u4ef6\u3001\u53d1\u751f\u7684\u4e8b\u4ef6\u3001\u4e0d\u540c\u4e8b\u4ef6\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\u90fd\u5c01\u88c5\u5728\u4e00\u8d77\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u7ebf\u7a0b\u6570&gt;=2\u7684\u60c5\u51b5\uff0cmainloop\uff08mainReactor\uff09\u7684\u4e3b\u8981\u5de5\u4f5c\uff1a<br>accept \u63a5\u6536\u8fde\u63a5 =&gt; \u5c06 accept \u8fd4\u56de\u7684 connfd \u6253\u5305\u4e3a Channel =&gt; TcpServer::newConnection \u901a\u8fc7\u8f6e\u8be2\u5c06 TcpConnection \u5bf9\u8c61\u5206\u914d\u7ed9 subloop \u5904\u7406<br>mainloop \u8c03\u7528 queueInLoop \u5c06\u56de\u8c03\u51fd\u6570\u52a0\u5165 subloop\uff08\u8be5\u56de\u8c03\u51fd\u6570\u9700\u8981 subloop \u6267\u884c\uff0c\u4f46 subloop \u8fd8\u5728 poller_-&gt;poll \u5904\u963b\u585e\uff09\uff0c\u7136\u540e queueInLoop \u901a\u8fc7 wakeup \u5c06 subloop \u5524\u9192\u6765\u6267\u884c\u56de\u8c03\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0cmainloop \u8d1f\u8d23\u8bf7\u6c42\u8fde\u63a5\uff0c\u5c06\u56de\u8c03\u51fd\u6570\u5199\u5165 subloop \u4e2d\uff0c\u901a\u8fc7\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u5373\u53ef\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\u3002\u4f46\u662f muduo \u901a\u8fc7 wakeup() \u673a\u5236\uff0c\u4f7f\u7528 eventfd \u521b\u5efa\u7684 wakeupFd_ notify\uff0c\u4f7f\u5f97 mainloop \u548c subloop \u4e4b\u95f4\u80fd\u591f\u901a\u4fe1\u3002<\/p>\n\n\n\n<p>ps\uff1aeventfd \u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u5185\u6838\u63d0\u4f9b\u7684 64 \u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u8ba1\u6570\u5668\uff0c\u5b83\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b<strong>\u5199\u5165\uff08\u589e\u52a0\u8ba1\u6570\uff09<\/strong>\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b <strong>\u8bfb\u53d6\uff08\u6e05\u96f6\u6216\u51cf\u5c11\u8ba1\u6570\uff09<\/strong>\uff0c\u4ece\u800c\u5b9e\u73b0 <strong>\u9ad8\u6548\u7684\u4e8b\u4ef6\u901a\u77e5<\/strong>\u3002 \u5728 muduo \u91cc\uff0ceventfd \u88ab\u7528\u4f5c\u4e3b\u7ebf\u7a0b\uff08mainLoop\uff09\u548c\u5b50\u7ebf\u7a0b\uff08subLoop\uff09\u4e4b\u95f4\u7684\u901a\u4fe1\u673a\u5236\uff0c\u4e3b\u8981\u7528\u4e8e \u5524\u9192 subLoop \u4ee5\u6267\u884c mainLoop \u5206\u53d1\u7684\u4efb\u52a1\u3002<br> <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Channel \u7c7b<\/h2>\n\n\n\n<p><br>\u5728 TCP \u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u60f3\u8981 IO \u591a\u8def\u590d\u7528\u76d1\u542c\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u5c31\u8981\u628a\u8fd9\u4e2a fd \u548c\u8be5 fd \u611f\u5174\u8da3\u7684\u4e8b\u4ef6\u901a\u8fc7epoll_ctl \u6ce8\u518c\u5230 IO \u591a\u8def\u590d\u7528\u6a21\u5757(\u4e8b\u4ef6\u76d1\u542c\u5668)\u4e0a\u3002\u5f53\u4e8b\u4ef6\u76d1\u542c\u5668\u76d1\u542c\u5230\u8be5 fd \u53d1\u751f\u4e86\u67d0\u4e2a\u4e8b\u4ef6\uff0c\u5c31\u4f1a\u8fd4\u56de\u53d1\u751f\u4e8b\u4ef6\u7684 fd \u96c6\u5408\u4ee5\u53ca\u6bcf\u4e2a fd \u90fd\u53d1\u751f\u4e86\u4ec0\u4e48\u4e8b\u4ef6\u3002<br>Channel \u7c7b\u76f8\u5f53\u4e8e\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4fdd\u59c6\uff0c\u5c01\u88c5\u4e86\u4e00\u4e2a <strong>fd<\/strong> \u548c\u8fd9\u4e2a <strong>fd\u611f\u5174\u8da3\u4e8b\u4ef6<\/strong> \u4ee5\u53ca\u4e8b\u4ef6\u76d1\u542c\u5668\u76d1\u542c\u5230<strong>\u8be5 fd \u5b9e\u9645\u53d1\u751f\u7684\u4e8b\u4ef6<\/strong>\u3002\u540c\u65f6 Channel \u7c7b\u8fd8\u63d0\u4f9b\u4e86\u8bbe\u7f6e\u8be5 fd \u7684\u611f\u5174\u8da3\u4e8b\u4ef6\u3001\u5c06\u8be5 fd \u53ca\u5176\u611f\u5174\u8da3\u4e8b\u4ef6\u6ce8\u518c\u5230\u4e8b\u4ef6\u76d1\u542c\u5668\u6216\u4ece\u4e8b\u4ef6\u76d1\u542c\u5668\u4e0a\u79fb\u9664\uff0c\u4ee5\u53ca\u4fdd\u5b58\u4e86\u8be5 fd \u7684\u6bcf\u79cd\u4e8b\u4ef6\u5bf9\u5e94\u7684\u5904\u7406\u51fd\u6570\u3002<\/p>\n\n\n\n<p><strong>\u4e3a\u4ec0\u4e48\u9700\u8981 <code>tie()<\/code>?<\/strong><\/p>\n\n\n\n<p>\u56e0\u4e3a <code>Channel<\/code> \u53ef\u80fd\u4f1a\u5728 <code>TcpConnection<\/code> \u4e4b\u524d\u89e6\u53d1\u56de\u8c03\uff0c\u4f46 <code>TcpConnection<\/code> \u53ef\u80fd\u5df2\u7ecf\u88ab\u9500\u6bc1\u4e86\uff0c\u5bfc\u81f4 <code>Channel<\/code> \u8bbf\u95ee\u60ac\u7a7a\u5bf9\u8c61\u3002\u5f53 <code>Channel<\/code> \u89e6\u53d1\u56de\u8c03\u65f6\uff0cmduo \u5148\u68c0\u67e5 <code>TcpConnection<\/code> \u662f\u5426\u4ecd\u7136\u5b58\u5728\u3002<\/p>\n\n\n\n<p>\u5728 muduo \u7684 <code>TcpConnection<\/code> \u4e2d\u6ce8\u518c\u4e86 <code>Channel<\/code> \u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\uff0c\u4f20\u5165\u7684\u56de\u8c03\u51fd\u6570\u5747\u4e3a <code>TcpConnection<\/code> \u5bf9\u8c61\u7684\u6210\u5458\u65b9\u6cd5\u3002<code>Channel<\/code> \u7c7b\u8d1f\u8d23\u7ba1\u7406 I\/O \u4e8b\u4ef6\uff0c\u4f46\u662f <code>TcpConnection<\/code> \u7684\u751f\u547d\u5468\u671f\u4e0d\u4e00\u5b9a\u4e0e <code>Channel<\/code> \u5b8c\u5168\u4e00\u81f4\uff0c\u56e0\u6b64\u9700\u8981\u4e00\u79cd\u65b9\u5f0f\u6765\u786e\u4fdd <code>TcpConnection<\/code> \u5728 <code>Channel<\/code> \u89e6\u53d1\u56de\u8c03\u65f6\u4ecd\u7136\u5b58\u6d3b\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4 <strong>\u8bbf\u95ee\u60ac\u7a7a\u6307\u9488\uff08\u91ce\u6307\u9488\uff09<\/strong> \u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>void Channel::tie(const std::shared_ptr&lt;void&gt; &amp;obj)<br>{<br>    tie_ = obj;<br>    tied_ = true;<br>}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tie_<\/code> \u662f\u4e00\u4e2a <code>weak_ptr&lt;void&gt;<\/code>\uff08\u5728 <code>Channel.h<\/code> \u91cc\u5b9a\u4e49\uff09\uff0c\u5b83\u4fdd\u5b58\u4e86 <code>TcpConnection<\/code> \u7684 <code>shared_ptr<\/code>\u3002<\/li>\n\n\n\n<li><code>tied_<\/code> \u662f\u4e00\u4e2a <code>bool<\/code> \u53d8\u91cf\uff0c\u6807\u8bb0\u662f\u5426\u542f\u7528\u4e86 <code>tie<\/code> \u673a\u5236\u3002<\/li>\n\n\n\n<li><code>tie()<\/code> \u65b9\u6cd5\u7684\u4f5c\u7528\u5c31\u662f\u628a <code>TcpConnection<\/code> \u7ed1\u5b9a\u5230 <code>Channel<\/code>\uff0c\u4ece\u800c <strong>\u907f\u514d <code>Channel<\/code> \u8bbf\u95ee\u5df2\u7ecf\u9500\u6bc1\u7684 <code>TcpConnection<\/code><\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Poller \u7c7b<\/h2>\n\n\n\n<p><strong>\u8d1f\u8d23\u76d1\u542c\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e8b\u4ef6\u662f\u5426\u89e6\u53d1<\/strong>\u4ee5\u53ca<strong>\u8fd4\u56de\u53d1\u751f\u4e8b\u4ef6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ca\u5176\u5177\u4f53\u4e8b\u4ef6<\/strong>\u7684\u6a21\u5757\u5c31\u662f Poller\u3002\u6240\u4ee5<strong>\u4e00\u4e2a Poller \u5bf9\u8c61\u5bf9\u5e94\u4e00\u4e2a\u4e8b\u4ef6\u76d1\u542c\u5668<\/strong>\u3002<strong>\u5728\u591a Reactor \u6a21\u578b\u4e2d\uff0c\u6709\u591a\u5c11 Reactor \u5c31\u6709\u591a\u5c11 Poller<\/strong>\u3002<\/p>\n\n\n\n<p><br>muduo \u63d0\u4f9b\u4e86epoll \u548c poll \u4e24\u79cd IO \u591a\u8def\u590d\u7528\u65b9\u6cd5\u6765\u5b9e\u73b0\u4e8b\u4ef6\u76d1\u542c\uff08\u9ed8\u8ba4\u4f7f\u7528 epoll \u5b9e\u73b0\uff09\uff0c\u6211\u91cd\u6784\u7684 muduo \u5e93\u5219\u53ea\u652f\u6301 epoll\u3002<br>Poller \u662f\u4e2a\u62bd\u8c61\u865a\u7c7b\uff0c\u7531 EpollPoller \u548c PollPoller \u7ee7\u627f\u5b9e\u73b0\uff0c\u76d1\u542c\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u8fd4\u56de\u76d1\u542c\u7ed3\u679c\u7684\u5177\u4f53\u65b9\u6cd5\u4e5f\u57fa\u672c\u4e0a\u662f\u5728\u8fd9\u4e24\u4e2a\u6d3e\u751f\u7c7b\u4e2d\u5b9e\u73b0\u3002EpollPoller \u5c01\u88c5\u4e86\u7528 epoll \u65b9\u6cd5\u5b9e\u73b0\u7684\u4e0e\u4e8b\u4ef6\u76d1\u542c\u6709\u5173\u7684\u5404\u79cd\u65b9\u6cd5\uff0c PollPoller \u5219\u5c01\u88c5\u4e86 poll \u65b9\u6cd5\u5b9e\u73b0\u7684\u4e0e\u4e8b\u4ef6\u76d1\u542c\u6709\u5173\u7684\u5404\u79cd\u65b9\u6cd5\uff08\u8be5\u91cd\u6784\u7248\u672c\u5220\u9664\u4e86 PollPoller \u8fd9\u4e00\u90e8\u5206\uff09\u3002 <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">EpollPoller \u7c7b<\/h2>\n\n\n\n<p><code>EpollPoller<\/code> \u7684\u6838\u5fc3\uff1a<code>Timestamp poll(int timeoutMs,channelList *activechannels);<\/code><\/p>\n\n\n\n<p>\u5f53\u5916\u90e8\u8c03\u7528 <code>poll<\/code> \u65b9\u6cd5\u7684\u65f6\u5019\uff0c\u5e95\u5c42\u662f\u901a\u8fc7 <code><strong>epoll_wait<\/strong><\/code> \u83b7\u53d6\u8fd9\u4e2a\u4e8b\u4ef6\u76d1\u542c\u5668\u4e0a\u53d1\u751f\u4e8b\u4ef6\u7684 fd \u53ca\u5176\u5bf9\u5e94\u53d1\u751f\u7684\u4e8b\u4ef6\uff0c\u6211\u4eec\u77e5\u9053\u6bcf\u4e2a fd \u90fd\u662f\u7531\u4e00\u4e2a Channel \u5c01\u88c5\u7684\uff0c\u901a\u8fc7\u54c8\u5e0c\u8868 channels_ \u53ef\u4ee5\u6839\u636e fd \u627e\u5230\u5c01\u88c5\u8fd9\u4e2a fd \u7684 Channel\u3002 \u5c06\u4e8b\u4ef6\u76d1\u542c\u5668\u6240\u76d1\u542c\u5230\u7684\u8be5 fd \u53d1\u751f\u7684\u4e8b\u4ef6\u5199\u8fdb Channel \u4e2d\u7684 revents \u6210\u5458\u53d8\u91cf\u4e2d\uff0c\u7136\u540e\u628a\u8fd9\u4e2a Channel \u88c5\u8fdb activeChannels \u4e2d\uff08\u5b83\u662f\u4e00\u4e2a vector \u5bb9\u5668\uff09\u3002\u8fd9\u6837\uff0c\u5f53\u5916\u754c\u8c03\u7528\u5b8c <code>poll<\/code> \u4e4b\u540e\u5c31\u80fd\u62ff\u5230\u4e8b\u4ef6\u76d1\u542c\u5668\u7684<strong>\u76d1\u542c\u7ed3\u679c(activeChannels)<\/strong>\uff0c\u8fd9\u4e2a activeChannels \u5c31\u662f\u4e8b\u4ef6\u76d1\u542c\u5668\u6240\u76d1\u542c\u5230\u7684\u53d1\u751f\u4e8b\u4ef6\u7684 fd \u53ca\u5176\u53d1\u751f\u7684\u4e8b\u4ef6\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">EventLoop \u7c7b<\/h2>\n\n\n\n<p>\u4e0a\u8ff0\u7684 Poller \u7c7b\u662f\u5c01\u88c5\u4e86\u548c\u4e8b\u4ef6\u76d1\u542c\u6709\u5173\u7684\u65b9\u6cd5\u548c\u6210\u5458\uff0c\u8c03\u7528\u4e00\u6b21 Poller::poll \u65b9\u6cd5\u5b83\u5c31\u80fd\u8fd4\u56de\u4e8b\u4ef6\u76d1\u542c\u5668\u7684\u76d1\u542c\u7ed3\u679c\uff08\u53d1\u751f\u4e8b\u4ef6\u7684 fd\u53ca\u5176\u53d1\u751f\u7684\u4e8b\u4ef6\uff09\u3002\u4f5c\u4e3a\u4e00\u4e2a\u7f51\u7edc\u670d\u52a1\u5668\uff0c\u9700\u8981\u6709\u6301\u7eed\u76d1\u542c\u3001\u6301\u7eed\u83b7\u53d6\u76d1\u542c\u7ed3\u679c\u3001\u6301\u7eed\u5904\u7406\u76d1\u542c\u7ed3\u679c\u5bf9\u5e94\u7684\u4e8b\u4ef6\u7684\u80fd\u529b\uff0c\u4e5f\u5c31\u662f\u9700\u8981\u5faa\u73af\u7684\u53bb\u8c03\u7528 Poller::poll \u65b9\u6cd5\u83b7\u53d6\u5b9e\u9645\u53d1\u751f\u4e8b\u4ef6\u7684 Channel \u96c6\u5408\uff0c\u7136\u540e\u901a\u8fc7\u8c03\u7528 Channel::HandlerEvent \u65b9\u6cd5\u6765\u8c03\u7528\u8fd9\u4e9b Channel \u91cc\u9762\u4fdd\u5b58\u7684\u4e0d\u540c\u7c7b\u578b\u4e8b\u4ef6\u7684\u5904\u7406\u51fd\u6570\u3002<br><strong>EventLoop \u7c7b\u5c31\u662f\u8d1f\u8d23\u5b9e\u73b0\u201d\u5faa\u73af\u201d\u3001\u9a71\u52a8\u201c\u5faa\u73af\u201d\u7684\u91cd\u8981\u6a21\u5757<\/strong>\u3002Channel \u548c Poller \u90fd\u76f8\u5f53\u4e8e EventLoop \u7684\u624b\u4e0b\uff0cEventLoop \u6574\u5408\u5c01\u88c5\u4e86\u4e8c\u8005\u5e76\u5411\u4e0a\u63d0\u4f9b\u4e86\u66f4\u65b9\u4fbf\u7684\u63a5\u53e3\u6765\u8c03\u7528\u3002<\/p>\n\n\n\n<p><strong>\u6bcf\u4e2a EventLoop \u5bf9\u8c61\u90fd\u552f\u4e00\u7ed1\u5b9a\u4e86\u4e00\u4e2a\u7ebf\u7a0b<\/strong>\uff0c\u5e76\u4e14\u8fd9\u4e2a\u7ebf\u7a0b\u4e00\u76f4\u5728\u6267\u884c EventLoop::loop \u51fd\u6570\u91cc\u7684 while \u5faa\u73af\uff0c\u8be5\u5faa\u73af\u7684\u4f5c\u7528\u662f\u8c03\u7528 Poller::poll \u51fd\u6570\u6301\u7eed\u83b7\u53d6\u4e8b\u4ef6\u76d1\u542c\u5668\u4e0a\u7684\u76d1\u542c\u7ed3\u679c\uff0c\u7136\u540e\u8c03\u7528\u76d1\u542c\u7ed3\u679c\u4e2d\u6bcf\u4e00\u4e2a Channel \u7684\u5904\u7406\u51fd\u6570 HandlerEvent()\uff0c\u6839\u636e\u5b9e\u9645\u53d1\u751f\u7684\u4e8b\u4ef6\u7c7b\u578b\u53bb\u6267\u884c Channel \u7c7b\u4e2d\u5c01\u88c5\u7684\u5404\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002\u6bd4\u5982\u67d0\u4e2a Channel \u53d1\u751f\u4e86\u53ef\u8bfb\u4e8b\u4ef6\uff0c\u90a3\u4e48\u8fd9\u4e2a Channel \u7684 HandlerEvent() \u5c31\u4f1a\u8c03\u7528\u63d0\u524d\u6ce8\u518c\u5728\u8fd9\u4e2a Channel \u91cc\u7684\u53ef\u8bfb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u7ebf\u7a0b\u6570&gt;=2\u7684\u60c5\u51b5\uff0c<strong>mainloop\uff08mainReactor\uff09<\/strong>\u7684\u4e3b\u8981\u5de5\u4f5c\uff1a<br>accept \u63a5\u6536\u8fde\u63a5 =&gt; \u5c06 accept \u8fd4\u56de\u7684 connfd \u6253\u5305\u4e3a Channel =&gt; TcpServer::newConnection \u901a\u8fc7\u8f6e\u8be2\u5c06 TcpConnection \u5bf9\u8c61\u5206\u914d\u7ed9 subloop \u5904\u7406<br>mainloop \u8c03\u7528 queueInLoop \u5c06\u56de\u8c03\u51fd\u6570\u52a0\u5165 subloop\uff08\u8be5\u56de\u8c03\u51fd\u6570\u9700\u8981 subloop \u6267\u884c\uff0c\u4f46 subloop \u8fd8\u5728 poller_-&gt;poll \u5904\u963b\u585e\uff09\uff0c\u7136\u540e queueInLoop \u901a\u8fc7 wakeup \u5c06 subloop \u5524\u9192\u6765\u6267\u884c\u56de\u8c03\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Acceptor \u7c7b<\/h2>\n\n\n\n<p>Accetpor \u7c7b\u5c01\u88c5\u4e86\u670d\u52a1\u5668\u76d1\u542c\u5957\u63a5\u5b57 fd \u4ee5\u53ca\u76f8\u5173\u5904\u7406\u65b9\u6cd5\u3002 <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Socket \u7c7b<\/h2>\n\n\n\n<p>Socket \u7c7b\u53ea\u6709\u4e00\u4e2a\u6210\u5458 sockfd\uff0c\u8be5\u7c7b\u7684\u4f5c\u7528\u5c31\u662f\u7528\u4e8e\u7ba1\u7406 TCP \u8fde\u63a5\u5bf9\u5e94\u7684 sockfd \u7684\u751f\u547d\u5468\u671f\uff08\u6790\u6784\u7684\u65f6\u5019 close \u8be5 sockfd\uff09\uff0c\u4ee5\u53ca\u63d0\u4f9b\u4e00\u4e9b\u51fd\u6570\u6765\u4fee\u6539 sockfd \u4e0a\u7684\u9009\u9879\uff0c\u6bd4\u5982 Nagel \u7b97\u6cd5\u3001\u8bbe\u7f6e\u5730\u5740\u590d\u7528\u7b49\u3002 <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Buffer \u7c7b<\/h2>\n\n\n\n<p>Buffer \u7c7b\u5c01\u88c5\u4e86\u4e00\u4e2a\u7528\u6237\u7f13\u51b2\u533a\uff0c\u4ee5\u53ca\u5411\u8fd9\u4e2a\u7f13\u51b2\u533a\u8bfb\u3001\u5199\u6570\u636e\u7b49\u4e00\u7cfb\u5217\u7684\u63a7\u5236\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u8be5 Buffer \u662f\u4e00\u4e2a\u53ef\u53d8\u957f\u7684\u7f13\u51b2\u533a\uff0c\u63d0\u4f9b\u4e86\u9884\u7559\u5934\u90e8\u7a7a\u95f4 (prependable)\u3001\u53ef\u8bfb (readable)\u3001\u53ef\u5199 (writable) \u7684\u4f18\u5316\u7ba1\u7406\u3002<\/p>\n\n\n\n<p>\u6574\u4e2a Buffer \u7531 std::vector&lt;char> buffer_ \u7ef4\u62a4\uff0c\u521d\u59cb\u5316\u65f6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5934\u90e8\u9884\u7559 kCheapPrepend \u4e2a\u5b57\u8282\uff0c\u7528\u4e8e\u5b58\u50a8\u989d\u5916\u7684\u5143\u6570\u636e\uff08\u5982\u6570\u636e\u957f\u5ea6\uff09\u3002<\/li>\n\n\n\n<li>\u6570\u636e\u533a\u5927\u5c0f\u4e3a kInitialSize = 1024\uff0c\u5373\u9ed8\u8ba4 buffer_ \u5171 1032 \u5b57\u8282\u3002<\/li>\n\n\n\n<li>readerIndex_ \u548c writerIndex_ \u90fd\u521d\u59cb\u5316\u4e3a kCheapPrepend\uff0c\u6570\u636e\u4ece kCheapPrepend \u4f4d\u7f6e\u5f00\u59cb\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6574\u7406\u7a7a\u95f4<\/strong>\uff1a| kCheapPrepend |xxx| reader | writer |  \/\/ xxx\u6807\u793areader\u4e2d\u5df2\u8bfb\u7684\u90e8\u5206<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>if (writableBytes() + prependableBytes() &lt; len + kCheapPrepend)<\/code> \/\/ \u5373 len > xxx\u7a7a\u95f4 + writer\u7684\u90e8\u5206\uff0c\u90a3\u5c31\u76f4\u63a5\u6269\u5bb9\uff1a<code>buffer_.resize(writerIndex_ + len);<\/code><\/li>\n\n\n\n<li>\u5426\u5219\uff0c\u8bf4\u660e\u73b0\u6709\u7a7a\u95f4\u8db3\u591f\uff0c\u628a readerIndex_ \u79fb\u5230\u4ece xxx \u5f00\u59cb\uff0c\u5373\u4e22\u5f03\u5df2\u8bfb\u90e8\u5206<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">TcpServer \u7c7b<br><\/h2>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u56fe\u4e3a\u591a\u7ebf\u7a0b\u591a Reactor \u6846\u67b6\u56fe \u56fe\u4e3a muduo \u6846\u67b6\u56fe \u6982\u89c8 \u6bcf\u4e00\u4e2a EventLoop \u5bf9\u8c61\u90fd\u7ed1\u5b9a\u4e86 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[98,99,100],"class_list":["post-2738","post","type-post","status-publish","format-standard","hentry","category-project","tag-muduo","tag-reactor","tag-100"],"_links":{"self":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts\/2738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2738"}],"version-history":[{"count":11,"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts\/2738\/revisions"}],"predecessor-version":[{"id":2781,"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts\/2738\/revisions\/2781"}],"wp:attachment":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}