diff options
author | Benoit Foucher <benoit@zeroc.com> | 2007-11-27 11:58:35 +0100 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2007-11-27 11:58:35 +0100 |
commit | 47f800495093fd7679a315e2d730fea22f6135b7 (patch) | |
tree | a7b8d3488f3841367dd03d10cae293f36fd10481 /java/src/IceInternal/Network.java | |
parent | Fixed SystemException to no longer derive from LocalException (diff) | |
download | ice-47f800495093fd7679a315e2d730fea22f6135b7.tar.bz2 ice-47f800495093fd7679a315e2d730fea22f6135b7.tar.xz ice-47f800495093fd7679a315e2d730fea22f6135b7.zip |
- Added support for non-blocking AMI/batch requests, connection
creation.
- Added support for AMI oneway requests.
- Changed collocation optimization to not perform any DNS lookups.
Diffstat (limited to 'java/src/IceInternal/Network.java')
-rw-r--r-- | java/src/IceInternal/Network.java | 103 |
1 files changed, 88 insertions, 15 deletions
diff --git a/java/src/IceInternal/Network.java b/java/src/IceInternal/Network.java index 07770711f45..5971e1122d5 100644 --- a/java/src/IceInternal/Network.java +++ b/java/src/IceInternal/Network.java @@ -162,7 +162,7 @@ public final class Network } } - private static void + public static void closeSocketNoThrow(java.nio.channels.SelectableChannel fd) { try @@ -274,13 +274,74 @@ public final class Network } } - public static void + public static boolean doConnect(java.nio.channels.SocketChannel fd, java.net.InetSocketAddress addr, int timeout) { try { if(!fd.connect(addr)) { + if(timeout == 0) + { + return false; + } + + try + { + doFinishConnect(fd, timeout); + } + catch(Ice.LocalException ex) + { + closeSocketNoThrow(fd); + throw ex; + } + return true; + } + } + catch(java.net.ConnectException ex) + { + closeSocketNoThrow(fd); + + Ice.ConnectFailedException se; + if(connectionRefused(ex)) + { + se = new Ice.ConnectionRefusedException(); + } + else + { + se = new Ice.ConnectFailedException(); + } + se.initCause(ex); + throw se; + } + catch(java.io.IOException ex) + { + closeSocketNoThrow(fd); + Ice.SocketException se = new Ice.SocketException(); + se.initCause(ex); + throw se; + } + + if(addr.equals(fd.socket().getLocalSocketAddress())) + { + closeSocketNoThrow(fd); + throw new Ice.ConnectionRefusedException(); + } + return true; + } + + public static void + doFinishConnect(java.nio.channels.SocketChannel fd, int timeout) + { + // + // Note: we don't close the socket if there's an exception. It's the responsibility + // of the caller to do so. + // + + if(timeout != 0) + { + try + { java.nio.channels.Selector selector = java.nio.channels.Selector.open(); try { @@ -288,17 +349,13 @@ public final class Network { try { - java.nio.channels.SelectionKey key = + java.nio.channels.SelectionKey key = fd.register(selector, java.nio.channels.SelectionKey.OP_CONNECT); int n; if(timeout > 0) { n = selector.select(timeout); } - else if(timeout == 0) - { - n = selector.selectNow(); - } else { n = selector.select(); @@ -306,7 +363,6 @@ public final class Network if(n == 0) { - closeSocketNoThrow(fd); throw new Ice.ConnectTimeoutException(); } @@ -335,17 +391,35 @@ public final class Network // Ignore } } + } + catch(java.io.IOException ex) + { + Ice.SocketException se = new Ice.SocketException(); + se.initCause(ex); + throw se; + } + } - if(!fd.finishConnect()) - { - throw new Ice.ConnectFailedException(); - } + try + { + if(!fd.finishConnect()) + { + throw new Ice.ConnectFailedException(); + } + + // + // Prevent self connect (self connect happens on Linux when a client tries to connect to + // a server which was just deactivated if the client socket re-uses the same ephemeral + // port as the server). + // + java.net.SocketAddress addr = fd.socket().getRemoteSocketAddress(); + if(addr != null && addr.equals(fd.socket().getLocalSocketAddress())) + { + throw new Ice.ConnectionRefusedException(); } } catch(java.net.ConnectException ex) { - closeSocketNoThrow(fd); - Ice.ConnectFailedException se; if(connectionRefused(ex)) { @@ -360,7 +434,6 @@ public final class Network } catch(java.io.IOException ex) { - closeSocketNoThrow(fd); Ice.SocketException se = new Ice.SocketException(); se.initCause(ex); throw se; |