diff --git a/net/src/main/java/com/zfoo/net/handler/codec/json/JsonPacket.java b/net/src/main/java/com/zfoo/net/handler/codec/json/JsonPacket.java index a037c2db4..7b63cfa72 100644 --- a/net/src/main/java/com/zfoo/net/handler/codec/json/JsonPacket.java +++ b/net/src/main/java/com/zfoo/net/handler/codec/json/JsonPacket.java @@ -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 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; } diff --git a/net/src/main/java/com/zfoo/net/handler/codec/json/JsonWebSocketCodecHandler.java b/net/src/main/java/com/zfoo/net/handler/codec/json/JsonWebSocketCodecHandler.java index f4b6e92bd..ec0d88256 100644 --- a/net/src/main/java/com/zfoo/net/handler/codec/json/JsonWebSocketCodecHandler.java +++ b/net/src/main/java/com/zfoo/net/handler/codec/json/JsonWebSocketCodecHandler.java @@ -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 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 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)); }