Skip to content

Commit

Permalink
perf[json]: extract json encode and decode method
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Dec 22, 2023
1 parent 7354019 commit 9045c25
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
37 changes: 37 additions & 0 deletions net/src/main/java/com/zfoo/net/handler/codec/json/JsonPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -35,6 +43,35 @@ public static JsonPacket valueOf(short protocolId, Object packet, short attachme
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}

Expand Down

0 comments on commit 9045c25

Please sign in to comment.