Fixed issue: NETTY-180 Channel.getRemoteAddress() can return null for a received MessageEvent when ExecutionHandler is in the pipeline.

* DatagramChannel implementations cache localAddresses now
* DatagramChannel implementations don't cache remoteAddress because a user can disconnect and then connect again.  However, MessageEvent.getRemoteAddress() will always have correct remoteAddress value, so it shouldn't be a problem at all.
This commit is contained in:
Trustin Lee 2009-06-23 07:52:12 +00:00
parent 5698904374
commit 590ebcc394
2 changed files with 30 additions and 7 deletions

View File

@ -120,6 +120,8 @@ class NioDatagramChannel extends AbstractChannel
*/ */
volatile boolean inWriteNowLoop; volatile boolean inWriteNowLoop;
private volatile InetSocketAddress localAddress;
NioDatagramChannel(final ChannelFactory factory, NioDatagramChannel(final ChannelFactory factory,
final ChannelPipeline pipeline, final ChannelSink sink, final ChannelPipeline pipeline, final ChannelSink sink,
final NioDatagramWorker worker) { final NioDatagramWorker worker) {
@ -142,12 +144,17 @@ class NioDatagramChannel extends AbstractChannel
} }
public InetSocketAddress getLocalAddress() { public InetSocketAddress getLocalAddress() {
try { InetSocketAddress localAddress = this.localAddress;
return (InetSocketAddress) datagramChannel.socket().getLocalSocketAddress(); if (localAddress == null) {
} catch (Throwable t) { try {
// Sometimes fails on a closed socket in Windows. this.localAddress = localAddress =
return null; (InetSocketAddress) datagramChannel.socket().getLocalSocketAddress();
} catch (Throwable t) {
// Sometimes fails on a closed socket in Windows.
return null;
}
} }
return localAddress;
} }
public InetSocketAddress getRemoteAddress() { public InetSocketAddress getRemoteAddress() {

View File

@ -57,6 +57,7 @@ final class OioDatagramChannel extends AbstractChannel
final Object interestOpsLock = new Object(); final Object interestOpsLock = new Object();
private final DatagramChannelConfig config; private final DatagramChannelConfig config;
volatile Thread workerThread; volatile Thread workerThread;
private volatile InetSocketAddress localAddress;
OioDatagramChannel( OioDatagramChannel(
ChannelFactory factory, ChannelFactory factory,
@ -87,11 +88,26 @@ final class OioDatagramChannel extends AbstractChannel
} }
public InetSocketAddress getLocalAddress() { public InetSocketAddress getLocalAddress() {
return (InetSocketAddress) socket.getLocalSocketAddress(); InetSocketAddress localAddress = this.localAddress;
if (localAddress == null) {
try {
this.localAddress = localAddress =
(InetSocketAddress) socket.getLocalSocketAddress();
} catch (Throwable t) {
// Sometimes fails on a closed socket in Windows.
return null;
}
}
return localAddress;
} }
public InetSocketAddress getRemoteAddress() { public InetSocketAddress getRemoteAddress() {
return (InetSocketAddress) socket.getRemoteSocketAddress(); try {
return (InetSocketAddress) socket.getRemoteSocketAddress();
} catch (Throwable t) {
// Sometimes fails on a closed socket in Windows.
return null;
}
} }
public boolean isBound() { public boolean isBound() {