package com.snowcattle.game.service.net.message.decoder;
import com.snowcattle.game.manager.LocalMananger;
import com.snowcattle.game.service.rpc.serialize.IRpcSerialize;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;
/**
* Created by jwp on 2017/3/7.
*/
public class RpcDecoder extends ByteToMessageDecoder {
private Class<?> genericClass;
public RpcDecoder(Class<?> genericClass) {
this.genericClass = genericClass;
}
@Override
public final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() < 4) {
return;
}
in.markReaderIndex();
int dataLength = in.readInt();
/*if (dataLength <= 0) {
ctx.close();
}*/
if (in.readableBytes() < dataLength) {
in.resetReaderIndex();
return;
}
byte[] data = new byte[dataLength];
in.readBytes(data);
IRpcSerialize IRpcSerialize = LocalMananger.getInstance().getLocalSpringBeanManager().getProtostuffSerialize();
Object obj = IRpcSerialize.deserialize(data, genericClass);
//Object obj = JsonUtil.deserialize(data,genericClass); // Not use this, have some bugs
out.add(obj);
}
}