Project late LUA Access Notes 05--network protocol PROTOBUF usage in LUA PBC

Source: Internet
Author: User
Tags lua

As a network game, it is inevitable to interact with the server, the data structure on the PROTOBUF is a good solution, the use of protobuf in Lua there are a variety of options, where the use of the cloud wind of the PBC.

First go to this web site to download the build tool http://repo1.maven.org/maven2/com/google/protobuf/protoc/, my side of the download of version 2.5. Put the Protoc.exe in the designated place, and then copy the proto file, for better tools in the game, place Protoc in the engineering Lua directory, then create a folder, put proto files in the folder, and then start cmd execution protoc.exe– Descriptor_set_out=./.. /GAME.PB./*.proto.
CMD Test OK, we can create a new bat file, one key processing, here will all the protocols packaged together, you can also file packaging.

CD%~dp0

CD proto

xcopy E:\*.proto%cd%/y

. \protoc.exe   --descriptor_set_out=./. /GAME.PB./*.proto

Here to modify the next copy of the directory, all proto files are copied over, if you want to this command as a Unity menu, you can refer to my previous article

LUA uses the following to first read the file and register

    Local Path = Luaconst.luadir.. /3RD/PBLUA/GAME.PB ";--note path local

    addr = io.open (path," RB ") local
    buffer = addr:read" *a "
    addr:close ()
    protobuf.register (buffer)

Message serialization and sending to the server

Local addressbook = {
        name = "Alice",
        id = 12345,
        phone = {
            {number = "1301234567"},
            {number = 876 54321 ", type =" WORK "},
        }
    local
    code = Protobuf.encode (" tutorial. Person ", addressbook);
    --Send a message to the server

Receive server-side messages and deserialize

Local decode = Protobuf.decode ("tutorial.") Person ", data)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.