Lua使用protocolbuf

本文详细介绍如何在Lua环境中集成Google的Protocol Buffers (Protobuf)。内容包括下载与编译Lua的Protobuf插件、Python版Protobuf模块,以及如何通过批处理脚本自动生成Lua协议文件,并对编译过程中的常见问题进行了解决。

http://www.cppblog.com/sunicdavy/archive/2013/04/24/199693.html

https://code.google.com/p/protoc-gen-lua/ 下载网易兄弟写的lua的protobuf插件(网易都把pb给弄完了,as3的也是他们写的..)

 

编译python版的protobuf模块

https://code.google.com/p/protobuf/downloads/list 下载官方的原生版本protobuf, 这里发文时使用的是2.4.1版本

编译出protoc执行文件, 放一份在protobuf-2.4.1\src\下

下载python2.7版本, 在protobuf-2.4.1\python下运行python setup.py install(如果找不到python请给python绝对路径)

这一步, python会下一个蛋( 真的是一个python的egg文件 ), 然后编译出python版本的protobuf模块放置在python下


这里我掉了下坑,我挂载在linux下编译之后在windows下无法安装,protoc走第2个分支去了。

 

# Find the Protocol Compiler.
if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
  protoc = os.environ['PROTOC']
#elif os.path.exists("../src/protoc"):
#  protoc = "../src/protoc"
elif os.path.exists("../src/protoc.exe"):
  protoc = "../src/protoc.exe"
elif os.path.exists("../vsprojects/Debug/protoc.exe"):
  protoc = "../vsprojects/Debug/protoc.exe"
elif os.path.exists("../vsprojects/Release/protoc.exe"):
  protoc = "../vsprojects/Release/protoc.exe"
else:
  protoc = find_executable("protoc")


制作protoc-gen-lua的批处理

放一份protoc在protoc-gen-lua的plugin目录

编写批处理:protoc-gen-lua.bat

@python "%~dp0protoc-gen-lua"

 

协议目录生成脚本

在你需要放置协议的目录编写如下批处理

buildproto.bat

rd /S /Q .\%1%
"..\..\src\protoc-gen-lua\plugin\protoc.exe" --plugin=protoc-gen-lua="..\..\src\protoc-gen-lua\plugin\protoc-gen-lua.bat" --lua_out=. %1%.proto

注意protoc.exe及protoc-gen-lua.bat的路径符合你的路径

再编写要编译的proto协议的批处理generate.bat

call buildproto.bat loginsvc

执行generate.bat后, 将会编译同目录下的loginsvc.proto,输出loginsvc_pb.lua

 

编译链接lua的pb库

将protoc-gen-lua\protobuf\目录拷贝到之前的协议目录

将其下的pb.c链入你的工程, 注意VS2010的VC下需要修改源码

1.将 #include <endian.h>修改为

#ifndef _WIN32
     #include <endian.h>
     #endif

避免在windows下缺失文件报错.

2. 调整struct_unpack函数前几行为

static int struct_unpack(lua_State *L)
{
    uint8_t format = luaL_checkinteger(L, 1);
    size_t len;
    const uint8_t* buffer = (uint8_t*)luaL_checklstring(L, 2, &len);
    size_t pos = luaL_checkinteger(L, 3);
    uint8_t out[8];   

    buffer += pos;

避免VS2010的VC编译器过于标准, 严格要求C风格函数变量前置声明

在lua_State声明后添加如下代码

extern "C" { int luaopen_pb (lua_State *L);}   // 注意防在命名空间外的全局声明

    luaopen_pb( L );   // 直接注入全局pb, 避免动态加载pb.dll造成的一系列跨平台问题

 

lua中使用pb

local loginsvc_pb = require “loginsvc_pb”
 
local REQ = loginsvc_pb.CheckVersionREQ()
local Data = REQ:SerializeToString( )
 
local ACK = loginsvc_pb.CheckVersionACK()
ACK:ParseFromString( Data )

 

.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

我的工程目录

script\
        protobuf\
            buildproto.bat
            generate.bat
            loginsvc_pb.lua
            loginsvc.proto
        Main.lua
    src\
        protoc-gen-lua\
            example\
            plugin\
            protobuf\

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值