Archived
0
This commit is contained in:
2018-03-26 00:54:26 +03:00
parent a6349bcf5d
commit ed7c2a75f3
6 changed files with 68 additions and 5 deletions

View File

@@ -9,6 +9,8 @@ import lombok.Getter;
import lombok.ToString;
import mc.core.NotSupportException;
import java.util.Optional;
import static mc.core.netty.Utils.*;
@Getter
@@ -18,7 +20,7 @@ public class HandshakeRequestPacket implements NettyPacket {
private int protocolVersion;
private String serverAddress;
private int serverPort;
private int nextState;
private State nextState;
private int size;
@Override
@@ -31,13 +33,14 @@ public class HandshakeRequestPacket implements NettyPacket {
protocolVersion = readVarInt(byteBuf);
serverAddress = readString(byteBuf);
serverPort = readUnsignedShort(byteBuf);
nextState = readVarInt(byteBuf);
final int nextStateInt = readVarInt(byteBuf);
nextState = State.getById(nextStateInt).orElseThrow(() -> new UnknowState(nextStateInt));
size = lengthVarInt(id)
+ lengthVarInt(protocolVersion)
+ lengthString(serverAddress)
+ lengthVarInt(serverPort)
+ lengthVarInt(nextState);
+ lengthVarInt(nextState.getId());
}
@Override

View File

@@ -9,6 +9,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import java.util.List;
import java.util.Optional;
import static mc.core.netty.Utils.readVarInt;
@@ -18,8 +19,10 @@ public class PacketDecoder extends ReplayingDecoder<NettyPacket> {
int length = readVarInt(byteBuf);
int id = readVarInt(byteBuf);
if (id == 0) {
NettyPacket packet = new HandshakeRequestPacket();
Optional<Class<? extends NettyPacket>> packetClass = context.channel().attr(State.ATTR_STATE).get().getPacketClass(id);
if (packetClass.isPresent()) {
NettyPacket packet = packetClass.get().newInstance();
packet.fillFromByteBuf(byteBuf);
list.add(packet);
} else {

View File

@@ -12,6 +12,11 @@ import static mc.core.netty.Utils.equalsPacket;
@Slf4j
public class PacketHandler extends SimpleChannelInboundHandler<NettyPacket> {
@Override
public void channelActive(ChannelHandlerContext context) throws Exception {
context.channel().attr(State.ATTR_STATE).set(State.Handshaking);
}
@Override
protected void channelRead0(ChannelHandlerContext context, NettyPacket packet) throws Exception {
log.info("Packet: {} | Data: {}",

View File

@@ -0,0 +1,36 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-03-25
*/
package mc.core.netty;
import com.google.common.collect.ImmutableMap;
import io.netty.util.AttributeKey;
import lombok.Getter;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
public enum State {
Handshaking(1, ImmutableMap.of(0, HandshakeRequestPacket.class));
public static final AttributeKey<State> ATTR_STATE = AttributeKey.newInstance("ATTR_STATE");
public static Optional<State> getById(final int id) {
return Arrays.stream(State.values()).filter(state -> state.id == id).findFirst();
}
@Getter
private final int id;
private final Map<Integer, Class<? extends NettyPacket>> requestMap;
State(int id, Map<Integer, Class<? extends NettyPacket>> requestMap) {
this.id = id;
this.requestMap = requestMap;
}
public Optional<Class<? extends NettyPacket>> getPacketClass(int id) {
return Optional.ofNullable(requestMap.get(id));
}
}

View File

@@ -0,0 +1,11 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-03-26
*/
package mc.core.netty;
public class UnknowState extends RuntimeException {
public UnknowState(int numberOfState) {
super("Unknown state: " + numberOfState);
}
}