(转)lua protobuffer的实现

转自: http://www.voidcn.com/article/p-vmuovdgn-bam.html

 

(1)lua实现protobuf的简介

需要读者对google的protobuf有一定的了解。
Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.    https://developers.google.com/protocol-buffers/docs/overview
 

      但遗憾的是,官网的probocol buffers并不支持lua,云风大侠做的pbc的项目,地址为:https://github.com/cloudwu/pbc/blob/master/binding/lua/README.md. 实现了protobuf对lua的支持。

    主要步骤如下:

     第一步: You specify how you want the information you're serializing to be structured by defining protocol buffer message types in .proto files(定义.proto)

   第二步: Once you've defined your messages, you run the protocol buffer compiler for your application's language on your .proto file to generate data access classes.(编译.proto文件,生成.pb文件)

   第三步: 使用pbc库提供的函数,实现protobuf.

(2)安装protobuf

    下载地址:https://code.google.com/p/protobuf/

    安装protobuf后,protobuf提供了protoc指令,可以编译.proto文件

 

#protobuf
cd $WORKDIR
tar xfz protobuf-2.4.1.tar.gz
cd protobuf-2.4.1
./configure || exit $?
make -j3 || exit $?
make install || exit $?

 

使用方法:protoc --descriptor_set_out test.pb test.proto   

使用protoc -h 可以查看protoc的其他选项

把 多个proto 文件编译到一个.pb文件的方法: protoc --descriptor_set_out=$confdir/common.cmd.pb  $protodir/*.proto

 

 (3) 安装pbc

 

rm -rf pbc-master 
tar xfz pbc-master.tar.gz

cd pbc-master
make || exit $?
cd binding/lua/ && make || exit $?
cp -rf protobuf.so   $WORKDIR/lualib/

 

把编译生成的protobu.so文件放到LUA_PATH目录里面,当require "protobuf"的时候,可以找到这个文件。放在哪里不重要,重要的是让lua找到它。

除此之外,还有一步非常重要:

把 pbc/binding/lua/protobuf.lua 赋值到LUA_PATH目录中,当require "protobuf"的时候,可以找到这个文件。

 

http://www.cnblogs.com/ghost240/p/3253092.html 这篇文章中说,

LUA_PATH下,就可以调用protobuf中的库方法 是错的!在pbc/binding/lua下面编译出protobuf.so放在LUA_PATH下面,或者将protobuf.lua放在

 

 

 (4)如何使用pbc

   a: 定义一个common.proto文件

 

	package Common;

	message accountRegister
	{
	  optional string accountid = 1;
	  optional string hashedpwd = 2; }


 b:编码common.proto,生成common.pb文件

 

  protoc --descriptor_set_out=/common.pb  common.proto

 

  c: 初始化protobuf

require("protobuf")   特别需要注意

 

	-- 生成的pb文件
	local pbfile ="/path/to/common.pb"

	local pbdesc = io.open(pbfile, "rb") if not pbdesc then echoError("failed to load protobuf description file:" .. pbfile) return end local pbbuffer = pbdesc:read("*a") protobuf.register(pbbuffer) pbdesc:close()

 

 

   d:  编码 protobuf.encode(ptype, msg)

      ptype = 'Common.accountRegister' 千万不要忘了 Common前缀

      msg = {accountid = '123', hashedpwd = '123456'}  msg的声明的格式必须要和proto文件里声明的一样,否则编码不通过

 

   e: 解码 protobuf.decode(pbtype, msgbuf) 返回msg 和err,具体的返回内容请参考pbc的文件

转载于:https://www.cnblogs.com/xingchong/p/10542618.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lua是一种通用的脚本编程语言,可以使用它来实现中缀表达式后缀表达式。中缀表达式是我们通常使用的形式,例如"1 + 2 * 3",而后缀表达式则是运算符在操作数之后的形式,例如"1 2 3 * +"。 下面是一个示例Lua代码,用于将中缀表达式换为后缀表达式: ```lua -- 定义运算符的优先级 local operators = { ["+"] = 1, ["-"] = 1, ["*"] = 2, ["/"] = 2, } -- 判断字符是否为运算符 local function isOperator(char) return operators[char] ~= nil end -- 比较两个运算符的优先级 local function compareOperators(op1, op2) return operators[op1] >= operators[op2] end -- 将中缀表达式换为后缀表达式 local function infixToPostfix(expression) local output = {} local stack = {} for i = 1, #expression do local char = expression:sub(i, i) if char == " " then -- 跳过空格 elseif isOperator(char) then -- 处理运算符 while #stack > 0 and isOperator(stack[#stack]) and compareOperators(stack[#stack], char) do table.insert(output, stack[#stack]) table.remove(stack) end table.insert(stack, char) else -- 处理操作数 table.insert(output, char) end end -- 将剩余的运算符加入后缀表达式 while #stack > 0 do table.insert(output, stack[#stack]) table.remove(stack) end return table.concat(output, " ") end -- 测试示例 local infixExpression = "1 + 2 * 3" local postfixExpression = infixToPostfix(infixExpression) print(postfixExpression) -- 输出: "1 2 3 * +" ``` 以上代码定义了一个`infixToPostfix`函数,它接受一个中缀表达式作为输入,并返回相应的后缀表达式。您可以将中缀表达式作为字符串传递给该函数,然后使用结果进行进一步的处理或输出。 请注意,上述代码仅适用于简单的四则运算,如果您需要支持更多运算符或具有更复杂的表达式,请相应地扩展代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值