Merge pull request #420 from ryleykimmel/netty-resource-leak-fix

Allow Netty to automatically maintain our internal usages of ByteBuf
This commit is contained in:
Major
2019-03-29 22:36:46 +00:00
committed by GitHub
8 changed files with 33 additions and 51 deletions
@@ -47,7 +47,7 @@ public final class GroupedRegionUpdateMessageEncoder extends MessageEncoder<Grou
GamePacket packet = encoder.encode(update); GamePacket packet = encoder.encode(update);
builder.put(DataType.BYTE, packet.getOpcode()); builder.put(DataType.BYTE, packet.getOpcode());
builder.putBytes(packet.getPayload()); builder.putBytes(packet.content());
} }
return builder.toGamePacket(); return builder.toGamePacket();
@@ -13,7 +13,7 @@ public final class SpamPacketMessageDecoder extends MessageDecoder<SpamPacketMes
@Override @Override
public SpamPacketMessage decode(GamePacket packet) { public SpamPacketMessage decode(GamePacket packet) {
return new SpamPacketMessage(packet.getPayload().array()); return new SpamPacketMessage(packet.content().array());
} }
} }
@@ -47,7 +47,7 @@ public final class GroupedRegionUpdateMessageEncoder extends MessageEncoder<Grou
GamePacket packet = encoder.encode(update); GamePacket packet = encoder.encode(update);
builder.put(DataType.BYTE, packet.getOpcode()); builder.put(DataType.BYTE, packet.getOpcode());
builder.putBytes(packet.getPayload()); builder.putBytes(packet.content());
} }
return builder.toGamePacket(); return builder.toGamePacket();
@@ -13,7 +13,7 @@ public final class SpamPacketMessageDecoder extends MessageDecoder<SpamPacketMes
@Override @Override
public SpamPacketMessage decode(GamePacket packet) { public SpamPacketMessage decode(GamePacket packet) {
return new SpamPacketMessage(packet.getPayload().array()); return new SpamPacketMessage(packet.content().array());
} }
} }
@@ -70,37 +70,32 @@ public final class ApolloHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception {
try { Channel channel = ctx.channel();
Channel channel = ctx.channel(); Attribute<Session> attribute = channel.attr(ApolloHandler.SESSION_KEY);
Attribute<Session> attribute = channel.attr(ApolloHandler.SESSION_KEY); Session session = attribute.get();
Session session = attribute.get();
if (message instanceof HttpRequest || message instanceof JagGrabRequest) { if (message instanceof HttpRequest || message instanceof JagGrabRequest) {
session = new UpdateSession(channel, serverContext); session = new UpdateSession(channel, serverContext);
}
if (session != null) {
session.messageReceived(message);
return;
}
// TODO: Perhaps let HandshakeMessage implement Message to remove this explicit check
if (message instanceof HandshakeMessage) {
HandshakeMessage handshakeMessage = (HandshakeMessage) message;
switch (handshakeMessage.getServiceId()) {
case HandshakeConstants.SERVICE_GAME:
attribute.set(new LoginSession(channel, serverContext));
break;
case HandshakeConstants.SERVICE_UPDATE:
attribute.set(new UpdateSession(channel, serverContext));
break;
} }
if (session != null) {
session.messageReceived(message);
return;
}
// TODO: Perhaps let HandshakeMessage implement Message to remove this explicit check
if (message instanceof HandshakeMessage) {
HandshakeMessage handshakeMessage = (HandshakeMessage) message;
switch (handshakeMessage.getServiceId()) {
case HandshakeConstants.SERVICE_GAME:
attribute.set(new LoginSession(channel, serverContext));
break;
case HandshakeConstants.SERVICE_UPDATE:
attribute.set(new UpdateSession(channel, serverContext));
break;
}
}
} finally {
ReferenceCountUtil.release(message);
} }
} }
@@ -2,6 +2,7 @@ package org.apollo.net.codec.game;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.DefaultByteBufHolder;
import org.apollo.net.meta.PacketType; import org.apollo.net.meta.PacketType;
/** /**
@@ -9,7 +10,7 @@ import org.apollo.net.meta.PacketType;
* *
* @author Graham * @author Graham
*/ */
public final class GamePacket { public final class GamePacket extends DefaultByteBufHolder {
/** /**
* The length. * The length.
@@ -21,11 +22,6 @@ public final class GamePacket {
*/ */
private final int opcode; private final int opcode;
/**
* The payload.
*/
private final ByteBuf payload;
/** /**
* The packet type. * The packet type.
*/ */
@@ -39,10 +35,10 @@ public final class GamePacket {
* @param payload The payload. * @param payload The payload.
*/ */
public GamePacket(int opcode, PacketType type, ByteBuf payload) { public GamePacket(int opcode, PacketType type, ByteBuf payload) {
super(payload);
this.opcode = opcode; this.opcode = opcode;
this.type = type; this.type = type;
length = payload.readableBytes(); length = payload.readableBytes();
this.payload = payload;
} }
/** /**
@@ -63,15 +59,6 @@ public final class GamePacket {
return opcode; return opcode;
} }
/**
* Gets the payload.
*
* @return The payload.
*/
public ByteBuf getPayload() {
return payload;
}
/** /**
* Gets the packet type. * Gets the packet type.
* *
@@ -44,7 +44,7 @@ public final class GamePacketEncoder extends MessageToByteEncoder<GamePacket> {
} else if (type == PacketType.VARIABLE_SHORT) { } else if (type == PacketType.VARIABLE_SHORT) {
out.writeShort(payloadLength); out.writeShort(payloadLength);
} }
out.writeBytes(packet.getPayload()); out.writeBytes(packet.content());
} }
} }
@@ -34,7 +34,7 @@ public final class GamePacketReader {
* @param packet The packet. * @param packet The packet.
*/ */
public GamePacketReader(GamePacket packet) { public GamePacketReader(GamePacket packet) {
buffer = packet.getPayload(); buffer = packet.content();
} }
/** /**