Log packets in dedicated handlers instead of in encoder/decoder
This commit is contained in:
47
src/main/java/net/packet/logging/InPacketLogger.java
Normal file
47
src/main/java/net/packet/logging/InPacketLogger.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package net.packet.logging;
|
||||
|
||||
import constants.net.OpcodeConstants;
|
||||
import io.netty.channel.ChannelHandler.Sharable;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import net.packet.InPacket;
|
||||
import net.packet.Packet;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import tools.HexTool;
|
||||
|
||||
@Sharable
|
||||
public class InPacketLogger extends ChannelInboundHandlerAdapter implements PacketLogger {
|
||||
private static final Logger log = LoggerFactory.getLogger(InPacketLogger.class);
|
||||
private static final int LOG_CONTENT_THRESHOLD = 3_000;
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
||||
if (msg instanceof InPacket packet) {
|
||||
log(packet);
|
||||
}
|
||||
|
||||
ctx.fireChannelRead(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(Packet packet) {
|
||||
final byte[] content = packet.getBytes();
|
||||
final int packetLength = content.length;
|
||||
|
||||
if (packetLength <= LOG_CONTENT_THRESHOLD) {
|
||||
final short opcode = LoggingUtil.readFirstShort(content);
|
||||
final String opcodeHex = Integer.toHexString(opcode).toUpperCase();
|
||||
final String opcodeName = getRecvOpcodeName(opcode);
|
||||
final String prefix = opcodeName == null ? "<UnknownPacket> " : "";
|
||||
log.info("{}ClientSend:{} [{}] ({}) - hex:{} - text:{}", prefix, opcodeName, opcodeHex, packetLength,
|
||||
HexTool.toString(content), HexTool.toStringFromAscii(content));
|
||||
} else {
|
||||
log.debug(HexTool.toString(new byte[]{content[0], content[1]}) + "...");
|
||||
}
|
||||
}
|
||||
|
||||
private String getRecvOpcodeName(short opcode) {
|
||||
return OpcodeConstants.recvOpcodeNames.get((int) opcode);
|
||||
}
|
||||
}
|
||||
17
src/main/java/net/packet/logging/LoggingUtil.java
Normal file
17
src/main/java/net/packet/logging/LoggingUtil.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package net.packet.logging;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class LoggingUtil {
|
||||
private static final Set<Short> ignoredDebugRecvPackets = Set.of((short) 167, (short) 197, (short) 89, (short) 91, (short) 41, (short) 188, (short) 107);
|
||||
|
||||
public static short readFirstShort(byte[] bytes) {
|
||||
return Unpooled.wrappedBuffer(bytes).readShortLE();
|
||||
}
|
||||
|
||||
public static boolean isIgnoredRecvPacket(short opcode) {
|
||||
return ignoredDebugRecvPackets.contains(opcode);
|
||||
}
|
||||
}
|
||||
48
src/main/java/net/packet/logging/OutPacketLogger.java
Normal file
48
src/main/java/net/packet/logging/OutPacketLogger.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package net.packet.logging;
|
||||
|
||||
import constants.net.OpcodeConstants;
|
||||
import io.netty.channel.ChannelHandler.Sharable;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelOutboundHandlerAdapter;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
import net.packet.OutPacket;
|
||||
import net.packet.Packet;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import tools.HexTool;
|
||||
|
||||
@Sharable
|
||||
public class OutPacketLogger extends ChannelOutboundHandlerAdapter implements PacketLogger {
|
||||
private static final Logger log = LoggerFactory.getLogger(OutPacketLogger.class);
|
||||
private static final int LOG_CONTENT_THRESHOLD = 50_000;
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
|
||||
if (msg instanceof OutPacket packet) {
|
||||
log(packet);
|
||||
}
|
||||
|
||||
ctx.write(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(Packet packet) {
|
||||
final byte[] content = packet.getBytes();
|
||||
final int packetLength = content.length;
|
||||
|
||||
if (packetLength <= LOG_CONTENT_THRESHOLD) {
|
||||
final short opcode = LoggingUtil.readFirstShort(content);
|
||||
String opcodeHex = Integer.toHexString(opcode).toUpperCase();
|
||||
String opcodeName = getSendOpcodeName(opcode);
|
||||
String prefix = opcodeName == null ? "<UnknownPacket> " : "";
|
||||
log.info("{}ServerSend:{} [{}] ({}) - hex:{} - text:{}", prefix, opcodeName, opcodeHex, packetLength,
|
||||
HexTool.toString(content), HexTool.toStringFromAscii(content));
|
||||
} else {
|
||||
log.info(HexTool.toString(new byte[]{content[0], content[1]}) + " ...");
|
||||
}
|
||||
}
|
||||
|
||||
private String getSendOpcodeName(short opcode) {
|
||||
return OpcodeConstants.sendOpcodeNames.get((int) opcode);
|
||||
}
|
||||
}
|
||||
7
src/main/java/net/packet/logging/PacketLogger.java
Normal file
7
src/main/java/net/packet/logging/PacketLogger.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package net.packet.logging;
|
||||
|
||||
import net.packet.Packet;
|
||||
|
||||
public interface PacketLogger {
|
||||
void log(Packet packet);
|
||||
}
|
||||
Reference in New Issue
Block a user