HomeServer/lua-protobuf使用说明.txt
2024-11-20 15:41:37 +08:00

33 lines
1.3 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

1. 先加载proto结构定义前面提到可以加载pb或者proto.Schema结构分别如下
protoc -I=. --plugin=protoc-gen-grpc-csharp=<plugin路径> --csharp_out=./ --grpc-csharp_out=./ <proto文件>
第一种大家都熟悉编一下pb文件使用loadfile加载
1 -- 加载pb文件需要把proto编成pb使用
2 local pb = require "pb"
3 assert(pb.loadfile "login.pb")
第二种是我使用的因为是Schema结构做成文本文件方便结合资源系统更新
1 local pb = require "pb"
2 local protoc = require "protoc" --protoc在lua-protobuf的目录里
3
4 local protoString = ResLoader.Instance:LoadLuaProtoFile('login.proto.txt') --用自己的资源系统加载文本格式文件
5 protoc:load(protoString) --加载文本文件的内容
2. 使用pb解码编码协议。这一步就是标准的流程了lua-protobuf可以直接对定义好的结构decode/encode
复制代码
1 -- 定义表数据
2 local loginCS = {
3 username="jack",
4 password="123456",
5 }
6
7 -- 序列化
8 local bytes = assert(pb.encode("login.req_login", loginCS))
9 print(pb.tohex(bytes))
10
11 -- 反序列化
12 local recvData = assert(pb.decode("login.req_login", bytes))
13 print(recvData .username)
复制代码
到这就跟以前其他平台使用protobuf没有什么区别了这篇入门到此结束。