Handshake packet
This commit is contained in:
@@ -6,9 +6,11 @@ package mc.core.netty;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
@ToString
|
||||
class BasePacket {
|
||||
private final int length;
|
||||
private final int id;
|
||||
|
||||
29
src/main/java/mc/core/netty/HandshakePacket.java
Normal file
29
src/main/java/mc/core/netty/HandshakePacket.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
import static mc.core.netty.Utilits.*;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public class HandshakePacket 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 {
|
||||
super(length, id);
|
||||
|
||||
protocolVersion = readVarInt(byteBuf);
|
||||
serverAddress = readString(byteBuf);
|
||||
serverPort = readUnsignedShort(byteBuf);
|
||||
nextState = readVarInt(byteBuf);
|
||||
}
|
||||
}
|
||||
@@ -10,32 +10,19 @@ import io.netty.handler.codec.ReplayingDecoder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static mc.core.netty.Utilits.readVarInt;
|
||||
|
||||
public class PacketDecoder extends ReplayingDecoder<BasePacket> {
|
||||
private int readVarInt(ByteBuf byteBuf) throws Exception {
|
||||
int result = 0;
|
||||
byte read;
|
||||
int numRead = 0;
|
||||
|
||||
do {
|
||||
read = byteBuf.readByte();
|
||||
int value = (read & 0b01111111);
|
||||
result |= (value << (7 * numRead));
|
||||
|
||||
numRead++;
|
||||
if (numRead > 5) {
|
||||
throw new Exception("VarInt is too big");
|
||||
}
|
||||
} while ((read & 0b10000000) != 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@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 HandshakePacket(length, id, byteBuf));
|
||||
} else {
|
||||
list.add(new BasePacket(length, id));
|
||||
byteBuf.skipBytes(length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
public class PacketHandler extends SimpleChannelInboundHandler<BasePacket> {
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext context, BasePacket packet) throws Exception {
|
||||
log.info("Lenght: {} | Id: {}",
|
||||
packet.getLength(),
|
||||
packet.getId());
|
||||
log.info("Packet: {} | Data: {}",
|
||||
packet.getClass().getSimpleName(),
|
||||
packet.toString());
|
||||
}
|
||||
}
|
||||
|
||||
45
src/main/java/mc/core/netty/Utilits.java
Normal file
45
src/main/java/mc/core/netty/Utilits.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class Utilits {
|
||||
public static int readVarInt(ByteBuf byteBuf) throws Exception {
|
||||
int result = 0;
|
||||
byte read;
|
||||
int numRead = 0;
|
||||
|
||||
do {
|
||||
read = byteBuf.readByte();
|
||||
int value = (read & 0b01111111);
|
||||
result |= (value << (7 * numRead));
|
||||
|
||||
numRead++;
|
||||
if (numRead > 5) {
|
||||
throw new Exception("VarInt is too big");
|
||||
}
|
||||
} while ((read & 0b10000000) != 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String readString(ByteBuf byteBuf) throws Exception {
|
||||
int length = readVarInt(byteBuf);
|
||||
byte[] buffer = new byte[length];
|
||||
int i = 0;
|
||||
do {
|
||||
buffer[i++] = byteBuf.readByte();
|
||||
} while (i < length);
|
||||
|
||||
return new String(buffer, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static int readUnsignedShort(ByteBuf byteBuf) {
|
||||
return byteBuf.readUnsignedShort();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user