From d1d9f131da5698ac797a78f1b4e71f28afbba463 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Fri, 14 Dec 2012 11:17:31 +0900 Subject: [PATCH] Tell NioTask the cause of unregistration - Add the 'cause' parameter to the channelUnregistered method --- .../socket/nio/AbstractNioByteChannel.java | 23 +++++++++- .../channel/socket/nio/NioEventLoop.java | 46 ++++++++++++++----- .../io/netty/channel/socket/nio/NioTask.java | 21 ++++++++- 3 files changed, 76 insertions(+), 14 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/socket/nio/AbstractNioByteChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/AbstractNioByteChannel.java index 09af899f30..702d4d44be 100755 --- a/transport/src/main/java/io/netty/channel/socket/nio/AbstractNioByteChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/AbstractNioByteChannel.java @@ -1,3 +1,19 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + /* * Copyright 2012 The Netty Project * @@ -187,7 +203,12 @@ abstract class AbstractNioByteChannel extends AbstractNioChannel { } @Override - public void channelUnregistered(SelectableChannel ch) throws Exception { + public void channelUnregistered(SelectableChannel ch, Throwable cause) throws Exception { + if (cause != null) { + future.setFailure(cause); + return; + } + if (writtenBytes < region.count()) { region.close(); if (!isOpen()) { diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioEventLoop.java b/transport/src/main/java/io/netty/channel/socket/nio/NioEventLoop.java index 29f1088946..970127db15 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioEventLoop.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioEventLoop.java @@ -1,3 +1,19 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + /* * Copyright 2012 The Netty Project * @@ -389,25 +405,31 @@ public final class NioEventLoop extends SingleThreadEventLoop { if (task == null) { break; } else { - invokeChannelUnregistered(task, ch.selectionKey()); + invokeChannelUnregistered(task, ch.selectionKey(), null); } } } private static void processSelectedKey(SelectionKey k, NioTask task) { - boolean success = false; + int state = 0; try { task.channelReady(k.channel(), k); - success = true; + state = 1; } catch (Exception e) { - logger.warn("Unexpected exception while running NioTask.channelReady() - cancelling the key", e); + k.cancel(); + invokeChannelUnregistered(task, k, e); + state = 2; } finally { - if (!success) { + switch (state) { + case 0: k.cancel(); - } - - if (!k.isValid()) { // Either cancelled by channelReady() or by this method. - invokeChannelUnregistered(task, k); + invokeChannelUnregistered(task, k, null); + break; + case 1: + if (!k.isValid()) { // Cancelled by channelReady() + invokeChannelUnregistered(task, k, null); + } + break; } } } @@ -424,7 +446,7 @@ public final class NioEventLoop extends SingleThreadEventLoop { k.cancel(); @SuppressWarnings("unchecked") NioTask task = (NioTask) a; - invokeChannelUnregistered(task, k); + invokeChannelUnregistered(task, k, null); } } @@ -434,9 +456,9 @@ public final class NioEventLoop extends SingleThreadEventLoop { } } - private static void invokeChannelUnregistered(NioTask task, SelectionKey k) { + private static void invokeChannelUnregistered(NioTask task, SelectionKey k, Throwable cause) { try { - task.channelUnregistered(k.channel()); + task.channelUnregistered(k.channel(), cause); } catch (Exception e) { logger.warn("Unexpected exception while running NioTask.channelUnregistered()", e); } diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioTask.java b/transport/src/main/java/io/netty/channel/socket/nio/NioTask.java index 5d3d1361e9..5bc20ceebf 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioTask.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioTask.java @@ -1,3 +1,19 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + /* * Copyright 2012 The Netty Project * @@ -33,6 +49,9 @@ public interface NioTask { /** * Invoked when the {@link SelectionKey} of the specified {@link SelectableChannel} has been cancelled and thus * this {@link NioTask} will not be notified anymore. + * + * @param cause the cause of the unregistration. {@code null} if a user called {@link SelectionKey#cancel()} or + * the event loop has been shut down. */ - void channelUnregistered(C ch) throws Exception; + void channelUnregistered(C ch, Throwable cause) throws Exception; }