All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iohao.game.common.kit.ProtoKit Maven / Gradle / Ivy

/*
 * ioGame
 * Copyright (C) 2021 - present  渔民小镇 ([email protected][email protected]) . All Rights Reserved.
 * # iohao.com . 渔民小镇
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package com.iohao.game.common.kit;

import com.baidu.bjf.remoting.protobuf.Codec;
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;
import com.iohao.game.common.consts.CommonConst;
import com.iohao.game.common.consts.IoGameLogName;
import com.iohao.game.common.kit.concurrent.TaskKit;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * @author 渔民小镇
 * @date 2022-01-11
 */
@UtilityClass
@Slf4j(topic = IoGameLogName.CommonStdout)
public class ProtoKit {
    /**
     * 将对象转为 pb 字节数组
     *
     * @param data 对象
     * @return 字节数组 (一定不为null)
     */
    @SuppressWarnings("unchecked")
    public byte[] toBytes(Object data) {

        if (Objects.isNull(data)) {
            return CommonConst.emptyBytes;
        }

        Class clazz = data.getClass();
        Codec codec = ProtobufProxy.create(clazz);

        try {
            return codec.encode(data);
        } catch (Throwable e) {
            log.error(e.getMessage(), e);
        }

        return CommonConst.emptyBytes;
    }

    /**
     * 将字节解析成 pb 对象
     *
     * @param data  pb 字节
     * @param clazz pb class
     * @param    t
     * @return pb 对象
     */
    public  T parseProtoByte(byte[] data, Class clazz) {

        if (Objects.isNull(data)) {
            return null;
        }

        Codec codec = ProtobufProxy.create(clazz);

        try {
            return codec.decode(data);
        } catch (Throwable e) {
            log.error(e.getMessage(), e);
        }

        return null;
    }

    public void create(Class clazz) {
        TaskKit.executeVirtual(() -> {
            // create a protobuf proxy class
            ProtobufProxy.create(clazz);
        });
    }
}