Closed zglbig closed 1 year ago
当使用原生的 protobuf 且都为默认值时,不会传递数据。或者说 ExternalMessage.data = Vector3 与 = null 序列化的字节是一样的。
框架默认使用的是 jprotobuf,当 data 没有数据时,解析出来的是 null;所以在游戏逻辑服中接收的参数为 null。
解决这个问题有两种方法。
优点:性能高 缺点:需要手动配置
推荐使用场景,处理类似业务不多的情况可以考虑
MethodParsers.me().mappingParamSupplier(Vector3.class, Vector3::new);
优点:简单省事 缺点:相比方法一,性能略低
推荐使用场景,喜欢省事的哥们
public class MyDefaultMethodParser implements MethodParser {
... ... 省略部分代码
@Override
public Object parseParam(byte[] data, ActionCommand.ParamInfo paramInfo) {
... ... 省略部分代码
if (Objects.isNull(data)) {
try {
return actualTypeArgumentClazz.getDeclaredConstructor().newInstance();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
return DataCodecKit.decode(data, actualTypeArgumentClazz);
}
}
设置
MethodParsers.me().setMethodParser(new MyDefaultMethodParser());
多说一个方式,通过编解码器的方式
当为 null 时,传递一个空数组
public class MyProtoDataCodec implements DataCodec {
byte[] EMPTY_BYTES = new byte[0];
@Override
public byte[] encode(Object data) {
return ProtoKit.toBytes(data);
}
@Override
public <T> T decode(byte[] data, Class<?> dataClass) {
if (Objects.isNull(data)) {
data = EMPTY_BYTES;
}
return (T) ProtoKit.parseProtoByte(data, dataClass);
}
}
设置
IoGameGlobalSetting.setDataCodec(new MyProtoDataCodec());
尝试使用 17.1.55 版本,已经在 ProtoDataCodec.java 做了增强处理。
好的大佬 目前使用了 MyProtoDataCodec 方式 因为 17.1.55 貌似拉取不下来
我用的是unity3d客户端,现在碰到个问题就是如果一个协议对象里面的字段值都是初始值的话那么客户端发送到服务端入参的时候这个参数对象是 null 值 比如我有一个api
如果这个协议对象的 x,y,z 的值都等于 0 的话发送请求到服务端 那么这个对象是 null 值 而不是一个 x,y,z 都等于 0 的对象