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 io.netty.buffer.ByteBuf;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
import mc.core.NotSupportException;
|
||||||
|
|
||||||
import static mc.core.netty.Utilits.*;
|
import static mc.core.netty.Utils.*;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ToString
|
@ToString
|
||||||
public class HandshakeRequestPacket extends BasePacket {
|
public class HandshakeRequestPacket implements NettyPacket {
|
||||||
private final int protocolVersion;
|
private static final int id = 0;
|
||||||
private final String serverAddress;
|
private int protocolVersion;
|
||||||
private final int serverPort;
|
private String serverAddress;
|
||||||
private final int nextState;
|
private int serverPort;
|
||||||
|
private int nextState;
|
||||||
|
private int size;
|
||||||
|
|
||||||
public HandshakeRequestPacket(int length, int id, ByteBuf byteBuf) throws Exception {
|
@Override
|
||||||
super(length, id);
|
public void writeSelf(ByteBuf byteBuf) {
|
||||||
|
throw new NotSupportException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillFromByteBuf(ByteBuf byteBuf) {
|
||||||
protocolVersion = readVarInt(byteBuf);
|
protocolVersion = readVarInt(byteBuf);
|
||||||
serverAddress = readString(byteBuf);
|
serverAddress = readString(byteBuf);
|
||||||
serverPort = readUnsignedShort(byteBuf);
|
serverPort = readUnsignedShort(byteBuf);
|
||||||
nextState = readVarInt(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 com.google.gson.JsonObject;
|
||||||
import io.netty.buffer.ByteBuf;
|
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 String json;
|
||||||
|
private static int size;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
JsonObject versionObj = new JsonObject();
|
JsonObject versionObj = new JsonObject();
|
||||||
@@ -30,19 +33,30 @@ public class HandshakeResponsePacket extends BasePacket {
|
|||||||
rootObj.add("description", descriptionObj);
|
rootObj.add("description", descriptionObj);
|
||||||
|
|
||||||
json = rootObj.toString();
|
json = rootObj.toString();
|
||||||
}
|
|
||||||
|
|
||||||
public HandshakeResponsePacket(int length, int id) {
|
size = lengthVarInt(id)
|
||||||
super(length, id);
|
+ lengthString(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeSelf(ByteBuf byteBuf) {
|
public void writeSelf(ByteBuf byteBuf) {
|
||||||
Utilits.writeString(json, byteBuf);
|
writeVarInt(getSize(), byteBuf);
|
||||||
|
writeVarInt(getId(), byteBuf);
|
||||||
|
writeString(json, byteBuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getLength() {
|
public void fillFromByteBuf(ByteBuf byteBuf) {
|
||||||
return lengthVarInt(getId()) + lengthVarInt(json.length()) + json.length();
|
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 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
|
@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 {
|
||||||
int length = readVarInt(byteBuf);
|
int length = readVarInt(byteBuf);
|
||||||
int id = readVarInt(byteBuf);
|
int id = readVarInt(byteBuf);
|
||||||
|
|
||||||
if (id == 0) {
|
if (id == 0) {
|
||||||
list.add(new HandshakeRequestPacket(length, id, byteBuf));
|
NettyPacket packet = new HandshakeRequestPacket();
|
||||||
|
packet.fillFromByteBuf(byteBuf);
|
||||||
|
list.add(packet);
|
||||||
} else {
|
} else {
|
||||||
list.add(new BasePacket(length, id));
|
list.add(new UnknownPacket(length, id));
|
||||||
byteBuf.skipBytes(length);
|
byteBuf.skipBytes(length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,13 +8,9 @@ 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 static mc.core.netty.Utilits.writeVarInt;
|
public class PacketEncoder extends MessageToByteEncoder<NettyPacket> {
|
||||||
|
|
||||||
public class PacketEncoder extends MessageToByteEncoder<BasePacket> {
|
|
||||||
@Override
|
@Override
|
||||||
protected void encode(ChannelHandlerContext context, BasePacket packet, ByteBuf byteBuf) throws Exception {
|
protected void encode(ChannelHandlerContext context, NettyPacket packet, ByteBuf byteBuf) throws Exception {
|
||||||
writeVarInt(packet.getLength(), byteBuf);
|
|
||||||
writeVarInt(packet.getId(), byteBuf);
|
|
||||||
packet.writeSelf(byteBuf);
|
packet.writeSelf(byteBuf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,16 +8,18 @@ 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 static mc.core.netty.Utils.equalsPacket;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class PacketHandler extends SimpleChannelInboundHandler<BasePacket> {
|
public class PacketHandler extends SimpleChannelInboundHandler<NettyPacket> {
|
||||||
@Override
|
@Override
|
||||||
protected void channelRead0(ChannelHandlerContext context, BasePacket packet) throws Exception {
|
protected void channelRead0(ChannelHandlerContext context, NettyPacket packet) throws Exception {
|
||||||
log.info("Packet: {} | Data: {}",
|
log.info("Packet: {} | Data: {}",
|
||||||
packet.getClass().getSimpleName(),
|
packet.getClass().getSimpleName(),
|
||||||
packet.toString());
|
packet.toString());
|
||||||
|
|
||||||
if (packet.getClass().getSimpleName().equals("HandshakeRequestPacket")) {
|
if (equalsPacket(packet, "HandshakeRequestPacket")) {
|
||||||
context.channel().writeAndFlush(new HandshakeResponsePacket(0, 0));
|
context.channel().writeAndFlush(new HandshakeResponsePacket());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,17 +8,26 @@ import io.netty.buffer.ByteBuf;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import mc.core.NotSupportException;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
@ToString
|
@ToString
|
||||||
@Slf4j
|
class UnknownPacket implements NettyPacket {
|
||||||
class BasePacket {
|
|
||||||
private final int length;
|
private final int length;
|
||||||
private final int id;
|
private final int id;
|
||||||
|
|
||||||
public void writeSelf(ByteBuf byteBuf) {
|
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;
|
package mc.core.netty;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class Utilits {
|
@Slf4j
|
||||||
public static int readVarInt(ByteBuf byteBuf) throws Exception {
|
public class Utils {
|
||||||
|
public static int readVarInt(ByteBuf byteBuf) {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
byte read;
|
byte read;
|
||||||
int numRead = 0;
|
int numRead = 0;
|
||||||
@@ -21,7 +23,8 @@ public class Utilits {
|
|||||||
|
|
||||||
numRead++;
|
numRead++;
|
||||||
if (numRead > 5) {
|
if (numRead > 5) {
|
||||||
throw new Exception("VarInt is too big");
|
log.warn("VarInt is too big");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} while ((read & 0b10000000) != 0);
|
} while ((read & 0b10000000) != 0);
|
||||||
|
|
||||||
@@ -51,7 +54,11 @@ public class Utilits {
|
|||||||
return result;
|
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);
|
int length = readVarInt(byteBuf);
|
||||||
byte[] buffer = new byte[length];
|
byte[] buffer = new byte[length];
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@@ -70,4 +77,8 @@ public class Utilits {
|
|||||||
writeVarInt(value.length(), byteBuf);
|
writeVarInt(value.length(), byteBuf);
|
||||||
byteBuf.writeBytes(value.getBytes());
|
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