{"id":67804,"date":"2026-01-29T07:25:50","date_gmt":"2026-01-28T23:25:50","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/67804.html"},"modified":"2026-01-29T07:25:50","modified_gmt":"2026-01-28T23:25:50","slug":"%e4%bb%bfmuduo%e5%ba%93%e5%ae%9e%e7%8e%b0%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%883%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/67804.html","title":{"rendered":"\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668\uff083\uff09"},"content":{"rendered":"<h2>LoopThread\u6a21\u5757<\/h2>\n<h3>\u76ee\u7684<\/h3>\n<p>\u5c06EventLoop\u6a21\u5757\u4e0e\u7ebf\u7a0b\u6574\u5408\u4e00\u8d77&#xff0c;\u8ba9EventLoop\u6a21\u5757\u4e0e\u7ebf\u7a0b\u662f\u4e00\u4e00\u5bf9\u5e94\u7684&#xff0c;\u8fd9\u91cc\u5c31\u5ef6\u4f38\u51fa\u4e00\u4e2a\u95ee\u9898&#xff1a;<span style=\"background-color:#fbd4d0\">\u662f\u8981\u5148\u5728\u4e3b\u7ebf\u7a0b\u521b\u5efaEventLoop\u5bf9\u8c61&#xff0c;\u518d\u5206\u914d\u7ed9\u5b50\u7ebf\u7a0b\u8fd8\u662f\u5148\u521b\u5efa\u5b50\u7ebf\u7a0b&#xff0c;\u518d\u5728\u5b50\u7ebf\u7a0b\u91cc\u521b\u5efaEventLoop\u5bf9\u8c61&#xff1f;<\/span><\/p>\n<h3>\u542b\u4e49<\/h3>\n<p>\u5728\u56de\u7b54\u4e0a\u9762\u7684\u7591\u95ee\u524d\u8981\u5148\u6e05\u695a&#xff1a;\u5728eventloop\u91cc\u8fdb\u884c\u4e00\u4e2a\u64cd\u4f5c\u65f6&#xff0c;\u5f80\u5f80\u4f1a\u6d89\u53ca\u5224\u65ad\u5f53\u524d\u64cd\u4f5c\u662f\u5426\u5728eventloop\u6a21\u5757\u5bf9\u5e94\u7684\u7ebf\u7a0b\u4e2d\u8fd0\u884c&#xff08;\u5c06\u5f53\u524d\u7ebf\u7a0bID\u548ceventloop\u7684\u6210\u5458_thread_id\u4f5c\u6bd4\u8f83&#xff0c;\u76f8\u540c\u5c31\u8868\u793a\u5728\u540c\u4e00\u7ebf\u7a0b\u53ef\u4ee5\u88ab\u7406\u89e3\u5904\u7406&#xff0c;\u4e0d\u540c\u5c31\u8868\u793a\u5f53\u524d\u7ebf\u7a0b\u5e76\u4e0d\u662f\u4e0eeventloop\u5bf9\u8c61\u7ed1\u5b9a\u7684\u90a3\u4e2a\u7ebf\u7a0b&#xff0c;\u5c31\u9700\u8981\u628a\u8fd9\u4e2a\u64cd\u4f5c\u538b\u5165eventloop\u5bf9\u8c61\u4efb\u52a1\u961f\u5217&#xff09;&#xff1b;<br \/>\nEventloop\u5b9e\u4f8b\u5316\u5bf9\u8c61&#xff0c;\u5728\u6784\u9020\u65f6\u5c31\u4f1a\u521d\u59cb\u5316_thread_id&#xff0c;\u8fd9\u4e2aid\u5c31\u662f\u5f53\u524d\u7ebf\u7a0bid\u3002<\/p>\n<p>\u5148\u521b\u5efaeventloop\u5bf9\u8c61\u518d\u521b\u5efa\u5b50\u7ebf\u7a0b&#xff0c;\u5c06\u5b50\u7ebf\u7a0b\u7684\u7ebf\u7a0bid\u518d\u8d4b\u503c\u7ed9eventloop\u7684_thread_id&#xff0c;\u8fd9\u4e2a\u671f\u95f4\u5c06\u662f\u4e0d\u53ef\u63a7\u7684&#xff1a;\u5f88\u53ef\u80fd\u51fa\u73b0<br \/>\n&#xff08;1&#xff09;\u521b\u5efaeventloop\u5bf9\u8c61\u65f6_thread_id\u4e3a\u4e3b\u7ebf\u7a0bid&#xff0c;\u5e76\u4e14\u8c03\u7528\u4e86_loop-&gt;RunInLoop(task)&#xff0c;\u6b64\u65f6\u5f53\u524d\u7ebf\u7a0bid\u7b49\u4e8e_thread_id&#xff0c;\u4efb\u52a1task\u76f4\u63a5\u6267\u884c&#xff1b;<br \/>\n&#xff08;2&#xff09;\u63a5\u7740\u521b\u5efa\u5b50\u7ebf\u7a0b\u5c06\u5b50\u7ebf\u7a0bid\u8d4b\u503c\u7ed9\u4e86_thread_id&#xff0c;\u5b50\u7ebf\u7a0b\u5f00\u59cb\u8fd0\u884c\u8bfb\u53d6Channel\u5217\u8868\u67e5\u770b\u89e6\u53d1\u4ec0\u4e48\u4e8b\u4ef6&#xff0c;\u800c\u521a\u521a\u7684\u4efb\u52a1\u8981\u662f\u6ca1\u6709\u8dd1\u5b8c\u5e76\u4e14\u5c1d\u8bd5\u53bb\u4fee\u6539Channel\u5217\u8868&#xff0c;\u5c31\u53d8\u6210\u4e86\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5bf9\u540c\u4e00\u5757\u8fdb\u884c\u64cd\u4f5c&#xff0c;\u4e00\u4e2a\u8fdb\u884c\u201c\u5199\u64cd\u4f5c\u201d\u4e00\u4e2a\u8fdb\u884c\u201c\u8bfb\u64cd\u4f5c\u201d&#xff0c;\u7a0b\u5e8f\u4f1a\u76f4\u63a5\u5d29\u6e83\u3002<\/p>\n<p>\u6240\u4ee5LoopThread\u6a21\u5757\u5c31\u8981\u6c42&#xff0c;<span style=\"background-color:#fbd4d0\">eventloop\u5728\u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6\u5fc5\u987b\u5728\u7ebf\u7a0b\u5185\u90e8&#xff0c;\u5c31\u662f\u8981\u5148\u521b\u5efa\u7ebf\u7a0b&#xff0c;\u7136\u540e\u5728\u7ebf\u7a0b\u7684\u5165\u53e3\u51fd\u6570\u53bb\u5b9e\u4f8b\u5316eventloop\u5bf9\u8c61&#xff0c;\u4e0d\u8981\u51fa\u73b0\u5bf9_thread_id\u91cd\u65b0\u8d4b\u503c\u7684\u60c5\u51b5\u3002<\/span><\/p>\n<h3>\u8bbe\u8ba1\u601d\u60f3<\/h3>\n<li>\u521b\u5efa\u7ebf\u7a0b<\/li>\n<li>\u5728\u7ebf\u7a0b\u4e2d\u5b9e\u4f8b\u5316eventloop \u5bf9\u8c61<\/li>\n<li>\u53ef\u4ee5\u5411\u5916\u90e8\u8fd4\u56de\u5b9e\u4f8b\u5316\u540e\u7684eventloop&#xff1a;\u7ed9connection\u521d\u59cb\u5316\u4f5c\u4e3a\u53c2\u6570\u4f7f\u7528<\/li>\n<h3>\u4ee3\u7801\u8bbe\u8ba1<\/h3>\n<p>\/\/ \u8ba9eventloop\u4e0e\u7ebf\u7a0b\u4e00\u4e00\u5bf9\u5e94<br \/>\nclass EventLoopThread<br \/>\n{<br \/>\nprivate:<br \/>\n    std::mutex _mutex; \/\/ \u4e92\u65a5\u9501<br \/>\n    std::condition_variable _cond; \/\/\u6761\u4ef6\u53d8\u91cf<br \/>\n    EventLoop *_loop;    \/\/ EventLoop\u9700\u5728\u7ebf\u7a0b\u5185\u90e8\u5b9e\u4f8b\u5316<br \/>\n    std::thread _thread; \/\/ EventLoop \u5bf9\u5e94\u7684\u7ebf\u7a0b<br \/>\nprivate:<br \/>\n    \/\/\u5b9e\u4f8b\u5316\u5bf9\u8c61&#xff0c;\u5524\u9192_cond\u4e0a\u6709\u53ef\u80fd\u963b\u585e\u7684\u7ebf\u7a0b&#xff0c;\u8fd0\u884cloop\u6a21\u5757\u7684\u529f\u80fd<br \/>\n    void ThreadEntry()<br \/>\n    {<br \/>\n        EventLoop loop;<br \/>\n        {<br \/>\n            std::unique_lock&lt;std::mutex&gt; _lock(_mutex);\/\/\u4fdd\u62a4_loop<br \/>\n            _loop &#061; &amp;loop;<br \/>\n            _cond.notify_all();<br \/>\n        }<br \/>\n        loop.Start();<br \/>\n    }<\/p>\n<p>public:<br \/>\n    \/\/\u521b\u5efa\u7ebf\u7a0b&#xff0c;\u6267\u884c\u7ebf\u7a0b\u5165\u53e3\u51fd\u6570ThreadEntry()<br \/>\n    EventLoopThread() : _loop(nullptr), _thread(&amp;EventLoopThread::ThreadEntry, this){}<br \/>\n    \/\/\u8fd4\u56de\u5f53\u524d\u7ebf\u7a0b\u5173\u8054\u7684eventloop\u6307\u9488<br \/>\n    EventLoop *GetLoop()<br \/>\n    {<br \/>\n        if (_loop &#061;&#061; nullptr)<br \/>\n        {<br \/>\n            \/\/\u907f\u514d_loop\u8fd8\u6ca1\u521d\u59cb\u5316\u5c31\u88ab\u83b7\u53d6<br \/>\n            std::unique_lock&lt;std::mutex&gt; _lock(_mutex); \/\/wait\u9700\u8981lock\u914d\u5957\u4f7f\u7528<br \/>\n            _cond.wait(_lock, [&amp;](){ return _loop !&#061; nullptr; });\/\/_loop\u4e3a\u7a7a\u5c31\u4e00\u76f4\u963b\u585e<br \/>\n        }<br \/>\n        return _loop;<br \/>\n    }<br \/>\n};<\/p>\n<p>\u4e0b\u9762\u5bf9tcp_srv.cpp\u8fdb\u4fee\u6539&#xff0c;\u4e3b\u8981\u662f\u7ed9\u4e0d\u540c\u7684\u7ebf\u7a0b\u5206\u914d\u4e0d\u540c\u7684\u4efb\u52a1&#xff1a;\u4e00\u4e2a\u8d1f\u8d23\u76d1\u542c\u90e8\u5206\u4e00\u4e2a\u8d1f\u8d23\u901a\u4fe1\u90e8\u5206&#xff0c;\u5b9e\u73b0\u6548\u7387\u63d0\u5347<\/p>\n<p>#include &#034;..\/source\/server.hpp&#034;<\/p>\n<p>uint64_t conn_id &#061; 0;<br \/>\nstd::unordered_map&lt;uint64_t, PtrConnection&gt; _conns;<br \/>\nEventLoop loop;<br \/>\nstd::vector&lt;EventLoopThread&gt; _loop_threads(2);<br \/>\nint next_loop &#061; 0;<br \/>\nvoid ConnectionDestroy(const PtrConnection&amp; ptr)<br \/>\n{<br \/>\n    _conns.erase(ptr-&gt;Id());<br \/>\n}<br \/>\nvoid OnConnected(const PtrConnection&amp; ptr)<br \/>\n{<br \/>\n    DBG_LOG(&#034;New Connection: %p&#034;, ptr.get());<br \/>\n}<br \/>\nvoid OnMessage(const PtrConnection&amp; ptr, Buffer* buf)<br \/>\n{<br \/>\n    DBG_LOG(&#034;%s&#034;, buf-&gt;ReadPosition());<br \/>\n    buf-&gt;MoveReadOffset(buf-&gt;ReadAbleSize());<br \/>\n    std::string s &#061; &#034;hello, bugubugu!&#034;;<br \/>\n    ptr-&gt;Send(s.c_str(), s.size());<br \/>\n    ptr-&gt;Shutdown();<br \/>\n}<br \/>\nvoid HandleNewConnection(int newfd)<br \/>\n{<br \/>\n    conn_id&#043;&#043;;<br \/>\n    next_loop &#061; (next_loop &#043; 1) % 2;<br \/>\n    PtrConnection ptr_con(new Connection(conn_id, newfd, _loop_threads[next_loop].GetLoop()));<br \/>\n    _conns.insert(std::make_pair(conn_id, ptr_con));<br \/>\n    ptr_con-&gt;SetConnectedCallback(std::bind(OnConnected, std::placeholders::_1));\/\/\u53c2\u6570\u7531connection\u5185\u90e8\u63d0\u4f9b<br \/>\n    ptr_con-&gt;SetMessageCallback(std::bind(OnMessage, std::placeholders::_1, std::placeholders::_2));<br \/>\n    ptr_con-&gt;SetServerClosedCallback(std::bind(ConnectionDestroy, std::placeholders::_1));<br \/>\n    \/\/10s\u540e\u5bf9\u975e\u6d3b\u8dc3\u8fde\u63a5\u8fdb\u884c\u91ca\u653e<br \/>\n    \/\/\u91ca\u653e\u4efb\u52a1\u7684\u6dfb\u52a0\u8981\u653e\u5728\u76d1\u63a7\u4e4b\u524d&#xff0c;\u5982\u679c\u5148\u5f00\u542f\u76d1\u63a7&#xff0c;\u7acb\u5373\u6709\u4e8b\u4ef6&#xff0c;\u5728\u5237\u65b0\u91ca\u653e\u4efb\u52a1\u91cc\u627e\u4e0d\u5230\u8fd9\u4e2a\u4efb\u52a1<br \/>\n    ptr_con-&gt;EnableInactiveRelease(10);<br \/>\n    ptr_con-&gt;Established();<br \/>\n    DBG_LOG(&#034;\u662f\u5426\u4e3b\u7ebf\u7a0b\u8fd0\u884c&#xff1f;&#xff1f;&#xff1f;&#xff1f;&#034;);<br \/>\n}<br \/>\nint main()<br \/>\n{<br \/>\n    Acceptor acceptor(&amp;loop, 8080);<br \/>\n    acceptor.SetAcceptCallback(std::bind(HandleNewConnection, std::placeholders::_1));<br \/>\n    acceptor.Listen();<br \/>\n    while(1)<br \/>\n    {<br \/>\n        loop.Start();<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p style=\"margin-left:0;margin-right:0\">\u4e3b\u4ecereactor\u7684\u4f5c\u7528&#xff1a;<span style=\"background-color:#e5b9b7\">\u4e3b\u7ebf\u7a0b\u7684<\/span><span style=\"background-color:#e5b9b7\">eventloop<\/span><span style=\"background-color:#e5b9b7\">\u8d1f\u8d23\u76d1\u542c\u65b0\u8fde\u63a5<\/span>&#xff0c;\u4ee5\u53ca\u8c03\u7528\u5bf9\u65b0\u8fde\u63a5\u7684\u5904\u7406\u51fd\u6570<br \/>\n<span style=\"color:#569cd6\">void <\/span><span style=\"color:#989875\">HandleNewConnection<\/span><span style=\"color:#979797\">(<\/span><span style=\"color:#569cd6\">int <\/span><span style=\"color:#72a1ba\">newfd<\/span><span style=\"color:#979797\">)<\/span> <span style=\"color:#979797\">&#xff0c;<\/span>\u5c06\u5176\u5206\u53d1\u7ed9\u4ece\u5c5e\u7ebf\u7a0b&#xff1a;Connection \u5bf9\u8c61\u5728\u6784\u9020\u65f6\u7ed1\u5b9a\u7684 _loop \u6307\u9488\u6307\u5411\u7684\u662f\u901a\u8fc7 <span style=\"background-color:#e5b9b7\">threads[next_loop]<\/span><span style=\"background-color:#e5b9b7\">.<\/span><span style=\"background-color:#e5b9b7\">GetLoop&#xff08;&#xff09;<\/span> \u83b7\u53d6\u7684\u4ece\u5c5e\u7ebf\u7a0b EventLoop\u3002\u8fd9\u4e00\u6b65\u5c31\u5c06\u65b0\u8fde\u63a5connection\u548c\u4ece\u5c5e\u7ebf\u7a0b EventLoop\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002<br \/>\n\u540e\u9762<span style=\"background-color:#fbd4d0\">\u4ece<\/span><span style=\"background-color:#e5b9b7\">\u5c5e\u7ebf\u7a0b EventLoop\u5c31\u53ea\u8d1f\u8d23\u901a\u4fe1\u7684\u76d1\u542c\u4e8b\u4ef6\u3001\u5bf9\u89e6\u53d1\u4e8b\u4ef6\u7684\u5904\u7406\u4ee5\u53ca\u4e1a\u52a1\u5904\u7406<\/span>&#xff0c;\u4e0d\u4f1a\u51fa\u73b0\u4e00\u4e2a\u7ebf\u7a0b\u4e00\u76f4\u5728\u5904\u7406\u4e1a\u52a1\u800c\u65e0\u6cd5\u53ca\u65f6\u5904\u7406\u65b0\u8fde\u63a5\u7684\u4ea7\u751f&#xff0c;\u6548\u7387\u63d0\u9ad8\u3002<\/p>\n<h2 style=\"margin-left:0px;margin-right:0px\">LoopThreadPool\u6a21\u5757<\/h2>\n<h3>\u76ee\u7684<\/h3>\n<p>\u5bf9\u6240\u6709\u7684LoopThread \u8fdb\u884c\u7ba1\u7406\u548c\u5206\u914d<\/p>\n<h3>\u529f\u80fd<\/h3>\n<p>1\u3001\u7ebf\u7a0b\u6570\u91cf\u53ef\u914d\u7f6e&#xff08;0\u4e2a\u6216\u591a\u4e2a&#xff09;&#xff1a;\u4e0a\u9762\u5df2\u7ecf\u8bf4\u660e\u4e86\u4e3b\u4eceReactor \u6a21\u578b&#xff1a;\u4e3b\u7ebf\u7a0b\u53ea\u8d1f\u8d23\u65b0\u8fde\u63a5\u83b7\u53d6&#xff0c;\u4ece\u5c5e\u7ebf\u7a0b\u8d1f\u8d23\u65b0\u8fde\u63a5\u7684\u4e8b\u4ef6\u76d1\u63a7\u4ee5\u53ca\u5904\u7406\u3002\u4e5f\u5b58\u5728\u4e00\u79cd\u60c5\u51b5&#xff1a;\u4ece\u5c5e\u7ebf\u7a0b\u6570\u91cf\u4e3a0&#xff0c;\u90a3\u4e48\u4e3b\u7ebf\u7a0b\u65e2\u8d1f\u8d23\u65b0\u8fde\u63a5\u83b7\u53d6\u3001\u4e5f\u8d1f\u8d23\u65b0\u8fde\u63a5\u7684\u4e8b\u4ef6\u76d1\u63a7\u4ee5\u53ca\u5904\u7406&#xff0c;\u76f8\u5f53\u4e8e\u5b9e\u73b0\u5355Reactor\u670d\u52a1\u5668<\/p>\n<p>2\u3001\u5bf9\u6240\u6709\u7ebf\u7a0b\u8fdb\u884c\u7ba1\u7406&#xff1a;\u7ba1\u74060\u4e2a\u6216\u8005\u591a\u4e2aLoopThread\u5bf9\u8c61<\/p>\n<p>3\u3001\u63d0\u4f9b\u7ebf\u7a0b\u5206\u914d\u7684\u529f\u80fd&#xff1a;\u5f53\u4e3b\u7ebf\u7a0b\u83b7\u53d6\u65b0\u8fde\u63a5&#xff0c;\u9700\u8981\u5c06\u8fde\u63a5\u6302\u5230\u4ece\u5c5e\u7ebf\u7a0b\u4e0a\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u4ee5\u53ca\u5904\u7406&#xff0c;\u5c31\u662f\u5c06\u7ebf\u7a0b\u4e00\u4e00\u5bf9\u5e94\u7684eventloop\u8fd4\u56de&#xff0c;\u4f9bConnection\u521d\u59cb\u5316\u3002\u8fd9\u91cc\u5b58\u5728\u4e24\u79cd\u60c5\u51b5&#xff1a;<br \/>\n&#xff08;1&#xff09;\u4ece\u5c5e\u7ebf\u7a0b\u6570\u91cf\u4e3a0&#xff0c;\u5c06\u8fde\u63a5\u76f4\u63a5\u5206\u914d\u7ed9\u4e3b\u7ebf\u7a0b\u7684EventLoop<br \/>\n&#xff08;2&#xff09;\u4ece\u5c5e\u7ebf\u7a0b\u6570\u91cf\u6709\u591a\u4e2a&#xff0c;\u91c7\u7528RR\u8f6e\u8f6c\u601d\u60f3&#xff0c;\u8fdb\u884c\u7ebf\u7a0b\u5206\u914d\u3002<\/p>\n<h3>\u4ee3\u7801\u8bbe\u8ba1<\/h3>\n<p>\/\/ \u7ba1\u7406EventLoopThread<br \/>\nclass LoopThreadPool<br \/>\n{<br \/>\nprivate:<br \/>\n    int _thread_count; \/\/ \u4ece\u5c5e\u7ebf\u7a0b\u7684\u6570\u91cf<br \/>\n    int _next_loop;<br \/>\n    EventLoop *_baseloop;                    \/\/ \u5728\u4e3b\u7ebf\u7a0b\u8fd0\u884c<br \/>\n    std::vector&lt;EventLoopThread *&gt; _threads; \/\/ \u4fdd\u5b58\u6240\u6709\u7684\u4ece\u5c5e\u7ebf\u7a0b\u4fe1\u606f<br \/>\n    std::vector&lt;EventLoop *&gt; _loops;         \/\/ \u4ece\u5c5e\u7ebf\u7a0b\u6570\u91cf\u5927\u4e8e0\u624d\u8fdb\u884ceventloop\u7684\u5206\u914d<br \/>\npublic:<br \/>\n    LoopThreadPool(EventLoop *baseloop) : _baseloop(baseloop), _next_loop(0), _thread_count(0) {}<br \/>\n    \/\/ \u7ebf\u7a0b\u6570\u91cf\u53ef\u914d\u7f6e<br \/>\n    void SetThreadCount(int count) { _thread_count &#061; count; }<br \/>\n    \/\/ \u521b\u5efa\u6240\u6709\u7684\u4ece\u5c5e\u7ebf\u7a0b<br \/>\n    void CreateChlids()<br \/>\n    {<br \/>\n        if (_thread_count &gt; 0)<br \/>\n        {<br \/>\n            _threads.resize(_thread_count);<br \/>\n            _loops.resize(_thread_count);<br \/>\n            for (int i &#061; 0; i &lt; _thread_count; i&#043;&#043;)<br \/>\n            {<br \/>\n                _threads[i] &#061; new EventLoopThread;<br \/>\n                _loops[i] &#061; _threads[i]-&gt;GetLoop();<br \/>\n            }<br \/>\n        }<br \/>\n        return;<br \/>\n    }<br \/>\n    \/\/ \u8fdb\u884ceventloop\u7684\u5206\u914d<br \/>\n    EventLoop *NextLoop()<br \/>\n    {<br \/>\n        if (_thread_count &#061;&#061; 0)<br \/>\n            return _baseloop;<br \/>\n        _next_loop &#061; (_next_loop &#043; 1) % _thread_count;<br \/>\n        return _loops[_next_loop];<br \/>\n    }<br \/>\n};<\/p>\n<h2>TcpServer\u6a21\u5757<\/h2>\n<h3>\u542b\u4e49<\/h3>\n<p>\u662f\u524d\u9762\u6240\u6709\u6a21\u5757\u7684\u6574\u5408&#xff0c;\u901a\u8fc7TcpServer\u7c7b\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61\u76f4\u63a5\u5b8c\u6210\u4e00\u4e2a\u670d\u52a1\u5668\u7684\u642d\u5efa<\/p>\n<p>\u4e0b\u9762\u6309\u7167\u524d\u9762\u7684tcp_srv.cc\u7684\u601d\u8def\u6d41\u7a0b&#xff0c;\u4e0d\u96be\u5bf9TcpServer\u7c7b\u8fdb\u884c\u58f0\u660e<\/p>\n<h3>\u7ba1\u7406<\/h3>\n<p>1\u3001\u901a\u8fc7Accpetor\u5bf9\u8c61&#xff0c;\u521b\u5efa\u4e00\u4e2a\u76d1\u542c\u5957\u63a5\u5b57<br \/>\n2\u3001\u521b\u5efa\u4e00\u4e2aEventLoop\u5bf9\u8c61&#8211;&gt;baseloop\u53d8\u91cf&#xff0c;\u5b9e\u73b0\u5bf9\u76d1\u542c\u5957\u63a5\u5b57\u7684\u4e8b\u4ef6\u76d1\u63a7\u53ca\u5904\u7406<br \/>\n3\u3001std::unordered_map&lt;uint64_t, PtrConnection&gt; _conns \u53ef\u4ee5\u5b9e\u73b0\u5bf9\u6240\u6709\u65b0\u5efa\u8fde\u63a5\u7684\u7ba1\u7406<br \/>\n4\u3001\u521b\u5efa\u4e00\u4e2aLoopThreadPool\u5bf9\u8c61&#8211;&gt;loop_pool\u7ebf\u7a0b\u6c60&#xff0c;\u901a\u8fc7\u5206\u914deventloop\u5bf9\u65b0\u5efa\u8fde\u63a5\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u53ca\u5904\u7406<\/p>\n<h3>\u529f\u80fd<\/h3>\n<p>1\u3001\u8bbe\u7f6e\u4ece\u5c5e\u7ebf\u7a0b\u6570\u91cf<br \/>\n2\u3001\u8bbe\u7f6e\u5404\u79cd\u56de\u8c03\u51fd\u6570&#xff08;\u5305\u62ec\u8fde\u63a5\u5efa\u7acb\u5b8c\u6210\u3001\u6536\u5230\u6d88\u606f\u3001\u5173\u95ed\u8fde\u63a5\u3001\u4efb\u610f\u4e8b\u4ef6&#xff09;&#xff1a;\u7528\u6237\/\u7ec4\u4ef6\u4f7f\u7528\u8005\u5148\u8bbe\u7f6e\u7ed9TcpServer&#xff0c;TcpServer\u8bbe\u7f6e\u7ed9\u83b7\u53d6\u7684\u65b0\u8fde\u63a5&#xff08;Connection&#xff09;<br \/>\n3\u3001\u662f\u5426\u542f\u52a8\u975e\u6d3b\u8dc3\u8fde\u63a5\u8d85\u65f6\u9500\u6bc1\u529f\u80fd<br \/>\n4\u3001\u6dfb\u52a0\u5b9a\u65f6\u4efb\u52a1&#xff08;\u53ef\u4ee5\u4e0d\u4f7f\u7528&#xff0c;\u6709\u7528\u6237\u51b3\u5b9a&#xff1b;\u8fd9\u4e2a\u5b9a\u65f6\u4efb\u52a1\u662f\u5728baseloop\u5bf9\u5e94\u7ebf\u7a0b\u6267\u884c&#xff0c;\u800c\u975e\u6d3b\u8dc3\u8fde\u63a5\u8d85\u65f6\u9500\u6bc1\u4efb\u52a1\u4e00\u822c\u662f\u5728\u4ece\u5c5eloop\u5bf9\u5e94\u7ebf\u7a0b\u6267\u884c&#xff09;<br \/>\n5\u3001\u542f\u52a8\u670d\u52a1\u5668<br \/>\n\u6ce8\u610f\u4e0a\u9762\u7684\u529f\u80fd\u662f\u63d0\u4f9b\u7ed9\u7528\u6237\u4f7f\u7528&#xff0c;\u800c\u8fd8\u6709\u4e00\u4e9b\u529f\u80fd\u662f\u88ab\u5305\u542b\u5728\u4e0a\u9762\u7684\u529f\u80fd\u4e2d\u7684&#xff0c;\u6bd4\u5982\u5bf9\u83b7\u53d6\u7684\u65b0\u8fde\u63a5\u5904\u7406\u65b9\u6cd5\u3001\u6dfb\u52a0\/\u79fb\u9664\u65b0\u8fde\u63a5\u7684\u7ba1\u7406\u4fe1\u606f\u7b49\u7b49\u3002<\/p>\n<h3>\u4ee3\u7801\u8bbe\u8ba1<\/h3>\n<p>class TcpServer<br \/>\n{<br \/>\nprivate:<br \/>\n    EventLoop _baseloop; \/\/ \u4e3b\u7ebf\u7a0b\u7684EventLoop&#xff0c;\u8d1f\u8d23\u76d1\u542c\u8fde\u63a5\u4ee5\u53ca\u5904\u7406<br \/>\n    int _port;<br \/>\n    Acceptor _acceptor;            \/\/ \u76d1\u542c\u5957\u63a5\u5b57\u7684\u7ba1\u7406\u5bf9\u8c61<br \/>\n    bool _enable_inactive_release; \/\/ \u662f\u5426\u542f\u52a8\u975e\u6d3b\u8dc3\u8fde\u63a5\u8d85\u65f6\u9500\u6bc1\u7684\u5224\u65ad\u6807\u5fd7<br \/>\n    int _timeout;                  \/\/ \u975e\u6d3b\u8dc3\u8fde\u63a5\u7684\u7edf\u8ba1\u65f6\u95f4&#xff0c;\u5373\u591a\u957f\u65f6\u95f4\u65e0\u901a\u4fe1\u5c31\u662f\u975e\u6d3b\u8dc3\u8fde\u63a5<br \/>\n    LoopThreadPool _pool;          \/\/ \u4ece\u5c5e\u7ebf\u7a0b\u6c60<br \/>\n    uint64_t _next_id;             \/\/ \u81ea\u52a8\u589e\u957fid&#xff0c;\u7528\u4e8e\u5b9a\u65f6\u4efb\u52a1id\u3001connection\u5bf9\u8c61(connection\u5bf9\u8c61\u91cc\u7684\u8d85\u65f6\u9500\u6bc1\u4efb\u52a1id)<br \/>\n    std::unordered_map&lt;uint64_t, PtrConnection&gt; _conns;<\/p>\n<p>    using ConnectedCallback &#061; std::function&lt;void(const PtrConnection &amp;)&gt;;<br \/>\n    using MessageCallback &#061; std::function&lt;void(const PtrConnection &amp;, Buffer *)&gt;;<br \/>\n    using ClosedCallback &#061; std::function&lt;void(const PtrConnection &amp;)&gt;;<br \/>\n    using AnyEventCallback &#061; std::function&lt;void(const PtrConnection &amp;)&gt;;<br \/>\n    ConnectedCallback _connected_callback;<br \/>\n    MessageCallback _message_callback;<br \/>\n    ClosedCallback _closed_callback;<br \/>\n    AnyEventCallback _anyevent_callback;<br \/>\n    using Functor &#061; std::function&lt;void()&gt;;<br \/>\nprivate:<br \/>\n    void RemoveConnectionInLoop(const PtrConnection &amp;ptr)<br \/>\n    {<br \/>\n        auto it &#061; _conns.find(ptr-&gt;Id());<br \/>\n        if(it !&#061; _conns.end()) _conns.erase(it);<br \/>\n    }<br \/>\n    \/\/\u4ece\u7ba1\u7406connection\u7684_conns\u4e2d\u79fb\u9664\u8fde\u63a5\u4fe1\u606f<br \/>\n    void RemoveConnection(const PtrConnection &amp;ptr)<br \/>\n    {<br \/>\n        _baseloop.RunInLoop(std::bind(&amp;TcpServer::RemoveConnectionInLoop, this, ptr));<br \/>\n    }<br \/>\n    \/\/\u4e3a\u65b0\u8fde\u63a5\u6784\u9020\u4e00\u4e2aconnection\u8fdb\u884c\u7ba1\u7406<br \/>\n    void HandleNewConnection(int newfd)<br \/>\n    {<br \/>\n        _next_id&#043;&#043;;<br \/>\n        PtrConnection ptr_con(new Connection(_next_id, newfd, _pool.NextLoop()));<br \/>\n        _conns.insert(std::make_pair(_next_id, ptr_con));<br \/>\n        ptr_con-&gt;SetConnectedCallback(_connected_callback); \/\/ \u53c2\u6570\u7531connection\u5185\u90e8\u63d0\u4f9b<br \/>\n        ptr_con-&gt;SetMessageCallback(_message_callback);<br \/>\n        ptr_con-&gt;SetAnyEventCallback(_anyevent_callback);<br \/>\n        ptr_con-&gt;SetClosedCallback(_closed_callback);<br \/>\n        ptr_con-&gt;SetServerClosedCallback(std::bind(&amp;TcpServer::RemoveConnection, this, std::placeholders::_1));<br \/>\n        \/\/ _timeout s\u540e\u5bf9\u975e\u6d3b\u8dc3\u8fde\u63a5\u8fdb\u884c\u91ca\u653e<br \/>\n        \/\/ \u91ca\u653e\u4efb\u52a1\u7684\u6dfb\u52a0\u8981\u653e\u5728\u76d1\u63a7\u4e4b\u524d&#xff0c;\u5982\u679c\u5148\u5f00\u542f\u76d1\u63a7&#xff0c;\u7acb\u5373\u6709\u4e8b\u4ef6&#xff0c;\u5728\u5237\u65b0\u91ca\u653e\u4efb\u52a1\u91cc\u627e\u4e0d\u5230\u8fd9\u4e2a\u4efb\u52a1<br \/>\n        if(_enable_inactive_release &#061;&#061; true) ptr_con-&gt;EnableInactiveRelease(_timeout);<br \/>\n        ptr_con-&gt;Established();<br \/>\n        DBG_LOG(&#034;\u662f\u5426\u4e3b\u7ebf\u7a0b\u8fd0\u884c&#xff1f;&#xff1f;&#xff1f;&#xff1f;&#034;);<br \/>\n    }<br \/>\n    void RunAfterInLoop(const Functor &amp; task, int delay)<br \/>\n    {<br \/>\n        _next_id&#043;&#043;;<br \/>\n        _baseloop.TimerAdd(_next_id, delay, task);<br \/>\n    }<br \/>\npublic:<br \/>\n    TcpServer(int port) : _port(port),<br \/>\n                          _acceptor(&amp;_baseloop, port),<br \/>\n                          _enable_inactive_release(false),<br \/>\n                          _next_id(0),<br \/>\n                          _pool(&amp;_baseloop)<br \/>\n    {<br \/>\n        _acceptor.SetAcceptCallback(std::bind(&amp;TcpServer::HandleNewConnection, this,std::placeholders::_1));<br \/>\n        _acceptor.Listen();<br \/>\n    }<br \/>\n    void SetThreadCount(int count)<br \/>\n    {<br \/>\n        _pool.SetThreadCount(count);<br \/>\n    }<br \/>\n    void SetConnectedCallback(const ConnectedCallback &amp;cb) { _connected_callback &#061; cb; }<br \/>\n    void SetMessageCallback(const MessageCallback &amp;cb) { _message_callback &#061; cb; }<br \/>\n    void SetClosedCallback(const ClosedCallback &amp;cb) { _closed_callback &#061; cb; }<br \/>\n    void SetAnyEventCallback(const AnyEventCallback &amp;cb) { _anyevent_callback &#061; cb; }<br \/>\n    void EnableInactiveRelease(int timeout)<br \/>\n    {<br \/>\n        _enable_inactive_release &#061; true;<br \/>\n        _timeout &#061; timeout;<br \/>\n    }<br \/>\n    \/\/\u5728baseloop\u6dfb\u52a0\u5b9a\u65f6\u4efb\u52a1<br \/>\n    void RunAfter(const Functor &amp; task, int delay)<br \/>\n    {<br \/>\n        _baseloop.RunInLoop(std::bind(&amp;TcpServer::RunAfterInLoop, this, task, delay));<br \/>\n    }<br \/>\n    void Start()<br \/>\n    {<br \/>\n        _pool.CreateChlids();<br \/>\n        _baseloop.Start();<br \/>\n    }<br \/>\n};<\/p>\n<h3>\u5b9e\u73b0\u6d41\u7a0b<\/h3>\n<li>\u5728TcpServer\u4e2d\u5b9e\u4f8b\u5316\u4e00\u4e2aAcceptor\u5bf9\u8c61\u3001\u4e00\u4e2aEventLoop\u5bf9\u8c61&#xff08;baseloop&#xff09;\u5e76\u7ed9Acceptor\u8bbe\u7f6e\u53ef\u8bfb\u56de\u8c03\u51fd\u6570<\/li>\n<li>\u5c06Acceptor \u6302\u5230baseloop\u4e0a\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7<\/li>\n<li>\u4e00\u65e6Acceptor\u5bf9\u8c61\u5c31\u7eea\u53ef\u8bfb\u4e8b\u4ef6&#xff0c;\u6267\u884c\u53ef\u8bfb\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570\u83b7\u53d6\u65b0\u8fde\u63a5<\/li>\n<li>\u5bf9\u65b0\u8fde\u63a5\u521b\u5efa\u4e00\u4e2aConnection\u8fdb\u884c\u7ba1\u7406&#xff0c;\u8bbe\u7f6e\u529f\u80fd\u56de\u8c03\u51fd\u6570&#xff08;\u8fde\u63a5\u5b8c\u6210\u3001\u6d88\u606f\u83b7\u53d6\u3001\u8fde\u63a5\u5173\u95ed\u3001\u4efb\u610f\u4e8b\u4ef6&#xff09;<\/li>\n<li>\u542f\u52a8Connection\u7684\u975e\u6d3b\u8dc3\u8fde\u63a5\u8d85\u65f6\u9500\u6bc1\u4efb\u52a1<\/li>\n<li>\u5c06\u65b0\u8fde\u63a5\u5bf9\u5e94\u7684Connection\u6302\u5230LoopThreadPool\u4e2d\u7684\u4ece\u5c5e\u7ebf\u7a0b\u5bf9\u5e94\u7684EventLoop\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7<\/li>\n<li>\u4e00\u65e6Connection\u5bf9\u8c61\u5c31\u7eea\u53ef\u8bfb\u4e8b\u4ef6&#xff0c;\u6267\u884c\u53ef\u8bfb\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570&#xff0c;\u8bfb\u53d6\u6570\u636e&#xff0c;\u8bfb\u53d6\u6570\u636e\u540e\u8c03\u7528TcpServer\u8bbe\u7f6e\u7684\u6d88\u606f\u56de\u8c03<\/li>\n<p>\u6d4b\u8bd5\u4ee3\u7801\u4fee\u6539&#xff08;tcp_svr.cc&#xff09;<\/p>\n<p>#include &#034;..\/source\/server.hpp&#034;<br \/>\nvoid OnConnected(const PtrConnection&amp; ptr)<br \/>\n{<br \/>\n    DBG_LOG(&#034;New Connection: %p&#034;, ptr.get());<br \/>\n}<br \/>\nvoid OnMessage(const PtrConnection&amp; ptr, Buffer* buf)<br \/>\n{<br \/>\n    DBG_LOG(&#034;%s&#034;, buf-&gt;ReadPosition());<br \/>\n    buf-&gt;MoveReadOffset(buf-&gt;ReadAbleSize());<br \/>\n    std::string s &#061; &#034;hello, bugubugu!&#034;;<br \/>\n    ptr-&gt;Send(s.c_str(), s.size());<br \/>\n    \/\/ptr-&gt;Shutdown();<br \/>\n}<br \/>\nvoid OnClosed(const PtrConnection&amp; ptr)<br \/>\n{<br \/>\n    DBG_LOG(&#034;Close Connection: %p&#034;, ptr.get());<br \/>\n}<br \/>\nint main()<br \/>\n{<br \/>\n    TcpServer svr(8080);<br \/>\n    svr.SetClosedCallback(OnClosed);<br \/>\n    svr.SetConnectedCallback(OnConnected);<br \/>\n    svr.SetMessageCallback(OnMessage);<br \/>\n    svr.SetThreadCount(2);<br \/>\n    svr.EnableInactiveRelease(10);<br \/>\n    svr.Start();<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u8fd9\u91cc\u7684\u6d4b\u8bd5\u7ed3\u679c\u548c\u524d\u9762\u6ca1\u6709\u5b9e\u73b0TcpServer\u6a21\u5757\u7ed3\u679c\u51e0\u4e4e\u4e00\u6837&#xff1a;\u552f\u4e00\u4e0d\u540c\u7684\u662f\u00a0Release Connection: 0x56083a72f740 \u6267\u884c\u7684\u7ebf\u7a0b\u4e0d\u4e00\u6837<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"162\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128232547-697a9afb44a22.png\" width=\"1410\" \/><\/p>\n<p>\u4e0a\u9762\u662f\u79fb\u9664\u8fde\u63a5\u4fe1\u606f\u6ca1\u6709\u88abRunInLoop\u7684\u6253\u5370\u754c\u9762<br \/>\n\u4e0b\u9762\u662f\u79fb\u9664\u8fde\u63a5\u4fe1\u606f\u88abRunInLoop\u7684\u6253\u5370\u754c\u9762<br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"366\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128232547-697a9afb69709.png\" width=\"1354\" \/><\/p>\n<p>\u9020\u6210\u6267\u884cConnection\u6790\u6784\u51fd\u6570\u7684\u7ebf\u7a0b\u4e0d\u4e00\u6837\u7684\u539f\u56e0\u662f<\/p>\n<p>TcpServer\u91cc\u5bf9\u4e8e<span style=\"background-color:#fbd4d0\">\u4ece\u7ba1\u7406connection\u7684_conns\u4e2d\u79fb\u9664\u8fde\u63a5\u4fe1\u606f<\/span>\u8fd9\u4e2a\u6a21\u5757&#xff0c;\u4e3a\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u653e\u5728\u4e3b\u7ebf\u7a0b\u8fd0\u884c<\/p>\n<p>\u00a0 \u00a0 void RemoveConnection(const PtrConnection &amp;ptr)<br \/>\n\u00a0 \u00a0 {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 _baseloop.RunInLoop(std::bind(&amp;TcpServer::RemoveConnectionInLoop, this, ptr));<br \/>\n\u00a0 \u00a0 }<\/p>\n<p><span style=\"background-color:#fbd4d0\">\u8c01\u6700\u540e\u91ca\u653e PtrConnection\u8c01\u5c31\u6267\u884cConnection\u7684\u6790\u6784\u51fd\u6570<\/span>&#xff0c;\u8fd9\u91cc\u5f88\u660e\u663e\u5c31\u662f _conns\u53d8\u91cf\u3002<br \/>\n&#xff08;1&#xff09;\u4e4b\u524dRemoveConnection\u662f\u7531<span style=\"background-color:#fbd4d0\">\u4ece\u5c5e\u7ebf\u7a0b<\/span>\u6267\u884c&#xff0c;\u5f53erase\u6267\u884c\u540e&#xff0c;PtrConnection\u51cf\u5230\u4e00\u65f6&#xff0c;\u6790\u6784\u51fd\u6570\u6267\u884c&#xff1b;<br \/>\n&#xff08;2&#xff09;\u73b0\u5728RemoveConnection\u8fd8\u662f\u7531\u4ece\u5c5e\u7ebf\u7a0b\u6267\u884c&#xff0c;\u4f46\u662f\u8d70\u5230_baseloop.RunInLoop \u8fd9\u4e00\u5757\u53d1\u73b0\u5f53\u524d\u7ebf\u7a0bid\u4e0d\u662fbaseloop\u7684id&#xff0c;\u90a3\u4e48\u7ebf\u7a0b\u5c31\u4f1a\u628a\u4efb\u52a1\u52a0\u5165 _baseloop\u7684\u4efb\u52a1\u961f\u5217\u5e76\u5524\u9192_baseloop\u5bf9\u5e94\u7684\u4e3b\u7ebf\u7a0b\u8fdb\u884c\u76d1\u63a7&#xff0c;<span style=\"background-color:#fbd4d0\">\u4e3b\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1\u961f\u5217\u7684RemoveConnectionInLoop<\/span>&#xff0c;\u90a3\u4e48conns_.erase \u4ece\u6620\u5c04\u8868\u4e2d\u79fb\u9664\u8fde\u63a5&#xff0c;PtrConnection\u51cf\u5230\u4e00\u65f6&#xff0c;\u6790\u6784\u51fd\u6570\u6267\u884c\u3002<\/p>\n<p>\u6ce8\u610f&#xff1a;SIGPIPE:\u5f53\u6709\u4e00\u65b9\u4e3b\u52a8\u5173\u95ed\u8fde\u63a5\u65f6&#xff0c;\u53e6\u4e00\u65b9\u53d1\u9001\u4fe1\u606f\u4f1a\u89e6\u53d1SIGPIPE\u5f02\u5e38&#xff0c;\u5bfc\u81f4\u53d1\u9001\u65b9\u7a0b\u5e8f\u9000\u51fa&#xff0c;\u6240\u4ee5\u8fd9\u91cc\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf\u5ffd\u7565\u8fd9\u4e2a\u4fe1\u53f7\u3002\u5f53\u5e95\u5c42\u7684 write \u6216 send \u51fd\u6570\u5728\u906d\u9047\u5bf9\u65b9\u5173\u95ed\u8fde\u63a5\u7684\u60c5\u51b5\u65f6&#xff0c;\u5c06\u4e0d\u4f1a\u5bfc\u81f4\u8fdb\u7a0b\u6b7b\u4ea1<\/p>\n<p>class NetWork<br \/>\n{<br \/>\npublic:<br \/>\n    NetWork()<br \/>\n    {<br \/>\n        DBG_LOG(&#034;SIGPIPE INIT&#034;);<br \/>\n        signal(SIGPIPE, SIG_IGN);<br \/>\n    }<br \/>\n};<br \/>\nstatic NetWork nw;<\/p>\n<h2>EchoServer\u56de\u663e\u670d\u52a1\u5668<\/h2>\n<p>\u7ee7\u7eed\u5bf9TcpServer\u6a21\u5757\u8fdb\u884c\u5c01\u88c5&#xff0c;\u628aTcpServer\u7684\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\u529f\u80fd\u3001\u8bbe\u7f6e\u4ece\u5c5e\u7ebf\u7a0b\u4e2a\u6570\u5df2\u7ecf\u542f\u52a8\u975e\u6d3b\u8dc3\u9500\u6bc1\u529f\u80fd\u5728EchoServer\u5185\u90e8\u5b9e\u73b0<\/p>\n<h3>\u4ee3\u7801\u8bbe\u8ba1<\/h3>\n<p>class EchoServer<br \/>\n{<br \/>\nprivate:<br \/>\n    TcpServer _server;<\/p>\n<p>private:<br \/>\n    void OnConnected(const PtrConnection &amp;ptr)<br \/>\n    {<br \/>\n        DBG_LOG(&#034;New Connection: %p&#034;, ptr.get());<br \/>\n    }<br \/>\n    void OnMessage(const PtrConnection &amp;ptr, Buffer *buf)<br \/>\n    {<br \/>\n        ptr-&gt;Send(buf-&gt;ReadPosition(), buf-&gt;ReadAbleSize());<br \/>\n        buf-&gt;MoveReadOffset(buf-&gt;ReadAbleSize());<br \/>\n        \/\/ ptr-&gt;Shutdown();<br \/>\n    }<br \/>\n    void OnClosed(const PtrConnection &amp;ptr)<br \/>\n    {<br \/>\n        DBG_LOG(&#034;Close Connection: %p&#034;, ptr.get());<br \/>\n    }<\/p>\n<p>public:<br \/>\n    EchoServer(int port) : _server(port)<br \/>\n    {<br \/>\n        _server.SetClosedCallback(std::bind(&amp;EchoServer::OnClosed, this, std::placeholders::_1));<br \/>\n        _server.SetConnectedCallback(std::bind(&amp;EchoServer::OnConnected, this, std::placeholders::_1));<br \/>\n        _server.SetMessageCallback(std::bind(&amp;EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));<br \/>\n        _server.SetThreadCount(2);<br \/>\n        _server.EnableInactiveRelease(10);<br \/>\n    }<br \/>\n    void Start()<br \/>\n    {<br \/>\n        _server.Start();<br \/>\n    }<br \/>\n};<\/p>\n<p>\u6ce8\u610f\u8fd9\u91cc\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\u4e00\u5b9a\u8981\u7528bind&#xff0c;\u56e0\u4e3aOnConnected\u3001OnMessage\u3001OnClosed\u7b49\u51fd\u6570\u7684\u5b9e\u73b0\u662f\u7c7b\u5185\u51fd\u6570&#xff0c;\u548c\u4e4b\u524d\u76f4\u63a5\u5728\u6d4b\u8bd5\u4ee3\u7801\u91cc\u9762\u5b9e\u73b0\u4e0d\u4e00\u6837&#xff08;\u5168\u5c40\u51fd\u6570&#xff09;&#xff0c;\u8fd9\u91cc\u9700\u8981\u4f20this\u6307\u9488\u3002<\/p>\n<p>\/\/main.cc<br \/>\n#include &#034;echo.hpp&#034;<br \/>\nint main()<br \/>\n{<br \/>\n    EchoServer _server(8080);<br \/>\n    _server.Start();<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u518d\u6b21\u5c01\u88c5\u540e&#xff0c;\u53ea\u9700\u8981\u4f20\u4e2a\u7aef\u53e3\u53f7\u5df2\u7ecf\u542f\u52a8Start\u51fd\u6570\u5c31\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u56de\u663e\u670d\u52a1\u5668&#xff0c;\u5373\u628a\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u4fe1\u606f\u518d\u6b21\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<h3>\u6a21\u5757\u5173\u7cfb\u56fe<br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1711\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128232547-697a9afb9f706.png\" width=\"1741\" \/><\/h3>\n<p>\n\u00a0<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>LoopThread\u6a21\u5757\u76ee\u7684\u5c06EventLoop\u6a21\u5757\u4e0e\u7ebf\u7a0b\u6574\u5408\u4e00\u8d77&#xff0c;\u8ba9EventLoop\u6a21\u5757\u4e0e\u7ebf\u7a0b\u662f\u4e00\u4e00\u5bf9\u5e94\u7684&#xff0c;\u8fd9\u91cc\u5c31\u5ef6\u4f38\u51fa\u4e00\u4e2a\u95ee\u9898&#xff1a;\u662f\u8981\u5148\u5728\u4e3b\u7ebf\u7a0b\u521b\u5efaEventLoop\u5bf9\u8c61&#xff0c;\u518d\u5206\u914d\u7ed9\u5b50\u7ebf\u7a0b\u8fd8\u662f\u5148\u521b\u5efa\u5b50\u7ebf\u7a0b&#xff0c;\u518d\u5728\u5b50\u7ebf\u7a0b\u91cc\u521b\u5efaEventLoop\u5bf9\u8c61&#xff1f;\u542b\u4e49\u5728\u56de\u7b54\u4e0a\u9762\u7684\u7591\u95ee\u524d\u8981\u5148\u6e05\u695a&#xff1a;\u5728eventloop\u91cc\u8fdb\u884c\u4e00\u4e2a\u64cd\u4f5c\u65f6&#xff0c;\u5f80\u5f80\u4f1a\u6d89\u53ca\u5224\u65ad\u5f53\u524d\u64cd\u4f5c\u662f\u5426\u5728eventloop\u6a21\u5757<\/p>\n","protected":false},"author":2,"featured_media":67801,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,190],"topic":[],"class_list":["post-67804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668\uff083\uff09 - \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\/67804.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668\uff083\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"LoopThread\u6a21\u5757\u76ee\u7684\u5c06EventLoop\u6a21\u5757\u4e0e\u7ebf\u7a0b\u6574\u5408\u4e00\u8d77&#xff0c;\u8ba9EventLoop\u6a21\u5757\u4e0e\u7ebf\u7a0b\u662f\u4e00\u4e00\u5bf9\u5e94\u7684&#xff0c;\u8fd9\u91cc\u5c31\u5ef6\u4f38\u51fa\u4e00\u4e2a\u95ee\u9898&#xff1a;\u662f\u8981\u5148\u5728\u4e3b\u7ebf\u7a0b\u521b\u5efaEventLoop\u5bf9\u8c61&#xff0c;\u518d\u5206\u914d\u7ed9\u5b50\u7ebf\u7a0b\u8fd8\u662f\u5148\u521b\u5efa\u5b50\u7ebf\u7a0b&#xff0c;\u518d\u5728\u5b50\u7ebf\u7a0b\u91cc\u521b\u5efaEventLoop\u5bf9\u8c61&#xff1f;\u542b\u4e49\u5728\u56de\u7b54\u4e0a\u9762\u7684\u7591\u95ee\u524d\u8981\u5148\u6e05\u695a&#xff1a;\u5728eventloop\u91cc\u8fdb\u884c\u4e00\u4e2a\u64cd\u4f5c\u65f6&#xff0c;\u5f80\u5f80\u4f1a\u6d89\u53ca\u5224\u65ad\u5f53\u524d\u64cd\u4f5c\u662f\u5426\u5728eventloop\u6a21\u5757\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/67804.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-28T23:25:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128232547-697a9afb44a22.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=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/67804.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/67804.html\",\"name\":\"\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668\uff083\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-28T23:25:50+00:00\",\"dateModified\":\"2026-01-28T23:25:50+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/67804.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/67804.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/67804.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668\uff083\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668\uff083\uff09 - \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\/67804.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668\uff083\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"LoopThread\u6a21\u5757\u76ee\u7684\u5c06EventLoop\u6a21\u5757\u4e0e\u7ebf\u7a0b\u6574\u5408\u4e00\u8d77&#xff0c;\u8ba9EventLoop\u6a21\u5757\u4e0e\u7ebf\u7a0b\u662f\u4e00\u4e00\u5bf9\u5e94\u7684&#xff0c;\u8fd9\u91cc\u5c31\u5ef6\u4f38\u51fa\u4e00\u4e2a\u95ee\u9898&#xff1a;\u662f\u8981\u5148\u5728\u4e3b\u7ebf\u7a0b\u521b\u5efaEventLoop\u5bf9\u8c61&#xff0c;\u518d\u5206\u914d\u7ed9\u5b50\u7ebf\u7a0b\u8fd8\u662f\u5148\u521b\u5efa\u5b50\u7ebf\u7a0b&#xff0c;\u518d\u5728\u5b50\u7ebf\u7a0b\u91cc\u521b\u5efaEventLoop\u5bf9\u8c61&#xff1f;\u542b\u4e49\u5728\u56de\u7b54\u4e0a\u9762\u7684\u7591\u95ee\u524d\u8981\u5148\u6e05\u695a&#xff1a;\u5728eventloop\u91cc\u8fdb\u884c\u4e00\u4e2a\u64cd\u4f5c\u65f6&#xff0c;\u5f80\u5f80\u4f1a\u6d89\u53ca\u5224\u65ad\u5f53\u524d\u64cd\u4f5c\u662f\u5426\u5728eventloop\u6a21\u5757","og_url":"https:\/\/www.wsisp.com\/helps\/67804.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-28T23:25:50+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260128232547-697a9afb44a22.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/67804.html","url":"https:\/\/www.wsisp.com\/helps\/67804.html","name":"\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668\uff083\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-28T23:25:50+00:00","dateModified":"2026-01-28T23:25:50+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/67804.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/67804.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/67804.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4effmuduo\u5e93\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668\uff083\uff09"}]},{"@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\/67804","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=67804"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/67804\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/67801"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=67804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=67804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=67804"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=67804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}