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