在lua环境中使用protobuf

xiaoxiao2021-02-27  172

在lua环境中使用protobuf

  26015人阅读  评论(4)  收藏  举报   分类: android(27)  最近在cocos2dx的项目中,需要在LUA脚本层使用protobuf协议。官方已经推出了很多种语言的版本。但唯独LUA版本不全。于是开始研究protobuf在LUA下的实现,将完整的过程记录了下来,希望对其它人能有所帮助。

1、下载protoc-gen-lua

可以通过HG从服务器(hg clone https://code.google.com/p/protoc-gen-lua/)上下载最新的版本。

简单介绍一下里面的三个目录:

example 存放的一个示例协议,

plugin 将.proto协议转为LUA脚本需要的工具。要注意,这个工具是用Python写的,所以后面我们需要安装python。

rotobuf这里存放了工程需要的文件。其中pb.c是C码文件。主要是在工程中引用,也可以编译成动态文件(.so)供LUA调用。其它LUA文件则需要引入到LUA工程中。

 

2、安装PYTHON27。推荐是这个版本。

3、下载protobuf然后编译出protoc.exe。

可以用SVN从服务器上(http://protobuf.googlecode.com/svn/trunk)下载最新的protobuf。我使用的是protobuf-2.4.1。

进入protobuf-2.4.1/vsprojects利用VS2010进行编译。生成的protoc.exe放到protobuf-2.4.1/src下。如果不放,后面无法安装python版的protobuf。

4、编译python版本的protobuf

在protobuf-2.4.1\python下运行python setup.py build,然后再执行python setup.py install。

注意:如果第3步的protoc.exe没有放,会出现错误找不到google\protobuf\compiler目录。

 

5、制作转换协议的批处理

在protoc-gen-lua/plugin目录下编写批处理:protoc-gen-lua.bat,就下面一行代码。

///

@python "%~dp0protoc-gen-lua"

///

但要确保你的python命令能正常运行。否则将python.exe所在的目录加到环境变量path中。

接着拷贝一份protoc.exe到protoc-gen-lua目录。第3步我们已经编译了出了protoc.exe。在协议转换中,我们需要使用他。

在protoc-gen-lua目录编写批处理:buildproto.bat 来转换协议。

[cpp]  view plain  copy rem 切换到.proto协议所在的目录   cd ../luascript   rem 将当前文件夹中的所有协议文件转换为lua文件   for %%i in (*.proto) do (     echo %%i   "..\protobuf\protoc\protoc.exe" --plugin=protoc-gen-lua="..\protobuf\plugin\protoc-gen-lua.bat" --lua_out=. %%i      )   echo end   pause  

请正确指定protoc.exe和protoc-gen-lua.bat相对协议目录的路径。

 

6、转换协议文件

protoc-gen-lua/example目录中,有一个协议文件person.proto,可以拿他做一下试验,会生成一个person_pb.lua

 

7、编译pb.c文件

protoc-gen-lua/protobuf目录中有一个pb.c文件。我们需要用他来协助lua完成protobuf的功能。

用vs2010新建一个控制台程序。将pb.c加入到工程中。在windows平台下,要对pb.c做如下修改。

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;

3)在主函数前面申明pb.c的入口。

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

 

编写主函数如下:

[html]  view plain  copy #include "stdafx.h"      extern "C"{       #include <lua.h>       #include <lualib.h>       #include <lauxlib.h>       int luaopen_pb (lua_State *L);   }   int main(int argc, char* argv[])   {              lua_State *L = lua_open();       luaL_openlibs(L);       luaopen_pb(L);       luaL_dofile(L, "main.lua");        lua_pcall(L, 0, LUA_MULTRET, 0);       lua_close(L);            return 0;    }  

工程需要lua5.1.lib的接入。这个请自行编译。

 

8、编写main.lua。

也就是测试文件,可以参考protoc-gen-lua/example中的test.lua。

[plain]  view plain  copy package.path = package.path .. ';./protobuf/?.lua'      require "person_pb"   local msg = person_pb.Person()   msg.id = 100    msg.name = "foo"    msg.email = "bar"       local pb_data = msg:SerializeToString()  -- Parse Example   print("create:", msg.id, msg.name, msg.email, pb_data)         local msg = person_pb.Person()    msg:ParseFromString(pb_data)    print("parser:", msg.id, msg.name, msg.email, pb_data)  

9、测试

编译并运行VS工程。运行目录应该是LUA文件所在的目录。运行结果如下:

 

 10、总结。

这里实现了,在C++中搭建lua的protobuf环境。但未实现纯粹的Lua-protobuf环境。

如果需要在LUA中实现protobuf,那需要自己将pb.c编译成dll。在Linux下需要利用protoc-gen-lua/protobuf中的makefile将pb.c编译成pb.so。

然后将pb.so或pb.dll导入到lua工程中。然后在main.lua中调用pb.c中的入口,代码如下:

[plain]  view plain  copy local a = package.loadlib("pb.dll", "luaopen_pb");   a()  

 

理论上是这样,我还没有做详细的测试。如果有进展,再完善本贴。

 

 转自:http://blog.csdn.net/sunshine7858/article/details/9260671

-----------------------------------------------------------------------------------------------

下载地址: http://code.google.com/p/protobuf/downloads/list 安装命令 tar -xzf protobuf-2.5.0.tar.gz   cd protobuf-2.5.0   ./configure --prefix=$INSTALL_DIR 

 make  

 make check

 make install 

然后进入python目录,

python setup.py install --prefix=$INSTALL_DIR

写proto文件 package lm; message Person {         required int32  id = 1;         required string str = 2;         optional int32  opt = 3; } 保存为 testp.testpb.proto 编译指令  protoc -I=/home/workspace/testprob --python_out=/home/workspace/testprob /home/workspace/testprob/testp.testpb.proto google https://developers.google.com/protocol-buffers/docs/pythontutorial 报错 package directory 'google/protobuf/compiler' does not exist 解决  https://groups.google.com/forum/?fromgroups=#!topic/protobuf/YeT5RW4qCxY python ./setup.py build sudo python ./setup.py install 报错  File "/home/workspace/testprob/testp/testpb_pb2.py", line 6, in <module>     from google.protobuf import reflection as _reflection   File "build/bdist.linux-i686/egg/google/protobuf/reflection.py", line 68, in <module>   File "build/bdist.linux-i686/egg/google/protobuf/internal/python_message.py"   ImportError: cannot import name enum_type_wrapper 解决 http://code.google.com/p/protobuf/issues/detail?id=438 Log message Fix  issue 438 : add missing 'enum_type_wrapper' to setup.py 是安装包的一个改进文件,copy下来, 重新安装 根据安装目录下的demo  自己改写了个简单的, 觉得它那个还是麻烦 write.py import testpb_pb4 import sys p = testpb_pb2.Person() try:   f = open(sys.argv[1], "rb")   p.ParseFromString(f.read())   f.close() except IOError:   print sys.argv[1] + ": File not found.  Creating a new file." p.id = 32 p.str = "test" f = open(sys.argv[1], "wb") f.write(p.SerializeToString()) f.close() print "write success" 编译指令 python write.py "test" read.py  import sys import testpb_pb2 if len(sys.argv) != 2:   print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"   sys.exit(-1) p = testpb_pb2.Person() f = open(sys.argv[1], "rb") p.ParseFromString(f.read()) f.close() print "p.str = ",  p.str print "p.id=", p.id 编译指令 python read.py "test"
转载请注明原文地址: https://www.6miu.com/read-16082.html

最新回复(0)