{"id":25,"date":"2018-08-29T16:24:39","date_gmt":"2018-08-29T08:24:39","guid":{"rendered":"https:\/\/chenqinghe.com\/?p=25"},"modified":"2018-09-01T11:02:57","modified_gmt":"2018-09-01T03:02:57","slug":"kcp%e5%8d%8f%e8%ae%ae%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/chenqinghe.com\/?p=25","title":{"rendered":"KCP\u534f\u8bae\u8be6\u89e3"},"content":{"rendered":"<p>KCP\u662f\u4e00\u79cd\u57fa\u4e8eUDP\u7684\u4e0a\u5c42\u534f\u8bae\uff0c\u9879\u76ee\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/skywind3000\/kcp\" target=\"_blank\"  rel=\"nofollow\" >KCP &#8211; A Fast and Reliable ARQ Protocol<\/a>\uff0c\u4ee5\u4e0b\u662fgithub\u4e0a\u7684\u4ecb\u7ecd\uff1a<\/p>\n<blockquote>\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\u4f4e 30%-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<\/p>\n<\/blockquote>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u5728UDP\u4e0a\u518d\u505a\u4e86\u4e00\u5c42\u5c01\u88c5\uff0c\u6765\u5b9e\u73b0TCP\u7684\u6548\u679c\u5e76\u4e14\u5f25\u8865TCP\u7684\u4e00\u4e9b\u4e0d\u8db3\u3002<\/p>\n<p><!--more--><\/p>\n<p>KCP\u534f\u8bae\u662f\u4e2a\u4e8c\u8fdb\u5236\u534f\u8bae\uff0c\u5f53\u7136\uff0c\u4f20\u8f93\u5c42\u4e0a\u7684\u5c01\u88c5\uff0c\u4e3a\u4e86\u4fdd\u6301\u6548\u7387\uff0c\u4f7f\u7528\u4e8c\u8fdb\u5236\u534f\u8bae\u4e5f\u662f\u7406\u6240\u5f53\u7136\u7684\u3002<\/p>\n<h3>KCP\u7684\u5e27\u5934\u7ed3\u6784<\/h3>\n<p>KCP\u5e27\u59348\u5b57\u8282\u5bf9\u9f50\uff0cKCP\u7a7a\u5305\u5927\u5c0f\u4e3a24\u5b57\u8282\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|             conv              |  cmd  |  frg  |      wnd      |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|               ts              |               sn              |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|               una             |               len             |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|                                                               |\r\n*                              data                             *\r\n|                                                               |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+<\/code><\/pre>\n<p><strong>conv<\/strong>\uff08conversation\uff09<\/p>\n<p>4\u5b57\u8282\uff0c\u4f1a\u8bdd\u6807\u8bc6\u3002<\/p>\n<p>\u6211\u4eec\u77e5\u9053UDP\u4e0d\u662f\u9762\u5411\u8fde\u63a5\u7684\uff0c\u56e0\u6b64KCP\u81ea\u5df1\u5b9e\u73b0\u4e86\u4e00\u5957\u4f1a\u8bdd\u673a\u5236\u3002conv\u7531\u5ba2\u6237\u7aef\u968f\u673a\u751f\u6210\uff0c\u4e4b\u540e\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u4ea4\u4e92\u7684conv\u503c\u5747\u4e00\u81f4\uff0c\u4ee5\u6b64\u6765\u6807\u8bc6\u4e24\u4e2apeer\u4e4b\u95f4\u7684\u901a\u4fe1\u662f\u4e00\u4e2a\u4f1a\u8bdd\u3002<\/p>\n<p><strong>cmd<\/strong>\uff08command\uff09<\/p>\n<p>1\u5b57\u8282\uff0c\u662f\u6807\u8bc6KCP\u5305\u7684\u529f\u80fd\u3002\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<ul>\n<li>IKCP_CMD_PUSH = 81\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ cmd: push data<\/li>\n<li>IKCP_CMD_ACK = 82\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ cmd: ack<\/li>\n<li>IKCP_CMD_WASK = 83\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ cmd: window probe (ask)<\/li>\n<li>IKCP_CMD_WINS = 84\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ cmd: window size (tell)<\/li>\n<\/ul>\n<p><strong>frg<\/strong>\uff08fragment\uff09<\/p>\n<p>1\u5b57\u8282\uff0c\u5206\u6bb5\u5e8f\u53f7\u3002<\/p>\n<p>KCP\u6709\u4e24\u79cd\u6a21\u5f0f\uff0c\u4e00\u79cd\u662fstream\u6a21\u5f0f\uff0c\u4e00\u79cd\u662fmessage\u6a21\u5f0f\u3002\u5f53\u4e3astream\u6a21\u5f0f\u65f6\uff0cfrg\u7684\u503c\u59cb\u7ec8\u4e3a0\u3002\u5f53\u4e3amessage\u6a21\u5f0f\u65f6\uff0c\u4f20\u8f93\u7684\u6570\u636e\u5927\u5c0f\u8d85\u8fc7MTU\u9650\u5236\uff0c\u4f1a\u88ab\u5206\u6210\u591a\u4e2a\u5305\uff0c\u901a\u8fc7frg\u6765\u6807\u8bc6\u4e0d\u540c\u5305\u7684\u5e8f\u53f7\uff0c\u4f7f\u5f97\u5728\u4e0d\u77e5\u9053\u5305\u5230\u8fbe\u7684\u5148\u540e\u987a\u5e8f\u7684\u60c5\u51b5\u4e0b\u4e5f\u80fd\u591f\u901a\u8fc7frg\u5b57\u6bb5\u6765\u91cd\u65b0\u6309\u7167\u987a\u5e8f\u7ec4\u88c5\u6210\u539f\u59cb\u6570\u636e\u3002<\/p>\n<p><span style=\"color: #000000;\">NOTE\uff1a\u5728\u4f7f\u7528kcp-go\u5e93\u7684\u65f6\u5019\uff0c\u4f1a\u5728\u5199\u5165KCP\u4e4b\u524d\u8fdb\u884c\u5206\u5305\u64cd\u4f5c\uff0c\u56e0\u6b64\u65e0\u8bba\u6570\u636e\u591a\u5927\uff0cfrg\u7684\u503c\u59cb\u7ec8\u4e3a0\u3002<del>\u81f3\u4e8e\u5e93\u4f5c\u8005\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u505a\uff0c\u6682\u65f6\u4e0d\u6e05\u695a\u3002<\/del>\u8fd9\u6837\u4f1a\u66f4\u5b89\u5168\u4e00\u4e9b\uff0c\u5426\u5219\u53d1\u9001\u4e00\u4e2a\u8d85\u5927\u7684\u5305\uff0c\u5728KCP core\u5185\u90e8\u8fdb\u884c\u5206\u5305\u65f6\uff0c\u5305\u7684\u6570\u91cf\u53ef\u80fd\u5927\u4e8e255\uff0c\u800cfrg\u4e3a\u56fa\u5b9a1\u5b57\u8282\u957f\uff0c\u56e0\u6b64\u4f1a\u51fa\u9519\uff0c\u4e3a\u4e86\u907f\u514d\u53d1\u751f\u8fd9\u6837\u7684\u9519\u8bef\uff0c\u63d0\u524d\u505a\u4e86\u5206\u5305\u64cd\u4f5c\u3002<\/span><\/p>\n<p><strong>wnd<\/strong><\/p>\n<p>1\u5b57\u8282\uff0c\u7a97\u53e3\u5927\u5c0f\uff0c\u81ea\u5df1\uff08\u53d1\u9001\u5f53\u524d\u5305\u7684peer\uff09\u80fd\u591f\u63a5\u6536\u6570\u636e\u91cf\u3002<\/p>\n<p>\u7a97\u53e3\u5927\u5c0f\u53ef\u4ee5\u4e3a0\uff0c\u5f53\u4e3a0\u65f6\uff0c\u5728\u53d1\u9001\u6570\u636e\u524d\u4f1a\u8fdb\u884c\u63a2\u6d4b\u7a97\u53e3\u5927\u5c0f\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>ts<\/strong><\/p>\n<p>4\u5b57\u8282\uff0c\u5f53\u524d<strong>\u6beb\u79d2<\/strong>\u65f6\u95f4\u6233\u3002<\/p>\n<p><strong>sn<\/strong><\/p>\n<p>4\u5b57\u8282\uff0c\u5305\u5e8f\u53f7\u3002<\/p>\n<p>\u8be5\u5305\u5e8f\u53f7\u4e3a\u5f53\u524d\u4f1a\u8bdd\u4e2d\u7684\u5e8f\u53f7\uff0c\u4ece0\u5f00\u59cb\u3002<\/p>\n<p><strong>una<\/strong><\/p>\n<p>4\u5b57\u8282\uff0c\u6b64\u7f16\u53f7\u524d\u6240\u6709\u7684\u5305\u90fd\u5df2\u6536\u5230\u3002<\/p>\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\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 KCP\u534f\u8bae\u4e2d\uff0c\u9664\u53bb\u5355\u72ec\u7684 ACK\u5305\u5916\uff0c\u6240\u6709\u5305\u90fd\u6709UNA\u4fe1\u606f\u3002<\/p>\n<p><strong>len<\/strong><\/p>\n<p>4\u5b57\u8282\uff0c\u6570\u636e\u90e8\u5206\u7684\u957f\u5ea6\u3002<\/p>\n<h3>\u524d\u5411\u7ea0\u9519<\/h3>\n<p>\u5728KCP\u7684<a href=\"https:\/\/github.com\/xtaci\/kcp-go\" target=\"_blank\"  rel=\"nofollow\" >go\u7248\u672c\u5b9e\u73b0<\/a>\u4e2d\u8fd8\u52a0\u5165\u4e86\u524d\u5411\u7ea0\u9519\u3002\u5177\u4f53\u5b9e\u73b0\u4e3a\u53d1\u9001\u6570\u636e\u5305\u540e\uff0c\u518d\u53d1\u9001n\u4e2a\u7ea0\u9519\u5305\u3002\u5176\u4e2d\uff0cn\u7531\u53c2\u6570parityShards\u786e\u5b9a\uff0cn =\u00a0parityShards\u3002<\/p>\n<p>\u5e27\u5934\u683c\u5f0f\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|             next              |      type     |      len      |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+<\/code><\/pre>\n<p><strong>next<\/strong><\/p>\n<p>4\u5b57\u8282\uff0c\u4e0b\u4e00\u4e2aFEC\u5305\u7684seq\u503c\u3002<\/p>\n<p><strong>type<\/strong><\/p>\n<p>2\u5b57\u8282\uff0c\u6709\u4e24\u79cd\u503c\uff1atypeData\u548ctypeFEC\u3002<\/p>\n<p>\u5728\u6570\u636e\u5305\u4e2d\uff0c\u4e3atypeData\u3002\u7ea0\u9519\u5305\u4e2d\u4e3atypeFEC\u3002<\/p>\n<p><strong>len<\/strong><\/p>\n<p>\u8be5\u503c\u4e3a\u6570\u636e\u5305\u4e2d\u7684\u6570\u636e\u957f\u5ea6\uff0c\u5305\u62ec<strong>KCP\u5e27\u5934\u548c\u8be5\u5b57\u6bb5\u7684\u4e24\u4e2a\u5b57\u8282\u3002<\/strong><\/p>\n<h3>\u6570\u636e\u52a0\u5bc6<\/h3>\n<p>\u540c\u6837\uff0ckcp-go\u4e2d\u8fd8\u53ef\u4ee5\u542f\u7528\u6570\u636e\u52a0\u5bc6\uff0c\u5e27\u5934\u683c\u5f0f\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|                              nonce                            |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|                              nonce                            |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|             crc32             |\r\n+-------+-------+-------+-------+<\/code><\/pre>\n<p><strong>nonce<\/strong><\/p>\n<p>16\u5b57\u8282\u3002<\/p>\n<p>nonce\u7684\u751f\u6210\u65b9\u5f0f\u4e3a\u9996\u5148\u5224\u65ad\u672c\u6b21\u4f1a\u8bdd\u4e2d\u4e0a\u6b21\u53d1\u5305\u7684nonce\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u662f\u5426\u662f0\uff0c\u5982\u679c\u662f\uff0c\u5219\u91cd\u65b0\u751f\u6210\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u7136\u540eMD5\u540e\u4f5c\u4e3a\u5f53\u524d\u5305\u7684nonce\u3002\u5982\u679c\u4e0d\u662f\uff0c\u5219\u4e0d\u91cd\u65b0\u751f\u6210\u5b57\u7b26\u4e32\uff0c\u4ec5\u5bf9\u4e0a\u4e00\u4e2a\u5305\u7684nonce\u505amd5\u540e\u4f5c\u4e3a\u5f53\u524d\u5305\u7684nonce\u3002\u91cd\u65b0\u751f\u6210\u968f\u673a\u5b57\u7b26\u4e32\u7684\u6982\u7387\u4e3a1\/256.<\/p>\n<p><strong>crc32<\/strong><\/p>\n<p>4\u5b57\u8282\u3002<\/p>\n<p>\u8be5\u503c\u4e3a\u9664\u53bb\u6570\u636e\u52a0\u5bc6\u7684\u5e27\u5934\u5916\u5176\u4ed6\u6570\u636e\uff08\u5305\u62ecFEC\u5e27\u5934\uff09\u7684crc32\u503c\uff08IEEE\uff09\u3002<\/p>\n<p><strong>\u52a0\u5bc6<\/strong><\/p>\n<p>\u52a0\u5bc6\u6570\u636e\u4e3a\u4ee5\u4e0a\u6240\u6709\u6570\u636e\uff0c\u5305\u62ec\u52a0\u5bc6\u5e27\u5934\u3001\u524d\u5411\u7ea0\u9519\u5e27\u5934\u3001KCP\u5e27\u5934\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u52a0\u5165\u524d\u5411\u7ea0\u9519\u548c\u6570\u636e\u52a0\u5bc6\u540e\uff0c\u4e00\u4e2a\u5b8c\u6574\u7684\u6570\u636e\u5305\u5e27\u5934\u683c\u5f0f\u4e3a\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">                                +-------+-------+-------+-------+\r\n                                |             nonce             |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|                              nonce                            |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|             nonce             |             crc32             |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|              next             |      type     |      len      |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|             conv              |  cmd  |  frg  |      wnd      |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|               ts              |               sn              |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|               una             |               len             |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+\r\n|                                                               |\r\n*                              data                             *\r\n|                                                               |\r\n+-------+-------+-------+-------+-------+-------+-------+-------+<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>KCP\u662f\u4e00\u79cd\u57fa\u4e8eUDP\u7684\u4e0a\u5c42\u534f\u8bae\uff0c\u9879\u76ee\u5730\u5740\uff1aKCP &#8211; A Fast and Reliable ARQ Protocol\uff0c\u4ee5\u4e0b\u662fgithub\u4e0a\u7684\u4ecb\u7ecd\uff1a 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\u4f4e 30%-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\u2026\u2026<\/p>\n","protected":false},"author":1,"featured_media":34,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[8,6,7],"_links":{"self":[{"href":"https:\/\/chenqinghe.com\/index.php?rest_route=\/wp\/v2\/posts\/25"}],"collection":[{"href":"https:\/\/chenqinghe.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chenqinghe.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chenqinghe.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chenqinghe.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=25"}],"version-history":[{"count":1,"href":"https:\/\/chenqinghe.com\/index.php?rest_route=\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":26,"href":"https:\/\/chenqinghe.com\/index.php?rest_route=\/wp\/v2\/posts\/25\/revisions\/26"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/chenqinghe.com\/index.php?rest_route=\/wp\/v2\/media\/34"}],"wp:attachment":[{"href":"https:\/\/chenqinghe.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chenqinghe.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chenqinghe.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}