refactoring: ProtocolDecoder
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package mc.protocol.codec;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.protocol.NettyAttributes;
|
||||
import mc.protocol.State;
|
||||
import mc.protocol.io.NetByteBuf;
|
||||
import mc.protocol.packets.ClientSidePacket;
|
||||
import mc.protocol.pool.PacketObjectPool;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class ProtocolDecoder extends ByteToMessageDecoder {
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
State state = ctx.channel().attr(NettyAttributes.STATE).get();
|
||||
NetByteBuf netByteBuf = new NetByteBuf(in);
|
||||
|
||||
int packetId = netByteBuf.readVarInt();
|
||||
Class<? extends ClientSidePacket> packetClass = state.getClientSidePacketById(packetId);
|
||||
if (packetClass == null) {
|
||||
log.warn("Unknown packet: State {} ; Id 0x{}", state, packetIdAsHexcode(packetId));
|
||||
netByteBuf.skipBytes(netByteBuf.readableBytes());
|
||||
} else {
|
||||
ClientSidePacket packet = PacketObjectPool.getInstance().getPool(packetClass).borrowObject();
|
||||
packet.readSelf(netByteBuf);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("IN: {}:{}", state, packet);
|
||||
}
|
||||
out.add(packet);
|
||||
}
|
||||
}
|
||||
|
||||
private static String packetIdAsHexcode(int packetId) {
|
||||
String hexPacketId = Integer.toHexString(packetId).toUpperCase();
|
||||
if (hexPacketId.length() == 1) hexPacketId = "0" + hexPacketId;
|
||||
|
||||
return hexPacketId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package mc.protocol.pool;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import mc.protocol.packets.ClientSidePacket;
|
||||
import mc.utils.pool.WideClassObjectPool;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class PacketObjectPool extends WideClassObjectPool<ClientSidePacket> {
|
||||
|
||||
@Getter
|
||||
private static final PacketObjectPool instance = new PacketObjectPool();
|
||||
}
|
||||
Reference in New Issue
Block a user