src/main/java/com/zy/core/netty/IMessageBody.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/core/netty/constant/FireDataType.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/core/netty/domain/fire/Fire_3030.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/core/netty/handle/PackageServerHandler.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/core/netty/handle/ProtectorHandler.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
src/main/java/com/zy/core/netty/IMessageBody.java
New file @@ -0,0 +1,11 @@ package com.zy.core.netty; public interface IMessageBody { int DEFAULT_LEN = 256; byte[] writeToBytes(); IMessageBody readFromBytes(byte[] bytes); } src/main/java/com/zy/core/netty/constant/FireDataType.java
New file @@ -0,0 +1,30 @@ package com.zy.core.netty.constant; public enum FireDataType { HEARTBEAT(0x30, 0x30, "心跳信息"), FIRE_ALARM(0x38, 0x30, "火警"), ; public int byte1; public int byte2; public String des; FireDataType(int byte1, int byte2, String des) { this.byte1 = byte1; this.byte2 = byte2; this.des = des; } public static FireDataType get(int byte1, int byte2) { for (FireDataType value : FireDataType.values()) { if (byte1 == value.byte1 && byte2 == value.byte2) { return value; } } return null; } } src/main/java/com/zy/core/netty/domain/fire/Fire_3030.java
New file @@ -0,0 +1,76 @@ package com.zy.core.netty.domain.fire; import com.zy.core.netty.IMessageBody; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; import lombok.Data; import java.nio.charset.StandardCharsets; /** * 心跳 pac * 8230303234303030303030313130353138303E303F3131383083 * Created by vincent on 2022/7/30 */ @Data public class Fire_3030 implements IMessageBody { private Integer no; private Integer year; private Integer month; private Integer day; private Integer hour; private Integer minute; private Integer seconds; private Integer validCode; @Override public byte[] writeToBytes() { ByteBuf byteBuf = ByteBufAllocator.DEFAULT.heapBuffer(DEFAULT_LEN); byteBuf.writeShort(no); // todo return ByteBufUtil.getBytes(byteBuf); } @Override public Fire_3030 readFromBytes(byte[] bytes) { // ByteBuf byteBuf = ByteBufAllocator.DEFAULT.heapBuffer(bytes.length > DEFAULT_LEN ? bytes.length + 100 : DEFAULT_LEN); // byteBuf.writeBytes(bytes); // byteBuf.resetReaderIndex(); // byteBuf.skipBytes(3); // this.no = byteBuf.readShort(); // byteBuf.skipBytes(6); // this.year = byteBuf.readShort(); // this.month = byteBuf.readShort(); // this.day = byteBuf.readShort(); // this.hour = byteBuf.readShort(); // this.minute = byteBuf.readShort(); // this.seconds = byteBuf.readShort(); // this.validCode = byteBuf.readShort(); // byteBuf.skipBytes(1); // byteBuf.release(); // System.out.println(byteBuf.refCnt()); this.no = analyze(new byte[]{bytes[3], bytes[4]}); this.year = analyze(new byte[]{bytes[11], bytes[12]}); this.month = analyze(new byte[]{bytes[13], bytes[14]}); this.day = analyze(new byte[]{bytes[15], bytes[16]}); this.hour = analyze(new byte[]{bytes[17], bytes[18]}); this.minute = analyze(new byte[]{bytes[19], bytes[20]}); this.seconds = analyze(new byte[]{bytes[21], bytes[22]}); this.validCode = analyze(new byte[]{bytes[23], bytes[24]}); return this; } public static Integer analyze(byte[] bytes) { return Integer.parseInt(new String(bytes, StandardCharsets.US_ASCII), 16); } } src/main/java/com/zy/core/netty/handle/PackageServerHandler.java
@@ -2,7 +2,9 @@ import com.zy.core.netty.AbstractInboundHandler; import com.zy.core.netty.cache.ChannelCache; import com.zy.core.netty.constant.FireDataType; import com.zy.core.netty.domain.ChPackage; import com.zy.core.netty.domain.fire.Fire_3030; import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; @@ -21,7 +23,16 @@ @Override protected boolean channelRead0(ChannelHandlerContext ctx, ChPackage pac) { log.info("{}", pac.getBytes().length); FireDataType dataType = FireDataType.get(pac.getBytes()[1], pac.getBytes()[2]); switch (dataType) { case HEARTBEAT: Fire_3030 fire3030 = new Fire_3030().readFromBytes(pac.getBytes()); break; case FIRE_ALARM: break; default: break; } return true; } src/main/java/com/zy/core/netty/handle/ProtectorHandler.java
@@ -62,8 +62,8 @@ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ChannelCache.removeChannel(ctx.channel()); ctx.close(); // ChannelCache.removeChannel(ctx.channel()); // ctx.close(); } }