From 2d8b3b0a4e7fe1c831b51fab6bb90feba22df217 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 4 Feb 2009 04:00:52 +0000 Subject: [PATCH] Made sure channel registration doesn't fail in Windows unexpectedly --- .../channel/socket/nio/NioSocketChannel.java | 14 ++++++++++++-- .../netty/channel/socket/nio/NioWorker.java | 16 +++++++++++++--- 2 files changed, 25 insertions(+), 5 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 a998995c48..6cdc3e2a0b 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 @@ -84,11 +84,21 @@ class NioSocketChannel extends AbstractChannel } 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() { - 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() { 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 d758f1bfd2..93c0ac02dc 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 @@ -25,6 +25,7 @@ package org.jboss.netty.channel.socket.nio; import static org.jboss.netty.channel.Channels.*; import java.io.IOException; +import java.net.SocketAddress; import java.nio.channels.AsynchronousCloseException; import java.nio.channels.ClosedChannelException; import java.nio.channels.NotYetConnectedException; @@ -628,6 +629,15 @@ class NioWorker implements Runnable { } 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 { channel.socket.register(selector, SelectionKey.OP_READ, channel); if (future != null) { @@ -641,11 +651,11 @@ class NioWorker implements Runnable { if (server) { fireChannelOpen(channel); - fireChannelBound(channel, channel.getLocalAddress()); + fireChannelBound(channel, localAddress); } else if (!((NioClientSocketChannel) channel).boundManually) { - fireChannelBound(channel, channel.getLocalAddress()); + fireChannelBound(channel, localAddress); } - fireChannelConnected(channel, channel.getRemoteAddress()); + fireChannelConnected(channel, remoteAddress); } } }