From a15da0d5830e20a72689bf61821709c38c084166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E5=99=A8=E8=BF=91?= Date: Fri, 11 Jun 2021 11:43:10 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4Header=E9=99=8D=E4=BD=8E?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=80=A6=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yezhihao/netmc/TCPServerHandler.java | 4 ++-- .../netmc/core/HandlerInterceptor.java | 3 +-- .../yezhihao/netmc/core/handler/Handler.java | 7 ------ .../yezhihao/netmc/core/model/Header.java | 22 ------------------- .../yezhihao/netmc/core/model/Message.java | 11 +++++++--- .../yezhihao/netmc/core/model/Response.java | 2 +- .../github/yezhihao/netmc/model/MyHeader.java | 8 +------ .../yezhihao/netmc/model/MyMessage.java | 17 ++++++++++---- 8 files changed, 26 insertions(+), 48 deletions(-) delete mode 100644 src/main/java/io/github/yezhihao/netmc/core/model/Header.java diff --git a/src/main/java/io/github/yezhihao/netmc/TCPServerHandler.java b/src/main/java/io/github/yezhihao/netmc/TCPServerHandler.java index e788f28..6dc13d8 100644 --- a/src/main/java/io/github/yezhihao/netmc/TCPServerHandler.java +++ b/src/main/java/io/github/yezhihao/netmc/TCPServerHandler.java @@ -47,7 +47,7 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter { long time = session.access(); try { - Handler handler = handlerMapping.getHandler(request.getMessageType()); + Handler handler = handlerMapping.getHandler(request.getMessageId()); if (handler != null) { if (!interceptor.beforeHandle(request, session)) return; @@ -67,7 +67,7 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter { } time = System.currentTimeMillis() - time; if (time > 200) - log.info("=========消息ID{},处理耗时{}ms,", request.getHeader(), time); + log.info("=========消息ID{},处理耗时{}ms,", request.getMessageId(), time); if (response != null) ctx.writeAndFlush(response); } diff --git a/src/main/java/io/github/yezhihao/netmc/core/HandlerInterceptor.java b/src/main/java/io/github/yezhihao/netmc/core/HandlerInterceptor.java index c548c38..82e82ec 100644 --- a/src/main/java/io/github/yezhihao/netmc/core/HandlerInterceptor.java +++ b/src/main/java/io/github/yezhihao/netmc/core/HandlerInterceptor.java @@ -1,6 +1,5 @@ package io.github.yezhihao.netmc.core; -import io.github.yezhihao.netmc.core.model.Header; import io.github.yezhihao.netmc.core.model.Message; import io.github.yezhihao.netmc.session.Session; @@ -9,7 +8,7 @@ import io.github.yezhihao.netmc.session.Session; * @author yezhihao * home https://gitee.com/yezhihao/jt808-server */ -public interface HandlerInterceptor> { +public interface HandlerInterceptor { /** 未找到对应的Handle */ T notSupported(T request, Session session); diff --git a/src/main/java/io/github/yezhihao/netmc/core/handler/Handler.java b/src/main/java/io/github/yezhihao/netmc/core/handler/Handler.java index d775ca5..b8e2119 100644 --- a/src/main/java/io/github/yezhihao/netmc/core/handler/Handler.java +++ b/src/main/java/io/github/yezhihao/netmc/core/handler/Handler.java @@ -1,6 +1,5 @@ package io.github.yezhihao.netmc.core.handler; -import io.github.yezhihao.netmc.core.model.Header; import io.github.yezhihao.netmc.core.model.Message; import io.github.yezhihao.netmc.session.Session; import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl; @@ -17,7 +16,6 @@ public abstract class Handler { public static final int MESSAGE = 0; public static final int SESSION = 1; - public static final int HEADER = 2; public final Object targetObject; public final Method targetMethod; @@ -44,8 +42,6 @@ public abstract class Handler { if (Message.class.isAssignableFrom(clazz)) parameterTypes[i] = MESSAGE; - else if (Header.class.isAssignableFrom(clazz)) - parameterTypes[i] = HEADER; else if (Session.class.isAssignableFrom(clazz)) parameterTypes[i] = SESSION; } @@ -67,9 +63,6 @@ public abstract class Handler { case Handler.SESSION: args[i] = session; break; - case Handler.HEADER: - args[i] = request.getHeader(); - break; } } return (T) targetMethod.invoke(targetObject, args); diff --git a/src/main/java/io/github/yezhihao/netmc/core/model/Header.java b/src/main/java/io/github/yezhihao/netmc/core/model/Header.java deleted file mode 100644 index a30cd19..0000000 --- a/src/main/java/io/github/yezhihao/netmc/core/model/Header.java +++ /dev/null @@ -1,22 +0,0 @@ -package io.github.yezhihao.netmc.core.model; - -import java.io.Serializable; - -/** - * 消息头 - * @author yezhihao - * home https://gitee.com/yezhihao/jt808-server - */ -public interface Header extends Serializable { - - /** 客户端唯一标识 */ - ClientID getClientId(); - - /** 消息类型 */ - Type getType(); - - /** 消息流水号 */ - int getSerialNo(); - - void setSerialNo(int serialNo); -} \ No newline at end of file diff --git a/src/main/java/io/github/yezhihao/netmc/core/model/Message.java b/src/main/java/io/github/yezhihao/netmc/core/model/Message.java index 824b221..bfecd06 100644 --- a/src/main/java/io/github/yezhihao/netmc/core/model/Message.java +++ b/src/main/java/io/github/yezhihao/netmc/core/model/Message.java @@ -7,9 +7,14 @@ import java.io.Serializable; * @author yezhihao * home https://gitee.com/yezhihao/jt808-server */ -public interface Message extends Serializable { +public interface Message extends Serializable { - T getHeader(); + /** 客户端唯一标识 */ + Serializable getClientId(); - Object getMessageType(); + /** 消息类型 */ + Serializable getMessageId(); + + /** 消息流水号 */ + int getSerialNo(); } \ No newline at end of file diff --git a/src/main/java/io/github/yezhihao/netmc/core/model/Response.java b/src/main/java/io/github/yezhihao/netmc/core/model/Response.java index ca8e115..697c3ae 100644 --- a/src/main/java/io/github/yezhihao/netmc/core/model/Response.java +++ b/src/main/java/io/github/yezhihao/netmc/core/model/Response.java @@ -7,6 +7,6 @@ package io.github.yezhihao.netmc.core.model; public interface Response { /** 应答消息流水号 */ - int getSerialNo(); + int getResponseSerialNo(); } \ No newline at end of file diff --git a/src/test/java/io/github/yezhihao/netmc/model/MyHeader.java b/src/test/java/io/github/yezhihao/netmc/model/MyHeader.java index 2dac7f8..7c21765 100644 --- a/src/test/java/io/github/yezhihao/netmc/model/MyHeader.java +++ b/src/test/java/io/github/yezhihao/netmc/model/MyHeader.java @@ -1,8 +1,6 @@ package io.github.yezhihao.netmc.model; -import io.github.yezhihao.netmc.core.model.Header; - -public class MyHeader implements Header { +public class MyHeader { /** 客户端ID */ private String clientId; @@ -20,7 +18,6 @@ public class MyHeader implements Header { this.serialNo = serialNo; } - @Override public String getClientId() { return clientId; } @@ -29,7 +26,6 @@ public class MyHeader implements Header { this.clientId = clientId; } - @Override public Integer getType() { return type; } @@ -38,12 +34,10 @@ public class MyHeader implements Header { this.type = type; } - @Override public int getSerialNo() { return serialNo; } - @Override public void setSerialNo(int serialNo) { this.serialNo = serialNo; } diff --git a/src/test/java/io/github/yezhihao/netmc/model/MyMessage.java b/src/test/java/io/github/yezhihao/netmc/model/MyMessage.java index 37d37ff..2c549e7 100644 --- a/src/test/java/io/github/yezhihao/netmc/model/MyMessage.java +++ b/src/test/java/io/github/yezhihao/netmc/model/MyMessage.java @@ -3,7 +3,7 @@ package io.github.yezhihao.netmc.model; import io.github.yezhihao.netmc.core.model.Message; import io.github.yezhihao.netmc.session.Session; -public class MyMessage implements Message { +public class MyMessage implements Message { private Session session; @@ -19,7 +19,6 @@ public class MyMessage implements Message { this.session = session; } - @Override public MyHeader getHeader() { return header; } @@ -37,10 +36,20 @@ public class MyMessage implements Message { } @Override - public Object getMessageType() { + public String getClientId() { + return header.getClientId(); + } + + @Override + public Integer getMessageId() { return header.getType(); } + @Override + public int getSerialNo() { + return header.getSerialNo(); + } + @Override public String toString() { final StringBuilder sb = new StringBuilder("MyMessage{"); @@ -50,4 +59,4 @@ public class MyMessage implements Message { sb.append('}'); return sb.toString(); } -} +} \ No newline at end of file