{"id":74003,"date":"2026-02-08T22:10:11","date_gmt":"2026-02-08T14:10:11","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/74003.html"},"modified":"2026-02-08T22:10:11","modified_gmt":"2026-02-08T14:10:11","slug":"%e7%bd%91%e7%bb%9c%e5%92%8clinux%e7%bd%91%e7%bb%9c-15io%e5%a4%9a%e8%b7%af%e8%bd%ac%e6%8e%a5reactor%e7%bc%96%e7%a8%8b-%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/74003.html","title":{"rendered":"\u7f51\u7edc\u548cLinux\u7f51\u7edc-15(IO\u591a\u8def\u8f6c\u63a5)reactor\u7f16\u7a0b-\u670d\u52a1\u5668"},"content":{"rendered":"<h3 id=\"reactor%E7%9A%84%E6%9C%8D%E5%8A%A1%E5%99%A8\">1. reactor\u7684\u670d\u52a1\u5668<\/h3>\n<h4 id=\"1.0%C2%A0reactor%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%BB%8B%E7%BB%8D\">1.0\u00a0reactor\u670d\u52a1\u5668\u4ecb\u7ecd<\/h4>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Reactor\u662f\u4e00\u79cd\u4e8b\u4ef6\u9a71\u52a8\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u6a21\u578b&#xff0c;\u6838\u5fc3\u662f\u901a\u8fc7\u5355\u7ebf\u7a0b\/\u5c11\u91cf\u7ebf\u7a0b\u7ba1\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u3002\u5176\u6838\u5fc3\u7ec4\u4ef6\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>\n<p>I\/O\u591a\u8def\u590d\u7528\u5668&#xff08;\u5982epoll&#xff09;&#xff1a;\u76d1\u542c\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u7684\u4e8b\u4ef6&#xff08;\u8bfb\u3001\u5199\u3001\u5f02\u5e38&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e8b\u4ef6\u5206\u53d1\u5668&#xff1a;\u5c06\u5c31\u7eea\u4e8b\u4ef6\u5206\u53d1\u7ed9\u5bf9\u5e94\u7684\u5904\u7406\u5668\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e8b\u4ef6\u5904\u7406\u5668&#xff1a;\u6267\u884c\u5177\u4f53\u7684I\/O\u64cd\u4f5c\u548c\u4e1a\u52a1\u903b\u8f91<\/p>\n<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u5b9e\u73b0\u4e00\u4e2a\u57fa\u4e8eepoll\u7684Reactor\u670d\u52a1\u5668&#xff0c;\u4e3b\u8981\u5206\u4e3a\u4e24\u90e8\u5206&#xff1a;<\/p>\n<ul>\n<li>\n<p>Epoll\u7c7b&#xff1a;\u5c01\u88c5epoll\u7cfb\u7edf\u8c03\u7528&#xff08;\u521b\u5efa\u3001\u6dfb\u52a0\u3001\u4fee\u6539\u3001\u5220\u9664fd&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>TcpServer\u7c7b&#xff1a;\u5b9e\u73b0Reactor\u6838\u5fc3\u903b\u8f91&#xff08;\u4e8b\u4ef6\u5faa\u73af\u3001\u56de\u8c03\u673a\u5236\u3001\u8fde\u63a5\u7ba1\u7406&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5173\u952e\u7ec4\u4ef6\u89e3\u6790<\/p>\n<p>1.\u00a0Epoll\u7c7b<\/p>\n<ul>\n<li>\n<p>\u529f\u80fd&#xff1a;\u5c01\u88c5Linux\u7684epoll\u63a5\u53e3&#xff0c;\u63d0\u4f9b\u5bf9fd\u7684\u589e\u5220\u6539\u67e5\u3002<\/p>\n<\/li>\n<li>\n<p>\u6838\u5fc3\u65b9\u6cd5&#xff1a;<\/p>\n<ul>\n<li>\n<p>CreateEpoll()&#xff1a;\u521b\u5efaepoll\u5b9e\u4f8b\u3002<\/p>\n<\/li>\n<li>\n<p>AddSockToEpoll()&#xff1a;\u6dfb\u52a0fd\u5230epoll\u76d1\u542c&#xff08;\u9ed8\u8ba4\u76d1\u542c\u8bfb\u4e8b\u4ef6&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>CtrlEpoll()&#xff1a;\u4fee\u6539fd\u7684\u76d1\u542c\u4e8b\u4ef6&#xff08;\u5982\u5f00\u542f\u5199\u4e8b\u4ef6&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>WaitEpoll()&#xff1a;\u963b\u585e\u7b49\u5f85\u5c31\u7eea\u4e8b\u4ef6&#xff08;\u6838\u5fc3\u4e8b\u4ef6\u5faa\u73af\u8c03\u7528&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>2.\u00a0Connection\u7c7b<\/p>\n<ul>\n<li>\u529f\u80fd&#xff1a;\u4ee3\u8868\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u7ba1\u7406\u5176\u72b6\u6001\u548c\u7f13\u51b2\u533a\u3002<\/li>\n<li>\u6838\u5fc3\u6210\u5458&#xff1a;\n<ul>\n<li>\n<p>_sock&#xff1a;\u8fde\u63a5\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<\/li>\n<li>\n<p>_inbuffer\/_outbuffer&#xff1a;\u8f93\u5165\/\u8f93\u51fa\u7f13\u51b2\u533a&#xff08;\u5b58\u50a8\u672a\u5904\u7406\u7684\u6570\u636e\u6216\u5f85\u53d1\u9001\u7684\u6570\u636e&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u56de\u8c03\u51fd\u6570&#xff1a;<\/p>\n<ul>\n<li>\n<p>_recv_cb&#xff1a;\u8bfb\u4e8b\u4ef6\u56de\u8c03&#xff08;\u5982\u63a5\u6536\u6570\u636e&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>_send_cb&#xff1a;\u5199\u4e8b\u4ef6\u56de\u8c03&#xff08;\u5982\u53d1\u9001\u6570\u636e&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>_except_cb&#xff1a;\u5f02\u5e38\u56de\u8c03&#xff08;\u5982\u8fde\u63a5\u65ad\u5f00&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>3.\u00a0TcpServer\u7c7b<\/p>\n<ul>\n<li>\n<p>\u529f\u80fd&#xff1a;Reactor\u7684\u6838\u5fc3\u5b9e\u73b0&#xff0c;\u7ba1\u7406\u76d1\u542c\u5957\u63a5\u5b57\u3001\u8fde\u63a5\u548c\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<\/li>\n<li>\n<p>\u6838\u5fc3\u6d41\u7a0b&#xff1a;<\/p>\n<li>\n<p>\u521d\u59cb\u5316&#xff1a;<\/p>\n<li>\n<p>\u521b\u5efa\u76d1\u542c\u5957\u63a5\u5b57&#xff08;_listensock&#xff09;\u5e76\u6dfb\u52a0\u5230epoll\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bbe\u7f6eAccepter\u4e3a_listensock\u7684\u8bfb\u56de\u8c03&#xff08;\u5904\u7406\u65b0\u8fde\u63a5&#xff09;\u3002<\/p>\n<\/li>\n<\/li>\n<li>\n<p>\u4e8b\u4ef6\u5faa\u73af&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8c03\u7528WaitEpoll()\u83b7\u53d6\u5c31\u7eea\u4e8b\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u6839\u636e\u4e8b\u4ef6\u7c7b\u578b&#xff08;EPOLLIN\/EPOLLOUT&#xff09;\u89e6\u53d1\u5bf9\u5e94\u7684\u56de\u8c03&#xff08;\u5982Recver\u6216Sender&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u56de\u8c03\u51fd\u6570&#xff1a;<\/p>\n<ul>\n<li>\n<p>Accepter&#xff1a;\u63a5\u53d7\u65b0\u8fde\u63a5&#xff0c;\u521b\u5efaConnection\u5bf9\u8c61\u5e76\u6ce8\u518c\u5230epoll\u3002<\/p>\n<\/li>\n<li>\n<p>Recver&#xff1a;\u975e\u963b\u585e\u8bfb\u53d6\u6570\u636e\u5230_inbuffer&#xff0c;\u89e3\u6790\u540e\u8c03\u7528\u4e1a\u52a1\u56de\u8c03_cb\u3002<\/p>\n<\/li>\n<li>\n<p>Sender&#xff1a;\u975e\u963b\u585e\u53d1\u9001_outbuffer\u4e2d\u7684\u6570\u636e&#xff0c;\u6309\u9700\u5f00\u542f\/\u5173\u95ed\u5199\u4e8b\u4ef6\u76d1\u542c\u3002<\/p>\n<\/li>\n<li>\n<p>Excepter&#xff1a;\u5904\u7406\u8fde\u63a5\u5f02\u5e38&#xff08;\u79fb\u9664fd\u3001\u91ca\u653e\u8d44\u6e90&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/li>\n<\/ul>\n<p>Reactor\u5de5\u4f5c\u6d41\u7a0b<\/p>\n<li>\n<p>\u542f\u52a8\u670d\u52a1\u5668&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u521b\u5efaepoll\u5b9e\u4f8b&#xff0c;\u76d1\u542c_listensock\u7684\u8bfb\u4e8b\u4ef6\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u63a5\u53d7\u65b0\u8fde\u63a5&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5f53_listensock\u5c31\u7eea\u65f6&#xff0c;Accepter\u88ab\u8c03\u7528&#xff0c;\u901a\u8fc7accept\u83b7\u53d6\u65b0\u8fde\u63a5&#xff0c;\u6ce8\u518c\u5230epoll\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5904\u7406I\/O\u4e8b\u4ef6&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8bfb\u4e8b\u4ef6&#xff1a;Recver\u8bfb\u53d6\u6570\u636e\u5230\u7f13\u51b2\u533a&#xff0c;\u89e3\u6790\u540e\u4ea4\u7ed9\u4e1a\u52a1\u903b\u8f91&#xff08;_cb&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5199\u4e8b\u4ef6&#xff1a;Sender\u53d1\u9001_outbuffer\u4e2d\u7684\u6570\u636e&#xff0c;\u53d1\u5b8c\u540e\u5173\u95ed\u5199\u4e8b\u4ef6\u76d1\u542c&#xff08;\u907f\u514d\u5fd9\u7b49\u5f85&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5f02\u5e38\u5904\u7406&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8fde\u63a5\u51fa\u9519\u6216\u5173\u95ed\u65f6&#xff0c;Excepter\u6e05\u7406\u8d44\u6e90&#xff08;\u79fb\u9664fd\u3001\u5173\u95edsocket&#xff09;<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<p>Log.hpp\u548c\u4ee5\u524d\u4e00\u6837&#xff0c;\u56e0\u4e3a\u4e0b\u9762\u8981\u5199ET\u6a21\u5f0f\u6240\u4ee5Sock.hpp\u52a0\u4e86\u4e00\u4e2a\u628asock\u8bbe\u7f6e\u6210\u975e\u963b\u585e\u7684\u51fd\u6570&#xff1a;&#xff08;\u8981#include &lt;fcntl.h&gt;&#xff09;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"480\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260208141007-6988993f6b1a6.png\" width=\"1098\" \/><\/p>\n<p>\u5199\u5230TcpServer.hpp\u7684Accepter\u51fd\u6570\u518d\u6539\u4e00\u4e0bSock.hpp\u7684Accept&#xff1a;&#xff08;\u52a0\u4e00\u4e2a\u8f93\u51fa\u9519\u8bef\u7801\u7684\u53c2\u6570&#xff09;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"901\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260208141007-6988993fa58f4.png\" width=\"1723\" \/><\/p>\n<h4 id=\"Sock.hpp\">1.1 Sock.hpp<\/h4>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cerrno&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;ctype.h&gt;<br \/>\n#include &#034;Log.hpp&#034;<br \/>\n#include &lt;fcntl.h&gt;<\/p>\n<p>class Sock<br \/>\n{<br \/>\nprivate:<br \/>\n    const static int gbacklog &#061; 20; \/\/ listen\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570&#xff0c;\u73b0\u5728\u5148\u4e0d\u7ba1<br \/>\npublic:<br \/>\n    Sock()<br \/>\n    {}<br \/>\n    ~Sock()<br \/>\n    {}<br \/>\n    static int Socket()<br \/>\n    {<br \/>\n        int listensock &#061; socket(AF_INET, SOCK_STREAM, 0); \/\/ \u57df &#043; \u7c7b\u578b &#043; 0 \/\/ UDP\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662fSOCK_DGRAM<br \/>\n        if (listensock &lt; 0)<br \/>\n        {<br \/>\n            logMessage(FATAL, &#034;create socket error, %d:%s&#034;, errno, strerror(errno));<br \/>\n            exit(2);<br \/>\n        }<br \/>\n        int opt &#061; 1;<br \/>\n        setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &amp;opt, sizeof(opt));<br \/>\n        logMessage(NORMAL, &#034;create socket success, listensock: %d&#034;, listensock);<br \/>\n        return listensock;<br \/>\n    }<\/p>\n<p>    static void Bind(int sock, uint16_t port, std::string ip &#061; &#034;0.0.0.0&#034;)<br \/>\n    {<br \/>\n        struct sockaddr_in local;<br \/>\n        memset(&amp;local, 0, sizeof local);<br \/>\n        local.sin_family &#061; AF_INET;<br \/>\n        local.sin_port &#061; htons(port);<br \/>\n        inet_pton(AF_INET, ip.c_str(), &amp;local.sin_addr);<br \/>\n        if (bind(sock, (struct sockaddr *)&amp;local, sizeof(local)) &lt; 0)<br \/>\n        {<br \/>\n            logMessage(FATAL, &#034;bind error, %d:%s&#034;, errno, strerror(errno));<br \/>\n            exit(3);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    static void Listen(int sock)<br \/>\n    {<br \/>\n        if (listen(sock, gbacklog) &lt; 0)<br \/>\n        {<br \/>\n            logMessage(FATAL, &#034;listen error, %d:%s&#034;, errno, strerror(errno));<br \/>\n            exit(4);<br \/>\n        }<br \/>\n        logMessage(NORMAL, &#034;init server success&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u4e00\u822c\u60c5\u51b5\u4e0b&#xff1a;<br \/>\n    \/\/ const std::string &amp;: \u8f93\u5165\u578b\u53c2\u6570<br \/>\n    \/\/ std::string *: \u8f93\u51fa\u578b\u53c2\u6570<br \/>\n    \/\/ std::string &amp;: \u8f93\u5165\u8f93\u51fa\u578b\u53c2\u6570<br \/>\n    static int Accept(int listensock, std::string *ip, uint16_t *port, int *accept_errno &#061; nullptr)<br \/>\n    {<br \/>\n        struct sockaddr_in src;<br \/>\n        socklen_t len &#061; sizeof(src);<br \/>\n        *accept_errno &#061; 0;<br \/>\n        int servicesock &#061; accept(listensock, (struct sockaddr *)&amp;src, &amp;len);<br \/>\n        if (servicesock &lt; 0)<br \/>\n        {<br \/>\n            *accept_errno &#061; errno;<br \/>\n            \/\/ logMessage(ERROR, &#034;accept error, %d:%s&#034;, errno, strerror(errno));<br \/>\n            return -1;<br \/>\n        }<br \/>\n        if (port)<br \/>\n            *port &#061; ntohs(src.sin_port);<br \/>\n        if (ip)<br \/>\n            *ip &#061; inet_ntoa(src.sin_addr);<br \/>\n        return servicesock;<br \/>\n    }<\/p>\n<p>    static bool Connect(int sock, const std::string &amp;server_ip, const uint16_t &amp;server_port)<br \/>\n    {<br \/>\n        struct sockaddr_in server;<br \/>\n        memset(&amp;server, 0, sizeof(server));<br \/>\n        server.sin_family &#061; AF_INET;<br \/>\n        server.sin_port &#061; htons(server_port);<br \/>\n        server.sin_addr.s_addr &#061; inet_addr(server_ip.c_str());<\/p>\n<p>        if (connect(sock, (struct sockaddr *)&amp;server, sizeof(server)) &#061;&#061; 0)<br \/>\n            return true;<br \/>\n        else<br \/>\n            return false;<br \/>\n    }<\/p>\n<p>    static bool SetNonBlock(int sock)<br \/>\n    {<br \/>\n        int fl &#061; fcntl(sock, F_GETFL);<br \/>\n        if(fl &lt; 0)<br \/>\n            return false;<br \/>\n        fcntl(sock, F_SETFL, fl | O_NONBLOCK);<br \/>\n            return true;<br \/>\n    }<br \/>\n};<\/p>\n<p>\u4e0b\u9762\u76f4\u63a5\u653e\u4e00\u90e8\u5206TcpServer.hpp\u4ee3\u7801\u8ddf\u7740\u6ce8\u91ca\u770b&#xff1a;&#xff08;\u5efa\u8bae\u590d\u5236\u5230VSCode\u91cc\u770b&#xff09;<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;unordered_map&gt;<br \/>\n#include &#034;Sock.hpp&#034;<br \/>\n#include &#034;Log.hpp&#034;<br \/>\n#include &#034;Epoll.hpp&#034;<\/p>\n<p>class TcpServer;<br \/>\nclass Connection;<\/p>\n<p>using func_t &#061; std::function&lt;void(Connection *)&gt;;<br \/>\n\/\/ \u4e3a\u4e86\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c&#xff0c;\u5e38\u89c4\u7684sock\u5fc5\u987b\u8981\u6709\u72ec\u7acb\u7684\u63a5\u6536\u7f13\u51b2\u533a\u548c\u53d1\u9001\u7f13\u51b2\u533a(\u5199\u5165)<br \/>\nclass Connection \/\/ \u4e00\u4e2a\u94fe\u63a5\u7c7b<br \/>\n{<br \/>\npublic:<br \/>\n    Connection(int sock &#061; -1)<br \/>\n        : _sock(sock), _tsvr(nullptr)<br \/>\n    {}<br \/>\n    void SetCallBack(func_t recv_cb, func_t send_cb, func_t except_cb)<br \/>\n    {   \/\/ \u8bbe\u7f6e\u4e09\u4e2a\u56de\u8c03\u65b9\u6cd5<br \/>\n        _recv_cb &#061; recv_cb;<br \/>\n        _send_cb &#061; send_cb;<br \/>\n        _except_cb &#061; except_cb;<br \/>\n    }<br \/>\n    ~Connection()<br \/>\n    {}<br \/>\npublic:<br \/>\n    int _sock; \/\/ \u8d1f\u8d23\u8fdb\u884cIO\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    func_t _recv_cb; \/\/ \u4e09\u4e2a\u56de\u8c03\u65b9\u6cd5&#xff0c;\u662f\u5bf9_sock\u8fdb\u884c\u7279\u5b9a\u8bfb\u5199\u7684\u5bf9\u5e94\u65b9\u6cd5<br \/>\n    func_t _send_cb;<br \/>\n    func_t _except_cb;<\/p>\n<p>    std::string _inbuffer; \/\/ \u63a5\u6536\u7f13\u51b2\u533a&amp;&amp;\u53d1\u9001\u7f13\u51b2\u533a<br \/>\n    std::string _outbuffer; \/\/ \u8fd9\u4e24string\u6682\u65f6\u6ca1\u6709\u529e\u6cd5\u5904\u7406\u4e8c\u8fdb\u5236\u6d41&#xff0c;\u6587\u672c\u662f\u53ef\u4ee5\u7684<\/p>\n<p>    TcpServer *_tsvr; \/\/ \u8bbe\u7f6e\u5bf9TcpServer\u7684\u56de\u6307\u6307\u9488&#xff0c;\u5bf9\u5199\u4e8b\u4ef6\u7684\u5173\u5fc3\u662f\u6309\u9700\u6253\u5f00<br \/>\n};<\/p>\n<p>class TcpServer<br \/>\n{<br \/>\n    const static int gport &#061; 8080;<br \/>\n    const static int gnum &#061; 128;<br \/>\npublic:<br \/>\n    TcpServer(int port &#061; gport)<br \/>\n        : _port(port), _revs_num(gnum)<br \/>\n    {<br \/>\n        \/\/ 1. \u521b\u5efalistensock<br \/>\n        _listensock &#061; Sock::Socket();<br \/>\n        Sock::Bind(_listensock, _port);<br \/>\n        Sock::Listen(_listensock);<\/p>\n<p>        \/\/ 2. \u521b\u5efa\u591a\u8def\u8f6c\u63a5\u5bf9\u8c61<br \/>\n        _poll.CreateEpoll();<\/p>\n<p>        \/\/ 3. \u6dfb\u52a0listensock\u5230\u670d\u52a1\u5668\u4e2d -&gt; \u4e09\u6b65(\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e5f\u80fd\u8c03\u7528\u7c7b\u7684\u6210\u5458\u65b9\u6cd5&#xff0c;\u8d70\u5230\u51fd\u6570\u4f53\u4e2d\u5bf9\u8c61\u5df2\u7ecf\u5b58\u5728\u4e86)<br \/>\n        \/\/ \u540e\u4e09\u4e2a\u53c2\u6570\u662f\u51fd\u6570\u5bf9\u8c61&#xff0c;\u8981bind\u7ed1\u5b9a\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61-&gt;\u7c7b\u5185\u51fd\u6570\u6709this\u6307\u9488&#xff0c;_1\u662f\u9884\u7559\u7684\u53c2\u6570<br \/>\n        AddConnection(_listensock, std::bind(&amp;TcpServer::Accepter, this, std::placeholders::_1), nullptr, nullptr);<\/p>\n<p>        \/\/ 4. \u6784\u5efa\u4e00\u4e2a\u83b7\u53d6\u5c31\u7eea\u4e8b\u4ef6\u7684\u7f13\u51b2\u533a<br \/>\n        _revs &#061; new struct epoll_event[_revs_num];<br \/>\n    }<br \/>\n    void AddConnection(int sock, func_t recv_cb, func_t send_cb, func_t except_cb) \/\/ \u628a\u4efb\u610fsock\u8fdb\u884c\u6dfb\u52a0\u5230TcpServer<br \/>\n    {<br \/>\n        Sock::SetNonBlock(sock); \/\/ ET\u6a21\u5f0f\u8981\u628asock\u8bbe\u7f6e\u6210\u975e\u963b\u585e -&gt; \u5728Sock.hpp\u4e2d\u5199\u6210\u51fd\u6570<\/p>\n<p>        \/\/ \u9664\u4e86_listensock&#xff0c;\u540e\u9762\u8fd8\u4f1a\u5b58\u5728\u5927\u91cf\u7684socket&#xff0c;\u6bcf\u4e00\u4e2asock\u90fd\u5fc5\u987b\u88ab\u5c01\u88c5\u6210\u4e3a\u4e00\u4e2aConnection<br \/>\n        \/\/ \u5f53\u670d\u52a1\u5668\u4e2d\u5b58\u5728\u5927\u91cf\u7684Connection\u65f6&#xff0c;TcpServer\u9700\u8981\u5c06\u6240\u6709Connection\u8fdb\u884c\u7ba1\u7406&#xff1a;\u4e0a\u9762\u63cf\u8ff0\u4e86&#xff0c;\u7ec4\u7ec7 -&gt; unordered_map<br \/>\n        \/\/ 3.1 \u6784\u5efaconn\u5bf9\u8c61&#xff0c;\u5c01\u88c5sock<br \/>\n        Connection *conn &#061; new Connection(sock);<br \/>\n        conn-&gt;SetCallBack(recv_cb, send_cb, except_cb);<br \/>\n        conn-&gt;_tsvr &#061; this;<\/p>\n<p>        \/\/ 3.2 \u6dfb\u52a0sock\u5230epoll\u4e2d(\u4efb\u52a1\u901a\u77e5)-&gt;\u8981\u77e5\u9053sock\u548c\u4e8b\u4ef6(\u4efb\u4f55\u591a\u8def\u8f6c\u63a5\u7684\u670d\u52a1\u5668&#xff0c;\u4e00\u822c\u53ea\u4f1a\u6253\u5f00\u8bfb\u53d6\u4e8b\u4ef6&#xff0c;\u5199\u5165\u4e8b\u4ef6\u6309\u9700\u6253\u5f00)<br \/>\n        _poll.AddSockToEpoll(sock, EPOLLIN | EPOLLET);<\/p>\n<p>        \/\/ 3.3 \u5c06\u5bf9\u5e94\u7684Connection*\u5bf9\u8c61\u6307\u9488\u6dfb\u52a0\u5230Connections\u6620\u5c04\u8868\u4e2d(\u4e1a\u52a1\u5904\u7406)<br \/>\n        _connections.insert(std::make_pair(sock, conn));<br \/>\n    }<\/p>\n<p>    void Accepter(Connection *conn)<br \/>\n    {<br \/>\n        logMessage(DEBUG, &#034;Accepter been called&#034;);<br \/>\n    }<\/p>\n<p>    void Dispather() \/\/ \u6839\u636e\u5c31\u7eea\u7684\u4e8b\u4ef6&#xff0c;\u8fdb\u884c\u7279\u5b9a\u4e8b\u4ef6\u7684\u6d3e\u53d1<br \/>\n    {<br \/>\n        while (true)<br \/>\n        {<br \/>\n            LoopOnce();<br \/>\n        }<br \/>\n    }<br \/>\n    void LoopOnce()<br \/>\n    {<br \/>\n        int n &#061; _poll.WaitEpoll(_revs, _revs_num);<br \/>\n        for (int i &#061; 0; i &lt; n; i&#043;&#043;) \/\/ \u83b7\u53d6\u4e8b\u4ef6<br \/>\n        {<br \/>\n            int sock &#061; _revs[i].data.fd;<br \/>\n            uint32_t revents &#061; _revs[i].events;<br \/>\n            if (revents &amp; EPOLLIN) \/\/ \u8bfb\u5c31\u7eea<br \/>\n            {<br \/>\n                \/\/ if(Connection\u662f\u5b58\u5728\u5e76\u4e14_connections[sock]-&gt;_recv_cb\u88ab\u8bbe\u7f6e\u8fc7)<br \/>\n                if (IsConnectionExists(sock) &amp;&amp; _connections[sock]-&gt;_recv_cb !&#061; nullptr)<br \/>\n                    _connections[sock]-&gt;_recv_cb(_connections[sock]); \/\/ \u8c03\u7528\u8bfb\u4e8b\u4ef6\u7684\u56de\u8c03<br \/>\n            }<br \/>\n            if (revents &amp; EPOLLOUT) \/\/ \u5199\u5c31\u7eea<br \/>\n            {<br \/>\n                \/\/ if(Connection\u662f\u5b58\u5728\u5e76\u4e14_connections[sock]-&gt;_send_cb\u88ab\u8bbe\u7f6e\u8fc7)<br \/>\n                if (IsConnectionExists(sock) &amp;&amp; _connections[sock]-&gt;_send_cb !&#061; nullptr)<br \/>\n                    _connections[sock]-&gt;_send_cb(_connections[sock]); \/\/ \u8c03\u7528\u5199\u4e8b\u4ef6\u7684\u56de\u8c03<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    bool IsConnectionExists(int sock) \/\/ \u5224\u5b9aConnection\u662f\u5426\u5b58\u5728<br \/>\n    {<br \/>\n        auto iter &#061; _connections.find(sock);<br \/>\n        if (iter &#061;&#061; _connections.end())<br \/>\n            return false;<br \/>\n        else<br \/>\n            return true;<br \/>\n    }<br \/>\n    ~TcpServer()<br \/>\n    {<br \/>\n        if (_listensock &gt;&#061; 0)<br \/>\n            close(_listensock);<br \/>\n        if (_revs)<br \/>\n            delete[] _revs;<br \/>\n    }<br \/>\nprivate:<br \/>\n    int _listensock;<br \/>\n    int _port;<br \/>\n    Epoll _poll;<br \/>\n    std::unordered_map&lt;int, Connection *&gt; _connections; \/\/ \u7ba1\u7406&#xff1a;sock\u6620\u5c04\u5230Connection<br \/>\n    struct epoll_event *_revs; \/\/ \u5c31\u7eea\u4e8b\u4ef6\u7f13\u51b2\u533a,\u5c31\u7eea\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6295\u9012\u5230\u8fd9\u91cc<br \/>\n    int _revs_num; \/\/ \u5c31\u7eea\u4e8b\u4ef6\u7f13\u51b2\u533a\u5927\u5c0f<br \/>\n};<\/p>\n<p>\u7f16\u8bd1\u8fd0\u884c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"339\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260208141008-698899405ed70.png\" width=\"2260\" \/><\/p>\n<p>\u6b64\u65f6\u6210\u529f\u8c03\u7528\u4e86Accepter&#xff0c;\u56e0\u4e3a\u662fET\u6a21\u5f0f&#xff0c;\u6240\u4ee5\u662f\u963b\u585e\u7684&#xff0c;\u4e8b\u4ef6\u6ca1\u5904\u7406\u4e5f\u6ca1\u6709\u8fde\u7eed\u6253\u5370\u3002<\/p>\n<p>\u5199\u4e00\u4e0bAccepter\u518d\u6d4b\u8bd5\u4e00\u4e0b&#xff1a;<\/p>\n<p>    void Accepter(Connection *conn)<br \/>\n    {<br \/>\n        \/\/ logMessage(DEBUG, &#034;Accepter been called&#034;);<br \/>\n        \/\/ \u4e00\u5b9a\u662flistensock\u5df2\u7ecf\u5c31\u7eea\u4e86&#xff0c;\u6b64\u6b21\u8bfb\u53d6\u4e0d\u4f1a\u963b\u585e&#xff0c;<br \/>\n        \/\/ \u600e\u4e48\u4fdd\u8bc1&#xff0c;\u5e95\u5c42\u53ea\u6709\u4e00\u4e2a\u8fde\u63a5\u5c31\u7eea\u5462 -&gt; \u5faa\u73af&#xff0c;\u76f4\u5230\u83b7\u53d6\u5931\u8d25<br \/>\n        while (true)<br \/>\n        {<br \/>\n            std::string clientip;<br \/>\n            uint16_t clientport;<br \/>\n            int accept_errno &#061; 0;<br \/>\n            \/\/ sock\u4e00\u5b9a\u662f\u5e38\u89c4\u7684IO sock<br \/>\n            int sock &#061; Sock::Accept(conn-&gt;_sock, &amp;clientip, &amp;clientport, &amp;accept_errno);<br \/>\n            if (sock &lt; 0) \/\/ \u83b7\u53d6\u5931\u8d25<br \/>\n            {<br \/>\n                if (accept_errno &#061;&#061; EAGAIN || accept_errno &#061;&#061; EWOULDBLOCK)<br \/>\n                    break;<br \/>\n                else if (accept_errno &#061;&#061; EINTR)  \/\/ \u6982\u7387\u975e\u5e38\u4f4e<br \/>\n                    continue;<br \/>\n                else \/\/ accept\u5931\u8d25<br \/>\n                {<br \/>\n                    logMessage(WARNING, &#034;accept error, %d : %s&#034;, accept_errno, strerror(accept_errno));<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n            else \/\/ (sock&gt;&#061;0)\u83b7\u53d6\u94fe\u63a5\u6210\u529f-&gt;\u5c06sock\u6258\u7ba1\u7ed9TcpServer<br \/>\n            {<br \/>\n                AddConnection(sock, std::bind(&amp;TcpServer::Recver, this, std::placeholders::_1),<br \/>\n                              std::bind(&amp;TcpServer::Sender, this, std::placeholders::_1),<br \/>\n                              std::bind(&amp;TcpServer::Excepter, this, std::placeholders::_1));<br \/>\n                logMessage(DEBUG, &#034;accept client %s:%d success, add to epoll&amp;&amp;TcpServer success, sock: %d&#034;,\\\\<br \/>\n                    clientip.c_str(), clientport, sock);<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void Recver(Connection *conn) \/\/ \u8bfb\u53d6\u4e00\u4e2a\u6b63\u5e38\u7684sock<br \/>\n    {<br \/>\n        logMessage(DEBUG, &#034;Recver event exists, Recver() been called&#034;);<br \/>\n    }<\/p>\n<p>    void Sender(Connection *conn)<br \/>\n    {<br \/>\n    }<\/p>\n<p>    void Excepter(Connection *conn)<br \/>\n    {<br \/>\n    }<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"435\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260208141008-69889940b83f9.png\" width=\"1763\" \/><\/p>\n<p>\u6210\u529f\u83b7\u53d6\u5230\u8bfb\u53d6\u4e8b\u4ef6&#xff0c;\u4e0b\u9762\u6765\u5904\u7406\u4e00\u4e0b&#xff1a;<\/p>\n<p>\u5148\u5199Recver\u7684\u7b2c\u4e00\u4e2a\u7248\u672c&#xff1a;\u76f4\u63a5\u9762\u5411\u5b57\u8282\u6d41&#xff0c;\u8fdb\u884c\u5e38\u89c4\u8bfb\u53d6&#xff1a;<\/p>\n<p>    void Recver(Connection *conn) \/\/ \u8bfb\u53d6\u4e00\u4e2a\u6b63\u5e38\u7684sock<br \/>\n    {<br \/>\n        \/\/ logMessage(DEBUG, &#034;Recver event exists, Recver() been called&#034;);<br \/>\n        \/\/ v1: \u76f4\u63a5\u9762\u5411\u5b57\u8282\u6d41&#xff0c;\u5148\u8fdb\u884c\u5e38\u89c4\u8bfb\u53d6<br \/>\n        const int num &#061; 1024;<br \/>\n        while (true)<br \/>\n        {<br \/>\n            char buffer[num];<br \/>\n            ssize_t n &#061; recv(conn-&gt;_sock, buffer, sizeof(buffer) &#8211; 1, 0);<br \/>\n            if (n &lt; 0)<br \/>\n            {<br \/>\n                if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK) \/\/ \u8bfb\u53d6\u5b8c\u6bd5\u4e86&#xff0c;\u6b63\u5e38\u7684<br \/>\n                    break;<br \/>\n                else if (errno &#061;&#061; EINTR) \/\/ \u8bfb\u53d6\u88ab\u4e2d\u65ad\u4e86&#xff0c;\u91cd\u65b0\u5f00\u59cb\u8bfb<br \/>\n                    continue;<br \/>\n                else \/\/ \u771f\u6b63\u8bfb\u53d6\u5931\u8d25 -&gt; \u4ea4\u7ed9\u5f02\u5e38\u56de\u8c03<br \/>\n                {<br \/>\n                    logMessage(ERROR, &#034;recv error, %d : %s&#034;, errno, strerror(errno));<br \/>\n                    conn-&gt;_except_cb(conn);<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n            else if (n &#061;&#061; 0)<br \/>\n            {<br \/>\n                logMessage(DEBUG, &#034;client[%d] quit, server close [%d]&#034;, conn-&gt;_sock, conn-&gt;_sock);<br \/>\n                conn-&gt;_except_cb(conn);<br \/>\n                break;<br \/>\n            }<br \/>\n            else \/\/ \u8bfb\u53d6\u6210\u529f<br \/>\n            {<br \/>\n                buffer[n] &#061; 0;<br \/>\n                conn-&gt;_inbuffer &#043;&#061; buffer; \/\/ \u8bfb\u53d6\u5230\u7684\u6570\u636e\u5168\u90e8\u62fc\u63a5\u5230\u63a5\u6536\u7f13\u51b2\u533a<br \/>\n            }<br \/>\n        }<br \/>\n        logMessage(DEBUG, &#034;conn-&gt;_inbuffer[sock: %d]: %s&#034;, conn-&gt;_sock, conn-&gt;_inbuffer.c_str());<br \/>\n    }<\/p>\n<p>\u6bcf\u4e2a\u670d\u52a1\u7aef\u90fd\u6709\u81ea\u5df1\u7684\u63a5\u6536\u7f13\u51b2\u533a&#xff0c;\u4e92\u4e0d\u5f71\u54cd&#xff08;\u8fd9\u91cc\u56de\u8f66\u4e5f\u88ab\u8f93\u5165\u8fdb\u53bb\u4e86\u53ea\u662ftelnet\u7684\u539f\u56e0&#xff0c;\u8fd9\u91cc\u4e0d\u5199\u5ba2\u6237\u7aef\u4e86\u5c31\u8fd9\u4e48\u7528\u4e86&#xff09;&#xff0c;\u4f46\u8fd8\u662f\u90a3\u53e5\u8bdd&#xff1a;\u600e\u4e48\u4fdd\u8bc1\u4f60\u8bfb\u5230\u7684\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u62a5\u6587\u5462&#xff1f;-&gt;\u5c31\u8981\u5b9a\u5236\u534f\u8bae\u4e86&#xff0c;\u5199\u4e00\u4e2aProtocol.hpp&#xff1a;<\/p>\n<h4 id=\"%E5%8A%A0%E5%8D%8F%E8%AE%AE%E5%88%86%E5%89%B2%E6%8A%A5%E6%96%87\">1.2 \u52a0\u534f\u8bae\u5206\u5272\u62a5\u6587<\/h4>\n<p>\u5728\u524d\u9762\u52a0\u4e0a\u8fd9\u884c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"337\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260208141009-6988994111a51.png\" width=\"1900\" \/><\/p>\n<p>\u52a0\u4e2a\u7c7b\u5185\u6210\u5458&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"353\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260208141009-6988994151d78.png\" width=\"1614\" \/><\/p>\n<p>Dispather&#xff1a;<\/p>\n<p>    void Dispather(callback_t cb) \/\/ \u6839\u636e\u5c31\u7eea\u7684\u4e8b\u4ef6&#xff0c;\u8fdb\u884c\u7279\u5b9a\u4e8b\u4ef6\u7684\u6d3e\u53d1<br \/>\n    {<br \/>\n        _cb &#061; cb;<br \/>\n        while (true)<br \/>\n        {<br \/>\n            LoopOnce();<br \/>\n        }<br \/>\n    }<\/p>\n<p>main.cc&#xff1a;<\/p>\n<p>#include &#034;TcpServer.hpp&#034;<br \/>\n#include &lt;memory&gt;<\/p>\n<p>void NetCal(Connection *conn, std::string &amp;request)<br \/>\n{<br \/>\n    logMessage(DEBUG, &#034;NetCal been called, get request: %s&#034;, request.c_str());<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    std::unique_ptr&lt;TcpServer&gt; svr(new TcpServer());<br \/>\n    svr-&gt;Dispather(NetCal);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>\u6539\u8fdb\u7684Recver&#xff1a;<\/p>\n<p>    void Recver(Connection *conn) \/\/ \u8bfb\u53d6\u4e00\u4e2a\u6b63\u5e38\u7684sock<br \/>\n    {<br \/>\n        const int num &#061; 1024;<br \/>\n        bool err &#061; false;<br \/>\n        \/\/ logMessage(DEBUG, &#034;Recver event exists, Recver() been called&#034;);<br \/>\n        while (true)<br \/>\n        {<br \/>\n            char buffer[num];<br \/>\n            ssize_t n &#061; recv(conn-&gt;_sock, buffer, sizeof(buffer) &#8211; 1, 0);<br \/>\n            if (n &lt; 0)<br \/>\n            {<br \/>\n                if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK) \/\/ \u8bfb\u53d6\u5b8c\u6bd5\u4e86&#xff0c;\u6b63\u5e38\u7684<br \/>\n                    break;<br \/>\n                else if (errno &#061;&#061; EINTR) \/\/ \u8bfb\u53d6\u88ab\u4e2d\u65ad\u4e86&#xff0c;\u91cd\u65b0\u5f00\u59cb\u8bfb<br \/>\n                    continue;<br \/>\n                else \/\/ \u771f\u6b63\u8bfb\u53d6\u5931\u8d25 -&gt; \u4ea4\u7ed9\u5f02\u5e38\u56de\u8c03<br \/>\n                {<br \/>\n                    logMessage(ERROR, &#034;recv error, %d : %s&#034;, errno, strerror(errno));<br \/>\n                    conn-&gt;_except_cb(conn);<br \/>\n                    err &#061; true;<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n            else if (n &#061;&#061; 0)<br \/>\n            {<br \/>\n                logMessage(DEBUG, &#034;client[%d] quit, server close [%d]&#034;, conn-&gt;_sock, conn-&gt;_sock);<br \/>\n                conn-&gt;_except_cb(conn);<br \/>\n                err &#061; true;<br \/>\n                break;<br \/>\n            }<br \/>\n            else \/\/ \u8bfb\u53d6\u6210\u529f<br \/>\n            {<br \/>\n                buffer[n] &#061; 0;<br \/>\n                conn-&gt;_inbuffer &#043;&#061; buffer; \/\/ \u8bfb\u53d6\u5230\u7684\u6570\u636e\u5168\u90e8\u62fc\u63a5\u5230\u63a5\u6536\u7f13\u51b2\u533a<br \/>\n            }<br \/>\n        }<br \/>\n        logMessage(DEBUG, &#034;conn-&gt;_inbuffer[sock: %d]: %s&#034;, conn-&gt;_sock, conn-&gt;_inbuffer.c_str());<br \/>\n        if (!err) \/\/ \u5982\u679c\u9519\u8bef\u7801\u8fd8\u662ffalse\u5c31\u662f\u6b63\u5e38break\u7684<br \/>\n        {<br \/>\n            std::vector&lt;std::string&gt; messages;<br \/>\n            SpliteMessage(conn-&gt;_inbuffer, &amp;messages);<br \/>\n            \/\/ \u4fdd\u8bc1\u8d70\u5230\u8fd9\u91cc&#xff0c;\u5c31\u662f\u4e00\u4e2a\u5b8c\u6574\u62a5\u6587<br \/>\n            for (auto &amp;msg : messages)<br \/>\n            {    \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u5c06message\u5c01\u88c5\u6210\u4e3atask,\u7136\u540epush\u5230\u4efb\u52a1\u961f\u5217,\u4efb\u52a1\u5904\u7406\u4ea4\u7ed9\u540e\u7aef\u7ebf\u7a0b\u6c60,\u8fd9\u91cc\u4e0d\u5904\u7406<br \/>\n                _cb(conn, msg);<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>\u4e00\u90e8\u5206Protocol.hpp&#xff1a;&#xff08;\u8fd9\u91cc\u7528\u5927\u5199X\u4f5c\u4e3a\u5207\u5206&#xff09;<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;vector&gt;<\/p>\n<p>\/\/ 1. \u62a5\u6587\u548c\u62a5\u6587\u4e4b\u95f4&#xff0c;\u6211\u4eec\u91c7\u7528\u7279\u6b8a\u5b57\u7b26\u6765\u8fdb\u884c\u89e3\u51b3\u7c98\u5305\u95ee\u9898<br \/>\n\/\/ 2. \u83b7\u53d6\u4e00\u4e2a\u4e00\u4e2a\u72ec\u7acb\u5b8c\u6574\u7684\u62a5\u6587&#xff0c;\u5e8f\u5217\u548c\u53cd\u5e8f\u5217\u5316 &#8212; \u81ea\u5b9a\u4e49<br \/>\n\/\/ 100&#043;19X100&#043;19X100&#043;19<br \/>\n#define SEP &#034;X&#034;<br \/>\n#define SEP_LEN strlen(SEP)<\/p>\n<p>\/\/ \u8981\u628a\u4f20\u5165\u8fdb\u6765\u7684\u7f13\u51b2\u533a\u8fdb\u884c\u5207\u5206&#xff0c;\u8981\u6c42&#xff1a;<br \/>\n\/\/ 1. buffer\u88ab\u5207\u8d70\u7684&#xff0c;\u4e5f\u540c\u65f6\u8981\u4ecebuffer\u4e2d\u79fb\u9664<br \/>\n\/\/ 2. \u53ef\u80fd\u4f1a\u5b58\u5728\u591a\u4e2a\u62a5\u6587&#xff0c;\u591a\u4e2a\u62a5\u6587\u4f9d\u6b21\u653e\u5165out<br \/>\nvoid SpliteMessage(std::string &amp;buffer, std::vector&lt;std::string&gt; *out) \/\/ \u5206\u5272\u62a5\u6587<br \/>\n{   \/\/ buffer: \u8f93\u5165\u8f93\u51fa\u578b\u53c2\u6570&#xff0c;out: \u8f93\u51fa\u578b\u53c2\u6570<br \/>\n    while (true)<br \/>\n    {<br \/>\n        auto pos &#061; buffer.find(SEP); \/\/ \u5728\u7f13\u51b2\u533a\u91cc\u627e\u5206\u9694\u7b26<br \/>\n        if (std::string::npos &#061;&#061; pos) \/\/ \u627e\u4e0d\u5230\u5c31break<br \/>\n            break;<br \/>\n        std::string message &#061; buffer.substr(0, pos); \/\/ \u63d0\u53d6\u5b50\u4e32&#xff1a;\u524d\u95ed\u540e\u5f00\u533a\u95f4<br \/>\n        buffer.erase(0, pos &#043; SEP_LEN); \/\/ \u79fb\u9664\u5b50\u4e32\u548c\u8861\u5a25\u798f<br \/>\n        out-&gt;push_back(message); \/\/ push_back\u5b8c\u6574\u7684\u5b50\u4e32<br \/>\n        \/\/ std::cout &lt;&lt; &#034;debug: &#034; &lt;&lt; message &lt;&lt; &#034; : &#034; &lt;&lt; buffer &lt;&lt; std::endl;<br \/>\n        \/\/ sleep(1);<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4 id=\"%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96\">1.3 \u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316<\/h4>\n<p>\u628a\u4ee5\u524d\u81ea\u5df1\u5199\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u590d\u5236\u5230Protocol.hpp&#xff1a;<\/p>\n<h5 id=\"Protocol.hpp\">Protocol.hpp<\/h5>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;vector&gt;<\/p>\n<p>\/\/ 1. \u62a5\u6587\u548c\u62a5\u6587\u4e4b\u95f4&#xff0c;\u6211\u4eec\u91c7\u7528\u7279\u6b8a\u5b57\u7b26\u6765\u8fdb\u884c\u89e3\u51b3\u7c98\u5305\u95ee\u9898<br \/>\n\/\/ 2. \u83b7\u53d6\u4e00\u4e2a\u4e00\u4e2a\u72ec\u7acb\u5b8c\u6574\u7684\u62a5\u6587&#xff0c;\u5e8f\u5217\u548c\u53cd\u5e8f\u5217\u5316 &#8212; \u81ea\u5b9a\u4e49<br \/>\n\/\/ 100&#043;19X100&#043;19X100&#043;19<br \/>\n#define SEP &#034;X&#034;<br \/>\n#define SEP_LEN strlen(SEP)<\/p>\n<p>\/\/ \u8981\u628a\u4f20\u5165\u8fdb\u6765\u7684\u7f13\u51b2\u533a\u8fdb\u884c\u5207\u5206&#xff0c;\u8981\u6c42&#xff1a;<br \/>\n\/\/ 1. buffer\u88ab\u5207\u8d70\u7684&#xff0c;\u4e5f\u540c\u65f6\u8981\u4ecebuffer\u4e2d\u79fb\u9664<br \/>\n\/\/ 2. \u53ef\u80fd\u4f1a\u5b58\u5728\u591a\u4e2a\u62a5\u6587&#xff0c;\u591a\u4e2a\u62a5\u6587\u4f9d\u6b21\u653e\u5165out<br \/>\nvoid SpliteMessage(std::string &amp;buffer, std::vector&lt;std::string&gt; *out) \/\/ \u5206\u5272\u62a5\u6587<br \/>\n{   \/\/ buffer: \u8f93\u5165\u8f93\u51fa\u578b\u53c2\u6570&#xff0c;out: \u8f93\u51fa\u578b\u53c2\u6570<br \/>\n    while (true)<br \/>\n    {<br \/>\n        auto pos &#061; buffer.find(SEP); \/\/ \u5728\u7f13\u51b2\u533a\u91cc\u627e\u5206\u9694\u7b26<br \/>\n        if (std::string::npos &#061;&#061; pos) \/\/ \u627e\u4e0d\u5230\u5c31break<br \/>\n            break;<br \/>\n        std::string message &#061; buffer.substr(0, pos); \/\/ \u63d0\u53d6\u5b50\u4e32&#xff1a;\u524d\u95ed\u540e\u5f00\u533a\u95f4<br \/>\n        buffer.erase(0, pos &#043; SEP_LEN); \/\/ \u79fb\u9664\u5b50\u4e32\u548c\u8861\u5a25\u798f<br \/>\n        out-&gt;push_back(message); \/\/ push_back\u5b8c\u6574\u7684\u5b50\u4e32<br \/>\n        \/\/ std::cout &lt;&lt; &#034;debug: &#034; &lt;&lt; message &lt;&lt; &#034; : &#034; &lt;&lt; buffer &lt;&lt; std::endl;<br \/>\n        \/\/ sleep(1);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u81ea\u5df1\u624b\u5199\u5e8f\u5217\u53cd\u5e8f\u5217\u5316<br \/>\n#define SPACE &#034; &#034;<br \/>\n#define SPACE_LEN strlen(SPACE)<\/p>\n<p>std::string Encode(std::string&amp; s)<br \/>\n{<br \/>\n    return s &#043; SEP;<br \/>\n}<\/p>\n<p>class Request<br \/>\n{<br \/>\npublic:<br \/>\n    std::string Serialize()<br \/>\n    {<br \/>\n        std::string str;<br \/>\n        str &#061; std::to_string(_x);<br \/>\n        str &#043;&#061; SPACE;<br \/>\n        str &#043;&#061; _op;<br \/>\n        str &#043;&#061; SPACE;<br \/>\n        str &#043;&#061; std::to_string(_y);<br \/>\n        return str;<br \/>\n    }<br \/>\n    bool Deserialized(const std::string&amp; str) \/\/ 1 &#043; 1<br \/>\n    {<br \/>\n        std::size_t left &#061; str.find(SPACE);<br \/>\n        if (left &#061;&#061; std::string::npos)<br \/>\n            return false;<br \/>\n        std::size_t right &#061; str.rfind(SPACE);<br \/>\n        if (right &#061;&#061; std::string::npos)<br \/>\n            return false;<br \/>\n        _x &#061; atoi(str.substr(0, left).c_str());<br \/>\n        _y &#061; atoi(str.substr(right &#043; SPACE_LEN).c_str());<br \/>\n        if (left &#043; SPACE_LEN &gt; str.size())<br \/>\n            return false;<br \/>\n        else<br \/>\n            _op &#061; str[left &#043; SPACE_LEN];<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>public:<br \/>\n    Request()<br \/>\n    {}<br \/>\n    Request(int x, int y, char op)<br \/>\n        : _x(x), _y(y), _op(op)<br \/>\n    {}<br \/>\n    ~Request()<br \/>\n    {}<br \/>\npublic:<br \/>\n    int _x;<br \/>\n    int _y;<br \/>\n    char _op; \/\/ &#039;&#043;&#039; &#039;-&#039; &#039;*&#039; &#039;\/&#039; &#039;%&#039;<br \/>\n};<\/p>\n<p>class Response<br \/>\n{<br \/>\npublic:<br \/>\n    std::string Serialize() \/\/ &#034;code_ result_&#034;<br \/>\n    {<\/p>\n<p>        std::string s;<br \/>\n        s &#061; std::to_string(_code);<br \/>\n        s &#043;&#061; SPACE;<br \/>\n        s &#043;&#061; std::to_string(_result);<\/p>\n<p>        return s;<br \/>\n    }<br \/>\n    bool Deserialized(const std::string&amp; s)<br \/>\n    {<br \/>\n        std::size_t pos &#061; s.find(SPACE);<br \/>\n        if (pos &#061;&#061; std::string::npos)<br \/>\n            return false;<br \/>\n        _code &#061; atoi(s.substr(0, pos).c_str());<br \/>\n        _result &#061; atoi(s.substr(pos &#043; SPACE_LEN).c_str());<br \/>\n        return true;<br \/>\n    }<br \/>\npublic:<br \/>\n    Response()<br \/>\n    {}<br \/>\n    Response(int result, int code)<br \/>\n        : _result(result), _code(code)<br \/>\n    {}<br \/>\n    ~Response()<br \/>\n    {}<br \/>\npublic:<br \/>\n    int _result; \/\/ \u8ba1\u7b97\u7ed3\u679c<br \/>\n    int _code;   \/\/ \u8ba1\u7b97\u7ed3\u679c\u7684\u72b6\u6001\u7801<br \/>\n};<\/p>\n<h5 id=\"main.cc\">main.cc<\/h5>\n<p>#include &#034;TcpServer.hpp&#034;<br \/>\n#include &lt;memory&gt;<\/p>\n<p>static Response calculator(const Request &amp;req)<br \/>\n{<br \/>\n    Response resp(0, 0);<br \/>\n    switch (req.op_)<br \/>\n    {<br \/>\n    case &#039;&#043;&#039;:<br \/>\n        resp.result_ &#061; req.x_ &#043; req.y_;<br \/>\n        break;<br \/>\n    case &#039;-&#039;:<br \/>\n        resp.result_ &#061; req.x_ &#8211; req.y_;<br \/>\n        break;<br \/>\n    case &#039;*&#039;:<br \/>\n        resp.result_ &#061; req.x_ * req.y_;<br \/>\n        break;<br \/>\n    case &#039;\/&#039;:<br \/>\n        if (0 &#061;&#061; req.y_)<br \/>\n            resp.code_ &#061; 1;<br \/>\n        else<br \/>\n            resp.result_ &#061; req.x_ \/ req.y_;<br \/>\n        break;<br \/>\n    case &#039;%&#039;:<br \/>\n        if (0 &#061;&#061; req.y_)<br \/>\n            resp.code_ &#061; 2;<br \/>\n        else<br \/>\n            resp.result_ &#061; req.x_ % req.y_;<br \/>\n        break;<br \/>\n    default:<br \/>\n        resp.code_ &#061; 3;<br \/>\n        break;<br \/>\n    }<br \/>\n    return resp;<br \/>\n}<\/p>\n<p>void NetCal(Connection *conn, std::string &amp;request)<br \/>\n{<br \/>\n    logMessage(DEBUG, &#034;NetCal been called, get request: %s&#034;, request.c_str());<br \/>\n    Request req; \/\/ 1. \u53cd\u5e8f\u5217\u5316&#xff0c;1 &#043; 1    2 &#043; 3<br \/>\n    if(!req.Deserialized(request))<br \/>\n        return;<\/p>\n<p>    Response resp &#061; calculator(req); \/\/ 2. \u4e1a\u52a1\u5904\u7406<\/p>\n<p>    std::string sendstr &#061; resp.Serialize(); \/\/ 3. \u5e8f\u5217\u5316,\u6784\u5efa\u5e94\u7b54<br \/>\n    sendstr &#061; Encode(sendstr);<\/p>\n<p>    conn-&gt;_outbuffer &#043;&#061; sendstr; \/\/ 4. \u4ea4\u7ed9\u670d\u52a1\u5668conn<\/p>\n<p>    \/\/ 5. \u60f3\u529e\u6cd5&#xff0c;\u8ba9\u5e95\u5c42\u7684TcpServer\u5f00\u59cb\u53d1\u9001 -&gt; \u9700\u8981\u6709\u5b8c\u6574\u7684\u53d1\u9001\u903b\u8f91<br \/>\n    \/\/ \u89e6\u53d1\u53d1\u9001\u7684\u52a8\u4f5c&#xff0c;\u4e00\u65e6\u5f00\u542fEPOLLOUT&#xff0c;epoll\u4f1a\u81ea\u52a8\u7acb\u9a6c\u89e6\u53d1\u4e00\u6b21\u53d1\u9001\u4e8b\u4ef6\u5c31\u7eea&#xff0c;\u5982\u679c\u540e\u7eed\u4fdd\u6301\u53d1\u9001\u7684\u5f00\u542f&#xff0c;epoll\u4f1a\u4e00\u76f4\u53d1\u9001<br \/>\n    conn-&gt;_tsvr-&gt;EnableReadWrite(conn, true, true); \/\/ \u5199\u5b8cEnableReadWrite\u624d\u53d1\u73b0\u56de\u6307\u6307\u9488\u7684\u4f5c\u7528<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    std::unique_ptr&lt;TcpServer&gt; svr(new TcpServer());<br \/>\n    svr-&gt;Dispather(NetCal);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>Sender\u51fd\u6570&#xff1a;<\/p>\n<p>   void Sender(Connection *conn)<br \/>\n    {<br \/>\n        while(true)<br \/>\n        {<br \/>\n            ssize_t n &#061; send(conn-&gt;_sock, conn-&gt;_outbuffer.c_str(), conn-&gt;_outbuffer.size(), 0);<br \/>\n            if(n &gt; 0) \/\/ \u53d1\u9001\u6210\u529f -&gt; \u79fb\u9664<br \/>\n            {<br \/>\n                conn-&gt;_outbuffer.erase(0, n);<br \/>\n                if(conn-&gt;_outbuffer.empty())  \/\/ \u53d1\u5b8c\u4e86 -&gt; break<br \/>\n                    break;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                if(errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK) \/\/ \u7f13\u51b2\u533a\u6ee1\u4e86 -&gt; break\u4e0b\u6b21\u518d\u53d1<br \/>\n                    break;<br \/>\n                else if(errno &#061;&#061; EINTR) \/\/ \u53d1\u9001\u88ab\u4e2d\u65ad -&gt; \u91cd\u65b0\u53d1\u9001<br \/>\n                    continue;<br \/>\n                else \/\/ \u771f\u6b63\u8bfb\u53d6\u5931\u8d25 -&gt; \u4ea4\u7ed9\u5f02\u5e38\u56de\u8c03<br \/>\n                {<br \/>\n                    logMessage(ERROR, &#034;send error, %d : %s&#034;, errno, strerror(errno));<br \/>\n                    conn-&gt;_except_cb(conn);<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        \/\/ \u4e0d\u786e\u5b9a\u6709\u6ca1\u6709\u53d1\u5b8c&#xff0c;\u4f46\u662f\u53ef\u4ee5\u4fdd\u8bc1&#xff0c;\u5982\u679c\u6ca1\u6709\u51fa\u9519&#xff0c;\u4e00\u5b9a\u662f\u8981\u4e48\u53d1\u5b8c&#xff0c;\u8981\u4e48\u53d1\u9001\u6761\u4ef6\u4e0d\u6ee1\u8db3&#xff0c;\u4e0b\u6b21\u518d\u53d1<br \/>\n        if(conn-&gt;_outbuffer.empty())<br \/>\n            EnableReadWrite(conn, true, false);<br \/>\n        else<br \/>\n            EnableReadWrite(conn, true, true);<br \/>\n    }<br \/>\n    void EnableReadWrite(Connection *conn, bool readable, bool writeable) \/\/ \u63a7\u5236\u8bfb\u5199\u5f00\u5173<br \/>\n    {<br \/>\n        uint32_t events &#061; ((readable ? EPOLLIN : 0) | (writeable ? EPOLLOUT : 0));<br \/>\n        bool res &#061; _poll.CtrlEpoll(conn-&gt;_sock, events);<br \/>\n        assert(res);<br \/>\n    }<\/p>\n<h5 id=\"Epoll.hpp\">Epoll.hpp<\/h5>\n<p>\u52a0\u4e86\u6253\u5f00\u548c\u5220\u9664\u5c31\u5b8c\u6574\u4e86&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"791\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260208141009-69889941928e3.png\" width=\"1535\" \/><\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<\/p>\n<p>class Epoll<br \/>\n{<br \/>\n    const static int gnum &#061; 128;<br \/>\n    const static int gtimeout &#061; 5000;<br \/>\npublic:<br \/>\n    Epoll(int timeout &#061; gtimeout)<br \/>\n        : _timeout(timeout)<br \/>\n    {}<br \/>\n    void CreateEpoll()<br \/>\n    {<br \/>\n        _epfd &#061; epoll_create(gnum);<br \/>\n        if (_epfd &lt; 0)<br \/>\n            exit(5);<br \/>\n    }<br \/>\n    bool DelFromEpoll(int sock) \/\/ \u79fb\u9664sock\u7684\u6240\u6709\u4e8b\u4ef6<br \/>\n    {<br \/>\n        int n &#061; epoll_ctl(_epfd, EPOLL_CTL_DEL, sock, nullptr);<br \/>\n        return n &#061;&#061; 0;<br \/>\n    }<br \/>\n    bool CtrlEpoll(int sock, uint32_t events) \/\/ \u6253\u5f00sock\u7684\u4e8b\u4ef6<br \/>\n    {<br \/>\n        events |&#061; EPOLLET;<br \/>\n        struct epoll_event ev;<br \/>\n        ev.events &#061; events;<br \/>\n        ev.data.fd &#061; sock;<br \/>\n        int n &#061; epoll_ctl(_epfd, EPOLL_CTL_MOD, sock, &amp;ev);<br \/>\n        return n &#061;&#061; 0;<br \/>\n    }<br \/>\n    bool AddSockToEpoll(int sock, uint32_t events)<br \/>\n    {   \/\/ \u6dfb\u52a0sock\u5230epoll\u4e2d(\u4efb\u52a1\u901a\u77e5)-&gt;\u8981\u77e5\u9053sock\u548c\u4e8b\u4ef6(\u4efb\u4f55\u591a\u8def\u8f6c\u63a5\u7684\u670d\u52a1\u5668&#xff0c;\u4e00\u822c\u53ea\u4f1a\u6253\u5f00\u8bfb\u53d6\u4e8b\u4ef6&#xff0c;\u5199\u5165\u4e8b\u4ef6\u6309\u9700\u6253\u5f00)<br \/>\n        struct epoll_event ev;<br \/>\n        ev.events &#061; events;<br \/>\n        ev.data.fd &#061; sock;<br \/>\n        int n &#061; epoll_ctl(_epfd, EPOLL_CTL_ADD, sock, &amp;ev);<br \/>\n        return n &#061;&#061; 0;<br \/>\n    }<br \/>\n    int WaitEpoll(struct epoll_event revs[], int num)<br \/>\n    {<br \/>\n        return epoll_wait(_epfd, revs, num, _timeout);<br \/>\n    }<br \/>\n    ~Epoll()<br \/>\n    {}<\/p>\n<p>private:<br \/>\n    int _epfd;<br \/>\n    int _timeout;<br \/>\n};<\/p>\n<p>Excepter\u51fd\u6570&#xff1a;<\/p>\n<p>    void Excepter(Connection *conn)<br \/>\n    {<br \/>\n        if(!IsConnectionExists(conn-&gt;_sock)) \/\/ _sock\u4e0d\u5b58\u5728\u5c31\u8fd4\u56de<br \/>\n            return;<br \/>\n        bool res &#061; _poll.DelFromEpoll(conn-&gt;_sock); \/\/ 1. \u4eceepoll\u4e2d\u79fb\u9664<br \/>\n        assert(res);<\/p>\n<p>        _connections.erase(conn-&gt;_sock); \/\/ 2. \u4eceunorder_map\u4e2d\u79fb\u9664<\/p>\n<p>        close(conn-&gt;_sock); \/\/ 3. \u5173\u95edsock<\/p>\n<p>        delete conn; \/\/ 4. \u91ca\u653e conn;<br \/>\n        logMessage(DEBUG, &#034;Excepter \u56de\u6536\u5b8c\u6bd5\u6240\u6709\u7684\u5f02\u5e38\u60c5\u51b5&#034;);<br \/>\n    }<\/p>\n<p>\u7f16\u8bd1\u8fd0\u884c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"918\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260208141009-69889941e6d93.png\" width=\"1617\" \/><\/p>\n<p>\u6b64\u65f6\u4ee3\u7801\u5c31\u7ed3\u675f\u4e86&#xff0c;\u53ef\u4ee5\u81ea\u5df1\u62d3\u5c55\u4e00\u4e0b\u3002\u4e0b\u9762\u653e\u4e00\u4e0b\u5b8c\u6574\u7684TcpServer.hpp&#xff1a;<\/p>\n<h5 id=\"TcpServer.hpp\">TcpServer.hpp<\/h5>\n<p>#pragma once<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cassert&gt;<br \/>\n#include &lt;unordered_map&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &#034;Sock.hpp&#034;<br \/>\n#include &#034;Log.hpp&#034;<br \/>\n#include &#034;Epoll.hpp&#034;<br \/>\n#include &#034;Protocol.hpp&#034;<\/p>\n<p>class TcpServer;<br \/>\nclass Connection;<\/p>\n<p>using func_t &#061; std::function&lt;void(Connection *)&gt;;<br \/>\nusing callback_t &#061; std::function&lt;void (Connection*, std::string &amp;request)&gt;; \/\/ \u4e0a\u5c42\u4e1a\u52a1\u5904\u7406\u7684\u65b9\u6cd5<\/p>\n<p>\/\/ \u4e3a\u4e86\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c&#xff0c;\u5e38\u89c4\u7684sock\u5fc5\u987b\u8981\u6709\u72ec\u7acb\u7684\u63a5\u6536\u7f13\u51b2\u533a\u548c\u53d1\u9001\u7f13\u51b2\u533a(\u5199\u5165)<br \/>\nclass Connection \/\/ \u4e00\u4e2a\u94fe\u63a5\u7c7b<br \/>\n{<br \/>\npublic:<br \/>\n    Connection(int sock &#061; -1)<br \/>\n        : _sock(sock), _tsvr(nullptr)<br \/>\n    {<br \/>\n    }<br \/>\n    void SetCallBack(func_t recv_cb, func_t send_cb, func_t except_cb)<br \/>\n    { \/\/ \u8bbe\u7f6e\u4e09\u4e2a\u56de\u8c03\u65b9\u6cd5<br \/>\n        _recv_cb &#061; recv_cb;<br \/>\n        _send_cb &#061; send_cb;<br \/>\n        _except_cb &#061; except_cb;<br \/>\n    }<br \/>\n    ~Connection()<br \/>\n    {<br \/>\n    }<\/p>\n<p>public:<br \/>\n    int _sock;       \/\/ \u8d1f\u8d23\u8fdb\u884cIO\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    func_t _recv_cb; \/\/ \u4e09\u4e2a\u56de\u8c03\u65b9\u6cd5&#xff0c;\u662f\u5bf9_sock\u8fdb\u884c\u7279\u5b9a\u8bfb\u5199\u7684\u5bf9\u5e94\u65b9\u6cd5<br \/>\n    func_t _send_cb;<br \/>\n    func_t _except_cb;<\/p>\n<p>    std::string _inbuffer;  \/\/ \u63a5\u6536\u7f13\u51b2\u533a&amp;&amp;\u53d1\u9001\u7f13\u51b2\u533a<br \/>\n    std::string _outbuffer; \/\/ \u8fd9\u4e24string\u6682\u65f6\u6ca1\u6709\u529e\u6cd5\u5904\u7406\u4e8c\u8fdb\u5236\u6d41&#xff0c;\u6587\u672c\u662f\u53ef\u4ee5\u7684<\/p>\n<p>    TcpServer *_tsvr; \/\/ \u8bbe\u7f6e\u5bf9TcpServer\u7684\u56de\u6307\u6307\u9488&#xff0c;\u5bf9\u5199\u4e8b\u4ef6\u7684\u5173\u5fc3\u662f\u6309\u9700\u6253\u5f00<br \/>\n};<\/p>\n<p>class TcpServer<br \/>\n{<br \/>\n    const static int gport &#061; 8080;<br \/>\n    const static int gnum &#061; 128;<\/p>\n<p>public:<br \/>\n    TcpServer(int port &#061; gport)<br \/>\n        : _port(port), _revs_num(gnum)<br \/>\n    {<br \/>\n        \/\/ 1. \u521b\u5efalistensock<br \/>\n        _listensock &#061; Sock::Socket();<br \/>\n        Sock::Bind(_listensock, _port);<br \/>\n        Sock::Listen(_listensock);<\/p>\n<p>        \/\/ 2. \u521b\u5efa\u591a\u8def\u8f6c\u63a5\u5bf9\u8c61<br \/>\n        _poll.CreateEpoll();<\/p>\n<p>        \/\/ 3. \u6dfb\u52a0listensock\u5230\u670d\u52a1\u5668\u4e2d -&gt; \u4e09\u6b65(\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e5f\u80fd\u8c03\u7528\u7c7b\u7684\u6210\u5458\u65b9\u6cd5&#xff0c;\u8d70\u5230\u51fd\u6570\u4f53\u4e2d\u5bf9\u8c61\u5df2\u7ecf\u5b58\u5728\u4e86)<br \/>\n        \/\/ \u540e\u4e09\u4e2a\u53c2\u6570\u662f\u51fd\u6570\u5bf9\u8c61&#xff0c;\u8981bind\u7ed1\u5b9a\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61-&gt;\u7c7b\u5185\u51fd\u6570\u6709this\u6307\u9488&#xff0c;_1\u662f\u9884\u7559\u7684\u53c2\u6570<br \/>\n        AddConnection(_listensock, std::bind(&amp;TcpServer::Accepter, this, std::placeholders::_1), nullptr, nullptr);<\/p>\n<p>        \/\/ 4. \u6784\u5efa\u4e00\u4e2a\u83b7\u53d6\u5c31\u7eea\u4e8b\u4ef6\u7684\u7f13\u51b2\u533a<br \/>\n        _revs &#061; new struct epoll_event[_revs_num];<br \/>\n    }<br \/>\n    void AddConnection(int sock, func_t recv_cb, func_t send_cb, func_t except_cb) \/\/ \u628a\u4efb\u610fsock\u8fdb\u884c\u6dfb\u52a0\u5230TcpServer<br \/>\n    {<br \/>\n        Sock::SetNonBlock(sock); \/\/ ET\u6a21\u5f0f\u8981\u628asock\u8bbe\u7f6e\u6210\u975e\u963b\u585e -&gt; \u5728Sock.hpp\u4e2d\u5199\u6210\u51fd\u6570<\/p>\n<p>        \/\/ \u9664\u4e86_listensock&#xff0c;\u540e\u9762\u8fd8\u4f1a\u5b58\u5728\u5927\u91cf\u7684socket&#xff0c;\u6bcf\u4e00\u4e2asock\u90fd\u5fc5\u987b\u88ab\u5c01\u88c5\u6210\u4e3a\u4e00\u4e2aConnection<br \/>\n        \/\/ \u5f53\u670d\u52a1\u5668\u4e2d\u5b58\u5728\u5927\u91cf\u7684Connection\u65f6&#xff0c;TcpServer\u9700\u8981\u5c06\u6240\u6709Connection\u8fdb\u884c\u7ba1\u7406&#xff1a;\u4e0a\u9762\u63cf\u8ff0\u4e86&#xff0c;\u7ec4\u7ec7 -&gt; unordered_map<br \/>\n        \/\/ 3.1 \u6784\u5efaconn\u5bf9\u8c61&#xff0c;\u5c01\u88c5sock<br \/>\n        Connection *conn &#061; new Connection(sock);<br \/>\n        conn-&gt;SetCallBack(recv_cb, send_cb, except_cb);<br \/>\n        conn-&gt;_tsvr &#061; this;<\/p>\n<p>        \/\/ 3.2 \u6dfb\u52a0sock\u5230epoll\u4e2d(\u4efb\u52a1\u901a\u77e5)-&gt;\u8981\u77e5\u9053sock\u548c\u4e8b\u4ef6(\u4efb\u4f55\u591a\u8def\u8f6c\u63a5\u7684\u670d\u52a1\u5668&#xff0c;\u4e00\u822c\u53ea\u4f1a\u6253\u5f00\u8bfb\u53d6\u4e8b\u4ef6&#xff0c;\u5199\u5165\u4e8b\u4ef6\u6309\u9700\u6253\u5f00)<br \/>\n        _poll.AddSockToEpoll(sock, EPOLLIN | EPOLLET);<\/p>\n<p>        \/\/ 3.3 \u5c06\u5bf9\u5e94\u7684Connection*\u5bf9\u8c61\u6307\u9488\u6dfb\u52a0\u5230Connections\u6620\u5c04\u8868\u4e2d(\u4e1a\u52a1\u5904\u7406)<br \/>\n        _connections.insert(std::make_pair(sock, conn));<br \/>\n    }<\/p>\n<p>    void Accepter(Connection *conn)<br \/>\n    {<br \/>\n        \/\/ logMessage(DEBUG, &#034;Accepter been called&#034;);<br \/>\n        \/\/ \u4e00\u5b9a\u662flistensock\u5df2\u7ecf\u5c31\u7eea\u4e86&#xff0c;\u6b64\u6b21\u8bfb\u53d6\u4e0d\u4f1a\u963b\u585e&#xff0c;<br \/>\n        \/\/ \u600e\u4e48\u4fdd\u8bc1&#xff0c;\u5e95\u5c42\u53ea\u6709\u4e00\u4e2a\u8fde\u63a5\u5c31\u7eea\u5462 -&gt; \u5faa\u73af&#xff0c;\u76f4\u5230\u83b7\u53d6\u5931\u8d25<br \/>\n        while (true)<br \/>\n        {<br \/>\n            std::string clientip;<br \/>\n            uint16_t clientport;<br \/>\n            int accept_errno &#061; 0;<br \/>\n            \/\/ sock\u4e00\u5b9a\u662f\u5e38\u89c4\u7684IO sock<br \/>\n            int sock &#061; Sock::Accept(conn-&gt;_sock, &amp;clientip, &amp;clientport, &amp;accept_errno);<br \/>\n            if (sock &lt; 0) \/\/ \u83b7\u53d6\u5931\u8d25<br \/>\n            {<br \/>\n                if (accept_errno &#061;&#061; EAGAIN || accept_errno &#061;&#061; EWOULDBLOCK)<br \/>\n                    break;<br \/>\n                else if (accept_errno &#061;&#061; EINTR) \/\/ \u6982\u7387\u975e\u5e38\u4f4e<br \/>\n                    continue;<br \/>\n                else \/\/ accept\u5931\u8d25<br \/>\n                {<br \/>\n                    logMessage(WARNING, &#034;accept error, %d : %s&#034;, accept_errno, strerror(accept_errno));<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n            else \/\/ (sock&gt;&#061;0)\u83b7\u53d6\u94fe\u63a5\u6210\u529f-&gt;\u5c06sock\u6258\u7ba1\u7ed9TcpServer<br \/>\n            {<br \/>\n                AddConnection(sock, std::bind(&amp;TcpServer::Recver, this, std::placeholders::_1),<br \/>\n                              std::bind(&amp;TcpServer::Sender, this, std::placeholders::_1),<br \/>\n                              std::bind(&amp;TcpServer::Excepter, this, std::placeholders::_1));<br \/>\n                logMessage(DEBUG, &#034;accept client %s:%d success, add to epoll&amp;&amp;TcpServer success, sock: %d&#034;,<br \/>\n                           clientip.c_str(), clientport, sock);<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    void Recver(Connection *conn) \/\/ \u8bfb\u53d6\u4e00\u4e2a\u6b63\u5e38\u7684sock<br \/>\n    {<br \/>\n        const int num &#061; 1024;<br \/>\n        bool err &#061; false;<br \/>\n        \/\/ logMessage(DEBUG, &#034;Recver event exists, Recver() been called&#034;);<br \/>\n        while (true)<br \/>\n        {<br \/>\n            char buffer[num];<br \/>\n            ssize_t n &#061; recv(conn-&gt;_sock, buffer, sizeof(buffer) &#8211; 1, 0);<br \/>\n            if (n &lt; 0)<br \/>\n            {<br \/>\n                if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK) \/\/ \u8bfb\u53d6\u5b8c\u6bd5\u4e86&#xff0c;\u6b63\u5e38\u7684<br \/>\n                    break;<br \/>\n                else if (errno &#061;&#061; EINTR) \/\/ \u8bfb\u53d6\u88ab\u4e2d\u65ad\u4e86&#xff0c;\u91cd\u65b0\u5f00\u59cb\u8bfb<br \/>\n                    continue;<br \/>\n                else \/\/ \u771f\u6b63\u8bfb\u53d6\u5931\u8d25 -&gt; \u4ea4\u7ed9\u5f02\u5e38\u56de\u8c03<br \/>\n                {<br \/>\n                    logMessage(ERROR, &#034;recv error, %d : %s&#034;, errno, strerror(errno));<br \/>\n                    conn-&gt;_except_cb(conn);<br \/>\n                    err &#061; true;<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n            else if (n &#061;&#061; 0)<br \/>\n            {<br \/>\n                logMessage(DEBUG, &#034;client[%d] quit, server close [%d]&#034;, conn-&gt;_sock, conn-&gt;_sock);<br \/>\n                conn-&gt;_except_cb(conn);<br \/>\n                err &#061; true;<br \/>\n                break;<br \/>\n            }<br \/>\n            else \/\/ \u8bfb\u53d6\u6210\u529f<br \/>\n            {<br \/>\n                buffer[n] &#061; 0;<br \/>\n                conn-&gt;_inbuffer &#043;&#061; buffer; \/\/ \u8bfb\u53d6\u5230\u7684\u6570\u636e\u5168\u90e8\u62fc\u63a5\u5230\u63a5\u6536\u7f13\u51b2\u533a<br \/>\n            }<br \/>\n        }<br \/>\n        logMessage(DEBUG, &#034;conn-&gt;_inbuffer[sock: %d]: %s&#034;, conn-&gt;_sock, conn-&gt;_inbuffer.c_str());<br \/>\n        if (!err) \/\/ \u5982\u679c\u9519\u8bef\u7801\u8fd8\u662ffalse\u5c31\u662f\u6b63\u5e38break\u7684<br \/>\n        {<br \/>\n            std::vector&lt;std::string&gt; messages;<br \/>\n            SpliteMessage(conn-&gt;_inbuffer, &amp;messages);<br \/>\n            \/\/ \u4fdd\u8bc1\u8d70\u5230\u8fd9\u91cc&#xff0c;\u5c31\u662f\u4e00\u4e2a\u5b8c\u6574\u62a5\u6587<br \/>\n            for (auto &amp;msg : messages)<br \/>\n            {    \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u5c06message\u5c01\u88c5\u6210\u4e3atask,\u7136\u540epush\u5230\u4efb\u52a1\u961f\u5217,\u4efb\u52a1\u5904\u7406\u4ea4\u7ed9\u540e\u7aef\u7ebf\u7a0b\u6c60,\u8fd9\u91cc\u4e0d\u5904\u7406<br \/>\n                _cb(conn, msg);<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>   void Sender(Connection *conn)<br \/>\n    {<br \/>\n        while(true)<br \/>\n        {<br \/>\n            ssize_t n &#061; send(conn-&gt;_sock, conn-&gt;_outbuffer.c_str(), conn-&gt;_outbuffer.size(), 0);<br \/>\n            if(n &gt; 0) \/\/ \u53d1\u9001\u6210\u529f -&gt; \u79fb\u9664<br \/>\n            {<br \/>\n                conn-&gt;_outbuffer.erase(0, n);<br \/>\n                if(conn-&gt;_outbuffer.empty())  \/\/ \u53d1\u5b8c\u4e86 -&gt; break<br \/>\n                    break;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                if(errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK) \/\/ \u7f13\u51b2\u533a\u6ee1\u4e86 -&gt; break\u4e0b\u6b21\u518d\u53d1<br \/>\n                    break;<br \/>\n                else if(errno &#061;&#061; EINTR) \/\/ \u53d1\u9001\u88ab\u4e2d\u65ad -&gt; \u91cd\u65b0\u53d1\u9001<br \/>\n                    continue;<br \/>\n                else \/\/ \u771f\u6b63\u8bfb\u53d6\u5931\u8d25 -&gt; \u4ea4\u7ed9\u5f02\u5e38\u56de\u8c03<br \/>\n                {<br \/>\n                    logMessage(ERROR, &#034;send error, %d : %s&#034;, errno, strerror(errno));<br \/>\n                    conn-&gt;_except_cb(conn);<br \/>\n                    break;<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        \/\/ \u4e0d\u786e\u5b9a\u6709\u6ca1\u6709\u53d1\u5b8c&#xff0c;\u4f46\u662f\u53ef\u4ee5\u4fdd\u8bc1&#xff0c;\u5982\u679c\u6ca1\u6709\u51fa\u9519&#xff0c;\u4e00\u5b9a\u662f\u53d1\u5b8c\u4e86&#xff0c;\u6216\u8005\u53d1\u9001\u6761\u4ef6\u4e0d\u6ee1\u8db3&#xff0c;\u4e0b\u6b21\u518d\u53d1<br \/>\n        if(conn-&gt;_outbuffer.empty()) \/\/ \u53d1\u5b8c\u4e86-&gt;\u4e0d\u7528\u5173\u5fc3\u5199<br \/>\n            EnableReadWrite(conn, true, false);<br \/>\n        else \/\/ \u53d1\u9001\u6761\u4ef6\u4e0d\u6ee1\u8db3&#xff0c;\u4e0b\u6b21\u518d\u53d1<br \/>\n            EnableReadWrite(conn, true, true);<br \/>\n    }<br \/>\n    void EnableReadWrite(Connection *conn, bool readable, bool writeable) \/\/ \u63a7\u5236\u8bfb\u5199\u5f00\u5173<br \/>\n    {            \/\/ \u4e0b\u9762\u7684\u4e09\u76ee&#xff1a;\u5982readable\u4e3a\u771f\u5c31\u5173\u5fc3\u8bfb\u4e8b\u4ef6,\u5426\u5219\u4e3a0,writeable\u5c31\u5173\u5fc3\u5199\u4e8b\u4ef6<br \/>\n        uint32_t events &#061; ((readable ? EPOLLIN : 0) | (writeable ? EPOLLOUT : 0));<br \/>\n        bool res &#061; _poll.CtrlEpoll(conn-&gt;_sock, events);<br \/>\n        assert(res);<br \/>\n    }<\/p>\n<p>    void Excepter(Connection *conn)<br \/>\n    {<br \/>\n        if(!IsConnectionExists(conn-&gt;_sock)) \/\/ _sock\u4e0d\u5b58\u5728\u5c31\u8fd4\u56de<br \/>\n            return;<br \/>\n        bool res &#061; _poll.DelFromEpoll(conn-&gt;_sock); \/\/ 1. \u4eceepoll\u4e2d\u79fb\u9664<br \/>\n        assert(res);<\/p>\n<p>        _connections.erase(conn-&gt;_sock); \/\/ 2. \u4eceunorder_map\u4e2d\u79fb\u9664<\/p>\n<p>        close(conn-&gt;_sock); \/\/ 3. \u5173\u95edsock<\/p>\n<p>        delete conn; \/\/ 4. \u91ca\u653e conn;<br \/>\n        logMessage(DEBUG, &#034;Excepter \u56de\u6536\u5b8c\u6bd5\u6240\u6709\u7684\u5f02\u5e38\u60c5\u51b5&#034;);<br \/>\n    }<\/p>\n<p>    void Dispather(callback_t cb) \/\/ \u6839\u636e\u5c31\u7eea\u7684\u4e8b\u4ef6&#xff0c;\u8fdb\u884c\u7279\u5b9a\u4e8b\u4ef6\u7684\u6d3e\u53d1<br \/>\n    {<br \/>\n        _cb &#061; cb;<br \/>\n        while (true)<br \/>\n        {<br \/>\n            LoopOnce();<br \/>\n        }<br \/>\n    }<br \/>\n    void LoopOnce()<br \/>\n    {<br \/>\n        int n &#061; _poll.WaitEpoll(_revs, _revs_num);<br \/>\n        for (int i &#061; 0; i &lt; n; i&#043;&#043;) \/\/ \u83b7\u53d6\u4e8b\u4ef6<br \/>\n        {<br \/>\n            int sock &#061; _revs[i].data.fd;<br \/>\n            uint32_t revents &#061; _revs[i].events;<br \/>\n            if (revents &amp; EPOLLIN) \/\/ \u8bfb\u5c31\u7eea<br \/>\n            {<br \/>\n                \/\/ if(Connection\u662f\u5b58\u5728\u5e76\u4e14_connections[sock]-&gt;_recv_cb\u88ab\u8bbe\u7f6e\u8fc7)<br \/>\n                if (IsConnectionExists(sock) &amp;&amp; _connections[sock]-&gt;_recv_cb !&#061; nullptr)<br \/>\n                    _connections[sock]-&gt;_recv_cb(_connections[sock]); \/\/ \u8c03\u7528\u8bfb\u4e8b\u4ef6\u7684\u56de\u8c03<br \/>\n            }<br \/>\n            if (revents &amp; EPOLLOUT) \/\/ \u5199\u5c31\u7eea<br \/>\n            {<br \/>\n                \/\/ if(Connection\u662f\u5b58\u5728\u5e76\u4e14_connections[sock]-&gt;_send_cb\u88ab\u8bbe\u7f6e\u8fc7)<br \/>\n                if (IsConnectionExists(sock) &amp;&amp; _connections[sock]-&gt;_send_cb !&#061; nullptr)<br \/>\n                    _connections[sock]-&gt;_send_cb(_connections[sock]); \/\/ \u8c03\u7528\u5199\u4e8b\u4ef6\u7684\u56de\u8c03<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    bool IsConnectionExists(int sock) \/\/ \u5224\u5b9aConnection\u662f\u5426\u5b58\u5728<br \/>\n    {<br \/>\n        auto iter &#061; _connections.find(sock);<br \/>\n        if (iter &#061;&#061; _connections.end())<br \/>\n            return false;<br \/>\n        else<br \/>\n            return true;<br \/>\n    }<br \/>\n    ~TcpServer()<br \/>\n    {<br \/>\n        if (_listensock &gt;&#061; 0)<br \/>\n            close(_listensock);<br \/>\n        if (_revs)<br \/>\n            delete[] _revs;<br \/>\n    }<\/p>\n<p>private:<br \/>\n    int _listensock;<br \/>\n    int _port;<br \/>\n    Epoll _poll;<br \/>\n    std::unordered_map&lt;int, Connection *&gt; _connections; \/\/ \u7ba1\u7406&#xff1a;sock\u6620\u5c04\u5230Connection<br \/>\n    struct epoll_event *_revs;                          \/\/ \u5c31\u7eea\u4e8b\u4ef6\u7f13\u51b2\u533a,\u5c31\u7eea\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6295\u9012\u5230\u8fd9\u91cc<br \/>\n    int _revs_num;                                      \/\/ \u5c31\u7eea\u4e8b\u4ef6\u7f13\u51b2\u533a\u5927\u5c0f<\/p>\n<p>    callback_t _cb; \/\/ \u5904\u7406\u4e0a\u5c42\u7684\u4e1a\u52a1\u7684\u56de\u8c03\u51fd\u6570<br \/>\n};<\/p>\n<hr \/>\n<h3 id=\"%E6%9C%AC%E7%AF%87%E5%AE%8C%E3%80%82\">\u672c\u7bc7\u5b8c\u3002<\/h3>\n","protected":false},"excerpt":{"rendered":"<p>1. reactor\u7684\u670d\u52a1\u56681.0\u00a0reactor\u670d\u52a1\u5668\u4ecb\u7ecdReactor\u662f\u4e00\u79cd\u4e8b\u4ef6\u9a71\u52a8\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u6a21\u578b&#xff0c;\u6838\u5fc3\u662f\u901a\u8fc7\u5355\u7ebf\u7a0b\/\u5c11\u91cf\u7ebf\u7a0b\u7ba1\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u3002\u5176\u6838\u5fc3\u7ec4\u4ef6\u5305\u62ec&#xff1a;I\/O\u591a\u8def\u590d\u7528\u5668&#xff08;\u5982epoll&#xff09;&#xff1a;\u76d1\u542c\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u7684\u4e8b\u4ef6&#xff08;\u8bfb\u3001\u5199\u3001\u5f02\u5e38&#xff09;\u3002\u4e8b\u4ef6\u5206\u53d1\u5668&#xff1a;\u5c06\u5c31\u7eea\u4e8b\u4ef6\u5206\u53d1\u7ed9\u5bf9\u5e94\u7684\u5904\u7406\u5668\u3002\u4e8b\u4ef6\u5904\u7406\u5668&#xff1a;\u6267\u884c\u5177\u4f53\u7684I\/O\u64cd\u4f5c\u548c<\/p>\n","protected":false},"author":2,"featured_media":73995,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,43,44],"topic":[],"class_list":["post-74003","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-linux","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7f51\u7edc\u548cLinux\u7f51\u7edc-15(IO\u591a\u8def\u8f6c\u63a5)reactor\u7f16\u7a0b-\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\/74003.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7f51\u7edc\u548cLinux\u7f51\u7edc-15(IO\u591a\u8def\u8f6c\u63a5)reactor\u7f16\u7a0b-\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"1. reactor\u7684\u670d\u52a1\u56681.0\u00a0reactor\u670d\u52a1\u5668\u4ecb\u7ecdReactor\u662f\u4e00\u79cd\u4e8b\u4ef6\u9a71\u52a8\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u6a21\u578b&#xff0c;\u6838\u5fc3\u662f\u901a\u8fc7\u5355\u7ebf\u7a0b\/\u5c11\u91cf\u7ebf\u7a0b\u7ba1\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u3002\u5176\u6838\u5fc3\u7ec4\u4ef6\u5305\u62ec&#xff1a;I\/O\u591a\u8def\u590d\u7528\u5668&#xff08;\u5982epoll&#xff09;&#xff1a;\u76d1\u542c\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u7684\u4e8b\u4ef6&#xff08;\u8bfb\u3001\u5199\u3001\u5f02\u5e38&#xff09;\u3002\u4e8b\u4ef6\u5206\u53d1\u5668&#xff1a;\u5c06\u5c31\u7eea\u4e8b\u4ef6\u5206\u53d1\u7ed9\u5bf9\u5e94\u7684\u5904\u7406\u5668\u3002\u4e8b\u4ef6\u5904\u7406\u5668&#xff1a;\u6267\u884c\u5177\u4f53\u7684I\/O\u64cd\u4f5c\u548c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/74003.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-08T14:10:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260208141007-6988993f6b1a6.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=\"17 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/74003.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/74003.html\",\"name\":\"\u7f51\u7edc\u548cLinux\u7f51\u7edc-15(IO\u591a\u8def\u8f6c\u63a5)reactor\u7f16\u7a0b-\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-08T14:10:11+00:00\",\"dateModified\":\"2026-02-08T14:10:11+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/74003.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/74003.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/74003.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7f51\u7edc\u548cLinux\u7f51\u7edc-15(IO\u591a\u8def\u8f6c\u63a5)reactor\u7f16\u7a0b-\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\u548cLinux\u7f51\u7edc-15(IO\u591a\u8def\u8f6c\u63a5)reactor\u7f16\u7a0b-\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\/74003.html","og_locale":"zh_CN","og_type":"article","og_title":"\u7f51\u7edc\u548cLinux\u7f51\u7edc-15(IO\u591a\u8def\u8f6c\u63a5)reactor\u7f16\u7a0b-\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"1. reactor\u7684\u670d\u52a1\u56681.0\u00a0reactor\u670d\u52a1\u5668\u4ecb\u7ecdReactor\u662f\u4e00\u79cd\u4e8b\u4ef6\u9a71\u52a8\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u6a21\u578b&#xff0c;\u6838\u5fc3\u662f\u901a\u8fc7\u5355\u7ebf\u7a0b\/\u5c11\u91cf\u7ebf\u7a0b\u7ba1\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u3002\u5176\u6838\u5fc3\u7ec4\u4ef6\u5305\u62ec&#xff1a;I\/O\u591a\u8def\u590d\u7528\u5668&#xff08;\u5982epoll&#xff09;&#xff1a;\u76d1\u542c\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u7684\u4e8b\u4ef6&#xff08;\u8bfb\u3001\u5199\u3001\u5f02\u5e38&#xff09;\u3002\u4e8b\u4ef6\u5206\u53d1\u5668&#xff1a;\u5c06\u5c31\u7eea\u4e8b\u4ef6\u5206\u53d1\u7ed9\u5bf9\u5e94\u7684\u5904\u7406\u5668\u3002\u4e8b\u4ef6\u5904\u7406\u5668&#xff1a;\u6267\u884c\u5177\u4f53\u7684I\/O\u64cd\u4f5c\u548c","og_url":"https:\/\/www.wsisp.com\/helps\/74003.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-08T14:10:11+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260208141007-6988993f6b1a6.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"17 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/74003.html","url":"https:\/\/www.wsisp.com\/helps\/74003.html","name":"\u7f51\u7edc\u548cLinux\u7f51\u7edc-15(IO\u591a\u8def\u8f6c\u63a5)reactor\u7f16\u7a0b-\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-08T14:10:11+00:00","dateModified":"2026-02-08T14:10:11+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/74003.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/74003.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/74003.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u7f51\u7edc\u548cLinux\u7f51\u7edc-15(IO\u591a\u8def\u8f6c\u63a5)reactor\u7f16\u7a0b-\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\/74003","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=74003"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/74003\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/73995"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=74003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=74003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=74003"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=74003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}