package net.yura.lobby.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;
import net.yura.lobby.server.LobbyServer;
/**
* @author Yura Mamyrin
*/
public class ProtoDecoder extends ByteToMessageDecoder {
final LobbyServer server;
public ProtoDecoder(LobbyServer server) {
this.server = server;
}
@Override
protected void decode(ChannelHandlerContext chc, ByteBuf in, List<Object> ml) throws Exception {
while (true) {
if (in.readableBytes() < 4) {
return;
}
in.markReaderIndex();
int dataLength = in.readInt();
if (in.readableBytes() < dataLength) {
in.resetReaderIndex();
return;
}
ml.add( server.decode( new ByteBufInputStream(in), dataLength) );
}
}
}