{"id":13626,"date":"2025-04-18T20:32:49","date_gmt":"2025-04-18T12:32:49","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/13626.html"},"modified":"2025-04-18T20:32:49","modified_gmt":"2025-04-18T12:32:49","slug":"muduo%e7%bd%91%e7%bb%9c%e5%ba%93%e5%ae%9e%e7%8e%b0-%e4%b8%83-connection%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/13626.html","title":{"rendered":"Muduo\u7f51\u7edc\u5e93\u5b9e\u73b0 [\u4e03] - Connection\u6a21\u5757"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E8%AE%BE%E8%AE%A1%E6%80%9D%E8%B7%AF-toc\" style=\"margin-left:0px\">\u8bbe\u8ba1\u601d\u8def<\/p>\n<p id=\"%E7%B1%BB%E7%9A%84%E8%AE%BE%E8%AE%A1-toc\" style=\"margin-left:0px\">\u7c7b\u7684\u8bbe\u8ba1<\/p>\n<p id=\"%E6%A8%A1%E5%9D%97%E5%AE%9E%E7%8E%B0-toc\" style=\"margin-left:0px\">\u6a21\u5757\u5b9e\u73b0<\/p>\n<p id=\"%E8%BF%9E%E6%8E%A5%E5%89%8D%E7%9A%84%E5%87%86%E5%A4%87%E9%98%B6%E6%AE%B5-toc\" style=\"margin-left:40px\">\u8fde\u63a5\u524d\u7684\u51c6\u5907\u9636\u6bb5<\/p>\n<p id=\"%E8%BF%9E%E6%8E%A5%E9%80%9A%E4%BF%A1%E9%98%B6%E6%AE%B5%C2%A0-toc\" style=\"margin-left:40px\">\u8fde\u63a5\u901a\u4fe1\u9636\u6bb5\u00a0<\/p>\n<p id=\"%E7%96%91%E6%83%91%E7%82%B9-toc\" style=\"margin-left:0px\">\u7591\u60d1\u70b9<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 id=\"%E8%AE%BE%E8%AE%A1%E6%80%9D%E8%B7%AF\">\u8bbe\u8ba1\u601d\u8def<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"701\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123244-6802466c69426.png\" width=\"1382\" \/><\/p>\n<p>Connection\u6a21\u5757\u662f\u5bf9\u901a\u4fe1\u8fde\u63a5\u4e5f\u5c31\u662f\u901a\u4fe1\u5957\u63a5\u5b57\u7684\u6574\u4f53\u7684\u7ba1\u7406\u6a21\u5757&#xff0c;\u5bf9\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u662f\u901a\u8fc7\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u7684\u63a5\u53e3\u6765\u5b8c\u6210\u7684\u3002<\/p>\n<p><span style=\"color:null\">\u5177\u4f53\u8981\u8fdb\u884c\u54ea\u4e9b\u65b9\u9762\u7684\u7ba1\u7406\u5462&#xff1f;<\/span><\/p>\n<p>\u5bf9\u4e8e\u7ba1\u7406\u901a\u4fe1\u5957\u63a5\u5b57\u6765\u8bf4&#xff0c;\u6211\u4eec\u6b64\u65f6\u5df2\u7ecf\u8fde\u63a5\u4e0a\u4e86\u5ba2\u6237\u7aef&#xff0c;<span style=\"color:#ad720d\">\u5f53\u5ba2\u6237\u7aef\u53d1\u6765\u6d88\u606f\u7684\u65f6\u5019&#xff0c;\u6211\u4eec\u65e0\u6cd5\u4fdd\u8bc1\u53d1\u6765\u7684\u6d88\u606f\u662f\u5b8c\u6574\u7684&#xff0c;<\/span>\u6240\u4ee5\u6b64\u65f6\u9700\u8981\u6709\u4e2a\u7f13\u51b2\u533a&#xff0c;\u901a\u8fc7send\u628a\u6570\u636e\u4ece\u7f51\u7edc\u5199\u5165\u5230\u8f93\u51fa\u7f13\u51b2\u533a\u4e2d&#xff0c;\u7136\u540e\u542f\u52a8\u5199\u4e8b\u4ef6\u76d1\u542c&#xff0c;\u5f53\u5185\u6838\u544a\u8bc9\u6211\u4eec&#034;\u4f60\u53ef\u4ee5\u628a\u6570\u636e\u5199\u8fdb\u6765\u4e86&#034;&#xff0c;\u6211\u4eec\u5c31\u628a\u6570\u636e\u53d1\u9001\u51fa\u53bb<\/p>\n<p>\u7f13\u51b2\u533a\u7ba1\u7406<\/p>\n<ul>\n<li>\u7ef4\u62a4\u4e24\u4e2aBuffer\u5bf9\u8c61&#xff1a;\u8f93\u5165\u7f13\u51b2\u533a\u548c\u8f93\u51fa\u7f13\u51b2\u533a<\/li>\n<li>\u63d0\u4f9bSend\u63a5\u53e3&#xff1a;\u5c06\u6570\u636e\u5199\u5165\u8f93\u51fa\u7f13\u51b2\u533a&#xff0c;\u542f\u52a8\u5199\u4e8b\u4ef6\u76d1\u542c<\/li>\n<li>\u5f53\u5199\u4e8b\u4ef6\u89e6\u53d1\u65f6&#xff0c;\u901a\u8fc7Channel\u56de\u8c03\u5904\u7406\u5b9e\u9645\u6570\u636e\u53d1\u9001<\/li>\n<\/ul>\n<p>\u5176\u6b21&#xff0c;\u8fd8\u4e5f\u9700\u8981\u5bf9\u6bcf\u4e2a\u901a\u4fe1\u5957\u63a5\u5b57\u672c\u8eab\u505a\u7ba1\u7406&#xff0c;\u80fd\u591f\u5b8c\u6210\u5957\u63a5\u5b57\u7684\u79cd\u79cd\u64cd\u4f5c&#xff0c;\u5176\u5b9e\u5c31\u662f\u5185\u90e8\u9700\u8981\u5305\u542b\u4e00\u4e2aSocket\u5bf9\u8c61&#xff0c;\u672a\u6765\u5173\u95ed\u5957\u63a5\u5b57\u65f6\u80fd\u591f\u76f4\u63a5\u8c03\u7528Socket\u7684\u63a5\u53e3<\/p>\n<p>\u00a0\u5957\u63a5\u5b57\u7ba1\u7406<\/p>\n<ul>\n<li>\u5185\u90e8\u5305\u542bSocket\u5bf9\u8c61<\/li>\n<li>\u5c01\u88c5\u5957\u63a5\u5b57\u7684\u57fa\u672c\u64cd\u4f5c<\/li>\n<li>\u63d0\u4f9b\u5173\u95ed\u5957\u63a5\u5b57\u7684\u63a5\u53e3&#xff0c;\u5728\u9700\u8981\u65f6\u76f4\u63a5\u8c03\u7528Socket\u5bf9\u8c61\u7684\u63a5\u53e3<\/li>\n<\/ul>\n<p>\u6211\u4eec\u4e0a\u9762\u8bf4\u4e86&#xff0c;\u5f53\u6570\u636e\u53d1\u9001\u5230\u7528\u6237\u7684\u8f93\u51fa\u7f13\u51b2\u533a\u4e4b\u540e&#xff0c;\u9700\u8981\u542f\u52a8\u5199\u4e8b\u4ef6\u76d1\u542c&#xff0c;\u800c\u8fd9\u4e2a\u76d1\u542c\u5c31\u9700\u8981Channel\u6a21\u5757\u6765\u5b9e\u73b0\u4e86&#xff0c;\u6240\u4ee5\u6211\u4eec\u8981\u8bbe\u8ba1\u4e2a\u4e8b\u4ef6\u76d1\u542c\u7ba1\u7406\u8fdb\u884c\u628a\u5404\u79cd\u4e8b\u4ef6\u90fd\u5c01\u88c5\u8fdb\u6765<\/p>\n<p>\u4e8b\u4ef6\u76d1\u542c\u7ba1\u7406<\/p>\n<ul>\n<li>\u5185\u90e8\u5305\u542bChannel\u5bf9\u8c61<\/li>\n<li>\u63d0\u4f9b\u8bbe\u7f6e\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570\u7684\u63a5\u53e3<\/li>\n<li>\u63d0\u4f9b\u8bbe\u7f6e\u9700\u8981\u76d1\u63a7\u4e8b\u4ef6\u7684\u63a5\u53e3<\/li>\n<li>\u5173\u8054\u4e8b\u4ef6\u89e6\u53d1\u4e0e\u56de\u8c03\u5904\u7406\u7684\u673a\u5236<\/li>\n<\/ul>\n<p>\u8fd8\u9700\u8981\u4fdd\u5b58\u534f\u8bae\u89e3\u6790\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f&#xff0c;\u4e5f\u5c31\u662f\u4e00\u4e2aAny\u5bf9\u8c61&#xff0c;\u672a\u6765\u8981\u63d0\u4f9b\u63a5\u53e3\u7528\u4e8e\u83b7\u53d6\u5185\u90e8\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u7684\u6307\u9488\u3002<\/p>\n<p>\u534f\u8bae\u4e0a\u4e0b\u6587\u7ba1\u7406<\/p>\n<ul>\n<li>\u4fdd\u5b58\u534f\u8bae\u89e3\u6790\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f(Any\u7c7b\u578b)<\/li>\n<li>\u63d0\u4f9b\u83b7\u53d6\u4e0a\u4e0b\u6587\u4fe1\u606f\u6307\u9488\u7684\u63a5\u53e3<\/li>\n<li>\u652f\u6301\u534f\u8bae\u5207\u6362\u548c\u72b6\u6001\u4fdd\u5b58<\/li>\n<\/ul>\n<p>\u5f53\u6211\u4eec\u7684\u8fde\u63a5\u5efa\u7acb\u6210\u529f\u4e4b\u540e&#xff0c;\u8be5\u5982\u4f55\u5904\u7406&#xff0c;\u8fde\u63a5\u5173\u95ed\u4e4b\u540e&#xff0c;\u8be5\u5982\u4f55\u5904\u7406&#xff0c;\u4ee5\u53ca\u5bf9\u4e8e\u4efb\u610f\u4e8b\u4ef6\u7684\u4ea7\u751f&#xff0c;\u53c8\u8be5\u5982\u4f55\u5904\u7406\u5462&#xff1f;\u6240\u4ee5\u6211\u4eec\u80af\u5b9a\u4e5f\u9700\u8981\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570<\/p>\n<p>\u00a0\u56de\u8c03\u51fd\u6570\u7ba1\u7406<\/p>\n<ul>\n<li>\u5b58\u50a8\u7528\u6237\u901a\u8fc7TcpServer\u8bbe\u7f6e\u7684\u56de\u8c03\u51fd\u6570<\/li>\n<li>\u5728\u8fd9\u4e9b\u56de\u8c03\u57fa\u7840\u4e0a\u589e\u52a0Connection\u81ea\u8eab\u7684\u5904\u7406\u903b\u8f91<\/li>\n<li>\u5c06\u6700\u7ec8\u7684\u56de\u8c03\u51fd\u6570\u8bbe\u7f6e\u5230Channel\u6a21\u5757&#xff0c;\u7528\u4e8e\u4e8b\u4ef6\u89e6\u53d1\u540e\u7684\u5904\u7406<\/li>\n<\/ul>\n<p><span style=\"color:#fe2c24\">\u8fd9\u79cd\u8bbe\u8ba1\u601d\u8def\u4f53\u73b0\u4e86\u6a21\u5757\u5316\u548c\u5c42\u6b21\u5316\u7684\u7f51\u7edc\u7f16\u7a0b\u67b6\u6784&#xff1a;<\/span><\/p>\n<p>Connection\u4f5c\u4e3a\u4e2d\u95f4\u5c42&#xff0c;<span style=\"color:#956fe7\">\u5411\u4e0b\u7ba1\u7406Socket\u548cChannel\u3002<\/span><span style=\"color:#4da8ee\">\u5411\u4e0a\u4e3aTcpServer\u63d0\u4f9b\u63a5\u53e3&#xff0c;<\/span>\u95f4\u63a5\u670d\u52a1\u4e8e\u7528\u6237&#xff0c;\u901a\u8fc7\u7f13\u51b2\u533a\u548c\u4e8b\u4ef6\u673a\u5236\u5b9e\u73b0\u5f02\u6b65I\/O\u5904\u7406&#xff0c;\u901a\u8fc7\u4e0a\u4e0b\u6587\u548c\u56de\u8c03\u51fd\u6570\u5b9e\u73b0\u7075\u6d3b\u7684\u534f\u8bae\u5904\u7406<\/p>\n<p>\u8fd9\u65f6\u5019\u6211\u4eec\u5c31\u9700\u8981\u8003\u8651\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u95ee\u9898&#xff1a; Connection \u5bf9\u8c61\u5982\u4f55\u7ba1\u7406&#xff1f;\u00a0<\/p>\n<p>\u9996\u5148&#xff0c;<span style=\"color:#fe2c24\">\u6240\u6709\u7684Connection \u5bf9\u8c61\u80af\u5b9a\u662f\u8981\u4ea4\u7ed9TcpServer \u6a21\u5757\u8fdb\u884c\u6574\u4f53\u7684\u7ba1\u7406\u7684&#xff0c;<\/span>\u8fd9\u4e2a\u6211\u4eec\u8fd8\u662f\u5f88\u597d\u7406\u89e3&#xff0c;\u56e0\u4e3a\u672a\u6765\u6211\u4eec\u7684Acceptor\u83b7\u53d6\u5230\u65b0\u8fde\u63a5\u4e4b\u540e&#xff0c;\u9700\u8981\u521b\u5efaConnection\u5bf9\u8c61&#xff0c;\u800c\u521b\u5efaConnection\u5bf9\u8c61\u7684\u63a5\u53e3\u5c31\u53ea\u80fd\u662fTcpServer\u6a21\u5757\u63d0\u4f9b\u7ed9\u4ed6\u7684&#xff0c;\u4e5f\u5c31\u662f\u7531TcpServer\u6765\u521b\u5efa&#xff0c;\u521b\u5efa\u4e4b\u540e&#xff0c;\u4ed6\u4e5f\u9700\u8981\u5bf9\u6240\u6709\u7684Connection\u8fdb\u884c\u7ba1\u7406\u3002\u00a0<\/p>\n<p>\u90a3\u4e48\u600e\u4e48\u7ba1\u7406\u5462&#xff1f;new\u4e00\u4e2aConnection\u5bf9\u8c61\u7136\u540e\u4f7f\u7528\u6307\u9488\u8fdb\u884c\u7ba1\u7406&#xff1f;<span style=\"color:#be191c\">\u90a3\u4e48\u4f1a\u4e0d\u4f1a\u6709\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5:<\/span><\/p>\n<p>\u60f3\u8c61\u4f60\u5728\u4e00\u4e2a\u7f51\u7edc\u804a\u5929\u5ba4\u91cc&#xff0c;\u6709\u8fd9\u6837\u4e00\u4e2a\u8fc7\u7a0b&#xff1a;<\/p>\n<li>\u5c0f\u660e\u7684\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u5efa\u7acb\u4e86\u8fde\u63a5<\/li>\n<li>\u4ed6\u53d1\u9001\u4e86\u4e00\u6761\u6d88\u606f<\/li>\n<li>\u5728\u6d88\u606f\u53d1\u9001\u7684\u8fc7\u7a0b\u4e2d&#xff0c;\u7f51\u7edc\u7a81\u7136\u65ad\u5f00<\/li>\n<li>\u8fd9\u65f6\u670d\u52a1\u5668\u6b63\u51c6\u5907\u5904\u7406\u8fd9\u6761\u6d88\u606f<\/li>\n<p>\u5728\u8fd9\u4e2a\u573a\u666f\u4e2d&#xff0c;\u8fde\u63a5\u5df2\u7ecf\u5173\u95ed&#xff0c;\u4f46\u662f\u540e\u7eed\u7684\u64cd\u4f5c\u8fd8\u5728\u5c1d\u8bd5\u4f7f\u7528\u8fd9\u4e2a\u5df2\u7ecf\u65e0\u6548\u7684\u8fde\u63a5\u3002<\/p>\n<p><span style=\"color:#956fe7\">\u8fde\u63a5\u6709\u591a\u79cd\u64cd\u4f5c&#xff0c;\u5982\u679c\u5176\u4e2d\u4e00\u4e2a\u64cd\u4f5c\u628a\u8fde\u63a5\u5173\u95ed\u91ca\u653e\u4e86&#xff0c;\u540e\u7eed\u7684\u64cd\u4f5c\u518d\u8c03\u7528Conenction\u7684\u63a5\u53e3\u4e0d\u5c31\u662f\u91ce\u6307\u9488\u8bbf\u95ee\u4e86\u5417&#xff1f;<\/span>\u90a3\u4e48\u7a0b\u5e8f\u4f1a\u89e6\u53d1\u786c\u4ef6\u5f02\u5e38\u800c\u5d29\u6e83\u3002\u8fd9\u662f\u6211\u4eec\u4e0d\u5e0c\u671b\u770b\u5230\u7684\u3002\u5373\u4fbf TcpServer\u4e2d\u53ef\u80fd\u8fd8\u4fdd\u5b58\u7740\u8fd9\u4e2a\u5bf9\u8c61\u7684\u6307\u9488&#xff0c;\u4f46\u662f\u6307\u9488\u6307\u5411\u7684\u8d44\u6e90\u5374\u53ef\u80fd\u5df2\u7ecf\u91ca\u653e\u4e86\u3002<\/p>\n<p><span style=\"color:#ad720d\">\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u7528\u5230\u667a\u80fd\u6307\u9488share_ptr \u6765\u5bf9Connction \u8fdb\u884c\u7ba1\u7406\u3002<\/span><span style=\"color:null\">\u4f7f\u7528\u53ea\u80fd\u6307\u9488\u6709\u54ea\u4e9b\u4f18\u52bf\u5462&#xff1f;\u6211\u4eec\u63a5\u7740\u770b<\/span><\/p>\n<p><span style=\"color:#4da8ee\">\u8d44\u6e90\u7ba1\u7406\u673a\u5236<\/span><\/p>\n<ul>\n<li>\u4f7f\u7528 share_ptr\u00a0\u667a\u80fd\u6307\u9488\u7ba1\u7406 Connection \u5bf9\u8c61<\/li>\n<li>\u901a\u8fc7\u5f15\u7528\u8ba1\u6570\u7684\u65b9\u5f0f\u63a7\u5236\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f<\/li>\n<\/ul>\n<p><span style=\"color:#4da8ee\">\u5b89\u5168\u8bbf\u95ee\u539f\u7406<\/span><\/p>\n<ul>\n<li>\u5f53\u4e00\u4e2a\u51fd\u6570\u63a5\u6536 Connection \u7684 share_ptr\u00a0 \u4f5c\u4e3a\u53c2\u6570\u65f6<\/li>\n<li>\u8be5\u51fd\u6570\u6808\u5e27\u4f1a\u589e\u52a0 Connection \u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570<\/li>\n<li>\u53ea\u8981\u51fd\u6570\u8fd8\u5728\u6267\u884c&#xff0c;Connection \u5bf9\u8c61\u5c31\u4e0d\u4f1a\u88ab\u771f\u6b63\u91ca\u653e<\/li>\n<\/ul>\n<p><span style=\"color:#4da8ee\">\u751f\u547d\u5468\u671f\u4fdd\u8bc1<\/span><\/p>\n<ul>\n<li>\u5373\u4f7f\u5176\u4ed6\u64cd\u4f5c\u5c1d\u8bd5\u91ca\u653e\u8fde\u63a5<\/li>\n<li>\u53ea\u4f1a\u51cf\u5c11\u5f15\u7528\u8ba1\u6570&#xff0c;\u4e0d\u4f1a\u7acb\u5373\u9500\u6bc1\u5bf9\u8c61<\/li>\n<li>\u786e\u4fdd\u6b63\u5728\u4f7f\u7528\u7684 Connection \u8d44\u6e90\u4e0d\u4f1a\u88ab\u610f\u5916\u9500\u6bc1<\/li>\n<\/ul>\n<p><span style=\"color:#4da8ee\">\u591a\u91cd\u4fdd\u62a4\u673a\u5236<\/span><\/p>\n<ul>\n<li>TcpServer \u7ba1\u7406\u7684\u57fa\u7840 share_ptr\u00a0 \u53ef\u80fd\u88ab\u91ca\u653e<\/li>\n<li>\u4f46\u53ea\u8981\u8fd8\u6709\u5176\u4ed6 share_ptr\u00a0 \u6301\u6709\u5bf9\u8c61<\/li>\n<li>Connection \u7684\u5b9e\u9645\u8d44\u6e90\u4e0d\u4f1a\u88ab\u9500\u6bc1<\/li>\n<\/ul>\n<p><span style=\"color:#4da8ee\">\u9632\u6b62\u91ce\u6307\u9488\u7684\u5173\u952e<\/span><\/p>\n<ul>\n<li>\u5f15\u7528\u8ba1\u6570\u4e3a\u96f6\u65f6\u624d\u771f\u6b63\u91ca\u653e\u5bf9\u8c61<\/li>\n<li>\u907f\u514d\u60ac\u7a7a\u6307\u9488\u548c\u975e\u6cd5\u5185\u5b58\u8bbf\u95ee<\/li>\n<li>\u63d0\u4f9b\u4e86\u4e00\u79cd\u7ebf\u7a0b\u5b89\u5168\u7684\u8d44\u6e90\u7ba1\u7406\u65b9\u5f0f<\/li>\n<\/ul>\n<p>\u57fa\u4e8e\u4f7f\u7528sharet_ptr\u6765\u7ba1\u7406Connection\u7684\u601d\u60f3&#xff0c;\u6211\u4eec\u672a\u6765\u8bbe\u7f6e\u63a5\u53e3\u7684\u65f6\u5019&#xff0c;\u5c31\u9700\u8981\u4f20\u9012Connection \u7684\u667a\u80fd\u6307\u9488\u5bf9\u8c61\u6216\u8005\u5f15\u7528\u4e86&#xff0c;\u800c\u4e0d\u662f\u76f4\u63a5\u4f20\u9012\u539f\u59cb\u6307\u9488\u3002<\/p>\n<p>Connection\u7c7b\u8be5\u5982\u4f55\u8bbe\u8ba1&#xff1f;<\/p>\n<p>\u6bcf\u4e00\u4e2a\u8fde\u63a5\u90fd\u9700\u8981\u6709\u4e00\u4e2a\u5728\u670d\u52a1\u5668\u5185\u90e8\u7684\u552f\u4e00\u6807\u8bc6&#xff0c;\u4e5f\u5c31\u662fid&#xff0c;\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u4f7f\u7528 fd &#xff1f;\u56e0\u4e3a\u6211\u4eec\u5bf9fd \u7684\u7ba1\u7406\u4e5f\u4e0d\u662f\u76f4\u63a5\u4f7f\u7528\u539f\u59cb\u7684fd \u7684&#xff0c;\u800c\u662f\u4f7f\u7528Socket\u6765\u7ba1\u7406&#xff0c;\u6211\u4eec\u9700\u8981\u5c06\u5176\u4e0e\u7cfb\u7edf\u7684IO\u7684\u90e8\u5206\u8fdb\u884c\u89e3\u8026\u3002 \u540c\u65f6&#xff0c;\u672a\u6765\u8fd9\u4e2a\u8fde\u63a5\u7684id\u4e5f\u662f\u5979\u6240\u5bf9\u5e94\u7684\u5b9a\u65f6\u4efb\u52a1\u7684id\u3002<\/p>\n<p>\u5176\u6b21&#xff0c;\u5b83\u9700\u8981\u4e00\u4e2aChannel\u5bf9\u8c61\u3001\u4e00\u4e2aSocket\u5bf9\u8c61&#xff0c;\u4e24\u4e2aBuffer\u5bf9\u8c61\u4ee5\u53ca\u4e00\u4e2aAny\u5bf9\u8c61<\/p>\n<p>\u7136\u540e&#xff0c;\u4ed6\u9700\u8981\u4fdd\u5b58\u56db\u4e2a\u56de\u8c03\u51fd\u6570&#xff0c;\u8fd9\u56db\u4e2a\u56de\u8c03\u51fd\u6570\u662f\u7531\u7528\u6237\u6765\u8fdb\u884c\u8bbe\u7f6e\u7684&#xff0c;\u5206\u522b\u662f\u8fde\u63a5\u5efa\u7acb\u65f6\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u65b0\u6570\u636e\u5230\u6765\u65f6\u6267\u884c\u7684\u56de\u8c03\u65b9\u6cd5&#xff0c;\u8fde\u63a5\u5173\u95ed\u65f6\u6267\u884c\u7684\u56de\u8c03\u65b9\u6cd5\u4ee5\u53ca\u4efb\u610f\u4e8b\u4ef6\u5230\u6765\u6267\u884c\u7684\u56de\u8c03\u65b9\u6cd5\u3002<\/p>\n<p>\u7531\u4e8eConnection\u6d89\u53ca\u5230\u8d85\u65f6\u7684\u7ba1\u7406&#xff0c;\u90a3\u4e48\u6211\u4eec\u8fd8\u9700\u8981\u4e00\u4e2a\u503c\u6765\u8868\u793a\u662f\u5426\u542f\u52a8\u4e86\u8d85\u65f6\u7ba1\u7406\u3002<\/p>\n<p>\u4ee5\u53ca\u6bcf\u4e00\u4e2aCconnection\u5bf9\u8c61\u9700\u8981\u548c\u4e00\u4e2aEventLoop\u5bf9\u8c61\u7ed1\u5b9a&#xff0c;\u6240\u4ee5\u4ed6\u8fd8\u9700\u8981\u4e00\u4e2aEventLoop\u7684\u6307\u9488\u3002<\/p>\n<p>\u8fd9\u662f\u51e0\u4e2a\u7b80\u5355\u7684\u6210\u5458&#xff0c;\u6211\u4eec\u8fd8\u9700\u8981\u4e00\u4e2a\u6210\u5458\u5c31\u662f\u8fde\u63a5\u6240\u5904\u7684\u72b6\u6001\u3002<\/p>\n<p>\u00a0\u8fd9\u4e2a\u72b6\u6001\u5e76\u4e0d\u662f\u7ad9\u5728\u5185\u6838\u5c42\u9762\u7684\u72b6\u6001&#xff0c;\u800c\u662f\u7ad9\u5728\u5e94\u7528\u5c42\u89d2\u5ea6\u7684\u72b6\u6001\u3002<\/p>\n<p>\u72b6\u6001\u6709\u4ee5\u4e0b\u51e0\u79cd&#xff1a;<\/p>\n<ul>\n<li>\u8fde\u63a5\u5efa\u7acb\u4e2d&#xff1a; \u6b64\u65f6\u6211\u4eec\u5df2\u7ecf\u4ece\u5e95\u5c42\u5c06\u8fde\u63a5\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u83b7\u53d6\u4e0a\u6765&#xff0c;\u5e76\u4e3a\u5176\u521b\u5efa\u4e86Connection\u5bf9\u8c61&#xff0c;\u4f46\u662fConnection\u5185\u90e8\u7684\u5404\u9879\u8bbe\u7f6e\u8fd8\u672a\u5b8c\u6210<\/li>\n<li>\u8fde\u63a5\u5efa\u7acb\u5b8c\u6210&#xff1a;Connection\u5bf9\u8c61\u7684\u5404\u9879\u8bbe\u7f6e\u5df2\u7ecf\u5b8c\u6210&#xff0c;\u53ef\u4ee5\u8fdb\u884c\u901a\u4fe1\u4e86<\/li>\n<li>\u8fde\u63a5\u5f85\u5173\u95ed\u72b6\u6001&#xff1a;\u5bf9\u7aef\u6216\u8005\u672c\u7aef\u9700\u8981\u5173\u95ed\u8fd9\u4e2a\u8fde\u63a5&#xff0c;\u4f46\u662f\u5728\u5b9e\u9645\u5173\u95ed\u8fde\u63a5\u4e4b\u524d\u6211\u4eec\u8fd8\u9700\u8981\u628a\u7f13\u51b2\u533a\u7684\u6570\u636e\u5168\u90e8\u5904\u7406\u5b8c<\/li>\n<li>\u8fde\u63a5\u5173\u95ed\u72b6\u6001&#xff0c;\u5904\u4e8e\u8fd9\u4e2a\u72b6\u6001\u7684\u65f6\u5019&#xff0c;\u5c31\u53ef\u4ee5\u628a\u5957\u63a5\u5b57\u5173\u95ed&#xff0c;\u4ee5\u53ca\u8d44\u6e90\u91ca\u653e\u4e86<\/li>\n<\/ul>\n<p>\u90a3\u4e48\u8fd9\u4e2a\u72b6\u6001\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u679a\u4e3e\u6765\u5b9a\u4e49\u3002<\/p>\n<p>enum CONN_STATU<br \/>\n{<br \/>\n    CLOSED,    \/\/\u5173\u95ed\u72b6\u6001&#xff0c;\u4e0d\u518d\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c&#xff0c;\u91ca\u653e\u8d44\u6e90<br \/>\n    CONNECTING,  \/\/\u8fde\u63a5\u5e26\u5904\u7406&#xff0c;\u8fd8\u9700\u8981\u8fdb\u884c\u5404\u9879\u8bbe\u7f6e\u624d\u80fd\u5f00\u59cb\u901a\u4fe1<br \/>\n    CONNECTED,   \/\/\u8fde\u63a5\u5efa\u7acb&#xff0c;\u53ef\u4ee5\u901a\u4fe1<br \/>\n    CLOSING  \/\/\u8fde\u63a5\u5e26\u5173\u95ed&#xff0c;\u5c3d\u5feb\u5904\u7406\u7f13\u51b2\u533a\u5269\u4f59\u6570\u636e<br \/>\n}; <\/p>\n<h2 id=\"%E7%B1%BB%E7%9A%84%E8%AE%BE%E8%AE%A1\">\u7c7b\u7684\u8bbe\u8ba1<\/h2>\n<p>\/\/ \u524d\u5411\u58f0\u660e<br \/>\nclass Buffer;<br \/>\nclass Channel;<br \/>\nclass Socket;<br \/>\nclass EventLoop;<\/p>\n<p>\/\/ \u8fde\u63a5\u72b6\u6001\u679a\u4e3e<br \/>\ntypedef enum<br \/>\n{<br \/>\n    CLOSED,     \/\/ \u5173\u95ed\u72b6\u6001&#xff0c;\u4e0d\u518d\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c&#xff0c;\u91ca\u653e\u8d44\u6e90<br \/>\n    CONNECTING, \/\/ \u8fde\u63a5\u5f85\u5904\u7406&#xff0c;\u8fd8\u9700\u8981\u8fdb\u884c\u5404\u9879\u8bbe\u7f6e\u624d\u80fd\u5f00\u59cb\u901a\u4fe1<br \/>\n    CONNECTED,  \/\/ \u8fde\u63a5\u5efa\u7acb&#xff0c;\u53ef\u4ee5\u901a\u4fe1<br \/>\n    CLOSING     \/\/ \u8fde\u63a5\u5f85\u5173\u95ed&#xff0c;\u5c3d\u5feb\u5904\u7406\u7f13\u51b2\u533a\u5269\u4f59\u6570\u636e<br \/>\n} ConnStatus;<\/p>\n<p>\/\/ Connection \u5171\u4eab\u6307\u9488\u7c7b\u578b<br \/>\nusing PtrConnection &#061; shared_ptr&lt;Connection&gt;;<\/p>\n<p>class Connection : public enable_shared_from_this&lt;Connection&gt;<br \/>\n{<br \/>\nprivate:<br \/>\n    \/\/ \u6210\u5458\u53d8\u91cf<br \/>\n    uint64_t _connid;           \/\/ \u8fde\u63a5\u7684\u552f\u4e00id&#xff0c;\u4e5f\u662f\u5b9a\u65f6\u4efb\u52a1\u7684timeid<br \/>\n    Buffer _in_buffer;          \/\/ \u7528\u6237\u6001\u7684\u8f93\u5165\u7f13\u51b2\u533a<br \/>\n    Buffer _out_buffer;         \/\/ \u7528\u6237\u6001\u7684\u8f93\u51fa\u7f13\u51b2\u533a<br \/>\n    Socket _socket;             \/\/ \u5957\u63a5\u5b57\u7684\u7ba1\u7406<br \/>\n    Channel _channel;           \/\/ \u8fde\u63a5\u7684\u4e8b\u4ef6\u7ba1\u7406<br \/>\n    EventLoop *_loop;           \/\/ \u8fde\u63a5\u6240\u5173\u8054\u7684loop,\u51fd\u6570\u6709RunInLoop\u8fdb\u884c\u7ebf\u7a0b\u5b89\u5168\u64cd\u4f5c<br \/>\n    bool _inactive_release;     \/\/ \u662f\u5426\u5f00\u542f\u8d85\u65f6\u8fde\u63a5\u9500\u6bc1&#xff0c;\u9ed8\u8ba4\u4e3afalse<br \/>\n    Any _context;               \/\/ \u8bf7\u6c42\u63a5\u6536\u5904\u7406\u7684\u4e0a\u4e0b\u6587<br \/>\n    ConnStatus _status;         \/\/ \u8fde\u63a5\u72b6\u6001<br \/>\n    int _sockfd;                \/\/ \u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26<\/p>\n<p>    \/\/ \u56de\u8c03\u51fd\u6570\u7c7b\u578b\u5b9a\u4e49<br \/>\n    using ConnectedCallback &#061; function&lt;void(const PtrConnection &amp;)&gt;;<br \/>\n    using MessageCallback &#061; function&lt;void(const PtrConnection &amp;, Buffer *)&gt;;<br \/>\n    using CloseCallback &#061; function&lt;void(const PtrConnection &amp;)&gt;;<br \/>\n    using EventCallback &#061; function&lt;void(const PtrConnection &amp;)&gt;;<\/p>\n<p>    \/\/ \u56de\u8c03\u51fd\u6570\u6210\u5458<br \/>\n    ConnectedCallback _connected_callback;  \/\/ \u8fde\u63a5\u5efa\u7acb\u540e\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    MessageCallback _message_callback;      \/\/ \u6d88\u606f\u5904\u7406\u7684\u56de\u8c03\u51fd\u6570,\u4e5f\u5c31\u662f\u7528\u6765\u5904\u7406\u63a5\u6536\u5230\u7684\u6570\u636e&#xff0c;\u5e76\u4e0d\u662f\u8981\u5904\u7406\u53d1\u9001\u51fa\u53bb\u7684\u6570\u636e<br \/>\n    CloseCallback _close_callback;          \/\/ \u5173\u95ed\u8fde\u63a5\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    EventCallback _event_callback;          \/\/ \u4efb\u610f\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    CloseCallback _svrclose_callback;       \/\/ \u670d\u52a1\u5668\u5173\u95ed\u56de\u8c03\u51fd\u6570<\/p>\n<p>private:<br \/>\n    \/\/ \u79c1\u6709\u6210\u5458\u51fd\u6570 &#8211; Channel \u4e8b\u4ef6\u5904\u7406\u5668<br \/>\n    void HanderRead();      \/\/ \u5904\u7406\u8bfb\u4e8b\u4ef6<br \/>\n    void HanderWrite();     \/\/ \u5904\u7406\u5199\u4e8b\u4ef6<br \/>\n    void HandlerError();    \/\/ \u5904\u7406\u9519\u8bef\u4e8b\u4ef6<br \/>\n    void HandlerClose();    \/\/ \u5904\u7406\u5173\u95ed\u4e8b\u4ef6<br \/>\n    void HandlerEvent();    \/\/ \u5904\u7406\u901a\u7528\u4e8b\u4ef6<\/p>\n<p>    \/\/ \u79c1\u6709\u6210\u5458\u51fd\u6570 &#8211; \u4e8b\u4ef6\u5faa\u73af\u4e2d\u7684\u64cd\u4f5c<br \/>\n    void EstablishInLoop();                 \/\/ \u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u5efa\u7acb\u8fde\u63a5<br \/>\n    void SendInLoop(Buffer &amp;buf);           \/\/ \u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u53d1\u9001\u6570\u636e<br \/>\n    void ShutdownInLoop();                  \/\/ \u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u5173\u95ed\u8fde\u63a5<br \/>\n    void EnableInactiveReleaseInLoop(int seconds);  \/\/ \u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u542f\u7528\u8d85\u65f6\u91ca\u653e<br \/>\n    void CancelInactiveReleaseInLoop();     \/\/ \u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u53d6\u6d88\u8d85\u65f6\u91ca\u653e<br \/>\n    void ReleaseInLoop();                   \/\/ \u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u91ca\u653e\u8fde\u63a5<br \/>\n    void UpgradeInLoop(const Any &amp;context,  \/\/ \u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u5347\u7ea7\u8fde\u63a5\u914d\u7f6e<br \/>\n                       const ConnectedCallback &amp;connected_callback,<br \/>\n                       const MessageCallback &amp;message_callback,<br \/>\n                       const CloseCallback &amp;close_callback,<br \/>\n                       const EventCallback &amp;event_callback);<\/p>\n<p>public:<br \/>\n    \/\/ \u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570<br \/>\n    Connection(EventLoop *loop, uint64_t connid, int sockfd);<br \/>\n    ~Connection();<\/p>\n<p>    \/\/ \u83b7\u53d6\u5668\u65b9\u6cd5<br \/>\n    int Socked();       \/\/ \u83b7\u53d6\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26<br \/>\n    int Id();           \/\/ \u83b7\u53d6\u8fde\u63a5ID<br \/>\n    bool Connected();   \/\/ \u68c0\u67e5\u662f\u5426\u5df2\u8fde\u63a5<\/p>\n<p>    \/\/ \u4e0a\u4e0b\u6587\u7ba1\u7406<br \/>\n    void SetContext(const Any &amp;context);    \/\/ \u8bbe\u7f6e\u4e0a\u4e0b\u6587<br \/>\n    Any *GetContext();                      \/\/ \u83b7\u53d6\u4e0a\u4e0b\u6587<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u56de\u8c03\u51fd\u6570<br \/>\n    void SetConnectedCallback(const ConnectedCallback &amp;cb);   \/\/ \u8bbe\u7f6e\u8fde\u63a5\u5efa\u7acb\u56de\u8c03<br \/>\n    void SetMessageCallback(const MessageCallback &amp;cb);       \/\/ \u8bbe\u7f6e\u6d88\u606f\u5904\u7406\u56de\u8c03<br \/>\n    void SetCloseCallback(const CloseCallback &amp;cb);           \/\/ \u8bbe\u7f6e\u5173\u95ed\u8fde\u63a5\u56de\u8c03<br \/>\n    void SetSvrCloseCallback(const CloseCallback &amp;cb);        \/\/ \u8bbe\u7f6e\u670d\u52a1\u5668\u5173\u95ed\u56de\u8c03<br \/>\n    void SetEventCallback(const EventCallback &amp;cb);           \/\/ \u8bbe\u7f6e\u4e8b\u4ef6\u56de\u8c03<\/p>\n<p>    \/\/ \u516c\u5171\u63a5\u53e3<br \/>\n    void Establish();                       \/\/ \u5efa\u7acb\u8fde\u63a5<br \/>\n    void Send(const char *data, size_t len);\/\/ \u53d1\u9001\u6570\u636e<br \/>\n    void ShutDown();                        \/\/ \u5173\u95ed\u8fde\u63a5<br \/>\n    void EnableInactiveRelease(int sec);    \/\/ \u542f\u7528\u8d85\u65f6\u91ca\u653e<br \/>\n    void CancelInactiveRelease();           \/\/ \u53d6\u6d88\u8d85\u65f6\u91ca\u653e<br \/>\n    void Release();                         \/\/ \u91ca\u653e\u8fde\u63a5<br \/>\n    void Upgrade(const Any &amp;context,        \/\/ \u5207\u6362\u534f\u8bae<br \/>\n                 const ConnectedCallback &amp;connected_callback,<br \/>\n                 const MessageCallback &amp;message_callback,<br \/>\n                 const CloseCallback &amp;close_callback,<br \/>\n                 const EventCallback &amp;event_callback);<br \/>\n}; <\/p>\n<h2 id=\"%E6%A8%A1%E5%9D%97%E5%AE%9E%E7%8E%B0\" style=\"background-color:transparent\">\u6a21\u5757\u5b9e\u73b0<\/h2>\n<h3 id=\"%E8%BF%9E%E6%8E%A5%E5%89%8D%E7%9A%84%E5%87%86%E5%A4%87%E9%98%B6%E6%AE%B5\" style=\"background-color:transparent\">\u8fde\u63a5\u524d\u7684\u51c6\u5907\u9636\u6bb5<\/h3>\n<p>\u9996\u5148\u662f\u6211\u4eec\u7684\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u7684\u5b9e\u73b0&#xff0c;\u4f20\u5165\u6211\u4eec\u7684\u8fde\u63a5id,\u5957\u63a5\u5b57&#xff0c;\u7136\u540e\u628a\u8d85\u65f6\u91ca\u653e\u5148\u8bbe\u7f6e\u4e3afalse&#xff0c;\u7b49\u540e\u9762\u9700\u8981\u518d\u7531\u7528\u6237\u53bb\u8bbe\u7f6e\u6210true&#xff0c;\u5c06\u72b6\u6001\u8bbe\u7f6e\u6210\u8fde\u63a5\u5f85\u5904\u7406\u7684\u72b6\u6001&#xff0c;\u56e0\u4e3a\u6211\u4eec\u5e76\u4e0d\u662f\u6784\u9020\u51fd\u6570\u4e4b\u540e\u5c31\u628a\u6240\u6709\u7684\u4e1c\u897f\u90fd\u521d\u59cb\u5316\u4e86&#xff0c;\u6bd4\u5982\u8bf4\u662f\u5426\u542f\u52a8\u8bfb\u4e8b\u4ef6\u548c\u8fde\u63a5\u540e\u7684\u56de\u8c03\u51fd\u6570\u6211\u4eec\u5c31\u9700\u8981\u521d\u59cb\u5316\u4e4b\u540e\u8fdb\u884c\u5b9e\u73b0\u7684\u3002<\/p>\n<p>public:<br \/>\n    Connection(EventLoop *loop, uint64_t connid, int sockfd)<br \/>\n        : _connid(connid)<br \/>\n        , _sockfd(sockfd)<br \/>\n        , _inactive_release(false)<br \/>\n        , _loop(loop)<br \/>\n        , _status(CONNECTING)<br \/>\n        , _socket(_sockfd)<br \/>\n        , _channel(loop, _sockfd)<br \/>\n    {<br \/>\n        _channel.SetReadCallback(bind(&amp;Connection::HanderRead,this));<br \/>\n        _channel.SetWriteCallback(bind(&amp;Connection::HanderWrite,this));<br \/>\n        _channel.SetEventCallback(bind(&amp;Connection::HanderEvent,this));<br \/>\n        _channel.SetErrorCallback(bind(&amp;Connection::HanderError,this));<br \/>\n        _channel.SetCloseCallback(bind(&amp;Connection::HanderClose,this));<br \/>\n    }<br \/>\n    ~Connection()<br \/>\n    {<br \/>\n        DBG_LOG(&#034;release connection:%p&#034;, this);<br \/>\n    } <\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u73b0\u5728\u5c31\u9700\u8981\u5b9e\u73b0\u4e94\u4e2a\u56de\u8c03\u51fd\u6570&#xff0c;<\/p>\n<p>\u9996\u5148\u8bfb\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u4ed6\u7684\u601d\u8def\u5f88\u7b80\u5355&#xff0c;\u53ea\u9700\u8981\u4ece\u5957\u63a5\u5b57\u4e2d\u8bfb\u53d6\u6570\u636e&#xff0c;\u7136\u540e\u653e\u5728\u8f93\u5165\u7f13\u51b2\u533a&#xff0c;\u518d\u6765\u8c03\u7528\u7528\u6237\u4f20\u8fdb\u6765\u7684\u65b0\u6570\u636e\u56de\u8c03\u51fd\u6570\u5c31\u884c\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"600\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123244-6802466cb5786.png\" width=\"1051\" \/><\/p>\n<p>    void HanderRead()<br \/>\n    {<br \/>\n        \/\/ \u521b\u5efa\u4e00\u4e2a64KB\u7684\u4e34\u65f6\u7f13\u51b2\u533a\u7528\u4e8e\u63a5\u6536socket\u6570\u636e<br \/>\n        char buf[65536];<\/p>\n<p>        \/\/ \u4f7f\u7528\u975e\u963b\u585e\u65b9\u5f0f\u4ecesocket\u8bfb\u53d6\u6570\u636e<br \/>\n        \/\/ RecvNonBlock\u8fd4\u56de\u5b9e\u9645\u8bfb\u53d6\u7684\u5b57\u8282\u6570&#xff0c;\u6216\u8005\u5728\u51fa\u9519\u65f6\u8fd4\u56de\u8d1f\u503c<br \/>\n        ssize_t ret &#061; _socket.RecvNonBlock(buf, 65535);<\/p>\n<p>        \/\/ \u5982\u679c\u8fd4\u56de\u503c\u5c0f\u4e8e0&#xff0c;\u8868\u793a\u53d1\u751f\u9519\u8bef&#xff08;\u5982\u8fde\u63a5\u5df2\u5173\u95ed&#xff09;<br \/>\n        if (ret &lt; 0)<br \/>\n        {<br \/>\n            \/\/ \u5173\u95ed\u8fde\u63a5\u5e76\u9000\u51fa<br \/>\n            return ShutDownInLoop();<br \/>\n        }<\/p>\n<p>        \/\/ \u5c06\u8bfb\u53d6\u7684\u6570\u636e\u5199\u5165\u8fde\u63a5\u7684\u8f93\u5165\u7f13\u51b2\u533a<br \/>\n        _in_buffer.WriteAndPush(buf, ret);<\/p>\n<p>        \/\/ \u5982\u679c\u8f93\u5165\u7f13\u51b2\u533a\u4e2d\u6709\u53ef\u8bfb\u6570\u636e<br \/>\n        if (_in_buffer.ReadAbleSize() &gt; 0)<br \/>\n        {<br \/>\n            \/\/ \u8c03\u7528\u7528\u6237\u8bbe\u7f6e\u7684\u6d88\u606f\u56de\u8c03\u51fd\u6570\u5904\u7406\u6570\u636e<br \/>\n            \/\/ shared_from_this()\u8fd4\u56de\u6307\u5411\u5f53\u524d\u5bf9\u8c61\u7684\u667a\u80fd\u6307\u9488&#xff0c;\u4fdd\u8bc1\u5728\u56de\u8c03\u671f\u95f4\u5bf9\u8c61\u4e0d\u4f1a\u88ab\u9500\u6bc1<br \/>\n            \/\/ &amp;_in_buffer\u4f20\u9012\u8f93\u5165\u7f13\u51b2\u533a\u7684\u6307\u9488&#xff0c;\u8ba9\u56de\u8c03\u51fd\u6570\u53ef\u4ee5\u8bfb\u53d6\u548c\u89e3\u6790\u6570\u636e<br \/>\n            return _message_callback(shared_from_this(), &amp;_in_buffer);<br \/>\n        }<br \/>\n    } <\/p>\n<p>\u00a0\u8fd9\u91cc\u6211\u4eec\u53ef\u80fd\u4f1a\u7591\u60d1&#xff0c;\u4e3a\u4ec0\u4e48\u8c03\u7528\u5b8c_message_cb \u4e4b\u540e\u4e0d\u6dfb\u52a0\u5199\u4e8b\u4ef6\u76d1\u63a7&#xff0c;\u5176\u5b9e\u662f\u56e0\u4e3a&#xff0c;\u672a\u6765\u7528\u6237<span style=\"color:#ad720d\">\u53ea\u80fd\u8c03\u7528\u6211\u4eecConnection\u63d0\u4f9b\u7684Send\u63a5\u53e3\u6765\u53d1\u9001\u6570\u636e&#xff0c;<\/span>\u4f46\u662fSend\u63a5\u53e3\u6211\u4eec\u4e5f\u61c2&#xff0c;\u53ea\u4f1a\u5c06\u6570\u636e\u5199\u5165\u5230\u8f93\u51fa\u7f13\u51b2\u533a\u4e2d&#xff0c;\u6211\u4eec\u5728Send\u51fd\u6570\u7684\u5b9e\u73b0\u4e2d&#xff0c;\u53ea\u6709\u5b9e\u9645\u5199\u5165\u4e86\u518d\u6765\u542f\u52a8\u5199\u4e8b\u4ef6\u7684\u76d1\u63a7\u66f4\u52a0\u5408\u7406\u3002<\/p>\n<p id=\"shared_from_this\">shared_from_this\u662f\u5e72\u561b\u7684&#xff1f;<\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u7ec6\u8282&#xff0c;\u5c31\u662f\u6211\u4eec\u524d\u9762\u58f0\u660e\u7c7b\u7684\u65f6\u5019&#xff0c;Connection \u7c7b\u7ee7\u627f\u4e86 std::enable_shared_from_this\u8fd9\u4e2a\u7c7b&#xff0c;\u8fd9\u4e2a\u7ee7\u627f\u5173\u7cfb\u662f\u4e3a\u4e86\u6211\u4eec\u80fd\u591f\u4f7f\u7528 shared_from_this\u8fd9\u4e2a\u529f\u80fd\u3002<\/p>\n<p>\u8fd9\u4e2a\u7528\u6cd5\u53eb\u505a\u81ea\u6211\u5f15\u7528\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981\u5f15\u8fdb\u4e00\u4e2ashared_from_this\u8fd9\u6837\u7684\u63a5\u53e3\u5462&#xff1f;\u00a0<\/p>\n<p>\u6211\u4eec\u8bf4\u4e86\u4f7f\u7528shared_ptr\u5bf9\u6240\u6709\u7684Connection\u5bf9\u8c61\u8fdb\u884c\u7ba1\u7406&#xff0c;\u8fd9\u6837\u80fd\u591f\u9632\u6b62\u5728\u64cd\u4f5c\u7684\u8fc7\u7a0b\u4e2d\u8d44\u6e90\u88ab\u91ca\u653e\u3002 \u4f46\u662f&#xff0c;\u6211\u4eec\u5728\u7ed9 _message_cb \u8fd9\u6837\u7684\u56de\u8c03\u51fd\u6570\u4f20\u53c2\u7684\u65f6\u5019&#xff0c;\u5982\u4f55\u4fdd\u8bc1\u4f20\u7ed9\u4ed6\u7684shared_ptr\u5bf9\u8c61\u662f\u548c\u7ba1\u7406Conenction \u7684shared_ptr\u7684\u5bf9\u8c61\u5171\u4eab\u8ba1\u6570\u5462&#xff1f;<\/p>\n<p>\u56e0\u4e3a\u5982\u679c\u6211\u4eec\u76f4\u63a5\u4f7f\u7528 shared_ptr&lt;Connection&gt; p (this) &#xff0c;\u8fd9\u6837\u521b\u5efa\u4e00\u4e2a\u53ea\u80fd\u6307\u9488\u5bf9\u8c61\u4f20\u53c2\u7684\u65f6\u5019&#xff0c;\u4ed6\u7684\u8ba1\u6570\u662f\u72ec\u7acb\u7684&#xff0c;\u5e76\u4e0d\u4f1a\u548cTcpServer\u4e2d\u7ba1\u7406Conenction\u7684shared_ptr\u5171\u4eab\u8ba1\u6570&#xff0c;\u90a3\u4e48\u6211\u4eec\u5c31\u9700\u8981\u4e00\u4e2a\u529e\u6cd5\u80fd\u591f\u521b\u5efa\u51fa\u4e00\u4e2a\u548cConenction \u7684\u7ba1\u7406\u7684shared_ptr\u5bf9\u8c61\u5171\u4eab\u6280\u672f\u7684\u667a\u80fd\u6307\u9488\u8fdb\u884c\u4f20\u53c2&#xff0c;\u800cshared_from_this\u5c31\u53ef\u4ee5\u89e3\u51b3\u8fd9\u6837\u7684\u95ee\u9898\u3002<\/p>\n<ul>\n<li>std::enable_shared_from_this&lt;T&gt; \u5185\u90e8\u7ef4\u62a4\u4e86\u4e00\u4e2a std::weak_ptr&lt;T&gt;\u3002<\/li>\n<li>\u5f53\u7b2c\u4e00\u4e2a std::shared_ptr&lt;T&gt; \u5f00\u59cb\u7ba1\u7406\u8be5\u5bf9\u8c61\u65f6&#xff0c;\u8fd9\u4e2a weak_ptr \u88ab\u521d\u59cb\u5316\u3002<\/li>\n<li>\u4e4b\u540e&#xff0c;\u5f53 shared_from_this() \u88ab\u8c03\u7528\u65f6&#xff0c;\u5b83\u5c06\u57fa\u4e8e\u8fd9\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684 weak_ptr \u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 std::shared_ptr&lt;T&gt;&#xff0c;\u8fd9\u4e2a\u65b0\u7684 shared_ptr \u4e0e\u539f\u6709\u7684 shared_ptr \u5171\u4eab\u5bf9\u5bf9\u8c61\u7684\u6240\u6709\u6743\u3002<\/li>\n<\/ul>\n<p>\u90a3\u4e48\u4f7f\u7528\u8fd9\u4e2a\u63a5\u53e3&#xff0c;\u6211\u4eec\u5c31\u80fd\u4fdd\u8bc1\u5728\u8fd9\u4e9b\u56de\u8c03\u51fd\u6570\u5728\u6267\u884c\u7684\u65f6\u5019&#xff0c;\u5373\u4f7f\u5176\u4ed6\u7684\u5730\u65b9\u8c03\u7528\u4e86_svr_close_cb\u628aTcpServer\u6a21\u5757\u4e2d\u7684\u57fa\u7840\u8ba1\u6570\u7684\u667a\u80fd\u6307\u9488\u91ca\u653e\u4e86&#xff0c;\u8fd9\u4efd\u8d44\u6e90\u4e5f\u8fd8\u5b58\u5728&#xff0c;\u81f3\u5c11\u5728\u6211\u4eec\u8fd9\u6b21\u51fd\u6570\u6808\u5e27\u5185\u8fd8\u5b58\u5728&#xff0c;\u4e0d\u4f1a\u51fa\u73b0\u91ce\u6307\u9488\u7684\u95ee\u9898\u3002<\/p>\n<hr \/>\n<p>\u7b2c\u4e8c\u4e2a\u51fd\u6570\u5c31\u662fHandlerWrite &#xff0c;\u9996\u5148\u8fd9\u662f\u8bbe\u7f6e\u7ed9channel\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u4e5f\u5c31\u662f\u8bf4\u53ea\u6709\u5f53\u5199\u4e8b\u4ef6\u89e6\u53d1\u65f6\u624d\u4f1a\u8c03\u7528&#xff0c;\u90a3\u4e48\u6211\u4eec\u76f4\u63a5\u8c03\u7528write\u63a5\u53e3\u662f\u4e0d\u4f1a\u88ab\u963b\u585e\u7684\u3002 \u5f53\u7136\u6211\u4eec\u9700\u8981\u5224\u65adWrite\u7684\u8fd4\u56de\u503c&#xff0c;\u5224\u65ad\u662f\u5426\u51fa\u9519&#xff0c;\u5982\u679c\u5199\u5165\u6570\u636e\u51fa\u9519\u4e86&#xff0c;\u90a3\u4e48\u6211\u4eec\u5c31\u6ca1\u5fc5\u8981\u518d\u7ee7\u7eed\u5904\u7406\u6570\u636e\u4e86&#xff0c;\u5373\u4f7f\u5904\u7406\u4e86\u4e5f\u4e0d\u53ef\u80fd\u518d\u53d1\u51fa\u53bb&#xff0c;\u90a3\u4e48\u8fd9\u65f6\u5019\u6211\u4eec\u5c31\u9700\u8981\u8c03\u7528\u5b9e\u9645\u5173\u95ed\u8fde\u63a5\u7684\u63a5\u53e3\u3002<\/p>\n<p>\u540c\u65f6&#xff0c;<span style=\"color:#fe2c24\">\u6211\u4eec\u8fd8\u8981\u8003\u8651\u4e00\u79cd\u60c5\u51b5<\/span>&#xff0c;\u5c31\u662f&#xff0c;\u6b64\u65f6\u5176\u5b9e\u662f\u8bfb\u51fa\u9519\u4e4b\u540e&#xff0c;\u8c03\u7528ShutDown\u800c\u76d1\u542c\u8c03\u7528\u7684\u5199\u4e8b\u4ef6&#xff0c;\u90a3\u4e48\u8fd9\u65f6\u5019\u5199\u5b8c\u8fd9\u4e00\u6b21\u6570\u636e\u4e4b\u540e\u5c31\u9700\u8981\u5173\u95ed\u8fde\u63a5\u3002 \u5176\u5b9e\u4e5f\u5c31\u662f\u5224\u65ad\u8fde\u63a5\u662f\u5426\u4e3a\u5f85\u5173\u95ed\u72b6\u6001\u3002<\/p>\n<p>\u6d41\u7a0b\u56fe\u5982\u4e0b\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1248\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123245-6802466d0307e.png\" width=\"1102\" \/><\/p>\n<p>    void HanderWrite()<br \/>\n    {<br \/>\n        ssize_t ret &#061; _socket.SendNonBlock(_out_buffer.ReadAblesize());<br \/>\n        if (ret &lt; 0)<br \/>\n        {<br \/>\n            \/\/ \u5982\u679c\u53d1\u9001\u9519\u8bef&#xff0c;\u5c31\u770b\u770b\u662f\u4e0d\u662f\u63a5\u6536\u7f13\u51b2\u533a\u8fd8\u6709\u6570\u636e<br \/>\n            if (_in_buffer.ReadAbleSize() &gt; 0)<br \/>\n            {<br \/>\n                _message_callback(shared_from_this(), &amp;_in_buffer);<br \/>\n            }<br \/>\n            \/\/ \u7136\u540e\u518d\u771f\u6b63\u5173\u95ed<br \/>\n            return Release();<br \/>\n        }<br \/>\n        _out_buffer.MoveRindex(ret);<\/p>\n<p>        if (_out_buffer.ReadAbleSize() &#061;&#061; 0)<br \/>\n        {<br \/>\n            _channel.DisableWrite();<\/p>\n<p>            if (_status &#061;&#061; DISCONNECTING)<br \/>\n            {<br \/>\n                return Release();<br \/>\n            }<br \/>\n            return<br \/>\n        }<br \/>\n    } <\/p>\n<p>\u7136\u540e\u5c31\u662f\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u6211\u4eec\u53ea\u9700\u8981\u5224\u65ad\u662f\u5426\u542f\u52a8\u4e86\u8d85\u65f6\u8fde\u63a5&#xff0c;\u5982\u679c\u542f\u52a8\u4e86&#xff0c;\u90a3\u4e48\u5c31\u9700\u8981\u5237\u65b0\u5b9a\u65f6\u4efb\u52a1\u3002 \u540c\u65f6\u6211\u4eec\u4e5f\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u7528\u6237\u7684\u4efb\u610f\u56de\u8c03\u51fd\u6570&#xff0c;\u9664\u6b64\u4e4b\u5916\u5c31\u6ca1\u5176\u4ed6\u7684\u64cd\u4f5c\u4e86\u3002<\/p>\n<p>    void HandlerEvent()<br \/>\n    {<br \/>\n        if (_inactive_release &#061;&#061; true)<br \/>\n        {<br \/>\n            _loop-&gt;RefreshTimerTask(_connid);<br \/>\n        }<\/p>\n<p>        if (_event_callback)<br \/>\n        {<br \/>\n            _event_callback(shared_from_this());<br \/>\n        }<br \/>\n    } <\/p>\n<p>\u00a0\u7136\u540e\u5c31\u662f\u6302\u65ad\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u6302\u65ad\u4e8b\u4ef6\u4e5f\u5f88\u7b80\u5355&#xff0c;\u56e0\u4e3a\u53ef\u80fd\u5728\u6302\u65ad\u4e8b\u4ef6\u89e6\u53d1\u7684\u65f6\u5019&#xff0c;\u4e5f\u89e6\u53d1\u4e86\u8bfb\u4e8b\u4ef6&#xff0c;\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5148\u5904\u7406\u4ee5\u4e0b\u6570\u636e&#xff0c;\u7136\u540e\u76f4\u63a5\u8c03\u7528Release\u5173\u95ed\u8fde\u63a5&#xff0c;\u5904\u4e0d\u5904\u7406\u90fd\u884c&#xff0c;\u53cd\u6b63\u4e5f\u65e0\u6cd5\u8fd4\u56de\u4e86&#xff0c;\u4f46\u662f\u6700\u597d\u5904\u7406\u4ee5\u4e0b&#xff0c;\u56e0\u4e3a\u53ef\u80fd\u662f\u4e00\u4e9b\u529f\u80fd\u6027\u8bf7\u6c42\u3002<\/p>\n<p>    void HandlerClose()<br \/>\n    {<br \/>\n        if (_in_buffer.ReadAbleSize() &gt; 0)<br \/>\n        {<br \/>\n            _message_callback(shared_from_this(), &amp;_in_buffer);<br \/>\n        }<br \/>\n        return Release();<br \/>\n    } <\/p>\n<p>\u6700\u540e\u5c31\u662f\u9519\u8bef\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u9519\u8bef\u4e8b\u4ef6\u89e6\u53d1\u7684\u65f6\u5019&#xff0c;\u6211\u4eec\u7684\u5904\u7406\u65b9\u6cd5\u548c\u6302\u65ad\u4e8b\u4ef6\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>    void HandlerClose()<br \/>\n    {<br \/>\n        if (_in_buffer.ReadAbleSize() &gt; 0)<br \/>\n        {<br \/>\n            _message_callback(shared_from_this(), &amp;_in_buffer);<br \/>\n        }<br \/>\n        return Release();<br \/>\n    } <\/p>\n<p>\u4e3a\u4ec0\u4e48\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e9b\u63a5\u53e3\u4e0d\u9700\u8981\u5305\u88c5\u4e00\u5c42 _loop-&gt;RunInLoop() \u5462&#xff1f; \u8fd8\u662f\u90a3\u53e5\u8bdd&#xff0c;\u56e0\u4e3a\u8fd9\u4e9bIO\u4e8b\u4ef6\u7684\u56de\u8c03\u4e00\u5b9a\u662f\u5728\u5bf9\u5e94\u7684EventLoop\u7ebf\u7a0b\u6267\u884c\u7684&#xff1f;<\/p>\n<p>\u4e8b\u4ef6\u5faa\u73af\u673a\u5236\u7684\u5de5\u4f5c\u539f\u7406&#xff1a;<\/p>\n<ul>\n<li>\u5728\u8fd9\u79cd\u57fa\u4e8eReactor\u6a21\u5f0f\u7684\u7f51\u7edc\u5e93\u4e2d&#xff0c;\u6bcf\u4e2aEventLoop\u90fd\u8fd0\u884c\u5728\u81ea\u5df1\u7684\u7ebf\u7a0b\u4e0a<\/li>\n<li>Channel\u5bf9\u8c61\u6ce8\u518c\u5230EventLoop\u65f6&#xff0c;\u4f1a\u5c06\u8fd9\u4e9b\u56de\u8c03\u51fd\u6570\u6ce8\u518c\u5230\u4e8b\u4ef6\u5faa\u73af\u7684\u4e8b\u4ef6\u5206\u53d1\u5668\u4e0a<\/li>\n<li>\u5f53IO\u4e8b\u4ef6\u53d1\u751f\u65f6&#xff0c;\u662fEventLoop\u7ebf\u7a0b\u672c\u8eab\u5728\u8c03\u7528\u8fd9\u4e9b\u56de\u8c03\u51fd\u6570&#xff0c;\u800c\u4e0d\u662f\u5176\u4ed6\u7ebf\u7a0b<\/li>\n<\/ul>\n<p>\u4e00\u7ebf\u4e00\u5e93\u7684\u8bbe\u8ba1\u7406\u5ff5&#xff1a;<\/p>\n<ul>\n<li>\u6bcf\u4e2aConnection\u5bf9\u8c61\u90fd\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684EventLoop<\/li>\n<li>\u6bcf\u4e2aEventLoop\u90fd\u8fd0\u884c\u5728\u81ea\u5df1\u7684\u7ebf\u7a0b\u4e0a<\/li>\n<li>\u4e8b\u4ef6\u7684\u68c0\u6d4b\u548c\u5206\u53d1\u90fd\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u5b8c\u6210<\/li>\n<\/ul>\n<p>\u6240\u4ee5\u6ca1\u6709\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff0c;\u4f46\u662f\u5176\u4ed6\u7684\u64cd\u4f5c\u5c31\u53ef\u80fd\u6709\u4e86&#xff0c;\u6bd4\u5982\u6211\u4eec\u7684Release\u4ee5\u53caShutDown\u7b49\u64cd\u4f5c&#xff0c;\u8fd9\u4e9b\u5bf9\u8fde\u63a5\u7684\u64cd\u4f5c\u672a\u6765\u90fd\u53ef\u80fd\u662f\u5728\u5176\u4ed6\u7ebf\u7a0b\u4e2d\u6267\u884c\u7684&#xff0c;\u53ef\u80fd\u662f\u5728\u522b\u7684\u6a21\u5757\u4e2d\u88ab\u8c03\u7528\u7684&#xff0c;\u6240\u4ee5\u9700\u8981\u5305\u88c5\u4e00\u5c42\u3002<\/p>\n<ul>\n<li>\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528&#xff1a;\n<ul>\n<li>\u4f8b\u5982&#xff0c;\u7528\u6237\u53ef\u80fd\u5728\u4e3b\u7ebf\u7a0b\u6216\u5176\u4ed6\u5de5\u4f5c\u7ebf\u7a0b\u4e2d\u8c03\u7528Connection::Send()<\/li>\n<li>\u4e3a\u4e86\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u9700\u8981\u5c06\u8fd9\u4e9b\u64cd\u4f5c\u8f6c\u79fb\u5230Connection\u6240\u5c5e\u7684EventLoop\u7ebf\u7a0b\u4e2d\u6267\u884c<\/li>\n<\/ul>\n<\/li>\n<li>\u907f\u514d\u540c\u6b65\u95ee\u9898&#xff1a;\n<ul>\n<li>\u4f7f\u7528RunInLoop\u673a\u5236\u53ef\u4ee5\u907f\u514d\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u76f4\u63a5\u8bbf\u95ee&#xff0c;\u9632\u6b62\u7ade\u6001\u6761\u4ef6<\/li>\n<li>\u6240\u6709\u7684\u64cd\u4f5c\u90fd\u4f1a\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5185\u6309\u987a\u5e8f\u6267\u884c&#xff0c;\u65e0\u9700\u62c5\u5fc3\u9501\u7684\u95ee\u9898<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u63a5\u7740\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e9b\u83b7\u53d6\u5668\u65b9\u6cd5&#xff0c;\u7528\u4e8e\u83b7\u53d6 _sockfd \u548c _connid\u548c\u8fde\u63a5\u72b6\u6001<\/p>\n<p>    int Socked()<br \/>\n    {<br \/>\n        return _sockfd;<br \/>\n    }<br \/>\n    int Id()<br \/>\n    {<br \/>\n        return _connid;<br \/>\n    }<br \/>\n    bool Connected()<br \/>\n    {<br \/>\n        return (_status &#061;&#061; CONNECTED);<br \/>\n    } <\/p>\n<p>\u00a0\u518d\u6765\u5b9e\u73b0\u51e0\u4e2a\u7b80\u5355\u7684\u63a5\u53e3&#xff0c;\u4e5f\u5c31\u662f\u7528\u6237\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\u7684\u65b9\u6cd5&#xff1a;<\/p>\n<p>    \/\/\u7528\u6237\u8bbe\u7f6e\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    void SetConnectedCallback()<br \/>\n    {<br \/>\n        _connected_callback &#061; cb;<br \/>\n    }<br \/>\n    void SetMessageCallback()<br \/>\n    {<br \/>\n        _message_callback &#061; cb;<br \/>\n    }<br \/>\n    void SetCloseCallback()<br \/>\n    {<br \/>\n        _close_callback &#061; cb;<br \/>\n    }<br \/>\n    void SetSvrCloseCallback()<br \/>\n    {<br \/>\n        _svrclose_callback &#061; cb;<br \/>\n    }<br \/>\n    void SetEventCallback()<br \/>\n    {<br \/>\n        _event_callback &#061; cb;<br \/>\n    } <\/p>\n<p>\u76ee\u524d\u6211\u4eec\u5927\u6982\u5c31\u80fd\u5b8c\u6210\u8fde\u63a5\u5f85\u5904\u7406\u72b6\u6001\u7684\u64cd\u4f5c\u4e86\u3002\u7136\u540e\u8bbe\u7f6eConnection\u7684\u5c5e\u6027\u5e76\u8ba9\u5176\u5f00\u59cb\u901a\u4fe1\u3002<\/p>\n<p>\u5c31\u5982\u540c\u4e0a\u9762\u7684\u8fd9\u51e0\u4e2a\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\u7684\u63a5\u53e3&#xff0c;\u6211\u4eec\u90fd\u662f\u5728\u521b\u5efa\u51fa\u6765\u4e00\u4e2a\u5bf9\u8c61\u4e4b\u540e&#xff0c;\u6b63\u5f0f\u901a\u4fe1\u4e4b\u524d\u8bbe\u7f6e\u7684&#xff0c;\u9664\u6b64\u4e4b\u5916&#xff0c;\u6211\u4eec\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u542f\u52a8\u548c\u53d6\u6d88\u975e\u6d3b\u8dc3\u8fde\u63a5\u9500\u6bc1\u673a\u5236\u3002<\/p>\n<p>\u6211\u4eec\u8981\u6ce8\u610f\u7684\u662f&#xff0c;<span style=\"color:#fe2c24\">\u8fd9\u4e24\u4e2a\u63a5\u53e3\u5176\u5b9e\u6709\u53ef\u80fd\u5728\u901a\u4fe1\u7684\u8fc7\u7a0b\u4e2d\u88ab\u8c03\u7528&#xff0c;<\/span>\u5982\u679c\u662f\u5728\u901a\u4fe1\u4e4b\u524d\u88ab\u8c03\u7528&#xff0c;\u90a3\u4e48\u662f\u6ca1\u6709\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u7684&#xff0c;<span style=\"color:#be191c\">\u4f46\u662f\u5982\u679c\u662f\u5728\u8fde\u63a5\u5df2\u7ecf\u5f00\u59cb\u901a\u4fe1\u4e4b\u540e\u518d\u88ab\u8c03\u7528&#xff0c;<\/span>\u90a3\u4e48\u6211\u4eec\u8981\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u5c31\u9700\u8981\u5c01\u88c5\u4e00\u5c42\u51fd\u6570\u653e\u5230 EventLoop \u7684RunInLoop \u4e2d\u8fd0\u884c&#xff0c;\u6bd4\u5982\u8fd9\u4e9b\u63a5\u53e3\u5176\u5b9e\u90fd\u662f\u5728TcpServer\u4e2d\u8c03\u7528\u7684&#xff0c;\u6211\u4eec\u65e0\u6cd5\u786e\u5b9a\u5177\u4f53\u5728\u54ea\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u6267\u884c\u3002<\/p>\n<p>    void EnableInactiveReleaseInLoop(int seconds)<br \/>\n    {<br \/>\n        _inactive_release &#061; true;<br \/>\n        if (_loop-&gt;HasTimerTask(_connid)) \/\/ \u5224\u65ad\u662f\u5426\u6709\u5b9a\u65f6\u5668<br \/>\n        {<br \/>\n            return _loop-&gt;RefreshTimerTask(_connid);<br \/>\n        }<br \/>\n        _loop-&gt;AddTimerTask(_connid, sec, bind(&amp;Connection::Release, this));<br \/>\n    }<\/p>\n<p>    void CancelInactiveReleaseInLoop(int seconds)<br \/>\n    {<br \/>\n        _inactive_release &#061; false;<\/p>\n<p>        _loop-&gt;CancelTimerTask(_connid);<br \/>\n    }<\/p>\n<p>    void EnableInactiveRelease(int sec)<br \/>\n    {<br \/>\n        _loop-&gt;RunInLoop(bind(&amp;Connection::EnableInactiveReleaseInLoop, this, seconds));<br \/>\n    }<\/p>\n<p>    void CancelInactiveRelease()<br \/>\n    {<br \/>\n        _loop-&gt;RunInLoop(bind(&amp;Connection::CancelInactiveReleaseInLoop, this));<br \/>\n    } <\/p>\n<p>\u00a0\u5f53\u8bbe\u7f6e\u5b8c\u8fd9\u4e9b\u4e4b\u540e&#xff0c;\u6211\u4eec\u5c31\u53ef\u4ee5\u542f\u52a8\u8fde\u63a5&#xff0c;\u5f00\u59cb\u901a\u4fe1\u4e86&#xff0c;\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a Established () \u63a5\u53e3\u4ee5\u4f9b\u5916\u754c\u6216\u8005\u8bf4\u4e0a\u5c42\u7684TcpServer\u8c03\u7528\u3002 \u4f46\u662f\u4e3a\u4e86\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u6211\u4eec\u9700\u8981\u5c06\u5b9e\u9645\u7684\u64cd\u4f5c\u5c01\u88c5\u6210\u4e00\u4e2a\u4efb\u52a1\u4ea4\u7ed9loop\u3002<\/p>\n<p>    void EstablishInLoop()<br \/>\n    {<br \/>\n        assert(_status &#061;&#061; CONNECTING);<br \/>\n        _status &#061; CONNECTED;<br \/>\n        _channel.EnableRead();<br \/>\n        if (_connected_callback)<br \/>\n        {<br \/>\n            _connected_callback(shared_from_this());<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void Establish() \/\/ \u8fde\u63a5\u83b7\u53d6\u4e4b\u540e\u8fdb\u884c\u5404\u79cd\u8bbe\u7f6e&#xff0c;\u8bbe\u7f6e\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u542f\u52a8\u8bfb\u76d1\u63a7&#xff0c;\u8c03\u7528\u8bfb\u4e66\u7b80\u56de\u8c03\u7b49<br \/>\n    {<br \/>\n        _loop-&gt;RunInLoop(bind(&amp;Connection::EstblishInLoop, this));<br \/>\n    } <\/p>\n<p>\u540c\u65f6&#xff0c;\u5728\u51c6\u5907\u9636\u6bb5\u4e5f\u9700\u8981\u8bbe\u7f6e\u4e0a\u4e0b\u6587\u7684\u63a5\u53e3&#xff0c;\u8fd9\u4e2a\u63a5\u53e3\u6211\u4eec\u5c31\u4e0d\u5173\u5fc3\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\u4e86&#xff0c;\u56e0\u4e3a\u4e00\u822c\u6765\u8bf4\u6211\u4eec\u7684\u903b\u8f91\u6ca1\u95ee\u9898\u7684\u8bdd\u662f\u4e0d\u4f1a\u91cd\u590d\u8bbe\u7f6e\u7684\u3002\u00a0<\/p>\n<p>    void SetContext(const Any &amp;context)<br \/>\n    {<br \/>\n        _context &#061; context;<br \/>\n    } <\/p>\n<h3 id=\"%E8%BF%9E%E6%8E%A5%E9%80%9A%E4%BF%A1%E9%98%B6%E6%AE%B5%C2%A0\" style=\"background-color:transparent\">\u8fde\u63a5\u901a\u4fe1\u9636\u6bb5\u00a0<\/h3>\n<p>\u90a3\u4e48\u5df2\u7ecf\u6b65\u5165\u901a\u4fe1\u9636\u6bb5\u4e4b\u540e&#xff0c;\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\u7ed9\u7528\u6237\u7528\u4e8e\u53d1\u9001\u6570\u636e&#xff0c;\u4e5f\u5c31\u662f\u5411\u8f93\u51fa\u7f13\u51b2\u533a\u5199\u5165\u6570\u636e&#xff0c;\u8fd9\u4e2a\u63a5\u53e3\u4e5f\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684&#xff0c;\u6240\u4ee5\u4e5f\u9700\u8981\u5c01\u88c5\u6210\u4efb\u52a1\u4ea4\u7ed9 RunInLoop<\/p>\n<p>    void SendInLoop(Buffer &amp;buf) \/\/ \u628a\u6570\u636e\u5199\u5165\u5230\u7528\u6237\u6001\u7684\u53d1\u9001\u7f13\u51b2\u533a\u4e2d<br \/>\n    {<br \/>\n        if (_status &#061;&#061; DISCONNECTED)<br \/>\n        {<br \/>\n            return;<br \/>\n        }<br \/>\n        _out_buffer.WriteBufferAndPush(buf);<br \/>\n        if (_channel.WriteAble() &#061;&#061; false)<br \/>\n        {<br \/>\n            _channel.EnableWrite();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void Send(const char *data, size_t len) \/\/ \u53d1\u9001\u5e76\u4e0d\u662f\u771f\u7684\u53d1\u9001\u4e86&#xff0c;\u662f\u8981\u5148\u628a\u6570\u636e\u653e\u5230\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e0b&#xff0c;\u7136\u540e\u518d\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u53bb\u53d1\u9001<br \/>\n    {<br \/>\n        Buffer buf;<br \/>\n        buf.WriteAndPush(data, len);<br \/>\n        _loop-&gt;RunInLoop(bind(&amp;Connection::SendInLoop, this, move(buf)));<br \/>\n    } <\/p>\n<p>\u7136\u540e\u5c31\u662f\u8bbe\u7f6e\u4e0a\u4e0b\u6587\u7684\u63a5\u53e3&#xff0c;\u540c\u65f6\u8981\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u00a0<\/p>\n<p>\u8bbe\u7f6e\u4e0a\u4e0b\u6587\u5176\u5b9e\u5c31\u9700\u8981<span style=\"color:#fe2c24\">\u91cd\u65b0\u8bbe\u7f6e\u4e0a\u4e0b\u6587\u4ee5\u53ca\u56db\u4e2a\u56de\u8c03\u51fd\u6570<\/span>\u3002\u56e0\u4e3a\u4e0d\u540c\u7684\u534f\u8bae&#xff0c;\u5b83\u7684\u5904\u7406\u65b9\u5f0f\u53ef\u80fd\u4e0d\u540c&#xff0c;\u6bd4\u5982\u8bf4\u539f\u5148\u7684\u8fde\u63a5\u56de\u8c03\u51fd\u6570\u7684\u903b\u8f91\u9047\u5230\u4e8b\u4ef6\u76f4\u63a5\u5c31\u5904\u7406&#xff0c;\u4f46\u662f\u4f60\u5207\u6362\u4e86\u67d0\u4e2a\u534f\u8bae\u4e4b\u540e&#xff0c;\u9700\u8981\u5148\u8fdb\u884c\u9a8c\u8bc1&#xff0c;\u90a3\u6b64\u65f6\u65e7\u7684\u56de\u8c03\u51fd\u6570\u7684\u903b\u8f91\u5c31\u4e0d\u7b26\u5408\u4e86<\/p>\n<p>    void UpgradeInLoop(const Any &amp;context,<br \/>\n                       const ConnectionCallback &amp;connected_callback,<br \/>\n                       const MessageCallback &amp;message_callback,<br \/>\n                       const CloseCallback &amp;close_callback,<br \/>\n                       const EventCallback &amp;event_callback)<br \/>\n    {<br \/>\n        _context &#061; context;<br \/>\n        _connected_callback &#061; connected_callback;<br \/>\n        _message_callback &#061; message_callback;<br \/>\n        _close_callback &#061; close_callback;<br \/>\n        _event_callback &#061; event_callback;<br \/>\n    } <\/p>\n<p>\u5177\u4f53\u6267\u884c\u7684\u64cd\u4f5c\u5c31\u662f\u8fd9\u6837\u7684&#xff0c;\u4f46\u662f Upgrade \u8fd9\u4e2a\u63a5\u53e3\u6709\u70b9\u7279\u6b8a&#xff0c;\u5b83\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684&#xff0c;\u6240\u4ee5\u5fc5\u987b\u5728EventLoop\u7ebf\u7a0b\u4e2d\u6267\u884c&#xff0c;\u540c\u65f6&#xff0c;\u8fd9\u8fd8\u4e0d\u591f&#xff0c;\u4ed6\u5fc5\u987b\u88ab\u7acb\u9a6c\u6267\u884c&#xff0c;\u4e0d\u80fd\u653e\u5230\u4efb\u52a1\u961f\u5217\u4e2d&#xff0c;\u56e0\u4e3a\u5982\u679c\u5982\u679c\u653e\u5230\u4efb\u52a1\u961f\u5217\u4e2d&#xff0c;\u90a3\u4e48\u5982\u679c\u6b64\u65f6\u6709\u8bfb\u4e8b\u4ef6\u5230\u6765&#xff0c;\u90a3\u4e48\u5c31\u4f1a\u5148\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406&#xff0c;\u4e5f\u5c31\u662f\u4f1a\u5148\u4f7f\u7528\u8001\u7684\u5904\u7406\u51fd\u6570\u8fdb\u884c\u5904\u7406&#xff0c;\u7136\u540e\u624d\u66f4\u65b0\u56de\u8c03\u65b9\u6cd5&#xff0c;\u8fd9\u662f\u4e0d\u5bf9\u7684&#xff0c;\u6211\u4eec\u5fc5\u987b\u5728\u8c03\u7528Upgrade\u7684\u65f6\u5019\u7acb\u5373\u5c06\u534f\u8bae\u7684\u56de\u8c03\u51fd\u6570\u548c\u4e0a\u4e0b\u6587\u8fdb\u884c\u66ff\u6362\u3002<span style=\"color:#fe2c24\">\u6216\u8bb8\u6362\u4e00\u79cd\u8bf4\u6cd5&#xff1a;<\/span><span style=\"color:#956fe7\">\u6211\u4eec\u5fc5\u987b\u5728EventLoop\u7ebf\u7a0b\u4e2d\u8c03\u7528Upgrade\u51fd\u6570&#xff0c;\u5982\u679c\u5728\u5176\u4ed6\u7ebf\u7a0b\u5185\u8c03\u7528&#xff0c;\u90a3\u4e48\u76f4\u63a5\u62a5\u9519\u3002 \u7edd\u5bf9\u4e0d\u80fd\u5728\u5176\u4ed6\u7684\u7ebf\u7a0b\u4e2d\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u3002<\/span><\/p>\n<p>\u8fd9\u4e2a\u8bfb\u4e8b\u4ef6\u4e3a\u4ec0\u4e48\u4f1a\u6bd4\u4ed6\u5148\u8fdb\u884c \u8fd9\u4e2a\u4efb\u52a1\u961f\u5217\u4e0d\u662f\u6309\u7167\u987a\u5e8f\u8fdb\u884c\u6267\u884c\u7684\u5417&#xff1f;\u5148\u628a\u66f4\u65b0\u534f\u8bae\u653e\u5230\u4efb\u52a1\u961f\u5217\u4e2d \u7136\u540e\u8bfb\u4e8b\u4ef6\u6765\u4e86&#xff0c;\u518d\u628a\u8bfb\u4e8b\u4ef6\u653e\u5165\u5230\u961f\u5217\u4e2d&#xff1f;<\/p>\n<p>\u5728EventLoop\u4e2d&#xff0c;\u4efb\u52a1\u961f\u5217\u786e\u5b9e\u662f\u6309\u987a\u5e8f\u6267\u884c\u7684&#xff0c;\u4f46\u95ee\u9898\u51fa\u5728\u8bfb\u4e8b\u4ef6\u7684\u89e6\u53d1\u673a\u5236\u4e0a&#xff1a;<\/p>\n<p>\u5f53\u7f51\u7edc\u6570\u636e\u5230\u8fbe\u65f6&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u901a\u77e5 EventLoop \u6709\u8bfb\u4e8b\u4ef6\u53d1\u751f\u3002\u8fd9\u4e2a\u901a\u77e5\u662f\u901a\u8fc7 epoll\/poll \u7b49 I\/O \u590d\u7528\u673a\u5236\u5b9e\u73b0\u7684&#xff0c;\u53d1\u751f\u5728 EventLoop \u7684\u4e8b\u4ef6\u5faa\u73af\u4e2d\u3002\u800c\u4efb\u52a1\u961f\u5217\u901a\u5e38\u662f\u5728\u5904\u7406\u5b8c\u6240\u6709\u5df2\u89e6\u53d1\u7684 I\/O \u4e8b\u4ef6\u540e\u624d\u4f1a\u88ab\u6267\u884c\u3002\u6240\u4ee5\u6267\u884c\u987a\u5e8f\u53ef\u80fd\u662f\u8fd9\u6837\u7684&#xff1a;<\/p>\n<ul>\n<li>\u8c03\u7528 Upgrade \u65b9\u6cd5&#xff0c;\u5c06\u534f\u8bae\u5207\u6362\u4efb\u52a1\u653e\u5165\u961f\u5217<\/li>\n<li>\u6570\u636e\u5230\u8fbe&#xff0c;\u89e6\u53d1\u8bfb\u4e8b\u4ef6<\/li>\n<li>EventLoop \u5148\u5904\u7406\u6240\u6709 I\/O \u4e8b\u4ef6&#xff08;\u5305\u62ec\u521a\u624d\u89e6\u53d1\u7684\u8bfb\u4e8b\u4ef6&#xff09;<\/li>\n<li>\u7136\u540e\u624d\u6267\u884c\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4efb\u52a1&#xff08;\u5305\u62ec\u5347\u7ea7\u8fde\u63a5\u7684\u4efb\u52a1&#xff09;<\/li>\n<\/ul>\n<p>\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5728EventLoop\u4e2d\u518d\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3<\/p>\n<p>    void AssertInLoop()const{assert(_thread_id &#061;&#061; std::this_thread::get_id());}<\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u7684Upgrade\u63a5\u53e3\u7684\u5b9e\u73b0\u5c31\u662f\u8fd9\u6837\u7684&#xff1a;<\/p>\n<p>    void Upgrade(const Any&amp; context,const ConnectCallBack&amp; con , const MessageCallBack&amp; msg, const CloseCallBack&amp; clo,const EventCallBack&amp; evt)<br \/>\n    {<br \/>\n       _loop-&gt;AssertInLoop();<br \/>\n        _loop-&gt;RunInLoop(std::bind(&amp;Connection::UpgradeInLoop,this,context,con,msg,clo,evt));<br \/>\n    } <\/p>\n<p>\u6211\u4eec\u8fd8\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\u7528\u4e8e\u83b7\u53d6\u4e0a\u4e0b\u6587&#xff0c;\u8fd9\u4e2a\u63a5\u53e3\u53ef\u4ee5\u4e0d\u8fdb\u884c\u5c01\u88c5<\/p>\n<p>    Any* GetContext() {return &amp;_context;}<\/p>\n<p>\u7136\u540e\u5c31\u662f\u5173\u95ed\u8fde\u63a5\u7684\u63a5\u53e3\u4e86&#xff0c;\u6211\u4eec\u6709\u4e24\u5957\u63a5\u53e3&#xff0c;\u9996\u5148\u6765\u5b9e\u73b0ShutDown&#xff0c;\u4e5f\u5c31\u662f\u7528\u6237\u5173\u95ed\u8fde\u63a5\u7684\u63a5\u53e3&#xff0c;\u8fd9\u4e2a\u63a5\u53e3\u4e5f\u662f\u9700\u8981\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff0c;\u9700\u8981\u5c01\u88c5\u6210\u4efb\u52a1\u3002<\/p>\n<p>\u5728\u8fd9\u4e2a\u63a5\u53e3\u4e2d&#xff0c;\u6211\u4eec\u9700\u8981\u5904\u7406\u8f93\u5165\u7f13\u51b2\u533a\u548c\u8f93\u51fa\u7f13\u51b2\u533a\u7684\u6570\u636e&#xff0c;\u7136\u540e\u518d\u8c03\u7528Release\u63a5\u53e3\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n<p>    void ShutDownInLoop()<br \/>\n    {<br \/>\n        _con_statu &#061; CLOSING;   \/\/\u8bbe\u7f6e\u8fde\u63a5\u5f85\u5173\u95ed<br \/>\n        if(_in_buffer.ReadSize()) \/\/\u6709\u6570\u636e\u5f85\u5904\u7406<br \/>\n            if(_message_cb) _message_cb(shared_from_this(),&amp;_in_buffer);<br \/>\n        \/\/\u6240\u6709\u6570\u636e\u90fd\u5904\u7406\u5b8c\u4e4b\u540e&#xff0c;\u5904\u7406\u5f85\u53d1\u9001\u6570\u636e<br \/>\n        if(_out_buffer.ReadSize())  \/\/\u5982\u679c\u6709\u6570\u636e\u5f85\u53d1\u9001<br \/>\n        {<br \/>\n            _channel.EnableWrite();    \/\/\u542f\u52a8\u5199\u4e8b\u4ef6\u76d1\u542c<br \/>\n            \/\/\u89e6\u53d1\u5199\u4e8b\u4ef6\u4e4b\u540e&#xff0c;\u5728\u5199\u4e8b\u4ef6\u56de\u8c03\u4e2d\u4f1a\u8c03\u7528Release\u8fdb\u884c\u8fde\u63a5\u91ca\u653e<br \/>\n        }<br \/>\n        else Release(); \/\/\u5982\u679c\u6ca1\u6709\u6570\u636e\u5f85\u53d1\u9001\u5c31\u76f4\u63a5\u5173\u95ed<br \/>\n    }<\/p>\n<p>    void ShutDown()<br \/>\n    {<br \/>\n        _loop-&gt;RunInLoop(std::bind(&amp;Connection::ShutDownInLoop,this));<br \/>\n    }  <\/p>\n<p>\u6700\u540e\u5c31\u662f\u5b9e\u9645\u91ca\u653e\u8fde\u63a5\u7684Release\u63a5\u53e3\u4e86<\/p>\n<p>\u9996\u5148&#xff0c;\u8fde\u63a5\u72b6\u6001\u8bbe\u7f6e\u4e3a DISCONNECTED, \u79fb\u9664\u6240\u6709\u4e8b\u4ef6\u76d1\u63a7&#xff0c;\u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u7136\u540e\u53d6\u6d88\u5b9a\u65f6\u4efb\u52a1&#xff0c;\u4ee5\u53ca\u8c03\u7528\u7528\u6237\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u6700\u540e\u91ca\u653e\u6389TcpServer\u6240\u7ba1\u7406\u7684\u57fa\u7840\u8ba1\u6570<\/p>\n<p>    void ReleaseInLoop()<br \/>\n    {<br \/>\n        \/\/ 1.\u5224\u65ad\u72b6\u6001 2.\u79fb\u9664\u6240\u6709\u4e8b\u4ef6\u76d1\u63a7 3.\u5173\u95ed\u5957\u63a5\u5b57 4.\u79fb\u9664\u5b9a\u65f6\u5668 5.\u8c03\u7528\u5173\u95ed\u56de\u8c03\u51fd\u6570<br \/>\n        _status &#061; DISCONNECTED;<br \/>\n        _channel.Remove();<br \/>\n        _socket.Close();<br \/>\n        if (_loop-&gt;HasTimerTask(_connid)) \/\/ \u5224\u65ad\u662f\u5426\u6709\u5b9a\u65f6\u5668<br \/>\n        {<br \/>\n            CancelInactiveReleaseInLoop();<br \/>\n        }<br \/>\n        if (_close_callback)<br \/>\n        {<br \/>\n            _close_callback(share_from_this());<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void Release()<br \/>\n    {<br \/>\n        _loop-&gt;RunInLoop(bind(&amp;Connection::ReleaseInLoop, this));<br \/>\n    } <\/p>\n<ul><\/ul>\n<h2 id=\"%E7%96%91%E6%83%91%E7%82%B9\">\u7591\u60d1\u70b9<\/h2>\n<p>\u5728\u8bbe\u8ba1 Connection \u7c7b\u65f6&#xff0c;\u6211\u4eec\u9700\u8981\u8003\u8651\u5982\u4f55\u552f\u4e00\u6807\u8bc6\u6bcf\u4e2a\u8fde\u63a5\u3002\u5bf9\u4e8e\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u4f7f\u7528\u6587\u4ef6\u63cf\u8ff0\u7b26(fd)\u4f5c\u4e3a\u552f\u4e00\u6807\u8bc6&#xff1f;<\/p>\n<p>\u6709\u51e0\u4e2a\u91cd\u8981\u539f\u56e0&#xff1a;<\/p>\n<li>\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u80fd\u4f1a\u88ab\u91cd\u7528 &#8211; \u5f53\u4e00\u4e2a\u8fde\u63a5\u5173\u95ed\u540e&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u4f1a\u5c06\u8fd9\u4e2a fd \u91cd\u65b0\u5206\u914d\u7ed9\u65b0\u7684\u8fde\u63a5&#xff0c;\u8fd9\u53ef\u80fd\u5bfc\u81f4\u6807\u8bc6\u6df7\u6dc6<\/li>\n<li>\u8de8\u8fdb\u7a0b\/\u8de8\u670d\u52a1\u5668\u95ee\u9898 &#8211; \u5982\u679c\u670d\u52a1\u5668\u662f\u5206\u5e03\u5f0f\u6216\u591a\u8fdb\u7a0b\u7684&#xff0c;fd \u5728\u4e0d\u540c\u8fdb\u7a0b\u95f4\u53ef\u80fd\u4e0d\u552f\u4e00<\/li>\n<li>\u6301\u4e45\u5316\u95ee\u9898 &#8211; \u5982\u679c\u9700\u8981\u6301\u4e45\u5316\u8fde\u63a5\u4fe1\u606f&#xff0c;\u5f53\u670d\u52a1\u5668\u91cd\u542f\u540e fd \u4f1a\u6539\u53d8<\/li>\n<p>\u00a0HanderRead\u7684\u6d41\u7a0b\u56fe<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"726\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123245-6802466d7932e.png\" width=\"1194\" \/>\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"699\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123245-6802466dc7003.png\" width=\"1044\" \/><\/p>\n<p>\u00a0\u200b\u200b\u200b\u200b\u200b\u200b<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"757\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123246-6802466e24ec4.png\" width=\"1878\" \/><\/p>\n<\/p>\n<p>\u4e3a\u5565connection\u6a21\u5757\u8fd8\u9700\u8981\u4e00\u4e9bInLoop\u7684\u63a5\u53e3 connection\u4e0d\u5c31\u5728EventLoop\u6a21\u5757\u4e2d\u5417 \u4e0d\u662f\u5df2\u7ecf\u7ed1\u5b9a\u4e86\u7ebf\u7a0b\u4e86\u5417&#xff1f;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"633\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123246-6802466e9cf83.png\" width=\"1123\" \/><\/p>\n<p>\u90a3\u4e0d\u80fd\u76f4\u63a5\u628a\u8fd9\u4e2asend\u653e\u5165\u79c1\u6709\u63a5\u53e3\u91cc\u9762\u5417&#xff1f;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1234\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123246-6802466ee8778.png\" width=\"1100\" \/><\/p>\n<p>\u00a0std::bind(&amp;Connection::SendInLoop, this, in, len)\u4e0d\u7406\u89e3\u8fd9\u4e2a\u4ee3\u7801<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"469\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123247-6802466f6a2ab.png\" width=\"1064\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"278\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123247-6802466fa6fef.png\" width=\"1017\" \/><\/p>\n<p>\u00a0move\u7684\u4f5c\u7528<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"128\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123247-6802466fc1c05.png\" width=\"1055\" \/><\/p>\n<p>\u4e0d\u662fchannel\u7ba1\u7406\u7684\u662f\u4e8b\u4ef6\u56de\u8c03\u5417&#xff1f;\u600e\u4e48\u8fd9\u91cc\u4e5f\u8981\u8bbe\u7f6e\u5462 \u4e0d\u80fd\u8c03\u7528channel\u6a21\u5757\u7684\u56de\u8c03\u51fd\u6570\u5417&#xff1f;\u00a0<\/p>\n<p>\u7b80\u800c\u8a00\u4e4b&#xff0c;Channel\u6a21\u5757\u7684\u56de\u8c03\u51fd\u6570\u8d1f\u8d23\u7684\u662f\u5e95\u5c42Socket\u7684\u4e8b\u4ef6\u7ba1\u7406&#xff0c;Connection\u6a21\u5757\u7684\u56de\u8c03\u51fd\u6570\u8d1f\u8d23\u7684\u662f\u7528\u6237\u5c42\u7684\u4e8b\u4ef6\u56de\u8c03\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"615\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123247-6802466fdcecd.png\" width=\"1047\" \/>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.2k\u6b21\uff0c\u70b9\u8d5e60\u6b21\uff0c\u6536\u85cf51\u6b21\u3002\u5177\u4f53\u6267\u884c\u7684\u64cd\u4f5c\u5c31\u662f\u8fd9\u6837\u7684\uff0c\u4f46\u662f Upgrade \u8fd9\u4e2a\u63a5\u53e3\u6709\u70b9\u7279\u6b8a\uff0c\u5b83\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u5fc5\u987b\u5728EventLoop\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u540c\u65f6\uff0c\u8fd9\u8fd8\u4e0d\u591f\uff0c\u4ed6\u5fc5\u987b\u88ab\u7acb\u9a6c\u6267\u884c\uff0c\u4e0d\u80fd\u653e\u5230\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u56e0\u4e3a\u5982\u679c\u5982\u679c\u653e\u5230\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u90a3\u4e48\u5982\u679c\u6b64\u65f6\u6709\u8bfb\u4e8b\u4ef6\u5230\u6765\uff0c\u90a3\u4e48\u5c31\u4f1a\u5148\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406\uff0c\u4e5f\u5c31\u662f\u4f1a\u5148\u4f7f\u7528\u8001\u7684\u5904\u7406\u51fd\u6570\u8fdb\u884c\u5904\u7406\uff0c\u7136\u540e\u624d\u66f4\u65b0\u56de\u8c03\u65b9\u6cd5\uff0c\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u6211\u4eec\u5fc5\u987b\u5728\u8c03\u7528Upgrade\u7684\u65f6\u5019\u7acb\u5373\u5c06\u534f\u8bae\u7684\u56de\u8c03\u51fd\u6570\u548c\u4e0a\u4e0b\u6587\u8fdb\u884c\u66ff\u6362\u3002\u7136\u540e\u5c31\u662f\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\uff0c\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u6211\u4eec\u53ea\u9700\u8981\u5224\u65ad\u662f\u5426\u542f\u52a8\u4e86\u8d85\u65f6\u8fde\u63a5\uff0c\u5982\u679c\u542f\u52a8\u4e86\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u5237\u65b0\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n","protected":false},"author":2,"featured_media":13614,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,827,43,78],"topic":[],"class_list":["post-13626","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-muduo","tag-43","tag-78"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Muduo\u7f51\u7edc\u5e93\u5b9e\u73b0 [\u4e03] - Connection\u6a21\u5757 - \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\/13626.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Muduo\u7f51\u7edc\u5e93\u5b9e\u73b0 [\u4e03] - Connection\u6a21\u5757 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.2k\u6b21\uff0c\u70b9\u8d5e60\u6b21\uff0c\u6536\u85cf51\u6b21\u3002\u5177\u4f53\u6267\u884c\u7684\u64cd\u4f5c\u5c31\u662f\u8fd9\u6837\u7684\uff0c\u4f46\u662f Upgrade \u8fd9\u4e2a\u63a5\u53e3\u6709\u70b9\u7279\u6b8a\uff0c\u5b83\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u5fc5\u987b\u5728EventLoop\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u540c\u65f6\uff0c\u8fd9\u8fd8\u4e0d\u591f\uff0c\u4ed6\u5fc5\u987b\u88ab\u7acb\u9a6c\u6267\u884c\uff0c\u4e0d\u80fd\u653e\u5230\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u56e0\u4e3a\u5982\u679c\u5982\u679c\u653e\u5230\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u90a3\u4e48\u5982\u679c\u6b64\u65f6\u6709\u8bfb\u4e8b\u4ef6\u5230\u6765\uff0c\u90a3\u4e48\u5c31\u4f1a\u5148\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406\uff0c\u4e5f\u5c31\u662f\u4f1a\u5148\u4f7f\u7528\u8001\u7684\u5904\u7406\u51fd\u6570\u8fdb\u884c\u5904\u7406\uff0c\u7136\u540e\u624d\u66f4\u65b0\u56de\u8c03\u65b9\u6cd5\uff0c\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u6211\u4eec\u5fc5\u987b\u5728\u8c03\u7528Upgrade\u7684\u65f6\u5019\u7acb\u5373\u5c06\u534f\u8bae\u7684\u56de\u8c03\u51fd\u6570\u548c\u4e0a\u4e0b\u6587\u8fdb\u884c\u66ff\u6362\u3002\u7136\u540e\u5c31\u662f\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\uff0c\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u6211\u4eec\u53ea\u9700\u8981\u5224\u65ad\u662f\u5426\u542f\u52a8\u4e86\u8d85\u65f6\u8fde\u63a5\uff0c\u5982\u679c\u542f\u52a8\u4e86\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u5237\u65b0\u5b9a\u65f6\u4efb\u52a1\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/13626.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T12:32:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123244-6802466c69426.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=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/13626.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/13626.html\",\"name\":\"Muduo\u7f51\u7edc\u5e93\u5b9e\u73b0 [\u4e03] - Connection\u6a21\u5757 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T12:32:49+00:00\",\"dateModified\":\"2025-04-18T12:32:49+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/13626.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/13626.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/13626.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Muduo\u7f51\u7edc\u5e93\u5b9e\u73b0 [\u4e03] - Connection\u6a21\u5757\"}]},{\"@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":"Muduo\u7f51\u7edc\u5e93\u5b9e\u73b0 [\u4e03] - Connection\u6a21\u5757 - \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\/13626.html","og_locale":"zh_CN","og_type":"article","og_title":"Muduo\u7f51\u7edc\u5e93\u5b9e\u73b0 [\u4e03] - Connection\u6a21\u5757 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.2k\u6b21\uff0c\u70b9\u8d5e60\u6b21\uff0c\u6536\u85cf51\u6b21\u3002\u5177\u4f53\u6267\u884c\u7684\u64cd\u4f5c\u5c31\u662f\u8fd9\u6837\u7684\uff0c\u4f46\u662f Upgrade \u8fd9\u4e2a\u63a5\u53e3\u6709\u70b9\u7279\u6b8a\uff0c\u5b83\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u5fc5\u987b\u5728EventLoop\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u540c\u65f6\uff0c\u8fd9\u8fd8\u4e0d\u591f\uff0c\u4ed6\u5fc5\u987b\u88ab\u7acb\u9a6c\u6267\u884c\uff0c\u4e0d\u80fd\u653e\u5230\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u56e0\u4e3a\u5982\u679c\u5982\u679c\u653e\u5230\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u90a3\u4e48\u5982\u679c\u6b64\u65f6\u6709\u8bfb\u4e8b\u4ef6\u5230\u6765\uff0c\u90a3\u4e48\u5c31\u4f1a\u5148\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406\uff0c\u4e5f\u5c31\u662f\u4f1a\u5148\u4f7f\u7528\u8001\u7684\u5904\u7406\u51fd\u6570\u8fdb\u884c\u5904\u7406\uff0c\u7136\u540e\u624d\u66f4\u65b0\u56de\u8c03\u65b9\u6cd5\uff0c\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u6211\u4eec\u5fc5\u987b\u5728\u8c03\u7528Upgrade\u7684\u65f6\u5019\u7acb\u5373\u5c06\u534f\u8bae\u7684\u56de\u8c03\u51fd\u6570\u548c\u4e0a\u4e0b\u6587\u8fdb\u884c\u66ff\u6362\u3002\u7136\u540e\u5c31\u662f\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\uff0c\u4efb\u610f\u4e8b\u4ef6\u56de\u8c03\u6211\u4eec\u53ea\u9700\u8981\u5224\u65ad\u662f\u5426\u542f\u52a8\u4e86\u8d85\u65f6\u8fde\u63a5\uff0c\u5982\u679c\u542f\u52a8\u4e86\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u5237\u65b0\u5b9a\u65f6\u4efb\u52a1\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/13626.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T12:32:49+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123244-6802466c69426.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/13626.html","url":"https:\/\/www.wsisp.com\/helps\/13626.html","name":"Muduo\u7f51\u7edc\u5e93\u5b9e\u73b0 [\u4e03] - Connection\u6a21\u5757 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T12:32:49+00:00","dateModified":"2025-04-18T12:32:49+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/13626.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/13626.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/13626.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Muduo\u7f51\u7edc\u5e93\u5b9e\u73b0 [\u4e03] - Connection\u6a21\u5757"}]},{"@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\/13626","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=13626"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/13626\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/13614"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=13626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=13626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=13626"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=13626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}