Tweak OutPacket
This commit is contained in:
@@ -33,6 +33,7 @@ import net.netty.InvalidPacketHeaderException;
|
|||||||
import net.packet.ByteBufOutPacket;
|
import net.packet.ByteBufOutPacket;
|
||||||
import net.packet.InPacket;
|
import net.packet.InPacket;
|
||||||
import net.packet.OutPacket;
|
import net.packet.OutPacket;
|
||||||
|
import net.packet.Packet;
|
||||||
import net.packet.logging.LoggingUtil;
|
import net.packet.logging.LoggingUtil;
|
||||||
import net.packet.logging.MapleLogger;
|
import net.packet.logging.MapleLogger;
|
||||||
import net.server.Server;
|
import net.server.Server;
|
||||||
@@ -1467,10 +1468,10 @@ public class MapleClient extends ChannelInboundHandlerAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendPacket(OutPacket outPacket) {
|
public void sendPacket(Packet packet) {
|
||||||
announcerLock.lock();
|
announcerLock.lock();
|
||||||
try {
|
try {
|
||||||
ioChannel.writeAndFlush(outPacket.getBytes());
|
ioChannel.writeAndFlush(packet.getBytes());
|
||||||
} finally {
|
} finally {
|
||||||
announcerLock.unlock();
|
announcerLock.unlock();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package net.encryption;
|
|||||||
import io.netty.buffer.ByteBuf;
|
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 net.packet.OutPacket;
|
import net.packet.Packet;
|
||||||
|
|
||||||
public class PacketEncoder extends MessageToByteEncoder<OutPacket> {
|
public class PacketEncoder extends MessageToByteEncoder<Packet> {
|
||||||
private final MapleAESOFB sendCypher;
|
private final MapleAESOFB sendCypher;
|
||||||
|
|
||||||
public PacketEncoder(MapleAESOFB sendCypher) {
|
public PacketEncoder(MapleAESOFB sendCypher) {
|
||||||
@@ -13,7 +13,7 @@ public class PacketEncoder extends MessageToByteEncoder<OutPacket> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void encode(ChannelHandlerContext ctx, OutPacket in, ByteBuf out) {
|
protected void encode(ChannelHandlerContext ctx, Packet in, ByteBuf out) {
|
||||||
byte[] packet = in.getBytes();
|
byte[] packet = in.getBytes();
|
||||||
out.writeBytes(getEncodedHeader(packet.length));
|
out.writeBytes(getEncodedHeader(packet.length));
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import java.awt.*;
|
|||||||
@NotThreadSafe
|
@NotThreadSafe
|
||||||
public class ByteBufOutPacket implements OutPacket {
|
public class ByteBufOutPacket implements OutPacket {
|
||||||
private final ByteBuf byteBuf;
|
private final ByteBuf byteBuf;
|
||||||
|
private byte[] bytes;
|
||||||
|
|
||||||
@Deprecated(forRemoval = true)
|
@Deprecated(forRemoval = true)
|
||||||
public ByteBufOutPacket() {
|
public ByteBufOutPacket() {
|
||||||
@@ -31,7 +32,12 @@ public class ByteBufOutPacket implements OutPacket {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] getBytes() {
|
public byte[] getBytes() {
|
||||||
return ByteBufUtil.getBytes(byteBuf);
|
if (bytes == null) {
|
||||||
|
// Avoid creating new byte arrays when the packet is broadcast
|
||||||
|
bytes = ByteBufUtil.getBytes(byteBuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -65,7 +71,7 @@ public class ByteBufOutPacket implements OutPacket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeBoolean(boolean value) {
|
public void writeBool(boolean value) {
|
||||||
byteBuf.writeByte(value ? 1 : 0);
|
byteBuf.writeByte(value ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package net.packet;
|
package net.packet;
|
||||||
|
|
||||||
|
import net.opcodes.SendOpcode;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
public interface OutPacket extends Packet {
|
public interface OutPacket extends Packet {
|
||||||
@@ -9,8 +11,12 @@ public interface OutPacket extends Packet {
|
|||||||
void writeShort(int value);
|
void writeShort(int value);
|
||||||
void writeInt(int value);
|
void writeInt(int value);
|
||||||
void writeLong(long value);
|
void writeLong(long value);
|
||||||
void writeBoolean(boolean value);
|
void writeBool(boolean value);
|
||||||
void writeString(String value);
|
void writeString(String value);
|
||||||
void writePoint(Point value);
|
void writePoint(Point value);
|
||||||
void skip(int numberOfBytes);
|
void skip(int numberOfBytes);
|
||||||
|
|
||||||
|
static OutPacket create(SendOpcode opcode) {
|
||||||
|
return new ByteBufOutPacket(opcode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ class ByteBufOutPacketTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void writeBoolean_true() {
|
void writeBoolean_true() {
|
||||||
outPacket.writeBoolean(true);
|
outPacket.writeBool(true);
|
||||||
|
|
||||||
ByteBuf wrapped = wrapExplicitlyWrittenBytes(outPacket);
|
ByteBuf wrapped = wrapExplicitlyWrittenBytes(outPacket);
|
||||||
byte readByte = wrapped.readByte();
|
byte readByte = wrapped.readByte();
|
||||||
@@ -153,7 +153,7 @@ class ByteBufOutPacketTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void writeBoolean_false() {
|
void writeBoolean_false() {
|
||||||
outPacket.writeBoolean(false);
|
outPacket.writeBool(false);
|
||||||
|
|
||||||
ByteBuf wrapped = wrapExplicitlyWrittenBytes(outPacket);
|
ByteBuf wrapped = wrapExplicitlyWrittenBytes(outPacket);
|
||||||
byte readByte = wrapped.readByte();
|
byte readByte = wrapped.readByte();
|
||||||
@@ -203,4 +203,15 @@ class ByteBufOutPacketTest {
|
|||||||
assertEquals(0, wrapped.readByte());
|
assertEquals(0, wrapped.readByte());
|
||||||
assertEquals(secondWrittenByte, wrapped.readByte());
|
assertEquals(secondWrittenByte, wrapped.readByte());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenGettingBytesRepeatedly_bytesShouldBeLockedInPlace() {
|
||||||
|
outPacket.writeByte(1);
|
||||||
|
byte[] initialWrite = outPacket.getBytes();
|
||||||
|
|
||||||
|
outPacket.writeByte(2);
|
||||||
|
byte[] afterWritingAgain = outPacket.getBytes();
|
||||||
|
|
||||||
|
assertArrayEquals(initialWrite, afterWritingAgain);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user