From 32ccdcdb189996211bff873b8cf2cd42f66f5a5e Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sat, 22 Mar 2014 15:01:49 +0100 Subject: [PATCH] Make sure the local / remote InetSocketAddres can be obtained. Part of [#2262] Motivation: Make sure the remote/local InetSocketAddress can be obtained correctly Modifications: Set the remote/local InetSocketAddress after a bind/connect operation was performed Result: It is possible to still access the informations even after the fd became invalid. This mirror the behaviour of NIO. --- .../netty/channel/epoll/EpollServerSocketChannel.java | 2 +- .../io/netty/channel/epoll/EpollSocketChannel.java | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollServerSocketChannel.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollServerSocketChannel.java index b4aba6e3da..fb851e1ab8 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollServerSocketChannel.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollServerSocketChannel.java @@ -49,7 +49,7 @@ public final class EpollServerSocketChannel extends AbstractEpollChannel impleme InetSocketAddress addr = (InetSocketAddress) localAddress; checkResolvable(addr); Native.bind(fd, addr.getAddress(), addr.getPort()); - local = addr; + local = Native.localAddress(fd); Native.listen(fd, config.getBacklog()); active = true; } diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java index f053ca8929..40969464cd 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollSocketChannel.java @@ -41,7 +41,6 @@ import java.io.IOException; import java.net.ConnectException; import java.net.InetSocketAddress; import java.net.SocketAddress; -import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -62,6 +61,8 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So private ScheduledFuture connectTimeoutFuture; private SocketAddress requestedRemoteAddress; + private volatile InetSocketAddress local; + private volatile InetSocketAddress remote; private volatile boolean inputShutdown; private volatile boolean outputShutdown; @@ -82,18 +83,19 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So @Override protected SocketAddress localAddress0() { - return Native.localAddress(fd); + return local; } @Override protected SocketAddress remoteAddress0() { - return Native.remoteAddress(fd); + return remote; } @Override protected void doBind(SocketAddress local) throws Exception { InetSocketAddress localAddress = (InetSocketAddress) local; Native.bind(fd, localAddress.getAddress(), localAddress.getPort()); + this.local = Native.localAddress(fd); } private void setEpollOut() { @@ -538,6 +540,8 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So checkResolvable(remoteAddress); boolean connected = Native.connect(fd, remoteAddress.getAddress(), remoteAddress.getPort()); + remote = remoteAddress; + local = Native.localAddress(fd); if (!connected) { setEpollOut(); }