From 60588b54d1e39dc16ff564280c9c66cec94ccce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E5=99=A8=E8=BF=91?= Date: Thu, 1 Jul 2021 16:31:57 +0800 Subject: [PATCH] update --- .../yezhihao/netmc/TCPServerHandler.java | 7 ++- .../yezhihao/netmc/codec/MessageDecoder.java | 4 +- .../yezhihao/netmc/codec/MessageEncoder.java | 2 - .../yezhihao/netmc/util/AdapterList.java | 4 ++ .../yezhihao/netmc/util/AdapterMap.java | 31 ++++++++++++ .../yezhihao/netmc/util/AdapterSet.java | 50 +++++++++++++++++++ .../netmc/codec/MyMessageDecoder.java | 5 -- .../netmc/codec/MyMessageEncoder.java | 3 +- 8 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 src/main/java/io/github/yezhihao/netmc/util/AdapterMap.java create mode 100644 src/main/java/io/github/yezhihao/netmc/util/AdapterSet.java diff --git a/src/main/java/io/github/yezhihao/netmc/TCPServerHandler.java b/src/main/java/io/github/yezhihao/netmc/TCPServerHandler.java index 94d838d..d4b4ae2 100644 --- a/src/main/java/io/github/yezhihao/netmc/TCPServerHandler.java +++ b/src/main/java/io/github/yezhihao/netmc/TCPServerHandler.java @@ -16,6 +16,8 @@ import io.netty.handler.timeout.IdleStateEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; + /** * @author yezhihao * home https://gitee.com/yezhihao/jt808-server @@ -97,7 +99,10 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) { Session session = ctx.channel().attr(Session.KEY).get(); - log.warn(">>>>>消息处理异常" + session, e); + if (e instanceof IOException) + log.warn(">>>>>终端主动断开连接{},{}", e.getMessage(), session); + else + log.warn(">>>>>消息处理异常" + session, e); } @Override diff --git a/src/main/java/io/github/yezhihao/netmc/codec/MessageDecoder.java b/src/main/java/io/github/yezhihao/netmc/codec/MessageDecoder.java index 9d9d773..a3ae408 100644 --- a/src/main/java/io/github/yezhihao/netmc/codec/MessageDecoder.java +++ b/src/main/java/io/github/yezhihao/netmc/codec/MessageDecoder.java @@ -1,7 +1,7 @@ package io.github.yezhihao.netmc.codec; -import io.netty.buffer.ByteBuf; import io.github.yezhihao.netmc.session.Session; +import io.netty.buffer.ByteBuf; /** * 基础消息解码 @@ -10,8 +10,6 @@ import io.github.yezhihao.netmc.session.Session; */ public interface MessageDecoder { - T decode(ByteBuf buf); - T decode(ByteBuf buf, Session session); } \ No newline at end of file diff --git a/src/main/java/io/github/yezhihao/netmc/codec/MessageEncoder.java b/src/main/java/io/github/yezhihao/netmc/codec/MessageEncoder.java index 646450a..e732d05 100644 --- a/src/main/java/io/github/yezhihao/netmc/codec/MessageEncoder.java +++ b/src/main/java/io/github/yezhihao/netmc/codec/MessageEncoder.java @@ -10,8 +10,6 @@ import io.netty.buffer.ByteBuf; */ public interface MessageEncoder { - ByteBuf encode(T message); - ByteBuf encode(T message, Session session); } \ No newline at end of file diff --git a/src/main/java/io/github/yezhihao/netmc/util/AdapterList.java b/src/main/java/io/github/yezhihao/netmc/util/AdapterList.java index cf57773..a54b669 100644 --- a/src/main/java/io/github/yezhihao/netmc/util/AdapterList.java +++ b/src/main/java/io/github/yezhihao/netmc/util/AdapterList.java @@ -4,6 +4,10 @@ import java.util.AbstractList; import java.util.List; import java.util.function.Function; +/** + * @author yezhihao + * home https://gitee.com/yezhihao/jt808-server + */ public final class AdapterList extends AbstractList { private final List src; diff --git a/src/main/java/io/github/yezhihao/netmc/util/AdapterMap.java b/src/main/java/io/github/yezhihao/netmc/util/AdapterMap.java new file mode 100644 index 0000000..a2b9492 --- /dev/null +++ b/src/main/java/io/github/yezhihao/netmc/util/AdapterMap.java @@ -0,0 +1,31 @@ +package io.github.yezhihao.netmc.util; + +import java.util.AbstractMap; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +/** + * @author yezhihao + * home https://gitee.com/yezhihao/jt808-server + */ +public final class AdapterMap extends AbstractMap { + + private final Map src; + private final Set> entries; + + public AdapterMap(Map src, Function function) { + this.src = src; + this.entries = new AdapterSet(src.entrySet(), (Function, Entry>) e -> new SimpleEntry(e.getKey(), function.apply(e.getValue()))); + } + + @Override + public Set> entrySet() { + return entries; + } + + @Override + public int size() { + return src.size(); + } +} \ No newline at end of file diff --git a/src/main/java/io/github/yezhihao/netmc/util/AdapterSet.java b/src/main/java/io/github/yezhihao/netmc/util/AdapterSet.java new file mode 100644 index 0000000..e9e9ccd --- /dev/null +++ b/src/main/java/io/github/yezhihao/netmc/util/AdapterSet.java @@ -0,0 +1,50 @@ +package io.github.yezhihao.netmc.util; + +import java.util.AbstractSet; +import java.util.Iterator; +import java.util.Set; +import java.util.function.Function; + +/** + * @author yezhihao + * home https://gitee.com/yezhihao/jt808-server + */ +public final class AdapterSet extends AbstractSet { + + private final Set src; + private final Iterator iterator; + + public AdapterSet(Set src, Function function) { + this.src = src; + this.iterator = new Iterator() { + + private final Function f = function; + private final Iterator it = src.iterator(); + + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public T next() { + return f.apply(it.next()); + } + + @Override + public void remove() { + it.remove(); + } + }; + } + + @Override + public Iterator iterator() { + return iterator; + } + + @Override + public int size() { + return src.size(); + } +} \ No newline at end of file diff --git a/src/test/java/io/github/yezhihao/netmc/codec/MyMessageDecoder.java b/src/test/java/io/github/yezhihao/netmc/codec/MyMessageDecoder.java index a7bd6c3..6c00f60 100644 --- a/src/test/java/io/github/yezhihao/netmc/codec/MyMessageDecoder.java +++ b/src/test/java/io/github/yezhihao/netmc/codec/MyMessageDecoder.java @@ -9,11 +9,6 @@ import java.nio.charset.StandardCharsets; public class MyMessageDecoder implements MessageDecoder { - @Override - public Object decode(ByteBuf buf) { - return decode(buf); - } - @Override public Object decode(ByteBuf buf, Session session) { String msgStr = buf.readCharSequence(buf.readableBytes(), StandardCharsets.UTF_8).toString(); diff --git a/src/test/java/io/github/yezhihao/netmc/codec/MyMessageEncoder.java b/src/test/java/io/github/yezhihao/netmc/codec/MyMessageEncoder.java index 7293064..09283c5 100644 --- a/src/test/java/io/github/yezhihao/netmc/codec/MyMessageEncoder.java +++ b/src/test/java/io/github/yezhihao/netmc/codec/MyMessageEncoder.java @@ -2,6 +2,7 @@ package io.github.yezhihao.netmc.codec; import io.github.yezhihao.netmc.model.MyHeader; import io.github.yezhihao.netmc.model.MyMessage; +import io.github.yezhihao.netmc.session.Session; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; @@ -10,7 +11,7 @@ import java.nio.charset.StandardCharsets; public class MyMessageEncoder implements MessageEncoder { @Override - public ByteBuf encode(MyMessage message) { + public ByteBuf encode(MyMessage message, Session session) { MyHeader header = message.getHeader(); StringBuilder msg = new StringBuilder();