Refactor base packet class

This commit is contained in:
Melledy
2023-11-29 22:58:15 -08:00
parent 3981caeb81
commit f801b442a4

View File

@@ -3,41 +3,28 @@ package emu.lunarcore.server.packet;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import lombok.Getter;
import us.hebi.quickbuf.ProtoMessage; import us.hebi.quickbuf.ProtoMessage;
@Getter
public class BasePacket { public class BasePacket {
public static final int HEADER_CONST = 0x9d74c714; public static final int HEADER_CONST = 0x9d74c714;
public static final int TAIL_CONST = 0xd7a152c8; public static final int TAIL_CONST = 0xd7a152c8;
private int opcode; private int cmdId;
private byte[] data; private byte[] data;
// Encryption public BasePacket(int cmdId) {
private boolean useDispatchKey; this.cmdId = cmdId;
public boolean shouldEncrypt = true;
public BasePacket(int opcode) {
this.opcode = opcode;
} }
public int getOpcode() { public BasePacket(int cmdId, byte[] data) {
return opcode; this.cmdId = cmdId;
this.data = data;
} }
public void setOpcode(int opcode) { public void setOpcode(int opcode) {
this.opcode = opcode; this.cmdId = opcode;
}
public boolean useDispatchKey() {
return useDispatchKey;
}
public void setUseDispatchKey(boolean useDispatchKey) {
this.useDispatchKey = useDispatchKey;
}
public byte[] getData() {
return data;
} }
public void setData(byte[] data) { public void setData(byte[] data) {
@@ -56,7 +43,7 @@ public class BasePacket {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4 + 2 + 4 + getData().length + 4); ByteArrayOutputStream baos = new ByteArrayOutputStream(4 + 2 + 4 + getData().length + 4);
this.writeUint32(baos, HEADER_CONST); this.writeUint32(baos, HEADER_CONST);
this.writeUint16(baos, opcode); this.writeUint16(baos, cmdId);
this.writeUint16(baos, 0); // Empty header this.writeUint16(baos, 0); // Empty header
this.writeUint32(baos, data.length); this.writeUint32(baos, data.length);
this.writeBytes(baos, data); this.writeBytes(baos, data);
@@ -67,13 +54,13 @@ public class BasePacket {
return packet; return packet;
} }
public void writeUint16(ByteArrayOutputStream baos, int i) { private void writeUint16(ByteArrayOutputStream baos, int i) {
// Unsigned short // Unsigned short
baos.write((byte) ((i >>> 8) & 0xFF)); baos.write((byte) ((i >>> 8) & 0xFF));
baos.write((byte) (i & 0xFF)); baos.write((byte) (i & 0xFF));
} }
public void writeUint32(ByteArrayOutputStream baos, int i) { private void writeUint32(ByteArrayOutputStream baos, int i) {
// Unsigned int (long) // Unsigned int (long)
baos.write((byte) ((i >>> 24) & 0xFF)); baos.write((byte) ((i >>> 24) & 0xFF));
baos.write((byte) ((i >>> 16) & 0xFF)); baos.write((byte) ((i >>> 16) & 0xFF));
@@ -81,7 +68,7 @@ public class BasePacket {
baos.write((byte) (i & 0xFF)); baos.write((byte) (i & 0xFF));
} }
public void writeBytes(ByteArrayOutputStream baos, byte[] bytes) { private void writeBytes(ByteArrayOutputStream baos, byte[] bytes) {
try { try {
baos.write(bytes); baos.write(bytes);
} catch (IOException e) { } catch (IOException e) {