{"id":4668,"date":"2020-10-23T15:57:22","date_gmt":"2020-10-23T07:57:22","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=4668"},"modified":"2020-10-23T15:57:22","modified_gmt":"2020-10-23T07:57:22","slug":"%e5%bf%ab%e9%80%9f%e5%8f%af%e9%9d%a0%e7%bd%91%e7%bb%9c%e4%bc%a0%e8%be%93%e5%8d%8f%e8%ae%ae-kcp","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/4668","title":{"rendered":"\u5feb\u901f\u53ef\u9760\u7f51\u7edc\u4f20\u8f93\u534f\u8bae KCP"},"content":{"rendered":"<div class=\"postTitle\">\n<p><a style=\"font-size: 16px;\" href=\"https:\/\/www.cnblogs.com\/wangbin\/p\/5708074.html\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div id=\"cnblogs_post_body\" class=\"blogpost-body\">\n<p>KCP \u662f\u4e00\u4e2a\u5feb\u901f\u53ef\u9760\u534f\u8bae\uff0c\u80fd\u4ee5\u6bd4 TCP\u6d6a\u8d3910%-20%\u7684\u5e26\u5bbd\u7684\u4ee3\u4ef7\uff0c\u6362\u53d6\u5e73\u5747\u5ef6\u8fdf\u964d\u4f4e30%-40%\uff0c\u4e14\u6700\u5927\u5ef6\u8fdf\u964d\u4f4e\u4e09\u500d\u7684\u4f20\u8f93\u6548\u679c\u3002\u7eaf\u7b97\u6cd5\u5b9e\u73b0\uff0c\u5e76\u4e0d\u8d1f\u8d23\u5e95\u5c42\u534f\u8bae\uff08\u5982UDP\uff09\u7684\u6536\u53d1\uff0c\u9700\u8981\u4f7f\u7528\u8005\u81ea\u5df1\u5b9a\u4e49\u4e0b\u5c42\u6570\u636e\u5305\u7684\u53d1\u9001\u65b9\u5f0f\uff0c\u4ee5 callback\u7684\u65b9\u5f0f\u63d0\u4f9b\u7ed9 KCP\u3002\u8fde\u65f6\u949f\u90fd\u9700\u8981\u5916\u90e8\u4f20\u9012\u8fdb\u6765\uff0c\u5185\u90e8\u4e0d\u4f1a\u6709\u4efb\u4f55\u4e00\u6b21\u7cfb\u7edf\u8c03\u7528\u3002<\/p>\n<p>\u6574\u4e2a\u534f\u8bae\u53ea\u6709 ikcp.h, ikcp.c\u4e24\u4e2a\u6e90\u6587\u4ef6\uff0c\u53ef\u4ee5\u65b9\u4fbf\u7684\u96c6\u6210\u5230\u7528\u6237\u81ea\u5df1\u7684\u534f\u8bae\u6808\u4e2d\u3002\u4e5f\u8bb8\u4f60\u5b9e\u73b0\u4e86\u4e00\u4e2aP2P\uff0c\u6216\u8005\u67d0\u4e2a\u57fa\u4e8e UDP\u7684\u534f\u8bae\uff0c\u800c\u7f3a\u4e4f\u4e00\u5957\u5b8c\u5584\u7684ARQ\u53ef\u9760\u534f\u8bae\u5b9e\u73b0\uff0c\u90a3\u4e48\u7b80\u5355\u7684\u62f7\u8d1d\u8fd9\u4e24\u4e2a\u6587\u4ef6\u5230\u73b0\u6709\u9879\u76ee\u4e2d\uff0c\u7a0d\u5fae\u7f16\u5199\u4e24\u884c\u4ee3\u7801\uff0c\u5373\u53ef\u4f7f\u7528\u3002<a href=\"http:\/\/www.mobanhou.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">HTTP:\/\/www.mobanhou.COM<\/a>;;;;;;;;;;<a href=\"https:\/\/github.com\/skywind3000\/kcp\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/skywind3000\/kcp<\/a><\/p>\n<h1><a name=\"t0\"><\/a><\/h1>\n<h1><a name=\"t1\"><\/a>\u6280\u672f\u7279\u6027<\/h1>\n<p>TCP\u662f\u4e3a\u6d41\u91cf\u8bbe\u8ba1\u7684\uff08\u6bcf\u79d2\u5185\u53ef\u4ee5\u4f20\u8f93\u591a\u5c11KB\u7684\u6570\u636e\uff09\uff0c\u8bb2\u7a76\u7684\u662f\u5145\u5206\u5229\u7528\u5e26\u5bbd\u3002\u800cKCP\u662f\u4e3a\u6d41\u901f\u8bbe\u8ba1\u7684\uff08\u5355\u4e2a\u6570\u636e\u5305\u4ece\u4e00\u7aef\u53d1\u9001\u5230\u4e00\u7aef\u9700\u8981\u591a\u5c11\u65f6\u95f4\uff09\uff0c\u4ee510%-20%\u5e26\u5bbd\u6d6a\u8d39\u7684\u4ee3\u4ef7\u6362\u53d6\u4e86\u6bd4 TCP\u5feb30%-40%\u7684\u4f20\u8f93\u901f\u5ea6\u3002TCP\u4fe1\u9053\u662f\u4e00\u6761\u6d41\u901f\u5f88\u6162\uff0c\u4f46\u6bcf\u79d2\u6d41\u91cf\u5f88\u5927\u7684\u5927\u8fd0\u6cb3\uff0c\u800cKCP\u662f\u6c34\u6d41\u6e4d\u6025\u7684\u5c0f\u6fc0\u6d41\u3002KCP\u6709\u6b63\u5e38\u6a21\u5f0f\u548c\u5feb\u901f\u6a21\u5f0f\u4e24\u79cd\uff0c\u901a\u8fc7\u4ee5\u4e0b\u7b56\u7565\u8fbe\u5230\u63d0\u9ad8\u6d41\u901f\u7684\u7ed3\u679c\uff1a<\/p>\n<h4><a name=\"t2\"><\/a>RTO\u7ffb\u500dvs\u4e0d\u7ffb\u500d\uff1a<\/h4>\n<p>TCP\u8d85\u65f6\u8ba1\u7b97\u662fRTOx2\uff0c\u8fd9\u6837\u8fde\u7eed\u4e22\u4e09\u6b21\u5305\u5c31\u53d8\u6210RTOx8\u4e86\uff0c\u5341\u5206\u6050\u6016\uff0c\u800cKCP\u542f\u52a8\u5feb\u901f \u00a0 \u6a21\u5f0f\u540e\u4e0dx2\uff0c\u53ea\u662fx1.5\uff08\u5b9e\u9a8c\u8bc1\u660e1.5\u8fd9\u4e2a\u503c\u76f8\u5bf9\u6bd4\u8f83\u597d\uff09\uff0c\u63d0\u9ad8\u4e86\u4f20\u8f93\u901f\u5ea6\u3002<\/p>\n<h4><a name=\"t3\"><\/a><\/h4>\n<h4><a name=\"t4\"><\/a>\u9009\u62e9\u6027\u91cd\u4f20 vs \u5168\u90e8\u91cd\u4f20\uff1a<\/h4>\n<p>TCP\u4e22\u5305\u65f6\u4f1a\u5168\u90e8\u91cd\u4f20\u4ece\u4e22\u7684\u90a3\u4e2a\u5305\u5f00\u59cb\u4ee5\u540e\u7684\u6570\u636e\uff0cKCP\u662f\u9009\u62e9\u6027\u91cd\u4f20\uff0c\u53ea\u91cd\u4f20\u771f\u6b63 \u00a0 \u4e22\u5931\u7684\u6570\u636e\u5305\u3002<\/p>\n<h4><a name=\"t5\"><\/a><\/h4>\n<h4><a name=\"t6\"><\/a>\u5feb\u901f\u91cd\u4f20\uff1a<\/h4>\n<p>\u53d1\u9001\u7aef\u53d1\u9001\u4e861,2,3,4,5\u51e0\u4e2a\u5305\uff0c\u7136\u540e\u6536\u5230\u8fdc\u7aef\u7684ACK: 1, 3, 4, 5\uff0c\u5f53\u6536\u5230ACK3\u65f6\uff0c \u00a0 KCP\u77e5\u90532\u88ab\u8df3\u8fc71\u6b21\uff0c\u6536\u5230ACK4\u65f6\uff0c\u77e5\u90532\u88ab\u8df3\u8fc7\u4e862\u6b21\uff0c\u6b64\u65f6\u53ef\u4ee5\u8ba4\u4e3a2\u53f7\u4e22\u5931\uff0c\u4e0d\u7528 \u00a0 \u7b49\u8d85\u65f6\uff0c\u76f4\u63a5\u91cd\u4f202\u53f7\u5305\uff0c\u5927\u5927\u6539\u5584\u4e86\u4e22\u5305\u65f6\u7684\u4f20\u8f93\u901f\u5ea6\u3002<\/p>\n<h4><a name=\"t7\"><\/a><\/h4>\n<h4><a name=\"t8\"><\/a>\u5ef6\u8fdfACK vs \u975e\u5ef6\u8fdfACK\uff1a<\/h4>\n<p>TCP\u4e3a\u4e86\u5145\u5206\u5229\u7528\u5e26\u5bbd\uff0c\u5ef6\u8fdf\u53d1\u9001ACK\uff08NODELAY\u90fd\u6ca1\u7528\uff09\uff0c\u8fd9\u6837\u8d85\u65f6\u8ba1\u7b97\u4f1a\u7b97\u51fa\u8f83\u5927 \u00a0 RTT\u65f6\u95f4\uff0c\u5ef6\u957f\u4e86\u4e22\u5305\u65f6\u7684\u5224\u65ad\u8fc7\u7a0b\u3002KCP\u7684ACK\u662f\u5426\u5ef6\u8fdf\u53d1\u9001\u53ef\u4ee5\u8c03\u8282\u3002<\/p>\n<h4><a name=\"t9\"><\/a>UNA vs ACK+UNA\uff1a<\/h4>\n<p>ARQ\u6a21\u578b\u54cd\u5e94\u6709\u4e24\u79cd\uff0cUNA\uff08\u6b64\u7f16\u53f7\u524d\u6240\u6709\u5305\u5df2\u6536\u5230\uff0c\u5982TCP\uff09\u548cACK\uff08\u8be5\u7f16\u53f7\u5305\u5df2\u6536\u5230 \u00a0 \uff09\uff0c\u5149\u7528UNA\u5c06\u5bfc\u81f4\u5168\u90e8\u91cd\u4f20\uff0c\u5149\u7528ACK\u5219\u4e22\u5931\u6210\u672c\u592a\u9ad8\uff0c\u4ee5\u5f80\u534f\u8bae\u90fd\u662f\u4e8c\u9009\u5176\u4e00\uff0c\u800c \u00a0 KCP\u534f\u8bae\u4e2d\uff0c\u9664\u53bb\u5355\u72ec\u7684 ACK\u5305\u5916\uff0c\u6240\u6709\u5305\u90fd\u6709UNA\u4fe1\u606f\u3002<\/p>\n<h4><a name=\"t10\"><\/a>\u975e\u9000\u8ba9\u6d41\u63a7\uff1a<\/h4>\n<p>KCP\u6b63\u5e38\u6a21\u5f0f\u540cTCP\u4e00\u6837\u4f7f\u7528\u516c\u5e73\u9000\u8ba9\u6cd5\u5219\uff0c\u5373\u53d1\u9001\u7a97\u53e3\u5927\u5c0f\u7531\uff1a\u53d1\u9001\u7f13\u5b58\u5927\u5c0f\u3001\u63a5\u6536 \u00a0 \u7aef\u5269\u4f59\u63a5\u6536\u7f13\u5b58\u5927\u5c0f\u3001\u4e22\u5305\u9000\u8ba9\u53ca\u6162\u542f\u52a8\u8fd9\u56db\u8981\u7d20\u51b3\u5b9a\u3002\u4f46\u4f20\u9001\u53ca\u65f6\u6027\u8981\u6c42\u5f88\u9ad8\u7684\u5c0f \u00a0 \u6570\u636e\u65f6\uff0c\u53ef\u9009\u62e9\u901a\u8fc7\u914d\u7f6e\u8df3\u8fc7\u540e\u4e24\u6b65\uff0c\u4ec5\u7528\u524d\u4e24\u9879\u6765\u63a7\u5236\u53d1\u9001\u9891\u7387\u3002\u4ee5\u727a\u7272\u90e8\u5206\u516c\u5e73 \u00a0 \u6027\u53ca\u5e26\u5bbd\u5229\u7528\u7387\u4e4b\u4ee3\u4ef7\uff0c\u6362\u53d6\u4e86\u5f00\u7740BT\u90fd\u80fd\u6d41\u7545\u4f20\u8f93\u7684\u6548\u679c\u3002<\/p>\n<h1><a name=\"t11\"><\/a>\u57fa\u672c\u4f7f\u7528<\/h1>\n<ol>\n<li>\u521b\u5efa KCP\u5bf9\u8c61\uff1a\n<pre>\/\/\u00a0\u521d\u59cb\u5316\u00a0kcp\u5bf9\u8c61\uff0cconv\u4e3a\u4e00\u4e2a\u8868\u793a\u4f1a\u8bdd\u7f16\u53f7\u7684\u6574\u6570\uff0c\u548ctcp\u7684\u00a0conv\u4e00\u6837\uff0c\u901a\u4fe1\u53cc\r\n\/\/\u00a0\u65b9\u9700\u4fdd\u8bc1\u00a0conv\u76f8\u540c\uff0c\u76f8\u4e92\u7684\u6570\u636e\u5305\u624d\u80fd\u591f\u88ab\u8ba4\u53ef\uff0cuser\u662f\u4e00\u4e2a\u7ed9\u56de\u8c03\u51fd\u6570\u7684\u6307\u9488\r\nikcpcb\u00a0*kcp\u00a0=\u00a0ikcp_create(conv,\u00a0user);<\/pre>\n<\/li>\n<li>\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\uff1a\n<div>\n<div id=\"highlighter_309596\" class=\"syntaxhighlighter  cpp\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<div class=\"line number8 index7 alt1\">8<\/div>\n<div class=\"line number9 index8 alt2\">9<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"cpp comments\">\/\/\u00a0KCP\u7684\u4e0b\u5c42\u534f\u8bae\u8f93\u51fa\u51fd\u6570\uff0cKCP\u9700\u8981\u53d1\u9001\u6570\u636e\u65f6\u4f1a\u8c03\u7528\u5b83<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"cpp comments\">\/\/\u00a0buf\/len\u00a0\u8868\u793a\u7f13\u5b58\u548c\u957f\u5ea6<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"cpp comments\">\/\/\u00a0user\u6307\u9488\u4e3a\u00a0kcp\u5bf9\u8c61\u521b\u5efa\u65f6\u4f20\u5165\u7684\u503c\uff0c\u7528\u4e8e\u533a\u522b\u591a\u4e2a\u00a0KCP\u5bf9\u8c61<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"cpp color1 bold\">int<\/code>\u00a0<code class=\"cpp plain\">udp_output(<\/code><code class=\"cpp keyword bold\">const<\/code>\u00a0<code class=\"cpp color1 bold\">char<\/code>\u00a0<code class=\"cpp plain\">*buf,\u00a0<\/code><code class=\"cpp color1 bold\">int<\/code>\u00a0<code class=\"cpp plain\">len,\u00a0ikcpcb\u00a0*kcp,\u00a0<\/code><code class=\"cpp keyword bold\">void<\/code>\u00a0<code class=\"cpp plain\">*user)<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"cpp plain\">{<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"cpp spaces\">\u00a0<\/code><code class=\"cpp plain\">....<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"cpp plain\">}<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"cpp comments\">\/\/\u00a0\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"cpp plain\">kcp-&gt;output\u00a0=\u00a0udp_output;<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/li>\n<li>\u5faa\u73af\u8c03\u7528 update\uff1a\n<div>\n<div id=\"highlighter_307740\" class=\"syntaxhighlighter  cpp\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"cpp comments\">\/\/\u00a0\u4ee5\u4e00\u5b9a\u9891\u7387\u8c03\u7528\u00a0ikcp_update\u6765\u66f4\u65b0\u00a0kcp\u72b6\u6001\uff0c\u5e76\u4e14\u4f20\u5165\u5f53\u524d\u65f6\u949f\uff08\u6beb\u79d2\u5355\u4f4d\uff09<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"cpp comments\">\/\/\u00a0\u5982\u00a010ms\u8c03\u7528\u4e00\u6b21\uff0c\u6216\u7528\u00a0ikcp_check\u786e\u5b9a\u4e0b\u6b21\u8c03\u7528\u00a0update\u7684\u65f6\u95f4\u4e0d\u5fc5\u6bcf\u6b21\u8c03\u7528<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"cpp plain\">ikcp_update(kcp,\u00a0millisec);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/li>\n<li>\u8f93\u5165\u4e00\u4e2a\u4e0b\u5c42\u6570\u636e\u5305\uff1a\n<div>\n<div id=\"highlighter_704094\" class=\"syntaxhighlighter  cpp\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"cpp comments\">\/\/\u00a0\u6536\u5230\u4e00\u4e2a\u4e0b\u5c42\u6570\u636e\u5305\uff08\u6bd4\u5982UDP\u5305\uff09\u65f6\u9700\u8981\u8c03\u7528\uff1a<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"cpp plain\">ikcp_input(kcp,\u00a0received_udp_packet,\u00a0received_udp_size);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u5904\u7406\u4e86\u4e0b\u5c42\u534f\u8bae\u7684\u8f93\u51fa\/\u8f93\u5165\u540e KCP\u534f\u8bae\u5c31\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u4e86\uff0c\u4f7f\u7528 ikcp_send \u6765\u5411\u8fdc\u7aef\u53d1\u9001\u6570\u636e\u3002\u800c\u53e6\u4e00\u7aef\u4f7f\u7528 ikcp_recv(kcp, ptr, size)\u6765\u63a5\u6536\u6570\u636e\u3002<\/li>\n<\/ol>\n<h1><a name=\"t12\"><\/a>\u534f\u8bae\u914d\u7f6e<\/h1>\n<p>\u534f\u8bae\u9ed8\u8ba4\u6a21\u5f0f\u662f\u4e00\u4e2a\u6807\u51c6\u7684 ARQ\uff0c\u9700\u8981\u901a\u8fc7\u914d\u7f6e\u6253\u5f00\u5404\u9879\u52a0\u901f\u5f00\u5173\uff1a<\/p>\n<ol>\n<li>\u5de5\u4f5c\u6a21\u5f0f\uff1a\n<pre>int\u00a0ikcp_nodelay(ikcpcb\u00a0*kcp,\u00a0int\u00a0nodelay,\u00a0int\u00a0interval,\u00a0int\u00a0resend,\u00a0int\u00a0nc)<\/pre>\n<p>nodelay \uff1a\u662f\u5426\u542f\u7528 nodelay\u6a21\u5f0f\uff0c0\u4e0d\u542f\u7528\uff1b1\u542f\u7528\u3002interval \uff1a\u534f\u8bae\u5185\u90e8\u5de5\u4f5c\u7684 interval\uff0c\u5355\u4f4d\u6beb\u79d2\uff0c\u6bd4\u5982 10ms\u6216\u8005 20msresend \uff1a\u5feb\u901f\u91cd\u4f20\u6a21\u5f0f\uff0c\u9ed8\u8ba40\u5173\u95ed\uff0c\u53ef\u4ee5\u8bbe\u7f6e2\uff082\u6b21ACK\u8de8\u8d8a\u5c06\u4f1a\u76f4\u63a5\u91cd\u4f20\uff09nc \uff1a\u662f\u5426\u5173\u95ed\u6d41\u63a7\uff0c\u9ed8\u8ba4\u662f0\u4ee3\u8868\u4e0d\u5173\u95ed\uff0c1\u4ee3\u8868\u5173\u95ed\u3002\u666e\u901a\u6a21\u5f0f\uff1a`ikcp_nodelay(kcp, 0, 40, 0, 0);\u6781\u901f\u6a21\u5f0f\uff1a ikcp_nodelay(kcp, 1, 10, 2, 1);<\/li>\n<li>\u6700\u5927\u7a97\u53e3\uff1a\n<div>\n<div id=\"highlighter_391778\" class=\"syntaxhighlighter  cpp\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"cpp color1 bold\">int<\/code>\u00a0<code class=\"cpp plain\">ikcp_wndsize(ikcpcb\u00a0*kcp,\u00a0<\/code><code class=\"cpp color1 bold\">int<\/code>\u00a0<code class=\"cpp plain\">sndwnd,\u00a0<\/code><code class=\"cpp color1 bold\">int<\/code>\u00a0<code class=\"cpp plain\">rcvwnd);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u8be5\u8c03\u7528\u5c06\u4f1a\u8bbe\u7f6e\u534f\u8bae\u7684\u6700\u5927\u53d1\u9001\u7a97\u53e3\u548c\u6700\u5927\u63a5\u6536\u7a97\u53e3\u5927\u5c0f\uff0c\u9ed8\u8ba4\u4e3a32.<\/li>\n<li>\u6700\u5927\u4f20\u8f93\u5355\u5143\uff1a\n<p>\u7eaf\u7b97\u6cd5\u534f\u8bae\u5e76\u4e0d\u8d1f\u8d23\u63a2\u6d4b MTU\uff0c\u9ed8\u8ba4 mtu\u662f1400\u5b57\u8282\uff0c\u53ef\u4ee5\u4f7f\u7528ikcp_setmtu\u6765\u8bbe\u7f6e\u8be5\u503c\u3002\u8be5\u503c\u5c06\u4f1a\u5f71\u54cd\u6570\u636e\u5305\u5f52\u5e76\u53ca\u5206\u7247\u65f6\u5019\u7684\u6700\u5927\u4f20\u8f93\u5355\u5143\u3002<\/li>\n<li>\u6700\u5c0fRTO\uff1a\n<p>\u4e0d\u7ba1\u662f TCP\u8fd8\u662f KCP\u8ba1\u7b97 RTO\u65f6\u90fd\u6709\u6700\u5c0f RTO\u7684\u9650\u5236\uff0c\u5373\u4fbf\u8ba1\u7b97\u51fa\u6765RTO\u4e3a40ms\uff0c\u7531\u4e8e\u9ed8\u8ba4\u7684 RTO\u662f100ms\uff0c\u534f\u8bae\u53ea\u6709\u5728100ms\u540e\u624d\u80fd\u68c0\u6d4b\u5230\u4e22\u5305\uff0c\u5feb\u901f\u6a21\u5f0f\u4e0b\u4e3a30ms\uff0c\u53ef\u4ee5\u624b\u52a8\u66f4\u6539\u8be5\u503c\uff1a<\/p>\n<div>\n<div id=\"highlighter_653809\" class=\"syntaxhighlighter  cpp\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"cpp plain\">kcp-&gt;rx_minrto\u00a0=\u00a010;<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/li>\n<\/ol>\n<h1><a name=\"t13\"><\/a>\u6700\u4f73\u5b9e\u8df5<\/h1>\n<h4><a name=\"t14\"><\/a>\u5185\u5b58\u5206\u914d\u5668<\/h4>\n<p>\u9ed8\u8ba4KCP\u534f\u8bae\u4f7f\u7528 malloc\/free\u8fdb\u884c\u5185\u5b58\u5206\u914d\u91ca\u653e\uff0c\u5982\u679c\u5e94\u7528\u5c42\u63a5\u7ba1\u4e86\u5185\u5b58\u5206\u914d\uff0c\u53ef\u4ee5 \u00a0\u7528ikcp_allocator\u6765\u8bbe\u7f6e\u65b0\u7684\u5185\u5b58\u5206\u914d\u5668\uff0c\u6ce8\u610f\u8981\u5728\u4e00\u5f00\u59cb\u8bbe\u7f6e\uff1a<\/p>\n<p>ikcp_allocator(my_new_malloc, my_new_free);<\/p>\n<h4><a name=\"t15\"><\/a>\u524d\u5411\u7ea0\u9519\u6ce8\u610f<\/h4>\n<p>\u4e3a\u4e86\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4f20\u8f93\u901f\u5ea6\uff0c\u4e0b\u5c42\u534f\u8bae\u4e5f\u8bb8\u4f1a\u4f7f\u7528\u524d\u5411\u7ea0\u9519\u6280\u672f\u3002\u9700\u8981\u6ce8\u610f\uff0c\u524d\u5411\u7ea0\u9519\u4f1a\u6839\u636e\u5197\u4f59\u4fe1\u606f\u89e3\u51fa\u539f\u59cb\u6570\u636e\u5305\u3002\u76f8\u540c\u7684\u539f\u59cb\u6570\u636e\u5305\u4e0d\u8981\u4e24\u6b21input\u5230KCP\uff0c\u5426\u5219\u5c06\u4f1a\u5bfc\u81f4kcp\u4ee5\u4e3a\u5bf9\u65b9\u91cd\u53d1\u4e86\uff0c\u8fd9\u6837\u4f1a\u4ea7\u751f\u66f4\u591a\u7684ack\u5360\u7528\u989d\u5916\u5e26\u5bbd\u3002<\/p>\n<p>\u6bd4\u5982\u4e0b\u5c42\u534f\u8bae\u4f7f\u7528\u6700\u7b80\u5355\u7684\u5197\u4f59\u5305\uff1a\u5355\u4e2a\u6570\u636e\u5305\u9664\u4e86\u81ea\u5df1\u5916\uff0c\u8fd8\u4f1a\u91cd\u590d\u5b58\u50a8\u4e00\u6b21\u4e0a\u4e00\u4e2a\u6570\u636e\u5305\uff0c\u4ee5\u53ca\u4e0a\u4e0a\u4e00\u4e2a\u6570\u636e\u5305\u7684\u5185\u5bb9\uff1a<\/p>\n<pre>Fn\u00a0=\u00a0(Pn,\u00a0Pn-1,\u00a0Pn-2)\r\n\r\nP0\u00a0=\u00a0(0,\u00a0X,\u00a0X)\r\nP1\u00a0=\u00a0(1,\u00a00,\u00a0X)\r\nP2\u00a0=\u00a0(2,\u00a01,\u00a00)\r\nP3\u00a0=\u00a0(3,\u00a02,\u00a01)<\/pre>\n<p>\u8fd9\u6837\u51e0\u4e2a\u5305\u53d1\u9001\u51fa\u53bb\uff0c\u63a5\u6536\u65b9\u5bf9\u4e8e\u5355\u4e2a\u539f\u59cb\u5305\u90fd\u53ef\u80fd\u88ab\u89e3\u51fa3\u6b21\u6765\uff08\u540e\u9762\u4e24\u4e2a\u5305\u4efb\u7136\u4f1a\u91cd\u590d\u8be5\u5305\u5185\u5bb9\uff09\uff0c\u90a3\u4e48\u8fd9\u91cc\u9700\u8981\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e00\u4e2a\u4e0b\u5c42\u6570\u636e\u5305\u53ea\u4f1ainput\u7ed9kcp\u4e00\u6b21\uff0c\u907f\u514d\u8fc7\u591a\u91cd\u590dack\u5e26\u6765\u7684\u6d6a\u8d39\u3002<\/p>\n<h4><a name=\"t16\"><\/a>\u7ba1\u7406\u5927\u89c4\u6a21\u8fde\u63a5<\/h4>\n<p>\u5982\u679c\u9700\u8981\u540c\u65f6\u7ba1\u7406\u5927\u89c4\u6a21\u7684 KCP\u8fde\u63a5\uff08\u6bd4\u5982\u5927\u4e8e3000\u4e2a\uff09\uff0c\u6bd4\u5982\u4f60\u6b63\u5728\u5b9e\u73b0\u4e00\u5957\u7c7b epoll\u7684\u673a\u5236\uff0c\u90a3\u4e48\u4e3a\u4e86\u907f\u514d\u6bcf\u79d2\u949f\u5bf9\u6bcf\u4e2a\u8fde\u63a5\u8c03\u7528\u5927\u91cf\u7684\u8c03\u7528 ikcp_update\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528ikcp_check\u6765\u5927\u5927\u51cf\u5c11 ikcp_update\u8c03\u7528\u7684\u6b21\u6570\u3002 ikcp_check\u8fd4\u56de\u503c\u4f1a\u544a\u8bc9\u4f60\u9700\u8981\u5728\u4ec0\u4e48\u65f6\u95f4\u70b9\u518d\u6b21\u8c03\u7528 ikcp_update\uff08\u5982\u679c\u4e2d\u9014\u6ca1\u6709 ikcp_send, ikcp_input\u7684\u8bdd\uff0c\u5426\u5219\u4e2d\u9014\u8c03\u7528\u4e86 ikcp_send, ikcp_input\u7684\u8bdd\uff0c\u9700\u8981\u5728\u4e0b\u4e00\u6b21interval\u65f6\u8c03\u7528 update\uff09<\/p>\n<p>\u6807\u51c6\u987a\u5e8f\u662f\u6bcf\u6b21\u8c03\u7528\u4e86 ikcp_update\u540e\uff0c\u4f7f\u7528 ikcp_check\u51b3\u5b9a\u4e0b\u6b21\u4ec0\u4e48\u65f6\u95f4\u70b9\u518d\u6b21\u8c03\u7528ikcp_update\uff0c\u800c\u5982\u679c\u4e2d\u9014\u53d1\u751f\u4e86 ikcp_send, ikcp_input\u7684\u8bdd\uff0c\u5728\u4e0b\u4e00\u8f6e interval \u7acb\u9a6c\u8c03\u7528 ikcp_update\u548c ikcp_check\u3002 \u4f7f\u7528\u8be5\u65b9\u6cd5\uff0c\u539f\u6765\u5728\u5904\u74062000\u4e2a kcp\u8fde\u63a5\u4e14\u6bcf\u4e2a\u8fde\u63a5\u6bcf10ms\u8c03\u7528\u4e00\u6b21update\uff0c\u6539\u4e3a check\u673a\u5236\u540e\uff0ccpu\u4ece 60%\u964d\u4f4e\u5230 15%\u3002<\/p>\n<p>&nbsp;<\/p>\n<h1><a name=\"t17\"><\/a>\u76f8\u5173\u5e94\u7528<\/h1>\n<ul>\n<li><a href=\"https:\/\/github.com\/vzex\/dog-tunnel\" target=\"_blank\" rel=\"noopener noreferrer\">dog-tunnel<\/a>: GO\u5f00\u53d1\u7684\u7f51\u7edc\u96a7\u9053\uff0c\u4f7f\u7528 KCP\u6781\u5927\u7684\u6539\u8fdb\u4e86\u4f20\u8f93\u901f\u5ea6\uff0c\u5e76\u79fb\u690d\u4e86\u4e00\u4efd GO\u7248\u672c KCP<\/li>\n<li><a href=\"https:\/\/github.com\/linxiaolong\/lua-kcp\" target=\"_blank\" rel=\"noopener noreferrer\">lua-kcp<\/a>\uff1aKCP\u7684 Lua\u6269\u5c55\uff0c\u7528\u4e8e Lua\u670d\u52a1\u5668<\/li>\n<li><a href=\"https:\/\/github.com\/libinzhangyuan\/asio_kcp\" target=\"_blank\" rel=\"noopener noreferrer\">asio-kcp<\/a>: \u4f7f\u7528 KCP\u7684\u5b8c\u6574 UDP\u7f51\u7edc\u5e93\uff0c\u5b8c\u6574\u5b9e\u73b0\u4e86\u57fa\u4e8e UDP\u7684\u94fe\u63a5\u72b6\u6001\u7ba1\u7406\uff0c\u4f1a\u8bdd\u63a7\u5236\uff0cKCP\u534f\u8bae\u8c03\u5ea6\u7b49<\/li>\n<li>KCP\u534f\u8bae\u5f00\u6e90\u5730\u5740:https:\/\/github.com\/skywind3000\/kcp<\/li>\n<\/ul>\n<h1><a name=\"t18\"><\/a>\u534f\u8bae\u6bd4\u8f83<\/h1>\n<p>\u5982\u679c\u6c38\u8fdc\u4e0d\u4e22\u5305\u90a3\u4e48 KCP\u548c TCP\u6027\u80fd\u5dee\u4e0d\u591a\uff0c\u4f46\u7f51\u7edc\u4f1a\u5361\uff0c\u9020\u6210\u5361\u7684\u539f\u56e0\u5c31\u662f\u4e22\u5305\u548c\u6296\u52a8\u3002\u5728\u5185\u7f51\u91cc\u76f4\u63a5\u6bd4\u8f83\uff0c\u5927\u5bb6\u90fd\u5dee\u4e0d\u591a\uff0c\u4f46\u662f\u653e\u5230\u516c\u7f51\u4e0a\uff0c\u653e\u52303G\/4G\u7f51\u7edc\u60c5\u51b5\u4e0b\uff0c\u6216\u8005\u4f7f\u7528\u5185\u7f51\u4e22\u5305\u6a21\u62df\uff0c\u5dee\u8ddd\u5c31\u5f88\u660e\u663e\u4e86\u3002\u516c\u7f51\u5728\u9ad8\u5cf0\u671f\u6709\u5e73\u5747\u63a5\u8fd110%\u7684\u4e22\u5305\uff0cwifi\/3g\/4g\u4e0b\u66f4\u7cdf\u7cd5\uff0c\u8fd9\u6b63\u662f\u9020\u6210\u5404\u79cd\u7f51\u7edc\u5361\u987f\u7684\u5143\u51f6\u3002<\/p>\n<p>\u611f\u8c22\u00a0<a href=\"https:\/\/github.com\/libinzhangyuan\/asio_kcp\" target=\"_blank\" rel=\"noopener noreferrer\">asio-kcp<\/a>\u00a0\u7684\u4f5c\u8005\u00a0<a href=\"https:\/\/github.com\/libinzhangyuan\" target=\"_blank\" rel=\"noopener noreferrer\">zhangyuan<\/a>\u00a0\u5bf9 KCP \u4e0e enet, udt\u505a\u8fc7\u7684\u4e00\u6b21\u6a2a\u5411\u8bc4\u6d4b\uff0c\u7ed3\u8bba\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>ASIO-KCP\u00a0<strong>has good performace in wifi and phone network(3G, 4G)<\/strong>.<\/li>\n<li>Extra using 20% ~ 50% network flow for speed improvement.<\/li>\n<li>The kcp is the\u00a0<strong>first choice for realtime pvp game<\/strong>.<\/li>\n<li>The lag is less than 1 second when network lag happen.\u00a0<strong>3 times better than enet<\/strong>\u00a0when lag happen.<\/li>\n<li>The enet is a good choice if your game allow 2 second lag.<\/li>\n<li><strong>UDT is a bad idea<\/strong>. It always sink into badly situation of more than serval seconds lag. And the recovery is not expected.<\/li>\n<li><a href=\"http:\/\/enet.bespin.org\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>enet<\/strong><\/a>\u00a0has the problem of lack of doc. And it has lots of functions that you may intrest.<\/li>\n<li>kcp&#8217;s doc is chinese. Good thing is the function detail which is writen in code is english. And you can use asio_kcp which is a good wrap.<\/li>\n<li>The kcp is a simple thing. You will write more code if you want more feature.<\/li>\n<li>UDT has a perfect doc. UDT may has more bug than others as I feeling.<\/li>\n<\/ul>\n<\/div>\n<div id=\"MySignature\">aliyun\u6d3b\u52a8 https:\/\/www.aliyun.com\/acts\/limit-buy?userCode=re2o7acl<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 &nbsp; KCP \u662f\u4e00\u4e2a\u5feb<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[118],"class_list":["post-4668","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=4668"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4668\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=4668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=4668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=4668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}