protobuf - ProtoBuffs Codec#

Adapted Air780E/Air700E Air780EP Air601 Air101/Air103 Air105 ESP32C3 ESP32S3

Note

This page document is automatically generated by this file. If there is any error, please submit issue or help modify pr, thank you!

Tip

This library has its own demo,click this link to view protobuf demo examples

Example

-- Load pb file, which is converted from pbtxt
-- Conversion command: protoc.exe -operson.pb --cpp_out=cpp person.pbtxt
-- protoc.exe Download Address: https://github.com/protocolbuffers/protobuf/releases
protobuf.load(io.readFile("/luadb/person.pb"))
local tb = {
    name = "wendal",
    id = 123,
    email = "[email protected]"
}
-- Encoding data with protobuf
local data = protobuf.encode("Person", tb)
if data then
    -- Print data length. Encoded data contains invisible characters, toHex is easy to display
    log.info("protobuf", #data, (data:toHex()))
end

protobuf.load(pbdata)#

Load pb binary definition data

Parameters

Incoming Value Type

Explanation

string

Data converted by the protoc.exe program, usually read from a file

Return Value

return value type

explanation

bool

Success or not

int

How long did it take to read, debugging

Examples

-- Note that the same file only needs to be loaded once, unless called protobuf.clear()
protobuf.load(io.readFile("/luadb/person.pb"))

protobuf.clear()#

Clear loaded binary definition data

Parameters

None

Return Value

return value type

explanation

nil

No return value, always success

Examples

-- Clear all loaded definition data
protobuf.clear()

protobuf.encode(tpname, data)#

Encoded protobuffs packets

Parameters

Incoming Value Type

Explanation

string

Data type name, defined in pb file, loaded by protobuf.load

table

The data to be encoded must be table, and the content must conform to the definition in the pb file.

Return Value

return value type

explanation

string

The encoded data, which is returned if it fails.nil

Examples

-- Data coding
local tb = {
    name = "wendal"
}
local pbdata = protobuf.encode("Person", tb)
if pbdata then
    -- Note that the encoded data usually has invisible characters
    log.info("protobuf", #pbdata, pbdata:toHex())
end

protobuf.decode(tpname, data)#

Decoding protobuffs packets

Parameters

Incoming Value Type

Explanation

string

Data type name, defined in pb file, loaded by protobuf.load

string

Data to be encoded

Return Value

return value type

explanation

table

Decoded data

Examples

-- Data coding
local tb = {
    name = "wendal"
}
local pbdata = protobuf.encode("Person", tb)
if pbdata then
    -- Note that the encoded data usually has invisible characters
    log.info("protobuf", #pbdata, pbdata:toHex())
end