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 8be02c0759..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,6 +45,7 @@ final class NioAcceptedSocketChannel extends NioSocketChannel { this.bossThread = bossThread; + 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 b9ea4c0f1c..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,6 +124,7 @@ class NioClientSocketPipelineSink extends AbstractChannelSink { try { channel.socket.socket().bind(localAddress); channel.boundManually = true; + 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 7e82ba2879..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,6 +45,12 @@ import org.jboss.netty.util.internal.ThreadLocalBoolean; class NioSocketChannel extends AbstractChannel implements org.jboss.netty.channel.socket.SocketChannel { + 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; private final NioSocketChannelConfig config; @@ -108,12 +114,33 @@ class NioSocketChannel extends AbstractChannel return remoteAddress; } + @Override + public boolean isOpen() { + return state >= ST_OPEN; + } + public boolean isBound() { - return isOpen() && socket.socket().isBound(); + return state >= ST_BOUND; } public boolean isConnected() { - return isOpen() && socket.isConnected(); + 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; + return super.setClosed(); } @Override @@ -155,11 +182,6 @@ class NioSocketChannel extends AbstractChannel super.setInterestOpsNow(interestOps); } - @Override - protected boolean setClosed() { - return super.setClosed(); - } - @Override public ChannelFuture write(Object message, SocketAddress remoteAddress) { if (remoteAddress == null || remoteAddress.equals(getRemoteAddress())) { 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 9af14ad5a2..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 @@ -772,6 +772,7 @@ class NioWorker implements Runnable { } if (!server) { + channel.setConnected(); if (!((NioClientSocketChannel) channel).boundManually) { fireChannelBound(channel, localAddress); }