enhancement: extract duplicate code (#8732)
Motivation: Clean up code to increase readability. Modification: Extract duplicate code and remove unnecessary throws Result: Share more code.
This commit is contained in:
parent
c893939bd8
commit
8ebaa1b972
@ -53,7 +53,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
private static final FastThreadLocal<Map<Class<?>, String>> nameCaches =
|
private static final FastThreadLocal<Map<Class<?>, String>> nameCaches =
|
||||||
new FastThreadLocal<Map<Class<?>, String>>() {
|
new FastThreadLocal<Map<Class<?>, String>>() {
|
||||||
@Override
|
@Override
|
||||||
protected Map<Class<?>, String> initialValue() throws Exception {
|
protected Map<Class<?>, String> initialValue() {
|
||||||
return new WeakHashMap<Class<?>, String>();
|
return new WeakHashMap<Class<?>, String>();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -163,7 +163,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
|
|
||||||
addFirst0(newCtx);
|
addFirst0(newCtx);
|
||||||
|
|
||||||
// If the registered is false it means that the channel was not registered on an eventloop yet.
|
// If the registered is false it means that the channel was not registered on an eventLoop yet.
|
||||||
// In this case we add the context to the pipeline and add a task that will call
|
// In this case we add the context to the pipeline and add a task that will call
|
||||||
// ChannelHandler.handlerAdded(...) once the channel is registered.
|
// ChannelHandler.handlerAdded(...) once the channel is registered.
|
||||||
if (!registered) {
|
if (!registered) {
|
||||||
@ -174,13 +174,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
|
|
||||||
EventExecutor executor = newCtx.executor();
|
EventExecutor executor = newCtx.executor();
|
||||||
if (!executor.inEventLoop()) {
|
if (!executor.inEventLoop()) {
|
||||||
newCtx.setAddPending();
|
callHandlerAddedInEventLoop(newCtx, executor);
|
||||||
executor.execute(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
callHandlerAdded0(newCtx);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -211,7 +205,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
|
|
||||||
addLast0(newCtx);
|
addLast0(newCtx);
|
||||||
|
|
||||||
// If the registered is false it means that the channel was not registered on an eventloop yet.
|
// If the registered is false it means that the channel was not registered on an eventLoop yet.
|
||||||
// In this case we add the context to the pipeline and add a task that will call
|
// In this case we add the context to the pipeline and add a task that will call
|
||||||
// ChannelHandler.handlerAdded(...) once the channel is registered.
|
// ChannelHandler.handlerAdded(...) once the channel is registered.
|
||||||
if (!registered) {
|
if (!registered) {
|
||||||
@ -222,13 +216,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
|
|
||||||
EventExecutor executor = newCtx.executor();
|
EventExecutor executor = newCtx.executor();
|
||||||
if (!executor.inEventLoop()) {
|
if (!executor.inEventLoop()) {
|
||||||
newCtx.setAddPending();
|
callHandlerAddedInEventLoop(newCtx, executor);
|
||||||
executor.execute(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
callHandlerAdded0(newCtx);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -263,7 +251,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
|
|
||||||
addBefore0(ctx, newCtx);
|
addBefore0(ctx, newCtx);
|
||||||
|
|
||||||
// If the registered is false it means that the channel was not registered on an eventloop yet.
|
// If the registered is false it means that the channel was not registered on an eventLoop yet.
|
||||||
// In this case we add the context to the pipeline and add a task that will call
|
// In this case we add the context to the pipeline and add a task that will call
|
||||||
// ChannelHandler.handlerAdded(...) once the channel is registered.
|
// ChannelHandler.handlerAdded(...) once the channel is registered.
|
||||||
if (!registered) {
|
if (!registered) {
|
||||||
@ -274,13 +262,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
|
|
||||||
EventExecutor executor = newCtx.executor();
|
EventExecutor executor = newCtx.executor();
|
||||||
if (!executor.inEventLoop()) {
|
if (!executor.inEventLoop()) {
|
||||||
newCtx.setAddPending();
|
callHandlerAddedInEventLoop(newCtx, executor);
|
||||||
executor.execute(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
callHandlerAdded0(newCtx);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -323,7 +305,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
|
|
||||||
addAfter0(ctx, newCtx);
|
addAfter0(ctx, newCtx);
|
||||||
|
|
||||||
// If the registered is false it means that the channel was not registered on an eventloop yet.
|
// If the registered is false it means that the channel was not registered on an eventLoop yet.
|
||||||
// In this case we remove the context from the pipeline and add a task that will call
|
// In this case we remove the context from the pipeline and add a task that will call
|
||||||
// ChannelHandler.handlerRemoved(...) once the channel is registered.
|
// ChannelHandler.handlerRemoved(...) once the channel is registered.
|
||||||
if (!registered) {
|
if (!registered) {
|
||||||
@ -333,13 +315,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
}
|
}
|
||||||
EventExecutor executor = newCtx.executor();
|
EventExecutor executor = newCtx.executor();
|
||||||
if (!executor.inEventLoop()) {
|
if (!executor.inEventLoop()) {
|
||||||
newCtx.setAddPending();
|
callHandlerAddedInEventLoop(newCtx, executor);
|
||||||
executor.execute(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
callHandlerAdded0(newCtx);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1168,6 +1144,16 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void callHandlerAddedInEventLoop(final AbstractChannelHandlerContext newCtx, EventExecutor executor) {
|
||||||
|
newCtx.setAddPending();
|
||||||
|
executor.execute(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
callHandlerAdded0(newCtx);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called once a {@link Throwable} hit the end of the {@link ChannelPipeline} without been handled by the user
|
* Called once a {@link Throwable} hit the end of the {@link ChannelPipeline} without been handled by the user
|
||||||
* in {@link ChannelHandler#exceptionCaught(ChannelHandlerContext, Throwable)}.
|
* in {@link ChannelHandler#exceptionCaught(ChannelHandlerContext, Throwable)}.
|
||||||
@ -1267,49 +1253,49 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRegistered(ChannelHandlerContext ctx) throws Exception { }
|
public void channelRegistered(ChannelHandlerContext ctx) { }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { }
|
public void channelUnregistered(ChannelHandlerContext ctx) { }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
public void channelActive(ChannelHandlerContext ctx) {
|
||||||
onUnhandledInboundChannelActive();
|
onUnhandledInboundChannelActive();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
public void channelInactive(ChannelHandlerContext ctx) {
|
||||||
onUnhandledInboundChannelInactive();
|
onUnhandledInboundChannelInactive();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
|
public void channelWritabilityChanged(ChannelHandlerContext ctx) {
|
||||||
onUnhandledChannelWritabilityChanged();
|
onUnhandledChannelWritabilityChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { }
|
public void handlerAdded(ChannelHandlerContext ctx) { }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { }
|
public void handlerRemoved(ChannelHandlerContext ctx) { }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
|
||||||
onUnhandledInboundUserEventTriggered(evt);
|
onUnhandledInboundUserEventTriggered(evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||||
onUnhandledInboundException(cause);
|
onUnhandledInboundException(cause);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
||||||
onUnhandledInboundMessage(msg);
|
onUnhandledInboundMessage(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
public void channelReadComplete(ChannelHandlerContext ctx) {
|
||||||
onUnhandledInboundChannelReadComplete();
|
onUnhandledInboundChannelReadComplete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1331,19 +1317,18 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
|
public void handlerAdded(ChannelHandlerContext ctx) {
|
||||||
// NOOP
|
// NOOP
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
|
public void handlerRemoved(ChannelHandlerContext ctx) {
|
||||||
// NOOP
|
// NOOP
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void bind(
|
public void bind(
|
||||||
ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise)
|
ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) {
|
||||||
throws Exception {
|
|
||||||
unsafe.bind(localAddress, promise);
|
unsafe.bind(localAddress, promise);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1351,22 +1336,22 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
public void connect(
|
public void connect(
|
||||||
ChannelHandlerContext ctx,
|
ChannelHandlerContext ctx,
|
||||||
SocketAddress remoteAddress, SocketAddress localAddress,
|
SocketAddress remoteAddress, SocketAddress localAddress,
|
||||||
ChannelPromise promise) throws Exception {
|
ChannelPromise promise) {
|
||||||
unsafe.connect(remoteAddress, localAddress, promise);
|
unsafe.connect(remoteAddress, localAddress, promise);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
|
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
|
||||||
unsafe.disconnect(promise);
|
unsafe.disconnect(promise);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
|
public void close(ChannelHandlerContext ctx, ChannelPromise promise) {
|
||||||
unsafe.close(promise);
|
unsafe.close(promise);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
|
public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) {
|
||||||
unsafe.deregister(promise);
|
unsafe.deregister(promise);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1376,28 +1361,28 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
|
||||||
unsafe.write(msg, promise);
|
unsafe.write(msg, promise);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void flush(ChannelHandlerContext ctx) throws Exception {
|
public void flush(ChannelHandlerContext ctx) {
|
||||||
unsafe.flush();
|
unsafe.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||||
ctx.fireExceptionCaught(cause);
|
ctx.fireExceptionCaught(cause);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
|
public void channelRegistered(ChannelHandlerContext ctx) {
|
||||||
invokeHandlerAddedIfNeeded();
|
invokeHandlerAddedIfNeeded();
|
||||||
ctx.fireChannelRegistered();
|
ctx.fireChannelRegistered();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
|
public void channelUnregistered(ChannelHandlerContext ctx) {
|
||||||
ctx.fireChannelUnregistered();
|
ctx.fireChannelUnregistered();
|
||||||
|
|
||||||
// Remove all handlers sequentially if channel is closed and unregistered.
|
// Remove all handlers sequentially if channel is closed and unregistered.
|
||||||
@ -1407,24 +1392,24 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
public void channelActive(ChannelHandlerContext ctx) {
|
||||||
ctx.fireChannelActive();
|
ctx.fireChannelActive();
|
||||||
|
|
||||||
readIfIsAutoRead();
|
readIfIsAutoRead();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
public void channelInactive(ChannelHandlerContext ctx) {
|
||||||
ctx.fireChannelInactive();
|
ctx.fireChannelInactive();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
||||||
ctx.fireChannelRead(msg);
|
ctx.fireChannelRead(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
public void channelReadComplete(ChannelHandlerContext ctx) {
|
||||||
ctx.fireChannelReadComplete();
|
ctx.fireChannelReadComplete();
|
||||||
|
|
||||||
readIfIsAutoRead();
|
readIfIsAutoRead();
|
||||||
@ -1437,12 +1422,12 @@ public class DefaultChannelPipeline implements ChannelPipeline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
|
||||||
ctx.fireUserEventTriggered(evt);
|
ctx.fireUserEventTriggered(evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
|
public void channelWritabilityChanged(ChannelHandlerContext ctx) {
|
||||||
ctx.fireChannelWritabilityChanged();
|
ctx.fireChannelWritabilityChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user