解码器拆分

This commit is contained in:
Guoqs
2024-11-27 14:32:51 +08:00
parent 9b95a07d8b
commit cd252d5ed1
8 changed files with 143 additions and 12 deletions

View File

@@ -1,6 +1,6 @@
package com.jsowell.netty.server.electricbicycles;
import com.jsowell.netty.decoder.YouDianDecoder;
import com.jsowell.netty.decoder.YouDianProtocolDecoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
@@ -20,7 +20,7 @@ public class ElectricBicyclesServerChannelInitializer extends ChannelInitializer
@Override
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast("frameDecoder", new YouDianDecoder());
pipeline.addLast("frameDecoder", new YouDianProtocolDecoder());
// pipeline.addLast("decoder", new MessageDecode());
// pipeline.addLast("encoder", new MessageEncode());
pipeline.addLast("decoder", new ByteArrayDecoder());

View File

@@ -20,8 +20,8 @@ public class NettyServerChannelInitializer extends ChannelInitializer<SocketChan
@Override
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
// pipeline.addLast("frameDecoder",new CustomDecoder());
pipeline.addLast("frameDecoder", new YunKuaiChongDecoder());
pipeline.addLast("frameDecoder",new YunKuaiChongDecoder());
// pipeline.addLast("frameDecoder", new YkcProtocolDecoder());
pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast("encoder", new ByteArrayDecoder());
//读超时时间设置为10s0表示不监控

View File

@@ -1,6 +1,7 @@
package com.jsowell.netty.server.yunkuaichong;
import com.google.common.collect.Lists;
import com.jsowell.common.core.domain.ykc.YKCDataProtocol;
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
import com.jsowell.common.enums.ykc.PileChannelEntity;
import com.jsowell.common.util.BytesUtil;
@@ -68,9 +69,11 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception {
// log.info("channelRead-aClass:{}", message.getClass());
// log.info("加载客户端报文channelRead=== channelId:" + ctx.channel().id() + ", msg:" + message);
// 下面可以解析数据保存数据生成返回报文将需要返回报文写入write函数
byte[] msg = (byte[]) message;
YKCDataProtocol ykcDataProtocol = (YKCDataProtocol) message;
byte[] msg = ykcDataProtocol.getBytes();
// 获取帧类型
byte[] frameTypeBytes = BytesUtil.copyBytes(msg, 5, 1);