Store datagram channel's remoteAddress as much as possible within the extent that does not cause a race condition
This commit is contained in:
parent
959e72a6b0
commit
c0267a9e4f
@ -22,7 +22,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.jboss.netty.channel.socket.nio;
|
package org.jboss.netty.channel.socket.nio;
|
||||||
|
|
||||||
import static org.jboss.netty.channel.Channels.*;
|
import static org.jboss.netty.channel.Channels.fireChannelInterestChanged;
|
||||||
|
import static org.jboss.netty.channel.Channels.fireChannelOpen;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
@ -121,6 +122,7 @@ class NioDatagramChannel extends AbstractChannel
|
|||||||
volatile boolean inWriteNowLoop;
|
volatile boolean inWriteNowLoop;
|
||||||
|
|
||||||
private volatile InetSocketAddress localAddress;
|
private volatile InetSocketAddress localAddress;
|
||||||
|
volatile InetSocketAddress remoteAddress;
|
||||||
|
|
||||||
NioDatagramChannel(final ChannelFactory factory,
|
NioDatagramChannel(final ChannelFactory factory,
|
||||||
final ChannelPipeline pipeline, final ChannelSink sink,
|
final ChannelPipeline pipeline, final ChannelSink sink,
|
||||||
@ -158,13 +160,18 @@ class NioDatagramChannel extends AbstractChannel
|
|||||||
}
|
}
|
||||||
|
|
||||||
public InetSocketAddress getRemoteAddress() {
|
public InetSocketAddress getRemoteAddress() {
|
||||||
|
InetSocketAddress remoteAddress = this.remoteAddress;
|
||||||
|
if (remoteAddress == null) {
|
||||||
try {
|
try {
|
||||||
return (InetSocketAddress) datagramChannel.socket().getRemoteSocketAddress();
|
this.remoteAddress = remoteAddress =
|
||||||
|
(InetSocketAddress) datagramChannel.socket().getRemoteSocketAddress();
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
// Sometimes fails on a closed socket in Windows.
|
// Sometimes fails on a closed socket in Windows.
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return remoteAddress;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isBound() {
|
public boolean isBound() {
|
||||||
return isOpen() && datagramChannel.socket().isBound();
|
return isOpen() && datagramChannel.socket().isBound();
|
||||||
|
@ -22,7 +22,11 @@
|
|||||||
*/
|
*/
|
||||||
package org.jboss.netty.channel.socket.nio;
|
package org.jboss.netty.channel.socket.nio;
|
||||||
|
|
||||||
import static org.jboss.netty.channel.Channels.*;
|
import static org.jboss.netty.channel.Channels.fireChannelBound;
|
||||||
|
import static org.jboss.netty.channel.Channels.fireChannelClosed;
|
||||||
|
import static org.jboss.netty.channel.Channels.fireChannelConnected;
|
||||||
|
import static org.jboss.netty.channel.Channels.fireChannelUnbound;
|
||||||
|
import static org.jboss.netty.channel.Channels.fireExceptionCaught;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
@ -174,6 +178,10 @@ class NioDatagramPipelineSink extends AbstractChannelSink {
|
|||||||
|
|
||||||
future.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
|
future.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
|
||||||
|
|
||||||
|
// Clear the cached address so that the next getRemoteAddress() call
|
||||||
|
// updates the cache.
|
||||||
|
channel.remoteAddress = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
channel.getDatagramChannel().connect(remoteAddress);
|
channel.getDatagramChannel().connect(remoteAddress);
|
||||||
connected = true;
|
connected = true;
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.jboss.netty.channel.socket.oio;
|
package org.jboss.netty.channel.socket.oio;
|
||||||
|
|
||||||
import static org.jboss.netty.channel.Channels.*;
|
import static org.jboss.netty.channel.Channels.fireChannelOpen;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
@ -58,6 +58,7 @@ final class OioDatagramChannel extends AbstractChannel
|
|||||||
private final DatagramChannelConfig config;
|
private final DatagramChannelConfig config;
|
||||||
volatile Thread workerThread;
|
volatile Thread workerThread;
|
||||||
private volatile InetSocketAddress localAddress;
|
private volatile InetSocketAddress localAddress;
|
||||||
|
volatile InetSocketAddress remoteAddress;
|
||||||
|
|
||||||
OioDatagramChannel(
|
OioDatagramChannel(
|
||||||
ChannelFactory factory,
|
ChannelFactory factory,
|
||||||
@ -102,13 +103,18 @@ final class OioDatagramChannel extends AbstractChannel
|
|||||||
}
|
}
|
||||||
|
|
||||||
public InetSocketAddress getRemoteAddress() {
|
public InetSocketAddress getRemoteAddress() {
|
||||||
|
InetSocketAddress remoteAddress = this.remoteAddress;
|
||||||
|
if (remoteAddress == null) {
|
||||||
try {
|
try {
|
||||||
return (InetSocketAddress) socket.getRemoteSocketAddress();
|
this.remoteAddress = remoteAddress =
|
||||||
|
(InetSocketAddress) socket.getRemoteSocketAddress();
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
// Sometimes fails on a closed socket in Windows.
|
// Sometimes fails on a closed socket in Windows.
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return remoteAddress;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isBound() {
|
public boolean isBound() {
|
||||||
return isOpen() && socket.isBound();
|
return isOpen() && socket.isBound();
|
||||||
|
@ -22,7 +22,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.jboss.netty.channel.socket.oio;
|
package org.jboss.netty.channel.socket.oio;
|
||||||
|
|
||||||
import static org.jboss.netty.channel.Channels.*;
|
import static org.jboss.netty.channel.Channels.fireChannelBound;
|
||||||
|
import static org.jboss.netty.channel.Channels.fireChannelConnected;
|
||||||
|
import static org.jboss.netty.channel.Channels.fireExceptionCaught;
|
||||||
|
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
@ -135,6 +137,10 @@ class OioDatagramPipelineSink extends AbstractChannelSink {
|
|||||||
|
|
||||||
future.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
|
future.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
|
||||||
|
|
||||||
|
// Clear the cached address so that the next getRemoteAddress() call
|
||||||
|
// updates the cache.
|
||||||
|
channel.remoteAddress = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
channel.socket.connect(remoteAddress);
|
channel.socket.connect(remoteAddress);
|
||||||
connected = true;
|
connected = true;
|
||||||
|
Loading…
Reference in New Issue
Block a user