From 8f36ff33f06e54f68ebb0275ec50a3867304b300 Mon Sep 17 00:00:00 2001 From: limqhz Date: Sun, 5 Jul 2020 22:55:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9netty=E7=BC=96=E8=A7=A3?= =?UTF-8?q?=E7=A0=81=E5=99=A8=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/sv/netty/netty/MessageEncoder.java | 27 ++----------------- .../com/sv/netty/netty/ServerHandler.java | 11 ++++---- .../netty/ServerProtocolInitializer.java | 2 +- 3 files changed, 9 insertions(+), 31 deletions(-) diff --git a/netty-pad/src/main/java/com/sv/netty/netty/MessageEncoder.java b/netty-pad/src/main/java/com/sv/netty/netty/MessageEncoder.java index 6026563..9dd2f80 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/MessageEncoder.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/MessageEncoder.java @@ -17,7 +17,7 @@ import java.nio.charset.Charset; * @Author peakren * @Date 07/05/2017 10:43 PM */ -public class MessageEncoder extends MessageToByteEncoder { +public class MessageEncoder extends MessageToByteEncoder { private final static int MESSAGE_LENGTH = 4; private final static int MESSAGE_SEQNO = 8; @@ -30,7 +30,7 @@ public class MessageEncoder extends MessageToByteEncoder { Charset charset = Charset.forName("UTF-8"); @Override - protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception { + protected void encode(ChannelHandlerContext ctx, BaseDto msg, ByteBuf out) throws Exception { String message = JsonUtils.encode(msg); logger.info("send message content:" + message); byte[] bodys = message.getBytes(charset.name()); @@ -40,27 +40,4 @@ public class MessageEncoder extends MessageToByteEncoder { out.writeInt(len); //发送head字节码 out.writeBytes(bodys); //发送消息内容 } - - byte[] getIntBytes(int crc) { - byte[] targets = new byte[4]; - targets[0] = (byte) (crc & 0xff); - targets[1] = (byte) ((crc >> 8) & 0xff); - targets[2] = (byte) ((crc >> 16) & 0xff); - targets[3] = (byte) (crc >> 24); - return targets; - } - - byte[] getLongBytes(long crc) { - byte[] targets = new byte[8]; - targets[0] = (byte) (crc & 0xff); - targets[1] = (byte) ((crc >> 8) & 0xff); - targets[2] = (byte) ((crc >> 16) & 0xff); - targets[3] = (byte) (crc >> 24 & 0xff); - targets[4] = (byte) (crc >> 32 & 0xff); - targets[5] = (byte) (crc >> 40 & 0xff); - targets[6] = (byte) (crc >> 48 & 0xff); - targets[7] = (byte) (crc >> 56); - return targets; - } - } diff --git a/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java b/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java index 10c599f..0fda373 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java @@ -18,7 +18,7 @@ import java.net.InetSocketAddress; */ @ChannelHandler.Sharable -public class ServerHandler extends ChannelInboundHandlerAdapter { +public class ServerHandler extends SimpleChannelInboundHandler { private static Logger logger = LoggerFactory.getLogger(ServerHandler.class); @@ -46,11 +46,12 @@ public class ServerHandler extends ChannelInboundHandlerAdapter { */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + + } + + @Override + protected void channelRead0(ChannelHandlerContext ctx, BaseDto msg) throws Exception { super.channelRead(ctx, msg); - //taskQueue -// ctx.channel().eventLoop().execute() - //scheduleTaskQueue -// ctx.channel().eventLoop().schedule() String clientIp = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getClientIp(); try { messageService.receive(ctx.channel(), msg.toString()); diff --git a/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitializer.java b/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitializer.java index 157d6c3..7f64364 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitializer.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitializer.java @@ -30,7 +30,7 @@ public class ServerProtocolInitializer extends ChannelInitializer ChannelPipeline pipeline = ch.pipeline(); logger.info("ServerProtocolInitializer"); // 通过指定的长度来标识整包的信息,这样就可以自动的处理粘包和半包的问题 - pipeline.addFirst(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 4, 4, 0, 0)); +// pipeline.addFirst(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 4, 4, 0, 0)); pipeline.addLast(new ReadTimeoutHandler(READ_TIMEOUT)); pipeline.addLast(new MessageDecoder()); pipeline.addLast(new MessageEncoder());