Tell NioTask the cause of unregistration

- Add the 'cause' parameter to the channelUnregistered method
This commit is contained in:
Trustin Lee 2012-12-14 11:17:31 +09:00
parent c4db51e85d
commit d1d9f131da
3 changed files with 76 additions and 14 deletions

View File

@ -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 * Copyright 2012 The Netty Project
* *
@ -187,7 +203,12 @@ abstract class AbstractNioByteChannel extends AbstractNioChannel {
} }
@Override @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()) { if (writtenBytes < region.count()) {
region.close(); region.close();
if (!isOpen()) { if (!isOpen()) {

View File

@ -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 * Copyright 2012 The Netty Project
* *
@ -389,25 +405,31 @@ public final class NioEventLoop extends SingleThreadEventLoop {
if (task == null) { if (task == null) {
break; break;
} else { } else {
invokeChannelUnregistered(task, ch.selectionKey()); invokeChannelUnregistered(task, ch.selectionKey(), null);
} }
} }
} }
private static void processSelectedKey(SelectionKey k, NioTask<SelectableChannel> task) { private static void processSelectedKey(SelectionKey k, NioTask<SelectableChannel> task) {
boolean success = false; int state = 0;
try { try {
task.channelReady(k.channel(), k); task.channelReady(k.channel(), k);
success = true; state = 1;
} catch (Exception e) { } catch (Exception e) {
logger.warn("Unexpected exception while running NioTask.channelReady() - cancelling the key", e); k.cancel();
invokeChannelUnregistered(task, k, e);
state = 2;
} finally { } finally {
if (!success) { switch (state) {
case 0:
k.cancel(); k.cancel();
} invokeChannelUnregistered(task, k, null);
break;
if (!k.isValid()) { // Either cancelled by channelReady() or by this method. case 1:
invokeChannelUnregistered(task, k); if (!k.isValid()) { // Cancelled by channelReady()
invokeChannelUnregistered(task, k, null);
}
break;
} }
} }
} }
@ -424,7 +446,7 @@ public final class NioEventLoop extends SingleThreadEventLoop {
k.cancel(); k.cancel();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a; NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
invokeChannelUnregistered(task, k); invokeChannelUnregistered(task, k, null);
} }
} }
@ -434,9 +456,9 @@ public final class NioEventLoop extends SingleThreadEventLoop {
} }
} }
private static void invokeChannelUnregistered(NioTask<SelectableChannel> task, SelectionKey k) { private static void invokeChannelUnregistered(NioTask<SelectableChannel> task, SelectionKey k, Throwable cause) {
try { try {
task.channelUnregistered(k.channel()); task.channelUnregistered(k.channel(), cause);
} catch (Exception e) { } catch (Exception e) {
logger.warn("Unexpected exception while running NioTask.channelUnregistered()", e); logger.warn("Unexpected exception while running NioTask.channelUnregistered()", e);
} }

View File

@ -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 * Copyright 2012 The Netty Project
* *
@ -33,6 +49,9 @@ public interface NioTask<C extends SelectableChannel> {
/** /**
* Invoked when the {@link SelectionKey} of the specified {@link SelectableChannel} has been cancelled and thus * Invoked when the {@link SelectionKey} of the specified {@link SelectableChannel} has been cancelled and thus
* this {@link NioTask} will not be notified anymore. * 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;
} }