refactory
This commit is contained in:
8
src/main/java/mc/core/NotSupportException.java
Normal file
8
src/main/java/mc/core/NotSupportException.java
Normal file
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core;
|
||||
|
||||
public class NotSupportException extends RuntimeException {
|
||||
}
|
||||
10
src/main/java/mc/core/Packet.java
Normal file
10
src/main/java/mc/core/Packet.java
Normal file
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core;
|
||||
|
||||
public interface Packet {
|
||||
int getSize();
|
||||
int getId();
|
||||
}
|
||||
@@ -7,23 +7,46 @@ package mc.core.netty;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
import mc.core.NotSupportException;
|
||||
|
||||
import static mc.core.netty.Utilits.*;
|
||||
import static mc.core.netty.Utils.*;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public class HandshakeRequestPacket extends BasePacket {
|
||||
private final int protocolVersion;
|
||||
private final String serverAddress;
|
||||
private final int serverPort;
|
||||
private final int nextState;
|
||||
public class HandshakeRequestPacket implements NettyPacket {
|
||||
private static final int id = 0;
|
||||
private int protocolVersion;
|
||||
private String serverAddress;
|
||||
private int serverPort;
|
||||
private int nextState;
|
||||
private int size;
|
||||
|
||||
public HandshakeRequestPacket(int length, int id, ByteBuf byteBuf) throws Exception {
|
||||
super(length, id);
|
||||
@Override
|
||||
public void writeSelf(ByteBuf byteBuf) {
|
||||
throw new NotSupportException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillFromByteBuf(ByteBuf byteBuf) {
|
||||
protocolVersion = readVarInt(byteBuf);
|
||||
serverAddress = readString(byteBuf);
|
||||
serverPort = readUnsignedShort(byteBuf);
|
||||
nextState = readVarInt(byteBuf);
|
||||
|
||||
size = lengthVarInt(id)
|
||||
+ lengthVarInt(protocolVersion)
|
||||
+ lengthString(serverAddress)
|
||||
+ lengthVarInt(serverPort)
|
||||
+ lengthVarInt(nextState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,14 @@ package mc.core.netty;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import mc.core.NotSupportException;
|
||||
|
||||
import static mc.core.netty.Utilits.lengthVarInt;
|
||||
import static mc.core.netty.Utils.*;
|
||||
|
||||
public class HandshakeResponsePacket extends BasePacket {
|
||||
public class HandshakeResponsePacket implements NettyPacket {
|
||||
private static final int id = 0;
|
||||
private static String json;
|
||||
private static int size;
|
||||
|
||||
static {
|
||||
JsonObject versionObj = new JsonObject();
|
||||
@@ -30,19 +33,30 @@ public class HandshakeResponsePacket extends BasePacket {
|
||||
rootObj.add("description", descriptionObj);
|
||||
|
||||
json = rootObj.toString();
|
||||
}
|
||||
|
||||
public HandshakeResponsePacket(int length, int id) {
|
||||
super(length, id);
|
||||
size = lengthVarInt(id)
|
||||
+ lengthString(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(ByteBuf byteBuf) {
|
||||
Utilits.writeString(json, byteBuf);
|
||||
writeVarInt(getSize(), byteBuf);
|
||||
writeVarInt(getId(), byteBuf);
|
||||
writeString(json, byteBuf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return lengthVarInt(getId()) + lengthVarInt(json.length()) + json.length();
|
||||
public void fillFromByteBuf(ByteBuf byteBuf) {
|
||||
throw new NotSupportException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
13
src/main/java/mc/core/netty/NettyPacket.java
Normal file
13
src/main/java/mc/core/netty/NettyPacket.java
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
@@ -10,18 +10,20 @@ import io.netty.handler.codec.ReplayingDecoder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static mc.core.netty.Utilits.readVarInt;
|
||||
import static mc.core.netty.Utils.readVarInt;
|
||||
|
||||
public class PacketDecoder extends ReplayingDecoder<BasePacket> {
|
||||
public class PacketDecoder extends ReplayingDecoder<NettyPacket> {
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext context, ByteBuf byteBuf, List<Object> list) throws Exception {
|
||||
int length = readVarInt(byteBuf);
|
||||
int id = readVarInt(byteBuf);
|
||||
|
||||
if (id == 0) {
|
||||
list.add(new HandshakeRequestPacket(length, id, byteBuf));
|
||||
NettyPacket packet = new HandshakeRequestPacket();
|
||||
packet.fillFromByteBuf(byteBuf);
|
||||
list.add(packet);
|
||||
} else {
|
||||
list.add(new BasePacket(length, id));
|
||||
list.add(new UnknownPacket(length, id));
|
||||
byteBuf.skipBytes(length);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,13 +8,9 @@ import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
|
||||
import static mc.core.netty.Utilits.writeVarInt;
|
||||
|
||||
public class PacketEncoder extends MessageToByteEncoder<BasePacket> {
|
||||
public class PacketEncoder extends MessageToByteEncoder<NettyPacket> {
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext context, BasePacket packet, ByteBuf byteBuf) throws Exception {
|
||||
writeVarInt(packet.getLength(), byteBuf);
|
||||
writeVarInt(packet.getId(), byteBuf);
|
||||
protected void encode(ChannelHandlerContext context, NettyPacket packet, ByteBuf byteBuf) throws Exception {
|
||||
packet.writeSelf(byteBuf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,16 +8,18 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import static mc.core.netty.Utils.equalsPacket;
|
||||
|
||||
@Slf4j
|
||||
public class PacketHandler extends SimpleChannelInboundHandler<BasePacket> {
|
||||
public class PacketHandler extends SimpleChannelInboundHandler<NettyPacket> {
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext context, BasePacket packet) throws Exception {
|
||||
protected void channelRead0(ChannelHandlerContext context, NettyPacket packet) throws Exception {
|
||||
log.info("Packet: {} | Data: {}",
|
||||
packet.getClass().getSimpleName(),
|
||||
packet.toString());
|
||||
|
||||
if (packet.getClass().getSimpleName().equals("HandshakeRequestPacket")) {
|
||||
context.channel().writeAndFlush(new HandshakeResponsePacket(0, 0));
|
||||
if (equalsPacket(packet, "HandshakeRequestPacket")) {
|
||||
context.channel().writeAndFlush(new HandshakeResponsePacket());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,17 +8,26 @@ import io.netty.buffer.ByteBuf;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.NotSupportException;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
@ToString
|
||||
@Slf4j
|
||||
class BasePacket {
|
||||
class UnknownPacket implements NettyPacket {
|
||||
private final int length;
|
||||
private final int id;
|
||||
|
||||
public void writeSelf(ByteBuf byteBuf) {
|
||||
log.warn("write not support");
|
||||
throw new NotSupportException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillFromByteBuf(ByteBuf byteBuf) {
|
||||
throw new NotSupportException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return length;
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,13 @@
|
||||
package mc.core.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class Utilits {
|
||||
public static int readVarInt(ByteBuf byteBuf) throws Exception {
|
||||
@Slf4j
|
||||
public class Utils {
|
||||
public static int readVarInt(ByteBuf byteBuf) {
|
||||
int result = 0;
|
||||
byte read;
|
||||
int numRead = 0;
|
||||
@@ -21,7 +23,8 @@ public class Utilits {
|
||||
|
||||
numRead++;
|
||||
if (numRead > 5) {
|
||||
throw new Exception("VarInt is too big");
|
||||
log.warn("VarInt is too big");
|
||||
break;
|
||||
}
|
||||
} while ((read & 0b10000000) != 0);
|
||||
|
||||
@@ -51,7 +54,11 @@ public class Utilits {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String readString(ByteBuf byteBuf) throws Exception {
|
||||
public static int lengthString(String value) {
|
||||
return lengthVarInt(value.length()) + value.length();
|
||||
}
|
||||
|
||||
public static String readString(ByteBuf byteBuf) {
|
||||
int length = readVarInt(byteBuf);
|
||||
byte[] buffer = new byte[length];
|
||||
int i = 0;
|
||||
@@ -70,4 +77,8 @@ public class Utilits {
|
||||
writeVarInt(value.length(), byteBuf);
|
||||
byteBuf.writeBytes(value.getBytes());
|
||||
}
|
||||
|
||||
public static boolean equalsPacket(NettyPacket packet, String name) {
|
||||
return packet.getClass().getSimpleName().equals(name);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user