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