{"id":61188,"date":"2026-01-17T08:34:27","date_gmt":"2026-01-17T00:34:27","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/61188.html"},"modified":"2026-01-17T08:34:27","modified_gmt":"2026-01-17T00:34:27","slug":"%e5%9f%ba%e4%ba%8eboost%e5%ba%93c%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e4%b8%89%e5%8f%91%e9%80%81%e9%98%9f%e5%88%97%e5%92%8c%e7%b2%98%e5%8c%85%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/61188.html","title":{"rendered":"\u57fa\u4e8eboost\u5e93C++\u7f51\u7edc\u7f16\u7a0b(\u4e09):\u53d1\u9001\u961f\u5217\u548c\u7c98\u5305\u95ee\u9898"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"-toc\" style=\"margin-left:40px\">\n<p id=\"%E5%B0%81%E8%A3%85%E5%8F%91%E9%80%81%E9%98%9F%E5%88%97-toc\" style=\"margin-left:40px\">\u5c01\u88c5\u53d1\u9001\u961f\u5217<\/p>\n<p id=\"%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86-toc\" style=\"margin-left:80px\">\u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6<\/p>\n<p id=\"%E5%A4%84%E7%90%86%E7%B2%98%E5%8C%85%E9%97%AE%E9%A2%98-toc\" style=\"margin-left:40px\">\u5904\u7406\u7c98\u5305\u95ee\u9898<\/p>\n<p id=\"%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86-toc\" style=\"margin-left:80px\">\u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6<\/p>\n<p id=\"%E5%AD%97%E8%8A%82%E5%BA%8F%E5%A4%84%E7%90%86%E5%92%8C%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97%E7%9A%84%E6%8E%A7%E5%88%B6-toc\" style=\"margin-left:40px\">\u5b57\u8282\u5e8f\u5904\u7406\u548c\u6d88\u606f\u961f\u5217\u7684\u63a7\u5236<\/p>\n<p id=\"%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86-toc\" style=\"margin-left:80px\">\u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6<\/p>\n<p id=\"asio%20%E5%A4%84%E7%90%86%E7%B2%98%E5%8C%85%E7%9A%84%E7%AE%80%E5%8D%95%E5%8A%9E%E6%B3%95-toc\" style=\"margin-left:40px\">asio \u5904\u7406\u7c98\u5305\u7684\u7b80\u5355\u529e\u6cd5<\/p>\n<p id=\"%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86-toc\" style=\"margin-left:80px\">\u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6<\/p>\n<p id=\"%E6%80%BB%E7%BB%93-toc\" style=\"margin-left:40px\">\u603b\u7ed3<\/p>\n<hr id=\"hr-toc\" \/>\n<h3 id=\"%E5%B0%81%E8%A3%85%E5%8F%91%E9%80%81%E9%98%9F%E5%88%97\">\u5c01\u88c5\u53d1\u9001\u961f\u5217<\/h3>\n<h4 id=\"%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86\">\u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6<\/h4>\n<li>\u5168\u53cc\u5de5\u901a\u4fe1&#xff1a;\u533a\u522b\u4e8e\u300c\u8bfb\u2192\u5199\u300d\u7684\u5e94\u7b54\u5f0f\u4ea4\u4e92&#xff0c;\u5168\u53cc\u5de5\u5141\u8bb8\u670d\u52a1\u7aef \/ \u5ba2\u6237\u7aef\u72ec\u7acb\u53d1\u8d77\u8bfb\u5199\u64cd\u4f5c&#xff08;\u5982\u670d\u52a1\u7aef\u4e3b\u52a8\u63a8\u9001\u6570\u636e\u3001\u5ba2\u6237\u7aef\u968f\u65f6\u53d1\u6d88\u606f&#xff09;&#xff0c;\u9700\u72ec\u7acb\u7684\u53d1\u9001\u961f\u5217\u7ba1\u7406\u5f02\u6b65\u5199\u903b\u8f91&#xff1b;<\/li>\n<li>std::enable_shared_from_this&#xff1a;\u8ba9 CSession \u7ee7\u627f\u8be5\u7c7b\u540e&#xff0c;\u53ef\u901a\u8fc7 shared_from_this() \u83b7\u53d6\u81ea\u8eab\u7684 std::shared_ptr&#xff0c;\u89e3\u51b3\u5f02\u6b65\u56de\u8c03\u4e2d delete this \u5bfc\u81f4\u7684\u4e8c\u6b21\u6790\u6784\u95ee\u9898&#xff08;\u56de\u8c03\u6267\u884c\u671f\u95f4 Session \u5f15\u7528\u8ba1\u6570 &gt; 0&#xff0c;\u4e0d\u4f1a\u88ab\u6790\u6784&#xff09;&#xff1b;<\/li>\n<li>\u53d1\u9001\u961f\u5217 &#043; \u4e92\u65a5\u9501&#xff1a;\n<ul>\n<li>\u961f\u5217&#xff08;_send_que&#xff09;\u4fdd\u8bc1\u5f02\u6b65\u5199\u7684\u987a\u5e8f\u6027&#xff0c;\u907f\u514d\u8fde\u7eed\u53d1\u8d77 async_write \u5bfc\u81f4\u6570\u636e\u4e71\u5e8f&#xff1b;<\/li>\n<li>\u4e92\u65a5\u9501&#xff08;_mutex&#xff09;\u4fdd\u8bc1\u591a\u7ebf\u7a0b&#xff08;\u5982\u4e1a\u52a1\u7ebf\u7a0b\u8c03\u7528 Send&#xff09;\u64cd\u4f5c\u961f\u5217\u7684\u7ebf\u7a0b\u5b89\u5168&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u4f2a\u95ed\u5305&#xff1a;\u56de\u8c03\u4e2d\u7ed1\u5b9a shared_from_this()&#xff08;\u81ea\u8eab\u667a\u80fd\u6307\u9488&#xff09;&#xff0c;\u672c\u8d28\u662f\u5ef6\u957f Session \u751f\u547d\u5468\u671f&#xff0c;\u786e\u4fdd\u56de\u8c03\u6267\u884c\u65f6\u5bf9\u8c61\u672a\u88ab\u6790\u6784&#xff1b;<\/li>\n<li>\u4f1a\u8bdd\u7ba1\u7406&#xff1a;CServer \u7528 std::map&lt;std::string, std::shared_ptr&lt;CSession&gt;&gt; \u7ba1\u7406\u6240\u6709\u6d3b\u8dc3\u8fde\u63a5&#xff0c;ClearSession \u6e05\u7406\u5931\u6548\u8fde\u63a5&#xff08;\u5ba2\u6237\u7aef\u65ad\u5f00 \/ \u51fa\u9519\u65f6&#xff09;&#xff1b;<\/li>\n<li>UUID \u6807\u8bc6&#xff1a;\u4e3a\u6bcf\u4e2a Session \u751f\u6210\u552f\u4e00 UUID&#xff0c;\u4f5c\u4e3a\u4f1a\u8bdd\u7ba1\u7406\u7684\u952e\u503c&#xff0c;\u907f\u514d\u6307\u9488\u76f4\u63a5\u4f5c\u4e3a\u6807\u8bc6\u7684\u98ce\u9669\u3002<\/li>\n<p>\/\/ \u5b9e\u9645\u4f7f\u7528\u7684\u670d\u52a1\u5668\u5e76\u4e0d\u662f\u5e94\u7b54\u5f0f\u7684,\u800c\u662f\u5168\u53cc\u5de5\u901a\u4fe1\u65b9\u5f0f<br \/>\nclass CServer;<br \/>\nclass CSession:public std::enable_shared_from_this&lt;CSession&gt;{<\/p>\n<p>public:<br \/>\n    CSession(asio::io_context&amp; ioc, CServer* server):_socket(ioc), _server(server) {<br \/>\n        uuids::uuid a_uuid &#061; uuids::random_generator()();<br \/>\n        _uuid &#061; uuids::to_string(a_uuid);<br \/>\n    }<\/p>\n<p>    asio::ip::tcp::socket&amp; Socket() {<br \/>\n        return _socket;<br \/>\n    }<\/p>\n<p>    ~CSession() {<br \/>\n        std::cout &lt;&lt;&#034;session destruct delete this &#034; &lt;&lt; this &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    void Start();<br \/>\n    std::string&amp; GetUuid();<\/p>\n<p>private:<br \/>\n    void handle_read(const boost::system::error_code&amp; error, size_t bytes_transferred, std::shared_ptr&lt;CSession&gt; _self_shared);<br \/>\n    void handle_write(const boost::system::error_code&amp; error, std::shared_ptr&lt;CSession&gt; _self_shared);<br \/>\n    asio::ip::tcp::socket _socket;<br \/>\n    enum{max_length &#061; 1024};<br \/>\n    char _data[max_length];<br \/>\n    CServer* _server;<br \/>\n    std::string _uuid;<br \/>\n};<\/p>\n<p>class CServer {<\/p>\n<p>public:<br \/>\n    CServer(asio::io_context&amp; io_context, short port);<br \/>\n    void ClearSession(std::string);<\/p>\n<p>private:<br \/>\n    void HandleAccept(std::shared_ptr&lt;CSession&gt;, const boost::system::error_code&amp; error);\/\/ \u667a\u80fd\u6307\u9488\u4e5f\u662f\u5e2e\u52a9\u5b9e\u73b0\u4f2a\u95ed\u5305<br \/>\n    void StartAccept();<br \/>\n    asio::io_context&amp; _io_context;<br \/>\n    short _port;<br \/>\n    asio::ip::tcp::acceptor _acceptor;<br \/>\n    std::map&lt;std::string, std::shared_ptr&lt;CSession&gt;&gt; _sessions; \/\/ \u7528\u6765\u5b9e\u73b0\u4f2a\u95ed\u5305<br \/>\n};<\/p>\n<p>\/\/ \u6570\u636e\u8282\u70b9\u8bbe\u8ba1<br \/>\nclass MsgNode {<br \/>\n    friend class CSession;<\/p>\n<p>public:<br \/>\n    MsgNode(char* msg, int max_len) {<br \/>\n        _data &#061; new char[max_len];<br \/>\n        memcpy(_data, msg, max_len);<br \/>\n    }<\/p>\n<p>    ~MsgNode() {<br \/>\n        delete[] _data;<br \/>\n    }<\/p>\n<p>private:<br \/>\n    int _cur_len;<br \/>\n    int _max_len;<br \/>\n    char* _data;<br \/>\n};<\/p>\n<p>\/\/ \u9996\u5148\u8981\u5728CSession\u4e2d\u65b0\u589e\u4e00\u4e2a\u961f\u5217\u5b58\u50a8\u8981\u53d1\u9001\u7684\u6570\u636e,\u540c\u65f6\u65b0\u589e\u4e00\u4e2a\u53d1\u9001\u63a5\u53e3send<br \/>\nclass CSession:public std::enable_shared_from_this&lt;CSession&gt; {<br \/>\n    \/* &#8230;. *\/<\/p>\n<p>private:<br \/>\n    void Send(char* msg, int _max_length);<br \/>\n    std::queue&lt;std::shared_ptr&lt;MsgNode&gt;&gt; _send_que;<br \/>\n    std::mutex _mutex;<br \/>\n};<\/p>\n<p>\/\/ \u5b9e\u73b0\u53d1\u9001\u63a5\u53e3<br \/>\nvoid CSession::Send(char *msg, int _max_length) {<br \/>\n    bool pending &#061; false;<br \/>\n    std::lock_guard&lt;std::mutex&gt; lock(_mutex);<br \/>\n    if (_send_que.size()&gt; 0) {<br \/>\n        pending &#061; true;<br \/>\n    }<br \/>\n    _send_que.push(std::make_shared&lt;MsgNode&gt;(msg, _max_length));<br \/>\n    if (pending) {<br \/>\n        return;<br \/>\n    }<br \/>\n    asio::async_write(_socket, asio::buffer(_data, _max_length), std::bind(&amp;CSeesion::handleWrite, this, std::placeholders::_1, shared_from_this()));<br \/>\n}<\/p>\n<p>void CSeesion::handleWrite(const boost::system::error_code&amp; error, std::shared_ptr&lt;MsgNode&gt; _self_shared) {<br \/>\n    if (!error) {<br \/>\n        std::lock_guard&lt;std::mutex&gt; lock(_sned_lock);<br \/>\n        _send_que.pop();<br \/>\n        if (!_send_que.empty()) {<br \/>\n            auto&amp; msgNode &#061; _send_que.front();<br \/>\n            asio::asnyc_write(_socket, asio::buffer(msgNode-&gt;data, msgNode-&gt;_max_len),<br \/>\n                std::bind(&amp;CSeesion::handleWrtie, this, std::placeholders::_1, _self_shared));<br \/>\n        }<br \/>\n    }<br \/>\n    else {<br \/>\n        std::cout &lt;&lt; error.message() &lt;&lt; std::endl;<br \/>\n        _server-&gt;ClearSession(_uuid);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u4fee\u6539\u8bfb\u56de\u8c03,\u56e0\u4e3a\u6211\u4eec\u8981\u4e00\u76f4\u76d1\u542c\u5bf9\u7aef\u53d1\u9001\u7684\u6570\u636e,\u6240\u4ee5\u8981\u5728\u6bcf\u6b21\u6536\u5230\u6570\u636e\u540e\u7ee7\u7eed\u7ed1\u5b9a\u76d1\u542c\u4e8b\u4ef6<br \/>\nvoid CSession::HandleRead(const boost::system::error_code&amp; error, std::size_t bytes_transferred, std::shared_ptr&lt;CSession&gt; _self_shared) {<br \/>\n    if (!error) {<br \/>\n        std::cout &lt;&lt; &#034;read data is&#034; &lt;&lt; _data &lt;&lt; std::endl;<br \/>\n        Send(_data, bytes_transferred);<br \/>\n        memset(_data, 0, max_length);<br \/>\n        _socket.async_read_some(asio::buffer(_data, max_length),<br \/>\n            std::bind(&amp;CSession::handleRead, this, std::placeholders::_1, std::placeholders::_2, _self_shared));<br \/>\n    }<br \/>\n    else {<br \/>\n        std::cout &lt;&lt; error.message() &lt;&lt; std::endl;<br \/>\n        _server-&gt;ClearSession(_uuid);<br \/>\n    }<br \/>\n} <\/p>\n<h3 id=\"%E5%A4%84%E7%90%86%E7%B2%98%E5%8C%85%E9%97%AE%E9%A2%98\">\u5904\u7406\u7c98\u5305\u95ee\u9898<\/h3>\n<h4 id=\"%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86\">\u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6<\/h4>\n<li>TCP \u7c98\u5305 \/ \u62c6\u5305\u672c\u8d28&#xff1a;TCP \u662f\u300c\u6d41\u5f0f\u534f\u8bae\u300d&#xff0c;\u65e0\u6d88\u606f\u8fb9\u754c&#xff0c;\u4f1a\u51fa\u73b0\u4e24\u79cd\u60c5\u51b5&#xff1a;\n<ul>\n<li>\u7c98\u5305&#xff1a;\u8fde\u7eed\u53d1\u9001\u7684\u5c0f\u6570\u636e\u88ab\u5408\u5e76\u4e3a\u4e00\u4e2a TCP \u5305&#xff08;\u5982\u53d1 &#034;a&#034;&#043;&#034;b&#034;&#xff0c;\u63a5\u6536\u7aef\u53ef\u80fd\u6536\u5230 &#034;ab&#034;&#xff09;&#xff1b;<\/li>\n<li>\u62c6\u5305&#xff1a;\u5927\u6570\u636e\u88ab\u62c6\u5206\u4e3a\u591a\u4e2a TCP \u5305&#xff08;\u5982\u53d1 1024 \u5b57\u8282&#xff0c;\u63a5\u6536\u7aef\u53ef\u80fd\u5206\u4e24\u6b21\u6536\u5230 500&#043;524 \u5b57\u8282&#xff09;&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u957f\u5ea6\u524d\u7f00\u6cd5&#xff08;\u4e3b\u6d41\u7c98\u5305\u89e3\u51b3\u65b9\u6848&#xff09;&#xff1a;\n<ul>\n<li>\u6d88\u606f\u7ed3\u6784&#xff1a;\u5934\u90e8&#xff08;\u56fa\u5b9a\u957f\u5ea6&#xff0c;\u5b58\u50a8\u6570\u636e\u4f53\u957f\u5ea6&#xff09; &#043; \u6570\u636e\u4f53&#xff0c;HEAD_LENGTH \u901a\u5e38\u4e3a 2 \u5b57\u8282&#xff08;short&#xff09;\u6216 4 \u5b57\u8282&#xff08;int&#xff09;&#xff1b;<\/li>\n<li>\u89e3\u6790\u903b\u8f91&#xff1a;\u5148\u8bfb\u5934\u90e8\u2192\u89e3\u6790\u6570\u636e\u4f53\u957f\u5ea6\u2192\u518d\u8bfb\u5bf9\u5e94\u957f\u5ea6\u7684\u6570\u636e\u4f53&#xff0c;\u5929\u7136\u5212\u5206\u6d88\u606f\u8fb9\u754c&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u5206\u9636\u6bb5\u8bfb\u53d6\u903b\u8f91&#xff1a;\n<ul>\n<li>\u9636\u6bb5 1&#xff1a;\u8bfb\u53d6\u5934\u90e8&#xff0c;\u5904\u7406\u300c\u5934\u90e8\u672a\u6536\u5168\u300d&#xff08;\u5355\u6b21 read_some \u4ec5\u6536\u5230\u90e8\u5206\u5934\u90e8&#xff09;&#xff1b;<\/li>\n<li>\u9636\u6bb5 2&#xff1a;\u8bfb\u53d6\u6570\u636e\u4f53&#xff0c;\u5904\u7406\u300c\u6570\u636e\u4f53\u672a\u6536\u5168\u300d\u300c\u7c98\u5305&#xff08;\u591a\u4e2a\u6d88\u606f&#xff09;\u300d&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u5faa\u73af\u5904\u7406\u5269\u4f59\u6570\u636e&#xff1a;\u5355\u6b21 async_read_some \u53ef\u80fd\u6536\u5230\u300c\u591a\u4e2a\u5b8c\u6574\u6d88\u606f\u300d\u6216\u300c\u90e8\u5206\u6d88\u606f &#043; \u5b8c\u6574\u6d88\u606f\u300d&#xff0c;\u9700\u5faa\u73af\u89e3\u6790\u7f13\u51b2\u533a\u6570\u636e&#xff0c;\u76f4\u5230\u5904\u7406\u5b8c\u6bd5\u3002<\/li>\n<p>class MsgNode<br \/>\n{<br \/>\n    friend class CSession;<br \/>\npublic:<br \/>\n    MsgNode(char * msg, short max_len):_total_len(max_len &#043; HEAD_LENGTH),_cur_len(0){<br \/>\n       _data &#061; new char[_total_len&#043;1]();<br \/>\n       memcpy(_data, &amp;max_len, HEAD_LENGTH);<br \/>\n       memcpy(_data&#043; HEAD_LENGTH, msg, max_len);<br \/>\n       _data[_total_len] &#061; &#039;\\\\0&#039;;<br \/>\n    }<\/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    }<\/p>\n<p>    ~MsgNode() {<br \/>\n       delete[] _data;<br \/>\n    }<\/p>\n<p>    void Clear() {<br \/>\n       ::memset(_data, 0, _total_len);<br \/>\n       _cur_len &#061; 0;<br \/>\n    }<br \/>\nprivate:<br \/>\n    short _cur_len;<br \/>\n    short _total_len;<br \/>\n    char* _data;<br \/>\n};<\/p>\n<p>\/\/ CSession\u5b8c\u5584<br \/>\n\/\/\u6536\u5230\u7684\u6d88\u606f\u7ed3\u6784<br \/>\nstd::shared_ptr&lt;MsgNode&gt; _recv_msg_node;<br \/>\n\/\/\u5934\u90e8\u662f\u5426\u63a5\u53d7\u5b8c\u6bd5<br \/>\nbool _b_head_parse;<br \/>\n\/\/\u6536\u5230\u7684\u5934\u90e8\u7ed3\u6784<br \/>\nstd::shared_ptr&lt;MsgNode&gt; _recv_head_node;<\/p>\n<p>void CSession::HandleRead(const boost::system::error_code&amp; error, size_t  bytes_transferred, std::shared_ptr&lt;CSession&gt; shared_self){<br \/>\n    if (!error) {<br \/>\n       \/\/\u5df2\u7ecf\u79fb\u52a8\u7684\u5b57\u7b26\u6570<br \/>\n       int copy_len &#061; 0;<br \/>\n       while (bytes_transferred&gt;0) {<br \/>\n          if (!_b_head_parse) {<br \/>\n             \/\/\u6536\u5230\u7684\u6570\u636e\u4e0d\u8db3\u5934\u90e8\u5927\u5c0f<br \/>\n             if (bytes_transferred &#043; _recv_head_node-&gt;_cur_len &lt; HEAD_LENGTH) {<br \/>\n                memcpy(_recv_head_node-&gt;_data &#043; _recv_head_node-&gt;_cur_len, _data&#043; copy_len, bytes_transferred);<br \/>\n                _recv_head_node-&gt;_cur_len &#043;&#061; bytes_transferred;<br \/>\n                ::memset(_data, 0, MAX_LENGTH);<br \/>\n                _socket.async_read_some(boost::asio::buffer(_data, MAX_LENGTH),<br \/>\n                   std::bind(&amp;CSession::HandleRead, this, std::placeholders::_1, std::placeholders::_2, shared_self));<br \/>\n                return;<br \/>\n             }<br \/>\n             \/\/\u6536\u5230\u7684\u6570\u636e\u6bd4\u5934\u90e8\u591a<br \/>\n             \/\/\u5934\u90e8\u5269\u4f59\u672a\u590d\u5236\u7684\u957f\u5ea6<br \/>\n             int head_remain &#061; HEAD_LENGTH &#8211; _recv_head_node-&gt;_cur_len;<br \/>\n             memcpy(_recv_head_node-&gt;_data &#043; _recv_head_node-&gt;_cur_len, _data&#043;copy_len, head_remain);<br \/>\n             \/\/\u66f4\u65b0\u5df2\u5904\u7406\u7684data\u957f\u5ea6\u548c\u5269\u4f59\u672a\u5904\u7406\u7684\u957f\u5ea6<br \/>\n             copy_len &#043;&#061; head_remain;<br \/>\n             bytes_transferred -&#061; head_remain;<br \/>\n             \/\/\u83b7\u53d6\u5934\u90e8\u6570\u636e<br \/>\n             short data_len &#061; 0;<br \/>\n             memcpy(&amp;data_len, _recv_head_node-&gt;_data, HEAD_LENGTH);<br \/>\n             cout &lt;&lt; &#034;data_len is &#034; &lt;&lt; data_len &lt;&lt; endl;<br \/>\n             \/\/\u5934\u90e8\u957f\u5ea6\u975e\u6cd5<br \/>\n             if (data_len &gt; MAX_LENGTH) {<br \/>\n                std::cout &lt;&lt; &#034;invalid data length is &#034; &lt;&lt; data_len &lt;&lt; endl;<br \/>\n                _server-&gt;ClearSession(_uuid);<br \/>\n                return;<br \/>\n             }<br \/>\n             _recv_msg_node &#061; make_shared&lt;MsgNode&gt;(data_len);<\/p>\n<p>             \/\/\u6d88\u606f\u7684\u957f\u5ea6\u5c0f\u4e8e\u5934\u90e8\u89c4\u5b9a\u7684\u957f\u5ea6&#xff0c;\u8bf4\u660e\u6570\u636e\u672a\u6536\u5168&#xff0c;\u5219\u5148\u5c06\u90e8\u5206\u6d88\u606f\u653e\u5230\u63a5\u6536\u8282\u70b9\u91cc<br \/>\n             if (bytes_transferred &lt; data_len) {<br \/>\n                memcpy(_recv_msg_node-&gt;_data &#043; _recv_msg_node-&gt;_cur_len, _data &#043; copy_len, bytes_transferred);<br \/>\n                _recv_msg_node-&gt;_cur_len &#043;&#061; bytes_transferred;<br \/>\n                ::memset(_data, 0, MAX_LENGTH);<br \/>\n                _socket.async_read_some(boost::asio::buffer(_data, MAX_LENGTH),<br \/>\n                   std::bind(&amp;CSession::HandleRead, this, std::placeholders::_1, std::placeholders::_2, shared_self));<br \/>\n                \/\/\u5934\u90e8\u5904\u7406\u5b8c\u6210<br \/>\n                _b_head_parse &#061; true;<br \/>\n                return;<br \/>\n             }<\/p>\n<p>             memcpy(_recv_msg_node-&gt;_data &#043; _recv_msg_node-&gt;_cur_len, _data &#043; copy_len, data_len);<br \/>\n             _recv_msg_node-&gt;_cur_len &#043;&#061; data_len;<br \/>\n             copy_len &#043;&#061; data_len;<br \/>\n             bytes_transferred -&#061; data_len;<br \/>\n             _recv_msg_node-&gt;_data[_recv_msg_node-&gt;_total_len] &#061; &#039;\\\\0&#039;;<br \/>\n             cout &lt;&lt; &#034;receive data is &#034; &lt;&lt; _recv_msg_node-&gt;_data &lt;&lt; endl;<br \/>\n             \/\/\u6b64\u5904\u53ef\u4ee5\u8c03\u7528Send\u53d1\u9001\u6d4b\u8bd5<br \/>\n             Send(_recv_msg_node-&gt;_data, _recv_msg_node-&gt;_total_len);<br \/>\n             \/\/\u7ee7\u7eed\u8f6e\u8be2\u5269\u4f59\u672a\u5904\u7406\u6570\u636e<br \/>\n             _b_head_parse &#061; false;<br \/>\n             _recv_head_node-&gt;Clear();<br \/>\n             if (bytes_transferred &lt;&#061; 0) {<br \/>\n                ::memset(_data, 0, MAX_LENGTH);<br \/>\n                _socket.async_read_some(boost::asio::buffer(_data, MAX_LENGTH),<br \/>\n                   std::bind(&amp;CSession::HandleRead, this, std::placeholders::_1, std::placeholders::_2, shared_self));<br \/>\n                return;<br \/>\n             }<br \/>\n             continue;<br \/>\n          }<\/p>\n<p>          \/\/\u5df2\u7ecf\u5904\u7406\u5b8c\u5934\u90e8&#xff0c;\u5904\u7406\u4e0a\u6b21\u672a\u63a5\u53d7\u5b8c\u7684\u6d88\u606f\u6570\u636e<br \/>\n          \/\/\u63a5\u6536\u7684\u6570\u636e\u4ecd\u4e0d\u8db3\u5269\u4f59\u672a\u5904\u7406\u7684<br \/>\n          int remain_msg &#061; _recv_msg_node-&gt;_total_len &#8211; _recv_msg_node-&gt;_cur_len;<br \/>\n          if (bytes_transferred &lt; remain_msg) {<br \/>\n             memcpy(_recv_msg_node-&gt;_data &#043; _recv_msg_node-&gt;_cur_len, _data &#043; copy_len, bytes_transferred);<br \/>\n             _recv_msg_node-&gt;_cur_len &#043;&#061; bytes_transferred;<br \/>\n             ::memset(_data, 0, MAX_LENGTH);<br \/>\n             _socket.async_read_some(boost::asio::buffer(_data, MAX_LENGTH),<br \/>\n                std::bind(&amp;CSession::HandleRead, this, std::placeholders::_1, std::placeholders::_2, shared_self));<br \/>\n             return;<br \/>\n          }<br \/>\n          memcpy(_recv_msg_node-&gt;_data &#043; _recv_msg_node-&gt;_cur_len, _data &#043; copy_len, remain_msg);<br \/>\n          _recv_msg_node-&gt;_cur_len &#043;&#061; remain_msg;<br \/>\n          bytes_transferred -&#061; remain_msg;<br \/>\n          copy_len &#043;&#061; remain_msg;<br \/>\n          _recv_msg_node-&gt;_data[_recv_msg_node-&gt;_total_len] &#061; &#039;\\\\0&#039;;<br \/>\n          cout &lt;&lt; &#034;receive data is &#034; &lt;&lt; _recv_msg_node-&gt;_data &lt;&lt; endl;<br \/>\n          \/\/\u6b64\u5904\u53ef\u4ee5\u8c03\u7528Send\u53d1\u9001\u6d4b\u8bd5<br \/>\n          Send(_recv_msg_node-&gt;_data, _recv_msg_node-&gt;_total_len);<br \/>\n          \/\/\u7ee7\u7eed\u8f6e\u8be2\u5269\u4f59\u672a\u5904\u7406\u6570\u636e<br \/>\n          _b_head_parse &#061; false;<br \/>\n          _recv_head_node-&gt;Clear();<br \/>\n          if (bytes_transferred &lt;&#061; 0) {<br \/>\n             ::memset(_data, 0, MAX_LENGTH);<br \/>\n             _socket.async_read_some(boost::asio::buffer(_data, MAX_LENGTH),<br \/>\n                std::bind(&amp;CSession::HandleRead, this, std::placeholders::_1, std::placeholders::_2, shared_self));<br \/>\n             return;<br \/>\n          }<br \/>\n          continue;<br \/>\n       }<br \/>\n    }<br \/>\n    else {<br \/>\n       std::cout &lt;&lt; &#034;handle read failed, error is &#034; &lt;&lt; error.what() &lt;&lt; endl;<br \/>\n       Close();<br \/>\n       _server-&gt;ClearSession(_uuid);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u5f53\u7136\u4e5f\u8981\u5904\u7406\u5ba2\u6237\u7aef <\/p>\n<h3 id=\"%E5%AD%97%E8%8A%82%E5%BA%8F%E5%A4%84%E7%90%86%E5%92%8C%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97%E7%9A%84%E6%8E%A7%E5%88%B6\">\u5b57\u8282\u5e8f\u5904\u7406\u548c\u6d88\u606f\u961f\u5217\u7684\u63a7\u5236<\/h3>\n<h4 id=\"%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86\">\u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6<\/h4>\n<li>\u5b57\u8282\u5e8f&#xff08;Endianness&#xff09;&#xff1a;\n<ul>\n<li>\u4e3b\u673a\u5b57\u8282\u5e8f&#xff1a;CPU \u5b58\u50a8\u6570\u636e\u7684\u987a\u5e8f&#xff0c;\u5206\u5c0f\u7aef&#xff08;x86\/x64 \u67b6\u6784&#xff0c;\u4f4e\u5b57\u8282\u5b58\u4f4e\u5730\u5740&#xff09;\u548c\u5927\u7aef&#xff08;\u90e8\u5206\u5d4c\u5165\u5f0f CPU&#xff09;&#xff1b;<\/li>\n<li>\u7f51\u7edc\u5b57\u8282\u5e8f&#xff1a;TCP\/IP \u534f\u8bae\u89c4\u5b9a\u7684\u7edf\u4e00\u5b57\u8282\u5e8f&#xff08;\u5927\u7aef&#xff09;&#xff0c;\u8de8\u673a\u5668\u901a\u4fe1\u5fc5\u987b\u5c06\u6570\u636e\u8f6c\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f&#xff0c;\u63a5\u6536\u540e\u8f6c\u56de\u4e3b\u673a\u5b57\u8282\u5e8f&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>Boost.Asio \u5b57\u8282\u5e8f\u8f6c\u6362\u63a5\u53e3&#xff1a;\n<ul>\n<li>host_to_network_short\/long&#xff1a;16\/32 \u4f4d\u6574\u6570\u4ece\u4e3b\u673a\u5b57\u8282\u5e8f\u2192\u7f51\u7edc\u5b57\u8282\u5e8f&#xff1b;<\/li>\n<li>network_to_host_short\/long&#xff1a;16\/32 \u4f4d\u6574\u6570\u4ece\u7f51\u7edc\u5b57\u8282\u5e8f\u2192\u4e3b\u673a\u5b57\u8282\u5e8f&#xff1b;<\/li>\n<li>\u4ec5\u9700\u8f6c\u6362\u300c\u957f\u5ea6\u5b57\u6bb5\u300d&#xff08;\u5934\u90e8&#xff09;&#xff0c;\u6570\u636e\u4f53&#xff08;\u5982\u5b57\u7b26\u4e32&#xff09;\u65e0\u9700\u8f6c\u6362&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u53d1\u9001\u961f\u5217\u9650\u6d41&#xff1a;\n<ul>\n<li>\u95ee\u9898&#xff1a;\u9ad8\u9891\u8c03\u7528 Send \u4f1a\u5bfc\u81f4 _send_que \u957f\u5ea6\u6fc0\u589e&#xff0c;\u5360\u7528\u5927\u91cf\u5185\u5b58\u4e14\u53d1\u9001\u5ef6\u8fdf\u53d8\u5927&#xff1b;<\/li>\n<li>\u89e3\u51b3\u65b9\u6848&#xff1a;\u8bbe\u7f6e\u961f\u5217\u6700\u5927\u957f\u5ea6&#xff08;MAX_SENDQUE&#xff09;&#xff0c;\u8d85\u8fc7\u5219\u4e22\u5f03\u65b0\u6570\u636e\u5305&#xff0c;\u4fdd\u8bc1\u901a\u4fe1\u5b9e\u65f6\u6027&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u7ebf\u7a0b\u5b89\u5168\u5f3a\u5316&#xff1a;Send \u63a5\u53e3\u53ef\u80fd\u88ab\u4e1a\u52a1\u7ebf\u7a0b\u8c03\u7528&#xff0c;\u9700\u7528 std::lock_guard \u4fdd\u62a4\u961f\u5217\u64cd\u4f5c&#xff0c;\u907f\u514d\u591a\u7ebf\u7a0b\u7ade\u4e89\u3002<\/li>\n<p>\/\/ \u8ba1\u7b97\u673a\u5185\u90e8\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\u5927\u7aef\u5e8f\u548c\u5c0f\u7aef\u5e8f<br \/>\n\/\/ \u5728\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u7684\u65f6\u5019,\u901a\u5e38\u4f7f\u7528\u7684\u662f\u5927\u7aef\u5e8f<\/p>\n<p>\/\/\u670d\u52a1\u5668\u4f7f\u7528\u7f51\u7edc\u5b57\u8282\u5e8f<br \/>\n\/\/ boost::asio::detail::socket_ops::host_to_network_long()\u5c06\u4e00\u4e2a32\u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u4ece\u4e3b\u673a\u5b57\u8282\u5e8f\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f<br \/>\n\/\/ boost::asio::detail::socket_ops::host_to_network_short()\u5c06\u4e00\u4e2a16\u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u4ece\u4e3b\u673a\u5b57\u8282\u5e8f\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f<\/p>\n<p>\/\/ \u5728\u670d\u52a1\u5668\u7684HandleRead\u51fd\u6570\u91cc,\u6dfb\u52a0\u5bf9data_len\u7684\u8f6c\u6362,\u5c06\u7f51\u7edc\u5b57\u8282\u5e8f\u8f6c\u4e3a\u672c\u5730\u5b57\u8282\u5e8f<br \/>\nshort data_len &#061; 0;<br \/>\nmemcpy(&amp;data_len, _recv_head_node-&gt;data, HEAD_LENGTH);<br \/>\ndata_len &#061; boost::asio::detail::socket_ops::network_to_host_short(data_len);<\/p>\n<p>\/\/ \u5728\u670d\u52a1\u5668\u7684\u53d1\u9001\u6570\u636e\u65f6\u4f1a\u6784\u9020\u6d88\u606f\u8282\u70b9,\u6784\u9020\u6d88\u606f\u8282\u70b9\u65f6,\u5c06\u53d1\u751f\u957f\u5ea6\u7531\u672c\u5730\u5b57\u8282\u5e8f\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f<br \/>\nMsgNode(char* msg, short max_len):_total_len(max_len &#043; HEAD_LENGTH), cur_len(0) {<br \/>\n    _data &#061; new char[_total_len&#043;1];<br \/>\n    int max_len_host &#061; boost::asio::detail::socket_ops::host_to_network_short(max_len);<br \/>\n}<br \/>\n\/\/ \u5ba2\u6237\u7aef\u4e5f\u9075\u5faa\u76f8\u540c\u7684\u5904\u7406<\/p>\n<p>\/\/ \u6d88\u606f\u961f\u5217\u63a7\u5236<br \/>\n\/\/ \u6bcf\u4e2asession\u90fd\u6709\u4e00\u4e2a\u53d1\u9001\u961f\u5217,\u56e0\u4e3a\u6709\u7684\u65f6\u5019\u53d1\u9001\u7684\u9891\u7387\u8fc7\u9ad8\u4f1a\u5bfc\u81f4\u961f\u5217\u589e\u5927,\u6240\u4ee5\u8981\u5bf9\u961f\u5217\u5927\u5c0f\u505a\u51fa\u9650\u5236,\u5f53\u961f\u5217\u5927\u4e8e\u6307\u5b9a\u957f\u5ea6\u65f6,\u5c31\u8981\u4e22\u5f03\u8981\u53d1\u9001\u7684\u6570\u636e\u5305,\u4ee5\u4fdd\u8bc1\u4fe1\u606f\u7684\u5feb\u901f\u6536\u53d1<br \/>\nvoid CSeesion::Send(char* msg, int max_length) {<br \/>\n    std::lock_guard&lt;std::mutex&gt; lock(_send_lock);<br \/>\n    int send_que_size &#061; _send_que.size();<br \/>\n    if (send_que_size &gt; MAX_SENDQUE) {<br \/>\n        std::cout &lt;&lt; &#034;session: &#034; &lt;&lt; _uuid &lt;&lt; &#034; send que fulled, size is &#034; &lt;&lt; max_length &lt;&lt; std::endl;<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    _send_que.push_back(std::make_shared&lt;MsgNode&gt;(msg, max_length));<br \/>\n    if (send_que_size &gt; 1) {<br \/>\n        return;<br \/>\n    }<br \/>\n    auto&amp; msgnode&#061; _send_que.front();<br \/>\n    bosst::asio::async_write(_socket, boost::asio::buffer(msgnode-&gt;_data, msgnode-&gt;_total_len),<br \/>\n        std::bind(&amp;CSeesion::HandleWrite, this, std::placeholders::_1, std::placeholders::_2));<br \/>\n} <\/p>\n<h3 id=\"asio%20%E5%A4%84%E7%90%86%E7%B2%98%E5%8C%85%E7%9A%84%E7%AE%80%E5%8D%95%E5%8A%9E%E6%B3%95\">asio \u5904\u7406\u7c98\u5305\u7684\u7b80\u5355\u529e\u6cd5<\/h3>\n<h4 id=\"%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86\">\u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6<\/h4>\n<li>async_read \u6838\u5fc3\u7279\u6027&#xff1a;\n<ul>\n<li>\u533a\u522b\u4e8e async_read_some&#xff08;\u8bfb\u53d6\u300c\u81f3\u5c11 1 \u5b57\u8282\u300d\u5c31\u89e6\u53d1\u56de\u8c03&#xff09;&#xff0c;async_read \u662f Boost.Asio \u9ad8\u5c42\u5c01\u88c5&#xff0c;\u4fdd\u8bc1\u8bfb\u53d6\u6307\u5b9a\u5b57\u8282\u6570\u540e\u624d\u89e6\u53d1\u56de\u8c03&#xff08;\u4ec5\u5728\u300c\u8bfb\u53d6\u5b8c\u6210\u300d\u6216\u300c\u51fa\u9519\u300d\u65f6\u56de\u8c03&#xff09;&#xff1b;<\/li>\n<li>\u5929\u7136\u89e3\u51b3\u62c6\u5305&#xff1a;\u65e0\u9700\u624b\u52a8\u8ffd\u8e2a\u5df2\u8bfb\u957f\u5ea6&#xff0c;async_read \u5185\u90e8\u5faa\u73af\u8c03\u7528 async_read_some \u76f4\u5230\u8bfb\u53d6\u6307\u5b9a\u5b57\u8282\u6570&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u5206\u9636\u6bb5 async_read \u89e3\u7c98\u5305&#xff1a;\n<ul>\n<li>\u9636\u6bb5 1&#xff1a;async_read \u8bfb\u53d6\u56fa\u5b9a\u957f\u5ea6\u7684\u5934\u90e8&#xff08;HEAD_LENGTH&#xff09;&#xff0c;\u56de\u8c03 HandleReadHead&#xff1b;<\/li>\n<li>\u9636\u6bb5 2&#xff1a;\u89e3\u6790\u5934\u90e8\u5f97\u5230\u6570\u636e\u4f53\u957f\u5ea6&#xff0c;\u518d async_read \u8bfb\u53d6\u5bf9\u5e94\u957f\u5ea6\u7684\u6570\u636e\u4f53&#xff0c;\u56de\u8c03 HandleReadMsg&#xff1b;<\/li>\n<li>\u5faa\u73af\u76d1\u542c&#xff1a;\u6570\u636e\u4f53\u8bfb\u53d6\u5b8c\u6210\u540e&#xff0c;\u518d\u6b21\u53d1\u8d77 async_read \u8bfb\u53d6\u4e0b\u4e00\u4e2a\u6d88\u606f\u7684\u5934\u90e8&#xff0c;\u5b9e\u73b0\u6301\u7eed\u76d1\u542c&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u7b80\u5316\u903b\u8f91&#xff1a;\u76f8\u6bd4 async_read_some &#043; \u624b\u52a8\u5faa\u73af\u89e3\u6790&#xff0c;async_read \u7701\u53bb\u300c\u5904\u7406\u90e8\u5206\u6570\u636e\u3001\u5269\u4f59\u6570\u636e\u5faa\u73af\u300d\u7684\u903b\u8f91&#xff0c;\u4ee3\u7801\u66f4\u7b80\u6d01&#xff0c;\u9002\u5408\u5bf9\u53ef\u8bfb\u6027\u8981\u6c42\u9ad8\u7684\u573a\u666f\u3002<\/li>\n<p>\/ \u6539\u4e3a\u4f7f\u7528boost::asio::async_read\u6765\u6307\u5b9a\u8bfb\u53d6\u5b57\u8282\u6570<\/p>\n<p>\/\/ \u83b7\u5f97\u5934\u90e8\u6570\u636e<br \/>\nvoid CSeesion::Start() {<br \/>\n    _recv_head_node-&gt;Clear();<br \/>\n    asio::async_read(_socket, asio::buffer(_recv_head_node-&gt;data, HEAD_LENGTH),<br \/>\n        std::bind(&amp;CSeesion::HandleReadHead, this, std::placeholders::_1, std::placeholders::_2), SharedSelf());<br \/>\n}<\/p>\n<p>void CSession::HandleReadHead(const boost::system::error_code&amp; error, std::size_t bytes_transferred, std::shared_ptr&lt;CSession&gt; shared_self) {<br \/>\n    if (!error) {<br \/>\n        if (bytes_transferred &lt; HEAD_LENGTH) {<br \/>\n            std::cout &lt;&lt; &#034;read head length error&#034; &lt;&lt; std::endl;<br \/>\n            Close();<br \/>\n            _server-&gt;ClearSession(_uuid);<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        \/\/ \u5934\u90e8\u63a5\u6536\u5b8c<br \/>\n        short data_len &#061; 0;<br \/>\n        memcpy(&amp;data_len, _recv_head_node-&gt;data, HEAD_LENGTH);<br \/>\n        \/\/ \u7701\u7565\u5b57\u8282\u5e8f\u8f6c\u6362<br \/>\n        \/* &#8230; *\/<br \/>\n        if (data_len &gt; MAX_LENGTH) {<br \/>\n            std::cout &lt;&lt; &#034;invalid data length&#034; &lt;&lt; std::endl;<br \/>\n            _server-&gt;ClearSession(__uuid);<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        _recv_msg_node &#061; make_shared&lt;MsgNode&gt;(data_len);<br \/>\n        asio::async_read(_socket, asio::buffer(_recv_msg_node-&gt;data, _recv_msg_node-&gt;total_len),<br \/>\n            std::bind(&amp;CSession::HandleReadMsg, this, std::placeholders::_1, std::placeholders::_2, SharedSelf()));<\/p>\n<p>    }<\/p>\n<p>    else {<br \/>\n        std::cout &lt;&lt; &#034;read head error&#034; &lt;&lt; std::endl;<br \/>\n        Close();<br \/>\n        _server-&gt;ClearSession(__uuid);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u83b7\u53d6\u4fe1\u606f\u4f53<br \/>\nvoid CSession::HandleReadMsg(const boost::system::error_code&amp; error, std::size_t bytes_transferred, std::shared_ptr&lt;CSession&gt; shared_self) {<br \/>\n    if (!error) {<br \/>\n        _recv_msg_node-&gt;data[_recv_msg_node-&gt;_total_len] &#061; &#039;\\\\0&#039;;<br \/>\n        std::cout &lt;&lt; _recv_msg_node-&gt;data &lt;&lt; std::endl;<br \/>\n        Send(_recv_msg_node-&gt;data, _recv_msg_node-&gt;total_len);<br \/>\n        _recv_msg_node-&gt;Clear();<br \/>\n        asio::async_read(_socket, asio::buffer(_recv_head_node-&gt;data, HEAD_LENGTH),<br \/>\n            std::bind(&amp;CSession::HandleReaddHead, this, std::placeholders::_1, std::placeholders::_2, SharedSelf()));<br \/>\n    }<br \/>\n    else {<br \/>\n        std::cout &lt;&lt; &#034;read msg error, error: &#034; &lt;&lt; error.what()&lt;&lt;std::endl;<br \/>\n        Close();<br \/>\n        _server-&gt;ClearSession(__uuid);<br \/>\n    }<br \/>\n} <\/p>\n<h3 id=\"%E6%80%BB%E7%BB%93\">\u603b\u7ed3<\/h3>\n<li>\u53d1\u9001\u961f\u5217\u5c01\u88c5&#xff1a;\u5168\u53cc\u5de5\u901a\u4fe1\u9700\u72ec\u7acb\u53d1\u9001\u961f\u5217 &#043;enable_shared_from_this&#xff0c;\u89e3\u51b3\u5f02\u6b65\u5199\u4e71\u5e8f\u548c Session \u6790\u6784\u98ce\u9669&#xff1b;<\/li>\n<li>\u7c98\u5305\u5904\u7406\u6838\u5fc3&#xff1a;\n<ul>\n<li>\u624b\u52a8\u89e3\u6790&#xff1a;async_read_some &#043; \u957f\u5ea6\u524d\u7f00 &#043; \u5faa\u73af\u5904\u7406\u5269\u4f59\u6570\u636e&#xff0c;\u7075\u6d3b\u4f46\u4ee3\u7801\u590d\u6742&#xff1b;<\/li>\n<li>\u7b80\u5316\u65b9\u6848&#xff1a;async_read \u5206\u9636\u6bb5\u8bfb\u5934\u90e8 \/ \u6570\u636e\u4f53&#xff0c;\u7701\u53bb\u624b\u52a8\u5206\u6bb5\u903b\u8f91&#xff0c;\u53ef\u8bfb\u6027\u66f4\u9ad8&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u5173\u952e\u4f18\u5316&#xff1a;\n<ul>\n<li>\u5b57\u8282\u5e8f\u8f6c\u6362&#xff1a;\u8de8\u673a\u5668\u901a\u4fe1\u5fc5\u987b\u8f6c\u6362\u957f\u5ea6\u5b57\u6bb5&#xff08;\u4e3b\u673a\u2192\u7f51\u7edc\u2192\u4e3b\u673a&#xff09;&#xff1b;<\/li>\n<li>\u961f\u5217\u9650\u6d41&#xff1a;\u907f\u514d\u9ad8\u9891\u53d1\u9001\u5bfc\u81f4\u961f\u5217\u81a8\u80c0&#xff0c;\u4fdd\u8bc1\u901a\u4fe1\u5b9e\u65f6\u6027&#xff1b;<\/li>\n<li>\u7ebf\u7a0b\u5b89\u5168&#xff1a;\u4e92\u65a5\u9501\u4fdd\u62a4\u961f\u5217\u64cd\u4f5c&#xff0c;\u907f\u514d\u591a\u7ebf\u7a0b\u7ade\u4e89\u3002<\/li>\n<\/ul>\n<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55 \u5c01\u88c5\u53d1\u9001\u961f\u5217<br \/>\n\u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6<br \/>\n\u5904\u7406\u7c98\u5305\u95ee\u9898<br \/>\n\u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6<br \/>\n\u5b57\u8282\u5e8f\u5904\u7406\u548c\u6d88\u606f\u961f\u5217\u7684\u63a7\u5236<br \/>\n\u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6<br \/>\nasio \u5904\u7406\u7c98\u5305\u7684\u7b80\u5355\u529e\u6cd5<br \/>\n\u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6<br \/>\n\u603b\u7ed3 \u5c01\u88c5\u53d1\u9001\u961f\u5217<br \/>\n\u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6<br \/>\n\u5168\u53cc\u5de5\u901a\u4fe1&#xff1a;\u533a\u522b\u4e8e\u300c\u8bfb\u2192\u5199\u300d\u7684\u5e94\u7b54\u5f0f\u4ea4\u4e92&#xff0c;\u5168\u53cc\u5de5\u5141\u8bb8\u670d\u52a1\u7aef \/ \u5ba2\u6237\u7aef\u72ec\u7acb\u53d1\u8d77\u8bfb\u5199\u64cd\u4f5c&#xff08;\u5982\u670d\u52a1\u7aef\u4e3b\u52a8\u63a8\u9001\u6570\u636e\u3001\u5ba2\u6237\u7aef\u968f\u65f6\u53d1\u6d88\u606f&#xff09;&#xff0c;\u9700\u72ec\u7acb\u7684\u53d1\u9001\u961f\u5217\u7ba1\u7406\u5f02\u6b65\u5199\u903b\u8f91&#xff1b;s<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[6456,55],"topic":[],"class_list":["post-61188","post","type-post","status-publish","format-standard","hentry","category-server","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u57fa\u4e8eboost\u5e93C++\u7f51\u7edc\u7f16\u7a0b(\u4e09):\u53d1\u9001\u961f\u5217\u548c\u7c98\u5305\u95ee\u9898 - \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\/61188.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u57fa\u4e8eboost\u5e93C++\u7f51\u7edc\u7f16\u7a0b(\u4e09):\u53d1\u9001\u961f\u5217\u548c\u7c98\u5305\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u76ee\u5f55 \u5c01\u88c5\u53d1\u9001\u961f\u5217 \u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6 \u5904\u7406\u7c98\u5305\u95ee\u9898 \u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6 \u5b57\u8282\u5e8f\u5904\u7406\u548c\u6d88\u606f\u961f\u5217\u7684\u63a7\u5236 \u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6 asio \u5904\u7406\u7c98\u5305\u7684\u7b80\u5355\u529e\u6cd5 \u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6 \u603b\u7ed3 \u5c01\u88c5\u53d1\u9001\u961f\u5217 \u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6 \u5168\u53cc\u5de5\u901a\u4fe1&#xff1a;\u533a\u522b\u4e8e\u300c\u8bfb\u2192\u5199\u300d\u7684\u5e94\u7b54\u5f0f\u4ea4\u4e92&#xff0c;\u5168\u53cc\u5de5\u5141\u8bb8\u670d\u52a1\u7aef \/ \u5ba2\u6237\u7aef\u72ec\u7acb\u53d1\u8d77\u8bfb\u5199\u64cd\u4f5c&#xff08;\u5982\u670d\u52a1\u7aef\u4e3b\u52a8\u63a8\u9001\u6570\u636e\u3001\u5ba2\u6237\u7aef\u968f\u65f6\u53d1\u6d88\u606f&#xff09;&#xff0c;\u9700\u72ec\u7acb\u7684\u53d1\u9001\u961f\u5217\u7ba1\u7406\u5f02\u6b65\u5199\u903b\u8f91&#xff1b;s\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/61188.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-17T00:34:27+00:00\" \/>\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=\"10 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/61188.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/61188.html\",\"name\":\"\u57fa\u4e8eboost\u5e93C++\u7f51\u7edc\u7f16\u7a0b(\u4e09):\u53d1\u9001\u961f\u5217\u548c\u7c98\u5305\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-17T00:34:27+00:00\",\"dateModified\":\"2026-01-17T00:34:27+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/61188.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/61188.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/61188.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u57fa\u4e8eboost\u5e93C++\u7f51\u7edc\u7f16\u7a0b(\u4e09):\u53d1\u9001\u961f\u5217\u548c\u7c98\u5305\u95ee\u9898\"}]},{\"@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":"\u57fa\u4e8eboost\u5e93C++\u7f51\u7edc\u7f16\u7a0b(\u4e09):\u53d1\u9001\u961f\u5217\u548c\u7c98\u5305\u95ee\u9898 - \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\/61188.html","og_locale":"zh_CN","og_type":"article","og_title":"\u57fa\u4e8eboost\u5e93C++\u7f51\u7edc\u7f16\u7a0b(\u4e09):\u53d1\u9001\u961f\u5217\u548c\u7c98\u5305\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u76ee\u5f55 \u5c01\u88c5\u53d1\u9001\u961f\u5217 \u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6 \u5904\u7406\u7c98\u5305\u95ee\u9898 \u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6 \u5b57\u8282\u5e8f\u5904\u7406\u548c\u6d88\u606f\u961f\u5217\u7684\u63a7\u5236 \u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6 asio \u5904\u7406\u7c98\u5305\u7684\u7b80\u5355\u529e\u6cd5 \u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6 \u603b\u7ed3 \u5c01\u88c5\u53d1\u9001\u961f\u5217 \u6838\u5fc3\u6982\u5ff5\u4e0e\u57fa\u7840\u77e5\u8bc6 \u5168\u53cc\u5de5\u901a\u4fe1&#xff1a;\u533a\u522b\u4e8e\u300c\u8bfb\u2192\u5199\u300d\u7684\u5e94\u7b54\u5f0f\u4ea4\u4e92&#xff0c;\u5168\u53cc\u5de5\u5141\u8bb8\u670d\u52a1\u7aef \/ \u5ba2\u6237\u7aef\u72ec\u7acb\u53d1\u8d77\u8bfb\u5199\u64cd\u4f5c&#xff08;\u5982\u670d\u52a1\u7aef\u4e3b\u52a8\u63a8\u9001\u6570\u636e\u3001\u5ba2\u6237\u7aef\u968f\u65f6\u53d1\u6d88\u606f&#xff09;&#xff0c;\u9700\u72ec\u7acb\u7684\u53d1\u9001\u961f\u5217\u7ba1\u7406\u5f02\u6b65\u5199\u903b\u8f91&#xff1b;s","og_url":"https:\/\/www.wsisp.com\/helps\/61188.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-17T00:34:27+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"10 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/61188.html","url":"https:\/\/www.wsisp.com\/helps\/61188.html","name":"\u57fa\u4e8eboost\u5e93C++\u7f51\u7edc\u7f16\u7a0b(\u4e09):\u53d1\u9001\u961f\u5217\u548c\u7c98\u5305\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-17T00:34:27+00:00","dateModified":"2026-01-17T00:34:27+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/61188.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/61188.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/61188.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u57fa\u4e8eboost\u5e93C++\u7f51\u7edc\u7f16\u7a0b(\u4e09):\u53d1\u9001\u961f\u5217\u548c\u7c98\u5305\u95ee\u9898"}]},{"@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\/61188","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=61188"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/61188\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=61188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=61188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=61188"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=61188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}