diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/QosDetector.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/QosDetector.java index 6a914053525..197b0e02eb5 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/QosDetector.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/QosDetector.java @@ -37,20 +37,20 @@ public QosDetector(FrameworkModel frameworkModel) { @Override public Result detect(ChannelBuffer in) { if(!QosEnableFlag) { - return Result.UNRECOGNIZED; + return Result.unrecognized(); } Result h1Res = qosHTTP1Detector.detect(in); - if(h1Res.equals(Result.RECOGNIZED)) { + if(h1Res.equals(Result.recognized())) { return h1Res; } Result telRes = telnetDetector.detect(in); - if(telRes.equals(Result.RECOGNIZED)) { + if(telRes.equals(Result.recognized())) { return telRes; } - if(h1Res.equals(Result.NEED_MORE_DATA) || telRes.equals(Result.NEED_MORE_DATA)) { - return Result.NEED_MORE_DATA; + if(h1Res.equals(Result.needMoreData()) || telRes.equals(Result.needMoreData())) { + return Result.needMoreData(); } - return Result.UNRECOGNIZED; + return Result.unrecognized(); } } diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/QosHTTP1Detector.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/QosHTTP1Detector.java index 9a62b841b1f..00fb9591215 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/QosHTTP1Detector.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/QosHTTP1Detector.java @@ -27,13 +27,13 @@ private static boolean isHttp(int magic) { @Override public Result detect(ChannelBuffer in) { if (in.readableBytes() < 2) { - return Result.NEED_MORE_DATA; + return Result.needMoreData(); } final int magic = in.getByte(in.readerIndex()); // h2 starts with "PR" if (isHttp(magic) && in.getByte(in.readerIndex()+1) != 'R' ){ - return Result.RECOGNIZED; + return Result.recognized(); } - return Result.UNRECOGNIZED; + return Result.unrecognized(); } } diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/TelnetDetector.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/TelnetDetector.java index d40f858377d..e7a66fcb6bf 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/TelnetDetector.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/TelnetDetector.java @@ -43,20 +43,20 @@ public TelnetDetector(FrameworkModel frameworkModel) { @Override public Result detect(ChannelBuffer in) { if (in.readableBytes() >= MaxSize) { - return Result.UNRECOGNIZED; + return Result.unrecognized(); } Result resCommand = commandDetect(in); - if (resCommand.equals(Result.RECOGNIZED)) { + if (resCommand.equals(Result.recognized())) { return resCommand; } Result resAyt = telnetAytDetect(in); - if (resAyt.equals(Result.RECOGNIZED)) { + if (resAyt.equals(Result.recognized())) { return resAyt; } - if (resAyt.equals(Result.UNRECOGNIZED) && resCommand.equals(Result.UNRECOGNIZED)) { - return Result.UNRECOGNIZED; + if (resAyt.equals(Result.unrecognized()) && resCommand.equals(Result.unrecognized())) { + return Result.unrecognized(); } - return Result.NEED_MORE_DATA; + return Result.needMoreData(); } private Result commandDetect(ChannelBuffer in) { @@ -75,9 +75,9 @@ private Result commandDetect(ChannelBuffer in) { s = s.trim(); CommandContext commandContext = TelnetCommandDecoder.decode(s); if (frameworkModel.getExtensionLoader(BaseCommand.class).hasExtension(commandContext.getCommandName())) { - return Result.RECOGNIZED; + return Result.recognized(); } - return Result.UNRECOGNIZED; + return Result.unrecognized(); } private Result telnetAytDetect(ChannelBuffer in) { @@ -85,16 +85,16 @@ private Result telnetAytDetect(ChannelBuffer in) { int prefaceLen = AytPreface.readableBytes(); int bytesRead = min(in.readableBytes(), prefaceLen); if (bytesRead == 0 || !ChannelBuffers.prefixEquals(in, AytPreface, bytesRead)) { - return Result.UNRECOGNIZED; + return Result.unrecognized(); } if (bytesRead == prefaceLen) { // we need to consume preface because it's not a qos command // consume and remember to mark, pu server handler reset reader index in.readBytes(AytPreface.readableBytes()); in.markReaderIndex(); - return Result.RECOGNIZED; + return Result.recognized(); } - return Result.NEED_MORE_DATA; + return Result.needMoreData(); } } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/ProtocolDetector.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/ProtocolDetector.java index 856d0f1631f..cfb87ea964c 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/ProtocolDetector.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/ProtocolDetector.java @@ -19,16 +19,59 @@ import org.apache.dubbo.remoting.buffer.ChannelBuffer; +import java.util.HashMap; +import java.util.Map; + /** * Determine incoming bytes belong to the specific protocol. - * */ public interface ProtocolDetector { Result detect(ChannelBuffer in); - enum Result { + class Result { + + private final Flag flag; + + private final Map detectContext = new HashMap<>(4); + + private Result(Flag flag) { + this.flag = flag; + } + + public void setAttribute(String key, String value) { + this.detectContext.put(key, value); + } + + public String getAttribute(String key) { + return this.detectContext.get(key); + } + + public void removeAttribute(String key) { + this.detectContext.remove(key); + } + + public Flag flag() { + return flag; + } + + public static Result recognized(){ + return new Result(Flag.RECOGNIZED); + } + + + public static Result unrecognized(){ + return new Result(Flag.UNRECOGNIZED); + } + + + public static Result needMoreData(){ + return new Result(Flag.NEED_MORE_DATA); + } + } + + enum Flag { RECOGNIZED, UNRECOGNIZED, NEED_MORE_DATA } } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/pu/ChannelOperator.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/pu/ChannelOperator.java index 82a3bde8fac..9f6f0dcf88b 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/pu/ChannelOperator.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/pu/ChannelOperator.java @@ -17,9 +17,12 @@ package org.apache.dubbo.remoting.api.pu; import org.apache.dubbo.remoting.ChannelHandler; +import org.apache.dubbo.remoting.api.ProtocolDetector; import java.util.List; public interface ChannelOperator { void configChannelHandler(List handlerList); + + ProtocolDetector.Result detectResult(); } diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyConfigOperator.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyConfigOperator.java index c708b12810a..a54c56d0eec 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyConfigOperator.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyConfigOperator.java @@ -24,6 +24,7 @@ import org.apache.dubbo.remoting.Codec; import org.apache.dubbo.remoting.Codec2; import org.apache.dubbo.remoting.Constants; +import org.apache.dubbo.remoting.api.ProtocolDetector; import org.apache.dubbo.remoting.api.pu.ChannelHandlerPretender; import org.apache.dubbo.remoting.api.pu.ChannelOperator; import org.apache.dubbo.remoting.api.pu.DefaultCodec; @@ -36,6 +37,8 @@ public class NettyConfigOperator implements ChannelOperator { private final Channel channel; private ChannelHandler handler; + private ProtocolDetector.Result detectResult; + public NettyConfigOperator(NettyChannel channel, ChannelHandler handler) { this.channel = channel; this.handler = handler; @@ -91,6 +94,15 @@ public void configChannelHandler(List handlerList) { } } + public void setDetectResult(ProtocolDetector.Result detectResult) { + this.detectResult = detectResult; + } + + @Override + public ProtocolDetector.Result detectResult() { + return detectResult; + } + private boolean isClientSide(Channel channel) { return channel.getUrl().getSide("").equalsIgnoreCase(CommonConstants.CONSUMER); } diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServerHandler.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServerHandler.java index cb7e672c67b..6cd5a910098 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServerHandler.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServerHandler.java @@ -107,7 +107,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) ChannelBuffer buf = new NettyBackedChannelBuffer(in); final ProtocolDetector.Result result = protocol.detector().detect(buf); in.resetReaderIndex(); - switch (result) { + switch (result.flag()) { case UNRECOGNIZED: continue; case RECOGNIZED: @@ -117,6 +117,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) URL localURL = this.urlMapper.getOrDefault(protocolName, url); channel.setUrl(localURL); NettyConfigOperator operator = new NettyConfigOperator(channel, localHandler); + operator.setDetectResult(result); protocol.configServerProtocolHandler(url, operator); ctx.pipeline().remove(this); case NEED_MORE_DATA: diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/pu/DubboDetector.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/pu/DubboDetector.java index 88d146c9333..d39c8e30d8a 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/pu/DubboDetector.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/pu/DubboDetector.java @@ -36,12 +36,12 @@ public Result detect(ChannelBuffer in) { int bytesRead = min(in.readableBytes(), prefaceLen); if (bytesRead ==0 || !ChannelBuffers.prefixEquals(in, Preface, bytesRead)) { - return Result.UNRECOGNIZED; + return Result.unrecognized(); } if (bytesRead == prefaceLen) { - return Result.RECOGNIZED; + return Result.recognized(); } - return Result.NEED_MORE_DATA; + return Result.needMoreData(); } } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/Http2ProtocolDetector.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/Http2ProtocolDetector.java index 9071ca4393d..b651af696d2 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/Http2ProtocolDetector.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/Http2ProtocolDetector.java @@ -36,11 +36,11 @@ public Result detect(ChannelBuffer in) { // If the input so far doesn't match the preface, break the connection. if (bytesRead == 0 || !ChannelBuffers.prefixEquals(in, clientPrefaceString, bytesRead)) { - return Result.UNRECOGNIZED; + return Result.unrecognized(); } if (bytesRead == prefaceLen) { - return Result.RECOGNIZED; + return Result.recognized(); } - return Result.NEED_MORE_DATA; + return Result.needMoreData(); } } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/Http2ProtocolDetectorTest.java b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/Http2ProtocolDetectorTest.java index 4edb95549f2..313396fc47f 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/Http2ProtocolDetectorTest.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/Http2ProtocolDetectorTest.java @@ -42,16 +42,16 @@ void testDetect() { ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(); ChannelBuffer in = new ByteBufferBackedChannelBuffer(byteBuf.nioBuffer()); ProtocolDetector.Result result = detector.detect(in); - Assertions.assertEquals(result, ProtocolDetector.Result.UNRECOGNIZED); + Assertions.assertEquals(result.flag(), ProtocolDetector.Result.unrecognized().flag()); byteBuf.writeBytes(connectionPrefaceBuf); result = detector.detect(new ByteBufferBackedChannelBuffer(byteBuf.nioBuffer())); - Assertions.assertEquals(result, ProtocolDetector.Result.RECOGNIZED); + Assertions.assertEquals(result.flag(), ProtocolDetector.Result.recognized().flag()); byteBuf.clear(); byteBuf.writeBytes(connectionPrefaceBuf, 0, 1); result = detector.detect(new ByteBufferBackedChannelBuffer(byteBuf.nioBuffer())); - Assertions.assertEquals(result, ProtocolDetector.Result.NEED_MORE_DATA); + Assertions.assertEquals(result.flag(), ProtocolDetector.Result.needMoreData().flag()); } }