{"id":43850,"date":"2025-06-15T15:31:54","date_gmt":"2025-06-15T07:31:54","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/43850.html"},"modified":"2025-06-15T15:31:54","modified_gmt":"2025-06-15T07:31:54","slug":"%e3%80%90linux%e3%80%91%e5%88%a9%e7%94%a8%e5%a4%9a%e8%b7%af%e8%bd%ac%e6%8e%a5epoll%e6%9c%ba%e5%88%b6%e3%80%81et%e6%a8%a1%e5%bc%8f%ef%bc%8c%e5%9f%ba%e4%ba%8ereactor%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/43850.html","title":{"rendered":"\u3010Linux\u3011\u5229\u7528\u591a\u8def\u8f6c\u63a5epoll\u673a\u5236\u3001ET\u6a21\u5f0f\uff0c\u57fa\u4e8eReactor\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668"},"content":{"rendered":"<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0&#x1f4da;\u00a0\u535a\u4e3b\u7684\u4e13\u680f<\/span><\/p>\n<p><span style=\"color:#0d0016\">&#x1f427;\u00a0<\/span><span style=\"color:#0d0016\">Linux<\/span><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0|\u00a0\u00a0 &#x1f5a5;\ufe0f\u00a0<\/span><span style=\"color:#0d0016\">C&#043;&#043;<\/span><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0|\u00a0\u00a0 &#x1f4ca;\u00a0<\/span><span style=\"color:#0d0016\">\u6570\u636e\u7ed3\u6784\u00a0\u00a0<\/span><span style=\"color:#0d0016\">|\u00a0&#x1f4a1;<\/span><span style=\"color:#0d0016\">C&#043;&#043; \u7b97\u6cd5<\/span><span style=\"color:#0d0016\">\u00a0|\u00a0<\/span><span style=\"color:#0d0016\">&#x1f152;<\/span><span style=\"color:#0d0016\">\u00a0<\/span><span style=\"color:#0d0016\">C \u8bed\u8a00<\/span><span style=\"color:#0d0016\">\u00a0 |\u00a0<\/span><span style=\"color:#0d0016\">&#x1f310;\u00a0<\/span><span style=\"color:#0d0016\">\u8ba1\u7b97\u673a\u7f51\u7edc<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u4e0a\u7bc7\u6587\u7ae0&#xff1a;\u591a\u8def\u8f6c\u63a5epoll&#xff0c;\u5b9e\u73b0echoserver<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u81f3\u6b64&#xff0c;Linux\u4e0e\u8ba1\u7b97\u673a\u7f51\u7edc\u7cfb\u5217\u4e13\u9898\u5df2\u5706\u6ee1\u6536\u5b98\u3002\u540e\u7eed\u6211\u4eec\u5c06\u6301\u7eed\u63a8\u51fa\u9879\u76ee\u5b9e\u6218\u3001MySQL\u6570\u636e\u5e93\u3001Qt\u5f00\u53d1\u4ee5\u53ca\u7b97\u6cd5\u7cbe\u8bb2\u7b49\u7cfb\u5217\u6587\u7ae0&#xff0c;\u656c\u8bf7\u671f\u5f85&#xff01;<\/span><\/p>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"Connection.hpp-toc\" style=\"margin-left:40px\">Connection.hpp<\/p>\n<p id=\"%E5%85%88%E5%AE%8C%E6%88%90Listen%E5%A5%97%E6%8E%A5%E5%AD%97%E7%9A%84%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%EF%BC%9A%C2%A0-toc\" style=\"margin-left:0px\">\u5148\u5b8c\u6210Listen\u5957\u63a5\u5b57\u7684\u4e8b\u4ef6\u5904\u7406&#xff1a;\u00a0<\/p>\n<p id=\"Common.hpp-toc\" style=\"margin-left:40px\">Common.hpp<\/p>\n<p id=\"Epoller.hpp-toc\" style=\"margin-left:40px\">Epoller.hpp<\/p>\n<p id=\"Listener.hpp-toc\" style=\"margin-left:40px\">Listener.hpp<\/p>\n<p id=\"TcpServer.hpp-toc\" style=\"margin-left:40px\">TcpServer.hpp<\/p>\n<p id=\"Main.cc-toc\" style=\"margin-left:40px\">Main.cc<\/p>\n<p id=\"Reactor(%E5%8F%8D%E5%BA%94%E5%A0%86)%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F-toc\" style=\"margin-left:0px\">Reactor(\u53cd\u5e94\u5806)\u8bbe\u8ba1\u6a21\u5f0f<\/p>\n<p id=\"%E5%A4%84%E7%90%86listen%E5%A5%97%E6%8E%A5%E5%AD%97%E7%9A%84%E6%96%B0%E8%BF%9E%E6%8E%A5%EF%BC%9A%E5%A6%82%E4%BD%95%E5%A4%84%E7%90%86Listen%E7%B1%BB%E4%B8%AD%E7%9A%84Accepter%3F-toc\" style=\"margin-left:0px\">\u5904\u7406listen\u5957\u63a5\u5b57\u7684\u65b0\u8fde\u63a5&#xff1a;\u5982\u4f55\u5904\u7406Listen\u7c7b\u4e2d\u7684Accepter?<\/p>\n<p id=\"%E5%9C%A8Connection%E7%B1%BB%E4%B8%AD%E5%AE%9A%E4%B9%89%E4%B8%80%E4%B8%AAReactor%E6%8C%87%E9%92%88%EF%BC%8C%E5%B9%B6%E5%AE%9E%E7%8E%B0Connection%E5%92%8CReactor%E4%B9%8B%E9%97%B4%E7%9A%84%E4%BA%92%E7%9B%B8%E5%BC%95%E7%94%A8-toc\" style=\"margin-left:80px\">\u5728Connection\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2aReactor\u6307\u9488&#xff0c;\u5e76\u5b9e\u73b0Connection\u548cReactor\u4e4b\u95f4\u7684\u4e92\u76f8\u5f15\u7528<\/p>\n<p id=\"%E5%9B%A0%E6%AD%A4%E6%88%91%E4%BB%AC%E9%9C%80%E8%A6%81%E5%B0%86%E6%89%80%E6%9C%89%E7%9A%84%E6%96%87%E4%BB%B6%E6%8F%8F%E8%BF%B0%E7%AC%A6%E9%83%BD%E8%AE%BE%E7%BD%AE%E4%B8%BA%E9%9D%9E%E9%98%BB%E5%A1%9E%E3%80%82-toc\" style=\"margin-left:80px\">\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5c06\u6240\u6709\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u90fd\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u3002<\/p>\n<p id=\"%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95%3Afcntl-toc\" style=\"margin-left:80px\">\u89e3\u51b3\u529e\u6cd5:fcntl<\/p>\n<p id=\"Listener%E7%B1%BBListener.hpp%3A-toc\" style=\"margin-left:40px\">Listener\u7c7bListener.hpp:<\/p>\n<p id=\"%E5%B0%86%E6%96%B0%E5%88%9B%E5%BB%BA%E7%9A%84%E5%A5%97%E6%8E%A5%E5%AD%97%E5%8A%A0%E5%85%A5Reactor%E7%9A%84%E8%BF%9E%E6%8E%A5%E6%B1%A0-toc\" style=\"margin-left:0px\">\u5c06\u65b0\u521b\u5efa\u7684\u5957\u63a5\u5b57\u52a0\u5165Reactor\u7684\u8fde\u63a5\u6c60<\/p>\n<p id=\"1.%E5%9C%A8Connection%E7%B1%BB%E4%B8%AD%E8%AE%BE%E7%BD%AEReactor%E3%80%81%E4%BB%A5%E5%8F%8AConnection%E7%B1%BB%E5%9E%8B-toc\" style=\"margin-left:40px\">1.\u5728Connection\u7c7b\u4e2d\u8bbe\u7f6eReactor\u3001\u4ee5\u53caConnection\u7c7b\u578b<\/p>\n<p id=\"%C2%A02.%E5%B0%86%E5%BD%93%E5%89%8D%E5%AF%B9%E8%B1%A1%EF%BC%8C%E8%AE%BE%E7%BD%AE%E8%BF%9B%E5%85%A5%E6%89%80%E6%9C%89%E7%9A%84conn%E5%AF%B9%E8%B1%A1%E4%B8%AD-toc\" style=\"margin-left:40px\">\u00a02.\u5c06\u5f53\u524d\u5bf9\u8c61&#xff0c;\u8bbe\u7f6e\u8fdb\u5165\u6240\u6709\u7684conn\u5bf9\u8c61\u4e2d<\/p>\n<p id=\"%E6%B7%BB%E5%8A%A0HandlerConnection%E6%A8%A1%E5%9D%97-toc\" style=\"margin-left:80px\">\u6dfb\u52a0HandlerConnection\u6a21\u5757<\/p>\n<p id=\"3.%E4%BF%AE%E6%AD%A3AddConnection-toc\" style=\"margin-left:40px\">3.\u4fee\u6b63AddConnection<\/p>\n<p id=\"%E6%9B%B4%E6%96%B0AddConnection-toc\" style=\"margin-left:80px\">\u66f4\u65b0AddConnection<\/p>\n<p id=\"%E6%9B%B4%E6%96%B0%E5%85%A5%E5%8F%A3%E5%87%BD%E6%95%B0%EF%BC%9AMain.cc-toc\" style=\"margin-left:80px\">\u66f4\u65b0\u5165\u53e3\u51fd\u6570&#xff1a;Main.cc<\/p>\n<p id=\"%E7%BB%A7%E7%BB%AD%E5%86%99HandlerConnection%E7%B1%BB%EF%BC%9A%E8%AF%BB%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86-toc\" style=\"margin-left:80px\">\u7ee7\u7eed\u5199HandlerConnection\u7c7b&#xff1a;\u8bfb\u4e8b\u4ef6\u5904\u7406<\/p>\n<p id=\"4.%E5%A4%84%E7%90%86%E8%AF%BB%E4%BA%8B%E4%BB%B6%EF%BC%8C%E5%BC%95%E5%85%A5%E5%8D%8F%E8%AE%AE%E8%BF%9B%E8%A1%8C%E6%8A%A5%E6%96%87%E8%A7%A3%E6%9E%90-toc\" style=\"margin-left:40px\">4.\u5904\u7406\u8bfb\u4e8b\u4ef6&#xff0c;\u5f15\u5165\u534f\u8bae\u8fdb\u884c\u62a5\u6587\u89e3\u6790<\/p>\n<p id=\"5.%E5%8D%8F%E8%AE%AE%E5%A4%84%E7%90%86%2B%E6%8A%A5%E6%96%87%E8%A7%A3%E6%9E%90%E6%A8%A1%E5%9D%97PackageParse.hpp%2BProtocol.hpp-toc\" style=\"margin-left:40px\">5.\u534f\u8bae\u5904\u7406&#043;\u62a5\u6587\u89e3\u6790\u6a21\u5757PackageParse.hpp&#043;Protocol.hpp<\/p>\n<p id=\"%E8%AF%BB%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%E9%80%BB%E8%BE%91%E5%9B%BE%EF%BC%9A-toc\" style=\"margin-left:80px\">\u8bfb\u4e8b\u4ef6\u5904\u7406\u903b\u8f91\u56fe&#xff1a;<\/p>\n<p id=\"6.%E6%B7%BB%E5%8A%A0%E4%B8%9A%E5%8A%A1%E5%A4%84%E7%90%86%E6%A8%A1%E5%9D%97%EF%BC%9A-toc\" style=\"margin-left:40px\">6.\u6dfb\u52a0\u4e1a\u52a1\u5904\u7406\u6a21\u5757&#xff1a;<\/p>\n<p id=\"7.%E5%85%B3%E4%BA%8E%E5%86%99%E5%85%A5%E7%9A%84%E8%AF%9D%E9%A2%98%EF%BC%9A-toc\" style=\"margin-left:40px\">7.\u5173\u4e8e\u5199\u5165\u7684\u8bdd\u9898&#xff1a;<\/p>\n<p id=\"%E5%86%99%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86-toc\" style=\"margin-left:80px\">\u5199\u4e8b\u4ef6\u5904\u7406<\/p>\n<p id=\"%E5%86%99%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B-toc\" style=\"margin-left:80px\">\u5199\u4e8b\u4ef6\u5904\u7406\u6d41\u7a0b<\/p>\n<p id=\"PackageParse.hpp-toc\" style=\"margin-left:40px\">PackageParse.hpp<\/p>\n<p id=\"%E6%95%B0%E6%8D%AE%E5%8F%91%E9%80%81HandlerSender%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:80px\">\u6570\u636e\u53d1\u9001HandlerSender\u51fd\u6570<\/p>\n<p id=\"%E6%9B%B4%E6%96%B0Epoller.hpp-toc\" style=\"margin-left:120px\">\u66f4\u65b0Epoller.hpp<\/p>\n<p id=\"%E6%9B%B4%E6%96%B0Reactor.hpp-toc\" style=\"margin-left:120px\">\u66f4\u65b0Reactor.hpp<\/p>\n<p id=\"DelConnection%E5%87%BD%E6%95%B0%EF%BC%9A-toc\" style=\"margin-left:80px\">DelConnection\u51fd\u6570&#xff1a;<\/p>\n<p id=\"%E6%AD%A3%E5%BC%8F%E8%AE%A4%E8%AF%86Reactor%E5%8F%8D%E5%BA%94%E5%A0%86%E6%A8%A1%E5%BC%8F-toc\" style=\"margin-left:0px\">\u6b63\u5f0f\u8ba4\u8bc6Reactor\u53cd\u5e94\u5806\u6a21\u5f0f<\/p>\n<p id=\"Reactor%20%E6%A8%A1%E5%BC%8F%E7%9A%84%E6%A0%B8%E5%BF%83%E6%80%9D%E6%83%B3-toc\" style=\"margin-left:40px\">Reactor \u6a21\u5f0f\u7684\u6838\u5fc3\u601d\u60f3<\/p>\n<p id=\"Reactor%20%E6%A8%A1%E5%BC%8F%E7%9A%84%E4%B8%BB%E8%A6%81%E7%BB%84%E4%BB%B6-toc\" style=\"margin-left:40px\">Reactor \u6a21\u5f0f\u7684\u4e3b\u8981\u7ec4\u4ef6<\/p>\n<p id=\"Reactor%EF%BC%88%E5%BA%95%E5%B1%82%EF%BC%89%2B%20%E7%BA%BF%E7%A8%8B%E6%B1%A0%EF%BC%88%E4%B8%9A%E5%8A%A1%E5%A4%84%E7%90%86%EF%BC%89-toc\" style=\"margin-left:40px\">Reactor&#xff08;\u5e95\u5c42&#xff09;&#043; \u7ebf\u7a0b\u6c60&#xff08;\u4e1a\u52a1\u5904\u7406&#xff09;<\/p>\n<p id=\"%E2%80%8B%E7%BC%96%E8%BE%91Reactor%20%2B%20%E7%BA%BF%E7%A8%8B%E6%B1%A0%E7%9A%84%E6%9E%B6%E6%9E%84-toc\" style=\"margin-left:80px\">\u200b\u7f16\u8f91Reactor &#043; \u7ebf\u7a0b\u6c60\u7684\u67b6\u6784<\/p>\n<p id=\"%E5%BC%95%E5%8F%91%E7%AB%9E%E4%BA%89%E6%9D%A1%E4%BB%B6%E3%80%81%E6%95%B0%E6%8D%AE%E4%B8%80%E8%87%B4%E6%80%A7%E9%97%AE%E9%A2%98%E5%A4%84%E7%90%86-toc\" style=\"margin-left:40px\">\u5f15\u53d1\u7ade\u4e89\u6761\u4ef6\u3001\u6570\u636e\u4e00\u81f4\u6027\u95ee\u9898\u5904\u7406<\/p>\n<p id=\"%E8%BD%AC%E5%8F%98%E6%80%9D%E8%B7%AF%EF%BC%9AOne%20thread%20One%20Loop-toc\" style=\"margin-left:40px\">\u8f6c\u53d8\u601d\u8def&#xff1a;One thread One Loop<\/p>\n<p id=\"%E8%AE%BE%E8%AE%A1%E6%80%9D%E8%B7%AF-toc\" style=\"margin-left:80px\">\u8bbe\u8ba1\u601d\u8def<\/p>\n<hr id=\"hr-toc\" \/>\n<p><span style=\"color:#0d0016\">\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u89e3\u8026\u5408\u5ea6\u8d85\u4f4eEchoServer\u7f16\u5199&#xff0c;\u7136\u540e\u7ed3\u5408\u4e4b\u524d\u6240\u8bb2\u8fc7\u7684\u7f51\u7edc\u7248\u8ba1\u7b97\u5668\u642d\u5efa\u4e1a\u52a1\u5c42<\/span><\/p>\n<\/p>\n<p><span style=\"color:#0d0016\">\u6458\u8981&#xff1a;\u672c\u6587\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5229\u7528Reactor\u6a21\u5f0f\u6784\u5efa\u4f4e\u8026\u5408\u3001\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u670d\u52a1\u5668\u3002\u901a\u8fc7\u5c01\u88c5Connection\u7c7b\u7ba1\u7406\u5957\u63a5\u5b57\u53ca\u7f13\u51b2\u533a&#xff0c;\u7ed3\u5408epoll\u591a\u8def\u590d\u7528\u6280\u672f\u5b9e\u73b0\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b&#xff0c;\u6838\u5fc3\u6a21\u5757\u5305\u62ecListener&#xff08;\u5904\u7406\u65b0\u8fde\u63a5&#xff09;\u3001HandlerConnection&#xff08;\u5904\u7406IO\u4e8b\u4ef6&#xff09;\u548cReactor&#xff08;\u4e8b\u4ef6\u6d3e\u53d1&#xff09;\u3002\u6587\u7ae0\u6df1\u5165\u89e3\u6790\u975e\u963b\u585eIO\u4e0e\u8fb9\u7f18\u89e6\u53d1&#xff08;ET&#xff09;\u6a21\u5f0f\u7684\u9ad8\u6548\u5b9e\u73b0&#xff0c;\u5e76\u901a\u8fc7\u7ebf\u7a0b\u6c60\u4f18\u5316\u4e1a\u52a1\u5904\u7406\u903b\u8f91&#xff0c;\u89e3\u51b3\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u7ade\u4e89\u6761\u4ef6\u4e0e\u6570\u636e\u4e00\u81f4\u6027\u95ee\u9898\u3002\u6b64\u5916&#xff0c;\u5f15\u5165eventfd\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u5524\u9192\u673a\u5236&#xff0c;\u9a8c\u8bc1errno\u7684\u7ebf\u7a0b\u5b89\u5168\u6027\u3002\u6700\u7ec8\u901a\u8fc7\u6a21\u5757\u5316\u8bbe\u8ba1\u5b9e\u73b0\u670d\u52a1\u5668\u7684\u9ad8\u6269\u5c55\u6027&#xff0c;\u4e3a\u5f00\u53d1\u9ad8\u6027\u80fd\u7f51\u7edc\u670d\u52a1\u63d0\u4f9b\u5b8c\u6574\u5b9e\u8df5\u65b9\u6848&#xff0c;\u9002\u7528\u4e8eHTTP\u670d\u52a1\u5668\u3001\u5373\u65f6\u901a\u4fe1\u7b49\u573a\u666f\u3002<\/span><\/p>\n<\/p>\n<p>                         Request(\u8bf7\u6c42)  &amp;&amp;  Response&#xff08;\u5e94\u7b54&#xff09;<br \/>\n                    Protocol&#xff08;\u534f\u8bae\u5904\u7406&#xff09;PackageParse&#xff08;\u62a5\u6587\u89e3\u6790&#xff09;<br \/>\nListener(\u5904\u7406\u65b0\u8fde\u63a5) HandlerConnection&#xff08;\u5904\u7406\u666e\u901a\u5957\u63a5\u5b57IO\u4e8b\u4ef6\u4ee5\u53ca\u5f02\u5e38&#xff09;<br \/>\nConnection&#xff08;\u5c01\u88c5\u5957\u63a5\u5b57\u4ee5\u53ca\u7f13\u51b2\u533a&#xff09;<br \/>\nReactor(\u6700\u5e95\u5c42&#xff09;<br \/>\nepoller(\u53cd\u5e94\u7ed9Reactor) <\/p>\n<p><span style=\"color:#0d0016\">\u4f7f\u7528\u5230\u7684\u4ee3\u7801\u5728\u4e4b\u524d\u7684\u535a\u5ba2\u90fd\u6709\u8be6\u7ec6\u8bb2\u89e3&#xff0c;\u53ef\u5728\u6211\u7684gitee\u4e2d\u81ea\u53d6&#xff1a;\u70b9\u51fb\u94fe\u63a5<\/span><\/p>\n<p><span style=\"color:#fe2c24\">\u672c\u7bc7\u6587\u7ae0\u7684\u4ee3\u7801&#xff1a;2.epollserver_v2<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u672c\u7bc7\u6587\u7ae0\u9700\u8981\u51c6\u5907\u4ee5\u4e0b\u6587\u4ef6&#xff1a;\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u8bb2\u89e3\u6bcf\u4e2a\u6a21\u5757\u7684\u5b9e\u73b0\u4ee5\u53ca\u5b9e\u73b0\u601d\u8def<\/span><\/p>\n<p>.<br \/>\n\u251c\u2500\u2500 Common.hpp<br \/>\n\u251c\u2500\u2500 Connection.hpp<br \/>\n\u251c\u2500\u2500 Epoller.hpp<br \/>\n\u251c\u2500\u2500 HandlerConnection.hpp<br \/>\n\u251c\u2500\u2500 InetAddr.hpp<br \/>\n\u251c\u2500\u2500 Listener.hpp<br \/>\n\u251c\u2500\u2500 LockGuard.hpp<br \/>\n\u251c\u2500\u2500 Log.hpp<br \/>\n\u251c\u2500\u2500 Main.cc<br \/>\n\u251c\u2500\u2500 Makefile<br \/>\n\u251c\u2500\u2500 NetCal.hpp<br \/>\n\u251c\u2500\u2500 PackageParse.hpp<br \/>\n\u251c\u2500\u2500 Protocol.hpp<br \/>\n\u251c\u2500\u2500 Reactor.hpp<br \/>\n\u2514\u2500\u2500 Socket.hpp<\/p>\n<p>0 directories, 15 files<\/p>\n<h3 id=\"Connection.hpp\"><span style=\"color:#0d0016\">Connection.hpp<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u5c01\u88c5\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1a;\u6bcf\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5982\u5957\u63a5\u5b57&#xff09;\u88ab\u62bd\u8c61\u4e3aConnection\u5bf9\u8c61&#xff0c;\u5305\u542b&#xff1a;<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">\u63a5\u6536\u7f13\u51b2\u533a&#xff08;_inbuffer&#xff09;\u548c\u53d1\u9001\u7f13\u51b2\u533a&#xff08;_outbuffer&#xff09;\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u4e8b\u4ef6\u5904\u7406\u51fd\u6570&#xff08;\u8bfb\u3001\u5199\u3001\u5f02\u5e38&#xff09;&#xff0c;\u901a\u8fc7RegisterHandler\u65b9\u6cd5\u6ce8\u518c\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u7ba1\u7406\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;_sockfd&#xff09;\u53ca\u5176\u5173\u6ce8\u7684\u4e8b\u4ef6&#xff08;_events&#xff09;\u3002<\/span><\/p>\n<\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u5c01\u88c5\u6587\u4ef6\u63cf\u8ff0\u7b26<\/span><\/p>\n<p><span style=\"color:#0d0016\">1.\u4fdd\u8bc1\u6bcf\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u90fd\u6709\u4e00\u4e2a\u81ea\u5df1\u7684\u63a5\u6536\u7f13\u51b2\u533a\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5728epoll\u673a\u5236\u4e2d&#xff0c;\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6982\u5ff5\u88ab\u62bd\u8c61\u5316&#xff0c;\u5305\u62ec\u5957\u63a5\u5b57\u5728\u5185\u7684\u6240\u6709\u901a\u4fe1\u7aef\u70b9\u90fd\u88ab\u7edf\u4e00\u5c01\u88c5\u4e3a\u8fde\u63a5&#xff08;connection&#xff09;\u5bf9\u8c61\u3002<\/span><\/p>\n<p> #pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;functional&gt;<\/p>\n<p>\/\/ \u5c01\u88c5\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n\/\/ 1.\u7ed9\u6bcf\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u505a\u4e00\u4e2a\u8bbe\u8ba1&#xff0c;\u4fdd\u8bc1\u6bcf\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u90fd\u6709\u4e00\u4e2a\u81ea\u5df1\u7684\u63a5\u6536\u7f13\u51b2\u533a&#xff0c;<br \/>\n\/\/ \u4e4b\u540e&#xff0c;\u5728epoll\u5f53\u4e2d&#xff0c;\u5c31\u6ca1\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6982\u5ff5&#xff0c;\u5305\u62ec\u5957\u63a5\u5b57&#xff0c;\u4e00\u8d77\u88ab\u5c01\u88c5\u6210\u4e3a\u8fde\u63a5&#xff1a;connection<\/p>\n<p>class Connection;<br \/>\nusing handler_t &#061; std::function&lt;void(Connection *conn)&gt;;<\/p>\n<p>\/\/ \u672a\u6765\u6211\u4eec\u7684\u670d\u52a1\u5668&#xff0c;\u4e00\u5207\u90fd\u662fConnection&#xff0c; \u5bf9\u6211\u4eec\u6765\u8bb2&#xff1a;listensockfd\u4e5f\u662f\u4e00\u6837<br \/>\nclass Connection<br \/>\n{<\/p>\n<p>public:<br \/>\n    Connection(int sockfd) : _sockfd(sockfd)<br \/>\n    {<br \/>\n    }<\/p>\n<p>    \/\/ \u6ce8\u518c\u5bf9\u5e94\u7684\u4e8b\u4ef6\u5904\u7406\u65b9\u6cd5<br \/>\n    void RegisterHandler(handler_t recver, handler_t sender, handler_t excepter)<br \/>\n    {<br \/>\n        _handler_recver &#061; recver;<br \/>\n        _handler_sender &#061; sender;<br \/>\n        _handler_excepter &#061; excepter;<br \/>\n    }<\/p>\n<p>    void SetEvents(uint32_t events)<br \/>\n    {<br \/>\n        _events &#061; events;<br \/>\n    }<br \/>\n    uint32_t Events()<br \/>\n    {<br \/>\n        return _events;<br \/>\n    }<br \/>\n    int Sockfd()<br \/>\n    {<br \/>\n        return _sockfd;<br \/>\n    }<\/p>\n<p>    ~Connection() {}<\/p>\n<p>private:<br \/>\n    int _sockfd;<\/p>\n<p>    uint32_t _events; \/\/ \u6240\u5173\u5fc3\u7684\u4e8b\u4ef6<\/p>\n<p>    std::string _inbuffer; \/\/ \u4f7f\u7528string\u6765\u5145\u5f53\u7f13\u51b2\u533a\u53ea\u80fd\u6ee1\u8db3\u6b64\u4ee3\u7801\u7684\u9700\u6c42&#xff08;\u5728\u7f51\u7edc\u901a\u4fe1\u4e2d\u4f20\u4e8c\u8fdb\u5236&#xff0c;\u5c31\u4e0d\u884c&#xff09;<br \/>\n    std::string _outbuffer;<br \/>\n    handler_t _handler_recver;   \/\/ \u5904\u7406\u8bfb\u53d6<br \/>\n    handler_t _handler_sender;   \/\/ \u5904\u7406\u5199\u5165<br \/>\n    handler_t _handler_excepter; \/\/ \u5904\u7406\u5f02\u5e38<br \/>\n};<\/p>\n<p><span style=\"color:#0d0016\">TcpServer.hpp<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u6682\u65f6\u79f0\u505aTcpServer<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u4f7f\u7528TcpServer\u5c06\u6240\u6709\u7684connections\u7ba1\u7406\u8d77\u6765<\/span><\/p>\n<p><span style=\"color:#0d0016\">key: sockfd<\/span><\/p>\n<p><span style=\"color:#0d0016\">value: Connection*<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u4f18\u52bf&#xff1a;\u80fd\u591f\u5f88\u5feb\u7684\u901a\u8fc7\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u6765\u627e\u5230connection\u5bf9\u8c61&#xff0c;\u5728\u672a\u6765\u9ad8\u6548\u6267\u884cconnection\u5bf9\u5e94\u7684\u5904\u7406\u4e8b\u4ef6\u7684\u65b9\u6cd5<\/span><\/p>\n<p> \u00a0 \u00a0 std::unordered_map&lt;int, Connection *&gt; _connections; <\/p>\n<h2 id=\"%E5%85%88%E5%AE%8C%E6%88%90Listen%E5%A5%97%E6%8E%A5%E5%AD%97%E7%9A%84%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%EF%BC%9A%C2%A0\"><span style=\"color:#0d0016\">\u5148\u5b8c\u6210Listen\u5957\u63a5\u5b57\u7684\u4e8b\u4ef6\u5904\u7406&#xff1a;\u00a0<\/span><\/h2>\n<p><span style=\"color:#0d0016\">Listener.hpp<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u7528\u4e8e\u7edf\u4e00\u7ba1\u7406listen\u5957\u63a5\u5b57&#xff0c;\u4ee5\u53ca\u83b7\u53d6\u65b0\u8fde\u63a5<\/span><\/p>\n<p> #pragma once<br \/>\n#include &lt;iostream&gt;<\/p>\n<p>#include &#034;Socket.hpp&#034;<\/p>\n<p>using namespace socket_ns;<\/p>\n<p>\/\/ \u7528\u6765\u7edf\u4e00\u7ba1\u7406Listen\u5957\u63a5\u5b57<br \/>\nclass Listener<br \/>\n{<br \/>\npublic:<br \/>\n    Listener(uint16_t port) : _port(port), _listensock(std::make_unique&lt;TcpSocket&gt;())<br \/>\n    {<br \/>\n        _listensock-&gt;BuildListenSocket(_port);<br \/>\n    }<br \/>\n    int ListenSockfd()<br \/>\n    {<br \/>\n        return _listensock-&gt;Sockfd();<br \/>\n    }<br \/>\n    ~Listener() {}<\/p>\n<p>private:<br \/>\n    std::unique_ptr&lt;Socket&gt; _listensock;<br \/>\n    uint16_t _port;<br \/>\n};<\/p>\n<p><span style=\"color:#0d0016\">Main.cc<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5728TCP\u901a\u4fe1\u4e2d&#xff0c;\u7cfb\u7edf\u4ec5\u9700\u4f20\u9012\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ca\u5176\u5bf9\u5e94\u7684\u4e8b\u4ef6&#xff0c;\u5373\u53ef\u5c06\u5176\u8f6c\u6362\u4e3aconnection\u5bf9\u8c61\u3002\u670d\u52a1\u5668\u901a\u8fc7\u54c8\u5e0c\u8868\u5bf9\u8fd9\u4e9bconnection\u5bf9\u8c61\u8fdb\u884c\u7edf\u4e00\u7ba1\u7406&#xff0c;\u5e76\u4ea4\u7531epoll\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u3002\u6211\u4eec\u901a\u8fc7\u5c01\u88c5connection\u5bf9\u8c61&#xff0c;\u4ec5\u5bf9\u5916\u66b4\u9732\u5fc5\u8981\u7684\u529f\u80fd\u63a5\u53e3&#xff0c;\u786e\u4fdd\u7cfb\u7edf\u7684\u5b89\u5168\u6027\u548c\u6613\u7528\u6027\u3002<\/span><\/p>\n<p>#include &lt;memory&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\n#include &#034;Listener.hpp&#034;<br \/>\n#include &#034;Log.hpp&#034;<br \/>\n#include &#034;TcpServer.hpp&#034;<\/p>\n<p>using namespace log_ns;<\/p>\n<p>\/\/ .\/tcpserver 8888<br \/>\nint main(int argc, char *argv[])<br \/>\n{<br \/>\n    if (argc !&#061; 2)<br \/>\n    {<br \/>\n        std::cerr &lt;&lt; &#034;Usage: &#034; &lt;&lt; argv[0] &lt;&lt; &#034; local-port&#034; &lt;&lt; std::endl;<br \/>\n        exit(0);<br \/>\n    }<br \/>\n    uint16_t port &#061; std::stoi(argv[1]);<br \/>\n    EnableScreen();<br \/>\n    Listener listener(port);<\/p>\n<p>    std::unique_ptr&lt;TcpServer&gt; tsvr &#061; std::make_unique&lt;TcpServer&gt;();<\/p>\n<p>    \/\/ \u5bf9\u4e8e\u4e00\u4e2atcp\u6765\u8bb2&#xff0c;\u53ea\u9700\u8981\u4f20\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u4ee5\u53ca\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u4e8b\u4ef6&#xff0c;\u8f6c\u6362\u6210\u4e00\u4e2aconnection\u5bf9\u8c61<br \/>\n    \/\/ \u5728\u670d\u52a1\u5668\u4e2d\u4ee5\u7ba1\u7406\u54c8\u5e0c\u8868\u7684\u65b9\u5f0f\u7ba1\u7406&#xff0c;\u518d\u8ba9epoll\u6765\u7ba1\u7406<br \/>\n    \/\/ \u5c01\u88c5connection\u5bf9\u8c61&#xff0c;\u4ec5\u5bf9\u5916\u66b4\u9732\u5fc5\u8981\u7684\u529f\u80fd\u63a5\u53e3<br \/>\n    tsvr-&gt;AddConnection(listener.ListenSockfd(), EPOLLIN);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u7f16\u5199\u670d\u52a1\u5668\u4e2d\u7684AddConnection\u51fd\u6570&#xff1a;<\/span><\/p>\n<li>\u5efa\u7acb\u8fde\u63a5<\/li>\n<li>\u5c06\u8fde\u63a5\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u4e8b\u4ef6\u6ce8\u518c\u5230\u64cd\u4f5c\u7cfb\u7edf<\/li>\n<li>\u5c06\u8fde\u63a5\u6258\u7ba1\u7ed9epoll\u8fdb\u884c\u4e8b\u4ef6\u76d1\u542c&#xff08;\u5c01\u88c5Epoll&#8211;&gt;Epoller.hpp&#xff09;&#xff0c;\u5728\u670d\u52a1\u5668\u8fd9\u91cc\u53ea\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u4e8b\u4ef6\u7684\u6982\u5ff5<\/li>\n<p><span style=\"color:#0d0016\">\u672a\u6765\u53ef\u4ee5&#xff1a;<\/span><\/p>\n<p>\/\/ \u8ba1\u5212\u5b9e\u73b0 Select\u3001Poll\u3001Epoll \u4e09\u79cd\u7248\u672c<br \/>\n\/\/ \u8bbe\u8ba1\u4e00\u4e2a\u57fa\u7c7b Poller&#xff0c;\u63d0\u4f9b\u7edf\u4e00\u7684\u516c\u5171\u63a5\u53e3&#xff0c;\u5305\u62ec control\u3001wait \u548c delete \u65b9\u6cd5<br \/>\n#pragma once<\/p>\n<p>\/\/ \u672a\u6765\u8fd8\u53ef\u4ee5Select\u3001poll\u3001epoll\u4e09\u4e2a\u7248\u672c\u90fd\u5b9e\u73b0<br \/>\n\/\/ \u505a\u4e00\u4e2a\u57fa\u7c7bpoller&#xff0c;\u63d0\u4f9b\u516c\u5171\u7684\u63a5\u53e3&#xff0c;\u66b4\u9732\u51facontrol\u3001wait\u3001delete\u3002<br \/>\nclass Multiplex<br \/>\n{<br \/>\npublic:<br \/>\nprivate:<br \/>\n};<\/p>\n<p>class Epoller : public Multiplex<br \/>\n{<br \/>\n};<\/p>\n<p>class Poller : public Multiplex<br \/>\n{<br \/>\n};<\/p>\n<p>class Selector : public Multiplex<br \/>\n{<br \/>\n}; <\/p>\n<p><span style=\"color:#0d0016\">\u672c\u7bc7\u6587\u7ae0\u4ec5\u5b9e\u73b0Epoller\u7248\u672c\u3002<\/span><\/p>\n<h3 id=\"Common.hpp\"><span style=\"color:#0d0016\">Common.hpp<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u5c06\u539f\u6765\u5c01\u88c5\u5728\u5404\u6587\u4ef6\u4e2d\u7684\u679a\u4e3e\u5e38\u91cf\u63d0\u53d6\u51fa\u6765&#xff08;\u9519\u8bef\u7801\u7b49\u7ea7&#xff09;&#xff0c;\u653e\u5728\u4e00\u4e2aCommon.hpp\u5f53\u4e2d<\/span><\/p>\n<p>#pragma once<\/p>\n<p>enum<br \/>\n{<br \/>\n    SOCKET_ERROR &#061; 1,<br \/>\n    BIND_ERROR,<br \/>\n    LISTEN_ERROR,<br \/>\n    EPOLL_CREATE_ERROR<br \/>\n}; <\/p>\n<h3 id=\"Epoller.hpp\"><span style=\"color:#0d0016\">Epoller.hpp<\/span><\/h3>\n<\/p>\n<p><span style=\"color:#0d0016\">\u57fa\u4e8eepoll\u7684\u4e8b\u4ef6\u7ba1\u7406&#xff1a;<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">AddEvent\u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ca\u5176\u4e8b\u4ef6\u6ce8\u518c\u5230epoll\u5b9e\u4f8b\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">Wait\u65b9\u6cd5\u8c03\u7528epoll_wait\u83b7\u53d6\u5c31\u7eea\u4e8b\u4ef6\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u8bbe\u8ba1\u652f\u6301\u672a\u6765\u6269\u5c55select\u548cpoll&#xff08;\u7ee7\u627f\u81eaMultiplex\u57fa\u7c7b&#xff09;\u3002<\/span><\/p>\n<\/li>\n<\/ul>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\n#include &#034;Log.hpp&#034;<br \/>\n#include &#034;Common.hpp&#034;<br \/>\n\/\/ \u672a\u6765\u8fd8\u53ef\u4ee5Select\u3001poll\u3001epoll\u4e09\u4e2a\u7248\u672c\u90fd\u5b9e\u73b0<br \/>\n\/\/ \u505a\u4e00\u4e2a\u57fa\u7c7bpoller&#xff0c;\u63d0\u4f9b\u516c\u5171\u7684\u63a5\u53e3&#xff0c;\u66b4\u9732\u51facontrol\u3001wait\u3001delete\u3002<br \/>\nusing namespace log_ns;<br \/>\nstatic const int gsize &#061; 128;<\/p>\n<p>class Multiplex<br \/>\n{<br \/>\npublic:<br \/>\n    virtual ~Multiplex() &#061; default; \/\/ \u6dfb\u52a0\u865a\u6790\u6784\u51fd\u6570<br \/>\n    virtual bool AddEvent(int fd, u_int32_t events) &#061; 0;<br \/>\n    virtual int Wait(struct epoll_event revs[], int num, int timeout) &#061; 0; \/\/ \u4e34\u65f6\u65b9\u6848<\/p>\n<p>private:<br \/>\n};<\/p>\n<p>class Epoller : public Multiplex<br \/>\n{<br \/>\npublic:<br \/>\n    Epoller()<br \/>\n    {<br \/>\n        _epfd &#061; ::epoll_create(gsize);<br \/>\n        if (_epfd &lt; 0)<br \/>\n        {<br \/>\n            LOG(FATAL, &#034;epoll create error\\\\n&#034;);<br \/>\n            exit(EPOLL_CREATE_ERROR);<br \/>\n        }<br \/>\n        LOG(INFO, &#034;epoll create success, epfd: %d&#034;, _epfd);<br \/>\n    }<\/p>\n<p>    std::string EventsToString(u_int32_t events)<br \/>\n    {<br \/>\n        std::string eventsstr;<br \/>\n        if (events &amp; EPOLLIN)<br \/>\n            eventsstr &#061; &#034;EPOLLIN &#034;;<br \/>\n        if (events &amp; EPOLLOUT)<br \/>\n            eventsstr &#043;&#061; &#034;| EPOLLOUT&#034;;<br \/>\n        if (events &amp; EPOLLET)<br \/>\n            eventsstr &#043;&#061; &#034;| EPOLLET&#034;;<\/p>\n<p>        return eventsstr;<br \/>\n    }<\/p>\n<p>    bool AddEvent(int fd, u_int32_t events) override<br \/>\n    {<br \/>\n        struct epoll_event ev;<br \/>\n        ev.events &#061; events;<br \/>\n        ev.data.fd &#061; fd;<\/p>\n<p>        int n &#061; ::epoll_ctl(_epfd, EPOLL_CTL_ADD, fd, &amp;ev);<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            LOG(ERROR, &#034;epoll_ctl add %d events %s is fail&#8230;\\\\n&#034;, fd, EventsToString(events).c_str());<br \/>\n            return false;<br \/>\n        }<br \/>\n        LOG(INFO, &#034;epoll_ctl add %d events %s is success&#8230;\\\\n&#034;, fd, EventsToString(events).c_str());<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    int Wait(struct epoll_event revs[], int num, int timeout) override<br \/>\n    {<br \/>\n        return ::epoll_wait(_epfd, revs, num, timeout);<br \/>\n    }<br \/>\n    ~Epoller()<br \/>\n    {<br \/>\n    }<\/p>\n<p>private:<br \/>\n    int _epfd;<br \/>\n};<\/p>\n<p>\/\/ class Poller : public Multiplex<br \/>\n\/\/ {<br \/>\n\/\/ };<\/p>\n<p>\/\/ class Selector : public Multiplex<br \/>\n\/\/ {<br \/>\n\/\/ }; <\/p>\n<h3 id=\"Listener.hpp\"><span style=\"color:#0d0016\">Listener.hpp<\/span><\/h3>\n<\/p>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">\u76d1\u542c\u5957\u63a5\u5b57\u7ba1\u7406&#xff1a;\u521b\u5efa\u5e76\u7ed1\u5b9a\u76d1\u542c\u5957\u63a5\u5b57&#xff0c;\u63a5\u53d7\u65b0\u8fde\u63a5&#xff08;Accepter\u65b9\u6cd5&#xff09;\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u4e0eTcpServer\u96c6\u6210&#xff1a;\u5c06\u76d1\u542c\u5957\u63a5\u5b57\u6ce8\u518c\u5230TcpServer\u4e2d&#xff0c;\u5904\u7406EPOLLIN\u4e8b\u4ef6&#xff08;\u65b0\u8fde\u63a5\u5230\u8fbe&#xff09;\u3002<\/span><\/p>\n<\/li>\n<\/ul>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<\/p>\n<p>#include &#034;Socket.hpp&#034;<br \/>\n#include &#034;Common.hpp&#034;<br \/>\n#include &#034;Connection.hpp&#034;<br \/>\nusing namespace socket_ns;<\/p>\n<p>\/\/ \u7528\u6765\u7edf\u4e00\u7ba1\u7406Listen\u5957\u63a5\u5b57<br \/>\nclass Listener<br \/>\n{<br \/>\npublic:<br \/>\n    Listener(uint16_t port) : _port(port), _listensock(std::make_unique&lt;TcpSocket&gt;())<br \/>\n    {<br \/>\n        _listensock-&gt;BuildListenSocket(_port);<br \/>\n    }<br \/>\n    int ListenSockfd()<br \/>\n    {<br \/>\n        return _listensock-&gt;Sockfd();<br \/>\n    }<br \/>\n    void Accepter(Connection *conn)<br \/>\n    {<br \/>\n        LOG(DEBUG, &#034;%d socker ready\\\\n&#034;, conn-&gt;Sockfd());<br \/>\n    }<br \/>\n    ~Listener() {}<\/p>\n<p>private:<br \/>\n    std::unique_ptr&lt;Socket&gt; _listensock;<br \/>\n    uint16_t _port;<br \/>\n};<\/p>\n<h3 id=\"TcpServer.hpp\"><span style=\"color:#0d0016\">TcpServer.hpp<\/span><\/h3>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">\u7edf\u4e00\u7ba1\u7406\u8fde\u63a5&#xff1a;\u901a\u8fc7\u54c8\u5e0c\u8868std::unordered_map&lt;int, Connection*&gt;\u5feb\u901f\u6839\u636e\u6587\u4ef6\u63cf\u8ff0\u7b26\u67e5\u627eConnection\u5bf9\u8c61\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b&#xff1a;\u4f9d\u8d56Epoller\u7c7b\u5b9e\u73b0\u4e8b\u4ef6\u76d1\u542c&#xff0c;\u901a\u8fc7epoll_wait\u83b7\u53d6\u5c31\u7eea\u4e8b\u4ef6&#xff0c;\u5e76\u6839\u636e\u4e8b\u4ef6\u7c7b\u578b&#xff08;\u8bfb\/\u5199&#xff09;\u6d3e\u53d1\u5230\u5bf9\u5e94\u7684Connection\u5904\u7406\u51fd\u6570\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u670d\u52a1\u5668\u542f\u52a8&#xff1a;\u901a\u8fc7Start\u65b9\u6cd5\u8fdb\u5165\u4e8b\u4ef6\u5faa\u73af&#xff0c;\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002<\/span><\/p>\n<\/li>\n<\/ul>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;unordered_map&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &#034;Connection.hpp&#034;<br \/>\n#include &#034;Epoller.hpp&#034;<br \/>\nusing namespace log_ns;<\/p>\n<p>\/\/ \u5e95\u5c42\u662f\u4f7f\u7528epoll&#xff0c;\u6682\u65f6\u79f0\u505aTcpServer<br \/>\nclass TcpServer<br \/>\n{<br \/>\n    static const int gnum &#061; 64;<\/p>\n<p>public:<br \/>\n    TcpServer() : _epoller(std::make_unique&lt;Epoller&gt;()), _isrunning(false)<br \/>\n    {<br \/>\n    }<\/p>\n<p>    void AddConnection(int fd, uint32_t events, handler_t recver, handler_t sender, handler_t excepter) \/\/ \u7a0d\u540e\u518d\u8c03\u6574<br \/>\n    {<br \/>\n        \/\/ 1.\u6784\u5efa\u4e00\u4e2aconnection<br \/>\n        Connection *conn &#061; new Connection(fd);<br \/>\n        conn-&gt;SetEvents(events);<br \/>\n        \/\/ TODO, \u8bbe\u7f6e\u5bf9connection\u7684\u4e0a\u5c42\u5904\u7406&#xff0c; \u5373&#xff0c; \u5982\u679c\u8be5connection\u5c31\u7eea\u88ab\u6fc0\u6d3b&#xff0c;\u8be5\u5982\u4f55\u5904\u7406&#xff1f;<br \/>\n        conn-&gt;RegisterHandler(recver, sender, excepter);<\/p>\n<p>        \/\/ 2.fd\u548cevents\u5199\u900f\u5230OS\u4e2d&#xff0c;\u6258\u7ba1\u7ed9epoll<br \/>\n        if (!_epoller-&gt;AddEvent(conn-&gt;Sockfd(), conn-&gt;Events()))<br \/>\n            return;<\/p>\n<p>        \/\/ 3.\u6258\u7ba1\u7ed9_connection<br \/>\n        _connections.insert(std::make_pair(fd, conn));<br \/>\n    }<br \/>\n    \/\/ \u5bf9\u4e8e\u670d\u52a1\u5668\u6765\u8bb2&#xff0c;\u53ea\u77e5\u9053\u81ea\u5df1\u5728\u7ba1\u7406connection<br \/>\n    void Start() \/\/ TODO<br \/>\n    {<br \/>\n        int timeout &#061; -1; \/\/ \u975e\u963b\u585e<br \/>\n        _isrunning &#061; true;<br \/>\n        while (true)<br \/>\n        {<br \/>\n            \/\/ \u4eceepoll\u83b7\u53d6\u5c31\u7eea\u4e8b\u4ef6<br \/>\n            int n &#061; _epoller-&gt;Wait(revs, gnum, timeout);<br \/>\n            \/\/ \u53ea\u6709n &gt; 0\u624d\u4f1a\u5904\u7406&#xff0c;\u6240\u4ee5\u4e0d\u7528\u518d\u5224\u65adn<br \/>\n            for (int i &#061; 0; i &lt; n; i&#043;&#043;)<br \/>\n            {<br \/>\n                int sockfd &#061; revs[i].data.fd;<br \/>\n                uint32_t revents &#061; revs[i].events;<br \/>\n                \/\/ \u5c06epoll\u53d1\u751f\u7684\u9519\u8bef\u6216\u6302\u65ad&#xff0c;\u8ba9\u8fd9\u4e9b\u4e8b\u4ef6\u5168\u90e8\u653e\u5728\u8bfb\u5199\u4e8b\u4ef6\u5c31\u7eea\u4e2d\u5ef6\u8fdf\u5904\u7406<br \/>\n                if (revents &amp; EPOLLERR)<br \/>\n                    revents |&#061; (EPOLLIN | EPOLLOUT);<br \/>\n                if (revents &amp; EPOLLHUP)<br \/>\n                    revents |&#061; (EPOLLIN | EPOLLOUT);<\/p>\n<p>                if (revents &amp; EPOLLIN) \/\/<br \/>\n                {<br \/>\n                    if (IsConnectionExist(sockfd) &amp;&amp; _connections[sockfd]-&gt;_handler_recver)<br \/>\n                    {<br \/>\n                        \/\/ \u8bfb\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u6d3e\u53d1\u7ed9\u5bf9\u5e94\u7684connection<br \/>\n                        _connections[sockfd]-&gt;_handler_recver(_connections[sockfd]);<br \/>\n                    }<br \/>\n                }<br \/>\n                if (revents &amp; EPOLLOUT)<br \/>\n                {<br \/>\n                    if (IsConnectionExist(sockfd) &amp;&amp; _connections[sockfd]-&gt;_handler_sender)<br \/>\n                    {<br \/>\n                        \/\/ \u5199\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u6d3e\u53d1\u7ed9\u5bf9\u5e94\u7684connection<br \/>\n                        _connections[sockfd]-&gt;_handler_sender(_connections[sockfd]);<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        _isrunning &#061; false;<br \/>\n    }<\/p>\n<p>    bool IsConnectionExist(int sockfd)<br \/>\n    {<br \/>\n        \/\/ \u5224\u65ad\u4e00\u4e2asockfd\u662f\u5426\u5b58\u5728\u4e8econnections(\u54c8\u5e0c\u8868)\u5f53\u4e2d<br \/>\n        return _connections.find(sockfd) !&#061; _connections.end();<br \/>\n    }<\/p>\n<p>    ~TcpServer() {}<\/p>\n<p>private:<br \/>\n    \/\/  \u4f7f\u7528TcpServer\u5c06\u6240\u6709\u7684connections\u7ba1\u7406\u8d77\u6765<br \/>\n    \/\/  key: sockfd<br \/>\n    \/\/  value: Connection*<br \/>\n    \/\/  \u4f18\u52bf&#xff1a;\u80fd\u591f\u5f88\u5feb\u7684\u901a\u8fc7\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u6765\u627e\u5230connection\u5bf9\u8c61&#xff0c;\u5728\u672a\u6765\u9ad8\u6548\u6267\u884cconnection\u5bf9\u5e94\u7684\u5904\u7406\u4e8b\u4ef6\u7684\u65b9\u6cd5<br \/>\n    std::unordered_map&lt;int, Connection *&gt; _connections;<\/p>\n<p>    std::unique_ptr&lt;Multiplex&gt; _epoller;<br \/>\n    \/\/ \u5224\u65ad\u670d\u52a1\u5668\u662f\u5426\u5f00\u542f<br \/>\n    bool _isrunning;<\/p>\n<p>    struct epoll_event revs[gnum];<br \/>\n}; <\/p>\n<h3 id=\"Main.cc\"><span style=\"color:#0d0016\">Main.cc<\/span><\/h3>\n<\/p>\n<ul>\n<li>\n<p><span style=\"color:#0d0016\">\u521d\u59cb\u5316\u670d\u52a1\u5668&#xff1a;\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570&#xff08;\u7aef\u53e3\u53f7&#xff09;&#xff0c;\u521b\u5efaListener\u548cTcpServer\u5b9e\u4f8b\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u6ce8\u518c\u76d1\u542c\u5957\u63a5\u5b57&#xff1a;\u5c06\u76d1\u542c\u5957\u63a5\u5b57\u7684\u4e8b\u4ef6&#xff08;EPOLLIN&#xff09;\u548c\u5904\u7406\u51fd\u6570&#xff08;Accepter&#xff09;\u7ed1\u5b9a\u5230TcpServer\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af&#xff1a;\u8c03\u7528tsvr-&gt;Start()\u8fdb\u5165epoll\u4e8b\u4ef6\u76d1\u542c\u72b6\u6001\u3002<\/span><\/p>\n<\/li>\n<\/ul>\n<p>#include &lt;memory&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\n#include &#034;Listener.hpp&#034;<br \/>\n#include &#034;Log.hpp&#034;<br \/>\n#include &#034;TcpServer.hpp&#034;<\/p>\n<p>using namespace log_ns;<\/p>\n<p>\/\/ .\/tcpserver 8888<br \/>\nint main(int argc, char *argv[])<br \/>\n{<br \/>\n    if (argc !&#061; 2)<br \/>\n    {<br \/>\n        std::cerr &lt;&lt; &#034;Usage: &#034; &lt;&lt; argv[0] &lt;&lt; &#034; local-port&#034; &lt;&lt; std::endl;<br \/>\n        exit(0);<br \/>\n    }<br \/>\n    uint16_t port &#061; std::stoi(argv[1]);<br \/>\n    EnableScreen();<br \/>\n    Listener listener(port);<\/p>\n<p>    std::unique_ptr&lt;TcpServer&gt; tsvr &#061; std::make_unique&lt;TcpServer&gt;();<\/p>\n<p>    \/\/ \u5bf9\u4e8e\u4e00\u4e2atcp\u6765\u8bb2&#xff0c;\u53ea\u9700\u8981\u4f20\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u4ee5\u53ca\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u4e8b\u4ef6&#xff0c;\u8f6c\u6362\u6210\u4e00\u4e2aconnection\u5bf9\u8c61<br \/>\n    \/\/ \u5728\u670d\u52a1\u5668\u4e2d\u4ee5\u7ba1\u7406\u54c8\u5e0c\u8868\u7684\u65b9\u5f0f\u7ba1\u7406&#xff0c;\u518d\u8ba9epoll\u6765\u7ba1\u7406<br \/>\n    \/\/ \u5c01\u88c5connection\u5bf9\u8c61&#xff0c;\u4ec5\u5bf9\u5916\u66b4\u9732\u5fc5\u8981\u7684\u529f\u80fd\u63a5\u53e3<br \/>\n    \/\/ \u5904\u7406listen\u5957\u63a5\u5b57\u7684\u63a5\u53e3<br \/>\n    tsvr-&gt;AddConnection(<br \/>\n        listener.ListenSockfd(),<br \/>\n        EPOLLIN | EPOLLET,<br \/>\n        std::bind(&amp;Listener::Accepter, &amp;listener, std::placeholders::_1),<br \/>\n        nullptr,<br \/>\n        nullptr);<\/p>\n<p>    \/\/ \u5f00\u542f\u670d\u52a1\u5668<br \/>\n    tsvr-&gt;Start();<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u8fd0\u884c\u7ed3\u679c&#xff1a;\u00a0<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"839\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073145-684e76e1be0bb.png\" width=\"1622\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u4ee5\u4e0a\u4ee3\u7801&#xff1a;<\/span><\/p>\n<p>.<br \/>\n\u251c\u2500\u2500 Common.hpp<br \/>\n\u251c\u2500\u2500 Connection.hpp<br \/>\n\u251c\u2500\u2500 Epoller.hpp<br \/>\n\u251c\u2500\u2500 InetAddr.hpp<br \/>\n\u251c\u2500\u2500 Listener.hpp<br \/>\n\u251c\u2500\u2500 LockGuard.hpp<br \/>\n\u251c\u2500\u2500 Log.hpp<br \/>\n\u251c\u2500\u2500 Main.cc<br \/>\n\u251c\u2500\u2500 Makefile<br \/>\n\u251c\u2500\u2500 Socket.hpp<br \/>\n\u2514\u2500\u2500 TcpServer.hpp<\/p>\n<p>0 directories, 11 files<\/p>\n<p><span style=\"color:#0d0016\">\u5b9e\u73b0\u4e86\u4e00\u4e2a\u57fa\u4e8eepoll\u7684\u4e8b\u4ef6\u9a71\u52a8TCP\u670d\u52a1\u5668&#xff0c;\u6838\u5fc3\u529f\u80fd\u5305\u62ec\u8fde\u63a5\u7ba1\u7406\u3001\u4e8b\u4ef6\u76d1\u542c\u3001\u65b0\u8fde\u63a5\u63a5\u53d7\u53ca\u8bfb\u5199\u4e8b\u4ef6\u5904\u7406\u3002\u901a\u8fc7\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u548c\u6a21\u5757\u5316\u5c01\u88c5&#xff0c;\u63d0\u4f9b\u4e86\u9ad8\u6548\u3001\u53ef\u6269\u5c55\u7684\u670d\u52a1\u5668\u6846\u67b6&#xff0c;\u4f46\u9700\u8fdb\u4e00\u6b65\u5b8c\u5584\u5f02\u5e38\u5904\u7406\u3001\u8d44\u6e90\u7ba1\u7406\u548c\u4e8c\u8fdb\u5236\u6570\u636e\u4f20\u8f93\u652f\u6301\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u6211\u4eec\u5c06TcpServer\u7c7b\u66f4\u540d\u4e3aReactor&#xff0c;\u56e0\u6b64\u9700\u8981\u5c06\u4ee3\u7801\u4e2d\u6240\u6709TcpServer\u7684\u5f15\u7528\u7edf\u4e00\u66ff\u6362\u4e3aReactor\u3002<\/span><\/p>\n<h2 id=\"Reactor(%E5%8F%8D%E5%BA%94%E5%A0%86)%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F\"><span style=\"color:#0d0016\">Reactor(\u53cd\u5e94\u5806)\u8bbe\u8ba1\u6a21\u5f0f<\/span><\/h2>\n<p><span style=\"color:#0d0016\">Reactor \u662f\u4e00\u79cd\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u4e8b\u4ef6\u6d41\u7684\u8bbe\u8ba1\u6a21\u5f0f&#xff0c;\u5e7f\u6cdb\u5e94\u7528\u4e8e\u73b0\u4ee3\u7f16\u7a0b\u6846\u67b6\u548c\u5e93\u4e2d&#xff0c;\u7279\u522b\u662f\u5728\u9700\u8981\u5904\u7406\u9ad8\u5e76\u53d1\u3001\u975e\u963b\u585e I\/O \u7684\u573a\u666f\u4e2d\u3002\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u662f\u5c06\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f16\u7a0b\u6a21\u578b\u4e0e\u4e8b\u4ef6\u5206\u53d1\u673a\u5236\u76f8\u7ed3\u5408&#xff0c;\u901a\u8fc7\u4e8b\u4ef6\u5faa\u73af&#xff08;Event Loop&#xff09;\u6765\u76d1\u542c\u548c\u5206\u53d1\u4e8b\u4ef6&#xff0c;\u4ece\u800c\u5b9e\u73b0\u9ad8\u6548\u7684\u4efb\u52a1\u5904\u7406\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5728Reactor\u8bbe\u8ba1\u6a21\u5f0f\u5f53\u4e2d&#xff0c;\u5f00\u542f\u670d\u52a1\u5668\u7684Start\u51fd\u6570\u66f4\u559c\u6b22\u53eb\u505a&#xff1a;\u4e8b\u4ef6\u6d3e\u53d1:Dispatcher<\/span><\/p>\n<p>    void Start()&#8212;-&gt; Dispatcher() \/\/ \u6539\u4e3a\u4e8b\u4ef6\u6d3e\u53d1<\/p>\n<p><span style=\"color:#0d0016\">\u5728\u6211\u4eec\u7684\u4ee3\u7801\u4e2d&#xff1a;Reactor\u7c7b\u4f3c\u4e00\u4e2aconnection\u7684\u5bb9\u5668&#xff0c;\u6838\u5fc3\u5de5\u4f5c\u5c31\u662f<\/span><\/p>\n<p><span style=\"color:#0d0016\">1. \u7ba1\u7406connection\u548c\u5bf9\u5e94\u7684\u5185\u6838\u4e8b\u4ef6<\/span><\/p>\n<p><span style=\"color:#0d0016\">2. \u4e8b\u4ef6\u6d3e\u53d1<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u4f18\u5316\u4ee3\u7801\u7ed3\u6784&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">LoopOnce(int timeout)\u00a0\u51fd\u6570\u7684\u4f5c\u7528\u662f\u00a0\u5355\u6b21\u5904\u7406\u5df2\u5c31\u7eea\u7684 epoll \u4e8b\u4ef6<\/span><\/p>\n<p>    \/\/ \u7528\u4e8e\u68c0\u6d4b\u83b7\u53d6\u4e00\u6b21\u5df2\u7ecf\u5c31\u7eea\u7684\u4e8b\u4ef6<br \/>\n    void LoopOnce(int timeout)<br \/>\n    {<br \/>\n        \/\/ \u4eceepoll\u83b7\u53d6\u5c31\u7eea\u4e8b\u4ef6<br \/>\n        int n &#061; _epoller-&gt;Wait(revs, gnum, timeout);<br \/>\n        \/\/ \u53ea\u6709n &gt; 0\u624d\u4f1a\u5904\u7406&#xff0c;\u6240\u4ee5\u4e0d\u7528\u518d\u5224\u65adn<br \/>\n        for (int i &#061; 0; i &lt; n; i&#043;&#043;)<br \/>\n        {<br \/>\n            int sockfd &#061; revs[i].data.fd;<br \/>\n            uint32_t revents &#061; revs[i].events;<br \/>\n            \/\/ \u5c06epoll\u53d1\u751f\u7684\u9519\u8bef\u6216\u6302\u65ad&#xff0c;\u8ba9\u8fd9\u4e9b\u4e8b\u4ef6\u5168\u90e8\u653e\u5728\u8bfb\u5199\u4e8b\u4ef6\u5c31\u7eea\u4e2d\u5ef6\u8fdf\u5904\u7406<br \/>\n            if (revents &amp; EPOLLERR)<br \/>\n                revents |&#061; (EPOLLIN | EPOLLOUT);<br \/>\n            if (revents &amp; EPOLLHUP)<br \/>\n                revents |&#061; (EPOLLIN | EPOLLOUT);<\/p>\n<p>            if (revents &amp; EPOLLIN) \/\/<br \/>\n            {<br \/>\n                if (IsConnectionExist(sockfd) &amp;&amp; _connections[sockfd]-&gt;_handler_recver)<br \/>\n                {<br \/>\n                    \/\/ \u8bfb\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u6d3e\u53d1\u7ed9\u5bf9\u5e94\u7684connection<br \/>\n                    _connections[sockfd]-&gt;_handler_recver(_connections[sockfd]);<br \/>\n                }<br \/>\n            }<br \/>\n            if (revents &amp; EPOLLOUT)<br \/>\n            {<br \/>\n                if (IsConnectionExist(sockfd) &amp;&amp; _connections[sockfd]-&gt;_handler_sender)<br \/>\n                {<br \/>\n                    \/\/ \u5199\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u6d3e\u53d1\u7ed9\u5bf9\u5e94\u7684connection<br \/>\n                    _connections[sockfd]-&gt;_handler_sender(_connections[sockfd]);<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u5bf9\u4e8e\u670d\u52a1\u5668\u6765\u8bb2&#xff0c;\u53ea\u77e5\u9053\u81ea\u5df1\u5728\u7ba1\u7406connection<br \/>\n    void Dispatcher() \/\/ \u6539\u4e3a\u4e8b\u4ef6\u6d3e\u53d1<br \/>\n    {<br \/>\n        int timeout &#061; -1; \/\/ \u975e\u963b\u585e<br \/>\n        _isrunning &#061; true;<br \/>\n        while (true)<br \/>\n        {<br \/>\n            LoopOnce(timeout);<br \/>\n        }<br \/>\n        _isrunning &#061; false;<br \/>\n    } <\/p>\n<h2 id=\"%E5%A4%84%E7%90%86listen%E5%A5%97%E6%8E%A5%E5%AD%97%E7%9A%84%E6%96%B0%E8%BF%9E%E6%8E%A5%EF%BC%9A%E5%A6%82%E4%BD%95%E5%A4%84%E7%90%86Listen%E7%B1%BB%E4%B8%AD%E7%9A%84Accepter%3F\"><span style=\"color:#0d0016\">\u5904\u7406listen\u5957\u63a5\u5b57\u7684\u65b0\u8fde\u63a5&#xff1a;\u5982\u4f55\u5904\u7406Listen\u7c7b\u4e2d\u7684Accepter?<\/span><\/h2>\n<h4 id=\"%E5%9C%A8Connection%E7%B1%BB%E4%B8%AD%E5%AE%9A%E4%B9%89%E4%B8%80%E4%B8%AAReactor%E6%8C%87%E9%92%88%EF%BC%8C%E5%B9%B6%E5%AE%9E%E7%8E%B0Connection%E5%92%8CReactor%E4%B9%8B%E9%97%B4%E7%9A%84%E4%BA%92%E7%9B%B8%E5%BC%95%E7%94%A8\"><span style=\"color:#0d0016\">\u5728Connection\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2aReactor\u6307\u9488&#xff0c;\u5e76\u5b9e\u73b0Connection\u548cReactor\u4e4b\u95f4\u7684\u4e92\u76f8\u5f15\u7528<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u8fd9\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u5728\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784\u4e2d\u975e\u5e38\u5e38\u89c1&#xff0c;\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684Reactor\u6a21\u5f0f\u3002Connection\u5bf9\u8c61\u4ee3\u8868\u4e00\u4e2a\u7f51\u7edc\u8fde\u63a5&#xff0c;\u800cReactor\u5bf9\u8c61\u8d1f\u8d23\u76d1\u542c\u548c\u5904\u7406\u4e8b\u4ef6\u3002\u901a\u8fc7\u8fd9\u79cd\u4e92\u76f8\u5f15\u7528\u7684\u65b9\u5f0f&#xff0c;Connection\u53ef\u4ee5\u901a\u77e5Reactor\u5904\u7406\u4e8b\u4ef6&#xff0c;\u800cReactor\u4e5f\u53ef\u4ee5\u7ba1\u7406\u591a\u4e2aConnection\u7684\u751f\u547d\u5468\u671f\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u7531\u4e8e\u91c7\u7528ET\u6a21\u5f0f&#xff0c;\u7cfb\u7edf\u4e0d\u4f1a\u4e3b\u52a8\u901a\u77e5\u65b0\u8fde\u63a5\u7684\u5230\u6765\u3002\u4e3a\u4e86\u786e\u4fdd\u5728\u975e\u963b\u585e\u72b6\u6001\u4e0b\u80fd\u591f\u4e00\u6b21\u6027\u83b7\u53d6\u6240\u6709\u8fde\u63a5&#xff0c;\u6211\u4eec\u9700\u8981\u901a\u8fc7\u5faa\u73af\u65b9\u5f0f\u6301\u7eed\u83b7\u53d6&#xff0c;\u76f4\u5230\u5e95\u5c42\u4e0d\u518d\u6709\u65b0\u7684\u8fde\u63a5\u4e3a\u6b62\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u7136\u800c&#xff0c;\u5728\u6700\u521d\u5c01\u88c5\u7684Socket\u4ee3\u7801\u4e2d&#xff0c;\u8fde\u63a5\u83b7\u53d6\u7684\u8bbe\u8ba1\u7684\u5c31\u662f\u9ed8\u8ba4\u7684\u963b\u585e\u6a21\u5f0f&#xff1a;<\/span><\/p>\n<p>        int Accepter(InetAddr *clientaddr) override<br \/>\n        {<br \/>\n            struct sockaddr_in client;<br \/>\n            socklen_t len &#061; sizeof(client);<br \/>\n            \/\/ 4.\u83b7\u53d6\u65b0\u94fe\u63a5<br \/>\n            \/\/ \u4ece\u76d1\u542c\u5957\u63a5\u5b57\u83b7\u53d6\u65b0\u7684\u5957\u63a5\u5b57\u3001\u83b7\u53d6\u5ba2\u6237\u7aef\u4fe1\u606f<br \/>\n            int sockfd &#061; ::accept(_sockfd, (struct sockaddr *)&amp;client, &amp;len);<br \/>\n            \/\/ \u83b7\u53d6\u8fde\u63a5\u5931\u8d25\u3001\u7ee7\u7eed\u83b7\u53d6<br \/>\n            if (sockfd &lt; 0)<br \/>\n            {<br \/>\n                LOG(WARNING, &#034;accept error\\\\n&#034;);<br \/>\n                return -1;<br \/>\n            }<br \/>\n            *clientaddr &#061; InetAddr(client);<br \/>\n            \/\/ \u83b7\u5ba2\u6210\u529f&#xff0c;\u63d0\u4f9b\u670d\u52a1<br \/>\n            return sockfd;<br \/>\n        } <\/p>\n<p><span style=\"color:#0d0016\">\u5982\u679c\u53ea\u6709\u4e00\u4e2a\u8fde\u63a5&#xff0c;\u5c06\u8be5\u8fde\u63a5\u83b7\u53d6\u540e&#xff0c;\u7531\u4e8e\u6211\u4eec\u662f\u5faa\u73af\u83b7\u53d6&#xff0c;\u8c03Accepter\u4e0d\u5c31\u963b\u585e\u5728accept\u4e86 ?<\/span><\/p>\n<h4 id=\"%E5%9B%A0%E6%AD%A4%E6%88%91%E4%BB%AC%E9%9C%80%E8%A6%81%E5%B0%86%E6%89%80%E6%9C%89%E7%9A%84%E6%96%87%E4%BB%B6%E6%8F%8F%E8%BF%B0%E7%AC%A6%E9%83%BD%E8%AE%BE%E7%BD%AE%E4%B8%BA%E9%9D%9E%E9%98%BB%E5%A1%9E%E3%80%82\"><span style=\"color:#ed7976\">\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5c06\u6240\u6709\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u90fd\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u3002<\/span><\/h4>\n<h4 id=\"%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95%3Afcntl\"><span style=\"color:#0d0016\">\u89e3\u51b3\u529e\u6cd5:fcntl<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u5728\u4e4b\u524d\u7684\u5b66\u4e60\u4e2d&#xff0c;\u6211\u4eec\u4e86\u89e3\u5230Linux\u7cfb\u7edf\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u63a5\u53e3&#xff1a;fcntl\u3002\u8fd9\u4e2a\u63a5\u53e3\u4e0d\u4ec5\u53ef\u4ee5\u83b7\u53d6\u548c\u8bbe\u7f6e\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u5c5e\u6027&#xff0c;\u8fd8\u80fd\u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\u663e\u5f0f\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5c06\u8be5\u65b9\u6cd5&#xff0c;\u653e\u5165Common.hpp\u4e2d<\/span><\/p>\n<p> #include &lt;iostream&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\nvoid SetNonBlock(int fd)<br \/>\n{<br \/>\n    int fl &#061; ::fcntl(fd, F_GETFL);<br \/>\n    if (fl &lt; 0)<br \/>\n    {<br \/>\n        std::cout &lt;&lt; &#034;fcntl error&#034; &lt;&lt; std::endl;<br \/>\n        return;<br \/>\n    }<br \/>\n    ::fcntl(fd, F_SETFL, fl | O_NONBLOCK);<br \/>\n} <\/p>\n<p><span style=\"color:#0d0016\">\u4fdd\u8bc1\u6240\u6709\u7684\u5957\u63a5\u5b57\u90fd\u662f\u975e\u963b\u585e&#xff1a;\u00a0<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1043\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073146-684e76e27533d.png\" width=\"1161\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f\u540e&#xff0c;\u5728\u8fdb\u884cI\/O\u64cd\u4f5c\u65f6\u53ef\u80fd\u4f1a\u7acb\u5373\u8fd4\u56deEAGAIN\u6216EWOULDBLOCK\u9519\u8bef&#xff0c;\u8fd9\u8868\u793a\u5f53\u524d\u6ca1\u6709\u6570\u636e\u53ef\u8bfb\u6216\u5199\u7f13\u51b2\u533a\u5df2\u6ee1\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b&#xff0c;\u7a0b\u5e8f\u5e94\u8be5\u4f7f\u7528select\u3001poll\u6216epoll\u7b49I\/O\u591a\u8def\u590d\u7528\u6280\u672f\u6765\u76d1\u63a7\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u72b6\u6001&#xff0c;\u5e76\u5728\u9002\u5f53\u7684\u65f6\u5019\u91cd\u8bd5I\/O\u64cd\u4f5c\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u56e0\u6b64\u6211\u4eec\u8fd9\u91cc\u5c31\u76f4\u63a5\u4f7f\u7528epoll\u6765\u76d1\u63a7&#xff0c;\u4f46\u662f\u6211\u4eec\u5fc5\u987b\u8981\u83b7\u5f97\u9519\u8bef\u7801&#xff0c;\u56e0\u6b64\u8fd8\u9700\u8981\u6539\u9020Accepter&#xff0c;\u5728\u5c06\u5957\u63a5\u5b57\u6539\u4e3afd\u4e4b\u540e&#xff0c;\u83b7\u53d6\u4e00\u4e0berrno:<\/span><\/p>\n<p>        int Accepter(InetAddr *clientaddr, int *code) override<br \/>\n        {<br \/>\n            struct sockaddr_in client;<br \/>\n            socklen_t len &#061; sizeof(client);<br \/>\n            \/\/ 4.\u83b7\u53d6\u65b0\u94fe\u63a5<br \/>\n            \/\/ \u4ece\u76d1\u542c\u5957\u63a5\u5b57\u83b7\u53d6\u65b0\u7684\u5957\u63a5\u5b57\u3001\u83b7\u53d6\u5ba2\u6237\u7aef\u4fe1\u606f<br \/>\n            int sockfd &#061; ::accept(_sockfd, (struct sockaddr *)&amp;client, &amp;len);<br \/>\n            *code &#061; errno; \/\/ \u5f97\u5230\u9519\u8bef\u7801<br \/>\n            SetNonBlock(sockfd);<\/p>\n<p>            \/\/ \u83b7\u53d6\u8fde\u63a5\u5931\u8d25\u3001\u7ee7\u7eed\u83b7\u53d6<br \/>\n            if (sockfd &lt; 0)<br \/>\n            {<br \/>\n                \/\/ LOG(WARNING, &#034;accept error\\\\n&#034;);<br \/>\n                return -1;<br \/>\n            }<br \/>\n            *clientaddr &#061; InetAddr(client);<br \/>\n            \/\/ \u83b7\u5ba2\u6210\u529f&#xff0c;\u63d0\u4f9b\u670d\u52a1<br \/>\n            return sockfd;<br \/>\n        } <\/p>\n<h3 id=\"Listener%E7%B1%BBListener.hpp%3A\"><span style=\"color:#0d0016\">Listener\u7c7bListener.hpp:<\/span><\/h3>\n<p> #pragma once<br \/>\n#include &lt;iostream&gt;<\/p>\n<p>#include &#034;Socket.hpp&#034;<br \/>\n#include &#034;Common.hpp&#034;<br \/>\n#include &#034;Reactor.hpp&#034;<br \/>\n#include &#034;Connection.hpp&#034;<br \/>\nusing namespace socket_ns;<\/p>\n<p>\/\/ \u7528\u6765\u7edf\u4e00\u7ba1\u7406Listen\u5957\u63a5\u5b57<br \/>\nclass Listener<br \/>\n{<br \/>\npublic:<br \/>\n    Listener(uint16_t port) : _port(port), _listensock(std::make_unique&lt;TcpSocket&gt;())<br \/>\n    {<br \/>\n        _listensock-&gt;BuildListenSocket(_port);<br \/>\n    }<br \/>\n    int ListenSockfd()<br \/>\n    {<br \/>\n        return _listensock-&gt;Sockfd();<br \/>\n    }<br \/>\n    void Accepter(Connection *conn)<br \/>\n    {<br \/>\n        \/\/ \u7531\u4e8e\u6211\u4eec\u91c7\u7528\u7684\u662fET\u6a21\u5f0f&#xff0c;\u5faa\u73af\u83b7\u53d6\u5e95\u5c42\u8fde\u63a5&#xff08;\u4e00\u6b21\u6027\u83b7\u53d6\u5b8c&#xff09;&#xff0c;\u975e\u963b\u585e<br \/>\n        \/\/ \u4f46\u662f\u8fd9\u91cc\u6709\u53ef\u80fd\u53ea\u6709\u4e00\u4e2alisten\u5957\u63a5\u5b57<br \/>\n        while (true)<br \/>\n        {<br \/>\n            \/\/ \u6bcf\u6b21\u5faa\u73af\u65f6&#xff0c;\u90fd\u5c06errno\u6e050<br \/>\n            errno &#061; 0;<br \/>\n            InetAddr addr;<br \/>\n            int code &#061; 0;<br \/>\n            int sockfd &#061; _listensock-&gt;Accepter(&amp;addr, &amp;code);<br \/>\n            if (sockfd &gt; 0)<br \/>\n            {<br \/>\n                \/\/ \u83b7\u53d6\u8fde\u63a5\u6210\u529f<br \/>\n                LOG(INFO, &#034;\u83b7\u53d6\u8fde\u63a5\u6210\u529f&#xff0c; \u5ba2\u6237\u7aef\u4fe1\u606f&#xff1a;%s:%d, new_sockfd: %d\\\\n&#034;, addr.Ip().c_str(), addr.Port(), sockfd);<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                \/\/ \u5931\u8d25<br \/>\n                if (code &#061;&#061; EWOULDBLOCK)<br \/>\n                {<br \/>\n                    LOG(INFO, &#034;\u5e95\u5c42\u8fde\u63a5\u5168\u90e8\u83b7\u53d6\u5b8c\u6bd5\\\\n&#034;);<br \/>\n                    break;<br \/>\n                }<br \/>\n                else if (code &#061;&#061; EINTR)<br \/>\n                {<br \/>\n                    continue;<br \/>\n                }<br \/>\n                else<br \/>\n                {<br \/>\n                    LOG(ERROR, &#034;\u83b7\u53d6\u8fde\u63a5\u5931\u8d25&#xff01;\\\\n&#034;);<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    ~Listener() {}<\/p>\n<p>private:<br \/>\n    std::unique_ptr&lt;Socket&gt; _listensock;<br \/>\n    uint16_t _port;<br \/>\n};<\/p>\n<p><span style=\"color:#0d0016\">\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/span><span style=\"color:#0d0016\">\u8fd9\u91cc\u6211\u5206\u522b\u4f7f\u7528telnet\u3001\u6d4f\u89c8\u5668\u6765\u8fde\u63a5&#xff08;\u6d4f\u89c8\u5668\u4f1a\u6709\u4e24\u4e2a\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u4e00\u4e2a\u662f\u8fde\u63a5\u5c31\u7eea\u4e5f\u5c31\u662f\u8bfb\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u5e76\u4e14\u6d4f\u89c8\u5668\u4f1a\u5411\u670d\u52a1\u5668\u53d1\u9001HTTP\u8bf7\u6c42\u6570\u636e&#8211;&gt;TCP\u4e09\u6b21\u63e1\u624b\u5efa\u7acb\u8fde\u63a5&#xff0c;\u968f\u540e\u7acb\u5373\u53d1\u9001HTTP\u8bf7\u6c42\u5934\u548c\u6570\u636e&#xff0c;\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u4e0eTelnet\u7684\u7b80\u5355\u8fde\u63a5\u884c\u4e3a\u5f62\u6210\u9c9c\u660e\u5bf9\u6bd4<\/span><span style=\"color:#0d0016\">&#xff09;<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"269\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073146-684e76e2e843d.png\" width=\"563\" \/><\/p>\n<\/p>\n<p><span style=\"color:#0d0016\">\u4ee5\u4e0a\u4ee3\u7801&#xff0c;\u53ea\u5904\u7406\u4e86listen\u5957\u63a5\u5b57\u4ee5\u53ca\u4ed6\u7684\u8bfb\u4e8b\u4ef6&#xff08;\u83b7\u53d6\u8fde\u63a5\u6210\u529f&#xff09;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u6211\u4eec\u83b7\u5f97\u4e86\u65b0\u7684\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26\u540e&#xff0c;\u4e3a\u4e86\u6709\u6548\u7ba1\u7406\u8fd9\u4e9b\u63cf\u8ff0\u7b26\u5e76\u786e\u4fdd\u540e\u7eed\u80fd\u591f\u4ee5\u975e\u963b\u585e\u65b9\u5f0f\u5904\u7406\u5176IO\u4e8b\u4ef6&#xff0c;\u9700\u8981\u5148\u5c06\u5b83\u4eec\u6dfb\u52a0\u81f3Reactor\u7ba1\u7406\u7684\u8fde\u63a5\u6c60\u4e2d\u3002<\/span><\/p>\n<p><span style=\"color:#ed7976\">\u63a5\u4e0b\u6765&#xff0c;\u6211\u4eec\u5c06\u7f16\u5199\u628a\u65b0\u521b\u5efa\u7684\u5957\u63a5\u5b57\u52a0\u5165Reactor\u7684\u8fde\u63a5\u6c60\u8fdb\u884c\u7edf\u4e00\u7ba1\u7406\u3002<\/span><\/p>\n<h2 id=\"%E5%B0%86%E6%96%B0%E5%88%9B%E5%BB%BA%E7%9A%84%E5%A5%97%E6%8E%A5%E5%AD%97%E5%8A%A0%E5%85%A5Reactor%E7%9A%84%E8%BF%9E%E6%8E%A5%E6%B1%A0\"><span style=\"color:#0d0016\">\u5c06\u65b0\u521b\u5efa\u7684\u5957\u63a5\u5b57\u52a0\u5165Reactor\u7684\u8fde\u63a5\u6c60<\/span><\/h2>\n<h3 id=\"1.%E5%9C%A8Connection%E7%B1%BB%E4%B8%AD%E8%AE%BE%E7%BD%AEReactor%E3%80%81%E4%BB%A5%E5%8F%8AConnection%E7%B1%BB%E5%9E%8B\"><span style=\"color:#0d0016\">1.\u5728Connection\u7c7b\u4e2d\u8bbe\u7f6eReactor\u3001\u4ee5\u53caConnection\u7c7b\u578b<\/span><\/h3>\n<p><span style=\"color:#0d0016\">Connection\u53ef\u4ee5\u901a\u77e5Reactor\u5904\u7406\u4e8b\u4ef6&#xff0c;\u800cReactor\u4e5f\u53ef\u4ee5\u7ba1\u7406\u591a\u4e2aConnection\u7684\u751f\u547d\u5468\u671f\u3002<\/span><\/p>\n<p>    void SetReactor(Reactor *R)<br \/>\n    {<br \/>\n        _R &#061; R;<br \/>\n    } <\/p>\n<p><span style=\"color:#0d0016\">\u5e76\u4e14\u8bbe\u7f6eConnection\u7c7b\u578b&#xff1a;<\/span><\/p>\n<p>#define ListenConnection 0<br \/>\n#define NormalConnection 1 <\/p>\n<h3 id=\"%C2%A02.%E5%B0%86%E5%BD%93%E5%89%8D%E5%AF%B9%E8%B1%A1%EF%BC%8C%E8%AE%BE%E7%BD%AE%E8%BF%9B%E5%85%A5%E6%89%80%E6%9C%89%E7%9A%84conn%E5%AF%B9%E8%B1%A1%E4%B8%AD\">\u00a0<span style=\"color:#0d0016\">2.\u5c06\u5f53\u524d\u5bf9\u8c61&#xff0c;\u8bbe\u7f6e\u8fdb\u5165\u6240\u6709\u7684conn\u5bf9\u8c61\u4e2d<\/span><\/h3>\n<p><span style=\"color:#0d0016\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"364\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073147-684e76e39076f.png\" width=\"1367\" \/><\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5728Listen\u7c7b\u4e2d&#xff1a;<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"292\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073147-684e76e3c2d32.png\" width=\"1746\" \/><\/p>\n<h4 id=\"%E6%B7%BB%E5%8A%A0HandlerConnection%E6%A8%A1%E5%9D%97\"><span style=\"color:#0d0016\">\u6dfb\u52a0<\/span><span style=\"color:#0d0016\">HandlerConnection<\/span><span style=\"color:#0d0016\">\u6a21\u5757<\/span><\/h4>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &#034;Connection.hpp&#034;<\/p>\n<p>class HandlerConnection<br \/>\n{<br \/>\npublic:<br \/>\n    void HandlerRecver(Connection *conn)<br \/>\n    {<br \/>\n    }<br \/>\n    void HandlerSender(Connection *conn)<br \/>\n    {<br \/>\n    }<br \/>\n    void HandlerExcepter(Connection *conn)<br \/>\n    {<br \/>\n    }<br \/>\n};<\/p>\n<h3 id=\"3.%E4%BF%AE%E6%AD%A3AddConnection\"><span style=\"color:#0d0016\">3.\u4fee\u6b63AddConnection<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u4f18\u5316Reactor\u7c7b\u7684AddConnection\u63a5\u53e3&#xff0c;\u5728Reactor\u4e2d\u5236\u4f5c\u65b9\u6cd5\u96c6&#xff1a;<\/span><\/p>\n<p>     \/\/ Reactor\u4e2d\u6dfb\u52a0\u5904\u7406sockfd\u7684\u65b9\u6cd5\u96c6<br \/>\n    \/\/ 1.\u5904\u7406\u65b0\u8fde\u63a5\u5230\u6765<br \/>\n    handler_t _OnConnect;<br \/>\n    \/\/ 2.\u5904\u7406\u666e\u901asockfd&#xff0c;\u4e3b\u8981\u662fIO\u5904\u7406<br \/>\n    handler_t _OnRecver;<br \/>\n    handler_t _OnSender;<br \/>\n    handler_t _OnExcept; <\/p>\n<p><span style=\"color:#0d0016\">\u8fd9\u610f\u5473\u7740\u5728Reactor\u4e2d\u53ef\u4ee5\u521d\u59cb\u5316\u6574\u4e2a\u65b9\u6cd5\u96c6<\/span><\/p>\n<p>     void SetOnConnect(handler_t OnConnect)<br \/>\n    {<br \/>\n        _OnConnect &#061; OnConnect;<br \/>\n    }<br \/>\n    void SetOnNormalHandler(handler_t recver, handler_t sender, handler_t excepter)<br \/>\n    {<br \/>\n        _OnRecver &#061; recver;<br \/>\n        _OnSender &#061; sender;<br \/>\n        _OnExcepter &#061; excepter;<br \/>\n    } <\/p>\n<p><span style=\"color:#0d0016\">\u5728Connection\u7c7b\u4e2d\u6dfb\u52a0&#xff1a;\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u4fe1\u606f<\/span><\/p>\n<p>     InetAddr _addr; \/\/\u5ba2\u6237\u7aef\u7684\u5957\u63a5\u5b57\u4fe1\u606f<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u8bbe\u7f6e&#xff0c;\u5e76\u6dfb\u52a0\u83b7\u53d6\u63a5\u53e3&#xff1a;<\/span><\/p>\n<p>     void SetAddr(const InetAddr &amp;addr)<br \/>\n    {<br \/>\n        _addr &#061; addr;<br \/>\n    } <\/p>\n<h4 id=\"%E6%9B%B4%E6%96%B0AddConnection\"><span style=\"color:#0d0016\">\u66f4\u65b0AddConnection<\/span><\/h4>\n<p>    void AddConnection(int fd, uint32_t events, const InetAddr &amp;addr, int type) \/\/ \u7a0d\u540e\u518d\u8c03\u6574<br \/>\n    {<br \/>\n        \/\/ 1.\u6784\u5efa\u4e00\u4e2aconnection<br \/>\n        Connection *conn &#061; new Connection(fd);<br \/>\n        conn-&gt;SetEvents(events);<br \/>\n        conn-&gt;SetConnectionType(type);<br \/>\n        conn-&gt;SetAddr(addr);<\/p>\n<p>        \/\/ \u5c06\u5f53\u524d\u5bf9\u8c61&#xff0c;\u8bbe\u7f6e\u8fdb\u5165\u6240\u6709\u7684conn\u5bf9\u8c61\u4e2d<br \/>\n        conn-&gt;SetReactor(this);<\/p>\n<p>        \/\/ TODO, \u8bbe\u7f6e\u5bf9connection\u7684\u4e0a\u5c42\u5904\u7406&#xff0c; \u5373&#xff0c; \u5982\u679c\u8be5connection\u5c31\u7eea\u88ab\u6fc0\u6d3b&#xff0c;\u8be5\u5982\u4f55\u5904\u7406&#xff1f;<br \/>\n        if (conn-&gt;Type() &#061;&#061; ListenConnection)<br \/>\n        {<br \/>\n            conn-&gt;RegisterHandler(_OnConnect, nullptr, nullptr);<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            conn-&gt;RegisterHandler(_OnRecver, _OnSender, _OnExcepter);<br \/>\n        }<\/p>\n<p>        \/\/ 2.fd\u548cevents\u5199\u900f\u5230OS\u4e2d&#xff0c;\u6258\u7ba1\u7ed9epoll<br \/>\n        if (!_epoller-&gt;AddEvent(conn-&gt;Sockfd(), conn-&gt;Events()))<br \/>\n            return;<\/p>\n<p>        \/\/ 3.\u6258\u7ba1\u7ed9_connection<br \/>\n        _connections.insert(std::make_pair(fd, conn));<br \/>\n    } <\/p>\n<p><span style=\"color:#0d0016\">\u66f4\u65b0InetAddr\u7c7b\u7684\u6784\u9020\u51fd\u6570&#xff1a;\u6dfb\u52a0\u4e00\u4e2a\u521d\u59cb\u5316ip\u548cport\u7684\u91cd\u8f7d<\/span><\/p>\n<p>    InetAddr(const std::string &amp;ip, uint16_t port)<br \/>\n    {<br \/>\n        _ip &#061; ip;<br \/>\n        _port &#061; port;<br \/>\n        _addr.sin_family &#061; AF_INET;<br \/>\n        _addr.sin_port &#061; htons(_port);<br \/>\n        _addr.sin_addr.s_addr &#061; inet_addr(ip.c_str());<br \/>\n    } <\/p>\n<h4 id=\"%E6%9B%B4%E6%96%B0%E5%85%A5%E5%8F%A3%E5%87%BD%E6%95%B0%EF%BC%9AMain.cc\"><span style=\"color:#0d0016\">\u66f4\u65b0\u5165\u53e3\u51fd\u6570&#xff1a;Main.cc<\/span><\/h4>\n<p> #include &lt;memory&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\n#include &#034;Listener.hpp&#034;<br \/>\n#include &#034;HandlerConnection.hpp&#034;<br \/>\n#include &#034;Log.hpp&#034;<br \/>\n#include &#034;Reactor.hpp&#034;<\/p>\n<p>using namespace log_ns;<\/p>\n<p>\/\/ .\/tcpserver 8888<br \/>\nint main(int argc, char *argv[])<br \/>\n{<br \/>\n    if (argc !&#061; 2)<br \/>\n    {<br \/>\n        std::cerr &lt;&lt; &#034;Usage: &#034; &lt;&lt; argv[0] &lt;&lt; &#034; local-port&#034; &lt;&lt; std::endl;<br \/>\n        exit(0);<br \/>\n    }<br \/>\n    uint16_t port &#061; std::stoi(argv[1]);<br \/>\n    EnableScreen();<\/p>\n<p>    InetAddr localaddr(&#034;0.0.0.0&#034;, port);<\/p>\n<p>    \/\/ \u4e13\u95e8\u7528\u4e8e\u5904\u7406\u65b0\u8fde\u63a5\u7684\u6a21\u5757<br \/>\n    Listener listener(port); \/\/ \u8fde\u63a5\u7ba1\u7406\u5668<\/p>\n<p>    \/\/ \u4e13\u95e8\u5904\u7406\u666e\u901asockfd\u7684\u6a21\u5757<br \/>\n    HandlerConnection handlers; \/\/ IO\u5904\u7406\u5668<\/p>\n<p>    \/\/ \u4e3b\u6a21\u5757&#xff0c;\u4e8b\u4ef6\u6d3e\u53d1<br \/>\n    std::unique_ptr&lt;Reactor&gt; R &#061; std::make_unique&lt;Reactor&gt;(); \/\/ \u4e8b\u4ef6\u6d3e\u53d1\u5668<\/p>\n<p>    \/\/ \u6a21\u5757\u4e4b\u95f4&#xff0c;\u4ea7\u751f\u5173\u8054<br \/>\n    R-&gt;SetOnConnect(std::bind(&amp;Listener::Accepter, &amp;listener, std::placeholders::_1));<br \/>\n    R-&gt;SetOnNormalHandler(<br \/>\n        std::bind(&amp;HandlerConnection::HandlerRecver, &amp;handlers, std::placeholders::_1),<br \/>\n        std::bind(&amp;HandlerConnection::HandlerSender, &amp;handlers, std::placeholders::_1),<br \/>\n        std::bind(&amp;HandlerConnection::HandlerExcepter, &amp;handlers, std::placeholders::_1));<\/p>\n<p>    R-&gt;AddConnection(listener.ListenSockfd(), EPOLLIN | EPOLLET, localaddr, ListenConnection);<\/p>\n<p>    \/\/ \u505a\u4e8b\u4ef6\u6d3e\u53d1<br \/>\n    R-&gt;Dispatcher();<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u66f4\u65b0Listener\u7c7b\u5f53\u4e2d\u7684Accepter:<\/span><\/p>\n<p> conn-&gt;_R-&gt;AddConnection(sockfd, EPOLLIN | EPOLLET, addr, NormalConnection); \/\/ \u9700\u8981\u6dfb\u52a0\u5bf9\u666e\u901a\u5957\u63a5\u5b57\u8fdb\u884cIO\u4e8b\u4ef6\u5904\u7406\u7684\u6a21\u5757<\/p>\n<p><span style=\"color:#0d0016\">\u6dfb\u52a0\u65e5\u5fd7\u4fe1\u606f&#xff0c;\u4fbf\u4e8e\u8c03\u8bd5\u4ee3\u7801&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5728Reactor\u5f53\u4e2d\u6dfb\u52a0\u00a0PrintDebug()\u7528\u4e8e\u6253\u5370epoll\u6240\u7ba1\u7406\u7684fd\u5217\u8868<\/span><\/p>\n<p>     void PrintDebug()<br \/>\n    {<br \/>\n        std::string fdlist;<br \/>\n        for (auto &amp;conn : _connections)<br \/>\n        {<br \/>\n            fdlist &#043;&#061; std::to_string(conn.second-&gt;Sockfd()) &#043; &#034; &#034;;<br \/>\n        }<\/p>\n<p>        LOG(DEBUG, &#034;epoll\u7ba1\u7406\u7684fd\u5217\u8868: %s\\\\n&#034;, fdlist.c_str());<br \/>\n    } <\/p>\n<p><span style=\"color:#0d0016\">\u5e76\u5728\u6bcf\u4e00\u6b21Loop\u540e\u6253\u5370&#xff1a;<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"310\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073148-684e76e40d177.png\" width=\"499\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u83b7\u53d6\u5ba2\u6237\u7aef\u8fde\u63a5\u4fe1\u606f&#xff0c;\u6d4b\u8bd5\u8c03\u7528HandlerRecver\u6210\u529f<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"186\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073148-684e76e4350b9.png\" width=\"1052\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"781\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073148-684e76e451acf.png\" width=\"1729\" \/><\/p>\n<h4 id=\"%E7%BB%A7%E7%BB%AD%E5%86%99HandlerConnection%E7%B1%BB%EF%BC%9A%E8%AF%BB%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86\"><span style=\"color:#0d0016\">\u7ee7\u7eed\u5199HandlerConnection\u7c7b&#xff1a;\u8bfb\u4e8b\u4ef6\u5904\u7406<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u9996\u5148\u5148\u5904\u7406\u63a5\u6536\u7f13\u51b2\u533a&#xff0c;\u4e5f\u5c31\u662f\u8bfb\u4e8b\u4ef6&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u7531\u4e8e\u4e0d\u80fd\u4fdd\u8bc1\u4e00\u6b21\u6027\u80fd\u5c06\u5ba2\u6237\u7aef\u53d1\u7684\u6240\u6709\u5185\u5bb9\u8bfb\u53d6\u5b8c\u6bd5&#xff0c;\u56e0\u6b64\u6211\u4eec\u5c06\u6bcf\u4e00\u6b21\u8bfb\u53d6\u4e0a\u6765\u7684\u6570\u636e\u5757&#xff0c;\u90fd\u62fc\u63a5\u5230\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u6240\u5bf9\u5e94\u7684\u63a5\u6536\u7f13\u51b2\u533ainbuffer\u5f53\u4e2d&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5728Connection\u7c7b\u4e2d\u6dfb\u52a0\u6570\u636e\u8ffd\u52a0\u63a5\u53e3&#xff1a;<\/span><\/p>\n<p>     void AppendInbuffer(const std::string &amp;in)<br \/>\n    {<br \/>\n        _inbuffer &#043;&#061; in;<br \/>\n    }<br \/>\n    std::string &amp;Inbuffer()<br \/>\n    {<br \/>\n        return _inbuffer;<br \/>\n    } <\/p>\n<h3 id=\"4.%E5%A4%84%E7%90%86%E8%AF%BB%E4%BA%8B%E4%BB%B6%EF%BC%8C%E5%BC%95%E5%85%A5%E5%8D%8F%E8%AE%AE%E8%BF%9B%E8%A1%8C%E6%8A%A5%E6%96%87%E8%A7%A3%E6%9E%90\"><span style=\"color:#0d0016\">4.\u5904\u7406\u8bfb\u4e8b\u4ef6&#xff0c;\u5f15\u5165\u534f\u8bae\u8fdb\u884c\u62a5\u6587\u89e3\u6790<\/span><\/h3>\n<p><span style=\"color:#0d0016\">HandlerRecver\u51fd\u6570<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u6ce8\u610f&#xff1a;\u8fd9\u91cc\u4fdd\u8bc1\u4e86&#xff0c;\u672c\u8f6e\u7684\u6570\u636e\u90fd\u8bfb\u53d6\u5b8c\u6bd5\u3002\u6240\u6709\u7684\u51fa\u9519\u4ee5\u53ca\u5f02\u5e38\u60c5\u51b5&#xff0c;\u5728\u540e\u9762\u7edf\u4e00\u5904\u7406&#xff0c;\u63a5\u4e0b\u6765\u6211\u4eec\u5148\u5bf9\u8bfb\u53d6\u5230\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406<\/span><\/p>\n<p>     void HandlerRecver(Connection *conn)<br \/>\n    {<br \/>\n        LOG(DEBUG, &#034;client \u7ed9\u6211\u53d1\u4e86\u6d88\u606f: %d\\\\n&#034;, conn-&gt;Sockfd());<br \/>\n        while (true)<br \/>\n        {<br \/>\n            char buffer[buffersize];<br \/>\n            int n &#061; ::recv(conn-&gt;Sockfd(), buffer, sizeof(buffer) &#8211; 1, 0);<br \/>\n            if (n &gt; 0) \/\/ \u8bf4\u660e\u83b7\u53d6\u6210\u529f<br \/>\n            {<br \/>\n                buffer[n] &#061; 0; \/\/ \u6570\u636e\u5757<br \/>\n                \/\/ \u5c06\u6bcf\u4e00\u6b21\u4ece\u540c\u4e00\u4e2afd\u4e2d\u8bfb\u53d6\u5230\u7684buffer\u62fc\u63a5\u8d77\u6765&#xff0c;\u4e34\u65f6\u653e\u5728inbuffer\u91cc<br \/>\n                conn-&gt;AppendInbuffer(buffer);<br \/>\n            }<br \/>\n            else \/\/ \u51fa\u9519<br \/>\n            {<br \/>\n                \/\/ \u672c\u8f6e\u6570\u636e\u8bfb\u53d6\u5b8c\u6bd5<br \/>\n                if (errno &#061;&#061; EWOULDBLOCK)<br \/>\n                {<br \/>\n                    break;<br \/>\n                }<br \/>\n                else if (errno &#061;&#061; EINTR)<br \/>\n                {<br \/>\n                    continue;<br \/>\n                }<br \/>\n                else<br \/>\n                {<br \/>\n                    \/\/ \u6700\u540e\u7edf\u4e00\u8fdb\u884c\u5f02\u5e38\u5904\u7406<br \/>\n                    conn-&gt;_handler_excepter(conn);<br \/>\n                    return;<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        \/\/ \u4e00\u5b9a\u8bfb\u53d6\u5b8c\u6bd5&#xff0c;\u5904\u7406\u6570\u636e<br \/>\n        std::cout &lt;&lt; &#034;Inbuffer content: &#034; &lt;&lt; conn-&gt;Inbuffer() &lt;&lt; std::endl;<br \/>\n    } <\/p>\n<p><span style=\"color:#0d0016\">\u9a8c\u8bc1\u8ffd\u52a0\u6210\u529f&#xff1a;<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"423\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073148-684e76e4dcc67.png\" width=\"624\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u5904\u7406\u6570\u636e: \u8ba9\u4e0a\u5c42\u5904\u7406\u62a5\u6587<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u73b0\u9636\u6bb5\u6211\u4eec\u7684\u7a0b\u5e8f\u67b6\u6784&#xff1a;<\/span><\/p>\n<h3 id=\"5.%E5%8D%8F%E8%AE%AE%E5%A4%84%E7%90%86%2B%E6%8A%A5%E6%96%87%E8%A7%A3%E6%9E%90%E6%A8%A1%E5%9D%97PackageParse.hpp%2BProtocol.hpp\"><span style=\"color:#0d0016\">5.\u534f\u8bae\u5904\u7406&#043;\u62a5\u6587\u89e3\u6790\u6a21\u5757PackageParse.hpp&#043;Protocol.hpp<\/span><\/h3>\n<p>                      Protocol&#xff08;\u534f\u8bae\u5904\u7406&#xff09;PackageParse&#xff08;\u62a5\u6587\u89e3\u6790&#xff09;<br \/>\nListener(\u5904\u7406\u65b0\u8fde\u63a5) HandlerConnection&#xff08;\u5904\u7406\u666e\u901a\u5957\u63a5\u5b57IO\u4e8b\u4ef6\u4ee5\u53ca\u5f02\u5e38&#xff09;<br \/>\nConnection&#xff08;\u5c01\u88c5\u5957\u63a5\u5b57\u4ee5\u53ca\u7f13\u51b2\u533a&#xff09;<br \/>\nReactor(\u6700\u5e95\u5c42&#xff09; <\/p>\n<p><span style=\"color:#0d0016\">\u4e3a\u4e86\u4fdd\u8bc1\u8bfb\u53d6\u4e0a\u6765\u7684\u62a5\u6587\u662f\u5b8c\u6574\u7684\u6211\u4eec\u9700\u8981\u81ea\u5b9a\u4e49\u534f\u8bae<\/span>&#xff0c;<span style=\"color:#0d0016\">\u8fd9\u7bc7\u6587\u7ae0\u8be6\u7ec6\u8bb2\u89e3\u4e86HTTP\u670d\u52a1\u5668\u5b9e\u73b0&#xff0c;\u5982\u4f55\u5904\u7406\u62a5\u6587&#xff08;\u5e8f\u5217\u5316\u3001\u53cd\u5e8f\u5217\u5316&#xff09;\u5728\u5b9e\u73b0\u4e00\u4e2aHTTP\u670d\u52a1\u5668\u65f6&#xff0c;\u786e\u4fdd\u8bfb\u53d6\u7684\u62a5\u6587\u5b8c\u6574\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u7531\u4e8eHTTP\u534f\u8bae\u672c\u8eab\u662f\u57fa\u4e8eTCP\u7684&#xff0c;\u800cTCP\u662f\u4e00\u4e2a\u6d41\u5f0f\u534f\u8bae&#xff0c;\u5b83\u5e76\u4e0d\u4fdd\u8bc1\u6570\u636e\u5305\u7684\u8fb9\u754c&#xff0c;\u56e0\u6b64\u6211\u4eec\u9700\u8981\u81ea\u5b9a\u4e49\u534f\u8bae\u6765\u5904\u7406\u62a5\u6587\u7684\u5b8c\u6574\u6027\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u672a\u6765\u5c06\u6536\u5230\u7684inbuffer\u5168\u90e8\u4ea4\u7ed9\u4e0a\u5c42\u534f\u8bae\u53bb\u5904\u7406&#xff1a;<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"208\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073149-684e76e548531.png\" width=\"499\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u8fd9\u91cc\u9700\u8981\u7528\u5230\u4e4b\u524d\u5199\u7f51\u7edc\u8ba1\u7b97\u5668\u65f6\u7528\u5230\u7684\u4ee3\u7801&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u534f\u8bae\u5904\u7406<\/span>&#xff1a;Protocol.hpp\u3001IOService.hpp<\/p>\n<p><span style=\"color:#0d0016\">\u66f4\u65b0Service.hpp\u7684\u540d\u5b57\u4e3a&#xff1a;PackageParse.hpp&#xff08;\u62a5\u6587\u89e3\u6790\u6a21\u5757&#xff09;&#xff0c;\u5e76\u4e14\u5bf9\u8be5\u4ee3\u7801\u9700\u8981\u505a\u4e00\u4e9b\u8c03\u6574&#xff0c;\u4e13\u95e8\u5904\u7406conn\u6570\u636e&#xff0c;\u6ce8\u610f&#xff0c;\u6570\u636e\u5df2\u7ecf\u8bfb\u53d6\u5230\u6211\u4eec\u7684\u670d\u52a1\u5668\u4e86&#xff0c;\u73b0\u5728\u4ec5\u9700\u5c06\u6570\u636e\u4f20\u5165&#xff0c;\u8fdb\u884c\u89e3\u6790\u5c31\u53ef\u4ee5.<\/span><\/p>\n<h4 id=\"%E8%AF%BB%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%E9%80%BB%E8%BE%91%E5%9B%BE%EF%BC%9A\"><span style=\"color:#0d0016\">\u8bfb\u4e8b\u4ef6\u5904\u7406\u903b\u8f91\u56fe&#xff1a;<\/span><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"351\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073149-684e76e584170.png\" width=\"653\" \/><\/p>\n<p><span style=\"color:#0d0016\">Execute\u51fd\u6570\u662f\u6210\u5458\u51fd\u6570&#xff0c;\u6709\u9690\u85cf\u7684this&#xff0c;handler\u51fd\u6570\u7c7b\u578b\u53ea\u6709\u4e00\u4e2a\u53c2\u6570&#xff0c;\u6240\u4ee5\u5728bind\u7684\u65f6\u5019\u8981\u663e\u793a\u7ed1\u5b9a\u3002\u5982\u679c\u4e0d\u663e\u5f0f\u7ed1\u5b9athis\u6307\u9488&#xff0c;\u7f16\u8bd1\u5668\u5c06\u65e0\u6cd5\u786e\u5b9aExecute\u51fd\u6570\u5e94\u8be5\u64cd\u4f5c\u54ea\u4e2a\u5bf9\u8c61&#xff0c;\u4ece\u800c\u5bfc\u81f4\u9519\u8bef\u3002\u56e0\u6b64&#xff0c;\u5728\u7ed1\u5b9a\u6210\u5458\u51fd\u6570\u65f6&#xff0c;\u663e\u5f0f\u4f20\u9012this\u6307\u9488\u662f\u5fc5\u8981\u7684\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u8fd9\u91cc\u6ce8\u610f\u4e00\u4e0b&#xff1a;\u6709\u4e00\u4e2a\u5c0f\u9519\u8bef&#xff1a;<\/span><\/p>\n<p><span style=\"color:#ed7976\">\u00a0 \u00a0 \/\/ \u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u6307\u9488\u9700\u8981\u4f7f\u7528&amp;\u7c7b\u540d::\u6210\u5458\u51fd\u6570\u540d\u7684\u5f62\u5f0f\u3002<\/span><\/p>\n<p><span style=\"color:#ed7976\">\u00a0 \u00a0 HandlerConnection handlers(std::bind(&amp;PackageParse::Execute, &amp;parse, std::placeholders::_1)); \/\/ IO\u5904\u7406\u5668<\/span><\/p>\n<h3 id=\"6.%E6%B7%BB%E5%8A%A0%E4%B8%9A%E5%8A%A1%E5%A4%84%E7%90%86%E6%A8%A1%E5%9D%97%EF%BC%9A\"><span style=\"color:#0d0016\">6.\u6dfb\u52a0\u4e1a\u52a1\u5904\u7406\u6a21\u5757&#xff1a;<\/span><\/h3>\n<p>                         Request(\u8bf7\u6c42)  &amp;&amp;  Response&#xff08;\u5e94\u7b54&#xff09;<br \/>\n                    Protocol&#xff08;\u534f\u8bae\u5904\u7406&#xff09;PackageParse&#xff08;\u62a5\u6587\u89e3\u6790&#xff09;<br \/>\nListener(\u5904\u7406\u65b0\u8fde\u63a5) HandlerConnection&#xff08;\u5904\u7406\u666e\u901a\u5957\u63a5\u5b57IO\u4e8b\u4ef6\u4ee5\u53ca\u5f02\u5e38&#xff09;<br \/>\nConnection&#xff08;\u5c01\u88c5\u5957\u63a5\u5b57\u4ee5\u53ca\u7f13\u51b2\u533a&#xff09;<br \/>\nReactor(\u6700\u5e95\u5c42&#xff09; <\/p>\n<p><span style=\"color:#0d0016\">\u8fd9\u91cc\u9700\u8981\u6dfb\u52a0\u4e1a\u52a1\u5904\u7406\u6a21\u5757\u7684\u4ee3\u7801<\/span>&#xff1a;NetCal.hpp&#xff0c;<span style=\"color:#0d0016\">\u8fd9\u662f\u4e4b\u524d\u6240\u8bb2\u89e3\u8fc7\u7684\u7f51\u7edc\u8ba1\u7b97\u673a\u7684\u7f16\u5199<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u8fd9\u5c31\u662f\u6211\u4eec\u8981\u5904\u7406\u7684\u4e1a\u52a1&#xff1a;<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"360\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073150-684e76e6241d4.png\" width=\"481\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u6ce8\u610f&#xff1a;Main.cc\u66f4\u65b0<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"356\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073150-684e76e68c3d3.png\" width=\"1775\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u7ee7\u7eed\u7f16\u5199PackageParse&#xff0c;\u5c06\u89e3\u6790\u597d\u7684\u62a5\u6587\u53d1\u56de\u5ba2\u6237\u7aef<\/span><\/p>\n<h3 id=\"7.%E5%85%B3%E4%BA%8E%E5%86%99%E5%85%A5%E7%9A%84%E8%AF%9D%E9%A2%98%EF%BC%9A\"><span style=\"color:#0d0016\">7.\u5173\u4e8e\u5199\u5165\u7684\u8bdd\u9898&#xff1a;<\/span><\/h3>\n<p><span style=\"color:#0d0016\">1.\u591a\u8fdb\u7a0b\u591a\u7ebf\u7a0b\u4e2d&#xff0c;write\u66f4\u7b80\u5355<\/span><\/p>\n<p><span style=\"color:#0d0016\">2.\u591a\u8def\u8f6c\u63a5\u5982\u4f55\u6b63\u786e\u7684write&#xff1f;<\/span><\/p>\n<p><span style=\"color:#0d0016\">a.\u5f53\u6211\u4eec\u83b7\u53d6\u4e00\u4e2a\u5168\u65b0\u7684sockfd\u7684\u65f6\u5019&#xff0c;\u8f93\u5165\u548c\u8f93\u51fa\u7f13\u51b2\u533a\u9ed8\u8ba4\u90fd\u662f\u7a7a\u7684\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">b.\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u672c\u8d28&#xff1a;\u8f93\u5165\u7f13\u51b2\u533a\u4e2d\u6709\u4e86\u6570\u636e&#xff0c;\u6216\u8005\u5e95\u5c42\u6709\u65b0\u8fde\u63a5<\/span><\/p>\n<p><span style=\"color:#0d0016\">c.\u5199\u4e8b\u4ef6\u5c31\u7eea&#xff1a;\u4e0d\u662f\u5173\u5fc3\u53d1\u9001\u7f13\u51b2\u533a\u662f\u5426\u6709\u6570\u636e&#xff0c;\u800c\u662f\u5173\u5fc3\u53d1\u9001\u7f13\u51b2\u533a\u4e2d\u662f\u5426\u8fd8\u6709\u7a7a\u95f4&#xff0c;\u6709\u7a7a\u95f4&#xff0c;\u53d1\u9001\u6761\u4ef6\u6ee1\u8db3&#xff0c;\u5199\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u5426\u5219&#xff0c;\u4e0d\u5c31\u7eea<\/span><\/p>\n<p><span style=\"color:#0d0016\">d.\u628a\u4e00\u4e2asockfd\u6258\u7ba1\u7ed9select\u3001poll\u3001epoll&#xff0c;\u539f\u56e0\u662f<\/span><span style=\"color:#1a439c\">sockfd\u4e0a\u7684\u4e8b\u4ef6\u6ca1\u6709\u5c31\u7eea<\/span><span style=\"color:#0d0016\">&#xff0c;\u8fd8\u662f\u4e8b\u4ef6\u5c31\u7eea\u4e86&#xff1f;\u6ca1\u6709\u5c31\u7eea<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u7ed3\u8bba&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;\u8bfb\u4e8b\u4ef6\u5904\u4e8e\u672a\u5c31\u7eea\u72b6\u6001&#xff0c;\u56e0\u4e3a\u8f93\u5165\u7f13\u51b2\u533a\u6682\u65f6\u6ca1\u6709\u6570\u636e\u3002\u56e0\u6b64&#xff0c;\u6211\u4eec\u9700\u8981\u5c06\u8bfb\u4e8b\u4ef6\u9ed8\u8ba4\u6dfb\u52a0\u5230epoll\u4e2d\u8fdb\u884c\u76d1\u63a7&#xff08;EPOLLIN&#xff09;\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5bf9\u4e8e\u5199\u4e8b\u4ef6&#xff0c;\u9ed8\u8ba4\u662f\u5c31\u7eea\u7684&#xff0c;\u56e0\u4e3a\u53d1\u9001\u7f13\u51b2\u533a\u6709\u53ef\u7528\u7a7a\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u5199\u64cd\u4f5c&#xff0c;\u53ea\u6709\u5f53\u5199\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6&#xff0c;\u624d\u9700\u8981\u5f00\u542f\u5bf9sockfd\u7684EPOLLOUT\u4e8b\u4ef6\u76d1\u63a7\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5f53\u6301\u7eed\u5199\u5165\u5bfc\u81f4\u53d1\u9001\u7f13\u51b2\u533a\u5df2\u6ee1\u4e14\u4ecd\u6709\u6570\u636e\u672a\u53d1\u9001\u65f6&#xff0c;\u5c31\u9700\u8981\u5f00\u542fEPOLLOUT\u4e8b\u4ef6\u76d1\u63a7\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u540e\u7eed\u5904\u7406\u5199\u4e8b\u4ef6\u65f6&#xff0c;\u6211\u4eec\u4f1a\u5148\u5c1d\u8bd5\u76f4\u63a5\u53d1\u9001\u6570\u636e\u3002\u5982\u679c\u53d1\u9001\u6761\u4ef6\u4e0d\u6ee1\u8db3&#xff0c;\u5219\u5f00\u542f\u5199\u4e8b\u4ef6\u76d1\u63a7&#xff0c;epoll\u4f1a\u81ea\u52a8\u5904\u7406\u5269\u4f59\u6570\u636e\u7684\u53d1\u9001\u3002<\/span><\/li>\n<\/ul>\n<h4 id=\"%E5%86%99%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86\"><span style=\"color:#0d0016\">\u5199\u4e8b\u4ef6\u5904\u7406<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u4e0e\u8bfb\u4e8b\u4ef6\u4e0d\u540c&#xff0c;\u5199\u4e8b\u4ef6\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u5c31\u7eea\u7684&#xff0c;\u56e0\u4e3asocket\u7684\u53d1\u9001\u7f13\u51b2\u533a\u901a\u5e38\u6709\u53ef\u7528\u7a7a\u95f4\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528write()\u6216send()\u7b49\u51fd\u6570\u8fdb\u884c\u6570\u636e\u53d1\u9001&#xff0c;\u800c\u4e0d\u9700\u8981\u7acb\u5373\u5c06\u5199\u4e8b\u4ef6\u6dfb\u52a0\u5230epoll\u4e2d\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u7136\u800c&#xff0c;\u5728\u7f51\u7edc\u4f20\u8f93\u8fc7\u7a0b\u4e2d&#xff0c;\u53ef\u80fd\u4f1a\u9047\u5230\u53d1\u9001\u7f13\u51b2\u533a\u5df2\u6ee1\u7684\u60c5\u51b5\u3002\u8fd9\u901a\u5e38\u53d1\u751f\u5728\u4ee5\u4e0b\u573a\u666f\u4e2d&#xff1a;<\/span><\/p>\n<li><span style=\"color:#0d0016\">\u7f51\u7edc\u62e5\u585e\u5bfc\u81f4\u6570\u636e\u53d1\u9001\u901f\u5ea6\u53d8\u6162<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5e94\u7528\u7a0b\u5e8f\u53d1\u9001\u6570\u636e\u7684\u901f\u5ea6\u8d85\u8fc7\u4e86\u7f51\u7edc\u4f20\u8f93\u80fd\u529b<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5bf9\u7aef\u63a5\u6536\u7f13\u51b2\u533a\u5df2\u6ee1&#xff0c;\u5bfc\u81f4\u672c\u7aef\u65e0\u6cd5\u7ee7\u7eed\u53d1\u9001\u6570\u636e<\/span><\/li>\n<p><span style=\"color:#0d0016\">\u5f53\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u65f6&#xff0c;write()\u6216send()\u51fd\u6570\u4f1a\u8fd4\u56deEAGAIN\u6216EWOULDBLOCK\u9519\u8bef&#xff0c;\u8868\u793a\u5f53\u524d\u65e0\u6cd5\u53d1\u9001\u66f4\u591a\u6570\u636e\u3002\u6b64\u65f6&#xff0c;\u6211\u4eec\u9700\u8981\u5c06\u5199\u4e8b\u4ef6&#xff08;EPOLLOUT&#xff09;\u6dfb\u52a0\u5230epoll\u4e2d\u8fdb\u884c\u76d1\u63a7\u3002epoll\u4f1a\u5728\u53d1\u9001\u7f13\u51b2\u533a\u6709\u53ef\u7528\u7a7a\u95f4\u65f6\u901a\u77e5\u5e94\u7528\u7a0b\u5e8f&#xff0c;\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u7ee7\u7eed\u53d1\u9001\u5269\u4f59\u7684\u6570\u636e\u3002<\/span><\/p>\n<h4 id=\"%E5%86%99%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B\"><span style=\"color:#0d0016\">\u5199\u4e8b\u4ef6\u5904\u7406\u6d41\u7a0b<\/span><\/h4>\n<li><span style=\"color:#0d0016\">\u5c1d\u8bd5\u76f4\u63a5\u53d1\u9001\u6570\u636e<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5982\u679c\u53d1\u9001\u6210\u529f&#xff0c;\u7ee7\u7eed\u5904\u7406\u540e\u7eed\u6570\u636e<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5982\u679c\u53d1\u9001\u5931\u8d25\u4e14errno\u4e3aEAGAIN\/EWOULDBLOCK&#xff1a;<\/span><span style=\"color:#9c8ec1\"><span style=\"background-color:#fef2f0\"> a. \u5c06EPOLLOUT\u4e8b\u4ef6\u6dfb\u52a0\u5230epoll\u76d1\u63a7\u4e2d b. \u4fdd\u5b58\u672a\u53d1\u9001\u7684\u6570\u636e<\/span><\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5f53epoll\u901a\u77e5EPOLLOUT\u4e8b\u4ef6\u5c31\u7eea\u65f6&#xff1a;<\/span><span style=\"color:#9c8ec1\"><span style=\"background-color:#fef2f0\"> a. \u5c1d\u8bd5\u53d1\u9001\u4e4b\u524d\u672a\u53d1\u9001\u7684\u6570\u636e b. \u5982\u679c\u53d1\u9001\u6210\u529f&#xff0c;\u79fb\u9664EPOLLOUT\u4e8b\u4ef6\u76d1\u63a7 c. \u5982\u679c\u518d\u6b21\u5931\u8d25&#xff0c;\u7ee7\u7eed\u7b49\u5f85\u4e0b\u4e00\u6b21\u901a\u77e5<\/span><\/span><\/li>\n<p><span style=\"color:#0d0016\">\u5c06\u89e3\u6790\u597d\u7684\u62a5\u6587\u8ffd\u52a0\u5230\u53d1\u9001\u7f13\u51b2\u533a\u5185&#xff1a;\u5728Connection\u7c7b\u4e2d\u6dfb\u52a0<\/span><\/p>\n<p>     void AppendOutbuffer(const std::string &amp;in)<br \/>\n    {<br \/>\n        _outbuffer &#043;&#061; in;<br \/>\n    }<br \/>\n    std::string &amp;Outbuffer()<br \/>\n    {<br \/>\n        return _outbuffer;<br \/>\n    }<\/p>\n<p><span style=\"color:#0d0016\">\u5728\u89e3\u6790\u62a5\u6587&#xff1a;<\/span><\/p>\n<p>             \/\/ 7.\u53d1\u56de<br \/>\n            conn-&gt;AppendOutbuffer(respjson);\/\/\u5c06\u7ed3\u679c\u8ffd\u52a0\u5230\u53d1\u9001\u7f13\u51b2\u533a\u5185 <\/p>\n<p><span style=\"color:#0d0016\">\u5e76\u4e14&#xff1a;\u6ca1\u6709\u62a5\u6587&#xff0c;\u76f4\u63a5\u7ed3\u675fExecute<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"251\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073150-684e76e6cef19.png\" width=\"450\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u51fa\u5faa\u73af\u540e&#xff1a;<\/span><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"313\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073151-684e76e70d3ad.png\" width=\"1147\" \/><\/p>\n<\/p>\n<h3 id=\"PackageParse.hpp\"><span style=\"color:#0d0016\">PackageParse.hpp<\/span><\/h3>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &#034;InetAddr.hpp&#034;<br \/>\n#include &#034;Log.hpp&#034;<br \/>\n#include &#034;Protocol.hpp&#034;<br \/>\n#include &#034;Connection.hpp&#034;<br \/>\n#include &#034;NetCal.hpp&#034;<\/p>\n<p>using namespace log_ns;<\/p>\n<p>class PackageParse<br \/>\n{<br \/>\npublic:<br \/>\n    void Execute(Connection *conn)<br \/>\n    {<br \/>\n        \/\/ \u957f\u670d\u52a1<br \/>\n        while (true)<br \/>\n        {<\/p>\n<p>            \/\/  2.\u62a5\u6587\u89e3\u6790&#xff0c;\u63d0\u53d6\u62a5\u6587\u548c\u6709\u6548\u8f7d\u8377&#xff0c;\u62a5\u6587\u5c31\u5728inbuffer\u91cc<br \/>\n            std::string package &#061; Decode(conn-&gt;Inbuffer());<br \/>\n            \/\/ \u7a7a\u5b57\u7b26\u4e32\u4ee3\u8868\u62a5\u6587\u4e0d\u5b8c\u6574&#xff0c;\u76f4\u63a5break<br \/>\n            if (package.empty())<br \/>\n                return; \/\/ \u5982\u679c\u6ca1\u6709\u62a5\u6587&#xff0c;\u76f4\u63a5\u7ed3\u675f<br \/>\n            \/\/ \u53cd\u5e8f\u5217\u5316\u505a\u5904\u7406<br \/>\n            auto req &#061; Factory::BuildRequestDefault();<\/p>\n<p>            std::cout &lt;&lt; &#034;package: \\\\n&#034;<br \/>\n                      &lt;&lt; package &lt;&lt; std::endl;<\/p>\n<p>            \/\/ 3.\u505a\u53cd\u5e8f\u5217\u5316<br \/>\n            req-&gt;Deserialize(package);<\/p>\n<p>            \/\/ 4.\u4e1a\u52a1\u5904\u7406<br \/>\n            auto resp &#061; cal.Calculator(req);<br \/>\n            \/\/ 5.\u6784\u5efa\u5e8f\u5217\u5316\u5e94\u7b54<br \/>\n            std::string respjson;<br \/>\n            \/\/ \u5148\u5e8f\u5217\u5316<br \/>\n            \/\/resp-&gt;Serialize(&amp;respjson);<br \/>\n            std::cout &lt;&lt; &#034;respjson: \\\\n&#034;<br \/>\n                      &lt;&lt; respjson &lt;&lt; std::endl;<\/p>\n<p>            \/\/ 6.\u6dfb\u52a0len\u957f\u5ea6\u62a5\u5934<br \/>\n            respjson &#061; Encode(respjson);<br \/>\n            std::cout &lt;&lt; &#034;respjson add header done: \\\\n&#034;<br \/>\n                      &lt;&lt; respjson &lt;&lt; std::endl;<\/p>\n<p>            \/\/ 7.\u53d1\u56de<br \/>\n            conn-&gt;AppendOutbuffer(respjson); \/\/ \u5c06\u7ed3\u679c\u8ffd\u52a0\u5230\u53d1\u9001\u7f13\u51b2\u533a\u5185<br \/>\n        }<\/p>\n<p>        \/\/ \u6211\u4eec\u5df2\u7ecf\u81f3\u5c11\u5904\u7406\u4e86\u4e00\u4e2a\u8bf7\u6c42&#xff0c;\u540c\u65f6\u81f3\u5c11\u4f1a\u6709\u4e00\u4e2a\u5e94\u7b54<br \/>\n        conn-&gt;_handler_sender(conn); \/\/ \u65b9\u6cd51&#xff1a;\u76f4\u63a5\u53d1\u9001\u6570\u636e<br \/>\n    }<\/p>\n<p>private:<br \/>\n    NetCal cal;<br \/>\n}; <\/p>\n<h4 id=\"%E6%95%B0%E6%8D%AE%E5%8F%91%E9%80%81HandlerSender%E5%87%BD%E6%95%B0\"><span style=\"color:#0d0016\">\u6570\u636e\u53d1\u9001HandlerSender\u51fd\u6570<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u8bf7\u6ce8\u610f&#xff1a;\u5f53outbuffer\u4e2d\u65e0\u6570\u636e&#xff0c;\u5373\u6240\u6709\u6570\u636e\u88ab\u53d1\u9001\u5b8c&#xff0c;\u6211\u4eec\u8fd9\u91cc\u7684\u5904\u7406\u65b9\u5f0f\u662f\u76f4\u63a5\u9000\u51fa\u3002\u4e4b\u540e\u8fd8\u8981\u4fee\u6539\u00a0<\/span><\/p>\n<p>    void HandlerSender(Connection *conn)<br \/>\n    {<br \/>\n        \/\/ 1.\u76f4\u63a5\u5199<br \/>\n        while (true)<br \/>\n        {<br \/>\n            ssize_t n &#061; ::send(conn-&gt;Sockfd(), conn-&gt;Outbuffer().c_str(), conn-&gt;Outbuffer().size(), 0);<br \/>\n            if (n &gt; 0)<br \/>\n            {<br \/>\n                \/\/ \u5c06\u6709\u6548\u5185\u5bb9\u53d1\u9001\u6210\u529f&#xff0c;\u6e05\u7406\u53d1\u9001\u7f13\u51b2\u533a\u5df2\u53d1\u9001\u7684\u6570\u636e\u5927\u5c0f<br \/>\n                conn-&gt;DiscardOutbuffer(n);<br \/>\n                \/\/ \u6570\u636e\u53d1\u9001\u5b8c\u6bd5<br \/>\n                if (conn-&gt;Outbuffer().empty())<br \/>\n                    return; \/\/ \u6682\u65f6return<br \/>\n            }<br \/>\n            else if (n &#061;&#061; 0)<br \/>\n            {<br \/>\n                \/\/ \u5728\u6211\u4eec\u4eca\u5929\u7684\u4ee3\u7801\u4e0d\u5b58\u5728&#xff0c;outbuffer\u4e3a\u7a7a&#xff0c;\u5199\u4e8b\u4ef6\u6ca1\u5fc5\u8981\u5904\u7406<br \/>\n                return;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                \/\/ &lt;0\u4e0d\u4e00\u5b9a\u662f\u51fa\u9519\u4e86&#xff0c; \u6709\u53ef\u80fd\u662f\u53d1\u9001\u7f13\u51b2\u533a\u5199\u6ee1\u4e86<br \/>\n                if (errno &#061;&#061; EWOULDBLOCK)<br \/>\n                {<br \/>\n                    break;<br \/>\n                }<br \/>\n                else if (errno &#061;&#061; EINTR)<br \/>\n                {<br \/>\n                    continue;<br \/>\n                }<br \/>\n                else<br \/>\n                {<br \/>\n                    \/\/ \u53d1\u9001\u5931\u8d25&#xff0c;\u4e0d\u80fd\u7ee7\u7eed\u53d1\u9001&#xff0c;\u8fdb\u5165\u5f02\u5e38\u5904\u7406\u903b\u8f91<br \/>\n                    conn-&gt;_handler_excepter(conn);<br \/>\n                }<br \/>\n            }<br \/>\n            \/\/ 2.\u4ee3\u7801\u8fd0\u884c\u5230\u8fd9\u91cc&#xff0c;\u53ea\u80fd\u662f\u53d1\u9001\u6761\u4ef6\u4e0d\u6ee1\u8db3&#xff0c;outbuffer\u5df2\u7ecf\u88ab\u5199\u6ee1&#xff1b;\u4f46\u662f\u6709\u53ef\u80fd\u6570\u636e\u6ca1\u53d1\u9001\u5b8c&#xff0c;\u56e0\u6b64\u8981\u5f00\u542fEPOLLOUT<br \/>\n            if (!conn-&gt;Outbuffer().empty()) \/\/ \u7f13\u51b2\u533a\u8fd8\u6709\u6570\u636e<br \/>\n            {<br \/>\n                \/\/ \u5f00\u542f\u5173\u5fc3\u5199\u4e8b\u4ef6, \u56e0\u6b64\u9700\u8981\u518dReactor\u4e2d\u6dfb\u52a0\u4fee\u6539\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u7684\u5173\u5fc3<br \/>\n            }<br \/>\n        }<br \/>\n    } <\/p>\n<p><span style=\"color:#0d0016\">\u5982\u4f55\u5f00\u542fEPOLLOUT&#xff1a;\u5728Reactor\u4e2d\u6dfb\u52a0\u4fee\u6539\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u7684\u5173\u5fc3:<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u8fd9\u6bb5\u4ee3\u7801\u4e3a\u6307\u5b9a\u5957\u63a5\u5b57\u8bbe\u7f6eepoll\u76d1\u542c\u4e8b\u4ef6&#xff0c;\u6839\u636e\u53c2\u6570\u63a7\u5236\u8bfb\/\u5199\u4e8b\u4ef6\u7684\u76d1\u542c\u72b6\u6001&#xff0c;\u5f3a\u5236\u6dfb\u52a0\u5199\u4e8b\u4ef6&#xff08;\u65e0\u8bba\u53c2\u6570\u5982\u4f55\u90fd\u5305\u542bEPOLLET&#xff09;\u3002<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"449\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073151-684e76e734510.png\" width=\"1500\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u56e0\u6b64\u5728\u591a\u8def\u8f6c\u63a5\u65b9\u6848\u4e2d&#xff0c;\u9664\u4e86\u6709\u65b0\u589e\u4e8b\u4ef6\u63a5\u53e3<\/span>AddEvent<span style=\"color:#0d0016\">\u8fd8\u8981\u6709\u4fee\u6539\u4e8b\u4ef6\u7684\u63a5\u53e3<\/span>ModEvent<\/p>\n<h5 id=\"%E6%9B%B4%E6%96%B0Epoller.hpp\"><span style=\"color:#0d0016\">\u66f4\u65b0Epoller.hpp<\/span><\/h5>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"973\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073151-684e76e7648f0.png\" width=\"1684\" \/><\/p>\n<p> private:<br \/>\n    bool ModEventHelper(int fd, u_int32_t events, int oper)<br \/>\n    {<br \/>\n        struct epoll_event ev;<br \/>\n        ev.events &#061; events;<br \/>\n        ev.data.fd &#061; fd;<\/p>\n<p>        int n &#061; ::epoll_ctl(_epfd, oper, fd, &amp;ev);<br \/>\n        if (n &lt; 0)<br \/>\n        {<br \/>\n            LOG(ERROR, &#034;epoll_ctl %d events %s is fail&#8230;\\\\n&#034;, fd, EventsToString(events).c_str());<br \/>\n            return false;<br \/>\n        }<br \/>\n        LOG(INFO, &#034;epoll_ctl %d events %s is success&#8230;\\\\n&#034;, fd, EventsToString(events).c_str());<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>public:<br \/>\n    bool AddEvent(int fd, u_int32_t events) override<br \/>\n    {<br \/>\n        return ModEventHelper(fd, events, EPOLL_CTL_ADD);<br \/>\n    }<\/p>\n<p>    bool ModEvent(int fd, uint32_t events) override<br \/>\n    {<br \/>\n        return (fd, events, EPOLL_CTL_MOD);<br \/>\n    } <\/p>\n<h5 id=\"%E6%9B%B4%E6%96%B0Reactor.hpp\"><span style=\"color:#0d0016\">\u66f4\u65b0Reactor.hpp<\/span><\/h5>\n<p>     \/\/ \u5f00\u542f\u8fde\u63a5\u5bf9\u4e8e\u8bfb\u5199\u4e8b\u4ef6\u7684\u5173\u5fc3<br \/>\n    void EnableConnectionReadWrite(int sockfd, bool readable, bool writable)<br \/>\n    {<br \/>\n        if (!IsConnectionExist(sockfd))<br \/>\n        {<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        uint32_t events &#061; (readable ? EPOLLIN : 0) | (writable ? EPOLLOUT : 0) | EPOLLET;<br \/>\n        _connections[sockfd]-&gt;SetEvents(events);<\/p>\n<p>        \/\/ \u5c06\u5173\u5fc3\u7684\u4e8b\u4ef6\u5199\u900f\u5185\u6838\u91cc&#xff0c;\u56e0\u6b64\u4f7f\u7528epoller<br \/>\n        _epoller-&gt;ModEvent(_connections[sockfd]-&gt;Sockfd(), _connections[sockfd]-&gt;Events());<br \/>\n    } <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"775\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073152-684e76e80ad46.png\" width=\"1663\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u5728\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u6a21\u578b\u4e2d&#xff0c;\u5f53\u5f00\u542f\u5199\u4e8b\u4ef6\u5173\u5fc3\u540e&#xff0c;\u7cfb\u7edf\u4f1a\u6301\u7eed\u76d1\u63a7\u5199\u64cd\u4f5c\u7684\u72b6\u6001\u3002\u5982\u679c\u5728\u5199\u6570\u636e\u7684\u8fc7\u7a0b\u4e2d&#xff0c;\u6570\u636e\u672a\u80fd\u4e00\u6b21\u6027\u5168\u90e8\u5199\u5165&#xff0c;\u7cfb\u7edf\u4f1a\u81ea\u52a8\u4fdd\u6301\u5199\u4e8b\u4ef6\u7684\u6d3b\u8dc3\u72b6\u6001&#xff0c;\u7ee7\u7eed\u5c1d\u8bd5\u5199\u5165\u5269\u4f59\u7684\u6570\u636e&#xff0c;\u76f4\u5230\u6240\u6709\u6570\u636e\u90fd\u6210\u529f\u5199\u5165\u4e3a\u6b62\u3002\u8fd9\u4e00\u673a\u5236\u786e\u4fdd\u4e86\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u8fde\u7eed\u6027&#xff0c;\u907f\u514d\u4e86\u6570\u636e\u4e22\u5931\u6216\u4e2d\u65ad\u7684\u60c5\u51b5\u3002\u4e00\u65e6\u6240\u6709\u6570\u636e\u90fd\u6210\u529f\u5199\u5165&#xff0c;\u7cfb\u7edf\u4f1a\u5173\u95ed\u5199\u4e8b\u4ef6&#xff0c;\u8868\u793a\u5f53\u524d\u5199\u64cd\u4f5c\u5df2\u7ecf\u5b8c\u6210\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5f53\u6570\u636e\u53d1\u9001\u5b8c\u6bd5\u540e&#xff0c;\u7f13\u51b2\u533a\u4e00\u5b9a\u662f\u7a7a\u7684&#xff0c;\u56e0\u6b64\u9700\u8981\u53d6\u6d88\u5173\u5fc3\u5199\u4e8b\u4ef6&#xff1a;<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"602\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073152-684e76e886964.png\" width=\"1201\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u4ee5\u4e0a\u5c31\u5b8c\u6210\u4e86\u53d1\u9001\u7684\u5904\u7406&#xff0c;\u5373\u5199\u4e8b\u4ef6\u7684\u5904\u7406<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u73b0\u5728\u6211\u4eec\u5c31\u53ef\u4ee5\u6765\u505a\u4e00\u6b21\u5305\u62ec\u7f51\u7edc\u8ba1\u7b97\u5668\u4e1a\u52a1\u5904\u7406\u7684\u5b9e\u9a8c&#xff0c;\u9700\u8981\u7528\u5230ClientMain.cc\u5ba2\u6237\u7aef\u7684\u4ee3\u7801<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"998\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073152-684e76e8c8dbd.png\" width=\"1341\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u53ef\u4ee5\u53d1\u73b0&#xff0c;\u670d\u52a1\u5668\u54cd\u5e94\u4e0d\u6210\u529f&#xff0c;\u5206\u6790\u95ee\u9898&#xff0c;\u662f\u7531\u4e8e&#xff0c;\u9519\u8bef\u7801\u6ca1\u6709\u91cd\u65b0\u66f4\u65b0\u7684\u539f\u56e0&#xff0c;\u56e0\u4e3a\u6574\u4e2a\u4ee3\u7801&#xff0c;\u90fd\u662f\u6839\u636e\u9519\u8bef\u7801\u6765\u5224\u5b9a\u8bfb\u53d6\u548c\u5199\u5165\u662f\u5426\u662f\u6210\u529f\u7684\u3002\u8fd8\u6709\u4e2a\u539f\u56e0\u662f&#xff0c;Excute\u4e2d\u7684\u5e8f\u5217\u5316\u88ab\u6ce8\u91ca&#xff0c;\u53d6\u6d88\u6ce8\u91ca&#xff0c;\u4ee5\u53ca\u5f53\u62a5\u6587\u4e3a\u7a7a\u5b57\u7b26\u4e32\u65f6\u5e94\u8be5break&#xff0c;\u5230\u53d1\u9001\u62a5\u6587\u51fa&#xff0c;\u800c\u4e0d\u662freturn\u76f4\u63a5\u8fd4\u56de&#xff1a;\u4f46\u662f\u6709\u53ef\u80fd&#xff0c;\u5bf9\u65b9\u6ca1\u6709\u4e00\u4e2a\u5b8c\u6574\u7684\u8bf7\u6c42&#xff0c;\u56e0\u6b64\u8fd8\u9700\u8981\u5728Execute\u505a\u5224\u65ad\u3002<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"268\" src=\"2025-06-153sxo4x5chci.png\" width=\"965\" \/><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"305\" src=\"2025-06-154axt2wrajqq.png\" width=\"315\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u8fd0\u884c\u7ed3\u679c&#xff1a;\u00a0<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"473\" src=\"2025-06-15ob0lhw5r0ty.png\" width=\"442\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u53ef\u4ee5\u53d1\u73b0&#xff0c;\u5728\u5ba2\u6237\u7aef\u9000\u51fa\u65f6&#xff0c;\u6ca1\u6709\u5ba2\u6237\u9000\u51fa\u7684\u4fe1\u606f&#xff1a;\u4e5f\u5c31\u662f\u5404\u79cd\u5f02\u5e38\u5904\u7406&#xff0c;\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u5bf9\u5404\u79cd\u5f02\u5e38\u8fdb\u884c\u5904\u7406&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u7531\u4e8eepoller\u7684\u5bf9key\u503c\u7684\u5904\u7406\u4e00\u5b9a\u662fsockfd\u6709\u6548\u7684\u60c5\u51b5\u4e0b&#xff0c;\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5148\u5220\u9664\u8fde\u63a5&#xff1a;<\/span><\/p>\n<p>    void HandlerExcepter(Connection *conn)<br \/>\n    {<br \/>\n        \/\/ \u6574\u4e2a\u4ee3\u7801\u7684\u6240\u6709\u7684\u5f02\u5e38\u5904\u7406&#xff0c;\u5168\u5728\u8fd9\u91cc\u5904\u7406\u8bfb\u5199\u9519\u8bef&#xff0c;\u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5<br \/>\n        \/\/ \u5220\u9664\u8fde\u63a5<br \/>\n        conn-&gt;_R-&gt;DelConnection(conn-&gt;Sockfd());<br \/>\n    } <\/p>\n<h4 id=\"DelConnection%E5%87%BD%E6%95%B0%EF%BC%9A\"><span style=\"color:#0d0016\">DelConnection\u51fd\u6570&#xff1a;<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0 \u00a0 \u00a0 0.\u00a0\u5b89\u5168\u68c0\u67e5 \u00a0 \u00a0 <\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 1.\u5728\u5185\u6838\u4e2d\u79fb\u9664\u5bf9sockfd\u7684\u5173\u5fc3&#xff0c;epoll\u5f53\u4e2d\u53bb\u6389\u4e8b\u4ef6\u5173\u5fc3<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0 \u00a0 \u00a0 2.\u5173\u95edsockfd<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0 \u00a0 \u00a0 3.\u5728Reactor\u4e2d\u79fb\u9664\u5bf9Connection\u7684\u5173\u5fc3<\/span><\/p>\n<p><span style=\"color:#0d0016\">1.\u9700\u8981\u5728\u5185\u6838\u4e2d\u79fb\u9664\u5bf9sockfd\u7684\u5173\u5fc3\u4e5f\u5c31\u9700\u8981\u5c06\u5728epoller\u7c7b\u5f53\u4e2d\u5904\u7406&#xff0c;\u6dfb\u52a0\u5220\u9664fd\u7684\u63a5\u53e3<\/span><\/p>\n<p>    bool DelEvent(int fd) override<br \/>\n    {<br \/>\n        return 0 &#061;&#061; ::epoll_ctl(_epfd, EPOLL_CTL_DEL, fd, nullptr);<br \/>\n    } <\/p>\n<p><span style=\"color:#0d0016\">2.\u5173\u95ed\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26&#xff1a;\u7531\u4e8e\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26\u662f\u88ab\u5c01\u88c5\u5728Connection\u4e2d\u7684&#xff0c;\u56e0\u6b64\u5728connection\u63d0\u4f9b\u63a5\u53e3&#xff1a;<\/span><\/p>\n<p>     void Close()<br \/>\n    {<br \/>\n        if(_sockfd &gt;&#061; 0)<br \/>\n            ::close(_sockfd);<br \/>\n    } <\/p>\n<p><span style=\"color:#0d0016\">\u7531\u4e8eReactor\u505a\u4e8b\u4ef6\u6d3e\u53d1\u7684\u65f6\u5019&#xff0c;\u662f\u8bfb\u4e8b\u4ef6&#xff0c;\u5199\u4e8b\u4ef6\u90fd\u6709\u53ef\u80fd\u89e6\u53d1&#xff0c;\u5f02\u5e38\u540e&#xff0c;\u8bfb\u5199\u4e8b\u4ef6\u90fd\u88ab\u8bbe\u7f6e\u4e86&#xff0c;\u5982\u679c\u8bfb\u5199\u4e8b\u4ef6\u90fd\u88ab\u89e6\u53d1&#xff0c;\u5728\u5faa\u73af\u5904\u7406\u65f6\u95f4\u7684\u65f6\u5019&#xff0c;\u8bfb\u5199\u4e8b\u4ef6\u90fd\u6709\u53ef\u80fd\u88ab\u540c\u65f6\u6267\u884c\u3002\u8bfb\u5199\u4e8b\u4ef6\u7684\u65f6\u5019&#xff0c;\u5c06\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u90fd\u89e6\u53d1\u5f02\u5e38&#xff08;\u53ef\u80fd\u6027\u8f83\u4f4e&#xff0c;\u4f46\u662f\u4e3a\u4e86\u9c81\u68d2\u6027&#xff0c;\u6211\u4eec\u5728DelConnection\u505a\u5b89<\/span><span style=\"color:#0d0016\">\u5168\u68c0\u67e5&#xff09;<\/span><\/p>\n<\/p>\n<p><span style=\"color:#0d0016\">DelConnection<\/span><\/p>\n<p>     void DelConnection(int sockfd)<br \/>\n    {<br \/>\n        \/\/ 0. \u5b89\u5168\u68c0\u67e5<br \/>\n        if (!IsConnectionExist(sockfd))<br \/>\n        {<br \/>\n            return;<br \/>\n        }<br \/>\n        LOG(INFO, &#034;sockfd quit, \u670d\u52a1\u5668\u91ca\u653e\u6240\u6709\u8d44\u6e90\\\\n&#034;, sockfd);<br \/>\n        \/\/ 1.\u5728\u5185\u6838\u4e2d\u79fb\u9664\u5bf9sockfd\u7684\u5173\u5fc3&#xff08;epoll\u5f53\u4e2d\u53bb\u6389\u4e8b\u4ef6\u5173\u5fc3<br \/>\n        EnableConnectionReadWrite(sockfd, false, false);<br \/>\n        \/\/ 1.1\u518d\u4eceepoll\u4e2d\u5220\u9664\u6389\u8fd9\u4e2afd<br \/>\n        _epoller-&gt;DelEvent(sockfd);<br \/>\n        \/\/ 2.\u5173\u95edsockfd<br \/>\n        _connections[sockfd]-&gt;Close();<br \/>\n        \/\/ 3.\u5728Reactor\u4e2d\u79fb\u9664\u5bf9Connection\u7684\u5173\u5fc3<br \/>\n        delete _connections[sockfd];<br \/>\n        _connections.erase(sockfd);<br \/>\n    } <\/p>\n<p><span style=\"color:#0d0016\">\u8fd0\u884c\u7ed3\u679c&#xff1a;\u00a0<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"487\" src=\"2025-06-15eqylme22u3r.png\" width=\"460\" \/><\/p>\n<h2 id=\"%E6%AD%A3%E5%BC%8F%E8%AE%A4%E8%AF%86Reactor%E5%8F%8D%E5%BA%94%E5%A0%86%E6%A8%A1%E5%BC%8F\"><span style=\"color:#0d0016\">\u6b63\u5f0f\u8ba4\u8bc6Reactor\u53cd\u5e94\u5806\u6a21\u5f0f<\/span><\/h2>\n<p><span style=\"color:#0d0016\">\u672a\u6765\u5728\u5de5\u4f5c\u7684\u65f6\u5019&#xff0c;Reactor\u7ba1\u7406\u7684\u53ef\u80fd\u4e0d\u662fconnection\u8fde\u63a5&#xff0c;\u800c\u662fEvent&#xff08;\u4e5f\u662f\u5bf9fd&#xff0c;IO\u7f13\u51b2\u533a\u505a\u5c01\u88c5&#xff09;<\/span><\/p>\n<p>                          Request(\u8bf7\u6c42)  &amp;&amp;  Response&#xff08;\u5e94\u7b54&#xff09;<br \/>\n                    Protocol&#xff08;\u534f\u8bae\u5904\u7406&#xff09;PackageParse&#xff08;\u62a5\u6587\u89e3\u6790&#xff09;<br \/>\nListener(\u5904\u7406\u65b0\u8fde\u63a5) HandlerConnection&#xff08;\u5904\u7406\u666e\u901a\u5957\u63a5\u5b57IO\u4e8b\u4ef6\u4ee5\u53ca\u5f02\u5e38&#xff09;<br \/>\nConnection&#xff08;\u5c01\u88c5\u5957\u63a5\u5b57\u4ee5\u53ca\u7f13\u51b2\u533a&#xff09;<br \/>\nReactor(\u6700\u5e95\u5c42&#xff09;<br \/>\nepoller(\u53cd\u5e94\u7ed9Reactor) <\/p>\n<p><span style=\"color:#0d0016\">Reactor\u5c31\u50cf\u4e00\u4e2a\u80fd\u91cf\u6c60\u4e00\u6837&#xff0c;\u54ea\u4e00\u4e2a\u4e8b\u4ef6\u8282\u70b9\u5c31\u7eea\u4e86&#xff0c;\u5c31\u6fc0\u6d3bconnection\u7684\u54ea\u4e00\u4e2a\u8282\u70b9&#xff0c;\u89e6\u53d1\u8282\u70b9IO\u64cd\u4f5c&#xff0c;\u4e00\u65e6\u64cd\u4f5c\u5b8c\u6210&#xff0c;\u5c31\u4ea4\u7ed9\u4e0a\u5c42\u53bb\u8fdb\u884c\u62a5\u6587\u89e3\u6790\u4ee5\u53ca\u4e1a\u52a1\u5904\u7406<\/span><\/p>\n<h3 id=\"Reactor%20%E6%A8%A1%E5%BC%8F%E7%9A%84%E6%A0%B8%E5%BF%83%E6%80%9D%E6%83%B3\"><span style=\"color:#0d0016\">Reactor \u6a21\u5f0f\u7684\u6838\u5fc3\u601d\u60f3<\/span><\/h3>\n<p><span style=\"color:#ed7976\">(\u7c7b\u4f3c\u4e8e\u6253\u5730\u9f20\u6e38\u620f)<\/span><\/p>\n<\/p>\n<p><span style=\"color:#0d0016\">\u6e38\u620f\u673a\u53f0\u00a0&#061; \u4e8b\u4ef6\u5faa\u73af&#xff08;Event Loop&#xff09;<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u6301\u7eed\u626b\u63cf\u6240\u6709\u6d1e\u53e3\u7684\u72b6\u6001<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u4e0d\u9700\u8981\u4e3b\u52a8\u68c0\u67e5\u6bcf\u4e2a\u6d1e\u53e3&#xff0c;\u800c\u662f\u7b49\u5f85&#034;\u6709\u5730\u9f20\u5192\u51fa&#034;\u7684\u4e8b\u4ef6\u901a\u77e5<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u5730\u9f20\u5192\u51fa\u00a0&#061; \u4e8b\u4ef6\u89e6\u53d1&#xff08;Event Trigger&#xff09;<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u6bcf\u4e2a\u6d1e\u53e3\u90fd\u662f\u72ec\u7acb\u7684\u4e8b\u4ef6\u6e90<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5192\u51fa\u52a8\u4f5c\u4f1a\u751f\u6210\u5f85\u5904\u7406\u4e8b\u4ef6<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u9524\u5b50\u51fb\u6253\u00a0&#061; \u4e8b\u4ef6\u5904\u7406&#xff08;Event Handler&#xff09;<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u6bcf\u4e2a\u5904\u7406\u52a8\u4f5c\u5bf9\u5e94\u7279\u5b9a\u4e8b\u4ef6\u7c7b\u578b<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5904\u7406\u5b8c\u6210\u540e\u7acb\u5373\u590d\u4f4d\u51c6\u5907\u4e0b\u6b21\u54cd\u5e94<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#ed7976\">\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f51\u7edc\u670d\u52a1\u5668\u8bbe\u8ba1\u7684\u4e3b\u6d41\u6a21\u5f0f&#xff08;libevent\u3001mudou&#8230;\u5e93&#xff09;<\/span><\/p>\n<p><span style=\"color:#0d0016\">Reactor \u6a21\u5f0f\u7684\u6838\u5fc3\u601d\u60f3\u662f\u5c06\u4e8b\u4ef6\u7684\u5904\u7406\u5206\u4e3a\u4e24\u4e2a\u4e3b\u8981\u90e8\u5206&#xff1a;\u4e8b\u4ef6\u5206\u79bb&#xff08;Demultiplexing&#xff09;\u548c\u4e8b\u4ef6\u5904\u7406&#xff08;Handling&#xff09;\u3002\u4e8b\u4ef6\u5206\u79bb\u8d1f\u8d23\u76d1\u542c\u591a\u4e2a\u8f93\u5165\u6e90&#xff0c;\u5e76\u5728\u4e8b\u4ef6\u53d1\u751f\u65f6\u5c06\u5176\u5206\u53d1\u5230\u5bf9\u5e94\u7684\u4e8b\u4ef6\u5904\u7406\u5668&#xff1b;\u4e8b\u4ef6\u5904\u7406\u5668\u5219\u8d1f\u8d23\u5177\u4f53\u7684\u4e8b\u4ef6\u5904\u7406\u903b\u8f91\u3002<\/span><\/p>\n<h3 id=\"Reactor%20%E6%A8%A1%E5%BC%8F%E7%9A%84%E4%B8%BB%E8%A6%81%E7%BB%84%E4%BB%B6\"><span style=\"color:#0d0016\">Reactor \u6a21\u5f0f\u7684\u4e3b\u8981\u7ec4\u4ef6<\/span><\/h3>\n<p><span style=\"color:#0d0016\">Reactor \u6a21\u5f0f\u901a\u5e38\u7531\u4ee5\u4e0b\u51e0\u4e2a\u7ec4\u4ef6\u6784\u6210&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">Reactor&#xff1a;\u8d1f\u8d23\u4e8b\u4ef6\u7684\u76d1\u542c\u548c\u5206\u53d1\u3002\u5b83\u901a\u8fc7\u4e8b\u4ef6\u5206\u79bb\u5668&#xff08;\u5982\u00a0select\u3001epoll\u00a0\u6216\u00a0kqueue&#xff09;\u76d1\u542c\u591a\u4e2a\u8f93\u5165\u6e90&#xff0c;\u5e76\u5728\u4e8b\u4ef6\u53d1\u751f\u65f6\u5c06\u5176\u5206\u53d1\u7ed9\u5bf9\u5e94\u7684\u4e8b\u4ef6\u5904\u7406\u5668\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u4e8b\u4ef6\u5904\u7406\u5668&#xff08;EventHandler&#xff09;&#xff1a;\u8d1f\u8d23\u5904\u7406\u5177\u4f53\u7684\u4e8b\u4ef6\u3002\u6bcf\u4e2a\u4e8b\u4ef6\u5904\u7406\u5668\u901a\u5e38\u4e0e\u4e00\u4e2a\u8f93\u5165\u6e90\u5173\u8054&#xff0c;\u5904\u7406\u8be5\u8f93\u5165\u6e90\u4ea7\u751f\u7684\u4e8b\u4ef6\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u4e8b\u4ef6\u5206\u79bb\u5668&#xff08;Demultiplexer&#xff09;&#xff1a;\u8d1f\u8d23\u76d1\u542c\u591a\u4e2a\u8f93\u5165\u6e90&#xff0c;\u5e76\u5728\u4e8b\u4ef6\u53d1\u751f\u65f6\u901a\u77e5 Reactor\u3002\u5e38\u89c1\u7684\u4e8b\u4ef6\u5206\u79bb\u5668\u5305\u62ec\u00a0select\u3001poll\u3001epoll\u00a0\u7b49\u3002<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Reactor&#xff08;\u5e95\u5c42&#xff09;&#043; \u7ebf\u7a0b\u6c60&#xff08;\u4e1a\u52a1\u5904\u7406&#xff09;<\/span><\/p>\n<h3 id=\"Reactor%EF%BC%88%E5%BA%95%E5%B1%82%EF%BC%89%2B%20%E7%BA%BF%E7%A8%8B%E6%B1%A0%EF%BC%88%E4%B8%9A%E5%8A%A1%E5%A4%84%E7%90%86%EF%BC%89\"><span style=\"color:#0d0016\">Reactor&#xff08;\u5e95\u5c42&#xff09;&#043; \u7ebf\u7a0b\u6c60&#xff08;\u4e1a\u52a1\u5904\u7406&#xff09;<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u5bf9\u4e8e\u6211\u4eec\u7684\u4ee3\u7801\u7684\u6539\u9020&#xff1a;<\/span><span style=\"color:#ed7976\"> \u5c06\u62a5\u6587\u89e3\u6790&#xff08;\u4e1a\u52a1\u5904\u7406&#xff09;\u7684\u63a7\u5236\u6743\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u53bb\u5904\u7406<\/span><\/p>\n<h4 id=\"%E2%80%8B%E7%BC%96%E8%BE%91Reactor%20%2B%20%E7%BA%BF%E7%A8%8B%E6%B1%A0%E7%9A%84%E6%9E%B6%E6%9E%84\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"191\" src=\"2025-06-15x1tehmm4pyd.png\" width=\"757\" \/><span style=\"color:#0d0016\">Reactor &#043; \u7ebf\u7a0b\u6c60\u7684\u67b6\u6784<\/span><\/h4>\n<p><span style=\"color:#0d0016\">Reactor \u548c\u7ebf\u7a0b\u6c60\u7684\u7ed3\u5408\u662f\u5f02\u6b65\u7f16\u7a0b\u6846\u67b6\u4e2d\u7684\u7ecf\u5178\u8bbe\u8ba1&#xff0c;\u5176\u5de5\u4f5c\u6d41\u7a0b\u5982\u4e0b&#xff1a;<\/span><\/p>\n<li><span style=\"color:#0d0016\">\u4e8b\u4ef6\u76d1\u542c&#xff1a;Reactor \u76d1\u542c I\/O \u4e8b\u4ef6&#xff08;\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u7b49&#xff09;\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u4e8b\u4ef6\u5206\u53d1&#xff1a;\u5f53\u4e8b\u4ef6\u53d1\u751f\u65f6&#xff0c;Reactor \u5c06\u5176\u5206\u53d1\u7ed9\u5bf9\u5e94\u7684\u5904\u7406\u5668\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u4efb\u52a1\u63d0\u4ea4&#xff1a;\u5904\u7406\u5668\u5c06\u5177\u4f53\u7684\u4e1a\u52a1\u903b\u8f91\u4efb\u52a1\u63d0\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u4efb\u52a1\u6267\u884c&#xff1a;\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1&#xff0c;\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9 Reactor\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u7ed3\u679c\u5904\u7406&#xff1a;Reactor \u5c06\u7ed3\u679c\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u6216\u8fdb\u884c\u540e\u7eed\u5904\u7406\u3002<\/span><\/li>\n<h3 id=\"%E5%BC%95%E5%8F%91%E7%AB%9E%E4%BA%89%E6%9D%A1%E4%BB%B6%E3%80%81%E6%95%B0%E6%8D%AE%E4%B8%80%E8%87%B4%E6%80%A7%E9%97%AE%E9%A2%98%E5%A4%84%E7%90%86\"><span style=\"color:#0d0016\">\u5f15\u53d1\u7ade\u4e89\u6761\u4ef6\u3001\u6570\u636e\u4e00\u81f4\u6027\u95ee\u9898\u5904\u7406<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d&#xff0c;\u5f53\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u64cd\u4f5c\u540c\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u65f6&#xff0c;\u53ef\u80fd\u4f1a\u5f15\u53d1\u7ade\u4e89\u6761\u4ef6\u548c\u6570\u636e\u4e00\u81f4\u6027\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e0d\u540c\u7ebf\u7a0b\u64cd\u4f5c\u540c\u4e00\u4e2afd\u7684\u8be6\u7ec6\u573a\u666f\u5206\u6790&#xff1a;<\/span><\/p>\n<li>\n<p><span style=\"color:#0d0016\">Reactor\u6a21\u5f0f\u4e2d\u7684\u6d88\u606f\u53d1\u9001&#xff1a; Reactor\u6a21\u5f0f\u901a\u5e38\u7528\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f16\u7a0b\u6a21\u578b\u3002\u5728Reactor\u4e2d&#xff0c;\u4e00\u4e2a\u4e3b\u7ebf\u7a0b&#xff08;\u6216\u4e8b\u4ef6\u5faa\u73af&#xff09;\u8d1f\u8d23\u76d1\u542c\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u4e8b\u4ef6&#xff08;\u5982\u53ef\u8bfb\u3001\u53ef\u5199\u7b49&#xff09;&#xff0c;\u5e76\u5c06\u4e8b\u4ef6\u5206\u53d1\u7ed9\u76f8\u5e94\u7684\u5904\u7406\u5668&#xff08;Handler&#xff09;\u3002\u5982\u679cReactor\u7ebf\u7a0b\u5728\u53d1\u9001\u6d88\u606f\u65f6&#xff0c;\u5176\u4ed6\u7ebf\u7a0b\u4e5f\u5728\u64cd\u4f5c\u540c\u4e00\u4e2afd&#xff0c;\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u6df7\u4e71\u6216\u4e22\u5931\u3002\u4f8b\u5982&#xff0c;Reactor\u7ebf\u7a0b\u6b63\u5728\u5411fd\u5199\u5165\u6570\u636e&#xff0c;\u800c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5173\u95ed\u4e86\u8be5fd&#xff0c;\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5199\u5165\u5931\u8d25\u6216\u7a0b\u5e8f\u5d29\u6e83\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u7ebf\u7a0b\u6c60\u4e2d\u7684\u6d88\u606f\u53d1\u9001&#xff1a; \u7ebf\u7a0b\u6c60\u901a\u5e38\u7528\u4e8e\u5904\u7406\u5e76\u53d1\u4efb\u52a1\u3002\u5f53\u7ebf\u7a0b\u6c60\u4e2d\u7684\u591a\u4e2a\u7ebf\u7a0b\u5c1d\u8bd5\u5411\u540c\u4e00\u4e2afd\u53d1\u9001\u6d88\u606f\u65f6&#xff0c;\u53ef\u80fd\u4f1a\u51fa\u73b0\u7ade\u6001\u6761\u4ef6\u3002\u4f8b\u5982&#xff0c;\u7ebf\u7a0bA\u6b63\u5728\u5411fd\u5199\u5165\u6d88\u606f&#xff0c;\u800c\u7ebf\u7a0bB\u4e5f\u5c1d\u8bd5\u5411\u540c\u4e00\u4e2afd\u5199\u5165\u6d88\u606f&#xff0c;\u8fd9\u53ef\u80fd\u5bfc\u81f4\u6d88\u606f\u5185\u5bb9\u4ea4\u9519\u6216\u6570\u636e\u635f\u574f\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u9501&#xff08;\u5982\u4e92\u65a5\u9501&#xff09;\u6765\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u64cd\u4f5cfd\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u5176\u4ed6\u7ebf\u7a0b\u7684IO\u4e8b\u4ef6\u5904\u7406&#xff1a; \u9664\u4e86Reactor\u548c\u7ebf\u7a0b\u6c60&#xff0c;\u5176\u4ed6\u7ebf\u7a0b\u4e5f\u53ef\u80fd\u76f4\u63a5\u64cd\u4f5cfd\u8fdb\u884cIO\u64cd\u4f5c\u3002\u4f8b\u5982&#xff0c;\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u6b63\u5728\u4ecefd\u8bfb\u53d6\u6570\u636e&#xff0c;\u800c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5173\u95ed\u4e86\u8be5fd&#xff0c;\u8fd9\u4f1a\u5bfc\u81f4\u8bfb\u53d6\u64cd\u4f5c\u5931\u8d25\u3002\u6b64\u5916&#xff0c;\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528close(fd)&#xff0c;\u53ef\u80fd\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a&#xff0c;\u56e0\u4e3afd\u53ef\u80fd\u5df2\u7ecf\u88ab\u91ca\u653e\u3002<\/span><\/p>\n<\/li>\n<p><span style=\"color:#0d0016\">\u5728\u6211\u4eec\u7684\u4ee3\u7801\u4e2d\u7684\u89e3\u51b3\u529e\u6cd5&#xff1a;\u53ea\u8fdb\u884c\u6fc0\u6d3b\u5bf9\u5199\u4e8b\u4ef6\u7684\u5173\u5fc3<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"436\" src=\"2025-06-15sscb5hrdzws.png\" width=\"1021\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u6211\u53ea\u8fdb\u884c\u6fc0\u6d3b\u5bf9\u5199\u4e8b\u4ef6\u7684\u5173\u5fc3&#xff0c;\u672a\u6765\u6240\u6709\u7684IO\u5168\u7531Reactor\u81ea\u52a8\u5904\u7406&#xff0c;\u591a\u7ebf\u7a0b\u53ea\u8981\u8d1f\u8d23\u5b89\u5168\u5904\u7406\u8bf7\u6c42\u548c\u5e94\u7b54\u00a0&#xff1a;<\/span><\/p>\n<p>         if (!conn-&gt;Outbuffer().empty())<br \/>\n        {<br \/>\n            \/\/ \u65b9\u6cd52&#xff1a;\u6211\u53ea\u8fdb\u884c\u6fc0\u6d3b\u5bf9\u5199\u4e8b\u4ef6\u7684\u5173\u5fc3&#xff0c;\u672a\u6765\u6240\u6709\u7684IO\u5168\u7531Reactor\u81ea\u52a8\u5904\u7406&#xff0c;\u591a\u7ebf\u7a0b\u53ea\u8981\u8d1f\u8d23\u5b89\u5168\u5904\u7406\u8bf7\u6c42\u548c\u5e94\u7b54<br \/>\n            conn-&gt;_R-&gt;EnableConnectionReadWrite(conn-&gt;Sockfd(), true, true);<br \/>\n        } <\/p>\n<p><span style=\"color:#0d0016\">\u5728\u8fd9\u91cc\u53ea\u4e13\u6ce8\u4e8e\u6fc0\u6d3b\u5199\u4e8b\u4ef6&#xff08;Write Event&#xff09;\u7684\u6ce8\u518c\u4e0e\u5904\u7406&#xff0c;\u800c\u5c06\u6240\u6709\u7684I\/O\u64cd\u4f5c&#xff08;\u5305\u62ec\u8bfb\u3001\u5199\u3001\u8fde\u63a5\u3001\u5173\u95ed\u7b49&#xff09;\u4ea4\u7531Reactor\u7ec4\u4ef6\u81ea\u52a8\u7ba1\u7406\u3002<\/span><\/p>\n<p>Reactor:\u68c0\u6d4b\u4e8b\u4ef6\u5c31\u7eea\u3001IO\u5904\u7406<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-\u300b\u534a\u540c\u6b65&#xff0c;\u534a\u5f02\u6b65\u6a21\u5f0f\u3001linux\u670d\u52a1\u5668\u6700\u5e38\u89c1\u7684\u6a21\u5f0f<\/p>\n<p>\u591a\u7ebf\u7a0b&#xff1a;\u4e1a\u52a1\u5904\u7406 <\/p>\n<p><span style=\"color:#0d0016\">\u5177\u4f53\u6765\u8bf4&#xff0c;Reactor\u4f1a\u901a\u8fc7\u4e8b\u4ef6\u5faa\u73af&#xff08;Event Loop&#xff09;\u6301\u7eed\u76d1\u542c\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;File Descriptor&#xff09;\u4e0a\u7684\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u5f53\u68c0\u6d4b\u5230\u53ef\u8bfb\u3001\u53ef\u5199\u7b49I\/O\u4e8b\u4ef6\u65f6&#xff0c;Reactor\u4f1a\u8c03\u7528\u9884\u5148\u6ce8\u518c\u7684\u56de\u8c03\u51fd\u6570\u8fdb\u884c\u5904\u7406\u3002\u5bf9\u4e8e\u5199\u4e8b\u4ef6&#xff0c;\u6211\u53ea\u9700\u5728\u9700\u8981\u53d1\u9001\u6570\u636e\u65f6&#xff0c;\u901a\u8fc7Reactor\u7684API\u6ce8\u518c\u5199\u4e8b\u4ef6&#xff0c;Reactor\u4f1a\u5728\u5408\u9002\u7684\u65f6\u673a&#xff08;\u5982\u7f13\u51b2\u533a\u53ef\u5199\u65f6&#xff09;\u89e6\u53d1\u56de\u8c03\u51fd\u6570&#xff0c;\u5b8c\u6210\u6570\u636e\u7684\u53d1\u9001\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b&#xff0c;\u7ebf\u7a0b\u7684\u4e3b\u8981\u804c\u8d23\u662f\u5b89\u5168\u5730\u5904\u7406\u8bf7\u6c42\u548c\u751f\u6210\u5e94\u7b54&#xff08;\u4e1a\u52a1\u5904\u7406&#xff09;&#xff0c;\u800c\u65e0\u9700\u76f4\u63a5\u53c2\u4e0eI\/O\u64cd\u4f5c\u3002\u6bcf\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u901a\u8fc7\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\u6216\u5176\u4ed6\u540c\u6b65\u673a\u5236&#xff0c;\u5c06\u5f85\u53d1\u9001\u7684\u6570\u636e\u4f20\u9012\u7ed9Reactor&#xff0c;\u7531Reactor\u7edf\u4e00\u5904\u7406\u3002\u8fd9\u79cd\u8bbe\u8ba1\u4e0d\u4ec5\u7b80\u5316\u4e86\u7ebf\u7a0b\u7684\u804c\u8d23&#xff0c;\u8fd8\u907f\u514d\u4e86\u591a\u7ebf\u7a0b\u76f4\u63a5\u64cd\u4f5cI\/O\u8d44\u6e90\u53ef\u80fd\u5bfc\u81f4\u7684\u7ade\u4e89\u6761\u4ef6\u548c\u6027\u80fd\u74f6\u9888\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u4f46\u662f\u4ee3\u7801\u4f1a\u5341\u5206\u4e0d\u4f18\u96c5\u3002<\/span><\/p>\n<h3 id=\"%E8%BD%AC%E5%8F%98%E6%80%9D%E8%B7%AF%EF%BC%9AOne%20thread%20One%20Loop\"><span style=\"color:#0d0016\">\u8f6c\u53d8\u601d\u8def&#xff1a;One thread One Loop<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u4e00\u4e2aReactor\u505a\u6240\u6709\u7684\u4e8b&#xff0c;\u518d\u6709\u4e00\u4e2a\u5355\u72ecReactor\u7528\u4e8e\u8fde\u63a5\u4e8b\u4ef6\u6216\u65b0\u7684sockfd\u7684\u6d3e\u53d1\u4efb\u52a1\u6d3e\u53d1\u7ed9Reactor&#xff08;\u591a\u8fdb\u7a0bor\u591a\u7ebf\u7a0b&#xff09;&#xff0c;\u672a\u6765\u53ef\u80fd\u670940000\u4e2afd\u5206\u522b\u6d3e\u53d1\u7ed9Reactor&#xff08;\u591a\u8fdb\u7a0bor\u591a\u7ebf\u7a0b&#xff09;&#xff0c;\u6765\u5e76\u53d1\u5904\u7406\u3002\u5c06\u4e8b\u4ef6\u5904\u7406\u4efb\u52a1\u5206\u6563\u5230\u591a\u4e2aReactor\u4e2d&#xff0c;\u6bcf\u4e2aReactor\u8fd0\u884c\u5728\u72ec\u7acb\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u4e2d&#xff0c;\u4ece\u800c\u5b9e\u73b0\u66f4\u9ad8\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"553\" src=\"2025-06-15aw4woltfoi0.png\" width=\"1531\" \/><\/p>\n<h4 id=\"%E8%AE%BE%E8%AE%A1%E6%80%9D%E8%B7%AF\"><span style=\"color:#0d0016\">\u8bbe\u8ba1\u601d\u8def<\/span><\/h4>\n<li>\n<p><span style=\"color:#0d0016\">\u4e3bReactor&#xff1a;\u8d1f\u8d23\u76d1\u542c\u548c\u63a5\u53d7\u65b0\u7684\u8fde\u63a5\u8bf7\u6c42\u3002\u5f53\u6709\u65b0\u7684\u8fde\u63a5\u5efa\u7acb\u65f6&#xff0c;\u4e3bReactor\u4f1a\u5c06\u65b0\u8fde\u63a5\u7684socket\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;sockfd&#xff09;\u5206\u914d\u7ed9\u4e00\u4e2a\u5b50Reactor\u3002\u4e3bReactor\u901a\u5e38\u8fd0\u884c\u5728\u5355\u72ec\u7684\u7ebf\u7a0b\u4e2d&#xff0c;\u4e13\u6ce8\u4e8e\u5904\u7406\u8fde\u63a5\u4e8b\u4ef6&#xff0c;\u786e\u4fdd\u8fde\u63a5\u7684\u5feb\u901f\u54cd\u5e94\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u5b50Reactor&#xff1a;\u8d1f\u8d23\u5904\u7406\u5df2\u5efa\u7acb\u8fde\u63a5\u7684I\/O\u4e8b\u4ef6\u3002\u6bcf\u4e2a\u5b50Reactor\u8fd0\u884c\u5728\u72ec\u7acb\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u4e2d&#xff0c;\u62e5\u6709\u81ea\u5df1\u7684\u4e8b\u4ef6\u5faa\u73af\u3002\u5b50Reactor\u7684\u6570\u91cf\u53ef\u4ee5\u6839\u636e\u7cfb\u7edf\u7684CPU\u6838\u5fc3\u6570\u6216\u9884\u671f\u7684\u5e76\u53d1\u8fde\u63a5\u6570\u8fdb\u884c\u52a8\u6001\u8c03\u6574\u3002\u4f8b\u5982&#xff0c;\u5982\u679c\u7cfb\u7edf\u67098\u4e2aCPU\u6838\u5fc3&#xff0c;\u53ef\u4ee5\u521b\u5efa8\u4e2a\u5b50Reactor&#xff0c;\u6bcf\u4e2aReactor\u5904\u7406\u5927\u7ea65000\u4e2a\u8fde\u63a5&#xff0c;\u4ece\u800c\u5145\u5206\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u7684\u8ba1\u7b97\u80fd\u529b\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u4efb\u52a1\u6d3e\u53d1&#xff1a;\u4e3bReactor\u5728\u63a5\u6536\u5230\u65b0\u7684\u8fde\u63a5\u540e&#xff0c;\u4f1a\u6839\u636e\u4e00\u5b9a\u7684\u7b56\u7565&#xff08;\u5982\u8f6e\u8be2\u3001\u54c8\u5e0c\u7b49&#xff09;\u5c06sockfd\u5206\u914d\u7ed9\u4e00\u4e2a\u5b50Reactor\u3002\u5b50Reactor\u5728\u63a5\u6536\u5230\u65b0\u7684sockfd\u540e&#xff0c;\u4f1a\u5c06\u5176\u6ce8\u518c\u5230\u81ea\u5df1\u7684\u4e8b\u4ef6\u5faa\u73af\u4e2d&#xff0c;\u5e76\u5f00\u59cb\u76d1\u542c\u8be5\u8fde\u63a5\u7684\u8bfb\u5199\u4e8b\u4ef6\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u5e76\u53d1\u5904\u7406&#xff1a;\u6bcf\u4e2a\u5b50Reactor\u72ec\u7acb\u5904\u7406\u5176\u8d1f\u8d23\u7684\u8fde\u63a5&#xff0c;\u4e92\u4e0d\u5e72\u6270\u3002\u8fd9\u79cd\u8bbe\u8ba1\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7cfb\u7edf\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b&#xff0c;\u5c24\u5176\u662f\u5728\u9762\u5bf9\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u65f6\u3002\u4f8b\u5982&#xff0c;\u5f53\u7cfb\u7edf\u9700\u8981\u5904\u740640000\u4e2a\u5e76\u53d1\u8fde\u63a5\u65f6&#xff0c;\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u8fde\u63a5\u5747\u5300\u5730\u5206\u914d\u7ed9\u591a\u4e2a\u5b50Reactor&#xff0c;\u6bcf\u4e2aReactor\u5904\u7406\u4e00\u90e8\u5206\u8fde\u63a5&#xff0c;\u4ece\u800c\u907f\u514d\u5355\u4e2a\u4e8b\u4ef6\u5faa\u73af\u7684\u8fc7\u8f7d\u3002<\/span><\/p>\n<\/li>\n<p><span style=\"color:#0d0016\">\u591a\u8fdb\u7a0b&#xff1a;<\/span><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1072\" src=\"2025-06-15g5otislhyj1.png\" width=\"1656\" \/><\/p>\n<p><span style=\"color:#fe2c24\">\u591a\u7ebf\u7a0b&#xff1a;<\/span><\/p>\n<p><span style=\"color:#fe2c24\">version1(\u4e0d\u597d)<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1102\" src=\"2025-06-15amtsons4w23.png\" width=\"2501\" \/><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"183\" src=\"2025-06-15cmih4jun50y.png\" width=\"275\" \/><\/p>\n<p><span style=\"color:#0d0016\">version(2)<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1093\" src=\"2025-06-15rzzbwon5oel.png\" width=\"2095\" \/><\/p>\n<p><span style=\"color:#0d0016\">version(3)<\/span><\/p>\n<p><span style=\"color:#0d0016\">eventfd<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86Linux\u7cfb\u7edf\u4e2d\u4f7f\u7528eventfd\u5b9e\u73b0\u7684\u7ebf\u7a0b\u95f4\u5524\u9192\u673a\u5236\u3002<\/span><\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include &lt;sys\/eventfd.h&gt;<\/p>\n<p>int evfd &#061; -1;<\/p>\n<p>\/\/ \u5524\u9192\u673a\u5236\u662f\u57fa\u4e8e\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684.<br \/>\nvoid *wait(void *args)<br \/>\n{<br \/>\n    std::string name &#061; (const char *)args;<br \/>\n    while (true)<br \/>\n    {<br \/>\n        uint64_t flag &#061; 0; \/\/ \u5fc5\u987b\u662f8\u5b57\u8282\u7684\u6570\u636e<br \/>\n        ssize_t n &#061; ::read(evfd, &amp;flag, sizeof(flag));<br \/>\n        std::cout &lt;&lt; name &lt;&lt; &#034; \u88ab\u5524\u9192&#8230;, flag: %d: &#034; &lt;&lt; flag &lt;&lt; &#034;, errno is : &#034; &lt;&lt; errno &lt;&lt; std::endl;<br \/>\n    }<br \/>\n}<\/p>\n<p>void *wakeup(void *args)<br \/>\n{<br \/>\n    std::string name &#061; (const char *)args;<br \/>\n    while (true)<br \/>\n    {<br \/>\n        errno&#043;&#043;; \/\/ \u4fee\u6539errno<br \/>\n        sleep(1);<br \/>\n        std::cout &lt;&lt; &#034;wake up one thread, errno : &#034; &lt;&lt; errno &lt;&lt; std::endl;<br \/>\n        uint64_t flag &#061; 1;<br \/>\n        ::write(evfd, &amp;flag, sizeof(flag));<br \/>\n    }<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    evfd &#061; ::eventfd(0, 0);<br \/>\n    pthread_t tid1, tid2;<br \/>\n    pthread_create(&amp;tid1, nullptr, wait, (void *)&#034;thread-1&#034;);<br \/>\n    pthread_create(&amp;tid2, nullptr, wakeup, (void *)&#034;thread-2&#034;);<\/p>\n<p>    pthread_join(tid1, nullptr);<br \/>\n    pthread_join(tid2, nullptr);<\/p>\n<p>    ::close(evfd);<br \/>\n    return 0;<br \/>\n} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"800\" src=\"2025-06-15ej2eepze1jf.png\" width=\"2299\" \/><\/p>\n<p><span style=\"color:#0d0016\">errno\u7279\u6027&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u6bcf\u4e2a\u7ebf\u7a0b\u7ef4\u62a4\u72ec\u7acb\u7684errno\u526f\u672c<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u7cfb\u7edf\u8c03\u7528\u6210\u529f\u65f6\u4e0d\u4f1a\u4fee\u6539errno<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u793a\u4f8b\u4e2derrno&#043;&#043;\u4ec5\u5f71\u54cd\u5f53\u524d\u7ebf\u7a0b\u7684errno\u503c<\/span><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e23\u6b21\uff0c\u6536\u85cf24\u6b21\u3002\u6458\u8981\uff1a\u672c\u6587\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5229\u7528Reactor\u6a21\u5f0f\u6784\u5efa\u4f4e\u8026\u5408\u3001\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u670d\u52a1\u5668\u3002\u901a\u8fc7\u5c01\u88c5Connection\u7c7b\u7ba1\u7406\u5957\u63a5\u5b57\u53ca\u7f13\u51b2\u533a\uff0c\u7ed3\u5408epoll\u591a\u8def\u590d\u7528\u6280\u672f\u5b9e\u73b0\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\uff0c\u6838\u5fc3\u6a21\u5757\u5305\u62ecListener\uff08\u5904\u7406\u65b0\u8fde\u63a5\uff09\u3001HandlerConnection\uff08\u5904\u7406IO\u4e8b\u4ef6\uff09\u548cReactor\uff08\u4e8b\u4ef6\u6d3e\u53d1\uff09\u3002\u6587\u7ae0\u6df1\u5165\u89e3\u6790\u975e\u963b\u585eIO\u4e0e\u8fb9\u7f18\u89e6\u53d1\uff08ET\uff09\u6a21\u5f0f\u7684\u9ad8\u6548\u5b9e\u73b0\uff0c\u5e76\u901a\u8fc7\u7ebf\u7a0b\u6c60\u4f18\u5316\u4e1a\u52a1\u5904\u7406\u903b\u8f91\uff0c\u89e3\u51b3\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u7ade\u4e89\u6761\u4ef6\u4e0e\u6570\u636e\u4e00\u81f4\u6027\u95ee\u9898\u3002\u6b64\u5916\uff0c\u5f15\u5165eventfd\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u5524\u9192\u673a\u5236\uff0c\u9a8c\u8bc1errno\u7684\u7ebf\u7a0b\u5b89\u5168\u6027\u3002\u6700\u7ec8\u901a\u8fc7\u6a21\u5757\u5316\u8bbe_epoii\u673a\u5236<\/p>\n","protected":false},"author":2,"featured_media":43830,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,43,78,211,2874],"topic":[],"class_list":["post-43850","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-43","tag-78","tag-211","tag-2874"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Linux\u3011\u5229\u7528\u591a\u8def\u8f6c\u63a5epoll\u673a\u5236\u3001ET\u6a21\u5f0f\uff0c\u57fa\u4e8eReactor\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/43850.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Linux\u3011\u5229\u7528\u591a\u8def\u8f6c\u63a5epoll\u673a\u5236\u3001ET\u6a21\u5f0f\uff0c\u57fa\u4e8eReactor\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e23\u6b21\uff0c\u6536\u85cf24\u6b21\u3002\u6458\u8981\uff1a\u672c\u6587\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5229\u7528Reactor\u6a21\u5f0f\u6784\u5efa\u4f4e\u8026\u5408\u3001\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u670d\u52a1\u5668\u3002\u901a\u8fc7\u5c01\u88c5Connection\u7c7b\u7ba1\u7406\u5957\u63a5\u5b57\u53ca\u7f13\u51b2\u533a\uff0c\u7ed3\u5408epoll\u591a\u8def\u590d\u7528\u6280\u672f\u5b9e\u73b0\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\uff0c\u6838\u5fc3\u6a21\u5757\u5305\u62ecListener\uff08\u5904\u7406\u65b0\u8fde\u63a5\uff09\u3001HandlerConnection\uff08\u5904\u7406IO\u4e8b\u4ef6\uff09\u548cReactor\uff08\u4e8b\u4ef6\u6d3e\u53d1\uff09\u3002\u6587\u7ae0\u6df1\u5165\u89e3\u6790\u975e\u963b\u585eIO\u4e0e\u8fb9\u7f18\u89e6\u53d1\uff08ET\uff09\u6a21\u5f0f\u7684\u9ad8\u6548\u5b9e\u73b0\uff0c\u5e76\u901a\u8fc7\u7ebf\u7a0b\u6c60\u4f18\u5316\u4e1a\u52a1\u5904\u7406\u903b\u8f91\uff0c\u89e3\u51b3\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u7ade\u4e89\u6761\u4ef6\u4e0e\u6570\u636e\u4e00\u81f4\u6027\u95ee\u9898\u3002\u6b64\u5916\uff0c\u5f15\u5165eventfd\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u5524\u9192\u673a\u5236\uff0c\u9a8c\u8bc1errno\u7684\u7ebf\u7a0b\u5b89\u5168\u6027\u3002\u6700\u7ec8\u901a\u8fc7\u6a21\u5757\u5316\u8bbe_epoii\u673a\u5236\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/43850.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-15T07:31:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073145-684e76e1be0bb.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=\"21 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/43850.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/43850.html\",\"name\":\"\u3010Linux\u3011\u5229\u7528\u591a\u8def\u8f6c\u63a5epoll\u673a\u5236\u3001ET\u6a21\u5f0f\uff0c\u57fa\u4e8eReactor\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-15T07:31:54+00:00\",\"dateModified\":\"2025-06-15T07:31:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/43850.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/43850.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/43850.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Linux\u3011\u5229\u7528\u591a\u8def\u8f6c\u63a5epoll\u673a\u5236\u3001ET\u6a21\u5f0f\uff0c\u57fa\u4e8eReactor\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010Linux\u3011\u5229\u7528\u591a\u8def\u8f6c\u63a5epoll\u673a\u5236\u3001ET\u6a21\u5f0f\uff0c\u57fa\u4e8eReactor\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/43850.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u3011\u5229\u7528\u591a\u8def\u8f6c\u63a5epoll\u673a\u5236\u3001ET\u6a21\u5f0f\uff0c\u57fa\u4e8eReactor\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e23\u6b21\uff0c\u6536\u85cf24\u6b21\u3002\u6458\u8981\uff1a\u672c\u6587\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5229\u7528Reactor\u6a21\u5f0f\u6784\u5efa\u4f4e\u8026\u5408\u3001\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u670d\u52a1\u5668\u3002\u901a\u8fc7\u5c01\u88c5Connection\u7c7b\u7ba1\u7406\u5957\u63a5\u5b57\u53ca\u7f13\u51b2\u533a\uff0c\u7ed3\u5408epoll\u591a\u8def\u590d\u7528\u6280\u672f\u5b9e\u73b0\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\uff0c\u6838\u5fc3\u6a21\u5757\u5305\u62ecListener\uff08\u5904\u7406\u65b0\u8fde\u63a5\uff09\u3001HandlerConnection\uff08\u5904\u7406IO\u4e8b\u4ef6\uff09\u548cReactor\uff08\u4e8b\u4ef6\u6d3e\u53d1\uff09\u3002\u6587\u7ae0\u6df1\u5165\u89e3\u6790\u975e\u963b\u585eIO\u4e0e\u8fb9\u7f18\u89e6\u53d1\uff08ET\uff09\u6a21\u5f0f\u7684\u9ad8\u6548\u5b9e\u73b0\uff0c\u5e76\u901a\u8fc7\u7ebf\u7a0b\u6c60\u4f18\u5316\u4e1a\u52a1\u5904\u7406\u903b\u8f91\uff0c\u89e3\u51b3\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u7ade\u4e89\u6761\u4ef6\u4e0e\u6570\u636e\u4e00\u81f4\u6027\u95ee\u9898\u3002\u6b64\u5916\uff0c\u5f15\u5165eventfd\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u5524\u9192\u673a\u5236\uff0c\u9a8c\u8bc1errno\u7684\u7ebf\u7a0b\u5b89\u5168\u6027\u3002\u6700\u7ec8\u901a\u8fc7\u6a21\u5757\u5316\u8bbe_epoii\u673a\u5236","og_url":"https:\/\/www.wsisp.com\/helps\/43850.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-15T07:31:54+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250615073145-684e76e1be0bb.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"21 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/43850.html","url":"https:\/\/www.wsisp.com\/helps\/43850.html","name":"\u3010Linux\u3011\u5229\u7528\u591a\u8def\u8f6c\u63a5epoll\u673a\u5236\u3001ET\u6a21\u5f0f\uff0c\u57fa\u4e8eReactor\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-15T07:31:54+00:00","dateModified":"2025-06-15T07:31:54+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/43850.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/43850.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/43850.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Linux\u3011\u5229\u7528\u591a\u8def\u8f6c\u63a5epoll\u673a\u5236\u3001ET\u6a21\u5f0f\uff0c\u57fa\u4e8eReactor\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/43850","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=43850"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/43850\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/43830"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=43850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=43850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=43850"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=43850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}