From 4ef936bea1346b10d9fd77943287dac2a70458b5 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Sat, 9 Jan 2010 08:21:28 +0000 Subject: [PATCH] Reverted back previous check-in related with state update, which seems unnecessary --- .../socket/nio/NioAcceptedSocketChannel.java | 2 +- .../nio/NioClientSocketPipelineSink.java | 2 +- .../channel/socket/nio/NioSocketChannel.java | 20 ++++++--- .../netty/channel/socket/nio/NioWorker.java | 44 +++++++------------ 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioAcceptedSocketChannel.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioAcceptedSocketChannel.java index 7956576873..7b94f8e9c9 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioAcceptedSocketChannel.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioAcceptedSocketChannel.java @@ -45,7 +45,7 @@ final class NioAcceptedSocketChannel extends NioSocketChannel { this.bossThread = bossThread; - state = NioSocketChannel.ST_CONNECTED; + setConnected(); fireChannelOpen(this); fireChannelBound(this, getLocalAddress()); fireChannelConnected(this, getRemoteAddress()); diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java index 13c1931b23..8dbc8e01ec 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java @@ -124,7 +124,7 @@ class NioClientSocketPipelineSink extends AbstractChannelSink { try { channel.socket.socket().bind(localAddress); channel.boundManually = true; - channel.state = NioSocketChannel.ST_BOUND; + channel.setBound(); future.setSuccess(); fireChannelBound(channel, channel.getLocalAddress()); } catch (Throwable t) { 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 ad5e6d6b8f..cd82015903 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 @@ -45,11 +45,11 @@ import org.jboss.netty.util.internal.ThreadLocalBoolean; class NioSocketChannel extends AbstractChannel implements org.jboss.netty.channel.socket.SocketChannel { - static final int ST_OPEN = 0; - static final int ST_BOUND = 1; - static final int ST_CONNECTED = 2; - static final int ST_CLOSED = -1; - volatile int state = ST_OPEN; + private static final int ST_OPEN = 0; + private static final int ST_BOUND = 1; + private static final int ST_CONNECTED = 2; + private static final int ST_CLOSED = -1; + private volatile int state = ST_OPEN; final SocketChannel socket; final NioWorker worker; @@ -127,6 +127,16 @@ class NioSocketChannel extends AbstractChannel return state == ST_CONNECTED; } + final void setBound() { + assert state == ST_OPEN; + state = ST_BOUND; + } + + final void setConnected() { + assert state == ST_OPEN || state == ST_BOUND; + state = ST_CONNECTED; + } + @Override protected boolean setClosed() { state = ST_CLOSED; 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 4f341751ac..ba257ab7ee 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,6 +23,7 @@ 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; @@ -302,8 +303,7 @@ class NioWorker implements Runnable { } private static boolean read(SelectionKey k) { - java.nio.channels.SocketChannel ch = - (java.nio.channels.SocketChannel) k.channel(); + ScatteringByteChannel ch = (ScatteringByteChannel) k.channel(); NioSocketChannel channel = (NioSocketChannel) k.attachment(); ReceiveBufferSizePredictor predictor = @@ -325,15 +325,10 @@ class NioWorker implements Runnable { } } failure = false; + } catch (AsynchronousCloseException e) { + // Can happen, and does not need a user attention. } catch (Throwable t) { - if (!ch.isConnected()) { - channel.state = NioSocketChannel.ST_CLOSED; - } - if (t instanceof AsynchronousCloseException) { - // Can happen, and does not need a user attention. - } else { - fireExceptionCaught(channel, t); - } + fireExceptionCaught(channel, t); } if (readBytes > 0) { @@ -473,23 +468,18 @@ 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) { - if (!channel.socket.isConnected()) { - channel.state = NioSocketChannel.ST_CLOSED; - } - 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)); - } + channel.currentWriteEvent = null; + evt.getFuture().setFailure(t); + evt = null; + fireExceptionCaught(channel, t); + if (t instanceof IOException) { + open = false; + close(channel, succeededFuture(channel)); } } } @@ -782,7 +772,7 @@ class NioWorker implements Runnable { } if (!server) { - channel.state = NioSocketChannel.ST_CONNECTED; + channel.setConnected(); if (!((NioClientSocketChannel) channel).boundManually) { fireChannelBound(channel, localAddress); }