Fixed issue: NETTY-180 Channel.getRemoteAddress() can return null for a received MessageEvent when ExecutionHandler is in the pipeline.
* All Channel implementations but DatagramChannel now cache localAddress and remoteAddress so that they do not return null even after the connection has been closed.
This commit is contained in:
parent
8448188706
commit
5698904374
@ -69,15 +69,15 @@ final class DefaultLocalChannel extends AbstractChannel implements LocalChannel
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isBound() {
|
public boolean isBound() {
|
||||||
return isOpen() && bound.get();
|
return bound.get() && isOpen();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
return localAddress != null && remoteAddress != null;
|
return pairedChannel != null && isOpen();
|
||||||
}
|
}
|
||||||
|
|
||||||
public LocalAddress getLocalAddress() {
|
public LocalAddress getLocalAddress() {
|
||||||
return isBound()? localAddress : null;
|
return localAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LocalAddress getRemoteAddress() {
|
public LocalAddress getRemoteAddress() {
|
||||||
@ -95,7 +95,6 @@ final class DefaultLocalChannel extends AbstractChannel implements LocalChannel
|
|||||||
DefaultLocalChannel pairedChannel = this.pairedChannel;
|
DefaultLocalChannel pairedChannel = this.pairedChannel;
|
||||||
if (pairedChannel != null) {
|
if (pairedChannel != null) {
|
||||||
this.pairedChannel = null;
|
this.pairedChannel = null;
|
||||||
this.localAddress = null;
|
|
||||||
fireChannelDisconnected(this);
|
fireChannelDisconnected(this);
|
||||||
fireChannelUnbound(this);
|
fireChannelUnbound(this);
|
||||||
}
|
}
|
||||||
@ -109,7 +108,6 @@ final class DefaultLocalChannel extends AbstractChannel implements LocalChannel
|
|||||||
DefaultLocalChannel me = pairedChannel.pairedChannel;
|
DefaultLocalChannel me = pairedChannel.pairedChannel;
|
||||||
if (me != null) {
|
if (me != null) {
|
||||||
pairedChannel.pairedChannel = null;
|
pairedChannel.pairedChannel = null;
|
||||||
pairedChannel.localAddress = null;
|
|
||||||
fireChannelDisconnected(pairedChannel);
|
fireChannelDisconnected(pairedChannel);
|
||||||
fireChannelUnbound(pairedChannel);
|
fireChannelUnbound(pairedChannel);
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,8 @@ class NioSocketChannel extends AbstractChannel
|
|||||||
final SocketChannel socket;
|
final SocketChannel socket;
|
||||||
final NioWorker worker;
|
final NioWorker worker;
|
||||||
private final NioSocketChannelConfig config;
|
private final NioSocketChannelConfig config;
|
||||||
|
private volatile InetSocketAddress localAddress;
|
||||||
|
private volatile InetSocketAddress remoteAddress;
|
||||||
|
|
||||||
final Object interestOpsLock = new Object();
|
final Object interestOpsLock = new Object();
|
||||||
final Object writeLock = new Object();
|
final Object writeLock = new Object();
|
||||||
@ -86,21 +88,31 @@ class NioSocketChannel extends AbstractChannel
|
|||||||
}
|
}
|
||||||
|
|
||||||
public InetSocketAddress getLocalAddress() {
|
public InetSocketAddress getLocalAddress() {
|
||||||
try {
|
InetSocketAddress localAddress = this.localAddress;
|
||||||
return (InetSocketAddress) socket.socket().getLocalSocketAddress();
|
if (localAddress == null) {
|
||||||
} catch (Throwable t) {
|
try {
|
||||||
// Sometimes fails on a closed socket in Windows.
|
this.localAddress = localAddress =
|
||||||
return null;
|
(InetSocketAddress) socket.socket().getLocalSocketAddress();
|
||||||
|
} catch (Throwable t) {
|
||||||
|
// Sometimes fails on a closed socket in Windows.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return localAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InetSocketAddress getRemoteAddress() {
|
public InetSocketAddress getRemoteAddress() {
|
||||||
try {
|
InetSocketAddress remoteAddress = this.remoteAddress;
|
||||||
return (InetSocketAddress) socket.socket().getRemoteSocketAddress();
|
if (remoteAddress == null) {
|
||||||
} catch (Throwable t) {
|
try {
|
||||||
// Sometimes fails on a closed socket in Windows.
|
this.remoteAddress = remoteAddress =
|
||||||
return null;
|
(InetSocketAddress) socket.socket().getRemoteSocketAddress();
|
||||||
|
} catch (Throwable t) {
|
||||||
|
// Sometimes fails on a closed socket in Windows.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return remoteAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isBound() {
|
public boolean isBound() {
|
||||||
|
@ -53,6 +53,8 @@ abstract class OioSocketChannel extends AbstractChannel
|
|||||||
final Object interestOpsLock = new Object();
|
final Object interestOpsLock = new Object();
|
||||||
private final SocketChannelConfig config;
|
private final SocketChannelConfig config;
|
||||||
volatile Thread workerThread;
|
volatile Thread workerThread;
|
||||||
|
private volatile InetSocketAddress localAddress;
|
||||||
|
private volatile InetSocketAddress remoteAddress;
|
||||||
|
|
||||||
OioSocketChannel(
|
OioSocketChannel(
|
||||||
Channel parent,
|
Channel parent,
|
||||||
@ -72,11 +74,31 @@ abstract class OioSocketChannel 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();
|
InetSocketAddress remoteAddress = this.remoteAddress;
|
||||||
|
if (remoteAddress == null) {
|
||||||
|
try {
|
||||||
|
this.remoteAddress = remoteAddress =
|
||||||
|
(InetSocketAddress) socket.getRemoteSocketAddress();
|
||||||
|
} catch (Throwable t) {
|
||||||
|
// Sometimes fails on a closed socket in Windows.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return remoteAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isBound() {
|
public boolean isBound() {
|
||||||
|
@ -55,6 +55,8 @@ class BaseXnioChannel extends AbstractChannel implements XnioChannel {
|
|||||||
|
|
||||||
private final XnioChannelConfig config;
|
private final XnioChannelConfig config;
|
||||||
volatile java.nio.channels.Channel xnioChannel;
|
volatile java.nio.channels.Channel xnioChannel;
|
||||||
|
private volatile SocketAddress localAddress;
|
||||||
|
private volatile SocketAddress remoteAddress;
|
||||||
|
|
||||||
final Object writeLock = new Object();
|
final Object writeLock = new Object();
|
||||||
final Queue<MessageEvent> writeBuffer = new WriteBuffer();
|
final Queue<MessageEvent> writeBuffer = new WriteBuffer();
|
||||||
@ -76,21 +78,31 @@ class BaseXnioChannel extends AbstractChannel implements XnioChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public SocketAddress getLocalAddress() {
|
public SocketAddress getLocalAddress() {
|
||||||
java.nio.channels.Channel xnioChannel = this.xnioChannel;
|
SocketAddress localAddress = this.localAddress;
|
||||||
if (!isOpen() || !(xnioChannel instanceof BoundChannel)) {
|
if (localAddress == null) {
|
||||||
return null;
|
java.nio.channels.Channel xnioChannel = this.xnioChannel;
|
||||||
}
|
if (!isOpen() || !(xnioChannel instanceof BoundChannel)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return (SocketAddress) ((BoundChannel) xnioChannel).getLocalAddress();
|
this.localAddress = localAddress =
|
||||||
|
(SocketAddress) ((BoundChannel) xnioChannel).getLocalAddress();
|
||||||
|
}
|
||||||
|
return localAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SocketAddress getRemoteAddress() {
|
public SocketAddress getRemoteAddress() {
|
||||||
java.nio.channels.Channel xnioChannel = this.xnioChannel;
|
SocketAddress remoteAddress = this.remoteAddress;
|
||||||
if (!isOpen() || !(xnioChannel instanceof ConnectedChannel)) {
|
if (remoteAddress == null) {
|
||||||
return null;
|
java.nio.channels.Channel xnioChannel = this.xnioChannel;
|
||||||
}
|
if (!isOpen() || !(xnioChannel instanceof ConnectedChannel)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return (SocketAddress) ((ConnectedChannel) xnioChannel).getPeerAddress();
|
this.remoteAddress = remoteAddress =
|
||||||
|
(SocketAddress) ((ConnectedChannel) xnioChannel).getPeerAddress();
|
||||||
|
}
|
||||||
|
return remoteAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isBound() {
|
public boolean isBound() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user