Made sure channel registration doesn't fail in Windows unexpectedly

This commit is contained in:
Trustin Lee 2009-02-04 04:00:52 +00:00
parent 42afa85e53
commit 2d8b3b0a4e
2 changed files with 25 additions and 5 deletions

View File

@ -84,11 +84,21 @@ class NioSocketChannel extends AbstractChannel
} }
public InetSocketAddress getLocalAddress() { public InetSocketAddress getLocalAddress() {
return (InetSocketAddress) socket.socket().getLocalSocketAddress(); try {
return (InetSocketAddress) socket.socket().getLocalSocketAddress();
} catch (Throwable t) {
// Sometimes fails on a closed socket in Windows.
return null;
}
} }
public InetSocketAddress getRemoteAddress() { public InetSocketAddress getRemoteAddress() {
return (InetSocketAddress) socket.socket().getRemoteSocketAddress(); try {
return (InetSocketAddress) socket.socket().getRemoteSocketAddress();
} catch (Throwable t) {
// Sometimes fails on a closed socket in Windows.
return null;
}
} }
public boolean isBound() { public boolean isBound() {

View File

@ -25,6 +25,7 @@ package org.jboss.netty.channel.socket.nio;
import static org.jboss.netty.channel.Channels.*; import static org.jboss.netty.channel.Channels.*;
import java.io.IOException; import java.io.IOException;
import java.net.SocketAddress;
import java.nio.channels.AsynchronousCloseException; import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.ClosedChannelException; import java.nio.channels.ClosedChannelException;
import java.nio.channels.NotYetConnectedException; import java.nio.channels.NotYetConnectedException;
@ -628,6 +629,15 @@ class NioWorker implements Runnable {
} }
public void run() { public void run() {
SocketAddress localAddress = channel.getLocalAddress();
SocketAddress remoteAddress = channel.getRemoteAddress();
if (localAddress == null || remoteAddress == null) {
if (future != null) {
future.setFailure(new ClosedChannelException());
}
return;
}
try { try {
channel.socket.register(selector, SelectionKey.OP_READ, channel); channel.socket.register(selector, SelectionKey.OP_READ, channel);
if (future != null) { if (future != null) {
@ -641,11 +651,11 @@ class NioWorker implements Runnable {
if (server) { if (server) {
fireChannelOpen(channel); fireChannelOpen(channel);
fireChannelBound(channel, channel.getLocalAddress()); fireChannelBound(channel, localAddress);
} else if (!((NioClientSocketChannel) channel).boundManually) { } else if (!((NioClientSocketChannel) channel).boundManually) {
fireChannelBound(channel, channel.getLocalAddress()); fireChannelBound(channel, localAddress);
} }
fireChannelConnected(channel, channel.getRemoteAddress()); fireChannelConnected(channel, remoteAddress);
} }
} }
} }