From 4668cb792e739e692daef709c7ca741c93b01610 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Sat, 9 Jan 2010 07:53:23 +0000 Subject: [PATCH] Update the state flag if read or write fails --- .../channel/socket/nio/NioSocketChannel.java | 6 ++- .../netty/channel/socket/nio/NioWorker.java | 42 ++++++++++++------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioSocketChannel.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioSocketChannel.java index cd82015903..c1fbabfef4 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioSocketChannel.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioSocketChannel.java @@ -139,10 +139,14 @@ class NioSocketChannel extends AbstractChannel @Override protected boolean setClosed() { - state = ST_CLOSED; + setClosedFlag(); return super.setClosed(); } + final void setClosedFlag() { + state = ST_CLOSED; + } + @Override public int getInterestOps() { if (!isOpen()) { diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java index ba257ab7ee..e04df807c1 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java @@ -23,7 +23,6 @@ import java.nio.channels.AsynchronousCloseException; import java.nio.channels.CancelledKeyException; import java.nio.channels.ClosedChannelException; import java.nio.channels.NotYetConnectedException; -import java.nio.channels.ScatteringByteChannel; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.util.Iterator; @@ -303,7 +302,8 @@ class NioWorker implements Runnable { } private static boolean read(SelectionKey k) { - ScatteringByteChannel ch = (ScatteringByteChannel) k.channel(); + java.nio.channels.SocketChannel ch = + (java.nio.channels.SocketChannel) k.channel(); NioSocketChannel channel = (NioSocketChannel) k.attachment(); ReceiveBufferSizePredictor predictor = @@ -325,10 +325,15 @@ class NioWorker implements Runnable { } } failure = false; - } catch (AsynchronousCloseException e) { - // Can happen, and does not need a user attention. } catch (Throwable t) { - fireExceptionCaught(channel, t); + if (!ch.isConnected()) { + channel.setClosedFlag(); + } + if (t instanceof AsynchronousCloseException) { + // Can happen, and does not need a user attention. + } else { + fireExceptionCaught(channel, t); + } } if (readBytes > 0) { @@ -468,18 +473,23 @@ class NioWorker implements Runnable { addOpWrite = true; break; } - } catch (AsynchronousCloseException e) { - // Doesn't need a user attention - ignore. - channel.currentWriteEvent = evt; - channel.currentWriteIndex = bufIdx; } catch (Throwable t) { - channel.currentWriteEvent = null; - evt.getFuture().setFailure(t); - evt = null; - fireExceptionCaught(channel, t); - if (t instanceof IOException) { - open = false; - close(channel, succeededFuture(channel)); + if (!channel.socket.isConnected()) { + channel.setClosedFlag(); + } + if (t instanceof AsynchronousCloseException) { + // Doesn't need a user attention - ignore. + channel.currentWriteEvent = evt; + channel.currentWriteIndex = bufIdx; + } else { + channel.currentWriteEvent = null; + evt.getFuture().setFailure(t); + evt = null; + fireExceptionCaught(channel, t); + if (t instanceof IOException) { + open = false; + close(channel, succeededFuture(channel)); + } } } }