Under xLua using lua-protobuf

This article was published in the programmer Liu's blog, reproduced, please indicate the source: https://www.cnblogs.com/xiaohutu/p/12168781.html

 

Foreword

protobuf As a universal socket format, a variety of plug-ins, the most essential, the most critical is based on two binary operations:

1. packed binary data according to the proto file.

The playing of the binary data of data format required. 

So in fact different platforms need is their parsing library. As a cross-platform socket, packing out the same version of the binary format must be exactly the same, 1 in the proto files are generally to be more common in project development platform, 2 years of playing in the current format is the format of the language and the environment. Lua environment in general is out of the playing table.

Because the official did not give lua support, so we each play, the more there are four kinds of use:

1. PBC-Lua : Yun Feng early to write parsing library, parsing pb official protoc.exe generated only one drawback is resolved.

2. sproto : Yun Feng (really tireless high output) strong push in the second edition of the agreement, in fact, it can not be said to be completely proto, but a new socket, just compatible. This is his own description of the sproto: https://blog.codingnow.com/2015/04/sproto_rpc.html

3. Protoc-Gen-Lua : seanlin write plug-ins, this we use is also more, ulua, tolua many projects are using this. Principle is to generate .lua lua end profile, the first profile when using a defined package to New, and Serialize. Only supports 5.1, the latter would not maintained.

4. Lua-protobuf : Now we use this much, is currently working in Lilith starwing written, this is also very simple to use, can be read pb, you can also read the definition file (property anxious to catch) directly, and then directly in lua writing table in accordance with the proto format, encode on the line. 

Here to talk about how to access lua-protobuf plug-reference links:

xLua official: https://github.com/Tencent/xLua

xLua official integrate third-party libraries: https://github.com/chexiongsheng/build_xlua_with_libs

lua-protobuf: https://github.com/starwing/lua-protobuf

 

Code libraries

1. The first is to get the library file, go look at the version integrated git, whether there are already numbers can be used, which is based on the version xlua to save some preprogrammed libraries, specifically ffi, lpeg, rapidjson , lua-protobuf, pbc (chexiongsheng intimate big brother).


 

2. There is no ready-made version does not matter, we have compiled a library with relevant and xlua source on the line. Svn won the first third-party libraries, and then down the next version that we actually use xlua source (such as: https: //github.com/Tencent/xLua/tree/master/build), local good Kaodao build_xlua_with_libs folder. Then depending on the platform were compiled Ann Zhuo at NDK, cmake, ninja, AndroidSDK, JDK all installed, click make_android_lua53.bat, first installed cmake and c ++ vs environment of the PC, click make_win64_lua53.bat, under MAC first "chmod 777 make_ios_lua53.sh ", then perform" ./make_ios_lua53.sh ". After compiled over plugin_lua53 \ Plugins under each platform libraries have friends.

 

 

The library files added to the project

1. Load Static library methods AddBuildin xLua provided, this method can specify a file name to specify a specific library in C # loading method in lua end side, and defining the format: 

1 public void AddBuildin(string name, LuaCSFunction initer)
2  //name:buildin模块的名字,require时输入的参数;
3  //initer:初始化函数,原型是这样的public delegate int lua_CSFunction(IntPtr L),必须是静态函数,
4  //而且带MonoPInvokeCallbackAttribute属性修饰,这个api会检查这两个条件。

 2. 接着我们在某个地方定义一下这个LuaCSFunction,一般是在LuaDLL.cs里,这里把luaopen_pb这个方法extern过来,写进了LoadPb里 

 1 namespace LuaDLL
 2 { 
 3     public partial class Lua
 4     {
 5         [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
 6         public static extern int luaopen_pb(System.IntPtr L);
 7 
 8         [MonoPInvokeCallback(typeof(LuaDLL.lua_CSFunction))]
 9         public static int LoadPb(System.IntPtr L)
10         {    
11             return luaopen_pb(L);  
12         }
13     }
14 }

 3. 准备完毕后,在我们lua端的初始化的地方添加加载这个"pb"库的方法:

1 LuaEnv luaenv = new LuaEnv();
2 luaenv.AddBuildin("pb", XLua.LuaDLL.Lua.LoadPb);
3 --luaenv.AddBuildin("rapidjson", XLua.LuaDLL.Lua.LoadRapidJson);

 4. 这样的话在lua中再require 'pb'的时候就会自动调用pb库

1 -- 加载pb文件
2 local pb = require "pb"

好了接下来就可以写网路框架代码了。

 

框架代码接入

1. 先加载proto结构定义,前面提到可以加载pb或者proto.Schema结构,分别如下:

    第一种大家都熟悉,编一下pb文件,使用loadfile加载

1 -- 加载pb文件,需要把proto编成pb使用
2 local pb = require "pb"
3 assert(pb.loadfile "login.pb")

    第二种是我使用的,因为是Schema结构,做成文本文件方便结合资源系统更新

1 local pb = require "pb"
2 local protoc = require "protoc" --protoc在lua-protobuf的目录里 
3 
4 local protoString = ResLoader.Instance:LoadLuaProtoFile('login.proto.txt') --用自己的资源系统加载文本格式文件
5 protoc:load(protoString) --加载文本文件的内容

 2. 使用pb解码,编码协议。这一步就是标准的流程了,lua-protobuf可以直接对定义好的结构decode/encode:

 1 -- 定义表数据
 2 local loginCS = {
 3     username="jack",
 4     password="123456",
 5 }
 6 
 7 -- 序列化
 8 local bytes = assert(pb.encode("login.req_login", loginCS))
 9 print(pb.tohex(bytes))
10 
11 -- 反序列化
12 local recvData = assert(pb.decode("login.req_login", bytes))
13 print(recvData .username)

 This is to use with other platforms before protobuf no difference, this entry-ended.

 

Guess you like

Origin www.cnblogs.com/xiaohutu/p/12168781.html