{"id":28048,"date":"2025-04-19T22:36:37","date_gmt":"2025-04-19T14:36:37","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/28048.html"},"modified":"2025-04-19T22:36:37","modified_gmt":"2025-04-19T14:36:37","slug":"%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%ef%bc%8819%ef%bc%89-c%e4%bd%bf%e7%94%a8asio%e5%8d%8f%e7%a8%8b%e5%ae%9e%e7%8e%b0%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/28048.html","title":{"rendered":"\u7f51\u7edc\u7f16\u7a0b\uff0819\uff09\u2014\u2014C++\u4f7f\u7528asio\u534f\u7a0b\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668"},"content":{"rendered":"<h3 id=\"h_957175334_0\">\u5341\u4e5d\u3001day19<\/h3>\n<p>\u4e0a\u4e00\u8282\u5b66\u4e60\u4e86\u5982\u679c\u901a\u8fc7asio\u534f\u7a0b\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u5e76\u53d1\u670d\u52a1\u5668demo&#xff08;\u5b98\u65b9\u6848\u4f8b&#xff09;&#xff0c;\u4eca\u5929\u5b66\u4e60\u5982\u4f55\u901a\u8fc7asio\u534f\u7a0b\u642d\u5efa\u4e00\u4e2a\u6bd4\u8f83\u5b8c\u6574\u7684\u5e76\u53d1\u670d\u52a1\u5668\u3002<\/p>\n<p>\u4e3b\u8981\u5b9e\u73b0\u4e86AsioIOServicePool\u7ebf\u7a0b\u6c60\u3001\u903b\u8f91\u5c42LogicSystem\u3001\u7c98\u5305\u5904\u7406\u3001\u63a5\u6536\u534f\u7a0b\u3001\u53d1\u9001\u961f\u5217\u3001\u7f51\u7edc\u5e8f\u5217\u5316\u3001json\u5e8f\u5217\u5316\u3001\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f&#xff08;\u7b2c\u4e00\u79cd\u65b9\u5f0f\u5b9e\u73b0\u7ebf\u7a0b\u6c60AsioIOServicePool&#xff0c;\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u5b9e\u73b0\u903b\u8f91\u5c42LogicSystem&#xff09;\u3002<\/p>\n<ul>\n<li>Const\u6587\u4ef6\u4e3b\u8981\u7528\u4e8e\u5b9a\u4e49\u5404\u79cd\u5b8f<\/li>\n<li>CServer\u4e3b\u8981\u7528\u4e8e\u76d1\u542c\u5ba2\u6237\u7aef\u7684\u8fde\u63a5&#xff0c;\u5e76\u751f\u6210\u5e76\u5206\u914dSession\u4f1a\u8bdd<\/li>\n<li>AsioIOServicePool\u7ebf\u7a0b\u6c60\u7528\u4e8e\u591a\u7ebf\u7a0b\u6536\u53d1<\/li>\n<li>LogicSystem\u903b\u8f91\u5c42\u7528\u4e8e\u5904\u7406\u903b\u8f91\u961f\u5217\u4e2d\u7684\u6570\u636e<\/li>\n<li>CSession\u7528\u4e8e\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684\u6536\u53d1\u64cd\u4f5c<\/li>\n<\/ul>\n<p>\u89c6\u9891\u8d44\u6599\u53c2\u8003up\u604b\u604b\u98ce\u8fb0&#xff1a;<\/p>\n<p>\u3010C&#043;&#043; \u7f51\u7edc\u7f16\u7a0b(19) \u5229\u7528\u534f\u7a0b\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668(\u4e0a)\u3011<\/p>\n<p>https:\/\/www.bilibili.com\/video\/BV18k4y1M7Nx?vd_source&#061;cb95e3058c2624d2641da6f4eeb7e3a1\u200bwww.bilibili.com\/video\/BV18k4y1M7Nx?vd_source&#061;cb95e3058c2624d2641da6f4eeb7e3a1<\/p>\n<p>\u3010C&#043;&#043; \u7f51\u7edc\u7f16\u7a0b(20) \u5229\u7528\u534f\u7a0b\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668(\u4e0b)\u3011<\/p>\n<p>https:\/\/www.bilibili.com\/video\/BV1ch4y1G7Dc?vd_source&#061;cb95e3058c2624d2641da6f4eeb7e3a1\u200bwww.bilibili.com\/video\/BV1ch4y1G7Dc?vd_source&#061;cb95e3058c2624d2641da6f4eeb7e3a1<\/p>\n<h3 id=\"h_957175334_1\">1. Const.h<\/h3>\n<h3>1. Const.h<\/h3>\n<p>#pragma once<br \/>\n#define MAX_LENGTH 1024*2<br \/>\n#define HEAD_TOTAL_LEN 4<br \/>\n#define HEAD_ID_LEN 2<br \/>\n#define HEAD_DATA_LEN 2<br \/>\n#define MAX_RECVQUE 1000<br \/>\n#define MAX_SENDQUE 1000<\/p>\n<p>enum MSG_ID {<br \/>\nMSG_HELLO_WORD &#061; 1001<br \/>\n};<\/p>\n<p>\u8be5\u5934\u6587\u4ef6\u7528\u4e8e\u5b9a\u4e49\u6d88\u606f\u6536\u53d1\u4e2d\u9700\u8981\u7528\u5230\u7684\u5b8f<\/p>\n<ul>\n<li>MAX_LENGTH&#xff1a;\u4e00\u6b21\u6700\u591a\u63a5\u65362048\u5b57\u8282\u7684\u5185\u5bb9<\/li>\n<li>HEAD_TOTAL_LEN&#xff1a;\u6d88\u606f\u5934\u7ed3\u70b9\u603b\u957f\u5ea6\u4e3a4\u5b57\u8282&#xff0c;\u5305\u62ec\u6d88\u606fid\u548c\u6d88\u606f\u4f53\u957f\u5ea6<\/li>\n<li>HEAD_ID_LEN&#xff1a;\u6d88\u606fid&#xff0c;2\u5b57\u8282<\/li>\n<li>HEAD_DATA_LEN&#xff1a;\u6d88\u606f\u957f\u5ea6&#xff0c;2\u5b57\u8282<\/li>\n<li>MAX_RECVQUE&#xff1a;\u63a5\u6536\u961f\u5217\u6700\u591a\u5bb9\u7eb31000\u4e2a\u5904\u7406\u8bf7\u6c42<\/li>\n<li>MAX_SENDQUE&#xff1a;\u53d1\u9001\u961f\u5217\u6700\u591a\u5bb9\u7eb31000\u4e2a\u5904\u7406\u8bf7\u6c42<\/li>\n<li>MSG_ID&#xff1a;\u7528\u4e8e\u5b58\u50a8\u6d88\u606fid&#xff0c;\u901a\u8fc7id\u5728\u903b\u8f91\u961f\u5217\u4e2d\u67e5\u627e\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570<\/li>\n<\/ul>\n<h3 id=\"h_957175334_2\">2. \u6d88\u606f\u5b58\u50a8\u8282\u70b9MsgNode<\/h3>\n<h4 id=\"h_957175334_3\">a. MsgNode.h<\/h4>\n<p>#pragma once<br \/>\n#include &lt;string&gt;<br \/>\n#include &#034;Const.h&#034;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;boost\/asio.hpp&gt;<\/p>\n<p>class MsgNode<br \/>\n{<br \/>\npublic:<br \/>\nshort _cur_len;<br \/>\nshort _total_len;<br \/>\nchar* _data;<\/p>\n<p>MsgNode(short max_len) : _total_len(max_len), _cur_len(0) {<br \/>\n_data &#061; new char[_total_len &#043; 1]();<br \/>\n_data[_total_len] &#061; &#039;\\\\0&#039;;<br \/>\n}<br \/>\n~MsgNode() {<br \/>\nstd::cout &lt;&lt; &#034;destruct MsgNode&#034; &lt;&lt; std::endl;<br \/>\ndelete[] _data;<br \/>\n}<br \/>\nvoid Clear() {<br \/>\n::memset(_data, 0, _total_len);<br \/>\n_cur_len &#061; 0;<br \/>\n}<br \/>\n};<\/p>\n<p>class RecvNode : public MsgNode {<br \/>\nprivate:<br \/>\nshort _msg_id;<br \/>\npublic:<br \/>\nRecvNode(short max_len, short msg_id);<br \/>\nconst short&amp; GetMsgID() { return _msg_id; }<br \/>\n};<\/p>\n<p>class SendNode : public MsgNode {<br \/>\nprivate:<br \/>\nshort _msg_id;<br \/>\npublic:<br \/>\nSendNode(const char* msg, short max_len, short msg_id);<br \/>\n};<\/p>\n<p>MsgNode\u7c7b\u4e3b\u8981\u7528\u4e8e\u6784\u5efa\u6d88\u606f\u63a5\u6536\u8282\u70b9\u548c\u6d88\u606f\u53d1\u9001\u8282\u70b9<\/p>\n<ul>\n<li>RecvNode\u8282\u70b9\u4f5c\u4e3a\u63a5\u6536\u8282\u70b9&#xff0c;\u6784\u9020\u51fd\u6570\u4ec5\u9700\u6700\u5927\u957f\u5ea6\u548c\u6d88\u606fid&#xff0c;\u540c\u65f6\u5b9a\u4e49\u4e00\u4e2aGetMsgID()\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u6d88\u606fid&#xff08;\u5728\u903b\u8f91\u5c42\u6839\u636e\u6d88\u606fid\u5bfb\u627e\u5bf9\u5e94\u5df2\u6ce8\u518c\u56de\u8c03\u51fd\u6570\u65f6\u9700\u8981\u7528\u5230&#xff09;<\/li>\n<li>SendNode\u8282\u70b9\u4f5c\u4e3a\u53d1\u9001\u8282\u70b9&#xff0c;\u4e0d\u4ec5\u9700\u8981\u77e5\u9053\u6700\u5927\u957f\u5ea6\u548c\u6d88\u606fid&#xff0c;\u8fd8\u9700\u4f20\u5165\u6d88\u606f\u9996\u5730\u5740msg<\/li>\n<\/ul>\n<h4 id=\"h_957175334_4\">b. \u6210\u5458\u51fd\u6570\u5b9e\u73b0<\/h4>\n<p>#include &#034;MsgNode.h&#034;<\/p>\n<p>RecvNode::RecvNode(short max_len, short msg_id) : MsgNode(max_len), _msg_id(msg_id){<br \/>\n}<\/p>\n<p>SendNode::SendNode(const char* msg, short max_len, short msg_id) :<br \/>\nMsgNode(max_len &#043; HEAD_TOTAL_LEN), _msg_id(msg_id) {<br \/>\n\/\/ \u5148\u53d1\u9001id&#xff0c;\u5e76\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f<br \/>\nshort msg_id_net &#061; boost::asio::detail::socket_ops::host_to_network_short(msg_id);<br \/>\nmemcpy(_data, &amp;msg_id_net, HEAD_ID_LEN);<br \/>\n\/\/ \u53d1\u9001\u6d88\u606f\u957f\u5ea6&#xff0c;\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f<br \/>\nshort max_len_net &#061; boost::asio::detail::socket_ops::host_to_network_short(max_len);<br \/>\nmemcpy(_data &#043; HEAD_ID_LEN, &amp;max_len_net, HEAD_DATA_LEN);<br \/>\nmemcpy(_data &#043; HEAD_TOTAL_LEN, msg, max_len);<br \/>\n}<\/p>\n<ul>\n<li>RecvNode\u8282\u70b9\u7684\u6784\u9020\u51fd\u6570\u6bd4\u8f83\u7b80\u5355&#xff0c;\u53ea\u9700\u8981\u8c03\u7528\u57fa\u7c7bMsgNode\u7684\u6784\u9020\u51fd\u6570\u5e76\u5c06\u6d88\u606fid\u4f20\u5165_msg_id<\/li>\n<li>SendNode\u4e0d\u4ec5\u9700\u8981\u4e0a\u8ff0\u4e24\u6b65&#xff08;\u57fa\u7c7bMsgNode\u6784\u9020\u51fd\u6570\u4f20\u5165\u7684\u6700\u5927\u957f\u5ea6\u4e0d\u662f\u4f20\u5165\u7684\u503c&#xff0c;\u8fd8\u9700\u8981\u518d\u4f20\u5165\u503c\u57fa\u7840\u4e0a\u52a04\u5b57\u8282&#xff0c;4\u5b57\u8282\u7528\u4e8e\u5b58\u50a8\u6d88\u606fid\u548c\u6d88\u606f\u4f53\u957f\u5ea6&#xff09;&#xff0c;\u8fd8\u9700\u8fdb\u884c\u4ee5\u4e0b\u64cd\u4f5c&#xff1a;\n<ul>\n<li>\u5c06\u4f20\u5165\u7684\u6d88\u606fid\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\u5e76\u5b58\u50a8\u81f3_data&#xff08;MsgNode\u7c7b\u6210\u5458\u53d8\u91cf&#xff09;&#xff0c;\u957f\u5ea62\u5b57\u8282<\/li>\n<li>\u5c06\u4f20\u5165\u7684\u6d88\u606f\u957f\u5ea6\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\u5e76\u5b58\u50a8\u81f3_data&#xff08;MsgNode\u7c7b\u6210\u5458\u53d8\u91cf&#xff09;&#xff0c;\u957f\u5ea62\u5b57\u8282<\/li>\n<li>_data\u504f\u79fb4\u5b57\u8282\u540e\u5b58\u50a8\u6d88\u606f\u4f53\u5185\u5bb9&#xff0c;\u957f\u5ea6\u4e3a\u4f20\u5165\u503cmax_len<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u56e0\u4e3a\u4e0d\u540c\u8bbe\u5907\u7684\u5b57\u8282\u5e8f\u5b58\u5728\u5927\u7aef\u5e8f\u548c\u5c0f\u7aef\u5e8f\u7684\u95ee\u9898&#xff0c;\u5fc5\u987b\u5c06\u6d88\u606fid\u548c\u6d88\u606f\u957f\u5ea6\u8f6c\u6362\u4e3a\u7f51\u7edc\u5e8f&#xff0c;\u5426\u5219\u6d88\u606f\u5185\u5bb9\u53ef\u80fd\u4f1a\u7d0a\u4e71&#xff0c;\u7f51\u7edc\u5b57\u8282\u5e8f\u7684\u5185\u5bb9\u53ef\u53c2\u8003<\/p>\n<p>\u7231\u5403\u571f\u8c46&#xff1a;\u7f51\u7edc\u7f16\u7a0b&#xff08;8&#xff09;&#043;\u5b57\u8282\u5e8f\u5904\u74060 \u8d5e\u540c \u00b7 0 \u8bc4\u8bba\u6587\u7ae0<\/p>\n<h3 id=\"h_957175334_5\">3. CServer<\/h3>\n<h4 id=\"h_957175334_6\">a. CServer.h<\/h4>\n<p>#pragma once<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;map&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &#034;boost\/asio.hpp&#034;<br \/>\n#include &#034;CSession.h&#034;<br \/>\n#include &#034;AsioIOServicePool.h&#034;<br \/>\n#include &lt;boost\/uuid\/uuid_generators.hpp&gt;<br \/>\n#include &lt;boost\/uuid\/uuid_io.hpp&gt;<br \/>\n#include &lt;boost\/asio\/co_composed.hpp&gt;<br \/>\n#include &lt;boost\/asio\/detached.hpp&gt;<br \/>\n#include &#034;Const.h&#034;<br \/>\n#include &lt;queue&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &#034;MsgNode.h&#034;<\/p>\n<p>using boost::asio::ip::tcp;<br \/>\nusing boost::asio::awaitable;<br \/>\nusing boost::asio::co_spawn;<br \/>\nusing boost::asio::detached;<br \/>\nusing boost::asio::use_awaitable;<br \/>\nnamespace this_coro &#061; boost::asio::this_coro;<\/p>\n<p>class CServer<br \/>\n{<br \/>\nprivate:<br \/>\nvoid HandleAccept(std::shared_ptr&lt;CSession&gt;, const boost::system::error_code&amp; error);<br \/>\nvoid StartAccept();<br \/>\nboost::asio::io_context&amp; _io_context;<br \/>\nshort _port;<br \/>\nboost::asio::ip::tcp::acceptor _acceptor;<br \/>\nstd::map&lt;std::string, std::shared_ptr&lt;CSession&gt;&gt; _sessions;<br \/>\nstd::mutex _mutex;<br \/>\npublic:<br \/>\nCServer(boost::asio::io_context&amp; io_context, short port);<br \/>\n~CServer();<br \/>\nvoid ClearSession(std::string);<br \/>\n};<\/p>\n<p>\u5305\u542b\u8981\u6c42\u7684\u5934\u6587\u4ef6\u5e76using\u9700\u8981\u4f7f\u7528\u7684\u547d\u540d\u7a7a\u95f4\u548c\u51fd\u6570&#xff08;\u534f\u7a0b&#xff09;<\/p>\n<p>\u6210\u5458\u51fd\u6570\u5728\u5b9a\u4e49\u7684\u65f6\u5019\u8be6\u7ec6\u89e3\u91ca&#xff0c;\u8fd9\u91cc\u4ecb\u7ecd\u4ee5\u4e0b\u6210\u5458\u53d8\u91cf&#xff1a;<\/p>\n<ul>\n<li>io_context&amp; _io_context&#xff1a;\u58f0\u660e\u4e00\u4e2a_io_context\u7684\u5f15\u7528&#xff0c;\u6ce8\u610f&#xff0c;\u548c\u00a0boost::asio::io_context _io_context\u4e0d\u540c&#xff0c;io_context&amp;\u00a0_io_context\u8868\u793a\u5bf9\u73b0\u6709io_context\u5bf9\u8c61&#xff08;\u5176\u4ed6\u5730\u65b9\u521b\u5efa\u7684ioc\u5bf9\u8c61&#xff09;\u7684\u5f15\u7528&#xff0c;\u8fd9\u91cc\u5e76\u4e0d\u662f\u58f0\u660e\u4e00\u4e2aioc\u5b9e\u4f8b&#xff0c;\u800c\u662f\u8868\u793a\u5bf9\u5916\u754c\u4f20\u5165ioc\u7684\u5f15\u7528&#xff08;\u4e3b\u51fd\u6570\u9700\u8981\u5c06\u76d1\u542c\u9519\u8bef\u4fe1\u53f7signal\u7684ioc\u4f20\u5165server\u7528\u4e8e\u76d1\u542c\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42accept&#xff09;<\/li>\n<li>_port&#xff1a;\u670d\u52a1\u5668\u7aef\u53e3&#xff0c;\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u7684\u6307\u5b9a\u7aef\u53e3\u8fdb\u884c\u8fde\u63a5<\/li>\n<li>_acceptor&#xff1a;\u7528\u4e8e\u7ed1\u5b9a\u670d\u52a1\u5668\u6307\u5b9aip\u548c\u7aef\u53e3\u53f7&#xff0c;\u5e76\u8fdb\u884c\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u4fe1\u53f7<\/li>\n<li>_sessions&#xff1a;\u5b58\u50a8\u4f1a\u8bdd\u4efb\u52a1\u7684\u5bb9\u5668&#xff0c;\u7c7b\u4f3cpython\u7684\u5b57\u5178&#xff0c;\u952e\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32&#xff08;\u4e0d\u540c\u4f1a\u8bdd\u6709\u4e0d\u540c\u7684uuid&#xff09;&#xff0c;\u503c\u662fsession&#xff0c;\u4e3a\u6bcf\u4e2asession\u4f1a\u8bdd\u6620\u5c04\u5bf9\u5e94\u7684uuid&#xff0c;\u901a\u8fc7uuid\u53ef\u5b9e\u73b0\u4f1a\u8bdd\u7684\u64e6\u9664\u3001\u63d2\u5165\u548c\u5bfb\u627e<\/li>\n<li>_mutex&#xff1a;\u9501&#xff0c;\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168<\/li>\n<\/ul>\n<h4 id=\"h_957175334_7\">b. \u6210\u5458\u51fd\u6570\u5b9e\u73b0<\/h4>\n<p>CServer::CServer(boost::asio::io_context&amp; io_context, short port) :<br \/>\n_io_context(io_context), _port(port), _acceptor(io_context, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)) {<br \/>\nstd::cout &lt;&lt; &#034;Server start success, on port: &#034; &lt;&lt; port &lt;&lt; std::endl;<br \/>\nStartAccept();<br \/>\n}<\/p>\n<p>CServer\u7684\u6784\u9020\u51fd\u6570\u63a5\u6536\u6307\u5b9a\u7684\u4e0a\u4e0b\u6587\u670d\u52a1io_context\u548c\u7aef\u53e3\u53f7&#xff0c;\u5e76\u5c06acceptor\u7ed1\u5b9a\u670d\u52a1\u5668\u6307\u5b9a\u7684ip\u548c\u7aef\u53e3\u53f7&#xff0c;\u6700\u540e\u8c03\u7528StartAccept()\u542f\u52a8\u76d1\u542c\u670d\u52a1<\/p>\n<p>void CServer::StartAccept() {<br \/>\nauto&amp; io_context &#061; AsioIOServicePool::GetInstance()-&gt;GetIOService();<br \/>\nstd::shared_ptr&lt;CSession&gt; new_session &#061; std::make_shared&lt;CSession&gt;(io_context, this);<br \/>\n_acceptor.async_accept(new_session-&gt;GetSocket(),<br \/>\nstd::bind(&amp;CServer::HandleAccept, this, new_session, std::placeholders::_1));<br \/>\n}<\/p>\n<p>\u4e3b\u51fd\u6570&#xff08;\u4e3b\u7ebf\u7a0b&#xff09;\u7684io_context\u4e3b\u8981\u7528\u4e8e\u76d1\u542c\u9519\u8bef\u4fe1\u53f7\u548c\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u4f46\u5728\u6bcf\u4e2a\u7ebf\u7a0b\u4e2d\u7684io_context\u90fd\u662f\u4ece\u7ebf\u7a0b\u6c60AsioIOServicePool\u4e2d\u53d6\u51fa\u72ec\u7acb\u8fd0\u884c\u7684\u3002\u603b\u5171\u542f\u52a8n&#043;1\u4e2aio_context&#xff0c;n\u662f\u7ebf\u7a0b\u6c60\u4e2d\u5b58\u50a8\u7684ioc&#xff0c;1\u662f\u4e3b\u51fd\u6570\u4e2d\u7684ioc\u3002\u51fd\u6570\u6d41\u7a0b\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\u9996\u5148&#xff0c;\u4ece\u7ebf\u7a0b\u6c60\u4e2d\u53d6\u51fa\u4e00\u4e2aioc&#xff0c;\u5e76\u5c06\u7ebf\u7a0b\u6c60\u4e2dioc\u7684\u7d22\u5f15&#043;1&#xff0c;\u4ee5\u4fbf\u8f6e\u8be2\u53d6\u51fa\u4e0b\u4e00\u4e2aioc&#xff1b;<\/li>\n<li>\u7136\u540e&#xff0c;\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684session\u4f1a\u8bdd&#xff0c;\u4f20\u5165\u4ece\u7ebf\u7a0b\u6c60\u4e2d\u53d6\u51fa\u7684ioc&#xff0c;\u5e76\u5c06server\u6307\u9488\u4f20\u5165&#xff08;\u5982\u679c\u8be5session\u4f1a\u8bdd\u9519\u8bef&#xff0c;\u901a\u8fc7server\u7684Clear\u51fd\u6570\u64e6\u9664\u8be5session\u4f1a\u8bdd&#xff09;\u3002\u901a\u8fc7\u667a\u80fd\u6307\u9488shared_ptr\u7ba1\u7406session\u58f0\u660e\u5468\u671f&#xff0c;\u901a\u8fc7\u667a\u80fd\u6307\u9488\u5728C&#043;&#043;\u4e2d\u5b9e\u73b0\u4f2a\u95ed\u5305&#xff0c;\u4ee5\u9632session\u5728\u7ebf\u7a0b\u7ed3\u675f\u524d\u88ab\u63d0\u524d\u91ca\u653e\u5bfc\u81f4\u7ebf\u7a0b\u5b89\u5168&#xff1b;<\/li>\n<li>\u6700\u540e&#xff0c;\u4f7f\u7528\u7ed1\u5b9a\u4e3b\u51fd\u6570\u4f20\u5165ioc\u548c\u6307\u5b9a\u7aef\u70b9\u7684acceptor\u76d1\u542c\u5ba2\u6237\u7aef&#xff08;\u5df2\u7ecf\u4e3a\u8be5\u5ba2\u6237\u7aef\u5efa\u7acb\u4e86\u4f1a\u8bddsession&#xff0c;\u53ea\u9700\u5ba2\u6237\u7aef\u8fdb\u884c\u8fde\u63a5\u5373\u53ef\u8fdb\u884c\u6536\u53d1\u64cd\u4f5c&#xff09;\u8fde\u63a5\u3002\u5982\u679c\u76d1\u542c\u6210\u529f&#xff0c;\u8c03\u7528\u56de\u8c03\u51fd\u6570HandleAccept\u3002<\/li>\n<\/ul>\n<p>void CServer::HandleAccept(std::shared_ptr&lt;CSession&gt; new_session, const boost::system::error_code&amp; error) {<br \/>\nif (!error) {<br \/>\nnew_session-&gt;Start();<br \/>\nstd::lock_guard&lt;std::mutex&gt; lock(_mutex);<br \/>\n_sessions.insert(make_pair(new_session-&gt;GetUuid(), new_session));<br \/>\n}<br \/>\nelse {<br \/>\nstd::cerr &lt;&lt; &#034;session accept failed, error is &#034; &lt;&lt; error.what() &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>StartAccept();<br \/>\n}<\/p>\n<p>\u8be5\u56de\u8c03\u51fd\u6570\u4e3b\u8981\u5904\u7406\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5982\u679c\u5ba2\u6237\u7aef\u8fde\u63a5\u6210\u529f&#xff08;\u6ca1\u6709\u9519\u8bef&#xff09;&#xff0c;\u90a3\u4e48\u542f\u52a8\u4f1a\u8bdd&#xff08;new_session-&gt;Start()&#xff09;&#xff0c;\u5e76\u52a0\u9501\u5c06\u8be5\u4f1a\u8bdd\u4ee5\u53ca\u5bf9\u5e94\u7684uuid\u63d2\u5165\u81f3_sessions&#xff0c;\u4ee5\u4fbf\u540e\u7eed\u7ba1\u7406\u6b64session&#xff0c;\u2018}\u2019\u7ed3\u675f\u540e\u81ea\u52a8\u91ca\u653e\u9501&#xff0c;\u65e0\u9700\u624b\u52a8\u91ca\u653e\u3002<\/p>\n<p>\u6700\u540e\u8c03\u7528StartAccept()&#xff0c;\u91cd\u65b0\u4ece\u7ebf\u7a0b\u6c60\u4e2d\u53d6\u51faioc\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4f1a\u8bddsession&#xff0c;\u53ea\u5f85\u4e3b\u7ebf\u7a0b\u7684ioc\u76d1\u542c\u5230\u5ba2\u6237\u7aef\u8fde\u63a5\u7533\u8bf7\u4e4b\u540e&#xff0c;\u4fbf\u5c06\u65b0\u4f1a\u8bdd\u4ea4\u7ed9\u65b0\u8fde\u63a5\u8bf7\u6c42\u8fdb\u884c\u6536\u53d1\u3002<\/p>\n<p>void CServer::ClearSession(std::string uuid) {<br \/>\nstd::lock_guard&lt;std::mutex&gt; lock(_mutex);<br \/>\n_sessions.erase(uuid);<br \/>\n}<\/p>\n<p>\u8be5\u51fd\u6570\u7528\u4e8e\u5c06\u6307\u5b9auuid\u7684\u4f1a\u8bdd\u4ece\u4f1a\u8bdd\u5b58\u50a8\u5bb9\u5668_sessions\u4e2d\u64e6\u9664&#xff0c;\u6bcf\u4e2auuid\u5bf9\u5e94\u4e00\u4e2asession&#xff0c;uuid\u901a\u8fc7\u96ea\u82b1\u7b97\u6cd5\u5b9e\u73b0\u3002<\/p>\n<h3 id=\"h_957175334_8\">4. \u7ebf\u7a0b\u6c60AsioIOServicePool<\/h3>\n<h4 id=\"h_957175334_9\">a. AsioIOServicePool.h<\/h4>\n<p>#pragma once<br \/>\n#include &#034;boost\/asio.hpp&#034;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;memory&gt;<\/p>\n<p>class AsioIOServicePool;<\/p>\n<p>class SafeDeletor{<br \/>\npublic:<br \/>\nvoid operator()(AsioIOServicePool* st);<br \/>\n};<\/p>\n<p>class AsioIOServicePool<br \/>\n{<br \/>\npublic:<br \/>\nfriend class SafeDeletor;<br \/>\nusing IOService &#061; boost::asio::io_context;<br \/>\nusing Work &#061; boost::asio::io_context::work;<br \/>\nusing WorkPtr &#061; std::unique_ptr&lt;Work&gt;;<\/p>\n<p>static std::shared_ptr&lt;AsioIOServicePool&gt; GetInstance() {<br \/>\nstatic std::once_flag s_flag;<br \/>\nstd::call_once(s_flag, [&amp;]() { \/\/ call_once\u5185\u90e8\u6709\u9501<br \/>\n_instance &#061; std::shared_ptr&lt;AsioIOServicePool&gt;(new AsioIOServicePool, SafeDeletor());<br \/>\n});<br \/>\nreturn _instance;<br \/>\n}<br \/>\nboost::asio::io_context&amp; GetIOService();<br \/>\nvoid Stop();<br \/>\nprivate:<br \/>\nAsioIOServicePool(std::size_t size &#061; std::thread::hardware_concurrency());<br \/>\n~AsioIOServicePool();<br \/>\nAsioIOServicePool(const AsioIOServicePool&amp;) &#061; delete;<br \/>\nAsioIOServicePool&amp; operator &#061; (const AsioIOServicePool&amp;) &#061; delete;<\/p>\n<p>std::vector&lt;IOService&gt; _ioServices;<br \/>\nstd::vector&lt;WorkPtr&gt; _works;<br \/>\nstd::vector&lt;std::thread&gt; _threads;<br \/>\nstd::size_t _nextIOService;<br \/>\nstatic std::shared_ptr&lt;AsioIOServicePool&gt; _instance;<br \/>\n};<\/p>\n<p>\u5efa\u7acb\u7ebf\u7a0b\u6c60AsioIOServicePool&#xff0c;\u7528\u4e8e\u591a\u7ebf\u7a0b\u6536\u53d1\u3002\u8be5\u670d\u52a1\u5668\u6709\u4e24\u4e2a\u5355\u4f8b\u7c7b&#xff0c;\u5206\u522b\u662fAsioIOServicePool&#xff08;\u7ebf\u7a0b\u6c60&#xff09;\u548cLogicSystem&#xff08;\u903b\u8f91\u5c42&#xff09;&#xff0c;\u6211\u4f7f\u7528\u4e86\u4e24\u79cd\u65b9\u5f0f\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f&#xff0c;AsioIOServicePool\u7684\u5355\u4f8b\u5b9e\u73b0\u6bd4\u8f83\u8be6\u7ec6&#xff0c;LogicSystem\u7684\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355&#xff0c;\u4f46\u4ee3\u7801\u66f4\u5c11\u66f4\u6e05\u6670\u3002<\/p>\n<p>class SafeDeletor{<br \/>\npublic:<br \/>\nvoid operator()(AsioIOServicePool* st);<br \/>\n};<\/p>\n<p>SafeDeletor\u662f\u6307\u5b9a\u7684\u5220\u9664\u5668&#xff0c;\u6211\u4eec\u5c06\u6790\u6784\u51fd\u6570\u8bbe\u7f6e\u4e3a\u79c1\u6709&#xff0c;\u9632\u6b62\u6790\u6784\u51fd\u6570\u88ab\u4e3b\u52a8\u8c03\u7528&#xff0c;\u6790\u6784\u51fd\u6570\u53ea\u80fd\u901a\u8fc7\u5220\u9664\u5668SafeDeletor\u88ab\u8c03\u7528&#xff0c;\u8fd9\u91cc\u91cd\u8f7d\u4e86()&#xff0c;\u5c06SafeDeletor\u7c7b\u4f5c\u4e3a\u4eff\u51fd\u6570\u4f7f\u7528\u3002<\/p>\n<p>AsioIOServicePool\u7684\u6210\u5458\u51fd\u6570\u5728\u540e\u9762\u5b9a\u4e49\u7684\u65f6\u5019\u8be6\u7ec6\u89e3\u91ca&#xff0c;\u8fd9\u91cc\u4ecb\u7ecdAsioIOServicePool\u4ee5\u4e0b\u6210\u5458\u53d8\u91cf&#xff1a;<\/p>\n<ul>\n<li>IOService&#xff1a;\u5c06boost::asio::io_context\u91cd\u547d\u540d\u4e3aIOService&#xff0c;\u4ee5\u4fbf\u4f7f\u7528<\/li>\n<li>Work&#xff1a;\u5c06boost::asio::io_context::work\u91cd\u547d\u540d\u4e3aWork&#xff0c;Work\u7528\u6765\u7ed1\u5b9aioc&#xff0c;\u4ee5\u9632ioc.run()\u63d0\u524d\u8fd4\u56de&#xff0c;\u8be6\u7ec6\u89e3\u91ca\u53ef\u53c2\u8003<\/li>\n<\/ul>\n<p>\u7231\u5403\u571f\u8c46&#xff1a;\u7f51\u7edc\u7f16\u7a0b&#xff08;16&#xff09;\u2014\u2014asio\u591a\u7ebf\u7a0b\u6a21\u578bIOServicePool5 \u8d5e\u540c \u00b7 0 \u8bc4\u8bba\u6587\u7ae0<\/p>\n<ul>\n<li>WorkPtr&#xff1a;\u4f7f\u7528unique_ptr\u7ba1\u7406work&#xff0c;\u5e0c\u671b\u8be5work\u4e0d\u4f1a\u88ab\u62f7\u8d1d&#xff0c;\u53ea\u80fd\u79fb\u52a8\u6216\u8005\u4ece\u5934\u7528\u5230\u5c3e\u4e0d\u88ab\u6539\u53d8<\/li>\n<li>_ioServices&#xff1a;io_context\u5b58\u50a8\u5bb9\u5668&#xff0c;\u7528\u4e8e\u5b58\u50a8\u6307\u5b9a\u6570\u91cf\u7684io_context<\/li>\n<li>_works&#xff1a;\u5b58\u50a8\u4e0eioc\u6570\u91cf\u5bf9\u5e94\u7684work<\/li>\n<li>_threads&#xff1a;\u5b58\u50a8\u6307\u5b9a\u6570\u91cf\u7684\u7ebf\u7a0b<\/li>\n<li>_nextIOService&#xff1a;\u8bb0\u5f55ioc\u5728vector\u7684\u4e0b\u6807&#xff0c;\u901a\u8fc7\u8f6e\u8be2\u8fd4\u56deioc\u65f6&#xff0c;\u9700\u8981\u8bb0\u5f55\u5f53\u524dioc\u7684\u4e0b\u6807&#xff0c;\u7d2f\u52a0&#xff0c;\u5f53\u8d85\u8fc7vector\u7684size\u65f6\u5c31\u5f52\u96f6&#xff0c;\u7136\u540e\u7ee7\u7eed\u6309\u8f6e\u8be2\u7684\u65b9\u5f0f\u8fd4\u56de<\/li>\n<li>_instance&#xff1a;\u58f0\u660e\u4e00\u4e2a\u9759\u6001\u6210\u5458\u53d8\u91cf&#xff0c;\u7528\u4e8e\u5b58\u50a8\u5355\u4f8b\u7c7bAsioIOServicePool\u552f\u4e00\u5b9e\u4f8b&#xff0c;\u901a\u8fc7\u61d2\u6c49\u65b9\u5f0f\u5b9e\u73b0&#xff0c;\u5355\u4f8b\u6a21\u5f0f\u7684\u8be6\u7ec6\u4ecb\u7ecd\u53ef\u53c2\u8003&#xff1a;<\/li>\n<\/ul>\n<p>\u7231\u5403\u571f\u8c46&#xff1a;\u7f51\u7edc\u7f16\u7a0b&#xff08;13&#xff09;\u2014\u2014\u5355\u4f8b\u6a21\u5f0f0 \u8d5e\u540c \u00b7 0 \u8bc4\u8bba\u6587\u7ae0<\/p>\n<h4 id=\"h_957175334_10\">b. \u6210\u5458\u51fd\u6570\u5b9e\u73b0<\/h4>\n<p>void SafeDeletor::operator()(AsioIOServicePool* st) {<br \/>\nstd::cout &lt;&lt; &#034;this is safe deleter operator() of AsioIOServicePool&#034; &lt;&lt; std::endl;<br \/>\ndelete st;<br \/>\n}<\/p>\n<p>\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\u5355\u4f8b\u7c7b\u7684\u6790\u6784\u51fd\u6570&#xff0c;\u53ea\u80fd\u901a\u8fc7\u5220\u9664\u5668\u95f4\u63a5\u8c03\u7528&#xff0c;\u5355\u4f8b\u7c7b\u7684\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u90fd\u79c1\u6709\u5316&#xff0c;\u5e76\u5c06\u590d\u5236\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26delete&#xff0c;\u9632\u6b62\u5355\u4f8b\u7c7b\u88ab\u62f7\u8d1d\u548c\u8d4b\u503c\u3002<\/p>\n<p>AsioIOServicePool::AsioIOServicePool(std::size_t size) :<br \/>\n_ioServices(size), _works(size), _nextIOService(0) {<br \/>\nfor (std::size_t i &#061; 0; i &lt; size; i&#043;&#043;) {<br \/>\n_works[i] &#061; std::unique_ptr&lt;Work&gt;(new Work(_ioServices[i]));<br \/>\n}<\/p>\n<p>\/\/ \u904d\u5386\u591a\u4e2aioservice,\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u5185\u90e8\u542f\u52a8ioservice<br \/>\nfor (std::size_t i &#061; 0; i &lt; _ioServices.size(); i&#043;&#043;) {<br \/>\n_threads.emplace_back([this, i]() {<br \/>\n_ioServices[i].run();<br \/>\n});<br \/>\n}<br \/>\n}<\/p>\n<p>\u7ebf\u7a0b\u6c60AsioIOServicePool\u7684\u6784\u9020\u51fd\u6570\u9700\u63a5\u6536ioc\u7684\u6307\u5b9a\u6570\u91cfsize&#xff0c;\u5982\u679c\u4e0d\u663e\u5f0f\u6307\u5b9a&#xff0c;\u90a3\u4e48size\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u503c\u00a0std::thread::hardware_concurrency()&#xff0c;\u8be5\u51fd\u6570\u7528\u4e8e\u83b7\u53d6CPU\u6838\u6570\u3002<\/p>\n<p>work\u7684\u6570\u91cf\u548cioc\u7684\u6570\u91cf\u76f8\u540c&#xff0c;\u7d22\u5f15_nextIOService\u9ed8\u8ba4\u503c\u4e3a0&#xff0c;\u4e0a\u9650\u4e3aioc\u7684\u6570\u91cfsize<\/p>\n<p>\u8fd8\u6709\u4e24\u4e2afor\u5faa\u73af&#xff0c;\u7b2c\u4e00\u4e2afor\u5faa\u73af\u4f7f\u7528Work()\u51fd\u6570\u5c06\u6307\u5b9aioc\u4e0ework\u7ed1\u5b9a\u5e76\u901a\u8fc7\u53f3\u503c\u79fb\u52a8\u7ed9\u5bf9\u5e94\u4f4d\u7f6e\u7684_works\u5bb9\u5668&#xff0c;\u6b64\u65f6_works\u5bb9\u5668\u4e2d\u5bf9\u5e94\u7d22\u5f15\u4fbf\u5b58\u50a8\u4e86\u4e0e\u5bf9\u5e94ioc\u7ed1\u5b9a\u7684work&#xff0c;\u53ea\u6709\u5c06work\u7ed9reset\u4e4b\u540e&#xff0c;ioc.run()\u624d\u4f1a\u8fd4\u56de\u3002\u7b2c\u4e8c\u4e2afor\u5faa\u73af\u542f\u52a8\u6307\u5b9aioc\u6570\u91cf_size\u7684\u7ebf\u7a0b&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u4e2d\u6709\u72ec\u7acb\u7684ioc&#xff0c;\u5e76\u542f\u52a8ioc.run()<\/p>\n<p>boost::asio::io_context&amp; AsioIOServicePool::GetIOService() {<br \/>\nauto&amp; service &#061; _ioServices[_nextIOService&#043;&#043;];<br \/>\nif (_nextIOService &#061;&#061; _ioServices.size())<br \/>\n_nextIOService &#061; 0;<\/p>\n<p>return service;<br \/>\n}<\/p>\n<p>\u4ee5_nextIOService\u4e3a\u7d22\u5f15\u8f6e\u8be2\u53d6\u51fa\u7ebf\u7a0b\u6c60\u4e2d\u7684io_context<\/p>\n<p>void AsioIOServicePool::Stop() {<br \/>\nfor (auto&amp; work : _works) {<br \/>\nwork.reset();<br \/>\n}<\/p>\n<p>for (auto&amp; t : _threads) {<br \/>\nt.join();<br \/>\n}<br \/>\n}<\/p>\n<p>\u4e0d\u540c\u7ebf\u7a0b\u7684io_context\u5982\u679c\u60f3\u8981\u505c\u6b62\u8fd4\u56de&#xff0c;\u90a3\u4e48\u5fc5\u987b\u5c06\u5df2\u7ed1\u5b9a\u7684work\u91ca\u653e&#xff0c;\u8be5\u51fd\u6570\u7528\u4e8e\u91ca\u653e\u6240\u6709ioc\u7684work&#xff0c;\u56e0\u4e3a\u4e0d\u540c\u7ebf\u7a0b\u72ec\u7acb\u8fd0\u884c&#xff0c;\u6240\u4ee5\u5fc5\u987b\u8981\u7b49\u5f85\u6bcf\u4e2a\u7ebf\u7a0b\u7ed3\u675f\u540e\u624d\u7ed3\u675f\u7a0b\u5e8f\u3002<\/p>\n<p>static std::shared_ptr&lt;AsioIOServicePool&gt; GetInstance() {<br \/>\nstatic std::once_flag s_flag;<br \/>\nstd::call_once(s_flag, [&amp;]() { \/\/ call_once\u5185\u90e8\u6709\u9501<br \/>\n_instance &#061; std::shared_ptr&lt;AsioIOServicePool&gt;(new AsioIOServicePool, SafeDeletor());<br \/>\n});<br \/>\nreturn _instance;<br \/>\n}<\/p>\n<p>\u5b9a\u4e49\u9759\u6001\u6210\u5458\u51fd\u6570GetInstance()&#xff0c;\u901a\u8fc7C&#043;&#043;11\u65b0\u7279\u6027once_flag \u548ccall_once\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u4fdd\u8bc1\u591a\u7ebf\u7a0b\u4e2dAsioIOServicePool\u53ea\u4f1a\u88ab\u521b\u5efa\u4e00\u6b21&#xff0c;\u6709\u4e14\u4ec5\u6709\u552f\u4e00\u5b9e\u4f8b\u3002<\/p>\n<p>once_flag \u548ccall_once\u7684\u8be6\u7ec6\u4ecb\u7ecd\u53ef\u53c2\u8003&#xff1a;<\/p>\n<p>\u7231\u5403\u571f\u8c46&#xff1a;C&#043;&#043;11\u65b0\u7279\u60270 \u8d5e\u540c \u00b7 0 \u8bc4\u8bba\u6587\u7ae0<\/p>\n<p>\u6700\u540e\u6ce8\u610f&#xff0c;\u975e\u6a21\u677f\u7c7b\u7684\u9759\u6001\u6210\u5458\u53d8\u91cf\u548c\u51fd\u6570\u9700\u8981\u5728\u5b9e\u73b0\u6587\u4ef6cpp\u4e2d\u521d\u59cb\u5316&#xff0c;\u4e0d\u80fd\u5728\u5934\u6587\u4ef6\u4e2d\u521d\u59cb\u5316\u3002\u6a21\u677f\u7c7b\u7684\u9759\u6001\u6210\u5458\u53d8\u91cf\u5fc5\u987b\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49&#xff0c;\u6a21\u677f\u7c7b\u4e2d\u7684\u9759\u6001\u53d8\u91cf\u53ef\u4ee5\u5728\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49&#xff0c;\u662f\u56e0\u4e3a\u6a21\u677f\u7c7b\u5728\u7f16\u8bd1\u65f6\u4e0d\u4f1a\u50cf\u666e\u901a\u7c7b\u90a3\u6837\u751f\u6210\u5177\u4f53\u7684\u7c7b\u578b\u548c\u7b26\u53f7\u3002\u76f8\u53cd&#xff0c;\u6a21\u677f\u7c7b\u7684\u4ee3\u7801\u53ea\u4f1a\u5728\u6a21\u677f\u88ab\u5b9e\u4f8b\u5316\u65f6\u624d\u751f\u6210\u5177\u4f53\u7684\u7c7b\u578b\u3002\u56e0\u6b64&#xff0c;\u6a21\u677f\u7c7b\u7684\u9759\u6001\u6210\u5458\u53d8\u91cf\u4e0d\u4f1a\u50cf\u666e\u901a\u7c7b\u7684\u9759\u6001\u6210\u5458\u90a3\u6837\u5728\u591a\u4e2a\u7f16\u8bd1\u5355\u5143\u4e2d\u91cd\u590d\u5b9a\u4e49\u3002<\/p>\n<p>AsioIOServicePool.cpp<br \/>\n\/\/ \u521d\u59cb\u5316<br \/>\nstd::shared_ptr&lt;AsioIOServicePool&gt; AsioIOServicePool::_instance &#061; nullptr;<\/p>\n<h3 id=\"h_957175334_11\">5. \u903b\u8f91\u5c42LogicSystem<\/h3>\n<p>\u4e00\u822c\u5728\u89e3\u6790\u5b8c\u5bf9\u7aef\u53d1\u9001\u7684\u6570\u636e\u4e4b\u540e&#xff0c;\u8fd8\u8981\u5bf9\u8be5\u8bf7\u6c42\u505a\u66f4\u8fdb\u4e00\u6b65\u5730\u5904\u7406&#xff0c;\u6bd4\u5982\u6839\u636e\u4e0d\u540c\u7684\u6d88\u606fid\u6267\u884c\u4e0d\u540c\u7684\u903b\u8f91\u5c42\u51fd\u6570\u6216\u4e0d\u540c\u7684\u64cd\u4f5c&#xff0c;\u6bd4\u5982\u8bfb\u6570\u636e\u5e93\u3001\u5199\u6570\u636e\u5e93&#xff0c;\u8fd8\u6bd4\u5982\u6e38\u620f\u4e2d\u53ef\u80fd\u9700\u8981\u7ed9\u73a9\u5bb6\u53e0\u52a0\u4e0d\u540c\u7684buff\u3001\u589e\u52a0\u79ef\u5206\u7b49\u7b49&#xff0c;\u8fd9\u4e9b\u90fd\u9700\u8981\u4ea4\u7ed9\u903b\u8f91\u5c42\u5904\u7406&#xff0c;\u800c\u4e0d\u4ec5\u4ec5\u662f\u628a\u6d88\u606f\u53d1\u7ed9\u5bf9\u7aef\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"328\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419143636-6803b4f42ea25.png\" width=\"720\" \/><\/p>\n<p style=\"text-align:center\">\u670d\u52a1\u5668\u67b6\u6784<\/p>\n<p>\u4e0a\u56fe\u6240\u793a\u7684\u662f\u4e00\u4e2a\u5b8c\u6210\u7684\u670d\u52a1\u5668\u67b6\u6784&#xff0c;\u4e00\u822c\u9700\u8981\u5c06\u903b\u8f91\u5c42\u72ec\u7acb\u51fa\u6765&#xff0c;\u56e0\u4e3a\u5982\u679c\u5728\u89e3\u6790\u5b8c\u5bf9\u7aef\u6570\u636e\u540e\u9700\u8981\u6267\u884c\u4e00\u4e9b\u590d\u6742\u7684\u64cd\u4f5c&#xff0c;\u6bd4\u5982\u73a9\u5bb6\u9700\u8981\u53e0\u52a0\u5404\u81eabuff\u6216\u8005\u6280\u80fd&#xff0c;\u6b64\u65f6\u53ef\u80fd\u4f1a\u8017\u65f61s\u751a\u81f3\u66f4\u591a&#xff0c;\u5982\u679c\u6ca1\u6709\u72ec\u7acb\u7684\u903b\u8f91\u5c42\u8fdb\u884c\u64cd\u4f5c&#xff0c;\u90a3\u4e48\u7cfb\u7edf\u4f1a\u4e00\u76f4\u505c\u7559\u5728\u6267\u884c\u56de\u8c03\u51fd\u6570\u90a3\u4e00\u6b65&#xff0c;\u9020\u6210\u963b\u585e&#xff0c;\u76f4\u81f3\u64cd\u4f5c\u7ed3\u675f\u3002<\/p>\n<p>\u800c\u903b\u8f91\u5c42\u662f\u72ec\u7acb\u7684&#xff0c;\u56de\u8c03\u51fd\u6570\u53ea\u9700\u5c06\u6570\u636e\u6295\u9012\u7ed9\u903b\u8f91\u961f\u5217&#xff08;\u56de\u8c03\u51fd\u6570\u5c06\u6570\u636e\u653e\u5165\u961f\u5217\u4e2d\u4e4b\u540e\u7cfb\u7edf\u4f1a\u8fd0\u884c\u4e0b\u4e00\u6b65&#xff0c;\u4fbf\u4e0d\u4f1a\u88ab\u963b\u585e&#xff09;&#xff0c;\u903b\u8f91\u7cfb\u7edf\u4f1a\u81ea\u52a8\u4ece\u961f\u5217\u4e2d\u53d6\u6570\u636e\u5e76\u505a\u76f8\u5e94\u64cd\u4f5c&#xff0c;\u5982\u679c\u9700\u8981\u5728\u6267\u884c\u5b8c\u64cd\u4f5c\u4e4b\u540e\u505a\u76f8\u5e94\u56de\u590d&#xff0c;\u90a3\u4e48\u903b\u8f91\u7cfb\u7edf\u4f1a\u8c03\u7528\u5199\u4e8b\u4ef6\u5e76\u6ce8\u518c\u5199\u56de\u8c03\u7ed9asio\u7f51\u7edc\u5c42&#xff0c;\u7f51\u7edc\u5c42\u5c31\u662fasio\u5e95\u5c42\u901a\u4fe1\u7684\u7f51\u7edc\u5c42\u6b65\u9aa4\u3002<\/p>\n<h4 id=\"h_957175334_12\">a. LogicSystem.h<\/h4>\n<p>#pragma once<br \/>\n#include &lt;queue&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &#034;CSession.h&#034;<br \/>\n#include &lt;map&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &#034;Const.h&#034;<br \/>\n#include &lt;json\/json.h&gt;<br \/>\n#include &lt;json\/value.h&gt;<br \/>\n#include &lt;json\/reader.h&gt;<\/p>\n<p>class LogicNode;<br \/>\nclass CSession;<\/p>\n<p>typedef std::function&lt;void(std::shared_ptr&lt;CSession&gt;, const short&amp; msg_id, const std::string&amp; msg_data)&gt; FunCallBack;<\/p>\n<p>class LogicSystem<br \/>\n{<br \/>\nprivate:<br \/>\nLogicSystem();<br \/>\n~LogicSystem();<br \/>\nLogicSystem(const LogicSystem&amp;) &#061; delete;<br \/>\nLogicSystem&amp; operator&#061;(const LogicSystem&amp;) &#061; delete;<\/p>\n<p>void RegisterCallBacks();<br \/>\nvoid HelloWordCallBack(std::shared_ptr&lt;CSession&gt;, const short&amp; msg_id, const std::string&amp; msg_data);<br \/>\nvoid DealMsg();<\/p>\n<p>std::queue&lt;std::shared_ptr&lt;LogicNode&gt;&gt; _msg_que;<br \/>\nstd::mutex _mutex;<br \/>\nstd::condition_variable _consume;<br \/>\nstd::thread _worker_thread;<br \/>\nbool _b_stop;<br \/>\nstd::map&lt;short, FunCallBack&gt; _fun_callback;<br \/>\npublic:<br \/>\nvoid PostMsgToQue(std::shared_ptr&lt;LogicNode&gt; msg);<br \/>\nstatic LogicSystem&amp; GetInstance();<br \/>\n};<\/p>\n<p>\u5b9a\u4e49\u903b\u8f91\u7c7b\u7684\u5934\u6587\u4ef6&#xff0c;\u6210\u5458\u51fd\u6570\u5728\u540e\u9762\u5b9a\u4e49\u65f6\u8be6\u7ec6\u4ecb\u7ecd&#xff0c;\u8fd9\u91cc\u4ecb\u7ecd\u4e00\u4e0b\u903b\u8f91\u7c7b\u7684\u6210\u5458\u53d8\u91cf&#xff1a;<\/p>\n<ul>\n<li>FunCallBack\u4e3a\u8981\u6ce8\u518c\u7684\u56de\u8c03\u51fd\u6570\u7c7b\u578b&#xff0c;\u5176\u53c2\u6570\u4e3a\u4f1a\u8bddSession\u7c7b\u667a\u80fd\u6307\u9488&#xff0c;\u6d88\u606fid&#xff0c;\u4ee5\u53ca\u6d88\u606f\u5185\u5bb9\u3002<\/li>\n<li>_msg_que\u4e3a\u903b\u8f91\u961f\u5217&#xff0c;\u5176\u4e2d\u7684\u5143\u7d20\u76f8\u5f53\u4e8eRecvNode&#xff0c;\u53ea\u4e0d\u8fc7\u4e3a\u4e86\u5b9e\u73b0\u4f2a\u95ed\u5305&#xff0c;\u6240\u4ee5\u521b\u5efa\u4e00\u4e2aLogicNode\u7c7b&#xff0c;\u5305\u542bSession\u7684\u667a\u80fd\u6307\u9488\u9632\u6b62\u88ab\u63d0\u524d\u91ca\u653e<\/li>\n<li>_mutex\u00a0\u4e3a\u4fdd\u8bc1\u903b\u8f91\u961f\u5217\u5b89\u5168\u7684\u4e92\u65a5\u91cf<\/li>\n<li>_consume\u8868\u793a\u6d88\u8d39\u8005\u6761\u4ef6\u53d8\u91cf&#xff0c;\u7528\u6765\u63a7\u5236\u5f53\u903b\u8f91\u961f\u5217\u4e3a\u7a7a\u65f6\u4fdd\u8bc1\u7ebf\u7a0b\u6682\u65f6\u6302\u8d77\u7b49\u5f85&#xff0c;\u4e0d\u8981\u5e72\u6270\u5176\u4ed6\u7ebf\u7a0b\u3002<\/li>\n<li>_fun_callbacks\u7528\u4e8e\u5b58\u50a8\u6d88\u606fid\u548c\u5bf9\u5e94\u6d88\u606fid\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u6839\u636eid\u67e5\u627e\u5bf9\u5e94\u7684\u903b\u8f91\u5904\u7406\u51fd\u6570\u3002<\/li>\n<li>_worker_thread\u8868\u793a\u5de5\u4f5c\u7ebf\u7a0b&#xff0c;\u7528\u6765\u4ece\u903b\u8f91\u961f\u5217\u4e2d\u53d6\u6570\u636e\u5e76\u6267\u884c\u56de\u8c03\u51fd\u6570\u3002<\/li>\n<li>_b_stop\u8868\u793a\u6536\u5230\u5916\u90e8\u7684\u505c\u6b62\u4fe1\u53f7&#xff0c;\u903b\u8f91\u7c7b\u8981\u4e2d\u6b62\u5de5\u4f5c\u7ebf\u7a0b\u5e76\u4f18\u96c5\u9000\u51fa\u3002<\/li>\n<\/ul>\n<p>_fun_callback\u7684\u5b9a\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<p>typedef std::function&lt;void(std::shared_ptr&lt;CSession&gt;, const short&amp; msg_id, const std::string&amp; msg_data)&gt; FunCallBack;<\/p>\n<p>std::map&lt;short, FunCallBack&gt; _fun_callback;<\/p>\n<p>_fun_callback\u662f\u4e00\u4e2amap&#xff0c;\u503c\u548c\u952e\u7684\u7c7b\u578b\u5206\u522b\u4e3ashort\u548cFunCallBack&#xff0c;\u524d\u8005\u662f\u6d88\u606fid\u7684\u7c7b\u578b&#xff0c;\u540e\u8005\u662f\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p>FunCallBack\u4f7f\u7528\u4e86C&#043;&#043;11\u65b0\u7279\u6027std::function\u6765\u58f0\u660e\u4e86\u4e00\u4e2a\u51fd\u6570\u7b7e\u540d&#xff0c;\u8868\u793a\u63a5\u53d7\u4e00\u4e2a\u00a0shared_ptr\u00a0\u6307\u5411\u00a0CSession\u3001\u4e00\u4e2a\u00a0short\u00a0\u7c7b\u578b\u7684\u6d88\u606f ID \u548c\u4e00\u4e2a\u00a0std::string\u00a0\u7c7b\u578b\u7684\u6d88\u606f\u6570\u636e\u7684\u53ef\u8c03\u7528\u5bf9\u8c61<\/p>\n<p>std::function\u7684\u8be6\u7ec6\u4ecb\u7ecd\u53ef\u53c2\u8003<\/p>\n<p>\u7231\u5403\u571f\u8c46&#xff1a;C&#043;&#043;11\u65b0\u7279\u60270 \u8d5e\u540c \u00b7 0 \u8bc4\u8bba\u6587\u7ae0<\/p>\n<p>\u56e0\u4e3a\u903b\u8f91\u961f\u5217\u5b58\u50a8\u7684\u6210\u5458\u9700\u8981\u5305\u542b\u5bf9\u5e94session\u548c\u6d88\u606f\u5185\u5bb9&#xff0c;\u6240\u4ee5\u9700\u8981\u91cd\u65b0\u5b9a\u4e49\u4e00\u4e2a\u903b\u8f91\u8282\u70b9&#xff0c;\u7528\u4e8e\u5b58\u50a8\u4f1a\u8bddsession\u4e2d\u5c06\u5bf9\u5e94\u6d88\u606f\u6295\u9012\u81f3\u903b\u8f91\u961f\u5217\u7684\u5185\u5bb9&#xff0c;\u5305\u62ec\u8be5session\u548c\u6d88\u606f\u5185\u5bb9\u3002<\/p>\n<h4 id=\"h_957175334_13\">b. LogicNode<\/h4>\n<p>class LogicNode {<br \/>\nfriend class LogicSystem;<br \/>\npublic:<br \/>\nLogicNode(std::shared_ptr&lt;CSession&gt;, std::shared_ptr&lt;RecvNode&gt;);<br \/>\nprivate:<br \/>\nstd::shared_ptr&lt;CSession&gt; _session;<br \/>\nstd::shared_ptr&lt;RecvNode&gt; _recvnode;<br \/>\n};<\/p>\n<p>LogicNode\u7c7b\u5b9a\u4e49\u5728CSession.h\u4e2d&#xff0c;CSession.h\u5728\u6587\u7ae0\u540e\u9762\u4ecb\u7ecd<\/p>\n<ul>\n<li>_session&#xff1a;\u58f0\u660eSession\u7684\u667a\u80fd\u6307\u9488&#xff0c;\u5b9e\u73b0\u4f2a\u95ed\u5305&#xff0c;\u9632\u6b62Session\u88ab\u63d0\u524d\u91ca\u653e<\/li>\n<li>_recvnode&#xff1a;\u63a5\u6536\u6d88\u606f\u8282\u70b9\u7684\u667a\u80fd\u6307\u9488<\/li>\n<\/ul>\n<p>\u6784\u9020\u51fd\u6570\u5982\u4e0b&#xff1a;<\/p>\n<p>LogicNode::LogicNode(std::shared_ptr&lt;CSession&gt; session, std::shared_ptr&lt;RecvNode&gt; recvnode) :<br \/>\n_session(session), _recvnode(recvnode){}<\/p>\n<p>\u7528\u4e8e\u63a5\u6536\u4e00\u4e2asession\u4f1a\u8bdd\u548c\u8be5\u4f1a\u8bdd\u6295\u9012\u7684\u6d88\u606f\u5185\u5bb9<\/p>\n<h4 id=\"h_957175334_14\">c. \u6210\u5458\u51fd\u6570\u5b9e\u73b0<\/h4>\n<p>LogicSystem::LogicSystem() :_b_stop(false) {<br \/>\nRegisterCallBacks();<br \/>\n_worker_thread &#061; std::thread(&amp;LogicSystem::DealMsg, this);<br \/>\n}<\/p>\n<ul>\n<li>_b_stop\u521d\u59cb\u5316\u4e3afalse&#xff0c;\u5f53\u524d\u5de5\u4f5c\u7ebf\u7a0b\u4e0d\u4f1a\u505c\u6b62<\/li>\n<li>RegisterCallBacks()&#xff1a;\u8c03\u7528\u56de\u8c03\u6ce8\u518c\u51fd\u6570&#xff1a;\u8fd9\u4e2a\u51fd\u6570\u7528\u4e8e\u6ce8\u518c\u6d88\u606f\u5904\u7406\u7684\u56de\u8c03\u51fd\u6570\u3002\u5c06\u4e0d\u540c\u7684\u6d88\u606f ID \u548c\u5bf9\u5e94\u7684\u5904\u7406\u51fd\u6570\u5173\u8054\u8d77\u6765&#xff0c;\u4ee5\u4fbf\u5728\u5904\u7406\u6d88\u606f\u65f6\u80fd\u591f\u627e\u5230\u6b63\u786e\u7684\u51fd\u6570\u3002<\/li>\n<li>_worker_thread &#061; std::thread(&amp;LogicSystem::DealMsg, this);\n<ul>\n<li>\u521b\u5efa\u5de5\u4f5c\u7ebf\u7a0b&#xff1a;\u4f7f\u7528 std::thread \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u3002<\/li>\n<li>&amp;LogicSystem::DealMsg&#xff1a;\u6307\u5b9a\u8981\u5728\u65b0\u7ebf\u7a0b\u4e2d\u6267\u884c\u7684\u6210\u5458\u51fd\u6570&#xff0c;\u5373 DealMsg&#xff0c;\u8be5\u51fd\u6570\u5c06\u8d1f\u8d23\u4ece\u6d88\u606f\u961f\u5217\u4e2d\u63d0\u53d6\u6d88\u606f\u5e76\u5904\u7406\u5b83\u3002<\/li>\n<li>this&#xff1a;\u4f20\u9012\u5f53\u524d\u5bf9\u8c61\u7684\u6307\u9488&#xff0c;\u4ee5\u4fbf DealMsg \u53ef\u4ee5\u8bbf\u95ee LogicSystem \u7684\u6210\u5458\u53d8\u91cf\u548c\u51fd\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>void LogicSystem::RegisterCallBacks() {<br \/>\n_fun_callback[MSG_HELLO_WORD] &#061; std::bind(&amp;LogicSystem::HelloWordCallBack,<br \/>\nthis, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);<br \/>\n}<\/p>\n<p>\u5c06\u6d88\u606fID&#xff1a;MSG_HELLO_WORD\u6620\u5c04\u81f3\u56de\u8c03\u51fd\u6570HelloWordCallBack&#xff0c;\u5e76\u5b58\u50a8\u81f3_fun_callback\u3002\u5f53\u63a5\u6536\u5230\u6d88\u606fMSG_HELLO_WORD\u65f6&#xff0c;\u7cfb\u7edf\u4f1a\u8c03\u7528 HelloWordCallBack \u65b9\u6cd5\u6765\u5904\u7406\u6d88\u606f\u3002<\/p>\n<p>void LogicSystem::HelloWordCallBack(std::shared_ptr&lt;CSession&gt; session, const short&amp; msg_id, const std::string&amp; msg_data) {<br \/>\nJson::Reader reader;<br \/>\nJson::Value root;<br \/>\nreader.parse(msg_data, root);<br \/>\nstd::cout &lt;&lt; &#034;receive msg id is &#034; &lt;&lt; root[&#034;id&#034;].asInt() &lt;&lt; &#034; msg data is &#034;<br \/>\n&lt;&lt; root[&#034;data&#034;].asString() &lt;&lt; std::endl;<br \/>\nroot[&#034;data&#034;] &#061; &#034;server has receive msg, msg data is &#034; &#043; root[&#034;data&#034;].asString();<\/p>\n<p>std::string return_str &#061; root.toStyledString();<br \/>\nsession-&gt;Send(return_str, root[&#034;id&#034;].asInt());<br \/>\n}<\/p>\n<ul>\n<li>HelloWordCallBack\u662f\u5bf9\u5e94\u6d88\u606fid&#xff1a;MSG_HELLO_WORD\u7684\u56de\u8c03\u51fd\u6570\n<ul>\n<li>\u4f7f\u7528jsoncpp\u5e93\u5305\u88c5\u6216\u8005\u89e3\u6790\u6570\u636e<\/li>\n<li>\u8c03\u7528 parse \u65b9\u6cd5\u5c06\u00a0msg_data\u00a0\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a JSON \u683c\u5f0f&#xff0c;\u7ed3\u679c\u5b58\u50a8\u5728 root \u4e2d<\/li>\n<li>\u901a\u8fc7session\u7c7b\u7684send\u56de\u4f20\u6d88\u606f<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>void LogicSystem::DealMsg() {<br \/>\nfor (;;) {<br \/>\nstd::unique_lock&lt;std::mutex&gt; unique_lk(_mutex);<br \/>\n\/\/ \u5faa\u73af\u4f1a\u8017\u8d39\u8d44\u6e90&#xff0c;\u8fd9\u91cc\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u6302\u8d77\u7ebf\u7a0b&#xff0c;\u5e76\u91ca\u653e\u9501&#xff0c;\u76f4\u81f3\u88ab\u5524\u9192<br \/>\nwhile (_msg_que.empty() and !_b_stop) {<br \/>\n_consume.wait(unique_lk);<br \/>\n}<\/p>\n<p>\/\/ \u5224\u65ad\u5982\u679c\u4e3a\u5173\u95ed\u72b6\u6001&#xff0c;\u53d6\u51fa\u903b\u8f91\u961f\u5217\u6240\u6709\u6570\u636e\u5904\u7406\u5e76\u9000\u51fa\u5faa\u73af<br \/>\nif (_b_stop) {<br \/>\nwhile (!_msg_que.empty()) {<br \/>\nauto msg_node &#061; _msg_que.front();<br \/>\nstd::cout &lt;&lt; &#034;recv msg id is &#034; &lt;&lt; msg_node-&gt;_recvnode-&gt;GetMsgID() &lt;&lt; std::endl;<br \/>\nauto call_back_iter &#061; _fun_callback.find(msg_node-&gt;_recvnode-&gt;GetMsgID());<br \/>\nif (call_back_iter &#061;&#061; _fun_callback.end()) { \/\/ \u5982\u679c\u672a\u6ce8\u518c\u5bf9\u5e94\u6d88\u606fid\u7684\u56de\u8c03\u51fd\u6570<br \/>\n_msg_que.pop(); \/\/ \u91ca\u653e\u8be5\u6d88\u606f<br \/>\ncontinue; \/\/ \u5904\u7406\u4e0b\u4e00\u6761\u6d88\u606f<br \/>\n}<br \/>\n\/\/ \u53d6\u51fa\u5bf9\u5e94\u6d88\u606fid\u7684\u56de\u8c03\u51fd\u6570<br \/>\ncall_back_iter-&gt;second(msg_node-&gt;_session, msg_node-&gt;_recvnode-&gt;GetMsgID(),<br \/>\nstd::string(msg_node-&gt;_recvnode-&gt;_data, msg_node-&gt;_recvnode-&gt;_total_len));<br \/>\n_msg_que.pop();<br \/>\n}<br \/>\nbreak;<br \/>\n}<\/p>\n<p>\/\/ \u5982\u679c\u6ca1\u6709\u505c\u670d&#xff0c;\u4e14\u961f\u5217\u4e0d\u4e3a\u7a7a<br \/>\nauto msg_node &#061; _msg_que.front();<br \/>\nstd::cout &lt;&lt; &#034;recv msg id is &#034; &lt;&lt; msg_node-&gt;_recvnode-&gt;GetMsgID() &lt;&lt; std::endl;<\/p>\n<p>auto call_back_iter &#061; _fun_callback.find(msg_node-&gt;_recvnode-&gt;GetMsgID());<br \/>\n\/\/ \u82e5\u961f\u5217\u4e2d\u6ca1\u6709\u8be5\u6d88\u606f\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u5219\u91ca\u653e\u8be5\u6d88\u606f\u5e76\u5904\u7406\u4e0b\u4e00\u6761<br \/>\n\/\/ \u6ce8\u518c_fun_callback\u65f6&#xff0c;\u5c06\u6d88\u606fid\u4ee5\u53ca\u5339\u914d\u7684\u56de\u8c03\u51fd\u6570\u7ed1\u5b9a\u5728\u4e00\u8d77<br \/>\nif (call_back_iter &#061;&#061; _fun_callback.end()) {<br \/>\n_msg_que.pop();<br \/>\ncontinue;<br \/>\n}<br \/>\n\/\/ \u6267\u884c\u6d88\u606f\u7c7b\u578b\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570<br \/>\ncall_back_iter-&gt;second(msg_node-&gt;_session, msg_node-&gt;_recvnode-&gt;GetMsgID(),<br \/>\nstd::string(msg_node-&gt;_recvnode-&gt;_data, msg_node-&gt;_recvnode-&gt;_total_len));<br \/>\n_msg_que.pop();<br \/>\n}<br \/>\n}<\/p>\n<p>\u5f53\u903b\u8f91\u7c7b\u5b9e\u4f8b\u5316\u540e&#xff0c;\u5f00\u59cb\u5faa\u73af\u8fd0\u884c\u6d88\u606f\u5904\u7406\u51fd\u6570DealMsg()<\/p>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u81ea\u7531\u52a0\u89e3\u9501&#xff0c;\u5b9a\u4e49\u4e00\u4e2aunique_lock\u7c7b\u7684\u9501unique_lk<\/p>\n<p>\u5f53\u903b\u8f91\u961f\u5217\u4e3a\u7a7a\u5e76\u4e14\u670d\u52a1\u5668\u672a\u505c\u6b62\u65f6&#xff0c;\u8fdb\u884c\u7b49\u5f85while\u5faa\u73af\u4e2d&#xff0c;\u56e0\u4e3awhile\u5faa\u73af\u4e5f\u4f1a\u5360\u7528\u8d44\u6e90&#xff0c;\u6240\u4ee5\u4f7f\u7528C&#043;&#043;11\u7279\u6027\u6761\u4ef6\u53d8\u91cf\u00a0std::condition_variable\u5c06\u7ebf\u7a0b\u6302\u8d77&#xff0c;\u8be5\u7ebf\u7a0b\u4e0d\u4f1a\u5360\u7528cpu\u8d44\u6e90&#xff0c;\u5e76\u91ca\u653e\u9501\u4f7f\u5f97\u5176\u4ed6\u7ebf\u7a0b\u53ef\u4ee5\u83b7\u53d6\u5171\u4eab\u8d44\u6e90&#xff0c;\u7ebf\u7a0b\u6302\u8d77\u6301\u7eed\u81f3\u88ab\u5524\u9192&#xff08;notify_one()\u6216\u8005notify_all()&#xff09;\u3002\u6761\u4ef6\u53d8\u91cf\u7684\u8be6\u7ec6\u4ecb\u7ecd\u53ef\u53c2\u8003<\/p>\n<p>\u7231\u5403\u571f\u8c46&#xff1a;C&#043;&#043;11\u65b0\u7279\u60270 \u8d5e\u540c \u00b7 0 \u8bc4\u8bba\u6587\u7ae0<\/p>\n<p>\u5f53\u7ebf\u7a0b\u88ab\u5524\u9192\u540e&#xff0c;\u5982\u679c\u670d\u52a1\u5668\u5173\u95ed&#xff0c;\u5219\u53d6\u51fa\u903b\u8f91\u961f\u5217\u6240\u6709\u6570\u636e\u5904\u7406\u5e76\u9000\u51fa\u5faa\u73af<\/p>\n<p>\u5f53\u7ebf\u7a0b\u88ab\u5524\u9192\u540e&#xff0c;\u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\u4e14\u670d\u52a1\u5668\u672a\u5173\u95ed&#xff0c;\u90a3\u4e48\u53d6\u51fa\u961f\u5217\u7b2c\u4e00\u4e2a\u6570\u636e&#xff0c;\u5e76\u5728\u6ce8\u518cmap\u4e2d\u67e5\u627e\u5bf9\u5e94\u6570\u636eid\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u5982\u679c\u6ca1\u6709\u627e\u5230&#xff0c;\u90a3\u4e48\u91ca\u653e\u8be5\u6570\u636e\u5e76\u5904\u7406\u4e0b\u4e00\u6761\u6d88\u606f&#xff1b;\u53cd\u4e4b&#xff0c;\u8c03\u7528\u8be5\u56de\u8c03\u51fd\u6570\u3002\u8fd9\u91cc\u6211\u4eec\u53ea\u4f1a\u4f20\u5165\u6d88\u606fid&#xff1a;MSG_HELLO_WORD&#xff0c;\u56de\u8c03\u51fd\u6570HelloWordCallBack\u4f1a\u88ab\u8c03\u7528<\/p>\n<p>LogicSystem::~LogicSystem() {<br \/>\nstd::cout &lt;&lt; &#034;\u903b\u8f91\u5c42\u6210\u529f\u6790\u6784&#034; &lt;&lt; std::endl;<br \/>\n_b_stop &#061; true;<br \/>\n_consume.notify_one();<br \/>\n_worker_thread.join();<br \/>\n}<\/p>\n<p>\u903b\u8f91\u7c7b\u7684\u6790\u6784\u51fd\u6570\u5728\u6240\u6709\u5de5\u4f5c\u7ebf\u7a0b\u8fd0\u884c\u7ed3\u675f\u540e\u88ab\u6267\u884c&#xff0c;\u4f46\u5de5\u4f5c\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u5904\u4e8e\u6302\u8d77\u72b6\u6001&#xff0c;\u6b64\u65f6\u9700\u8981\u4e00\u4e2a\u6fc0\u6d3b\u4fe1\u53f7\u6253\u65ad_consume\u7684wait\u72b6\u6001&#xff08;\u5728\u8be5\u547d\u4ee4\u524d\u4e00\u6b65\u5c06_b_stop\u7f6e\u4e3atrue&#xff09;<\/p>\n<p>void LogicSystem::PostMsgToQue(std::shared_ptr&lt;LogicNode&gt; msg) {<br \/>\nstd::unique_lock&lt;std::mutex&gt; unique_lk(_mutex); \/\/ \u9501\u5b9a\u4e92\u65a5\u91cf\u4ee5\u4fdd\u62a4\u5171\u4eab\u6570\u636e<br \/>\n_msg_que.push(msg); \/\/ \u5c06\u6d88\u606f\u63a8\u5165\u961f\u5217<\/p>\n<p>if (_msg_que.size() &#061;&#061; 1) { \/\/ \u5982\u679c\u8fd9\u662f\u961f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u6d88\u606f<br \/>\n_consume.notify_one(); \/\/ \u901a\u77e5\u6d88\u8d39\u8005\u7ebf\u7a0b&#xff0c;\u7ed3\u675fwait\u72b6\u6001<br \/>\n}<br \/>\n}<\/p>\n<p>\u8be5\u51fd\u6570\u5728Session\u4f1a\u8bdd\u4e2d\u5c06\u5bf9\u5e94session\u6307\u9488\u548c\u6d88\u606f\u5185\u5bb9\u6295\u9012\u81f3\u903b\u8f91\u961f\u5217\u5904\u7406\u3002<\/p>\n<p>LogicSystem&amp; LogicSystem::GetInstance() {<br \/>\n\/\/ \u8fd4\u56de\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u5728C&#043;&#043;11\u53ca\u4ee5\u4e0a\u662f\u7ebf\u7a0b\u5b89\u5168\u7684<br \/>\nstatic LogicSystem instance;<br \/>\nreturn instance;<br \/>\n}<\/p>\n<p>LogicSystem\u7c7b\u4e5f\u662f\u4e00\u4e2a\u5355\u4f8b\u6a21\u5f0f&#xff0c;\u6211\u8fd9\u91cc\u4f7f\u7528\u4e86\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u6765\u5b9a\u4e49\u5355\u4f8b\u6a21\u5f0f\u3002\u6211\u4eec\u53ea\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u9759\u6001\u6210\u5458\u51fd\u6570GetInstance()&#xff0c;\u5728\u8be5\u51fd\u6570\u4e2d\u8fd4\u56de\u5c40\u90e8\u9759\u6001\u53d8\u91cfinstance&#xff0c;instance\u662fLogicSystem \u7684\u5b9e\u4f8b\u3002\u8be5\u65b9\u6cd5\u53ea\u80fd\u5728C&#043;&#043;11\u53ca\u4ee5\u540e\u7684\u5e73\u53f0\u624d\u53ef\u4ee5\u4f7f\u7528&#xff0c;\u56e0\u4e3a\u8fd4\u56de\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u53ea\u6709\u5728C&#043;&#043;11\u53ca\u4ee5\u4e0a\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<p>\u903b\u8f91\u7c7b\u7684\u8be6\u7ec6\u4ecb\u7ecd\u53ef\u53c2\u8003&#xff1a;<\/p>\n<p>\u7231\u5403\u571f\u8c46&#xff1a;\u7f51\u7edc\u7f16\u7a0b&#xff08;12&#xff09;\u2014\u2014\u5b8c\u5584\u7c98\u5305\u5904\u7406\u64cd\u4f5c&#043;asio\u5e95\u5c42\u901a\u4fe1\u8fc7\u7a0b2 \u8d5e\u540c \u00b7 0 \u8bc4\u8bba\u6587\u7ae0<\/p>\n<p>\u7231\u5403\u571f\u8c46&#xff1a;\u7f51\u7edc\u7f16\u7a0b&#xff08;14&#xff09;\u2014\u2014\u57fa\u4e8e\u5355\u4f8b\u6a21\u677f\u5b9e\u73b0\u7684\u903b\u8f91\u5c421 \u8d5e\u540c \u00b7 0 \u8bc4\u8bba\u6587\u7ae0<\/p>\n<h3 id=\"h_957175334_15\">6. CSession<\/h3>\n<p>CSession\u7528\u4e8e\u63a5\u6536\u5ba2\u6237\u7aef\u7684\u8fde\u63a5&#xff0c;\u5e76\u5904\u7406\u5f02\u6b65\u6536\u53d1&#xff0c;\u5728\u8be5\u7c7b\u4e2d&#xff0c;\u6784\u9020\u4e00\u4e2a\u6536\u534f\u7a0b\u7528\u4e8e\u63a5\u6536\u6570\u636e&#xff0c;\u4f46\u53d1\u4ecd\u7136\u901a\u8fc7Send\u51fd\u6570\u8fdb\u884c\u961f\u5217\u53d1\u9001&#xff0c;\u4e0d\u5728\u6784\u9020\u4e00\u4e2a\u53d1\u534f\u7a0b\u3002\u7406\u7531\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\u53ef\u4ee5\u6784\u5efa\u53d1\u9001\u534f\u7a0b\u7528\u4e8e\u53d1\u9001\u6570\u636e&#xff0c;\u4f46\u8003\u8651\u5230\u534f\u7a0b\u5f88\u5927\u7a0b\u5ea6\u4e0a\u5728\u4f7f\u7528\u5c42\u9762\u7b80\u5316\u4e86\u4ee3\u7801&#xff0c;\u4f46\u5b9e\u9645\u4e0a\u4e5f\u8017\u8d39\u8d44\u6e90&#xff0c;\u5982\u679c\u670d\u52a1\u5668\u8fdb\u884c\u9891\u7e41\u53d1\u9001\u64cd\u4f5c&#xff0c;\u4e14\u5904\u4e8e\u4efb\u4f55\u65f6\u523b\u90fd\u53ef\u80fd\u8c03\u7528\u7684\u60c5\u51b5\u4e0b&#xff0c;\u4f7f\u7528\u534f\u7a0b\u53ef\u80fd\u5e76\u4e0d\u65b9\u4fbf &#xff0c;\u56e0\u4e3a\u53ef\u80fd\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d&#xff0c;\u968f\u7740\u53d1\u9001\u6b21\u6570\u7684\u4e0a\u5347&#xff0c;\u542f\u52a8\u5f88\u591a\u534f\u7a0b<\/li>\n<li>\u8fd8\u6709\u4e00\u4e2a\u539f\u56e0\u662f&#xff1a;\u5982\u679c\u53d1\u9001\u7684\u65f6\u5019\u5904\u4e8e\u53e6\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u5982\u679c\u7528\u53d1\u9001\u534f\u7a0b\u7684\u8bdd&#xff0c;\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5176\u4ed6\u7ebf\u7a0b\u58f0\u660e\u5468\u671f\u7684\u6539\u53d8<\/li>\n<\/ul>\n<p>\u6240\u4ee5\u6211\u4eec\u8fd9\u91cc\u4ecd\u7136\u4f7f\u7528\u4e4b\u524d\u4ee3\u7801\u4e2d\u7528\u5230\u7684\u53d1\u9001\u961f\u5217\u7684\u65b9\u5f0f\u3002<\/p>\n<h4 id=\"h_957175334_16\">a. CSession.h<\/h4>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;boost\/asio.hpp&gt;<br \/>\n#include &lt;queue&gt;<br \/>\n#include &#034;MsgNode.h&#034;<br \/>\n#include &#034;LogicSystem.h&#034;<\/p>\n<p>class CServer;<\/p>\n<p>class CSession :public std::enable_shared_from_this&lt;CSession&gt;<br \/>\n{<br \/>\nprivate:<br \/>\nvoid HandleWrite(const boost::system::error_code&amp; error, std::shared_ptr&lt;CSession&gt; shared_self);<br \/>\nboost::asio::io_context&amp; _io_context;<br \/>\nCServer* _server;<br \/>\nboost::asio::ip::tcp::socket _socket;<br \/>\nstd::string _uuid;<br \/>\nbool _b_close;<br \/>\nstd::mutex _send_lock;<br \/>\nstd::queue&lt;std::shared_ptr&lt;SendNode&gt;&gt; _send_que;<br \/>\nstd::shared_ptr&lt;RecvNode&gt; _recv_msg_node;<br \/>\nstd::shared_ptr&lt;MsgNode&gt; _recv_head_node;<br \/>\npublic:<br \/>\nCSession(boost::asio::io_context&amp; io_context, CServer* server);<br \/>\n~CSession();<br \/>\nboost::asio::ip::tcp::socket&amp; GetSocket();<br \/>\nvoid Start();<br \/>\nstd::string&amp; GetUuid();<br \/>\nvoid Close();<br \/>\nvoid Send(const char* msg, short max_length, short msg_id);<br \/>\nvoid Send(std::string msg, short msg_id);<br \/>\n};<\/p>\n<p>class LogicNode {<br \/>\nfriend class LogicSystem;<br \/>\npublic:<br \/>\nLogicNode(std::shared_ptr&lt;CSession&gt;, std::shared_ptr&lt;RecvNode&gt;);<br \/>\nprivate:<br \/>\nstd::shared_ptr&lt;CSession&gt; _session;<br \/>\nstd::shared_ptr&lt;RecvNode&gt; _recvnode;<br \/>\n};<\/p>\n<p>\u540c\u6837&#xff0c;CSession\u7c7b\u7684\u6210\u5458\u51fd\u6570\u5728\u540e\u9762\u5b9a\u4e49\u65f6\u8be6\u7ec6\u4ecb\u7ecd&#xff0c;\u8fd9\u91cc\u4ec5\u4ecb\u7ecd\u6210\u5458\u53d8\u91cf&#xff1a;<\/p>\n<ul>\n<li>boost::asio::io_context&amp;\u00a0_io_context&#xff1a;\u7528\u4e8e\u63a5\u6536\u4ece\u7ebf\u7a0b\u6c60AsioIOServicePool\u4e2d\u63d0\u53d6\u7684ioc&#xff0c;\u6ce8\u610f&#xff0c;\u8fd9\u91cc\u662f\u5f15\u7528\u800c\u4e0d\u662f\u91cd\u65b0\u58f0\u660e\u4e00\u4e2aio_context\u65b0\u5b9e\u4f8b<\/li>\n<li>CServer* _server&#xff1a;\u63a5\u6536CServer\u6307\u9488&#xff0c;\u5f53Session\u6536\u53d1\u64cd\u4f5c\u51fa\u9519\u65f6&#xff0c;\u65b9\u4fbf\u8c03\u7528Server\u7684Clear\u51fd\u6570\u64e6\u9664\u8be5session\u4f1a\u8bdd\u4efb\u52a1<\/li>\n<li>_socket&#xff1a;\u6bcf\u4e2asession\u90fd\u6709\u4e00\u4e2a\u72ec\u7acb\u7684\u5957\u63a5\u5b57&#xff0c;\u8d1f\u8d23\u5f02\u6b65\u6536\u53d1<\/li>\n<li>_b_close&#xff1a;session\u4f1a\u8bdd\u4efb\u52a1\u662f\u5426\u7ed3\u675f\u7684\u6807\u5fd7\u4f4d<\/li>\n<li>_send_que&#xff1a;\u53d1\u9001\u961f\u5217&#xff0c;\u903b\u8f91\u5c42\u4f1a\u8c03\u7528session\u7684Send\u53d1\u9001\u51fd\u6570&#xff0c;Send\u51fd\u6570\u4f1a\u5c06\u9700\u8981\u53d1\u9001\u7684\u6d88\u606f\u6dfb\u52a0\u5230\u53d1\u9001\u961f\u5217&#xff0c;\u4fdd\u8bc1\u53d1\u9001\u7684\u987a\u5e8f\u6027<\/li>\n<li>_recv_msg_node&#xff1a;\u63a5\u6536\u8282\u70b9&#xff0c;\u5c06\u63a5\u6536\u7684\u6d88\u606f\u5b58\u50a8\u81f3\u8be5\u8282\u70b9&#xff0c;\u5305\u62ec\u6d88\u606fid\u548c\u6d88\u606f\u5185\u5bb9<\/li>\n<li>_recv_head_node&#xff1a;\u6d88\u606f\u5934\u8282\u70b9&#xff0c;\u5c06\u6536\u5230\u6d88\u606f\u7684id\u548c\u957f\u5ea6\u5b58\u50a8\u81f3\u8be5\u8282\u70b9<\/li>\n<li>LogicNode&#xff1a;\u903b\u8f91\u8282\u70b9&#xff0c;\u8d1f\u8d23\u5c06session\u548c\u6d88\u606f\u5185\u5bb9\u6295\u9012\u81f3\u903b\u8f91\u961f\u5217&#xff0c;\u903b\u8f91\u961f\u5217\u5143\u7d20\u7684\u683c\u5f0f\u4e3aLogicNode<\/li>\n<\/ul>\n<h4 id=\"h_957175334_17\">b. \u6210\u5458\u51fd\u6570\u5b9e\u73b0<\/h4>\n<p>CSession::CSession(boost::asio::io_context&amp; io_context, CServer* server) :<br \/>\n_io_context(io_context), _server(server), _socket(io_context), _b_close(false) {<br \/>\n\/\/ random_generator\u662f\u51fd\u6570\u5bf9\u8c61&#xff0c;\u52a0()\u5c31\u662f\u51fd\u6570&#xff0c;\u518d\u52a0\u4e00\u4e2a()\u5c31\u662f\u8c03\u7528\u8be5\u51fd\u6570<br \/>\nboost::uuids::uuid a_uuid &#061; boost::uuids::random_generator()();<br \/>\n_uuid &#061; boost::uuids::to_string(a_uuid);<br \/>\n_recv_head_node &#061; std::make_shared&lt;MsgNode&gt;(HEAD_TOTAL_LEN);<br \/>\n}<\/p>\n<ul>\n<li>CSession\u7684\u6784\u9020\u51fd\u6570\u9700\u8981\u63a5\u6536\u7ebf\u7a0b\u6c60\u4e2d\u7684ioc\u3001server\u6307\u9488&#xff0c;\u5e76\u5c06_b_close\u7f6e\u4e3afalse&#xff0c;\u8868\u793a\u8be5\u4f1a\u8bdd\u672a\u5173\u95ed<\/li>\n<li>\u901a\u8fc7boost\u7684random_generator()()\u51fd\u6570\u83b7\u53d6\u4e00\u4e2auuid&#xff0c;\u6bcf\u4e2asession\u90fd\u6709\u81ea\u5df1\u7684uuid&#xff0c;\u76f8\u5f53\u4e8e\u4e00\u4e2a\u540d\u79f0&#xff0c;\u901a\u8fc7\u8be5\u540d\u79f0\u53ef\u627e\u51fa\u5bf9\u5e94\u7684session\u4f1a\u8bdd&#xff0c;\u8be5\u51fd\u6570\u901a\u8fc7\u96ea\u82b1\u7b97\u6cd5\u5b9e\u73b0\u3002<\/li>\n<li>\u5b9a\u4e49\u6d88\u606f\u63a5\u6536\u8282\u70b9_recv_head_node&#xff0c;\u603b\u957f\u5ea64\u5b57\u8282&#xff0c;\u5305\u62ec\u6d88\u606fid\u548c\u6d88\u606f\u957f\u5ea6<\/li>\n<\/ul>\n<p>void CSession::Start() {<br \/>\n\/\/ \u9632\u6b62\u534f\u7a0b\u5904\u7406\u8fc7\u7a0b\u4e2d&#xff0c;\u667a\u80fd\u6307\u9488\u88ab\u610f\u5916\u91ca\u653e&#xff0c;\u901a\u8fc7\u667a\u80fd\u6307\u9488\u5b9e\u73b0\u4f2a\u95ed\u5305<br \/>\nauto shared_this &#061; shared_from_this();<br \/>\n\/\/ \u5f00\u542f\u534f\u7a0b\u63a5\u6536<br \/>\nboost::asio::co_spawn(_io_context, [self &#061; shared_from_this(), this]()-&gt;boost::asio::awaitable&lt;void&gt; {<br \/>\ntry {<br \/>\nfor (; !_b_close;) {<br \/>\n_recv_head_node-&gt;Clear();<br \/>\n\/\/ \u4ee5\u540c\u6b65\u7684\u65b9\u5f0f\u8c03\u7528\u5f02\u6b65\u8bfb&#xff0c;\u5e76\u5c06\u8bfb\u5230\u7684\u5b57\u8282\u6570&#xff08;4\u5b57\u8282&#xff09;\u8fd4\u56de&#xff08;\u4f7f\u7528\u7b80\u6613\u7c98\u5305\u5904\u7406\u65b9\u5f0f&#xff09;<br \/>\nstd::size_t n &#061; co_await boost::asio::async_read(_socket, boost::asio::buffer(<br \/>\n_recv_head_node-&gt;_data, HEAD_TOTAL_LEN), use_awaitable);<\/p>\n<p>if (n &#061;&#061; 0) { \/\/ \u5982\u679c\u6ca1\u8bfb\u5230\u6570\u636e<br \/>\nstd::cout &lt;&lt; &#034;receive peer closed&#034; &lt;&lt; std::endl;<br \/>\nClose();<br \/>\n_server-&gt;ClearSession(_uuid);<br \/>\nco_return; \/\/ \u7c7b\u4f3creturn&#xff0c;\u4f46co_return\u662f\u975e\u963b\u585e\u7684<br \/>\n}<\/p>\n<p>\/\/ \u83b7\u53d6\u5934\u90e8\u6d88\u606fid<br \/>\nshort msg_id &#061; 0;<br \/>\nmemcpy(&amp;msg_id, _recv_head_node-&gt;_data, HEAD_ID_LEN);<br \/>\n\/\/ \u8f6c\u6362\u7f51\u7edc\u5b57\u8282\u5e8f\u4e3a\u672c\u5730\u5b57\u8282\u5e8f<br \/>\nmsg_id &#061; boost::asio::detail::socket_ops::network_to_host_short(msg_id);<br \/>\nstd::cout &lt;&lt; &#034;msg id is &#034; &lt;&lt; msg_id &lt;&lt; std::endl;<br \/>\nif (msg_id &gt; MAX_LENGTH) {<br \/>\nstd::cout &lt;&lt; &#034;invalid msg id is &#034; &lt;&lt; msg_id &lt;&lt; std::endl;<br \/>\nClose();<br \/>\n_server-&gt;ClearSession(_uuid);<br \/>\nco_return;<br \/>\n}<\/p>\n<p>\/\/ \u83b7\u53d6\u6d88\u606f\u957f\u5ea6<br \/>\nshort msg_len &#061; 0;<br \/>\nmemcpy(&amp;msg_len, _recv_head_node-&gt;_data &#043; HEAD_ID_LEN, HEAD_DATA_LEN);<br \/>\nmsg_len &#061; boost::asio::detail::socket_ops::network_to_host_short(msg_len);<br \/>\nstd::cout &lt;&lt; &#034;msg len is &#034; &lt;&lt; msg_len &lt;&lt; std::endl;<br \/>\nif (msg_len &gt; MAX_LENGTH) {<br \/>\nstd::cout &lt;&lt; &#034;invalid msg len is &#034; &lt;&lt; msg_len &lt;&lt; std::endl;<br \/>\nClose();<br \/>\n_server-&gt;ClearSession(_uuid);<br \/>\nco_return;<br \/>\n}<\/p>\n<p>\/\/ \u83b7\u53d6\u6d88\u606f\u5185\u5bb9<br \/>\n_recv_msg_node &#061; std::make_shared&lt;RecvNode&gt;(msg_len, msg_id); \/\/ \u6784\u9020\u6d88\u606f\u8282\u70b9<br \/>\n\/\/ \u5c06\u5f02\u6b65\u8bfb\u4ee5\u540c\u6b65\u65b9\u5f0f\u8c03\u7528&#xff0c;\u6307\u5b9a\u8bfb\u53d6\u5b57\u8282\u6570<br \/>\nn &#061; co_await boost::asio::async_read(_socket, boost::asio::buffer(_recv_msg_node-&gt;_data,<br \/>\n_recv_msg_node-&gt;_total_len),use_awaitable);<br \/>\nif (n &#061;&#061; 0) {<br \/>\nstd::cout &lt;&lt; &#034;receive peer closed &#034; &lt;&lt; std::endl;<br \/>\nClose();<br \/>\n_server-&gt;ClearSession(_uuid);<br \/>\nco_return;<br \/>\n}<\/p>\n<p>_recv_msg_node-&gt;_data[_recv_msg_node-&gt;_total_len] &#061; &#039;\\\\0&#039;;<br \/>\nstd::cout &lt;&lt; &#034;receive data is &#034; &lt;&lt; _recv_msg_node-&gt;_data &lt;&lt; std::endl;<br \/>\n\/\/ \u6295\u9012\u81f3\u903b\u8f91\u7ebf\u7a0b<br \/>\nLogicSystem::GetInstance().PostMsgToQue(std::make_shared&lt;LogicNode&gt;(shared_from_this(), _recv_msg_node));<br \/>\n}<br \/>\n}<br \/>\ncatch (std::exception&amp; e) {<br \/>\nstd::cerr &lt;&lt; &#034;exception is &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\nClose();<br \/>\n_server-&gt;ClearSession(_uuid);<br \/>\n}<br \/>\n}, detached);<br \/>\n}<\/p>\n<p>Start()\u51fd\u6570\u7528\u4e8e\u63a5\u6536\u6d88\u606f\u5185\u5bb9&#xff0c;\u5e76\u901a\u8fc7\u53e6\u4e00\u79cd\u7b80\u4fbf\u7684\u7c98\u5305\u5904\u7406\u65b9\u5f0f\u83b7\u5f97\u6d88\u606fid\u3001\u6d88\u606f\u957f\u5ea6&#xff0c;\u53ef\u53c2\u8003&#xff1a;<\/p>\n<p>\u7231\u5403\u571f\u8c46&#xff1a;\u7f51\u7edc\u7f16\u7a0b&#xff08;11&#xff09;\u2014\u2014\u53e6\u4e00\u79cd\u7b80\u4fbf\u7684\u7c98\u5305\u5904\u7406\u65b9\u5f0f1 \u8d5e\u540c \u00b7 0 \u8bc4\u8bba\u6587\u7ae0<\/p>\n<p>\u8be5\u51fd\u6570\u5728Server\u4e2d\u6536\u5230\u5ba2\u6237\u7aef\u8fde\u63a5\u8bf7\u6c42\u540e&#xff0c;Server\u5c06\u4e00\u4e2a\u65b0\u7684session\u5206\u914d\u7ed9\u8be5\u5ba2\u6237\u7aef&#xff0c;\u5e76\u8c03\u7528Start()\u51fd\u6570\u3002<\/p>\n<p>\u9996\u5148&#xff0c;\u6784\u9020\u4e00\u4e2a\u6536\u534f\u7a0b&#xff1a;<\/p>\n<ul>\n<li>\u8fd9\u91cc\u901a\u8fc7boost::asio::co_spawn\u6784\u9020\u4e00\u4e2a\u6536\u534f\u7a0b&#xff0c;\u4f20\u5165ioc\u548clambda\u51fd\u6570&#xff0c;\u4ee5\u53cadetached\u3002\u6ce8\u610f&#xff0c;\u5982\u679c\u7528&#061;\u6355\u83b7\u6240\u6709\u53d8\u91cf&#xff0c;\u4f46\u662flambda\u8868\u8fbe\u5f0f\u5e76\u672a\u4f7f\u7528\u667a\u80fd\u6307\u9488\u5219\u4e0d\u4f1a\u589e\u52a0\u5f15\u7528\u8ba1\u6570&#xff0c;\u9664\u975e\u5728lambda\u5185\u90e8\u4f7f\u7528\u8fd9\u4e2a\u6307\u9488\u6216\u8005\u5728\u3010\u3011\u4e2d\u663e\u5f0f\u6355\u83b7\u8be5\u6307\u9488&#xff0c;\u6211\u4eec\u8fd9\u91cc\u901a\u8fc7\u3010self &#061; shared_from_this(), this\u3011\u663e\u5f0f\u589e\u52a0session\u7684\u5f15\u7528\u8ba1\u6570&#xff0c;\u9632\u6b62\u534f\u7a0b\u5904\u7406\u8fc7\u7a0b\u4e2d&#xff0c;\u667a\u80fd\u6307\u9488\u88ab\u610f\u5916\u91ca\u653e&#xff0c;\u901a\u8fc7\u667a\u80fd\u6307\u9488\u5b9e\u73b0\u4f2a\u95ed\u5305\u3002<\/li>\n<li>\u663e\u5f0f\u6307\u5b9a\u8fd4\u56de\u7c7b\u578b\u4e3aboost::asio::awaitable&lt;void&gt;&#xff0c;awaitable&lt;void&gt;\u7c7b\u578b\u5141\u8bb8\u51fd\u6570\u5728\u6267\u884c\u65f6\u53ef\u4ee5\u88ab\u6682\u505c\u548c\u6062\u590d&#xff0c;\u8fd9\u4f7f\u5f97\u5b83\u80fd\u591f\u4e0e\u00a0co_await\u00a0\u4e00\u8d77\u4f7f\u7528&#xff0c;\u6240\u4ee5\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u662fawaitable&lt;void&gt;\u3002<\/li>\n<li>session\u4f1a\u5728Start()\u51fd\u6570\u4e2d\u5faa\u73af\u6267\u884c\u6536\u534f\u7a0b&#xff0c;\u76f4\u81f3\u8be5\u4f1a\u8bdd\u88ab\u5173\u95ed&#xff0c;\u6bcf\u4e00\u6b21\u6267\u884c\u90fd\u9700\u8981\u5c06\u6d88\u606f\u5934\u7ed3\u70b9\u6e05\u7a7a&#xff0c;\u63a5\u6536\u4e0b\u4e00\u4e2a\u6d88\u606f\u3002\u901a\u8fc7co_await\u00a0\u5173\u952e\u5b57\u548cuse_awaitable\u00a0\u4ee5\u540c\u6b65\u7684\u65b9\u5f0f\u8c03\u7528\u5f02\u6b65\u8bfb\u64cd\u4f5casync_read&#xff0c;\u5e76\u5c06\u8bfb\u5230\u7684\u5b57\u8282\u6570&#xff08;4\u5b57\u8282&#xff09;\u8fd4\u56de&#xff0c;\u53ea\u6709\u8bfb\u52304\u5b57\u8282\u624d\u4f1a\u8fd4\u56de&#xff0c;\u5426\u5219\u5df2\u77e5\u6302\u8d77\u3002\u8fd9\u91cc\u548casynv_read_some\u6709\u4e00\u4e9b\u533a\u522b\u7136\u540e\u5206\u522b\u8bfb\u53d6\u6d88\u606fid\u3001\u6d88\u606f\u957f\u5ea6\u548c\u6d88\u606f\u5185\u5bb9&#xff0c;\u4e14\u9700\u8981\u5c06\u6d88\u606fid\u548c\u6d88\u606f\u957f\u5ea6\u4ece\u7f51\u7edc\u5e8f\u5217\u8f6c\u6362\u4e3a\u672c\u5730\u5e8f\u5217\u3002\u8fd9\u91cc\u4f7f\u7528async_read\u6307\u5b9a\u8bfb\u53d6\u5b57\u8282\u6570&#xff0c;\u800c\u4e0d\u662f\u50cf\u4e4b\u524d\u7c98\u5305\u5904\u7406\u4f7f\u7528async_read_some\u51fd\u6570&#xff0c;\u901a\u8fc7\u8c03\u7528\u56de\u8c03\u51fd\u6570\u5904\u7406\u7c98\u5305\u95ee\u9898\u3002<\/li>\n<li>\u6700\u540e&#xff0c;\u5728\u63a5\u6536\u5b8c\u6240\u6709\u6d88\u606f\u540e&#xff0c;\u5c06\u6d88\u606f\u6295\u9012\u81f3\u903b\u8f91\u961f\u5217\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/li>\n<\/ul>\n<p>\u5728\u534f\u7a0b\u4e2d&#xff0c;co_return\u7528\u6765\u8fd4\u56de\u4e00\u4e2a\u503c\u6216\u8868\u793a\u534f\u7a0b\u7ed3\u675f&#xff0c;\u5b83\u5c06\u628a\u503c\u4f20\u9012\u7ed9\u534f\u7a0b\u7684\u8fd4\u56de\u5bf9\u8c61&#xff08;\u5982\u679c\u8fd4\u56de\u7c7b\u578b\u4e0d\u4e3aboost::asio::awaitable&lt;void&gt;&#xff0c;\u5bf9\u8c61\u662fboost::asio::awaitable&#xff09;\u3002<\/p>\n<ul>\n<li>co_return \u53ea\u80fd\u5728\u534f\u7a0b\u4e2d\u4f7f\u7528&#xff0c;\u800c\u666e\u901a\u51fd\u6570\u4e2d\u4f7f\u7528\u7684\u662f return\u3002<\/li>\n<li>co_return \u5c06\u7ed3\u679c\u4f20\u9012\u7ed9\u534f\u7a0b\u7684\u627f\u8bfa&#xff08;promise&#xff09;\u5bf9\u8c61&#xff0c;\u8fd9\u4e2a\u5bf9\u8c61\u4f1a\u5c06\u503c\u4ea4\u7ed9\u534f\u7a0b\u7684\u8fd4\u56de\u7c7b\u578b<\/li>\n<\/ul>\n<p>\u534f\u7a0b\u5173\u952e\u5b57\u548c\u51fd\u6570use_awaitable\u3001co_spawn\u3001detached\u5982\u4f55\u4f7f\u7528\u8bf7\u53c2\u8003\u6587\u7ae0&#xff1a;<\/p>\n<p>\u7231\u5403\u571f\u8c46&#xff1a;\u7f51\u7edc\u7f16\u7a0b&#xff08;18&#xff09;\u2014\u2014\u4f7f\u7528asio\u534f\u7a0b\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668demo&#xff08;\u5b98\u65b9\u6848\u4f8b&#xff09;4 \u8d5e\u540c \u00b7 0 \u8bc4\u8bba\u6587\u7ae0<\/p>\n<p>\u6211\u4eec\u5df2\u7ecf\u6784\u5efa\u4e86\u4e00\u4e2a\u63a5\u6536\u534f\u7a0b\u7528\u4e8e\u63a5\u6536\u6570\u636e&#xff0c;\u90a3\u4e48\u6211\u4eec\u9700\u8981\u6784\u5efa\u53d1\u9001\u534f\u7a0b\u5417&#xff1f;<\/p>\n<ul>\n<li>\u53ef\u4ee5\u6784\u5efa\u53d1\u9001\u534f\u7a0b\u7528\u4e8e\u53d1\u9001\u6570\u636e&#xff0c;\u4f46\u8003\u8651\u5230\u534f\u7a0b\u5f88\u5927\u7a0b\u5ea6\u4e0a\u5728\u4f7f\u7528\u5c42\u9762\u7b80\u5316\u4e86\u4ee3\u7801&#xff0c;\u4f46\u5b9e\u9645\u4e0a\u4e5f\u8017\u8d39\u8d44\u6e90&#xff0c;\u5982\u679c\u670d\u52a1\u5668\u8fdb\u884c\u9891\u7e41\u53d1\u9001\u64cd\u4f5c&#xff0c;\u4e14\u5904\u4e8e\u4efb\u4f55\u65f6\u523b\u90fd\u53ef\u80fd\u8c03\u7528\u7684\u60c5\u51b5\u4e0b&#xff0c;\u4f7f\u7528\u534f\u7a0b\u53ef\u80fd\u5e76\u4e0d\u65b9\u4fbf &#xff0c;\u56e0\u4e3a\u53ef\u80fd\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d&#xff0c;\u968f\u7740\u53d1\u9001\u6b21\u6570\u7684\u4e0a\u5347&#xff0c;\u542f\u52a8\u5f88\u591a\u534f\u7a0b<\/li>\n<li>\u8fd8\u6709\u4e00\u4e2a\u539f\u56e0\u662f&#xff1a;\u5982\u679c\u53d1\u9001\u7684\u65f6\u5019\u5904\u4e8e\u53e6\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u5982\u679c\u7528\u53d1\u9001\u534f\u7a0b\u7684\u8bdd&#xff0c;\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5176\u4ed6\u7ebf\u7a0b\u58f0\u660e\u5468\u671f\u7684\u6539\u53d8<\/li>\n<\/ul>\n<p>\u6240\u4ee5\u6211\u4eec\u8fd9\u91cc\u4ecd\u7136\u4f7f\u7528\u4e4b\u524d\u4ee3\u7801\u4e2d\u7528\u5230\u7684\u53d1\u9001\u961f\u5217\u7684\u65b9\u5f0f&#xff0c;Send\u51fd\u6570\u4e24\u79cd\u91cd\u8f7d\u5b9a\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<p>void CSession::Send(const char* msg, short max_length, short msg_id) {<br \/>\nbool pending &#061; false; \/\/ \u53d1\u9001\u6807\u5fd7&#xff0c;true\u65f6\u6709\u672a\u5b8c\u6210\u7684\u53d1\u9001\u64cd\u4f5c&#xff0c;false\u4e3a\u7a7a<br \/>\nstd::unique_lock&lt;std::mutex&gt; lock(_send_lock);<br \/>\nint send_que_size &#061; _send_que.size();<br \/>\nif (send_que_size &gt; MAX_SENDQUE) {<br \/>\nstd::cout &lt;&lt; &#034;session: &#034; &lt;&lt; _uuid &lt;&lt; &#034; send que fulled, size is &#034; &lt;&lt; MAX_SENDQUE &lt;&lt; std::endl;<br \/>\nreturn;<br \/>\n}<\/p>\n<p>\/\/ \u5224\u65ad\u961f\u5217\u662f\u5426\u6709\u672a\u5b8c\u6210\u7684\u53d1\u9001\u64cd\u4f5c<br \/>\nif (_send_que.size() &gt; 0) {<br \/>\npending &#061; true;<br \/>\n}<br \/>\n_send_que.push(std::make_shared&lt;SendNode&gt;(msg, max_length, msg_id));<br \/>\nif (pending) { \/\/ \u5982\u679c\u6709\u672a\u5b8c\u6210\u7684\u53d1\u9001&#xff0c;\u76f4\u63a5\u8fd4\u56de<br \/>\nreturn;<br \/>\n}<\/p>\n<p>auto msgnode &#061; _send_que.front();<br \/>\n\/\/ \u6570\u636e\u53d6\u51fa\u6765\u4e4b\u540e\u5c31\u89e3\u9501&#xff0c;\u4ec5\u5728\u4f7f\u7528\u961f\u5217\u65f6\u52a0\u9501&#xff0c;\u907f\u514d\u4e0d\u76f8\u5e72\u7684\u903b\u8f91\u5360\u7528\u9501<br \/>\nlock.unlock();<br \/>\nboost::asio::async_write(_socket, boost::asio::buffer(msgnode-&gt;_data, msgnode-&gt;_total_len),<br \/>\nstd::bind(&amp;CSession::HandleWrite, this, std::placeholders::_1, shared_from_this()));<br \/>\n}<\/p>\n<p>void CSession::Send(std::string msg, short msg_id) {<br \/>\nbool pending &#061; false; \/\/ \u53d1\u9001\u6807\u5fd7&#xff0c;true\u65f6\u6709\u672a\u5b8c\u6210\u7684\u53d1\u9001\u64cd\u4f5c&#xff0c;false\u4e3a\u7a7a<br \/>\nstd::unique_lock&lt;std::mutex&gt; lock(_send_lock);<br \/>\nint send_que_size &#061; _send_que.size();<br \/>\nif (send_que_size &gt; MAX_SENDQUE) {<br \/>\nstd::cout &lt;&lt; &#034;session: &#034; &lt;&lt; _uuid &lt;&lt; &#034; send que fulled, size is &#034; &lt;&lt; MAX_SENDQUE &lt;&lt; std::endl;<br \/>\nreturn;<br \/>\n}<\/p>\n<p>\/\/ \u5224\u65ad\u961f\u5217\u662f\u5426\u6709\u672a\u5b8c\u6210\u7684\u53d1\u9001\u64cd\u4f5c<br \/>\nif (_send_que.size() &gt; 0) {<br \/>\npending &#061; true;<br \/>\n}<br \/>\n_send_que.push(std::make_shared&lt;SendNode&gt;(msg.c_str(), msg.length(), msg_id));<br \/>\nif (pending) { \/\/ \u5982\u679c\u6709\u672a\u5b8c\u6210\u7684\u53d1\u9001&#xff0c;\u76f4\u63a5\u8fd4\u56de<br \/>\nreturn;<br \/>\n}<\/p>\n<p>auto msgnode &#061; _send_que.front();<br \/>\n\/\/ \u6570\u636e\u53d6\u51fa\u6765\u4e4b\u540e\u5c31\u89e3\u9501&#xff0c;\u4ec5\u5728\u4f7f\u7528\u961f\u5217\u65f6\u52a0\u9501&#xff0c;\u907f\u514d\u4e0d\u76f8\u5e72\u7684\u903b\u8f91\u5360\u7528\u9501<br \/>\nlock.unlock();<br \/>\nboost::asio::async_write(_socket, boost::asio::buffer(msgnode-&gt;_data, msgnode-&gt;_total_len),<br \/>\nstd::bind(&amp;CSession::HandleWrite, this, std::placeholders::_1, shared_from_this()));<br \/>\n}<\/p>\n<p>Send()\u7684\u4e24\u79cd\u91cd\u8f7d&#xff0c;\u9488\u5bf9\u6570\u636e\u5185\u5bb9\u662fstring\u7c7b\u578b\u548cchar*\u7c7b\u578b\u3002<\/p>\n<p>\u9996\u5148\u8bbe\u7f6eunique_lock&#xff0c;\u65b9\u4fbf\u81ea\u7531\u52a0\u89e3\u9501&#xff1b;<\/p>\n<p>\u83b7\u53d6\u53d1\u9001\u961f\u5217\u5143\u7d20\u4e2a\u6570&#xff0c;\u5e76\u5224\u65ad\u5143\u7d20\u6570\u91cf\u662f\u5426\u5408\u6cd5&#xff0c;\u5982\u679c\u961f\u5217\u4e2d\u5df2\u5b58\u5728\u5143\u7d20&#xff0c;\u90a3\u4e48\u5c06\u672c\u6761\u6d88\u606f\u4f20\u5165\u961f\u5217\u7136\u540e\u8fd4\u56de&#xff0c;\u7b49\u5f85\u4e0a\u4e00\u6761\u6d88\u606f\u53d1\u9001\u6210\u529f&#xff1b;\u5982\u679c\u961f\u5217\u4e3a\u7a7a&#xff0c;\u90a3\u4e48\u53d6\u51fa\u961f\u5217\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5e76\u89e3\u9501&#xff08;\u6211\u4eec\u53ea\u9700\u8981\u5728\u4f7f\u7528\u961f\u5217\u65f6\u52a0\u9501&#xff0c;\u4e0d\u4f7f\u7528\u961f\u5217\u662f\u89e3\u9501&#xff0c;\u907f\u514d\u4e0d\u76f8\u5e72\u7684\u903b\u8f91\u5360\u7528\u9501&#xff09;&#xff0c;\u7136\u540e\u540c\u6b65\u5f02\u6b65\u5199async_write\u53d1\u9001\u6570\u636e&#xff08;\u6307\u5b9a\u5b57\u8282\u6570\u91cf&#xff09;&#xff0c;\u8fd9\u91cc\u4e0d\u9700\u8981\u6784\u9020\u5199\u534f\u7a0b&#xff0c;\u800c\u662f\u76f4\u63a5\u901a\u8fc7send\u51fd\u6570\u8c03\u7528\u5f02\u6b65\u5199\u8fdb\u884c\u53d1\u9001&#xff0c;\u53d1\u9001\u6210\u529f\u540e\u8c03\u7528\u56de\u8c03\u51fd\u6570HandleWrite\u3002<\/p>\n<p>void CSession::HandleWrite(const boost::system::error_code&amp; error, std::shared_ptr&lt;CSession&gt; shared_self) {<br \/>\ntry {<br \/>\nif (!error) {<br \/>\nstd::unique_lock&lt;std::mutex&gt; lock(_send_lock); \/\/ \u52a0\u9501\u4fdd\u62a4\u53d1\u9001\u961f\u5217<br \/>\n_send_que.pop(); \/\/ \u79fb\u9664\u4e0a\u4e00\u4e2a\u5df2\u53d1\u9001\u7684\u6d88\u606f&#xff08;send\u51fd\u6570\u4e2d\u7684\u5f02\u6b65\u53d1&#xff09;<br \/>\nif (!_send_que.empty()) { \/\/ \u82e5\u961f\u5217\u4e0d\u4e3a\u7a7a&#xff0c;\u5904\u7406\u4e0b\u4e00\u4e2a\u6d88\u606f<br \/>\nauto&amp; msgnode &#061; _send_que.front();<br \/>\nlock.unlock(); \/\/ \u89e3\u9501&#xff0c;\u907f\u514d\u4e0d\u76f8\u5e72\u903b\u8f91\u5360\u7528\u9501<br \/>\nboost::asio::async_write(_socket, boost::asio::buffer(msgnode-&gt;_data, msgnode-&gt;_total_len),<br \/>\nstd::bind(&amp;CSession::HandleWrite, this, std::placeholders::_1, shared_self));<br \/>\n}<br \/>\n}<br \/>\nelse {<br \/>\nstd::cerr &lt;&lt; &#034;handle write failed, error is &#034; &lt;&lt; error.what() &lt;&lt; std::endl;<br \/>\nClose();<br \/>\n_server-&gt;ClearSession(_uuid);<br \/>\n}<br \/>\n}<br \/>\ncatch (std::exception&amp; e) {<br \/>\nstd::cout &lt;&lt; &#034;exception is &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\nClose();<br \/>\n_server-&gt;ClearSession(_uuid);<br \/>\n}<br \/>\n}<\/p>\n<p>\u5982\u679c\u53d1\u9001\u6210\u529f&#xff0c;\u90a3\u4e48\u5148\u52a0\u9501\u4fdd\u62a4\u53d1\u9001\u961f\u5217&#xff0c;\u7136\u540e\u79fb\u9664\u4e0a\u4e00\u4e2a\u5df2\u53d1\u9001\u7684\u6d88\u606f&#xff0c;\u5e76\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a&#xff0c;\u5982\u679c\u4e0d\u4e3a\u7a7a&#xff0c;\u53d6\u51fa\u7b2c\u4e00\u4e2a\u6570\u636e\u5e76\u89e3\u9501&#xff0c;\u5e76\u8c03\u7528\u5f02\u6b65\u53d1\u51fd\u6570\u53d1\u9001\u8be5\u6570\u636e&#xff1b;\u5982\u679c\u53d1\u9001\u5931\u8d25&#xff0c;\u90a3\u4e48\u5173\u95edsession&#xff0c;\u5e76\u8c03\u7528Server\u7684ClearSession\u51fd\u6570\u64e6\u9664\u5bf9\u5e94uuid\u7684session\u3002<\/p>\n<p>boost::asio::ip::tcp::socket&amp; CSession::GetSocket() {<br \/>\nreturn _socket;<br \/>\n}<\/p>\n<p>\u8fd4\u56desession\u7684\u72ec\u7acb\u5957\u63a5\u5b57<\/p>\n<p>std::string&amp; CSession::GetUuid() {<br \/>\nreturn _uuid;<br \/>\n}<\/p>\n<p>\u8fd4\u56desession\u7684uuid<\/p>\n<p>void CSession::Close() {<br \/>\n_b_close &#061; true;<br \/>\n_socket.close();<br \/>\n}<\/p>\n<p>\u663e\u5f0f\u5173\u95edSession\u4f1a\u8bdd&#xff0c;\u9996\u5148\u5c06\u6807\u5fd7\u4f4d_b_close \u7f6e1&#xff0c;\u5e76\u5173\u95ed\u5957\u63a5\u5b57<\/p>\n<p>CSession::~CSession() {<br \/>\ntry {<br \/>\nstd::cout &lt;&lt; &#034;~CSession destruct&#034; &lt;&lt; std::endl;<br \/>\n}<br \/>\ncatch (std::exception&amp; e) {<br \/>\nstd::cerr &lt;&lt; &#034;exception is &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\n}<br \/>\n}<\/p>\n<h3 id=\"h_957175334_18\">7. \u4e3b\u51fd\u6570<\/h3>\n<p>#include &lt;iostream&gt;<br \/>\n#include &#034;CServer.h&#034;<br \/>\n#include &lt;csignal&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &#034;AsioIOServicePool.h&#034;<\/p>\n<p>int main()<br \/>\n{<br \/>\ntry {<br \/>\nauto pool &#061; AsioIOServicePool::GetInstance();<br \/>\nboost::asio::io_context io_context;<br \/>\nboost::asio::signal_set signals(io_context, SIGINT, SIGTERM);<br \/>\nsignals.async_wait([&amp;io_context, &amp;pool](auto,auto) {<br \/>\nio_context.stop();<br \/>\npool-&gt;Stop();<br \/>\n});<\/p>\n<p>CServer s(io_context, 10086);<br \/>\nio_context.run();<br \/>\n}<br \/>\ncatch (std::exception&amp; e) {<br \/>\nstd::cerr &lt;&lt; &#034;Exception: &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\n   }<br \/>\n}<\/p>\n<h3 id=\"h_957175334_19\">8. \u5ba2\u6237\u7aef<\/h3>\n<p>#include &lt;boost\/asio.hpp&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;json\/json.h&gt;<br \/>\n#include &lt;json\/value.h&gt;<br \/>\n#include &lt;json\/reader.h&gt;<\/p>\n<p>using namespace boost::asio::ip;<br \/>\nusing std::cout;<br \/>\nusing std::endl;<br \/>\nconst int MAX_LENGTH &#061; 1024 * 2; \/\/ \u53d1\u9001\u548c\u63a5\u6536\u7684\u957f\u5ea6\u4e3a1024 * 2\u5b57\u8282<br \/>\nconst int HEAD_LENGTH &#061; 2;<br \/>\nconst int HEAD_TOTAL &#061; 4;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    try {<br \/>\n        boost::asio::io_context ioc; \/\/ \u521b\u5efa\u4e0a\u4e0b\u6587\u670d\u52a1<br \/>\n        \/\/ 127.0.0.1\u662f\u672c\u673a\u7684\u56de\u8def\u5730\u5740&#xff0c;\u4e5f\u5c31\u662f\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u5728\u4e00\u4e2a\u673a\u5668\u4e0a<br \/>\n        tcp::endpoint remote_ep(address::from_string(&#034;127.0.0.1&#034;), 10086); \/\/ \u6784\u9020endpoint<br \/>\n        tcp::socket sock(ioc);<br \/>\n        boost::system::error_code error &#061; boost::asio::error::host_not_found; \/\/ \u9519\u8bef&#xff1a;\u4e3b\u673a\u672a\u627e\u5230<br \/>\n        sock.connect(remote_ep, error);<br \/>\n        if (error) {<br \/>\n            cout &lt;&lt; &#034;connect failed, code is &#034; &lt;&lt; error.value() &lt;&lt; &#034; error msg is &#034; &lt;&lt; error.message();<br \/>\n            return 0;<br \/>\n        }<\/p>\n<p>        Json::Value root;<br \/>\n        root[&#034;id&#034;] &#061; 1001;<br \/>\n        root[&#034;data&#034;] &#061; &#034;hello world&#034;;<br \/>\n        std::string request &#061; root.toStyledString();<br \/>\n        size_t request_length &#061; request.length();<br \/>\n        char send_data[MAX_LENGTH] &#061; { 0 };<br \/>\n        int msgid &#061; 1001;<br \/>\n        int msgid_host &#061; boost::asio::detail::socket_ops::host_to_network_short(msgid);<br \/>\n        memcpy(send_data, &amp;msgid_host, 2);<\/p>\n<p>        \/\/\u8f6c\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f<br \/>\n        int request_host_length &#061; boost::asio::detail::socket_ops::host_to_network_short(request_length);<br \/>\n        memcpy(send_data &#043; 2, &amp;request_host_length, 2);<br \/>\n        memcpy(send_data &#043; 4, request.c_str(), request_length);<br \/>\n        boost::asio::write(sock, boost::asio::buffer(send_data, request_length &#043; 4));<\/p>\n<p>        char reply_head[HEAD_TOTAL]; \/\/ \u9996\u5148\u8bfb\u53d6\u5bf9\u7aef\u53d1\u9001\u6d88\u606f\u7684\u603b\u957f\u5ea6<br \/>\n        size_t reply_length &#061; boost::asio::read(sock, boost::asio::buffer(reply_head, HEAD_TOTAL));<br \/>\n        msgid &#061; 0;<br \/>\n        memcpy(&amp;msgid, reply_head, HEAD_LENGTH);<br \/>\n        short msglen &#061; 0; \/\/ \u6d88\u606f\u603b\u957f\u5ea6<br \/>\n        memcpy(&amp;msglen, reply_head &#043; 2, HEAD_LENGTH); \/\/ \u5c06\u6d88\u606f\u603b\u957f\u5ea6\u8d4b\u503c\u7ed9msglen<br \/>\n        \/\/\u8f6c\u4e3a\u672c\u5730\u5b57\u8282\u5e8f<br \/>\n        msglen &#061; boost::asio::detail::socket_ops::network_to_host_short(msglen);<br \/>\n        char msg[MAX_LENGTH] &#061; { 0 }; \/\/ \u6784\u5efa\u6d88\u606f\u4f53&#xff08;\u4e0d\u542b\u6d88\u606f\u603b\u957f\u5ea6&#xff09;<br \/>\n        size_t msg_length &#061; boost::asio::read(sock, boost::asio::buffer(msg, msglen));<\/p>\n<p>        Json::Reader reader;<br \/>\n        reader.parse(std::string(msg, msg_length), root);<br \/>\n        std::cout &lt;&lt; &#034;msg id is &#034; &lt;&lt; root[&#034;id&#034;] &lt;&lt; &#034; msg is &#034; &lt;&lt; root[&#034;data&#034;] &lt;&lt; endl;<br \/>\n        getchar();<br \/>\n    }<br \/>\n    catch (std::exception&amp; e) {<br \/>\n        std::cerr &lt;&lt; &#034;Exception: &#034; &lt;&lt; e.what() &lt;&lt; endl;<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<h3 id=\"h_957175334_20\">9. \u6d4b\u8bd5<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"334\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419143636-6803b4f456377.png\" width=\"720\" \/><\/p>\n<\/p>\n<p>\u4ee3\u7801&#xff1a;<\/p>\n<p>https:\/\/github.com\/qiaobeibei\/CoroutineServer\u200bgithub.com\/qiaobeibei\/CoroutineServer<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.2k\u6b21\uff0c\u70b9\u8d5e11\u6b21\uff0c\u6536\u85cf32\u6b21\u3002\u53cd\u4e4b\uff0c\u8c03\u7528\u8be5\u56de\u8c03\u51fd\u6570\u3002\u4e0a\u56fe\u6240\u793a\u7684\u662f\u4e00\u4e2a\u5b8c\u6210\u7684\u670d\u52a1\u5668\u67b6\u6784\uff0c\u4e00\u822c\u9700\u8981\u5c06\u903b\u8f91\u5c42\u72ec\u7acb\u51fa\u6765\uff0c\u56e0\u4e3a\u5982\u679c\u5728\u89e3\u6790\u5b8c\u5bf9\u7aef\u6570\u636e\u540e\u9700\u8981\u6267\u884c\u4e00\u4e9b\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u73a9\u5bb6\u9700\u8981\u53e0\u52a0\u5404\u81eabuff\u6216\u8005\u6280\u80fd\uff0c\u6b64\u65f6\u53ef\u80fd\u4f1a\u8017\u65f61s\u751a\u81f3\u66f4\u591a\uff0c\u5982\u679c\u6ca1\u6709\u72ec\u7acb\u7684\u903b\u8f91\u5c42\u8fdb\u884c\u64cd\u4f5c\uff0c\u90a3\u4e48\u7cfb\u7edf\u4f1a\u4e00\u76f4\u505c\u7559\u5728\u6267\u884c\u56de\u8c03\u51fd\u6570\u90a3\u4e00\u6b65\uff0c\u9020\u6210\u3002\u4e00\u822c\u5728\u89e3\u6790\u5b8c\u5bf9\u7aef\u53d1\u9001\u7684\u6570\u636e\u4e4b\u540e\uff0c\u8fd8\u8981\u5bf9\u8be5\u8bf7\u6c42\u505a\u66f4\u8fdb\u4e00\u6b65\u5730\u5904\u7406\uff0c\u6bd4\u5982\u6839\u636e\u4e0d\u540c\u7684\u6d88\u606fid\u6267\u884c\u4e0d\u540c\u7684\u903b\u8f91\u5c42\u51fd\u6570\u6216\u4e0d\u540c\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u8bfb\u6570\u636e\u5e93\u3001\u5199\u6570\u636e\u5e93\uff0c\u8fd8\u6bd4\u5982\u6e38\u620f\u4e2d\u53ef\u80fd\u9700\u8981\u7ed9\u73a9\u5bb6\u53e0\u52a0\u4e0d\u540c\u7684buff\u3001\u589e\u52a0\u79ef\u5206\u7b49\u7b49\uff0c\u8fd9\u4e9b\u90fd\u9700\u8981\u4ea4\u7ed9\u3002_c++ asio<\/p>\n","protected":false},"author":2,"featured_media":28046,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2245,55,2246,43,396],"topic":[],"class_list":["post-28048","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-asio","tag-c","tag-2246","tag-43","tag-396"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7f51\u7edc\u7f16\u7a0b\uff0819\uff09\u2014\u2014C++\u4f7f\u7528asio\u534f\u7a0b\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/28048.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7f51\u7edc\u7f16\u7a0b\uff0819\uff09\u2014\u2014C++\u4f7f\u7528asio\u534f\u7a0b\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.2k\u6b21\uff0c\u70b9\u8d5e11\u6b21\uff0c\u6536\u85cf32\u6b21\u3002\u53cd\u4e4b\uff0c\u8c03\u7528\u8be5\u56de\u8c03\u51fd\u6570\u3002\u4e0a\u56fe\u6240\u793a\u7684\u662f\u4e00\u4e2a\u5b8c\u6210\u7684\u670d\u52a1\u5668\u67b6\u6784\uff0c\u4e00\u822c\u9700\u8981\u5c06\u903b\u8f91\u5c42\u72ec\u7acb\u51fa\u6765\uff0c\u56e0\u4e3a\u5982\u679c\u5728\u89e3\u6790\u5b8c\u5bf9\u7aef\u6570\u636e\u540e\u9700\u8981\u6267\u884c\u4e00\u4e9b\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u73a9\u5bb6\u9700\u8981\u53e0\u52a0\u5404\u81eabuff\u6216\u8005\u6280\u80fd\uff0c\u6b64\u65f6\u53ef\u80fd\u4f1a\u8017\u65f61s\u751a\u81f3\u66f4\u591a\uff0c\u5982\u679c\u6ca1\u6709\u72ec\u7acb\u7684\u903b\u8f91\u5c42\u8fdb\u884c\u64cd\u4f5c\uff0c\u90a3\u4e48\u7cfb\u7edf\u4f1a\u4e00\u76f4\u505c\u7559\u5728\u6267\u884c\u56de\u8c03\u51fd\u6570\u90a3\u4e00\u6b65\uff0c\u9020\u6210\u3002\u4e00\u822c\u5728\u89e3\u6790\u5b8c\u5bf9\u7aef\u53d1\u9001\u7684\u6570\u636e\u4e4b\u540e\uff0c\u8fd8\u8981\u5bf9\u8be5\u8bf7\u6c42\u505a\u66f4\u8fdb\u4e00\u6b65\u5730\u5904\u7406\uff0c\u6bd4\u5982\u6839\u636e\u4e0d\u540c\u7684\u6d88\u606fid\u6267\u884c\u4e0d\u540c\u7684\u903b\u8f91\u5c42\u51fd\u6570\u6216\u4e0d\u540c\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u8bfb\u6570\u636e\u5e93\u3001\u5199\u6570\u636e\u5e93\uff0c\u8fd8\u6bd4\u5982\u6e38\u620f\u4e2d\u53ef\u80fd\u9700\u8981\u7ed9\u73a9\u5bb6\u53e0\u52a0\u4e0d\u540c\u7684buff\u3001\u589e\u52a0\u79ef\u5206\u7b49\u7b49\uff0c\u8fd9\u4e9b\u90fd\u9700\u8981\u4ea4\u7ed9\u3002_c++ asio\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/28048.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-19T14:36:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419143636-6803b4f42ea25.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=\"23 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/28048.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/28048.html\",\"name\":\"\u7f51\u7edc\u7f16\u7a0b\uff0819\uff09\u2014\u2014C++\u4f7f\u7528asio\u534f\u7a0b\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-19T14:36:37+00:00\",\"dateModified\":\"2025-04-19T14:36:37+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/28048.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/28048.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/28048.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7f51\u7edc\u7f16\u7a0b\uff0819\uff09\u2014\u2014C++\u4f7f\u7528asio\u534f\u7a0b\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u7f51\u7edc\u7f16\u7a0b\uff0819\uff09\u2014\u2014C++\u4f7f\u7528asio\u534f\u7a0b\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/28048.html","og_locale":"zh_CN","og_type":"article","og_title":"\u7f51\u7edc\u7f16\u7a0b\uff0819\uff09\u2014\u2014C++\u4f7f\u7528asio\u534f\u7a0b\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.2k\u6b21\uff0c\u70b9\u8d5e11\u6b21\uff0c\u6536\u85cf32\u6b21\u3002\u53cd\u4e4b\uff0c\u8c03\u7528\u8be5\u56de\u8c03\u51fd\u6570\u3002\u4e0a\u56fe\u6240\u793a\u7684\u662f\u4e00\u4e2a\u5b8c\u6210\u7684\u670d\u52a1\u5668\u67b6\u6784\uff0c\u4e00\u822c\u9700\u8981\u5c06\u903b\u8f91\u5c42\u72ec\u7acb\u51fa\u6765\uff0c\u56e0\u4e3a\u5982\u679c\u5728\u89e3\u6790\u5b8c\u5bf9\u7aef\u6570\u636e\u540e\u9700\u8981\u6267\u884c\u4e00\u4e9b\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u73a9\u5bb6\u9700\u8981\u53e0\u52a0\u5404\u81eabuff\u6216\u8005\u6280\u80fd\uff0c\u6b64\u65f6\u53ef\u80fd\u4f1a\u8017\u65f61s\u751a\u81f3\u66f4\u591a\uff0c\u5982\u679c\u6ca1\u6709\u72ec\u7acb\u7684\u903b\u8f91\u5c42\u8fdb\u884c\u64cd\u4f5c\uff0c\u90a3\u4e48\u7cfb\u7edf\u4f1a\u4e00\u76f4\u505c\u7559\u5728\u6267\u884c\u56de\u8c03\u51fd\u6570\u90a3\u4e00\u6b65\uff0c\u9020\u6210\u3002\u4e00\u822c\u5728\u89e3\u6790\u5b8c\u5bf9\u7aef\u53d1\u9001\u7684\u6570\u636e\u4e4b\u540e\uff0c\u8fd8\u8981\u5bf9\u8be5\u8bf7\u6c42\u505a\u66f4\u8fdb\u4e00\u6b65\u5730\u5904\u7406\uff0c\u6bd4\u5982\u6839\u636e\u4e0d\u540c\u7684\u6d88\u606fid\u6267\u884c\u4e0d\u540c\u7684\u903b\u8f91\u5c42\u51fd\u6570\u6216\u4e0d\u540c\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u8bfb\u6570\u636e\u5e93\u3001\u5199\u6570\u636e\u5e93\uff0c\u8fd8\u6bd4\u5982\u6e38\u620f\u4e2d\u53ef\u80fd\u9700\u8981\u7ed9\u73a9\u5bb6\u53e0\u52a0\u4e0d\u540c\u7684buff\u3001\u589e\u52a0\u79ef\u5206\u7b49\u7b49\uff0c\u8fd9\u4e9b\u90fd\u9700\u8981\u4ea4\u7ed9\u3002_c++ asio","og_url":"https:\/\/www.wsisp.com\/helps\/28048.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-19T14:36:37+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250419143636-6803b4f42ea25.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"23 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/28048.html","url":"https:\/\/www.wsisp.com\/helps\/28048.html","name":"\u7f51\u7edc\u7f16\u7a0b\uff0819\uff09\u2014\u2014C++\u4f7f\u7528asio\u534f\u7a0b\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-19T14:36:37+00:00","dateModified":"2025-04-19T14:36:37+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/28048.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/28048.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/28048.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u7f51\u7edc\u7f16\u7a0b\uff0819\uff09\u2014\u2014C++\u4f7f\u7528asio\u534f\u7a0b\u5b9e\u73b0\u5e76\u53d1\u670d\u52a1\u5668"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/28048","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=28048"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/28048\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/28046"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=28048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=28048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=28048"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=28048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}