del NettyPacket
This commit is contained in:
@@ -7,4 +7,7 @@ package mc.core;
|
||||
public interface Packet {
|
||||
int getSize();
|
||||
int getId();
|
||||
|
||||
void readSelf(NetStream netStream);
|
||||
void writeSelf(NetStream netStream);
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import mc.core.Packet;
|
||||
|
||||
public interface NettyPacket extends Packet {
|
||||
void writeSelf(ByteBuf byteBuf);
|
||||
void fillFromByteBuf(ByteBuf byteBuf);
|
||||
}
|
||||
@@ -9,12 +9,13 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ReplayingDecoder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.NetStream;
|
||||
import mc.core.Packet;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
public class PacketDecoder extends ReplayingDecoder<NettyPacket> {
|
||||
public class PacketDecoder extends ReplayingDecoder<Packet> {
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext context, ByteBuf byteBuf, List<Object> list) throws Exception {
|
||||
NetStream netStream = new WrapperByteBufNetStream(byteBuf);
|
||||
@@ -23,11 +24,11 @@ public class PacketDecoder extends ReplayingDecoder<NettyPacket> {
|
||||
int id = netStream.readVarInt();
|
||||
log.debug("PktLEN: {} | PktID: {}", length, id);
|
||||
|
||||
Optional<Class<? extends NettyPacket>> packetClass = context.channel().attr(State.ATTR_STATE).get().getPacketClass(id);
|
||||
Optional<Class<? extends Packet>> packetClass = context.channel().attr(State.ATTR_STATE).get().getPacketClass(id);
|
||||
|
||||
if (packetClass.isPresent()) {
|
||||
NettyPacket packet = packetClass.get().newInstance();
|
||||
packet.fillFromByteBuf(byteBuf);
|
||||
Packet packet = packetClass.get().newInstance();
|
||||
packet.readSelf(netStream);
|
||||
list.add(packet);
|
||||
if (length < packet.getSize()) {
|
||||
log.warn("WTF?! length < packet.getSize() !!");
|
||||
|
||||
@@ -7,10 +7,11 @@ package mc.core.netty;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import mc.core.Packet;
|
||||
|
||||
public class PacketEncoder extends MessageToByteEncoder<NettyPacket> {
|
||||
public class PacketEncoder extends MessageToByteEncoder<Packet> {
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext context, NettyPacket packet, ByteBuf byteBuf) throws Exception {
|
||||
packet.writeSelf(byteBuf);
|
||||
protected void encode(ChannelHandlerContext context, Packet packet, ByteBuf byteBuf) throws Exception {
|
||||
packet.writeSelf(new WrapperByteBufNetStream(byteBuf));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,20 +7,21 @@ package mc.core.netty;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.Packet;
|
||||
import mc.core.netty.packets.HandshakeRequestPacket;
|
||||
import mc.core.netty.packets.HandshakeResponsePacket;
|
||||
|
||||
import static mc.core.netty.Utils.equalsPacket;
|
||||
|
||||
@Slf4j
|
||||
public class PacketHandler extends SimpleChannelInboundHandler<NettyPacket> {
|
||||
public class PacketHandler extends SimpleChannelInboundHandler<Packet> {
|
||||
@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 {
|
||||
protected void channelRead0(ChannelHandlerContext context, Packet packet) throws Exception {
|
||||
log.info("Packet: {} | Data: {}",
|
||||
packet.getClass().getSimpleName(),
|
||||
packet.toString());
|
||||
|
||||
@@ -7,6 +7,7 @@ package mc.core.netty;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import io.netty.util.AttributeKey;
|
||||
import lombok.Getter;
|
||||
import mc.core.Packet;
|
||||
import mc.core.netty.packets.HandshakeRequestPacket;
|
||||
import mc.core.netty.packets.PingPacket;
|
||||
|
||||
@@ -29,14 +30,14 @@ public enum State {
|
||||
|
||||
@Getter
|
||||
private final int id;
|
||||
private final Map<Integer, Class<? extends NettyPacket>> requestMap;
|
||||
private final Map<Integer, Class<? extends Packet>> requestMap;
|
||||
|
||||
State(int id, Map<Integer, Class<? extends NettyPacket>> requestMap) {
|
||||
State(int id, Map<Integer, Class<? extends Packet>> requestMap) {
|
||||
this.id = id;
|
||||
this.requestMap = requestMap;
|
||||
}
|
||||
|
||||
public Optional<Class<? extends NettyPacket>> getPacketClass(int id) {
|
||||
public Optional<Class<? extends Packet>> getPacketClass(int id) {
|
||||
return Optional.ofNullable(requestMap.get(id));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,30 +4,32 @@
|
||||
*/
|
||||
package mc.core.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import mc.core.NetStream;
|
||||
import mc.core.NotSupportException;
|
||||
import mc.core.Packet;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
@ToString
|
||||
class UnknownPacket implements NettyPacket {
|
||||
class UnknownPacket implements Packet {
|
||||
private final int length;
|
||||
private final int id;
|
||||
|
||||
public void writeSelf(ByteBuf byteBuf) {
|
||||
throw new NotSupportException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillFromByteBuf(ByteBuf byteBuf) {
|
||||
throw new NotSupportException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
throw new NotSupportException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NetStream netStream) {
|
||||
throw new NotSupportException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
package mc.core.netty;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.Packet;
|
||||
|
||||
@Slf4j
|
||||
public class Utils {
|
||||
@@ -38,7 +39,7 @@ public class Utils {
|
||||
return lengthVarInt(value.length()) + value.length();
|
||||
}
|
||||
|
||||
public static boolean equalsPacket(NettyPacket packet, String name) {
|
||||
public static boolean equalsPacket(Packet packet, String name) {
|
||||
return packet.getClass().getSimpleName().equals(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,21 +4,19 @@
|
||||
*/
|
||||
package mc.core.netty.packets;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
import mc.core.NetStream;
|
||||
import mc.core.NotSupportException;
|
||||
import mc.core.netty.NettyPacket;
|
||||
import mc.core.Packet;
|
||||
import mc.core.netty.State;
|
||||
import mc.core.netty.UnknowState;
|
||||
import mc.core.netty.WrapperByteBufNetStream;
|
||||
|
||||
import static mc.core.netty.Utils.*;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public class HandshakeRequestPacket implements NettyPacket {
|
||||
public class HandshakeRequestPacket implements Packet {
|
||||
private static final int id = 0;
|
||||
private int protocolVersion;
|
||||
private String serverAddress;
|
||||
@@ -26,29 +24,6 @@ public class HandshakeRequestPacket implements NettyPacket {
|
||||
private State nextState;
|
||||
private int size;
|
||||
|
||||
@Override
|
||||
public void writeSelf(ByteBuf byteBuf) {
|
||||
throw new NotSupportException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillFromByteBuf(ByteBuf byteBuf) {
|
||||
NetStream netStream = new WrapperByteBufNetStream(byteBuf);
|
||||
|
||||
protocolVersion = netStream.readVarInt();
|
||||
serverAddress = netStream.readString();
|
||||
serverPort = netStream.readUnsignedShort();
|
||||
final int nextStateInt = netStream.readVarInt();
|
||||
nextState = State.getById(nextStateInt).orElseThrow(() -> new UnknowState(nextStateInt));
|
||||
byteBuf.skipBytes(2); //TODO magic
|
||||
|
||||
size = lengthVarInt(id)
|
||||
+ lengthVarInt(protocolVersion)
|
||||
+ lengthString(serverAddress)
|
||||
+ lengthUnsignedShort(serverPort)
|
||||
+ lengthVarInt(nextState.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return size;
|
||||
@@ -58,4 +33,25 @@ public class HandshakeRequestPacket implements NettyPacket {
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
protocolVersion = netStream.readVarInt();
|
||||
serverAddress = netStream.readString();
|
||||
serverPort = netStream.readUnsignedShort();
|
||||
final int nextStateInt = netStream.readVarInt();
|
||||
nextState = State.getById(nextStateInt).orElseThrow(() -> new UnknowState(nextStateInt));
|
||||
netStream.skipBytes(2); //TODO magic
|
||||
|
||||
size = lengthVarInt(id)
|
||||
+ lengthVarInt(protocolVersion)
|
||||
+ lengthString(serverAddress)
|
||||
+ lengthUnsignedShort(serverPort)
|
||||
+ lengthVarInt(nextState.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NetStream netStream) {
|
||||
throw new NotSupportException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,15 +5,13 @@
|
||||
package mc.core.netty.packets;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import mc.core.NetStream;
|
||||
import mc.core.NotSupportException;
|
||||
import mc.core.netty.NettyPacket;
|
||||
import mc.core.netty.WrapperByteBufNetStream;
|
||||
import mc.core.Packet;
|
||||
|
||||
import static mc.core.netty.Utils.*;
|
||||
|
||||
public class HandshakeResponsePacket implements NettyPacket {
|
||||
public class HandshakeResponsePacket implements Packet {
|
||||
private static final int id = 0;
|
||||
private static String json;
|
||||
private static int size;
|
||||
@@ -41,20 +39,6 @@ public class HandshakeResponsePacket implements NettyPacket {
|
||||
+ lengthString(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(ByteBuf byteBuf) {
|
||||
NetStream netStream = new WrapperByteBufNetStream(byteBuf);
|
||||
|
||||
netStream.writeVarInt(getSize());
|
||||
netStream.writeVarInt(getId());
|
||||
netStream.writeString(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillFromByteBuf(ByteBuf byteBuf) {
|
||||
throw new NotSupportException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return size;
|
||||
@@ -64,4 +48,16 @@ public class HandshakeResponsePacket implements NettyPacket {
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
throw new NotSupportException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NetStream netStream) {
|
||||
netStream.writeVarInt(getSize());
|
||||
netStream.writeVarInt(getId());
|
||||
netStream.writeString(json);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,41 +4,18 @@
|
||||
*/
|
||||
package mc.core.netty.packets;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.ToString;
|
||||
import mc.core.NetStream;
|
||||
import mc.core.netty.NettyPacket;
|
||||
import mc.core.netty.WrapperByteBufNetStream;
|
||||
import mc.core.Packet;
|
||||
|
||||
import static mc.core.netty.Utils.*;
|
||||
|
||||
@ToString
|
||||
public class PingPacket implements NettyPacket {
|
||||
public class PingPacket implements Packet {
|
||||
private static final int id = 1;
|
||||
private long payload;
|
||||
private int size;
|
||||
|
||||
@Override
|
||||
public void writeSelf(ByteBuf byteBuf) {
|
||||
NetStream netStream = new WrapperByteBufNetStream(byteBuf);
|
||||
|
||||
long payload = System.currentTimeMillis();
|
||||
int size = lengthVarLong(payload);
|
||||
|
||||
netStream.writeVarInt(size);
|
||||
netStream.writeVarInt(id);
|
||||
netStream.writeVarLong(payload);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillFromByteBuf(ByteBuf byteBuf) {
|
||||
NetStream netStream = new WrapperByteBufNetStream(byteBuf);
|
||||
|
||||
payload = netStream.readVarLong();
|
||||
size = lengthVarInt(id)
|
||||
+ lengthVarLong(payload);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return size;
|
||||
@@ -48,4 +25,21 @@ public class PingPacket implements NettyPacket {
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
payload = netStream.readVarLong();
|
||||
size = lengthVarInt(id)
|
||||
+ lengthVarLong(payload);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(NetStream netStream) {
|
||||
long payload = System.currentTimeMillis();
|
||||
int size = lengthVarLong(payload);
|
||||
|
||||
netStream.writeVarInt(size);
|
||||
netStream.writeVarInt(id);
|
||||
netStream.writeVarLong(payload);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user