From f210d8a1e69fdb9553d11ddf6d9cacdb0ee64b48 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Sun, 2 Aug 2020 23:16:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E6=8B=86=E5=8C=85=E7=B2=98?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cm/socket/consts/ISocketConst.java | 12 ++++++++++ .../com/cm/socket/decoder/MessageDecoder.java | 22 ++++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/cloud-common-socket/src/main/java/com/cm/socket/consts/ISocketConst.java b/cloud-common-socket/src/main/java/com/cm/socket/consts/ISocketConst.java index 69843a1..6702224 100644 --- a/cloud-common-socket/src/main/java/com/cm/socket/consts/ISocketConst.java +++ b/cloud-common-socket/src/main/java/com/cm/socket/consts/ISocketConst.java @@ -20,5 +20,17 @@ public interface ISocketConst { * 客户端密码 */ String CLIENT_SECRET = "clientSecret"; + /** + * 异常类 + */ + String EXCEPTION_CLASS = "exceptionClass"; + /** + * 异常名称 + */ + String EXCEPTION_MESSAGE = "exceptionName"; + /** + * 异常内容 + */ + String EXCEPTION_CONTENT = "exceptionContent"; } diff --git a/cloud-common-socket/src/main/java/com/cm/socket/decoder/MessageDecoder.java b/cloud-common-socket/src/main/java/com/cm/socket/decoder/MessageDecoder.java index c08a4b5..272596d 100644 --- a/cloud-common-socket/src/main/java/com/cm/socket/decoder/MessageDecoder.java +++ b/cloud-common-socket/src/main/java/com/cm/socket/decoder/MessageDecoder.java @@ -1,5 +1,7 @@ package com.cm.socket.decoder; +import com.cm.socket.enums.SocketMessageEnum; +import com.cm.socket.enums.SocketTypeMessageEnum; import com.cm.socket.pojo.Message; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; @@ -8,6 +10,8 @@ import io.netty.util.CharsetUtil; import java.nio.charset.Charset; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * When you feel like quitting. Think about why you started @@ -38,14 +42,18 @@ public class MessageDecoder extends ByteToMessageDecoder { byte type = in.readByte(); int tokenBytesLength = in.readInt(); int contentBytesLength = in.readInt(); + if (in.readableBytes() < contentBytesLength) { + in.resetReaderIndex(); + return; + } String token = getToken(in, tokenBytesLength); String content = getContent(in, contentBytesLength); - Message message = new Message(); - message.setStart(start); - message.setType(type); - message.setToken(token); - message.setContent(content); - out.add(message); + Message tempMessage = new Message(); + tempMessage.setStart(start); + tempMessage.setType(type); + tempMessage.setToken(token); + tempMessage.setContent(content); + out.add(tempMessage); } /** @@ -59,7 +67,6 @@ public class MessageDecoder extends ByteToMessageDecoder { return null; } if (in.readableBytes() < tokenBytesLength) { - in.resetReaderIndex(); return null; } byte[] tokenBytes = new byte[tokenBytesLength]; @@ -78,7 +85,6 @@ public class MessageDecoder extends ByteToMessageDecoder { return null; } if (in.readableBytes() < contentBytesLength) { - in.resetReaderIndex(); return null; } byte[] contentBytes = new byte[contentBytesLength];