State
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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: {}",
|
||||
|
||||
36
src/main/java/mc/core/netty/State.java
Normal file
36
src/main/java/mc/core/netty/State.java
Normal 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));
|
||||
}
|
||||
}
|
||||
11
src/main/java/mc/core/netty/UnknowState.java
Normal file
11
src/main/java/mc/core/netty/UnknowState.java
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user