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

View File

@ -57,6 +57,7 @@ final class OioDatagramChannel extends AbstractChannel
final Object interestOpsLock = new Object();
private final DatagramChannelConfig config;
volatile Thread workerThread;
private volatile InetSocketAddress localAddress;
OioDatagramChannel(
ChannelFactory factory,
@ -87,11 +88,26 @@ final class OioDatagramChannel extends AbstractChannel
}
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() {
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() {