Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解码的过程碰到一个奇怪的问题 #16

Open
dqzg12300 opened this issue Aug 4, 2018 · 3 comments
Open

解码的过程碰到一个奇怪的问题 #16

dqzg12300 opened this issue Aug 4, 2018 · 3 comments

Comments

@dqzg12300
Copy link

协议体
syntax = "proto3";
package login;
message login {
string account = 1 ;
string password = 2 ;
int32 sdkid = 3 ;
}

发送下面这个包,客户端发完后直接解包是正常解出来的

send pbstr:0A 04 6B 69 6E 67 12 06 31 31 31 31 31 31 18 01
send:cmd(login.login) check(0) len(24) msg->{
["password"] = "111111",
["account"] = "king",
["sdkid"] = 1,
}

服务端接收到这个包的打印
recv pbstr:0A 04 6B 69 6E 67 12 06 31 31 31 31 31 31 18 01
recv:cmd(login.login) check(0) msg->"
king111111"

这个msg里面直接是个字符串了。
我看了下。
local msg = pb.decode(cmd, pbstr)
这里msg解码后得到的是一个字符串。
解码失败会直接得到tostring的结果吗?使用的协议文件是同一个。怎么会解码失败呢

@dqzg12300
Copy link
Author

我怀疑服务端的import_proto_file这个函数未能成功。可能路径不对。这个导入失败是否有返回值?

@dqzg12300
Copy link
Author

查到问题了。。原因特别奇葩。不知道你用过skynet么
local pb = require("luapbintf")
local path = skynet.getenv("app_root").."proto"
pb.add_proto_path(path)
local tool = require "tool"

skynet.start(function()
pb.import_proto_file("login.proto")
local pbstr = pb.encode("login.login", {account="king",password="111111"})
local msg= pb.decode("login.login", pbstr)
print("11111111111")
print(tool.dump(msg))
end)
上面这种情况就会导致上面我说的。pb.decode无法解编码

local pb = require("luapbintf")
local path = skynet.getenv("app_root").."proto"
pb.add_proto_path(path)
local tool = require "tool"

pb.import_proto_file("login.proto")
local pbstr = pb.encode("login.login", {account="king",password="111111"})
local msg= pb.decode("login.login", pbstr)
print("11111111111")
print(tool.dump(msg))

skynet.start(function()

end)

把decode的部分拿到外面。就可以正常解编码了。。。
想请问下。这种情况有啥办法解决不????

@dqzg12300
Copy link
Author

最后解决了。然后得出一个神奇的结论
local pb = require("luapbintf") 你这句必须放在里面声明。
如果声明在外面,在里面使用。则无法解码,我觉得这个应该是个问题。你可以找找看是啥原因。

下面的版本是我改了后。测试在skynet下可以正常解码
local path = skynet.getenv("app_root").."proto"
local tool = require "tool"

skynet.start(function()
local pb = require("luapbintf")
pb.add_proto_path(path)
pb.import_proto_file("login.proto")
local pbstr = pb.encode("login.login", {account="king",password="111111"})
local msg= pb.decode("login.login", pbstr)
print("11111111111")
print(tool.dump(msg))
end)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant