perf[json]: extract json encode and decode method

This commit is contained in:
godotg
2023-12-22 10:41:57 +08:00
parent 7354019db1
commit 9045c25174
2 changed files with 40 additions and 28 deletions
@@ -13,6 +13,14 @@
package com.zfoo.net.handler.codec.json;
import com.zfoo.net.packet.DecodedPacketInfo;
import com.zfoo.net.packet.EncodedPacketInfo;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.buffer.ByteBufUtils;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.StringUtils;
import io.netty.buffer.ByteBuf;
/**
* @author godotg
*/
@@ -35,6 +43,35 @@ public class JsonPacket {
return jsonPacket;
}
public static void writeEncodedPacketInfo(ByteBuf byteBuf, EncodedPacketInfo encodedPacketInfo) {
var packet = encodedPacketInfo.getPacket();
var attachment = encodedPacketInfo.getAttachment();
var attachmentId = attachment == null ? -1 : ProtocolManager.protocolId(attachment.getClass());
var jsonPacket = JsonPacket.valueOf(ProtocolManager.protocolId(packet.getClass()), packet, attachmentId, attachment);
var bytes = StringUtils.bytes(JsonUtils.object2String(jsonPacket));
byteBuf.writeBytes(bytes);
}
public static DecodedPacketInfo readDecodedPacketInfo(ByteBuf byteBuf) {
var bytes = ByteBufUtils.readAllBytes(byteBuf);
var jsonStr = StringUtils.bytesToString(bytes);
var jsonMap = JsonUtils.getJsonMap(jsonStr);
var protocolId = Short.parseShort(jsonMap.get("protocolId"));
var packetStr = jsonMap.get("packet");
var attachmentStr = jsonMap.get("attachmentId");
Object attachment = null;
if (StringUtils.isNotEmpty(attachmentStr)) {
var attachmentId = Short.parseShort(attachmentStr);
if (attachmentId >= 0) {
var attachmentClass = ProtocolManager.getProtocol(attachmentId).protocolConstructor().getDeclaringClass();
attachment = JsonUtils.string2Object(jsonMap.get("attachment"), attachmentClass);
}
}
var protocolClass = ProtocolManager.getProtocol(protocolId).protocolConstructor().getDeclaringClass();
var packet = JsonUtils.string2Object(packetStr, protocolClass);
return DecodedPacketInfo.valueOf(packet, attachment);
}
public short getProtocolId() {
return protocolId;
}
@@ -13,10 +13,8 @@
package com.zfoo.net.handler.codec.json;
import com.zfoo.net.packet.DecodedPacketInfo;
import com.zfoo.net.packet.EncodedPacketInfo;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.buffer.ByteBufUtils;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.StringUtils;
import io.netty.channel.ChannelHandlerContext;
@@ -34,37 +32,14 @@ public class JsonWebSocketCodecHandler extends MessageToMessageCodec<WebSocketFr
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, WebSocketFrame webSocketFrame, List<Object> list) {
var byteBuf = webSocketFrame.content();
var bytes = ByteBufUtils.readAllBytes(byteBuf);
var jsonStr = StringUtils.bytesToString(bytes);
var jsonMap = JsonUtils.getJsonMap(jsonStr);
var protocolId = Short.parseShort(jsonMap.get("protocolId"));
var packetStr = jsonMap.get("packet");
var attachmentStr = jsonMap.get("attachmentId");
Object attachment = null;
if (StringUtils.isNotEmpty(attachmentStr)) {
var attachmentId = Short.parseShort(attachmentStr);
if (attachmentId >= 0) {
var attachmentClass = ProtocolManager.getProtocol(attachmentId).protocolConstructor().getDeclaringClass();
attachment = JsonUtils.string2Object(jsonMap.get("attachment"), attachmentClass);
}
}
var protocolClass = ProtocolManager.getProtocol(protocolId).protocolConstructor().getDeclaringClass();
var packet = JsonUtils.string2Object(packetStr, protocolClass);
list.add(DecodedPacketInfo.valueOf(packet, attachment));
var decodedPacketInfo = JsonPacket.readDecodedPacketInfo(byteBuf);
list.add(decodedPacketInfo);
}
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, EncodedPacketInfo out, List<Object> list) {
var byteBuf = channelHandlerContext.alloc().ioBuffer();
var packet = out.getPacket();
var attachment = out.getAttachment();
var attachmentId = attachment == null ? -1 : ProtocolManager.protocolId(attachment.getClass());
var jsonPacket = JsonPacket.valueOf(ProtocolManager.protocolId(packet.getClass()), packet, attachmentId, attachment);
var bytes = StringUtils.bytes(JsonUtils.object2String(jsonPacket));
byteBuf.writeBytes(bytes);
JsonPacket.writeEncodedPacketInfo(byteBuf, out);
list.add(new BinaryWebSocketFrame(byteBuf));
}