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:
parent
5698904374
commit
590ebcc394
@ -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() {
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user