Archived
0

refactoring

This commit is contained in:
2021-06-13 17:06:46 +03:00
parent 222f2dba61
commit 475f1a28ca
12 changed files with 206 additions and 15 deletions

View File

@@ -1,7 +1,7 @@
apply from: rootDir.toPath().resolve('logic.gradle').toFile()
dependencies {
implementation project(':utils')
api project(':utils')
implementation libs.netty.transport
implementation libs.netty.codec

View File

@@ -4,7 +4,7 @@ import io.netty.util.AttributeKey;
import lombok.experimental.UtilityClass;
@UtilityClass
public class NettyAttributes {
public class ProtocolAttributes {
public static final AttributeKey<State> STATE = AttributeKey.newInstance("STATE");
}

View File

@@ -0,0 +1,30 @@
package mc.protocol.handler;
import io.netty.channel.ChannelHandlerContext;
import mc.protocol.State;
import mc.protocol.packets.ClientSidePacket;
import mc.protocol.utils.Table;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class PacketProcessor {
private final Table<State, Class<? extends ClientSidePacket>, Handler> table = new Table<>();
public <P extends ClientSidePacket> PacketProcessor addHandler(State state, Class<P> packetClass, Handler<P> handler) {
table.put(state, packetClass, handler);
return this;
}
public <P extends ClientSidePacket> void process(State state, ChannelHandlerContext ctx, P packet) {
Handler handler = table.getColumnAndRow(state, packet.getClass());
if (handler != null) {
handler.handle(ctx, packet);
}
}
@FunctionalInterface
public interface Handler<P extends ClientSidePacket> {
void handle(ChannelHandlerContext ctx, P packet);
}
}

View File

@@ -4,23 +4,25 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import mc.protocol.NettyAttributes;
import mc.protocol.ProtocolAttributes;
import mc.protocol.State;
import mc.protocol.packets.ClientSidePacket;
import java.io.IOException;
import java.util.Objects;
@Slf4j
@RequiredArgsConstructor
public class PacketInboundHandler extends SimpleChannelInboundHandler<ClientSidePacket> {
public class ProtocolInboundHandler extends SimpleChannelInboundHandler<ClientSidePacket> {
private static final String CLIENT_FORCE_DISCONNECTED_IOEXCEPTION_MESSAGE_RU = "Программа на вашем хост-компьютере разорвала установленное подключение";
private final PacketProcessor packetProcessor;
@Override
protected void channelRead0(ChannelHandlerContext ctx, ClientSidePacket packet) {
State state = ctx.channel().attr(NettyAttributes.STATE).get();
//TODO process
State state = Objects.requireNonNull(ctx.channel().attr(ProtocolAttributes.STATE).get());
packetProcessor.process(state, ctx, packet);
}
@Override

View File

@@ -4,20 +4,26 @@ 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.ProtocolAttributes;
import mc.protocol.State;
import mc.protocol.buffer.NetByteBuf;
import mc.protocol.packets.ClientSidePacket;
import mc.protocol.pool.PacketObjectPool;
import java.util.List;
import java.util.Objects;
@Slf4j
public class ProtocolDecoder extends ByteToMessageDecoder {
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.channel().attr(ProtocolAttributes.STATE).set(State.HANDSHAKING);
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
State state = ctx.channel().attr(NettyAttributes.STATE).get();
State state = Objects.requireNonNull(ctx.channel().attr(ProtocolAttributes.STATE).get());
NetByteBuf netByteBuf = new NetByteBuf(in);
int packetId = netByteBuf.readVarInt();

View File

@@ -5,17 +5,19 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;
import mc.protocol.NettyAttributes;
import mc.protocol.ProtocolAttributes;
import mc.protocol.State;
import mc.protocol.buffer.NetByteBuf;
import mc.protocol.packets.ServerSidePacket;
import java.util.Objects;
@Slf4j
public class ProtocolEncoder extends MessageToByteEncoder<ServerSidePacket> {
@Override
protected void encode(ChannelHandlerContext ctx, ServerSidePacket packet, ByteBuf out) {
State state = ctx.channel().attr(NettyAttributes.STATE).get();
State state = Objects.requireNonNull(ctx.channel().attr(ProtocolAttributes.STATE).get());
Integer packetId = state.getServerSidePacketId(packet.getClass());
if (packetId == null) {
log.error("Unknown send packet: State {} ; Class {}", state, packet.getClass());

View File

@@ -0,0 +1,23 @@
package mc.protocol.utils;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
public class Table<C, R, V> {
private final Map<C, Map<R, V>> map = new HashMap<>();
@Nullable
public V getColumnAndRow(C column, R row) {
if (!map.containsKey(column)) {
return null;
}
return map.get(column).get(row);
}
public void put(C column, R row, V value) {
map.computeIfAbsent(column, c -> new HashMap<>()).put(row, value);
}
}