Unity3d使用ToLua lua protobuf3配合java neety protobuf3包含源码

博主环境(所有文件都在最底下):

  1. Unity环境: 2018.1.0b11(64bit)
  2. java环境:jdk 1.8,
  3. netty环境:netty-all-4.1.16.Final.jar
  4. protobuf环境:protobuf-java-3.3.0
  5. python环境:python 2.7

网络协议:

  1. 使用简单的协议一段完整的数据,前4位byte是数据总长度,向后偏移4位是protoId,用于指向解析的protoBytes

JAVA解析数据的地方:

文件:NioServerHandler.java
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
		// TODO Auto-generated method stub
	ByteBuf buf=(ByteBuf) msg;
        int totalByteLen = buf.readableBytes();
        int indeLne = 0;
        //解包
        while(totalByteLen>indeLne) {
        	buf.readBytes(dataLenBytes,0,4);
            int totalLen = Utils.bytesToInt(dataLenBytes, 0);
            indeLne += 4;
            buf.readBytes(dataLenBytes,0,4);
            int protoId = Utils.bytesToInt(dataLenBytes, 0);
            indeLne += 4;
            byte[]protoData = new byte[totalLen - 8];
            buf.readBytes(protoData,0,totalLen-8);
            indeLne += totalLen-8;
            Utils.Parse(protoId, protoData);
        }
        /***
         * readBytes这个方法的三个参数分别是:(1)复制目标地址,(2)起始位置注意这里的起始位置如果写0,他就是0+你上次读取的位置,(3)复制数据长度
         * 
         */
}


//这是Utils里面的方法
 public static void Parse(int protoId,byte[] protoData) {
		try {
			if(protoId == 1001) {
	        	Person person = Person.parseFrom(protoData);
	        	System.out.println("\n解析成功:\n"+person.toString());
	        }
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

Lua处理解析数据和发送加密protobuffer数据的地方:

--接收数据
function NetWork.Receive(data)
    local protoId = data:GetProtoId()
    local luaBytes = data:ReadBuffer()
    local pbName = UNITY.CMD2PB[protoId].name
    local pbData = pb.decode(pbName, luaBytes)
    GameNetManager:sendNetMessage(protoId,pbData)

    --测试发送
    local personData = {
        name = "ilse",
        age = 18,
        contacts = {
            { name = "alice", phonenumber = 12312341234 },
            { name = "bob", phonenumber = 45645674567 }
        }
    }
    NetWork.SendProtoData(1001,personData)
    local datac =0
end
--发送数据
function NetWork.SendProtoData(protoId,data)
    local pbName = UNITY.CMD2PB[protoId].name
    local bytes = assert(pb.encode(pbName, data))
    local ByteBuffer = GameByteBuffer.getNewInstance()
    ByteBuffer:WriteBuffer(protoId,bytes)
    local gameSoketClient = GameSoketClient.GetInstance()
    gameSoketClient:WriteGameByteBuffer(ByteBuffer)
end

C#处理数据的地方:

文件:GameSoketClient.cs
  void OnReceive(byte[] bytes, int length)
    {
        // Array.Reverse(bytes, 0, length);
        //拆包处理分包,协议是最前面4位是总长度,然后偏移4位是protoid,最后是protoData
        int resolveLen = 0;
        while (resolveLen<length)
        {
            //数据段大小
           int size = BitConverter.ToInt32(bytes, 0);
            resolveLen += 4;
            //protoId
            int protoId = BitConverter.ToInt32(bytes, resolveLen);
            resolveLen += 4;
            //
            int prptoDataSize = size - 4 - 4;
            //数据
            GameByteBuffer ByteBuffer = GameByteBuffer.getNewInstance();
            ByteBuffer.ParseByteBuffer(ref bytes, resolveLen, prptoDataSize, protoId);
            ReceiveBytesQueue.Enqueue(ByteBuffer);
            resolveLen += prptoDataSize;
        }
    }

最后我们来看看运行结果:

你可能需要的源代码(注意所有的源码和工具都有说明,请先看下载说明):

ToLua集成Protobuffer3

Protobuf-3.3生成器PB和java资源.zip

Netty Protobuf3 测试服务器

集成Protobuffer3到ToLua

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一路随云00000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值