{"id":533,"date":"2023-01-29T17:54:29","date_gmt":"2023-01-29T09:54:29","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=533"},"modified":"2023-02-16T19:12:57","modified_gmt":"2023-02-16T11:12:57","slug":"%e4%bb%8e%e7%94%9f%e4%ba%a7%e7%8e%af%e5%a2%83%e6%8a%a5%e9%94%99%e5%ad%a6%e4%b9%a0protobuf%e7%bc%96%e7%a0%81%e8%a7%84%e5%88%99","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=533","title":{"rendered":"\u4ece\u751f\u4ea7\u73af\u5883\u62a5\u9519\u5b66\u4e60protobuf\u7f16\u7801\u89c4\u5219"},"content":{"rendered":"<h1>\u80cc\u666f<\/h1>\n<p>\u4e2a\u522b\u73a9\u5bb6\u53cd\u9988,\u67d0\u4e2a\u6d3b\u52a8\u6253\u5f00\u65f6,\u6d88\u8d39\u5217\u8868\u663e\u793a\u9519\u8bef,\u7ecf\u524d\u7aef\u65e5\u5fd7\u6392\u67e5\u53d1\u73b0\u662f\u56e0\u4e3a\u8be5\u73a9\u5bb6\u7684\u6d88\u8d39\u4fe1\u606f\u89e3\u6790\u5931\u8d25\u4e86<\/p>\n<h1>\u6211\u4eec\u7684 protobuf \u7684\u7b80\u6613\u914d\u7f6e<\/h1>\n<pre><code class=\"line-numbers\">syntax = \"proto3\";\n\nmessage Pay {\n    '''\n    string role_id = 2;\n    '''\n    int32 tc=22;\n    '''\n}\n\nmessage MsgPayInfo {\n    repeated Pay pay_list = 1;\n    int32 flag=2;\n}\n\n<\/code><\/pre>\n<h1>\u7ecf\u8fc7\u5c31\u7ebf\u4e0a\u6570\u636e\u7684\u6392\u9664\u6d4b\u8bd5,\u53d1\u73b0\u9519\u8bef\u6570\u636e<\/h1>\n<ul>\n<li>\u5f53tc\u7684\u503c\u662f 127\u7684\u6d6e\u70b9\u6570\u65f6\u4f1a\u62a5\u9519,protobuf\u89e3\u6790\u5931\u8d25,\u4f8b\u5982:<\/li>\n<\/ul>\n<pre><code class=\"line-numbers\">lua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc=127.05}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,4,176,1,127]\n<\/code><\/pre>\n<h2>\u5f00\u59cb\u731c\u60f3<\/h2>\n<ul>\n<li>tc\u662f int32 \u7c7b\u578b,\u8d4b\u503c\u65f6\u4f7f\u7528\u6d6e\u70b9\u6570\u4f1a\u51fa\u9519<\/li>\n<li>\u5c1d\u8bd5\u53d1\u9001\u51e0\u4e2a\u6570\u636e \u53ef\u4ee5\u53d1\u9001\u6d6e\u70b9\u6570,\u5e76\u4e14\u4e0d\u4f1a\u62a5\u9519<\/li>\n<\/ul>\n<pre><code class=\"line-numbers\">lua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc=128.05}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,4,176,1,128,1]\n\nlua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc=126.05}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,3,176,1,126]\n\nlua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc=127}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,3,176,1,127]\n<\/code><\/pre>\n<h3>\u89c2\u5bdf\u51e0\u4e2a\u6570\u636e\u6211\u4eec\u5f97\u51fa\u4e00\u4e9b\u7ed3\u8bba:<\/h3>\n<ol>\n<li>\u5f53\u53d1\u9001tc&lt;=127\u65f6,\u7b2c\u4e8c\u4e2a\u6570\u5b57\u662f3\n<ul>\n<li>[10,3,176,1,126]<\/li>\n<li>[10,3,176,1,127]<\/li>\n<\/ul>\n<\/li>\n<li>\u5f53\u53d1\u9001tc>=128\u65f6,\u7b2c\u4e8c\u4e2a\u6570\u5b57\u662f4\n<ul>\n<li>[10,4,176,1,128,1]<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4e0e\u4e4b\u5bf9\u5e94\u7684\u662f\u53d1\u9001tc=128\u65f6,uint8\u6570\u7ec4\u957f\u5ea6\u591a\u4e861\u4e2a\u6570\u636e<\/p>\n<\/li>\n<li>\u731c\u60f3\u7b2c\u4e8c\u4e2a\u6570\u5b57\u76843\u30014\u53ef\u80fd\u4ee3\u8868\u7740\u540e\u7eed\u6570\u636e\u957f\u5ea6<\/li>\n<\/ol>\n<h3>\u540e\u7eed\u89c2\u5bdf\u51fa\u9519\u7684\u6570\u636e<\/h3>\n<pre><code class=\"line-numbers\">lua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc=127.05}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,4,176,1,127]\n<\/code><\/pre>\n<ul>\n<li>\u8fd9\u4e2a\u6570\u636e\u7684\u7b2c\u4e8c\u4e2a\u6570\u5b57\u662f4,\u800c\u4ed6\u7684\u540e\u7eed\u6570\u636e\u957f\u5ea6\u53ea\u67093<\/li>\n<li>!!!\u53ef\u80fd\u662f\u56e0\u4e3a\u8fd9\u6837\u6240\u4ee5\u6570\u636e\u957f\u5ea6\u5bf9\u4e0d\u4e0a,\u5bfc\u81f4protobuf\u89e3\u6790\u6d88\u606f\u5931\u8d25\u4e86!!!<\/li>\n<\/ul>\n<h1>\u67e5\u770b\u5b98\u7f51\u7684protobuf\u534f\u8bae\u6587\u6863\u9a8c\u8bc1\u6211\u4eec\u7684\u731c\u60f3<\/h1>\n<ul>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/encoding\" target=\"_blank\" rel=\"noopener\">https:\/\/developers.google.com\/protocol-buffers\/docs\/encoding<\/a><\/li>\n<\/ul>\n<h2>\u5b98\u7f51\u6587\u6863\u7684\u767d\u8bdd\u7406\u89e3\u4ee5\u62a5\u9519\u6570\u636e\u4e3a\u4f8b\u5b50<\/h2>\n<pre><code class=\"line-numbers\">lua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc=127.05}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,4,176,1,127]\n<\/code><\/pre>\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u6570\u636e 10, \u6211\u4eec\u53ef\u4ee5\u6839\u636e\u516c\u5f0f (field_number &lt;&lt; 3) | wire_type \u5c06\u89e3\u6790\u51fa\u5b57\u6bb5\u7f16\u53f7,\u548c\u540e\u7eed\u8bfb\u53d6\u6570\u636e\u7684\u7c7b\u578b<\/li>\n<\/ul>\n<pre><code class=\"line-numbers\">\/\/ 10\u7684\u4e8c\u8fdb\u5236\u5bf9\u5e9400001010\n10 = 0b00001010\n\u53ef\u4ee5\u89e3\u6790\u51fa\nfield_number = 0b00001 = 1\nwire_type = 0b010 = 2\n\n<\/code><\/pre>\n<h2>wire_type\u662f\u4ec0\u4e48\u610f\u601d\u5462?\u7ee7\u7eed\u770b\u6587\u6863<\/h2>\n<ul>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/encoding#structure\" target=\"_blank\" rel=\"noopener\">https:\/\/developers.google.com\/protocol-buffers\/docs\/encoding#structure<\/a><\/li>\n<li>wire_type\u5b57\u6bb5\u7c7b\u578b<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<th>ID<\/th>\n<th>Name<\/th>\n<th>Used For<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>VARINT<\/td>\n<td>int32, int64, uint32, uint64, sint32, sint64, bool, enum<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>I64<\/td>\n<td>fixed64, sfixed64, double<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>LEN<\/td>\n<td>string, bytes, embedded messages, packed repeated fields<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>SGROUP<\/td>\n<td>group start (deprecated)<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>EGROUP<\/td>\n<td>group end (deprecated)<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>I32<\/td>\n<td>fixed32, sfixed32, float<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\u6211\u4eec\u56de\u5230\u6211\u4eec\u4e0a\u9762\u9519\u8bef\u7684\u4f8b\u5b50<\/li>\n<\/ul>\n<pre><code class=\"line-numbers\">message MsgPayInfo {\n    repeated Pay pay_list = 1;\n    int32 flag=2;\n}\n\n\/\/ 10\u7684\u4e8c\u8fdb\u5236\u5bf9\u5e9400001010\n10 = 0b00001010\n\u53ef\u4ee5\u89e3\u6790\u51fa\nfield_number = 0b00001 = 1 \u4ee3\u8868proto\u4e2d\u7f16\u53f7\u4e3a1\u7684\u5b57\u6bb5,\u65e2 pay_list\nwire_type = 0b010 = 2\n\n<\/code><\/pre>\n<ul>\n<li>wire_type\u662f2\u4ee3\u8868\u7684,\u63a5\u4e0b\u6765\u662f\u4e00\u4e2a\u957f\u5ea6\u503c<\/li>\n<\/ul>\n<h1>!!!\u6211\u4eec\u7684\u731c\u60f3\u662f\u5bf9\u7684,\u7b2c\u4e8c\u4f4d\u4ee3\u8868\u7740\u6570\u636e\u957f\u5ea6\u503c,\u800c\u6211\u4eec\u6536\u5230\u7684\u4f1a\u62a5\u9519\u7684\u6570\u636e,\u957f\u5ea6\u503c\u4e0d\u5bf9!!!<\/h1>\n<h2>\u6211\u4eec\u7ee7\u7eed\u770b\u4e3a\u4ec0\u4e48\u5f53tc=128.05\u7684\u65f6\u5019\u53ef\u4ee5\u53d1\u9001\u6210\u529f<\/h2>\n<pre><code class=\"line-numbers\">lua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc=128.05}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,4,176,1,128,1]\n<\/code><\/pre>\n<ul>\n<li>\u53ef\u4ee5\u770b\u51fa,\u7b2c\u4e8c\u4f4d\u662f4,\u800c\u540e\u7eed\u786e\u5b9e\u67094\u4e2a\u6570\u636e,\u6240\u4ee5\u80fd\u89e3\u6790\u6210\u529f<\/li>\n<\/ul>\n<h1>\u90a3\u4e48[176,1,128,1]\u53c8\u662f\u5982\u4f55\u89e3\u6790\u51fa<\/h1>\n<pre><code class=\"line-numbers\">message Pay {\n    '''\n    string role_id = 2;\n    '''\n    int32 tc=22;\n    '''\n}\n\n176 = 0b10110000\nfield_number = 0b10110 = 22   \u4ee3\u8868proto\u4e2d\u7f16\u53f7\u4e3a22\u7684\u5b57\u6bb5,\u65e2 tc  !!!\u8fd9\u662f\u9519\u7684,\u51d1\u5de7\u5bf9\u4e0a\u4e86,\u540e\u7eed\u4f1a\u8bf4\u660e!!!\nwire_type = 0b000 = 0\n\n<\/code><\/pre>\n<ul>\n<li>wire_type\u662f0\u4ee3\u8868\u7684,\u63a5\u4e0b\u6765\u662f\u4e00\u4e2aVARINT<\/li>\n<\/ul>\n<h1>\u5f00\u59cb\u6709\u4e86\u65b0\u7591\u95ee?\u53ea\u67095\u4f4d\u4ee3\u8868\u7f16\u53f7,\u4e5f\u5c3131,\u7ebf\u4e0a\u4f7f\u7528\u7684\u5b57\u6bb5\u4e0d\u6b6231<\/h1>\n<pre><code class=\"line-numbers\">message Pay {\n    '''\n    string role_id = 2;\n    '''\n    int32 tc=22;\n    '''\n    int32 tc1=1;\n    int32 tc15=15;\n    int32 tc16=16;\n    int32 tc30=30;\n    int32 tc31=31;\n    int32 tc32=32;\n}\n\n\u5c1d\u8bd5\u53d1\u9001\u51e0\u4e2a\u6570\u636e\n\nlua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc1=1}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,2,8,1]\n\nlua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc15=15}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,2,120,15]\n\nlua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc16=16}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,3,128,1,16]\n\nlua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc30=30}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,3,240,1,30]\n\nlua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc31=31}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,3,248,1,31]\n\nlua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc32=32}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,3,128,2,32]\n\n\n<\/code><\/pre>\n<h2>!!!\u53d1\u73b0\u51e0\u4e2a\u7ed3\u8bba!!!<\/h2>\n<ol>\n<li>\u5f53\u5b57\u6bb5\u7f16\u53f7\u5c0f\u4e8e&lt;=15\u65f6\u5019 \u6570\u636e[120,15]\u957f\u5ea6\u662f2<\/li>\n<li>\u5f53\u5b57\u6bb5\u7f16\u53f7\u5728 16-31\u7684\u65f6\u5019,\u6570\u636e[248,1,31]\u957f\u5ea6\u662f3<\/li>\n<li>\u5f53\u5b57\u6bb5\u7f16\u53f7\u5728 \u5927\u4e8e31\u7684\u65f6\u5019, \u6570\u636e[128,2,32]\u7b2c\u4e8c\u4f4d\u4f1a\u53d1\u751f\u53d8\u5316<\/li>\n<\/ol>\n<h1>\u6211\u4eec\u6587\u6863\u67e5\u770b\u53d1\u751f\u4e86\u4ec0\u4e48<\/h1>\n<ul>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/encoding#varints\" target=\"_blank\" rel=\"noopener\">https:\/\/developers.google.com\/protocol-buffers\/docs\/encoding#varints<\/a><\/li>\n<li>\u5b98\u7f51\u8bf4\u4e86,varints\u767d\u8bdd\u7406\u89e3<\/li>\n<li>\u4e00\u4e2a8\u4f4d\u6574\u6570\n<ol>\n<li>\u6700\u9ad8\u4f4d1\u4ee3\u8868\u8fd8\u6709\u540e\u7eed\u6570\u636e,\u6700\u9ad8\u4f4d0\u4ee3\u8868\u6570\u636e\u7ed3\u675f\u4e86<\/li>\n<li>\u4f4e7\u4f4d\u7528\u6765\u4fdd\u5b58\u6570\u636e<\/li>\n<li>\u5982\u679c\u6709\u591a\u4e2a\u6570\u636e,\u8981\u7528\u5c0f\u7aef\u5e8f\u5408\u5e76\u8d77\u6765<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h2>\u56de\u5230\u6211\u4eec\u7684\u4f8b\u5b50[176,1,128,1]<\/h2>\n<pre><code class=\"line-numbers\">lua\u811a\u672c\u5c42\u53d1\u9001\u6570\u636e \n    {pay_list = {{tc=128.05}}}\nprotobuf\u7f16\u8bd1\u540e \n    uint8_t* = [10,4,176,1,128,1]\n<\/code><\/pre>\n<h3>[176,1]\u7528\u6765\u89e3\u6790\u5b57\u6bb5\u957f\u5ea6<\/h3>\n<ul>\n<li>\u6570\u5b57:176\n<ul>\n<li>field_number = 0b10110\n<ul>\n<li>\u6839\u636evarints\u767d\u8bdd\u7b2c\u4e00\u70b9,\u8fd8\u6709\u540e\u7eed\u6570\u636e<\/li>\n<li>\u4fdd\u7559\u5176\u4f59\u90e8\u5206 0b0110<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u6570\u5b57:1\n<ul>\n<li>\u56e0\u4e3a176\u4e2d\u6570\u636e\u672a\u53d6\u5b8c,\u7ee7\u7eed\u8bfb\u53d6<\/li>\n<li>1 = 0b00000001<\/li>\n<\/ul>\n<\/li>\n<li>\u6839\u636e varints \u767d\u8bdd\u7406\u89e3 \u7b2c\u4e09\u70b9 \u4ee5\u5c0f\u7aef\u5e8f\u5408\u5e76\u6570\u636e\n<ul>\n<li>00000001  0110 =  000000010110 = 22<\/li>\n<li>\u5f97\u5230\u5b57\u6bb5\u7f16\u53f7\u4e3a22<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>[128,1] \u662f\u6211\u4eec\u7684\u6570\u636e 128<\/h3>\n<ul>\n<li>\u6570\u5b57:128\n<ul>\n<li>0b128 = 10000000<\/li>\n<li>\u6839\u636evarints\u767d\u8bdd\u7b2c\u4e00\u70b9,\u8fd8\u6709\u540e\u7eed\u6570\u636e<\/li>\n<li>\u4fdd\u7559\u5176\u4f59\u90e8\u5206 0b0000000<\/li>\n<\/ul>\n<\/li>\n<li>\u6570\u5b57:1\n<ul>\n<li>\u56e0\u4e3a128\u4e2d\u6570\u636e\u672a\u53d6\u5b8c,\u7ee7\u7eed\u8bfb\u53d6<\/li>\n<li>1 = 0b00000001<\/li>\n<\/ul>\n<\/li>\n<li>\u6839\u636e varints \u767d\u8bdd\u7406\u89e3 \u7b2c\u4e09\u70b9 \u4ee5\u5c0f\u7aef\u5e8f\u5408\u5e76\u6570\u636e<\/li>\n<li>00000001  0000000 =   000000010000000 = 128<\/li>\n<li>\u5f97\u5230\u6570\u636e128<\/li>\n<\/ul>\n<h1>\u89e3\u51b3<\/h1>\n<ul>\n<li>\u5c06\u6570\u636e * 100 \u53d6\u6574\u540e\u5728\u53d1\u9001\u7ed9\u524d\u7aef<\/li>\n<\/ul>\n<h1>\u989d\u5916\u7684\u6536\u83b7<\/h1>\n<ul>\n<li>\u5f53\u6211\u4eec\u5b57\u6bb5\u7f16\u53f7>=16\u7684\u65f6\u5019,\u4f1a\u5bfc\u81f4\u6570\u636e\u591a\u4f7f\u7528\u4e00\u4e2a uint8\u7684\u957f\u5ea6<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>protobuf \u7f16\u7801\u89c4\u5219<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":[],"categories":[33,30,19],"tags":[34,35],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/533"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=533"}],"version-history":[{"count":5,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions"}],"predecessor-version":[{"id":562,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions\/562"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}