调试protobuffer接口的神器——luapb
2014-01-16 19:45 mahisaoo 阅读(5528) 评论(2) 编辑 收藏 举报luapb和云风写的lua-pbc(git://github.com/cloudwu/pbc.git)并不是一个东西。它们最主要的区别是:pbc虽然也不需要把.proto文件生成.js文件后使用,但是它却需要生成.pb文件;luapb是完全不需要生成任何中间文件,可以直接对.proto文件操作。
luapb是开源的一个小项目,源码也很小。github的连接为: https://github.com/zhanjunxiong/luapb。
我在工作中经常遇到要和后端的protobuffer对象调试,后端的程序是C封装的protobuffer,前端是用protojs(https://github.com/sirikata/protojs)解析protobuffer对象;在调试的过程中为了能更好的验证接口,luapb可以快速的验证;还是直接看代码吧:
利用luapb动态的填充protobuffer对象的文件simple.lua:
///////////////
require("luapb")
function encodeSimp()
pb.import("simple.proto")
local msg = pb.new("simple.Simple")
msg.name = "jack"
pb.import("simple.proto")
local msg = pb.new("simple.Simple")
msg.name = "jack"
msg.id = 2
msg.email = "jack@request.com"
msg.longid = 18446744073709551615
--[[ for decode
local resProto = pb.new("simple.Simple")
pb.parseFromString(resProto, decStr)
print("res:",resProto.uid)
print("res:",resProto.uid)
]]--
return pb.serializeToString(msg)
end
return pb.serializeToString(msg)
end
proto文件:simple.proto
package simple;
message Simple {
required string name = 1;
required int32 id = 2;
optional string email = 3;
required uint64 longid = 4;
}
message Simple {
required string name = 1;
required int32 id = 2;
optional string email = 3;
required uint64 longid = 4;
}
从上面的例子中可以看到,luapb的用法比较直观;适合需要快速开发的场景。似乎太简单了也不需要多的解释。
和server端通信的文件send.lua, 通信的方式是zmq,假设在本地的8765端口起了一个server程序:
require("zmq")
function writeFile(file,str)
local f = assert(io.open(file, "w"))
f:write(str)
f:close()
end
function readIO()
local method = os.getenv("REQUEST_METHOD")
local cmd = ""
if method == "POST" then
local len = os.getenv("CONTENT_LENGTH")
cmd = io.read(tonumber(len))
elseif method == "GET" then
cmd = os.getenv("QUERY_STRING")
end
local addr = os.getenv("REMOTE_ADDR")
local uci = luci.model.uci.cursor()
uci:tset("luci","sysauth_info",{ipaddr=addr})
uci:save("luci")
uci:commit("luci")
return cmd
end
function send_one(reqstring)
local cmd = reqstring
if cmd ~="" then
local ctx = zmq.init(1,1,0)
local s = ctx:socket(zmq.REQ)
local path = "tcp://127.0.0.1:8765"
s:connect(path)
s:send(cmd)
local result=s:recv()
writefile(result)
io.write(result)
s:close()
ctx:term()
else
io.write("")
end
end
dofile("simple.lua")
local req = encodeSimp()
local resString = send_one(req)
print("Decode Message:"..resString)
function writeFile(file,str)
local f = assert(io.open(file, "w"))
f:write(str)
f:close()
end
function readIO()
local method = os.getenv("REQUEST_METHOD")
local cmd = ""
if method == "POST" then
local len = os.getenv("CONTENT_LENGTH")
cmd = io.read(tonumber(len))
elseif method == "GET" then
cmd = os.getenv("QUERY_STRING")
end
local addr = os.getenv("REMOTE_ADDR")
local uci = luci.model.uci.cursor()
uci:tset("luci","sysauth_info",{ipaddr=addr})
uci:save("luci")
uci:commit("luci")
return cmd
end
function send_one(reqstring)
local cmd = reqstring
if cmd ~="" then
local ctx = zmq.init(1,1,0)
local s = ctx:socket(zmq.REQ)
local path = "tcp://127.0.0.1:8765"
s:connect(path)
s:send(cmd)
local result=s:recv()
writefile(result)
io.write(result)
s:close()
ctx:term()
else
io.write("")
end
end
dofile("simple.lua")
local req = encodeSimp()
local resString = send_one(req)
print("Decode Message:"..resString)
以上的这部分代码实现的是lua-zmq的通讯,即利用lua-zmq将动态填充的proto对象发送到server端;反之我们也通过lua-zmq接受server端返回的proto,并用luapb解析。
luapb提供的API有:
new:新建一个pb的对象; 用法: pb.import("simple.proto"); local msg = pb.new("simple.Simple")
import:导入proto文件; 用法: pb.import("simple.proto")
tostring: 转化成stringpb.serializeToString(msg)
parseFromString:解析成proto对象,用于decode时; 用法:local resProto = pb.new("lm.test"); pb.parseFromString(resProto, decStr); print("res:",resProto.uid)
serializeToString:序列化proto, 用于encode时; 用法:pb.serializeToString(msg)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架