Automatic messageType detection for ChannelInboundMessageHandlerAdapter
This commit is contained in:
parent
f9eff51683
commit
e5616c85c4
@ -58,7 +58,6 @@ public class WebSocketServerProtocolHandler extends ChannelInboundMessageHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions) {
|
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions) {
|
||||||
super(WebSocketFrame.class);
|
|
||||||
this.websocketPath = websocketPath;
|
this.websocketPath = websocketPath;
|
||||||
this.subprotocols = subprotocols;
|
this.subprotocols = subprotocols;
|
||||||
this.allowExtensions = allowExtensions;
|
this.allowExtensions = allowExtensions;
|
||||||
|
@ -44,7 +44,6 @@ public class WebSocketServerProtocolHandshakeHandler
|
|||||||
|
|
||||||
public WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols,
|
public WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols,
|
||||||
boolean allowExtensions) {
|
boolean allowExtensions) {
|
||||||
super(FullHttpRequest.class);
|
|
||||||
this.websocketPath = websocketPath;
|
this.websocketPath = websocketPath;
|
||||||
this.subprotocols = subprotocols;
|
this.subprotocols = subprotocols;
|
||||||
this.allowExtensions = allowExtensions;
|
this.allowExtensions = allowExtensions;
|
||||||
|
@ -37,12 +37,7 @@ public final class SocksServerConnectHandler extends ChannelInboundMessageHandle
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Bootstrap b;
|
private final Bootstrap b = new Bootstrap();
|
||||||
|
|
||||||
public SocksServerConnectHandler() {
|
|
||||||
super(SocksCmdRequest.class);
|
|
||||||
b = new Bootstrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void messageReceived(final ChannelHandlerContext ctx, final SocksCmdRequest request) throws Exception {
|
public void messageReceived(final ChannelHandlerContext ctx, final SocksCmdRequest request) throws Exception {
|
||||||
|
@ -34,10 +34,6 @@ public final class SocksServerHandler extends ChannelInboundMessageHandlerAdapte
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SocksServerHandler() {
|
|
||||||
super(SocksRequest.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void messageReceived(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception {
|
public void messageReceived(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception {
|
||||||
switch (socksRequest.requestType()) {
|
switch (socksRequest.requestType()) {
|
||||||
|
@ -35,7 +35,6 @@ public class SctpInboundByteStreamHandler extends ChannelInboundMessageHandlerAd
|
|||||||
* @param protocolIdentifier supported application protocol.
|
* @param protocolIdentifier supported application protocol.
|
||||||
*/
|
*/
|
||||||
public SctpInboundByteStreamHandler(int protocolIdentifier, int streamIdentifier) {
|
public SctpInboundByteStreamHandler(int protocolIdentifier, int streamIdentifier) {
|
||||||
super(SctpMessage.class);
|
|
||||||
this.protocolIdentifier = protocolIdentifier;
|
this.protocolIdentifier = protocolIdentifier;
|
||||||
this.streamIdentifier = streamIdentifier;
|
this.streamIdentifier = streamIdentifier;
|
||||||
}
|
}
|
||||||
|
@ -46,9 +46,6 @@ public class EchoMessageHandler extends
|
|||||||
}
|
}
|
||||||
|
|
||||||
public EchoMessageHandler(final Meter meter, final int messageSize) {
|
public EchoMessageHandler(final Meter meter, final int messageSize) {
|
||||||
|
|
||||||
super(UdtMessage.class);
|
|
||||||
|
|
||||||
this.meter = meter;
|
this.meter = meter;
|
||||||
|
|
||||||
final ByteBuf byteBuf = Unpooled.buffer(messageSize);
|
final ByteBuf byteBuf = Unpooled.buffer(messageSize);
|
||||||
|
@ -18,6 +18,10 @@ package io.netty.channel;
|
|||||||
import io.netty.buffer.MessageBuf;
|
import io.netty.buffer.MessageBuf;
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link ChannelHandler} which handles inbound messages of a specific type.
|
* {@link ChannelHandler} which handles inbound messages of a specific type.
|
||||||
*
|
*
|
||||||
@ -43,14 +47,43 @@ import io.netty.buffer.Unpooled;
|
|||||||
public abstract class ChannelInboundMessageHandlerAdapter<I>
|
public abstract class ChannelInboundMessageHandlerAdapter<I>
|
||||||
extends ChannelStateHandlerAdapter implements ChannelInboundMessageHandler<I> {
|
extends ChannelStateHandlerAdapter implements ChannelInboundMessageHandler<I> {
|
||||||
|
|
||||||
private final Class<?>[] acceptedMsgTypes;
|
private static final ConcurrentMap<Class<?>, Class<?>> messageTypeMap =
|
||||||
|
new ConcurrentHashMap<Class<?>, Class<?>>();
|
||||||
|
|
||||||
/**
|
private final Class<?> acceptedMsgType = findMessageType();
|
||||||
* The types which will be accepted by the message handler. If a received message is an other type it will be just
|
|
||||||
* forwarded to the next {@link ChannelInboundMessageHandler} in the {@link ChannelPipeline}.
|
private Class<?> findMessageType() {
|
||||||
*/
|
Class<?> thisClass = getClass();
|
||||||
protected ChannelInboundMessageHandlerAdapter(Class<?>... acceptedMsgTypes) {
|
Class<?> messageType = messageTypeMap.get(thisClass);
|
||||||
this.acceptedMsgTypes = ChannelHandlerUtil.acceptedMessageTypes(acceptedMsgTypes);
|
if (messageType == null) {
|
||||||
|
for (Method m: getClass().getDeclaredMethods()) {
|
||||||
|
if (!"messageReceived".equals(m.getName())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (m.isSynthetic() || m.isBridge()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Class<?>[] p = m.getParameterTypes();
|
||||||
|
if (p.length != 2) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (p[0] != ChannelHandlerContext.class) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
messageType = p[1];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (messageType == null) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"cannot determine the inbound message type of " + thisClass.getSimpleName());
|
||||||
|
}
|
||||||
|
|
||||||
|
messageTypeMap.put(thisClass, messageType);
|
||||||
|
}
|
||||||
|
|
||||||
|
return messageType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -121,7 +154,7 @@ public abstract class ChannelInboundMessageHandlerAdapter<I>
|
|||||||
* @param msg the message
|
* @param msg the message
|
||||||
*/
|
*/
|
||||||
public boolean isSupported(Object msg) throws Exception {
|
public boolean isSupported(Object msg) throws Exception {
|
||||||
return ChannelHandlerUtil.acceptMessage(acceptedMsgTypes, msg);
|
return acceptedMsgType.isInstance(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -155,10 +155,6 @@ public class DefaultChannelPipelineTest {
|
|||||||
private static final class StringInboundHandler extends ChannelInboundMessageHandlerAdapter<String> {
|
private static final class StringInboundHandler extends ChannelInboundMessageHandlerAdapter<String> {
|
||||||
boolean called;
|
boolean called;
|
||||||
|
|
||||||
public StringInboundHandler() {
|
|
||||||
super(String.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isSupported(Object msg) throws Exception {
|
public boolean isSupported(Object msg) throws Exception {
|
||||||
called = true;
|
called = true;
|
||||||
|
Loading…
Reference in New Issue
Block a user