HandshakeResponsePacket
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -41,6 +41,11 @@
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.16.16</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Netty -->
|
||||
<dependency>
|
||||
|
||||
@@ -4,14 +4,21 @@
|
||||
*/
|
||||
package mc.core.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
@ToString
|
||||
@Slf4j
|
||||
class BasePacket {
|
||||
private final int length;
|
||||
private final int id;
|
||||
|
||||
public void writeSelf(ByteBuf byteBuf) {
|
||||
log.warn("write not support");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,13 +12,13 @@ import static mc.core.netty.Utilits.*;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public class HandshakePacket extends BasePacket {
|
||||
public class HandshakeRequestPacket extends BasePacket {
|
||||
private final int protocolVersion;
|
||||
private final String serverAddress;
|
||||
private final int serverPort;
|
||||
private final int nextState;
|
||||
|
||||
public HandshakePacket(int length, int id, ByteBuf byteBuf) throws Exception {
|
||||
public HandshakeRequestPacket(int length, int id, ByteBuf byteBuf) throws Exception {
|
||||
super(length, id);
|
||||
|
||||
protocolVersion = readVarInt(byteBuf);
|
||||
48
src/main/java/mc/core/netty/HandshakeResponsePacket.java
Normal file
48
src/main/java/mc/core/netty/HandshakeResponsePacket.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core.netty;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import static mc.core.netty.Utilits.lengthVarInt;
|
||||
|
||||
public class HandshakeResponsePacket extends BasePacket {
|
||||
private static String json;
|
||||
|
||||
static {
|
||||
JsonObject versionObj = new JsonObject();
|
||||
versionObj.addProperty("name", "1.12.2");
|
||||
versionObj.addProperty("protocol", 340);
|
||||
|
||||
JsonObject playersObj = new JsonObject();
|
||||
playersObj.addProperty("max", 100);
|
||||
playersObj.addProperty("online", 15);
|
||||
|
||||
JsonObject descriptionObj = new JsonObject();
|
||||
descriptionObj.addProperty("text", "MC Core");
|
||||
|
||||
JsonObject rootObj = new JsonObject();
|
||||
rootObj.add("version", versionObj);
|
||||
rootObj.add("players", playersObj);
|
||||
rootObj.add("description", descriptionObj);
|
||||
|
||||
json = rootObj.toString();
|
||||
}
|
||||
|
||||
public HandshakeResponsePacket(int length, int id) {
|
||||
super(length, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelf(ByteBuf byteBuf) {
|
||||
Utilits.writeString(json, byteBuf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return lengthVarInt(getId()) + lengthVarInt(json.length()) + json.length();
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ public class NettyServer implements Server {
|
||||
protected void initChannel(SocketChannel socketChannel) {
|
||||
socketChannel.pipeline().addLast(
|
||||
new LoggingHandler(),
|
||||
new PacketEncoder(),
|
||||
new PacketDecoder(),
|
||||
new PacketHandler()
|
||||
);
|
||||
|
||||
@@ -19,7 +19,7 @@ public class PacketDecoder extends ReplayingDecoder<BasePacket> {
|
||||
int id = readVarInt(byteBuf);
|
||||
|
||||
if (id == 0) {
|
||||
list.add(new HandshakePacket(length, id, byteBuf));
|
||||
list.add(new HandshakeRequestPacket(length, id, byteBuf));
|
||||
} else {
|
||||
list.add(new BasePacket(length, id));
|
||||
byteBuf.skipBytes(length);
|
||||
|
||||
20
src/main/java/mc/core/netty/PacketEncoder.java
Normal file
20
src/main/java/mc/core/netty/PacketEncoder.java
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core.netty;
|
||||
|
||||
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> {
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext context, BasePacket packet, ByteBuf byteBuf) throws Exception {
|
||||
writeVarInt(packet.getLength(), byteBuf);
|
||||
writeVarInt(packet.getId(), byteBuf);
|
||||
packet.writeSelf(byteBuf);
|
||||
}
|
||||
}
|
||||
@@ -15,5 +15,9 @@ public class PacketHandler extends SimpleChannelInboundHandler<BasePacket> {
|
||||
log.info("Packet: {} | Data: {}",
|
||||
packet.getClass().getSimpleName(),
|
||||
packet.toString());
|
||||
|
||||
if (packet.getClass().getSimpleName().equals("HandshakeRequestPacket")) {
|
||||
context.channel().writeAndFlush(new HandshakeResponsePacket(0, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,29 @@ public class Utilits {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void writeVarInt(int value, ByteBuf byteBuf) {
|
||||
do {
|
||||
byte temp = (byte)(value & 0b01111111);
|
||||
value >>>= 7;
|
||||
if (value != 0) {
|
||||
temp |= 0b10000000;
|
||||
}
|
||||
|
||||
byteBuf.writeByte(temp);
|
||||
} while (value != 0);
|
||||
}
|
||||
|
||||
public static int lengthVarInt(int value) {
|
||||
int result = 0;
|
||||
|
||||
do {
|
||||
value >>>= 7;
|
||||
result++;
|
||||
} while (value != 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String readString(ByteBuf byteBuf) throws Exception {
|
||||
int length = readVarInt(byteBuf);
|
||||
byte[] buffer = new byte[length];
|
||||
@@ -42,4 +65,9 @@ public class Utilits {
|
||||
public static int readUnsignedShort(ByteBuf byteBuf) {
|
||||
return byteBuf.readUnsignedShort();
|
||||
}
|
||||
|
||||
public static void writeString(String value, ByteBuf byteBuf) {
|
||||
writeVarInt(value.length(), byteBuf);
|
||||
byteBuf.writeBytes(value.getBytes());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user