{"id":82783,"date":"2026-07-25T12:33:19","date_gmt":"2026-07-25T04:33:19","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/82783.html"},"modified":"2026-07-25T12:33:19","modified_gmt":"2026-07-25T04:33:19","slug":"%e9%ab%98%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%bf%85%e5%a4%87%ef%bc%9a%e5%8d%87%e5%ba%8f%e9%93%be%e8%a1%a8%e5%ae%9a%e6%97%b6%e5%99%a8%e4%bb%8e%e8%ae%be%e8%ae%a1%e5%88%b0%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/82783.html","title":{"rendered":"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u5fc5\u5907\uff1a\u5347\u5e8f\u94fe\u8868\u5b9a\u65f6\u5668\u4ece\u8bbe\u8ba1\u5230\u5b9e\u73b0\u5168\u6d41\u7a0b"},"content":{"rendered":"<p>\u672c\u6587\u4ece\u96f6\u5f00\u59cb\u8bb2\u89e3 Linux \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u4e2d\u6700\u57fa\u7840\u7684\u5347\u5e8f\u94fe\u8868\u5b9a\u65f6\u5668\u5b9e\u73b0&#xff0c;\u4ece\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u5230\u5b8c\u6574\u53ef\u8fd0\u884c\u4ee3\u7801&#xff0c;\u4e00\u6b65\u6b65\u62c6\u89e3\u5b9a\u65f6\u5668\u7684\u6838\u5fc3\u539f\u7406\u3002\u9002\u5408\u6b63\u5728\u5b66\u4e60 Linux \u7f51\u7edc\u7f16\u7a0b\u3001\u60f3\u8981\u638c\u63e1\u670d\u52a1\u5668\u8d85\u65f6\u8fde\u63a5\u7ba1\u7406\u7684\u540c\u5b66\u3002<\/p>\n<p>\u6700\u7ec8\u5b9e\u73b0\u6548\u679c&#xff1a;\u670d\u52a1\u5668\u81ea\u52a8\u6e05\u7406 15 \u79d2\u65e0\u6570\u636e\u4ea4\u4e92\u7684\u95f2\u7f6e\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u4fdd\u8bc1\u670d\u52a1\u5668\u8d44\u6e90\u4e0d\u88ab\u8017\u5c3d\u3002<\/p>\n<p>\u4ece\u6267\u884c\u6548\u7387\u6765\u770b\u6dfb\u52a0\u5b9a\u65f6\u5668\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)&#xff0c;\u5220\u9664\u5b9a\u65f6\u5668\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)&#xff0c;\u6267\u884c\u5b9a\u65f6\u4efb\u52a1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\u3002<\/p>\n<h3>\u4e00\u3001\u5b9a\u65f6\u5668\u6838\u5fc3\u67b6\u6784\u8bbe\u8ba1<\/h3>\n<p>\u5b9a\u65f6\u5668\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4\u548c\u4e00\u4e2a\u4efb\u52a1\u56de\u8c03\u51fd\u6570&#xff0c;\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u6765\u4e32\u8054\u6240\u6709\u7684\u5b9a\u65f6\u5668\u3002<\/p>\n<h4>1.1 \u6838\u5fc3\u6570\u636e\u7ed3\u6784\u5b9a\u4e49<\/h4>\n<p>\u9996\u5148\u5b9a\u4e49\u5ba2\u6237\u7aef\u4fe1\u606f\u7ed3\u6784\u4f53&#xff0c;\u7528\u6765\u5b58\u50a8 sockfd \u548c\u5b9a\u65f6\u5668\u7b49&#xff1a;<\/p>\n<p>struct client_data<br \/>\n{<br \/>\n    sockaddr_in address;   \/\/\u5ba2\u6237\u7aefsocket\u5730\u5740<br \/>\n    int socket;            \/\/socket\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    char buf[BUFFER_SIZE]; \/\/\u8bfb\u7f13\u5b58<br \/>\n    util_timer *timer;     \/\/\u5b9a\u65f6\u5668&#xff08;\u63d0\u524d\u58f0\u660e&#xff09;<br \/>\n};<\/p>\n<p>\u5b9a\u4e49\u5b9a\u65f6\u5668\u7ed3\u6784\u4f53&#xff0c;\u5305\u542b\u4efb\u52a1\u7684\u8d85\u65f6\u65f6\u95f4&#xff08;\u7edd\u5bf9\u65f6\u95f4&#xff09;\u3001\u51fd\u6570\u6307\u9488\u3001\u5ba2\u6237\u7aef\u6570\u636e\u548c\u524d\u540e\u6307\u9488&#xff1a;<\/p>\n<p>class util_timer<br \/>\n{<br \/>\npublic:<br \/>\n    util_timer() : prev(NULL), next(NULL) {}<\/p>\n<p>public:<br \/>\n    time_t expire; \/\/\u4efb\u52a1\u7684\u8d85\u65f6\u65f6\u95f4&#xff0c;\u7528\u7edd\u5bf9\u65f6\u95f4<br \/>\n    \/\/\u51fd\u6570\u6307\u9488<br \/>\n    void (*cb_func)(client_data *); \/\/\u4efb\u52a1\u56de\u8c03\u51fd\u6570<br \/>\n    \/\/\u56de\u8c03\u51fd\u6570\u5904\u7406\u7684\u5ba2\u6237\u6570\u636e&#xff0c;\u7531\u5b9a\u65f6\u5668\u4f20\u9012\u7ed9\u56de\u8c03\u51fd\u6570<br \/>\n    client_data *user_data;<br \/>\n    util_timer *prev; \/\/\u6307\u5411\u524d\u4e00\u4e2a<br \/>\n    util_timer *next; \/\/\u6307\u5411\u540e\u4e00\u4e2a<br \/>\n};<\/p>\n<h4>1.2 \u51fd\u6570\u6307\u9488\u4e0e\u6307\u9488\u51fd\u6570\u7684\u533a\u522b<\/h4>\n<p>\u8fd9\u91cc\u7684\u56de\u8c03\u51fd\u6570\u662f\u51fd\u6570\u6307\u9488\u7c7b\u578b&#xff0c;\u683c\u5f0f\u4e3a&#xff1a;\u8fd4\u56de\u503c\u7c7b\u578b (* \u6307\u9488\u53d8\u91cf\u540d)(\u53c2\u6570\u5217\u8868);<\/p>\n<p>\u6307\u9488cb_func\u6307\u5411\u8fd4\u56de\u503c\u662fvoid\u3001\u53c2\u6570\u662fclient_data*\u7684\u51fd\u6570\u3002\u88ab\u8d4b\u503c\u51fd\u6570\u540e&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u6307\u9488\u8c03\u7528\u6307\u5411\u7684\u51fd\u6570\u3002\u4e3b\u8981\u662f\u4e3a\u4e86\u5c06\u67b6\u6784\u548c\u4e1a\u52a1\u4ee3\u7801\u89e3\u8026&#xff1a;\u67b6\u6784\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u7edf\u4e00\u4f7f\u7528cb_func&#xff0c;\u4f46\u56de\u8c03\u51fd\u6570\u6307\u5411\u7684\u4e1a\u52a1\u5904\u7406\u51fd\u6570\u53ef\u4ee5\u66f4\u6539&#xff0c;\u4e0d\u7528\u4fee\u6539\u67b6\u6784\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u6ce8\u610f\u548c\u6307\u9488\u51fd\u6570\u533a\u5206&#xff1a;\u6307\u9488\u51fd\u6570\u662f\u51fd\u6570&#xff0c;\u8fd4\u56de\u503c\u662f\u6307\u9488\u7c7b\u578b&#xff0c;\u6bd4\u5982void* handler(int arg);\u662f\u8fd4\u56de\u503cvoid*\u7c7b\u578b\u7684\u51fd\u6570\u3002<\/p>\n<h4>1.3 \u53cc\u5411\u7ed1\u5b9a\u7684\u8bbe\u8ba1\u610f\u4e49<\/h4>\n<p>\u5728client_data\u548cutil_timer\u91cc\u53ef\u4ee5\u53d1\u73b0&#xff0c;\u5b83\u4eec\u4e92\u76f8\u6301\u6709\u5bf9\u65b9\u7684\u6307\u9488&#xff08;\u53cc\u5411\u7ed1\u5b9a&#xff09;&#xff0c;\u662f\u4e3a\u4e86\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u5bf9\u65b9&#xff0c;\u907f\u514d\u904d\u5386\u94fe\u8868&#xff1a;<\/p>\n<ul>\n<li>\u5ba2\u6237\u7aef\u53d1\u6765\u4e86\u6570\u636e\u3001\u5ba2\u6237\u7aef\u4e3b\u52a8\u65ad\u5f00\u8fde\u63a5\u573a\u666f\u4e0b&#xff0c;\u53ef\u4ee5\u901a\u8fc7client_data\u5feb\u901f\u627e\u5230\u5bf9\u5e94\u7684\u5b9a\u65f6\u5668<\/li>\n<li>\u5b9a\u65f6\u5668\u8d85\u65f6&#xff0c;\u9700\u8981\u6267\u884c\u56de\u8c03\u51fd\u6570\u65f6&#xff0c;\u76f4\u63a5\u628a\u81ea\u5df1\u5b58\u50a8\u7684client_data\u4f20\u7ed9cb_func&#xff0c;\u77e5\u9053\u8be5\u5173\u95ed\u54ea\u4e2a\u5ba2\u6237\u7aef<\/li>\n<\/ul>\n<h4>1.4 \u5347\u5e8f\u94fe\u8868\u6838\u5fc3\u64cd\u4f5c<\/h4>\n<p>\u5b9a\u65f6\u5668\u94fe\u8868\u662f\u4e00\u4e2a\u5347\u5e8f\u3001\u53cc\u5411\u94fe\u8868&#xff0c;\u5e26\u6709\u5934\u7ed3\u70b9\u548c\u5c3e\u8282\u70b9&#xff1a;<\/p>\n<p>\/\/\u5b9a\u65f6\u5668\u94fe\u8868&#xff0c;\u662f\u4e00\u4e2a\u5347\u5e8f&#xff0c;\u53cc\u5411\u94fe\u8868&#xff0c;\u5e26\u6709\u5934\u7ed3\u70b9\u548c\u5c3e\u8282\u70b9<br \/>\nclass sort_timer_lst<br \/>\n{<br \/>\npublic:<br \/>\n  sort_timer_lst():head(NULL),tail(NULL){}<br \/>\n  \/\/\u94fe\u8868\u88ab\u9500\u6bc1\u65f6&#xff0c;\u5220\u9664\u91cc\u9762\u6240\u6709\u5b9a\u65f6\u5668<br \/>\n  ~sort_timer_lst()<br \/>\n  {<br \/>\n      util_timer*tmp&#061;head;<br \/>\n      while(tmp)<br \/>\n      {<br \/>\n          head &#061; tmp-&gt;next;<br \/>\n          delete tmp;<br \/>\n          tmp &#061; head;<br \/>\n      }<br \/>\n  }<\/p>\n<p>  \/\/\u5c06\u76ee\u6807\u5b9a\u65f6\u5668timer\u6dfb\u52a0\u5230\u94fe\u8868\u4e2d<br \/>\n  void add_timer(util_timer* timer)<br \/>\n  {<br \/>\n      if(!timer)<br \/>\n      {\/\/timer\u4e3a\u7a7a\u8fd4\u56de<br \/>\n          return;<br \/>\n      }<br \/>\n      if(!head)<br \/>\n      {\/\/\u94fe\u8868\u4e3a\u7a7a<br \/>\n          head &#061; tail &#061; timer;<br \/>\n          return;<br \/>\n      }<\/p>\n<p>      \/\/\u5982\u679ctimer\u7684\u8d85\u65f6\u65f6\u95f4\u5c0f\u4e8e\u94fe\u8868\u4e2d\u7684\u6240\u6709\u5b9a\u65f6\u5668\u7684\u8d85\u65f6\u65f6\u95f4&#xff0c;\u628atimer\u63d2\u5165\u5934\u90e8&#xff0c;\u5426\u5219<br \/>\n      \/\/\u8c03\u7528\u91cd\u6784\u51fd\u6570add_timer(timer,lst_head)\u628a\u5b83\u63d2\u5165\u5408\u9002\u4f4d\u7f6e&#xff0c;\u4fdd\u8bc1\u94fe\u8868\u7684\u5347\u5e8f<br \/>\n      if(timer-&gt;expire&lt;head-&gt;expire)<br \/>\n      {<br \/>\n          timer-&gt;next &#061; head;<br \/>\n          head-&gt;prev&#061; timer;<br \/>\n          head &#061;timer;<br \/>\n          return;<br \/>\n      }<br \/>\n      add_timer(timer,head);<br \/>\n  }<\/p>\n<p>  \/\/\u5f53\u67d0\u4e2a\u5b9a\u65f6\u4efb\u52a1\u53d1\u751f\u53d8\u5316\u65f6&#xff0c;\u8c03\u6574timer\u7684\u4f4d\u7f6e&#xff0c;\u8fd9\u4e2a\u51fd\u6570\u53ea\u8003\u8651timer\u8d85\u65f6\u65f6\u95f4\u5ef6\u957f\u60c5\u51b5&#xff0c;\u5373timer\u53ea\u5f80\u94fe\u8868\u5c3e\u90e8\u79fb\u52a8<br \/>\n  void adjust_timer(util_timer* timer)<br \/>\n  {<br \/>\n      if(!timer) return;\/\/timer\u4e3a\u7a7a<br \/>\n      util_timer*tmp &#061; timer-&gt;next;<br \/>\n      \/\/\u5982\u679ctimer\u5728\u5c3e\u90e8&#xff0c;\u6216timer\u7684\u8d85\u65f6\u503c\u4ecd\u5c0f\u4e8e\u4e0b\u4e00\u4e2atimer\u5219\u4e0d\u7528\u8c03\u6574<br \/>\n      if(!tmp||(timer-&gt;expire&lt;tmp-&gt;expire)) return;<\/p>\n<p>      \/\/\u5982\u679c\u76ee\u6807\u5b9a\u65f6\u5668\u662f\u94fe\u8868\u7684\u5934\u7ed3\u70b9&#xff0c;\u5219\u5c06\u5b9a\u65f6\u5668\u4ece\u94fe\u8868\u53d6\u51fa\u91cd\u65b0\u63d2\u5165\u94fe\u8868<br \/>\n      if(timer&#061;&#061;head)<br \/>\n      {<br \/>\n          head&#061;head-&gt;next;<br \/>\n          head-&gt;prev &#061; NULL;<br \/>\n          timer-&gt;next &#061; NULL;<br \/>\n          add_timer(timer,head);<br \/>\n      }else<br \/>\n      {\/\/\u5982\u679ctimer\u4e0d\u662f\u5934\u7ed3\u70b9&#xff0c;\u4ece\u94fe\u8868\u53d6\u51fa&#xff0c;\u63d2\u5165\u539f\u6765\u4f4d\u7f6e\u7684\u540e\u9762\u90e8\u5206\u94fe\u8868\u4e2d<br \/>\n         timer-&gt;prev-&gt;next &#061; timer-&gt;next;<br \/>\n         timer-&gt;next-&gt;prev &#061; timer-&gt;prev;<br \/>\n         add_timer(timer,timer-&gt;next);<br \/>\n      }<br \/>\n  }<\/p>\n<p>  \/\/\u5c06timer\u4ece\u94fe\u8868\u4e2d\u5220\u9664<br \/>\n  void del_timer(util_timer* timer)<br \/>\n  {<br \/>\n      if(!timer) return;\/\/\u4e3a\u7a7a<br \/>\n      if((timer&#061;&#061;head)&amp;&amp;(timer&#061;&#061;tail))\/\/\u94fe\u8868\u4e2d\u53ea\u6709\u4e00\u4e2a\u5b9a\u65f6\u5668<br \/>\n      {<br \/>\n          delete timer;<br \/>\n          head&#061;NULL;<br \/>\n          tail &#061; NULL;<br \/>\n          return;<br \/>\n      }<\/p>\n<p>      \/\/\u5982\u679c\u94fe\u8868\u4e2d\u6709\u81f3\u5c11\u4e24\u4e2a\u5b9a\u65f6\u5668&#xff0c;\u4e14timer\u662f\u5934\u7ed3\u70b9&#xff0c;\u5219\u5c06\u5934\u7ed3\u70b9\u8bbe\u7f6e\u4e3atimer\u7684\u4e0b\u4e00\u4e2a<br \/>\n      if(timer &#061;&#061; head)<br \/>\n      {<br \/>\n          head &#061; head-&gt;next;<br \/>\n          head-&gt;prev &#061; NULL;<br \/>\n          delete timer;<br \/>\n          return;<br \/>\n      }<br \/>\n      \/\/\u5982\u679c\u94fe\u8868\u6709\u81f3\u5c11\u4e24\u4e2a\u5b9a\u65f6\u5668&#xff0c;\u4e14timer\u662f\u5c3e\u8282\u70b9&#xff0c;\u5219\u5c06\u5c3e\u8282\u70b9\u8bbe\u4e3atimer\u524d\u4e00\u4e2a\u8282\u70b9<br \/>\n      if(timer &#061;&#061; tail)<br \/>\n      {<br \/>\n          tail &#061; tail-&gt;prev;<br \/>\n          tail-&gt;next &#061; NULL;<br \/>\n          delete timer;<br \/>\n          return;<br \/>\n      }<br \/>\n      \/\/\u5982\u679ctimer\u4f4d\u4e8e\u4e2d\u95f4&#xff0c;\u628a\u5b83\u524d\u540e\u7684\u5b9a\u65f6\u5668\u8fde\u8d77\u6765<br \/>\n      timer-&gt;prev-&gt;next &#061; timer-&gt;next;<br \/>\n      timer-&gt;next-&gt;prev &#061; timer-&gt;prev;<br \/>\n      delete timer;<br \/>\n  }<\/p>\n<p>  \/\/SIGALRM\u4fe1\u53f7\u6bcf\u6b21\u88ab\u89e6\u53d1\u5c31\u5728\u5904\u7406\u4fe1\u53f7\u51fd\u6570\u91cc\u6267\u884c\u4e00\u6b21tick\u51fd\u6570&#xff0c;\u5904\u7406\u94fe\u8868\u4e2d\u5230\u671f\u4efb\u52a1<br \/>\n  void tick()<br \/>\n  {<br \/>\n      if(!head) return;<br \/>\n      printf(&#034;timer tick\\\\n&#034;);<br \/>\n      time_t cur &#061; time(NULL);\/\/\u83b7\u5f97\u7cfb\u7edf\u5f53\u524d\u65f6\u95f4<br \/>\n      util_timer*tmp &#061; head;<br \/>\n      \/\/\u4ece\u5934\u8282\u70b9\u5f00\u59cb\u904d\u5386&#xff0c;\u76f4\u5230\u9047\u5230\u4e00\u4e2a\u672a\u5230\u671f\u7684\u5b9a\u65f6\u5668<br \/>\n      while(tmp)<br \/>\n      {<br \/>\n          \/\/\u56e0\u4e3a\u6bcf\u4e2a\u5b9a\u65f6\u5668\u90fd\u662f\u7528\u7edd\u5bf9\u65f6\u95f4\u505a\u503c&#xff0c;\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u6bd4\u8f83timer\u7684\u8d85\u65f6\u503c\u548ccur<br \/>\n          if(cur&lt;tmp-&gt;expire) break;\/\/\u672a\u8d85\u65f6<br \/>\n          \/\/\u8c03\u7528\u5b9a\u65f6\u5668\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u6267\u884c\u5b9a\u65f6\u4efb\u52a1<br \/>\n          tmp-&gt;cb_func(tmp-&gt;user_data);\/\/\u4f20\u5165\u5ba2\u6237\u6570\u636e<br \/>\n          head &#061; tmp-&gt;next;<br \/>\n          if(head)<br \/>\n          {<br \/>\n              head-&gt;prev &#061; NULL;<br \/>\n          }<br \/>\n          delete tmp;<br \/>\n          tmp &#061; head;<br \/>\n      }<br \/>\n  }<\/p>\n<p>  private:<br \/>\n   \/\/\u91cd\u6784\u8f85\u52a9\u51fd\u6570add_timer&#xff0c;\u5c06timer\u6dfb\u52a0\u5230\u5f53\u524d\u4f4d\u7f6e\u7684\u540e\u9762\u90e8\u5206\u94fe\u8868\u4e2d<br \/>\n   void add_timer(util_timer* timer,util_timer*lst_head)<br \/>\n   {<br \/>\n     util_timer* prev&#061;lst_head;<br \/>\n     util_timer* tmp &#061; prev-&gt;next;<br \/>\n     \/\/\u904d\u5386lst_head\u8282\u70b9\u4e4b\u540e\u7684\u90e8\u5206\u94fe\u8868&#xff0c;\u76f4\u5230\u627e\u5230\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4\u5927\u4e8etimer\u7684\u8282\u70b9&#xff0c;\u5c06timer\u63d2\u5165\u8be5\u8282\u70b9\u524d<br \/>\n     while(tmp)<br \/>\n     {<br \/>\n         if(timer-&gt;expire&lt;tmp-&gt;expire)<br \/>\n         {<br \/>\n             prev-&gt;next &#061;timer;<br \/>\n             timer-&gt;next &#061; tmp;<br \/>\n             tmp-&gt;prev &#061; timer;<br \/>\n             timer-&gt;prev &#061; prev;<br \/>\n             break;<br \/>\n         }<br \/>\n         prev &#061; tmp;<br \/>\n         tmp &#061; tmp-&gt;next;<br \/>\n     }<br \/>\n    \/\/\u5982\u679c\u904d\u5386\u5b8clst_head\u4e4b\u540e\u7684\u94fe\u8868&#xff0c;\u4ecd\u7136\u627e\u4e0d\u5230&#xff0c;\u5c31\u628atimer\u4f5c\u4e3a\u5c3e\u8282\u70b9<br \/>\n    if(!tmp)<br \/>\n    {<br \/>\n        prev-&gt;next &#061; timer;<br \/>\n        timer-&gt;prev &#061; prev;<br \/>\n        timer-&gt;next &#061; NULL;<br \/>\n        tail &#061; timer;<br \/>\n    }<br \/>\n   }<br \/>\nprivate:<br \/>\n   util_timer*head;<br \/>\n   util_timer*tail;<br \/>\n};<\/p>\n<p>\u8fd9\u4e2a\u7c7b\u91cc\u5927\u90e8\u5206\u90fd\u662f\u94fe\u8868\u7684\u57fa\u7840\u64cd\u4f5c&#xff0c;\u6311\u4e9b\u5173\u952e\u7684\u6765\u770b\u4e0b&#xff1a;<\/p>\n<ul>\n<li>add_timer&#xff1a;\u904d\u5386\u94fe\u8868&#xff0c;\u5c06\u65b0\u5b9a\u65f6\u5668\u63d2\u5165\u5230\u5408\u9002\u4f4d\u7f6e&#xff0c;\u4fdd\u8bc1\u94fe\u8868\u6309\u8d85\u65f6\u65f6\u95f4\u5347\u5e8f\u6392\u5217<\/li>\n<li>adjust_timer&#xff1a;\u5ba2\u6237\u7aef\u53d1\u6570\u636e\u65f6&#xff0c;\u5ef6\u957f\u8d85\u65f6\u65f6\u95f4&#xff0c;\u5c06\u5b9a\u65f6\u5668\u5f80\u94fe\u8868\u5c3e\u90e8\u79fb\u52a8<\/li>\n<li>del_timer&#xff1a;\u5ba2\u6237\u7aef\u4e0b\u7ebf\u6216\u8d85\u65f6&#xff0c;\u5c06\u5b9a\u65f6\u5668\u4ece\u94fe\u8868\u4e2d\u5220\u9664<\/li>\n<li>tick&#xff1a;SIGALRM \u4fe1\u53f7\u6bcf 5 \u79d2\u89e6\u53d1\u4e00\u6b21&#xff0c;\u626b\u63cf\u94fe\u8868\u5934\u90e8&#xff0c;\u6e05\u7406\u6240\u6709\u5230\u671f\u7684\u5b9a\u65f6\u5668<\/li>\n<\/ul>\n<p>\u5728adjust_timer\u4e2d&#xff0c;\u56e0\u4e3a timer \u5ef6\u65f6\u4e00\u5b9a\u4f1a\u63d2\u5165\u5728\u539f\u6765\u4f4d\u7f6e\u7684\u540e\u9762&#xff0c;\u6240\u4ee5\u8c03\u7528 add_timer \u7684\u91cd\u6784\u51fd\u6570&#xff0c;\u4f20\u5165 timer \u548c\u5f53\u524d\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u670d\u52a1\u5668\u96c6\u6210\u5b9a\u65f6\u5668\u5b8c\u6574\u6d41\u7a0b<\/h3>\n<p>\u901a\u8fc7alarm\u8bbe\u7f6e 5 \u79d2\u89e6\u53d1\u4e00\u6b21 SIGALRM \u4fe1\u53f7&#xff0c;\u8c03\u7528\u4fe1\u53f7\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u901a\u8fc7\u7ba1\u9053\u53d1\u9001\u5bf9\u5e94\u4fe1\u53f7&#xff0c;\u5728epoll_wait\u5904\u963b\u585e\u7b49\u5f85\u68c0\u67e5\u94fe\u8868\u662f\u5426\u6709\u8d85\u65f6 timer&#xff0c;\u5f53\u5ba2\u6237\u7aef\u8d85\u65f6\u6216\u65ad\u5f00\u94fe\u63a5\u7b49\u60c5\u51b5\u5206\u522b\u8c03\u7528\u5bf9\u5e94\u51fd\u6570\u3002<\/p>\n<h4>2.1 \u4fe1\u53f7\u5904\u7406\u4e0e\u7edf\u4e00\u4e8b\u4ef6\u6e90&#xff08;\u6838\u5fc3&#xff09;<\/h4>\n<p>\u5177\u4f53\u5148\u6765\u770b\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u8fd9\u4e00\u5757&#xff1a;<\/p>\n<p>void sig_handler(int sig)<br \/>\n{<br \/>\n    int save_errno &#061; errno;\/\/\u5b58\u50a8\u539f\u6765\u7684errno,\u56e0\u4e3a send \u51fd\u6570\u53ef\u80fd\u4fee\u6539 errno<br \/>\n    int msg &#061;sig;\/\/\u628a\u4fe1\u53f7\u7f16\u53f7\u5b58\u4e3a\u53d8\u91cf<br \/>\n    send(pipefd[1],(char*)&amp;msg,1,0);\/\/\u5f80\u7ba1\u9053\u5199\u7aef\u53d1\u9001\u4fe1\u53f7\u7f16\u7801<br \/>\n    errno&#061;save_errno;\/\/\u6062\u590d errno&#xff0c;\u4fdd\u8bc1\u4e0d\u5f71\u54cd\u4e3b\u7a0b\u5e8f\u7684\u9519\u8bef\u5904\u7406<br \/>\n}<\/p>\n<p>void addsig(int sig)<br \/>\n{<br \/>\n    struct sigaction sa;\/\/\u5b9a\u4e49\u4fe1\u53f7\u914d\u7f6e\u7ed3\u6784\u4f53<br \/>\n    memset(&amp;sa,&#039;\\\\0&#039;,sizeof(sa));\/\/\u6e05\u7a7a\u7ed3\u6784\u4f53\u5185\u5b58&#xff0c;\u907f\u514d\u810f\u6570\u636e<br \/>\n    sa.sa_handler&#061;sig_handler;\/\/\u4fe1\u53f7\u89e6\u53d1\u65f6&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u8c03\u7528 sig_handler<br \/>\n    sa.sa_flags |&#061; SA_RESTART;\/\/\u9632\u6b62\u7a0b\u5e8f\u56e0\u4e3a\u4fe1\u53f7\u4e2d\u65ad\u62a5\u9519&#xff0c;\u88ab\u6253\u65ad\u5c31\u91cd\u542f<br \/>\n    sigfillset(&amp;sa.sa_mask);\/\/\u5904\u7406\u5f53\u524d\u4fe1\u53f7\u65f6&#xff0c;\u5c4f\u853d\u6240\u6709\u5176\u4ed6\u4fe1\u53f7<br \/>\n    assert(sigaction(sig,&amp;sa,NULL)!&#061;-1);\/\/\u5c06\u914d\u7f6e\u5e94\u7528\u5230\u4fe1\u53f7 sig<br \/>\n}<\/p>\n<p>\u5728\u4e3b\u7a0b\u5e8f\u4e2d&#xff0c;\u901a\u8fc7addsig\u51fd\u6570\u6ce8\u518c SIGALRM (\u95f9\u949f\u4fe1\u53f7) \u548c SIGTERM (\u5173\u95ed\u4fe1\u53f7)\u3002<\/p>\n<p>sigaction\u7cfb\u7edf\u8c03\u7528\u7c7b\u4f3c\u4e8esignal&#xff0c;\u4f46\u6bd4signal\u66f4\u5065\u58ee&#xff1a;<\/p>\n<p>#include &lt;signal.h&gt;<br \/>\nint sigaction( int sig, const struct sigaction* act, struct sigaction* oact );<br \/>\n\/\/sig \u53c2\u6570\u6307\u51fa\u8981\u6355\u83b7\u7684\u4fe1\u53f7\u7c7b\u578b<br \/>\n\/\/act \u53c2\u6570\u6307\u5b9a\u65b0\u7684\u4fe1\u53f7\u5904\u7406\u65b9\u5f0f<br \/>\n\/\/oact \u53c2\u6570\u5219\u8f93\u51fa\u4fe1\u53f7\u5148\u524d\u7684\u5904\u7406\u65b9\u5f0f&#xff08;\u5982\u679c\u4e0d\u4e3a NULL \u7684\u8bdd&#xff09;<\/p>\n<p>act \u548c oact \u90fd\u662f sigaction \u7ed3\u6784\u4f53\u7c7b\u578b\u7684\u6307\u9488&#xff0c;sigaction \u7ed3\u6784\u4f53\u63cf\u8ff0\u4e86\u4fe1\u53f7\u5904\u7406\u7684\u7ec6\u8282&#xff1a;<\/p>\n<p>struct sigaction<br \/>\n{<br \/>\n#ifdef __USE_POSIX199309<br \/>\n  union<br \/>\n  {<br \/>\n    _sighandler_t sa_handler;<br \/>\n      \/\/\u56de\u8c03\u51fd\u6570<br \/>\n    void (*sa_sigaction) ( int, siginfo_t*, void* );<br \/>\n  }<br \/>\n  _sigaction_handler;<br \/>\n# define sa_handler    _sigaction_handler.sa_handler<br \/>\n# define sa_sigaction  _sigaction_handler.sa_sigaction<br \/>\n#else<br \/>\n  _sighandler_t sa_handler;<br \/>\n#endif<\/p>\n<p>  _sigset_t sa_mask;<br \/>\n  int sa_flags;<br \/>\n  void (*sa_restorer) (void);<br \/>\n};<br \/>\n \/\/\u8be5\u7ed3\u6784\u4f53\u4e2d\u7684 sa_handler \u6210\u5458\u6307\u5b9a\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002<br \/>\n\/\/sa_mask \u6210\u5458\u8bbe\u7f6e\u8fdb\u7a0b\u7684\u4fe1\u53f7\u63a9\u7801,\u4ee5\u6307\u5b9a\u54ea\u4e9b\u4fe1\u53f7\u4e0d\u80fd\u53d1\u9001\u7ed9\u672c\u8fdb\u7a0b<br \/>\n\/\/sa_mask \u662f\u4fe1\u53f7\u96c6 sigset_t\u7c7b\u578b&#xff0c;\u8be5\u7c7b\u578b\u6307\u5b9a\u4e00\u7ec4\u4fe1\u53f7\u3002<br \/>\n\/\/sa_flags \u6210\u5458\u7528\u4e8e\u8bbe\u7f6e\u7a0b\u5e8f\u6536\u5230\u4fe1\u53f7\u65f6\u7684\u884c\u4e3a<\/p>\n<p>\u5176\u4e2dsa_flags\u53c2\u6570\u5982\u4e0b&#xff0c;\u5728addsig\u51fd\u6570\u4e2dsa_flags\u8bbe\u4e3aSA_RESTART&#xff0c;\u88ab\u4fe1\u53f7\u6253\u65ad\u7684\u7cfb\u7edf\u8c03\u7528 (epoll_wait \u7b49) \u4f1a\u81ea\u52a8\u91cd\u65b0\u542f\u52a8\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"435\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260725043318-6a643c8e55b97.png\" width=\"788\" \/><\/p>\n<p>\u5728sa\u8bbe\u7f6e\u7684\u56de\u8c03\u51fd\u6570sig_handler\u4e2d&#xff0c;\u901a\u8fc7\u7ba1\u9053\u901a\u77e5\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u7528\u7ba1\u9053&#xff1f; \u56e0\u4e3a\u4fe1\u53f7\u662f\u5f02\u6b65\u4e2d\u65ad&#xff0c;\u76f4\u63a5\u5904\u7406\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u4e71\u6570\u636e\u7b49 (\u6bd4\u5982timer_lst.tick()\u4f1a\u4fee\u6539\u94fe\u8868&#xff0c;\u662f\u975e\u53ef\u91cd\u5165\u51fd\u6570)\u3002\u901a\u8fc7\u7ba1\u9053\u5c06\u4e0d\u5b89\u5168\u7684\u5f02\u6b65\u4fe1\u53f7\u53d8\u4e3a\u5b89\u5168\u7684 IO \u4e8b\u4ef6&#xff0c;\u4ea4\u7ed9 epoll \u7edf\u4e00\u7ba1\u7406&#xff0c;\u8fd9\u5c31\u662f\u7edf\u4e00\u4e8b\u4ef6\u6e90\u7684\u6838\u5fc3\u601d\u60f3\u3002<\/p>\n<h4>2.2 \u5b9a\u65f6\u5668\u5fc3\u8df3\u4e0e\u8d85\u65f6\u5904\u7406<\/h4>\n<p>void timer_handler()<br \/>\n{<br \/>\n    \/\/\u5b9a\u65f6\u5904\u7406\u4efb\u52a1&#xff0c;\u5b9e\u9645\u4e0a\u5c31\u662f\u4f7f\u7528tick\u51fd\u6570<br \/>\n    timer_lst.tick();<br \/>\n    \/\/\u56e0\u4e3a\u4e00\u6b21alarm\u8c03\u7528\u53ea\u4f1a\u5f15\u8d77\u4e00\u6b21SIGALRM\u4fe1\u53f7&#xff0c;\u6240\u4ee5\u8981\u91cd\u65b0\u5b9a\u65f6<br \/>\n    alarm(TIMESLOT);<br \/>\n}<\/p>\n<p>\u5f53 epoll_wait \u68c0\u6d4b\u5230\u7ba1\u9053\u7684 SIGALRM \u4fe1\u53f7\u540e&#xff0c;\u4f1a\u6267\u884ctimer_handler\u51fd\u6570\u5904\u7406\u8d85\u65f6\u7684 timer \u5e76\u91cd\u8bbe alarm\u3002<\/p>\n<p>\/\/\u5b9a\u65f6\u5668\u56de\u8c03\u51fd\u6570&#xff0c;\u5b83\u5220\u9664\u975e\u6d3b\u52a8\u94fe\u63a5socket\u4e0a\u7684\u6ce8\u518c\u4e8b\u4ef6&#xff0c;\u5e76\u5173\u95edfd<br \/>\nvoid cb_func(client_data*user_data)<br \/>\n{<br \/>\n    epoll_ctl(epollfd,EPOLL_CTL_DEL,user_data-&gt;socket,0);<br \/>\n    assert(user_data);<br \/>\n    close(user_data-&gt;socket);<br \/>\n    printf(&#034;close fd %d\\\\n&#034;,user_data-&gt;socket);<br \/>\n}<\/p>\n<p>\u5728\u65b0\u5ba2\u6237\u7aef\u94fe\u63a5\u65f6&#xff0c;\u5c06\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\u6ce8\u518c\u5230client_data\u91cc&#xff0c;\u5728 timer \u68c0\u6d4b\u5230\u8d85\u65f6\u540e&#xff0c;\u8c03\u7528\u56de\u8c03\u51fd\u6570&#xff0c;\u5220\u9664 epoll \u6ce8\u518c\u4e8b\u4ef6&#xff0c;\u5173\u95ed fd\u3002<\/p>\n<h4>2.3 \u670d\u52a1\u5668\u521d\u59cb\u5316<\/h4>\n<p>\u5728 main \u51fd\u6570\u4e2d\u521b\u5efa\u5168\u53cc\u5de5\u65e0\u540d\u7ba1\u9053&#xff0c;\u8bbe\u7f6e\u7ba1\u9053\u5199\u7aef\u975e\u963b\u585e\u5e76\u6ce8\u518c\u5230 epoll \u91cc\u3002\u901a\u8fc7addsig\u6355\u83b7 SIGALRM \u548c SIGTERM \u4fe1\u53f7\u3002<\/p>\n<p>\u521b\u5efa\u7528\u6237\u4fe1\u606f\u6570\u7ec4users&#xff0c;\u8bbe\u7f6e alarm \u5b9a\u65f6\u4fe1\u53f7&#xff1a;<\/p>\n<p>  \/\/&#8230;\u5f88\u591a\u4ee3\u7801<br \/>\n  epoll_event events[MAX_EVENT_NUMBER];<br \/>\n  epollfd &#061; epoll_create(5);<br \/>\n   assert(epollfd!&#061;-1);<br \/>\n   addfd(epollfd,listenfd);<\/p>\n<p> \/\/\u521b\u5efa\u5168\u53cc\u5de5\u65e0\u540d\u7ba1\u9053<br \/>\n   ret &#061; socketpair(PF_UNIX,SOCK_STREAM,0,pipefd);<br \/>\n  assert(ret !&#061; -1);<br \/>\n  \/\/\u907f\u514d\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u963b\u585e<br \/>\n  setnonblocking(pipefd[1]);<br \/>\n  addfd(epollfd,pipefd[0]);<\/p>\n<p>  \/\/\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u51fd\u6570<br \/>\n  \/\/\u95f9\u949f\u5b9a\u65f6\u4fe1\u53f7<br \/>\n  addsig(SIGALRM);<br \/>\n  \/\/\u7a0b\u5e8f\u7ec8\u6b62\u4fe1\u53f7<br \/>\n  addsig(SIGTERM);<br \/>\n  bool stop_server &#061; false;<\/p>\n<p>  \/\/\u7528\u6237\u4fe1\u606f\u6570\u7ec4<br \/>\n  client_data* users &#061; new client_data[FD_LIMIT];<br \/>\n  bool timeout &#061; false;<br \/>\n  alarm(TIMESLOT);\/\/\u5b9a\u65f6<\/p>\n<p>\/\/\u5f88\u591a\u4ee3\u7801&#8230;<\/p>\n<h4>2.4 epoll \u4e8b\u4ef6\u5faa\u73af\u4e0e\u5b9a\u65f6\u5668\u8054\u52a8<\/h4>\n<p>epoll \u4f1a\u5904\u7406\u4e09\u7c7b\u6838\u5fc3\u4e8b\u4ef6&#xff0c;\u5206\u522b\u5bf9\u5e94\u5b9a\u65f6\u5668\u7684\u4e0d\u540c\u64cd\u4f5c&#xff1a;<\/p>\n<h5>\u4e8b\u4ef6 1&#xff1a;\u65b0\u7684\u5ba2\u6237\u7aef\u94fe\u63a5<\/h5>\n<p>\u6dfb\u52a0\u5230\u7528\u6237\u4fe1\u606f\u6570\u7ec4users\u91cc&#xff0c;\u521b\u5efa\u5b9a\u65f6\u5668&#xff0c;\u5c06\u8d1f\u8d23\u68c0\u67e5\u8d85\u65f6\u7684\u56de\u8c03\u51fd\u6570\u8d4b\u503c\u7ed9cb_func&#xff0c;\u8bbe\u7f6eexpire\u4e3a\u5f53\u524d\u65f6\u95f4 &#043; 15 \u79d2\u7684\u7edd\u5bf9\u65f6\u95f4\u3002\u5e76\u8fdb\u884c\u53cc\u5411\u7ed1\u5b9a&#xff0c;\u6dfb\u52a0\u5230\u94fe\u8868\u4e2d&#xff1a;<\/p>\n<p>          int sockfd &#061; events[i].data.fd;<br \/>\n          \/\/\u5904\u7406\u65b0\u5230\u7684\u5ba2\u6237\u94fe\u63a5<br \/>\n          if(sockfd &#061;&#061; listenfd)<br \/>\n          {<br \/>\n              struct sockaddr_in client_address;<br \/>\n              socklen_t client_addrlength &#061; sizeof(client_address);<br \/>\n              int connfd &#061; accept(listenfd,(struct sockaddr*)&amp;client_address,&amp;client_addrlength);<\/p>\n<p>              addfd(epollfd,connfd);<br \/>\n              users[connfd].address &#061; client_address;<br \/>\n              users[connfd].socket&#061;connfd;<\/p>\n<p>              \/\/\u521b\u5efa\u5b9a\u65f6\u5668&#xff0c;\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\u548c\u8d85\u65f6\u65f6\u95f4&#xff0c;\u7ed1\u5b9a\u5b9a\u65f6\u5668\u4e0e\u7528\u6237\u6570\u636e&#xff0c;\u6dfb\u52a0\u5230timer\u94fe\u8868\u4e2d<br \/>\n              util_timer* timer &#061; new util_timer;<br \/>\n              timer-&gt;user_data &#061; &amp;users[connfd];<br \/>\n              timer-&gt;cb_func &#061; cb_func;\/\/\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570<br \/>\n              time_t cur &#061; time(NULL);<br \/>\n              \/\/\u8bbe\u7f6e\u5b9a\u65f6\u5668\u7684\u3010\u8d85\u65f6\u65f6\u95f4\u3011&#061; \u5f53\u524d\u65f6\u95f4 &#043; 15\u79d2<br \/>\n              timer-&gt;expire &#061; cur&#043;3*TIMESLOT;<br \/>\n              \/\/\u8ba9\u5ba2\u6237\u7aef\u6570\u636e\u8bb0\u5f55\u81ea\u5df1\u7684\u5b9a\u65f6\u5668<br \/>\n              users[connfd].timer &#061; timer;<br \/>\n              timer_lst.add_timer(timer);<\/p>\n<h5>\u4e8b\u4ef6 2&#xff1a;\u7ba1\u9053\u7684\u8bfb\u4e8b\u4ef6<\/h5>\n<p>\u5c06\u4fe1\u53f7\u8d4b\u7ed9signals\u6570\u7ec4\u63a5\u6536&#xff0c;\u904d\u5386\u6240\u6709\u89e6\u53d1\u7684\u4fe1\u53f7\u3002\u5982\u679c\u662f SIGALRM \u5c31\u6807\u8bb0\u5b9a\u65f6\u4efb\u52a1\u5728\u6700\u540e\u5904\u7406&#xff0c;\u5982\u679c\u662f SIGTERM \u5c31\u4f18\u96c5\u5173\u95ed\u670d\u52a1\u5668&#xff1a;<\/p>\n<p>          }else if((sockfd &#061;&#061; pipefd[0])&amp;&amp;(events[i].events&amp;EPOLLIN))<br \/>\n          {<br \/>\n              int sig;<br \/>\n              char signals[1024];<br \/>\n              ret &#061; recv(pipefd[0],signals,sizeof(signals),0);<br \/>\n              if(ret &#061;&#061; -1)<br \/>\n              {<br \/>\n                  continue;\/\/\u51fa\u9519<br \/>\n              }<br \/>\n              else if(ret &#061;&#061; 0)<br \/>\n              {<br \/>\n                  continue;<br \/>\n              }<br \/>\n              else{<br \/>\n                  for(int i&#061;0;i&lt;ret;i&#043;&#043;)<br \/>\n                  {<br \/>\n                      switch(signals[i])<br \/>\n                      {<br \/>\n                          case SIGALRM:<br \/>\n                          {\/\/\u7528timeout\u6807\u8bb0\u6709\u5b9a\u65f6\u4efb\u52a1&#xff0c;\u56e0\u4e3a\u5b9a\u65f6\u4efb\u52a1\u4f18\u5148\u7ea7\u4e0d\u9ad8&#xff0c;\u4f18\u5148\u5904\u7406\u522b\u7684<br \/>\n                              timeout &#061; true;<br \/>\n                              break;<br \/>\n                          }<br \/>\n                          case SIGTERM:<br \/>\n                          {<br \/>\n                              stop_server &#061; true;<br \/>\n                          }<br \/>\n                      }<br \/>\n                  }<br \/>\n              }<\/p>\n<h5>\u4e8b\u4ef6 3&#xff1a;\u5ba2\u6237\u7aef\u8bfb\u4e8b\u4ef6<\/h5>\n<p>\u5982\u679c\u63a5\u6536\u6570\u636e\u9519\u8bef\u6216\u8005\u5ba2\u6237\u7aef\u4e0b\u7ebf\u5c31\u8c03\u7528cb_func\u56de\u8c03\u51fd\u6570&#xff0c;\u5e76\u79fb\u9664\u5b9a\u65f6\u5668\u3002\u5982\u679c\u6b63\u5e38\u63a5\u6536\u6d88\u606f&#xff0c;\u5c31\u5ef6\u957f\u5bf9\u5e94 timer \u7684expire\u65f6\u95f4\u5e76\u8c03\u7528adjust_timer\u51fd\u6570\u8c03\u6574&#xff1a;<\/p>\n<p>          } else if(events[i].events&amp;EPOLLIN)<br \/>\n           {<br \/>\n                memset(users[sockfd].buf,&#039;\\\\0&#039;,BUFFER_SIZE);<br \/>\n                ret &#061; recv(sockfd,users[sockfd].buf,BUFFER_SIZE-1,0);<br \/>\n                printf(&#034;get %d bytes of client data %s from %d\\\\n&#034;,ret,users[sockfd].buf,sockfd);<br \/>\n                util_timer* timer &#061; users[sockfd].timer;<br \/>\n                if(ret &lt; 0)<br \/>\n                {<br \/>\n                    \/\/\u53d1\u751f\u9519\u8bef&#xff0c;\u5173\u95ed\u8fde\u63a5\u5e76\u79fb\u9664\u5b9a\u65f6\u5668<br \/>\n                    if(errno!&#061;EAGAIN)<br \/>\n                    {<br \/>\n                       cb_func(&amp;users[sockfd]);<br \/>\n                       if(timer){<br \/>\n                           timer_lst.del_timer(timer);<br \/>\n                       }<br \/>\n                    }<br \/>\n                }else if(ret &#061;&#061; 0)<br \/>\n                {\/\/\u5bf9\u7aef\u5173\u95ed\u8fde\u63a5&#xff0c;\u8fd9\u8fb9\u4e5f\u5173\u95ed\u8fde\u63a5&#xff0c;\u5e76\u79fb\u9664\u5bf9\u5e94\u5b9a\u65f6\u5668<br \/>\n                    cb_func(&amp;users[sockfd]);<br \/>\n                    if(timer)<br \/>\n                    {<br \/>\n                        timer_lst.del_timer(timer);<br \/>\n                    }<br \/>\n                }<br \/>\n                else{<br \/>\n                    \/\/\u5982\u679c\u67d0\u4e2a\u5ba2\u6237\u8fde\u63a5\u6709\u6570\u636e\u53ef\u8bfb&#xff0c;\u4e5f\u9700\u8981\u8c03\u6574\u5bf9\u5e94\u7684timer&#xff0c;\u5ef6\u8fdf\u5173\u95ed\u7684\u65f6\u95f4<br \/>\n                    if(timer)<br \/>\n                    {<br \/>\n                        time_t cur &#061; time(NULL);<br \/>\n                        timer-&gt;expire &#061; cur &#043; 3*TIMESLOT;<br \/>\n                        printf(&#034;adjust timer once\\\\n&#034;);<br \/>\n                        timer_lst.adjust_timer(timer);<br \/>\n                    }<br \/>\n                }<\/p>\n<p>           }<\/p>\n<p>\u5728\u6700\u540e\u5904\u7406\u5b9a\u65f6\u4e8b\u4ef6&#xff0c;\u5982\u679ctimeout\u4e3a true&#xff0c;\u5c31\u8c03\u7528timer_handler&#xff0c;\u7528tick\u5904\u7406\u8d85\u65f6 timer&#xff0c;\u5e76\u91cd\u65b0 alarm \u5b9a\u65f6&#xff1a;<\/p>\n<p>        \/\/\u6700\u540e\u5904\u7406\u5b9a\u65f6\u4e8b\u4ef6&#xff0c;\u56e0\u4e3aIO\u4e8b\u4ef6\u4f18\u5148\u7ea7\u66f4\u9ad8<br \/>\n      if(timeout){<br \/>\n          timer_handler();<br \/>\n          timeout &#061; false;<br \/>\n      } <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"460\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260725043318-6a643c8e7ef26.png\" width=\"821\" \/><\/p>\n<h3>\u4e09\u3001\u5b8c\u6574\u53ef\u8fd0\u884c\u4ee3\u7801<\/h3>\n<p>\u7f16\u8bd1\u547d\u4ee4&#xff1a;g&#043;&#043; main.cpp -o server -Wall \u8fd0\u884c\u547d\u4ee4&#xff1a;.\/server ip\u00a0\u7aef\u53e3 \u6d4b\u8bd5\u547d\u4ee4&#xff1a;\u65b0\u5f00\u7ec8\u7aef\u6267\u884c nc ip\u00a0\u7aef\u53e3<\/p>\n<h4>lst_timer.h<\/h4>\n<p>#ifndef LST_TIMER<br \/>\n#define LST_TIMER<\/p>\n<p>#include &lt;time.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;assert.h&gt;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;errno.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;sys\/sendfile.h&gt;<br \/>\n#include&lt;sys\/epoll.h&gt;<br \/>\n#include&lt;pthread.h&gt;<br \/>\n#include &lt;signal.h&gt;<br \/>\n#define BUFFER_SIZE 64<\/p>\n<p>class util_timer; \/\/\u63d0\u524d\u58f0\u660e<\/p>\n<p>struct client_data<br \/>\n{<br \/>\n    sockaddr_in address;   \/\/\u5ba2\u6237\u7aefsocket\u5730\u5740<br \/>\n    int socket;            \/\/socket\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    char buf[BUFFER_SIZE]; \/\/\u8bfb\u7f13\u5b58<br \/>\n    util_timer *timer;     \/\/\u5b9a\u65f6\u5668<br \/>\n};<\/p>\n<p>\/\/ \u5b9a\u65f6\u5668\u7c7b<\/p>\n<p>class util_timer<br \/>\n{<br \/>\npublic:<br \/>\n    util_timer() : prev(NULL), next(NULL) {}<\/p>\n<p>public:<br \/>\n    time_t expire; \/\/\u4efb\u52a1\u7684\u8d85\u65f6\u65f6\u95f4&#xff0c;\u7528\u7edd\u5bf9\u65f6\u95f4<br \/>\n    \/\/\u51fd\u6570\u6307\u9488<br \/>\n    void (*cb_func)(client_data *); \/\/\u4efb\u52a1\u56de\u8c03\u51fd\u6570<br \/>\n    \/\/\u56de\u8c03\u51fd\u6570\u5904\u7406\u7684\u5ba2\u6237\u6570\u636e&#xff0c;\u7531\u5b9a\u65f6\u5668\u4f20\u9012\u7ed9\u56de\u8c03\u51fd\u6570<br \/>\n    client_data *user_data;<br \/>\n    util_timer *prev; \/\/\u6307\u5411\u524d\u4e00\u4e2a<br \/>\n    util_timer *next; \/\/\u6307\u5411\u540e\u4e00\u4e2a<br \/>\n};<\/p>\n<p>\/\/\u5b9a\u65f6\u5668\u94fe\u8868&#xff0c;\u662f\u4e00\u4e2a\u5347\u5e8f&#xff0c;\u53cc\u5411\u94fe\u8868&#xff0c;\u5e26\u6709\u5934\u7ed3\u70b9\u548c\u5c3e\u8282\u70b9<br \/>\nclass sort_timer_lst<br \/>\n{<br \/>\npublic:<br \/>\n  sort_timer_lst():head(NULL),tail(NULL){}<br \/>\n  \/\/\u94fe\u8868\u88ab\u9500\u6bc1\u65f6&#xff0c;\u5220\u9664\u91cc\u9762\u6240\u6709\u5b9a\u65f6\u5668<br \/>\n  ~sort_timer_lst()<br \/>\n  {<br \/>\n      util_timer*tmp&#061;head;<br \/>\n      while(tmp)<br \/>\n      {<br \/>\n          head &#061; tmp-&gt;next;<br \/>\n          delete tmp;<br \/>\n          tmp &#061; head;<br \/>\n      }<br \/>\n  }<\/p>\n<p>  \/\/\u5c06\u76ee\u6807\u5b9a\u65f6\u5668timer\u6dfb\u52a0\u5230\u94fe\u8868\u4e2d<br \/>\n  void add_timer(util_timer* timer)<br \/>\n  {<br \/>\n      if(!timer)<br \/>\n      {\/\/timer\u4e3a\u7a7a\u8fd4\u56de<br \/>\n          return;<br \/>\n      }<br \/>\n      if(!head)<br \/>\n      {\/\/\u94fe\u8868\u4e3a\u7a7a<br \/>\n          head &#061; tail &#061; timer;<br \/>\n          return;<br \/>\n      }<\/p>\n<p>      \/\/\u5982\u679ctimer\u7684\u8d85\u65f6\u65f6\u95f4\u5c0f\u4e8e\u94fe\u8868\u4e2d\u7684\u6240\u6709\u5b9a\u65f6\u5668\u7684\u8d85\u65f6\u65f6\u95f4&#xff0c;\u628atimer\u63d2\u5165\u5934\u90e8&#xff0c;\u5426\u5219<br \/>\n      \/\/\u8c03\u7528\u91cd\u6784\u51fd\u6570add_timer(timer,lst_head)\u628a\u5b83\u63d2\u5165\u5408\u9002\u4f4d\u7f6e&#xff0c;\u4fdd\u8bc1\u94fe\u8868\u7684\u5347\u5e8f<br \/>\n      if(timer-&gt;expire&lt;head-&gt;expire)<br \/>\n      {<br \/>\n          timer-&gt;next &#061; head;<br \/>\n          head-&gt;prev&#061; timer;<br \/>\n          head &#061;timer;<br \/>\n          return;<br \/>\n      }<br \/>\n      add_timer(timer,head);<br \/>\n  }<\/p>\n<p>  \/\/\u5f53\u67d0\u4e2a\u5b9a\u65f6\u4efb\u52a1\u53d1\u751f\u53d8\u5316\u65f6&#xff0c;\u8c03\u6574timer\u7684\u4f4d\u7f6e&#xff0c;\u8fd9\u4e2a\u51fd\u6570\u53ea\u8003\u8651timer\u8d85\u65f6\u65f6\u95f4\u5ef6\u957f\u60c5\u51b5&#xff0c;\u5373timer\u53ea\u5f80\u94fe\u8868\u5c3e\u90e8\u79fb\u52a8<br \/>\n  void adjust_timer(util_timer* timer)<br \/>\n  {<br \/>\n      if(!timer) return;\/\/timer\u4e3a\u7a7a<br \/>\n      util_timer*tmp &#061; timer-&gt;next;<br \/>\n      \/\/\u5982\u679ctimer\u5728\u5c3e\u90e8&#xff0c;\u6216timer\u7684\u8d85\u65f6\u503c\u4ecd\u5c0f\u4e8e\u4e0b\u4e00\u4e2atimer\u5219\u4e0d\u7528\u8c03\u6574<br \/>\n      if(!tmp||(timer-&gt;expire&lt;tmp-&gt;expire)) return;<\/p>\n<p>      \/\/\u5982\u679c\u76ee\u6807\u5b9a\u65f6\u5668\u662f\u94fe\u8868\u7684\u5934\u7ed3\u70b9&#xff0c;\u5219\u5c06\u5b9a\u65f6\u5668\u4ece\u94fe\u8868\u53d6\u51fa\u91cd\u65b0\u63d2\u5165\u94fe\u8868<br \/>\n      if(timer&#061;&#061;head)<br \/>\n      {<br \/>\n          head&#061;head-&gt;next;<br \/>\n          head-&gt;prev &#061; NULL;<br \/>\n          timer-&gt;next &#061; NULL;<br \/>\n          add_timer(timer,head);<br \/>\n      }else<br \/>\n      {\/\/\u5982\u679ctimer\u4e0d\u662f\u5934\u7ed3\u70b9&#xff0c;\u4ece\u94fe\u8868\u53d6\u51fa&#xff0c;\u63d2\u5165\u539f\u6765\u4f4d\u7f6e\u7684\u540e\u9762\u90e8\u5206\u94fe\u8868\u4e2d<br \/>\n         timer-&gt;prev-&gt;next &#061; timer-&gt;next;<br \/>\n         timer-&gt;next-&gt;prev &#061; timer-&gt;prev;<br \/>\n         add_timer(timer,timer-&gt;next);<br \/>\n      }<br \/>\n  }<\/p>\n<p>  \/\/\u5c06timer\u4ece\u94fe\u8868\u4e2d\u5220\u9664<br \/>\n  void del_timer(util_timer* timer)<br \/>\n  {<br \/>\n      if(!timer) return;\/\/\u4e3a\u7a7a<br \/>\n      if((timer&#061;&#061;head)&amp;&amp;(timer&#061;&#061;tail))\/\/\u94fe\u8868\u4e2d\u53ea\u6709\u4e00\u4e2a\u5b9a\u65f6\u5668<br \/>\n      {<br \/>\n          delete timer;<br \/>\n          head&#061;NULL;<br \/>\n          tail &#061; NULL;<br \/>\n          return;<br \/>\n      }<\/p>\n<p>      \/\/\u5982\u679c\u94fe\u8868\u4e2d\u6709\u81f3\u5c11\u4e24\u4e2a\u5b9a\u65f6\u5668&#xff0c;\u4e14timer\u662f\u5934\u7ed3\u70b9&#xff0c;\u5219\u5c06\u5934\u7ed3\u70b9\u8bbe\u7f6e\u4e3atimer\u7684\u4e0b\u4e00\u4e2a<br \/>\n      if(timer &#061;&#061; head)<br \/>\n      {<br \/>\n          head &#061; head-&gt;next;<br \/>\n          head-&gt;prev &#061; NULL;<br \/>\n          delete timer;<br \/>\n          return;<br \/>\n      }<br \/>\n      \/\/\u5982\u679c\u94fe\u8868\u6709\u81f3\u5c11\u4e24\u4e2a\u5b9a\u65f6\u5668&#xff0c;\u4e14timer\u662f\u5c3e\u8282\u70b9&#xff0c;\u5219\u5c06\u5c3e\u8282\u70b9\u8bbe\u4e3atimer\u524d\u4e00\u4e2a\u8282\u70b9<br \/>\n      if(timer &#061;&#061; tail)<br \/>\n      {<br \/>\n          tail &#061; tail-&gt;prev;<br \/>\n          tail-&gt;next &#061; NULL;<br \/>\n          delete timer;<br \/>\n          return;<br \/>\n      }<br \/>\n      \/\/\u5982\u679ctimer\u4f4d\u4e8e\u4e2d\u95f4&#xff0c;\u628a\u5b83\u524d\u540e\u7684\u5b9a\u65f6\u5668\u8fde\u8d77\u6765<br \/>\n      timer-&gt;prev-&gt;next &#061; timer-&gt;next;<br \/>\n      timer-&gt;next-&gt;prev &#061; timer-&gt;prev;<br \/>\n      delete timer;<br \/>\n  }<\/p>\n<p>  \/\/SIGALRM\u4fe1\u53f7\u6bcf\u6b21\u88ab\u89e6\u53d1\u5c31\u5728\u5904\u7406\u4fe1\u53f7\u51fd\u6570\u91cc\u6267\u884c\u4e00\u6b21tick\u51fd\u6570&#xff0c;\u5904\u7406\u94fe\u8868\u4e2d\u5230\u671f\u4efb\u52a1<br \/>\n  void tick()<br \/>\n  {<br \/>\n      if(!head) return;<br \/>\n      printf(&#034;timer tick\\\\n&#034;);<br \/>\n      time_t cur &#061; time(NULL);\/\/\u83b7\u5f97\u7cfb\u7edf\u5f53\u524d\u65f6\u95f4<br \/>\n      util_timer*tmp &#061; head;<br \/>\n      \/\/\u4ece\u5934\u8282\u70b9\u5f00\u59cb\u904d\u5386&#xff0c;\u76f4\u5230\u9047\u5230\u4e00\u4e2a\u672a\u5230\u671f\u7684\u5b9a\u65f6\u5668<br \/>\n      while(tmp)<br \/>\n      {<br \/>\n          \/\/\u56e0\u4e3a\u6bcf\u4e2a\u5b9a\u65f6\u5668\u90fd\u662f\u7528\u7edd\u5bf9\u65f6\u95f4\u505a\u503c&#xff0c;\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u6bd4\u8f83timer\u7684\u8d85\u65f6\u503c\u548ccur<br \/>\n          if(cur&lt;tmp-&gt;expire) break;\/\/\u672a\u8d85\u65f6<br \/>\n          \/\/\u8c03\u7528\u5b9a\u65f6\u5668\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u6267\u884c\u5b9a\u65f6\u4efb\u52a1<br \/>\n          tmp-&gt;cb_func(tmp-&gt;user_data);\/\/\u4f20\u5165\u5ba2\u6237\u6570\u636e<br \/>\n          head &#061; tmp-&gt;next;<br \/>\n          if(head)<br \/>\n          {<br \/>\n              head-&gt;prev &#061; NULL;<br \/>\n          }<br \/>\n          delete tmp;<br \/>\n          tmp &#061; head;<br \/>\n      }<br \/>\n  }<\/p>\n<p>  private:<br \/>\n   \/\/\u91cd\u6784\u8f85\u52a9\u51fd\u6570add_timer&#xff0c;\u5c06timer\u6dfb\u52a0\u5230\u5f53\u524d\u4f4d\u7f6e\u7684\u540e\u9762\u90e8\u5206\u94fe\u8868\u4e2d<br \/>\n   void add_timer(util_timer* timer,util_timer*lst_head)<br \/>\n   {<br \/>\n     util_timer* prev&#061;lst_head;<br \/>\n     util_timer* tmp &#061; prev-&gt;next;<br \/>\n     \/\/\u904d\u5386lst_head\u8282\u70b9\u4e4b\u540e\u7684\u90e8\u5206\u94fe\u8868&#xff0c;\u76f4\u5230\u627e\u5230\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4\u5927\u4e8etimer\u7684\u8282\u70b9&#xff0c;\u5c06timer\u63d2\u5165\u8be5\u8282\u70b9\u524d<br \/>\n     while(tmp)<br \/>\n     {<br \/>\n         if(timer-&gt;expire&lt;tmp-&gt;expire)<br \/>\n         {<br \/>\n             prev-&gt;next &#061;timer;<br \/>\n             timer-&gt;next &#061; tmp;<br \/>\n             tmp-&gt;prev &#061; timer;<br \/>\n             timer-&gt;prev &#061; prev;<br \/>\n             break;<br \/>\n         }<br \/>\n         prev &#061; tmp;<br \/>\n         tmp &#061; tmp-&gt;next;<br \/>\n     }<br \/>\n    \/\/\u5982\u679c\u904d\u5386\u5b8clst_head\u4e4b\u540e\u7684\u94fe\u8868&#xff0c;\u4ecd\u7136\u627e\u4e0d\u5230&#xff0c;\u5c31\u628atimer\u4f5c\u4e3a\u5c3e\u8282\u70b9<br \/>\n    if(!tmp)<br \/>\n    {<br \/>\n        prev-&gt;next &#061; timer;<br \/>\n        timer-&gt;prev &#061; prev;<br \/>\n        timer-&gt;next &#061; NULL;<br \/>\n        tail &#061; timer;<br \/>\n    }<br \/>\n   }<br \/>\nprivate:<br \/>\n   util_timer*head;<br \/>\n   util_timer*tail;<br \/>\n};<\/p>\n<p>#endif<\/p>\n<h4>main.cpp<\/h4>\n<p>#include &#034;lst_timer.h&#034;<\/p>\n<p>using namespace std;<br \/>\n#define FD_LIMIT 65535<br \/>\n#define MAX_EVENT_NUMBER 1024<br \/>\n#define TIMESLOT 5<br \/>\nstatic int pipefd[2];<\/p>\n<p>\/\/\u5229\u7528\u5347\u5e8f\u94fe\u8868\u6765\u7ba1\u7406\u5b9a\u65f6\u5668<br \/>\nstatic sort_timer_lst timer_lst;<br \/>\nstatic int epollfd &#061;0;<\/p>\n<p>int setnonblocking(int fd)<br \/>\n{<br \/>\n    int old_option &#061; fcntl(fd,F_GETFL);<br \/>\n    int new_option &#061; old_option | O_NONBLOCK;<br \/>\n    fcntl(fd,F_SETFL,new_option);<br \/>\n    return old_option;<br \/>\n}<\/p>\n<p>void addfd(int epollfd,int fd)<br \/>\n{<br \/>\n    epoll_event event;<br \/>\n    event.data.fd &#061; fd;<br \/>\n    event.events &#061; EPOLLIN|EPOLLET;<br \/>\n    epoll_ctl(epollfd,EPOLL_CTL_ADD,fd,&amp;event);<br \/>\n    setnonblocking(fd);<br \/>\n}<\/p>\n<p>void sig_handler(int sig)<br \/>\n{<br \/>\n    int save_errno &#061; errno;<br \/>\n    int msg &#061;sig;<br \/>\n    send(pipefd[1],(char*)&amp;msg,1,0);<br \/>\n    errno&#061;save_errno;<br \/>\n}<\/p>\n<p>void addsig(int sig)<br \/>\n{<br \/>\n    struct sigaction sa;<br \/>\n    memset(&amp;sa,&#039;\\\\0&#039;,sizeof(sa));<br \/>\n    sa.sa_handler&#061;sig_handler;<br \/>\n    sa.sa_flags |&#061; SA_RESTART;<br \/>\n    sigfillset(&amp;sa.sa_mask);<br \/>\n    assert(sigaction(sig,&amp;sa,NULL)!&#061;-1);<br \/>\n}<\/p>\n<p>void timer_handler()<br \/>\n{<br \/>\n    \/\/\u5b9a\u65f6\u5904\u7406\u4efb\u52a1&#xff0c;\u5b9e\u9645\u4e0a\u5c31\u662f\u4f7f\u7528tick\u51fd\u6570<br \/>\n    timer_lst.tick();<br \/>\n    \/\/\u56e0\u4e3a\u4e00\u6b21alarm\u8c03\u7528\u53ea\u4f1a\u5f15\u8d77\u4e00\u6b21SIGALRM\u4fe1\u53f7&#xff0c;\u6240\u4ee5\u8981\u91cd\u65b0\u5b9a\u65f6<br \/>\n    alarm(TIMESLOT);<br \/>\n}<\/p>\n<p>\/\/\u5b9a\u65f6\u5668\u56de\u8c03\u51fd\u6570&#xff0c;\u5b83\u5220\u9664\u975e\u6d3b\u52a8\u94fe\u63a5socket\u4e0a\u7684\u6ce8\u518c\u4e8b\u4ef6&#xff0c;\u5e76\u5173\u95edfd<br \/>\nvoid cb_func(client_data*user_data)<br \/>\n{<br \/>\n    epoll_ctl(epollfd,EPOLL_CTL_DEL,user_data-&gt;socket,0);<br \/>\n    assert(user_data);<br \/>\n    close(user_data-&gt;socket);<br \/>\n    printf(&#034;close fd %d\\\\n&#034;,user_data-&gt;socket);<br \/>\n}<\/p>\n<p>int main(int argc,char*argv[])<br \/>\n{<br \/>\n   if(argc&lt;&#061;2)<br \/>\n   {<br \/>\n       printf(&#034;argc error\\\\n&#034;);<br \/>\n       return -1;<br \/>\n   }<br \/>\n   const char*ip &#061; argv[1];<br \/>\n   int port &#061;atoi(argv[2]);<\/p>\n<p>   int ret &#061;0;<br \/>\n   struct sockaddr_in address;<br \/>\n   bzero(&amp;address,sizeof(address));<br \/>\n   address.sin_family &#061; AF_INET;<br \/>\n   inet_pton(AF_INET,ip,&amp;address.sin_addr);<br \/>\n   address.sin_port&#061;htons(port);<\/p>\n<p>   int listenfd &#061; socket(PF_INET,SOCK_STREAM,0);<br \/>\n   assert(listenfd&gt;&#061;0);<\/p>\n<p>   ret &#061; bind(listenfd,(struct sockaddr*)&amp;address,sizeof(address));<br \/>\n   assert(ret !&#061; -1);<\/p>\n<p>   ret &#061;listen(listenfd,5);<br \/>\n   assert(ret !&#061; -1);<\/p>\n<p>   epoll_event events[MAX_EVENT_NUMBER];<br \/>\n   epollfd &#061; epoll_create(5);<br \/>\n   assert(epollfd!&#061;-1);<br \/>\n   addfd(epollfd,listenfd);<\/p>\n<p> \/\/\u521b\u5efa\u5168\u53cc\u5de5\u65e0\u540d\u7ba1\u9053<br \/>\n   ret &#061; socketpair(PF_UNIX,SOCK_STREAM,0,pipefd);<br \/>\n  assert(ret !&#061; -1);<br \/>\n  \/\/\u907f\u514d\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u963b\u585e<br \/>\n  setnonblocking(pipefd[1]);<br \/>\n  addfd(epollfd,pipefd[0]);<\/p>\n<p>  \/\/\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u51fd\u6570<br \/>\n  \/\/\u95f9\u949f\u5b9a\u65f6\u4fe1\u53f7<br \/>\n  addsig(SIGALRM);<br \/>\n  \/\/\u7a0b\u5e8f\u7ec8\u6b62\u4fe1\u53f7<br \/>\n  addsig(SIGTERM);<br \/>\n  bool stop_server &#061; false;<\/p>\n<p>  \/\/\u7528\u6237\u4fe1\u606f\u6570\u7ec4<br \/>\n  client_data* users &#061; new client_data[FD_LIMIT];<br \/>\n  bool timeout &#061; false;<br \/>\n  alarm(TIMESLOT);\/\/\u5b9a\u65f6<\/p>\n<p>  while(!stop_server)<br \/>\n  {<br \/>\n      int number &#061; epoll_wait(epollfd,events,MAX_EVENT_NUMBER,-1);<br \/>\n      if((number&lt;0)&amp;&amp;(errno!&#061;EINTR))<br \/>\n      {<br \/>\n          printf(&#034;epoll failture\\\\n&#034;);<br \/>\n          break;<br \/>\n      }<\/p>\n<p>      for(int i&#061;0;i&lt;number;i&#043;&#043;)<br \/>\n      {<br \/>\n          int sockfd &#061; events[i].data.fd;<br \/>\n          \/\/\u5904\u7406\u65b0\u5230\u7684\u5ba2\u6237\u94fe\u63a5<br \/>\n          if(sockfd &#061;&#061; listenfd)<br \/>\n          {<br \/>\n              struct sockaddr_in client_address;<br \/>\n              socklen_t client_addrlength &#061; sizeof(client_address);<br \/>\n              int connfd &#061; accept(listenfd,(struct sockaddr*)&amp;client_address,&amp;client_addrlength);<\/p>\n<p>              addfd(epollfd,connfd);<br \/>\n              users[connfd].address &#061; client_address;<br \/>\n              users[connfd].socket&#061;connfd;<\/p>\n<p>              \/\/\u521b\u5efa\u5b9a\u65f6\u5668&#xff0c;\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\u548c\u8d85\u65f6\u65f6\u95f4&#xff0c;\u7ed1\u5b9a\u5b9a\u65f6\u5668\u4e0e\u7528\u6237\u6570\u636e&#xff0c;\u6dfb\u52a0\u5230timer\u94fe\u8868\u4e2d<br \/>\n              util_timer* timer &#061; new util_timer;<br \/>\n              timer-&gt;user_data &#061; &amp;users[connfd];<br \/>\n              timer-&gt;cb_func &#061; cb_func;\/\/\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570<br \/>\n              time_t cur &#061; time(NULL);<br \/>\n              \/\/\u8bbe\u7f6e\u5b9a\u65f6\u5668\u7684\u3010\u8d85\u65f6\u65f6\u95f4\u3011&#061; \u5f53\u524d\u65f6\u95f4 &#043; 15\u79d2<br \/>\n              timer-&gt;expire &#061; cur&#043;3*TIMESLOT;<br \/>\n              \/\/\u8ba9\u5ba2\u6237\u7aef\u6570\u636e\u8bb0\u5f55\u81ea\u5df1\u7684\u5b9a\u65f6\u5668<br \/>\n              users[connfd].timer &#061; timer;<br \/>\n              timer_lst.add_timer(timer);<br \/>\n          }else if((sockfd &#061;&#061; pipefd[0])&amp;&amp;(events[i].events&amp;EPOLLIN))<br \/>\n          {<br \/>\n              int sig;<br \/>\n              char signals[1024];<br \/>\n              ret &#061; recv(pipefd[0],signals,sizeof(signals),0);<br \/>\n              if(ret &#061;&#061; -1)<br \/>\n              {<br \/>\n                  continue;\/\/\u51fa\u9519<br \/>\n              }<br \/>\n              else if(ret &#061;&#061; 0)<br \/>\n              {<br \/>\n                  continue;<br \/>\n              }<br \/>\n              else{<br \/>\n                  for(int i&#061;0;i&lt;ret;i&#043;&#043;)<br \/>\n                  {<br \/>\n                      switch(signals[i])<br \/>\n                      {<br \/>\n                          case SIGALRM:<br \/>\n                          {\/\/\u7528timeout\u6807\u8bb0\u6709\u5b9a\u65f6\u4efb\u52a1&#xff0c;\u56e0\u4e3a\u5b9a\u65f6\u4efb\u52a1\u4f18\u5148\u7ea7\u4e0d\u9ad8&#xff0c;\u4f18\u5148\u5904\u7406\u522b\u7684<br \/>\n                              timeout &#061; true;<br \/>\n                              break;<br \/>\n                          }<br \/>\n                          case SIGTERM:<br \/>\n                          {<br \/>\n                              stop_server &#061; true;<br \/>\n                          }<br \/>\n                      }<br \/>\n                  }<br \/>\n              }<\/p>\n<p>          } else if(events[i].events&amp;EPOLLIN)<br \/>\n           {<br \/>\n                memset(users[sockfd].buf,&#039;\\\\0&#039;,BUFFER_SIZE);<br \/>\n                ret &#061; recv(sockfd,users[sockfd].buf,BUFFER_SIZE-1,0);<br \/>\n                printf(&#034;get %d bytes of client data %s from %d\\\\n&#034;,ret,users[sockfd].buf,sockfd);<br \/>\n                util_timer* timer &#061; users[sockfd].timer;<br \/>\n                if(ret &lt; 0)<br \/>\n                {<br \/>\n                    \/\/\u53d1\u751f\u9519\u8bef&#xff0c;\u5173\u95ed\u8fde\u63a5\u5e76\u79fb\u9664\u5b9a\u65f6\u5668<br \/>\n                    if(errno!&#061;EAGAIN)<br \/>\n                    {<br \/>\n                       cb_func(&amp;users[sockfd]);<br \/>\n                       if(timer){<br \/>\n                           timer_lst.del_timer(timer);<br \/>\n                       }<br \/>\n                    }<br \/>\n                }else if(ret &#061;&#061; 0)<br \/>\n                {\/\/\u5bf9\u7aef\u5173\u95ed\u8fde\u63a5&#xff0c;\u8fd9\u8fb9\u4e5f\u5173\u95ed\u8fde\u63a5&#xff0c;\u5e76\u79fb\u9664\u5bf9\u5e94\u5b9a\u65f6\u5668<br \/>\n                    cb_func(&amp;users[sockfd]);<br \/>\n                    if(timer)<br \/>\n                    {<br \/>\n                        timer_lst.del_timer(timer);<br \/>\n                    }<br \/>\n                }<br \/>\n                else{<br \/>\n                    \/\/\u5982\u679c\u67d0\u4e2a\u5ba2\u6237\u8fde\u63a5\u6709\u6570\u636e\u53ef\u8bfb&#xff0c;\u4e5f\u9700\u8981\u8c03\u6574\u5bf9\u5e94\u7684timer&#xff0c;\u5ef6\u8fdf\u5173\u95ed\u7684\u65f6\u95f4<br \/>\n                    if(timer)<br \/>\n                    {<br \/>\n                        time_t cur &#061; time(NULL);<br \/>\n                        timer-&gt;expire &#061; cur &#043; 3*TIMESLOT;<br \/>\n                        printf(&#034;adjust timer once\\\\n&#034;);<br \/>\n                        timer_lst.adjust_timer(timer);<br \/>\n                    }<br \/>\n                }<\/p>\n<p>           }<br \/>\n           else<br \/>\n           {<br \/>\n               \/\/other<br \/>\n           }<br \/>\n      }<br \/>\n        \/\/\u6700\u540e\u5904\u7406\u5b9a\u65f6\u4e8b\u4ef6&#xff0c;\u56e0\u4e3aIO\u4e8b\u4ef6\u4f18\u5148\u7ea7\u66f4\u9ad8<br \/>\n      if(timeout){<br \/>\n          timer_handler();<br \/>\n          timeout &#061; false;<br \/>\n      }<br \/>\n  }<br \/>\n  close(listenfd);<br \/>\n  close(pipefd[1]);<br \/>\n  close(pipefd[0]);<br \/>\n  delete [] users;<br \/>\n  return 0;<br \/>\n}<\/p>\n<p>(\u539f\u7b14\u8bb0&#xff1a;GameServer-Learning\/00-Notes\/Network\/linux-high-performance at main \u00b7 maomianbaobumoyu\/GameServer-Learning)<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ece\u96f6\u5f00\u59cb\u8bb2\u89e3 Linux \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u4e2d\u6700\u57fa\u7840\u7684\u5347\u5e8f\u94fe\u8868\u5b9a\u65f6\u5668\u5b9e\u73b0&#xff0c;\u4ece\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u5230\u5b8c\u6574\u53ef\u8fd0\u884c\u4ee3\u7801&#xff0c;\u4e00\u6b65\u6b65\u62c6\u89e3\u5b9a\u65f6\u5668\u7684\u6838\u5fc3\u539f\u7406\u3002\u9002\u5408\u6b63\u5728\u5b66\u4e60 Linux \u7f51\u7edc\u7f16\u7a0b\u3001\u60f3\u8981\u638c\u63e1\u670d\u52a1\u5668\u8d85\u65f6\u8fde\u63a5\u7ba1\u7406\u7684\u540c\u5b66\u3002<br \/>\n\u6700\u7ec8\u5b9e\u73b0\u6548\u679c&#xff1a;\u670d\u52a1\u5668\u81ea\u52a8\u6e05\u7406 15 \u79d2\u65e0\u6570\u636e\u4ea4\u4e92\u7684\u95f2\u7f6e\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u4fdd\u8bc1\u670d\u52a1\u5668\u8d44\u6e90\u4e0d\u88ab\u8017\u5c3d\u3002<br \/>\n\u4ece\u6267\u884c\u6548\u7387\u6765\u770b\u6dfb\u52a0\u5b9a\u65f6\u5668\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)&#xff0c;\u5220\u9664\u5b9a\u65f6\u5668\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)&#xff0c;\u6267\u884c\u5b9a\u65f6\u4efb\u52a1\u7684\u65f6<\/p>\n","protected":false},"author":2,"featured_media":82781,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9137,55,43,78,44,2640],"topic":[],"class_list":["post-82783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-atomgit","tag-c","tag-43","tag-78","tag-44","tag-2640"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u5fc5\u5907\uff1a\u5347\u5e8f\u94fe\u8868\u5b9a\u65f6\u5668\u4ece\u8bbe\u8ba1\u5230\u5b9e\u73b0\u5168\u6d41\u7a0b - \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\/82783.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u5fc5\u5907\uff1a\u5347\u5e8f\u94fe\u8868\u5b9a\u65f6\u5668\u4ece\u8bbe\u8ba1\u5230\u5b9e\u73b0\u5168\u6d41\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u672c\u6587\u4ece\u96f6\u5f00\u59cb\u8bb2\u89e3 Linux \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u4e2d\u6700\u57fa\u7840\u7684\u5347\u5e8f\u94fe\u8868\u5b9a\u65f6\u5668\u5b9e\u73b0&#xff0c;\u4ece\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u5230\u5b8c\u6574\u53ef\u8fd0\u884c\u4ee3\u7801&#xff0c;\u4e00\u6b65\u6b65\u62c6\u89e3\u5b9a\u65f6\u5668\u7684\u6838\u5fc3\u539f\u7406\u3002\u9002\u5408\u6b63\u5728\u5b66\u4e60 Linux \u7f51\u7edc\u7f16\u7a0b\u3001\u60f3\u8981\u638c\u63e1\u670d\u52a1\u5668\u8d85\u65f6\u8fde\u63a5\u7ba1\u7406\u7684\u540c\u5b66\u3002 \u6700\u7ec8\u5b9e\u73b0\u6548\u679c&#xff1a;\u670d\u52a1\u5668\u81ea\u52a8\u6e05\u7406 15 \u79d2\u65e0\u6570\u636e\u4ea4\u4e92\u7684\u95f2\u7f6e\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u4fdd\u8bc1\u670d\u52a1\u5668\u8d44\u6e90\u4e0d\u88ab\u8017\u5c3d\u3002 \u4ece\u6267\u884c\u6548\u7387\u6765\u770b\u6dfb\u52a0\u5b9a\u65f6\u5668\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)&#xff0c;\u5220\u9664\u5b9a\u65f6\u5668\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)&#xff0c;\u6267\u884c\u5b9a\u65f6\u4efb\u52a1\u7684\u65f6\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/82783.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-25T04:33:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260725043318-6a643c8e55b97.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=\"13 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/82783.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/82783.html\",\"name\":\"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u5fc5\u5907\uff1a\u5347\u5e8f\u94fe\u8868\u5b9a\u65f6\u5668\u4ece\u8bbe\u8ba1\u5230\u5b9e\u73b0\u5168\u6d41\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-25T04:33:19+00:00\",\"dateModified\":\"2026-07-25T04:33:19+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/82783.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/82783.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/82783.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u5fc5\u5907\uff1a\u5347\u5e8f\u94fe\u8868\u5b9a\u65f6\u5668\u4ece\u8bbe\u8ba1\u5230\u5b9e\u73b0\u5168\u6d41\u7a0b\"}]},{\"@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":"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u5fc5\u5907\uff1a\u5347\u5e8f\u94fe\u8868\u5b9a\u65f6\u5668\u4ece\u8bbe\u8ba1\u5230\u5b9e\u73b0\u5168\u6d41\u7a0b - \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\/82783.html","og_locale":"zh_CN","og_type":"article","og_title":"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u5fc5\u5907\uff1a\u5347\u5e8f\u94fe\u8868\u5b9a\u65f6\u5668\u4ece\u8bbe\u8ba1\u5230\u5b9e\u73b0\u5168\u6d41\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u672c\u6587\u4ece\u96f6\u5f00\u59cb\u8bb2\u89e3 Linux \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u4e2d\u6700\u57fa\u7840\u7684\u5347\u5e8f\u94fe\u8868\u5b9a\u65f6\u5668\u5b9e\u73b0&#xff0c;\u4ece\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u5230\u5b8c\u6574\u53ef\u8fd0\u884c\u4ee3\u7801&#xff0c;\u4e00\u6b65\u6b65\u62c6\u89e3\u5b9a\u65f6\u5668\u7684\u6838\u5fc3\u539f\u7406\u3002\u9002\u5408\u6b63\u5728\u5b66\u4e60 Linux \u7f51\u7edc\u7f16\u7a0b\u3001\u60f3\u8981\u638c\u63e1\u670d\u52a1\u5668\u8d85\u65f6\u8fde\u63a5\u7ba1\u7406\u7684\u540c\u5b66\u3002 \u6700\u7ec8\u5b9e\u73b0\u6548\u679c&#xff1a;\u670d\u52a1\u5668\u81ea\u52a8\u6e05\u7406 15 \u79d2\u65e0\u6570\u636e\u4ea4\u4e92\u7684\u95f2\u7f6e\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u4fdd\u8bc1\u670d\u52a1\u5668\u8d44\u6e90\u4e0d\u88ab\u8017\u5c3d\u3002 \u4ece\u6267\u884c\u6548\u7387\u6765\u770b\u6dfb\u52a0\u5b9a\u65f6\u5668\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)&#xff0c;\u5220\u9664\u5b9a\u65f6\u5668\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)&#xff0c;\u6267\u884c\u5b9a\u65f6\u4efb\u52a1\u7684\u65f6","og_url":"https:\/\/www.wsisp.com\/helps\/82783.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-25T04:33:19+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260725043318-6a643c8e55b97.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"13 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/82783.html","url":"https:\/\/www.wsisp.com\/helps\/82783.html","name":"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u5fc5\u5907\uff1a\u5347\u5e8f\u94fe\u8868\u5b9a\u65f6\u5668\u4ece\u8bbe\u8ba1\u5230\u5b9e\u73b0\u5168\u6d41\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-25T04:33:19+00:00","dateModified":"2026-07-25T04:33:19+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/82783.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/82783.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/82783.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u5fc5\u5907\uff1a\u5347\u5e8f\u94fe\u8868\u5b9a\u65f6\u5668\u4ece\u8bbe\u8ba1\u5230\u5b9e\u73b0\u5168\u6d41\u7a0b"}]},{"@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\/82783","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=82783"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/82783\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/82781"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=82783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=82783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=82783"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=82783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}