diff options
author | Mark Spruiell <mes@zeroc.com> | 2006-04-10 18:25:35 +0000 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2006-04-10 18:25:35 +0000 |
commit | 2273b3bfc528377f10dd4ab24b22ea840a2031f1 (patch) | |
tree | e7cdfb924fa3eac6f78fe2d4978c495449a63d6b /java/src | |
parent | removing bogus deprecate metadata (diff) | |
download | ice-2273b3bfc528377f10dd4ab24b22ea840a2031f1.tar.bz2 ice-2273b3bfc528377f10dd4ab24b22ea840a2031f1.tar.xz ice-2273b3bfc528377f10dd4ab24b22ea840a2031f1.zip |
eliminating repeated creation of Selectors
Diffstat (limited to 'java/src')
-rw-r--r-- | java/src/IceInternal/TcpAcceptor.java | 94 | ||||
-rw-r--r-- | java/src/IceInternal/TcpTransceiver.java | 260 |
2 files changed, 217 insertions, 137 deletions
diff --git a/java/src/IceInternal/TcpAcceptor.java b/java/src/IceInternal/TcpAcceptor.java index 608d18a14bf..5a9f91ccf2b 100644 --- a/java/src/IceInternal/TcpAcceptor.java +++ b/java/src/IceInternal/TcpAcceptor.java @@ -27,10 +27,13 @@ class TcpAcceptor implements Acceptor } java.nio.channels.ServerSocketChannel fd; + java.nio.channels.Selector selector; synchronized(this) { fd = _fd; + selector = _selector; _fd = null; + _selector = null; } if(fd != null) { @@ -43,6 +46,17 @@ class TcpAcceptor implements Acceptor // Ignore. } } + if(selector != null) + { + try + { + selector.close(); + } + catch(java.io.IOException ex) + { + // Ignore. + } + } } public void @@ -60,7 +74,84 @@ class TcpAcceptor implements Acceptor public Transceiver accept(int timeout) { - java.nio.channels.SocketChannel fd = Network.doAccept(_fd, timeout); + java.nio.channels.SocketChannel fd = null; + while(fd == null) + { + try + { + fd = _fd.accept(); + if(fd == null) + { + if(_selector == null) + { + _selector = java.nio.channels.Selector.open(); + } + + while(true) + { + try + { + java.nio.channels.SelectionKey key = + _fd.register(_selector, java.nio.channels.SelectionKey.OP_ACCEPT); + if(timeout > 0) + { + if(_selector.select(timeout) == 0) + { + throw new Ice.TimeoutException(); + } + } + else if(timeout == 0) + { + if(_selector.selectNow() == 0) + { + throw new Ice.TimeoutException(); + } + } + else + { + _selector.select(); + } + + break; + } + catch(java.io.IOException ex) + { + if(Network.interrupted(ex)) + { + continue; + } + Ice.SocketException se = new Ice.SocketException(); + se.initCause(ex); + throw se; + } + } + } + } + catch(java.io.IOException ex) + { + if(Network.interrupted(ex)) + { + continue; + } + Ice.SocketException se = new Ice.SocketException(); + se.initCause(ex); + throw se; + } + } + + try + { + java.net.Socket socket = fd.socket(); + socket.setTcpNoDelay(true); + socket.setKeepAlive(true); + } + catch(java.io.IOException ex) + { + Ice.SocketException se = new Ice.SocketException(); + se.initCause(ex); + throw se; + } + if(!_instance.threadPerConnection()) { Network.setBlock(fd, false); @@ -149,4 +240,5 @@ class TcpAcceptor implements Acceptor private java.nio.channels.ServerSocketChannel _fd; private int _backlog; private java.net.InetSocketAddress _addr; + private java.nio.channels.Selector _selector; } diff --git a/java/src/IceInternal/TcpTransceiver.java b/java/src/IceInternal/TcpTransceiver.java index 8e29b45e841..22364ec5482 100644 --- a/java/src/IceInternal/TcpTransceiver.java +++ b/java/src/IceInternal/TcpTransceiver.java @@ -44,6 +44,30 @@ final class TcpTransceiver implements Transceiver { _fd = null; } + if(_readSelector != null) + { + try + { + _readSelector.close(); + } + catch(java.io.IOException ex) + { + // Ignore. + } + _readSelector = null; + } + if(_writeSelector != null) + { + try + { + _writeSelector.close(); + } + catch(java.io.IOException ex) + { + // Ignore. + } + _writeSelector = null; + } } } @@ -117,95 +141,76 @@ final class TcpTransceiver implements Transceiver { java.nio.ByteBuffer buf = stream.prepareWrite(); - java.nio.channels.Selector selector = null; - - try + while(buf.hasRemaining()) { - while(buf.hasRemaining()) + try { - try + assert(_fd != null); + int ret = _fd.write(buf); + + if(ret == -1) + { + throw new Ice.ConnectionLostException(); + } + + if(ret == 0) { - assert(_fd != null); - int ret = _fd.write(buf); + if(timeout == 0) + { + throw new Ice.TimeoutException(); + } - if(ret == -1) + if(_writeSelector == null) { - throw new Ice.ConnectionLostException(); + _writeSelector = java.nio.channels.Selector.open(); + _fd.register(_writeSelector, java.nio.channels.SelectionKey.OP_WRITE, null); } - if(ret == 0) + try { - if(timeout == 0) - { - throw new Ice.TimeoutException(); - } - - if(selector == null) - { - selector = java.nio.channels.Selector.open(); - _fd.register(selector, java.nio.channels.SelectionKey.OP_WRITE, null); - } - - try + if(timeout > 0) { - if(timeout > 0) + long start = System.currentTimeMillis(); + int n = _writeSelector.select(timeout); + if(n == 0 && System.currentTimeMillis() >= start + timeout) { - long start = System.currentTimeMillis(); - int n = selector.select(timeout); - if(n == 0 && System.currentTimeMillis() >= start + timeout) - { - throw new Ice.TimeoutException(); - } - } - else - { - selector.select(); + throw new Ice.TimeoutException(); } } - catch(java.io.InterruptedIOException ex) + else { - // Ignore. + _writeSelector.select(); } - - continue; } - - - if(_traceLevels.network >= 3) + catch(java.io.InterruptedIOException ex) { - String s = "sent " + ret + " of " + buf.limit() + " bytes via tcp\n" + toString(); - _logger.trace(_traceLevels.networkCat, s); + // Ignore. } - if(_stats != null) - { - _stats.bytesSent(type(), ret); - } + continue; } - catch(java.io.InterruptedIOException ex) + + + if(_traceLevels.network >= 3) { - continue; + String s = "sent " + ret + " of " + buf.limit() + " bytes via tcp\n" + toString(); + _logger.trace(_traceLevels.networkCat, s); } - catch(java.io.IOException ex) + + if(_stats != null) { - Ice.SocketException se = new Ice.SocketException(); - se.initCause(ex); - throw se; + _stats.bytesSent(type(), ret); } } - } - finally - { - if(selector != null) + catch(java.io.InterruptedIOException ex) { - try - { - selector.close(); - } - catch(java.io.IOException ex) - { - // Ignore. - } + continue; + } + catch(java.io.IOException ex) + { + Ice.SocketException se = new Ice.SocketException(); + se.initCause(ex); + throw se; } } } @@ -221,104 +226,85 @@ final class TcpTransceiver implements Transceiver remaining = buf.remaining(); } - java.nio.channels.Selector selector = null; - - try + while(buf.hasRemaining()) { - while(buf.hasRemaining()) + try { - try + assert(_fd != null); + int ret = _fd.read(buf); + + if(ret == -1) { - assert(_fd != null); - int ret = _fd.read(buf); - - if(ret == -1) + throw new Ice.ConnectionLostException(); + } + + if(ret == 0) + { + if(timeout == 0) + { + throw new Ice.TimeoutException(); + } + + if(_readSelector == null) { - throw new Ice.ConnectionLostException(); + _readSelector = java.nio.channels.Selector.open(); + _fd.register(_readSelector, java.nio.channels.SelectionKey.OP_READ, null); } - if(ret == 0) + try { - if(timeout == 0) - { - throw new Ice.TimeoutException(); - } - - if(selector == null) - { - selector = java.nio.channels.Selector.open(); - _fd.register(selector, java.nio.channels.SelectionKey.OP_READ, null); - } - - try + if(timeout > 0) { - if(timeout > 0) + long start = System.currentTimeMillis(); + int n = _readSelector.select(timeout); + if(n == 0 && System.currentTimeMillis() >= start + timeout) { - long start = System.currentTimeMillis(); - int n = selector.select(timeout); - if(n == 0 && System.currentTimeMillis() >= start + timeout) - { - throw new Ice.TimeoutException(); - } - } - else - { - selector.select(); + throw new Ice.TimeoutException(); } } - catch(java.io.InterruptedIOException ex) + else { - // Ignore. + _readSelector.select(); } - - continue; } - - if(ret > 0) + catch(java.io.InterruptedIOException ex) { - if(_traceLevels.network >= 3) - { - String s = "received " + ret + " of " + remaining + " bytes via tcp\n" + toString(); - _logger.trace(_traceLevels.networkCat, s); - } + // Ignore. + } - if(_stats != null) - { - _stats.bytesReceived(type(), ret); - } - } - } - catch(java.io.InterruptedIOException ex) - { continue; } - catch(java.io.IOException ex) + + if(ret > 0) { - if(Network.connectionLost(ex)) + if(_traceLevels.network >= 3) { - Ice.ConnectionLostException se = new Ice.ConnectionLostException(); - se.initCause(ex); - throw se; + String s = "received " + ret + " of " + remaining + " bytes via tcp\n" + toString(); + _logger.trace(_traceLevels.networkCat, s); + } + + if(_stats != null) + { + _stats.bytesReceived(type(), ret); } - - Ice.SocketException se = new Ice.SocketException(); - se.initCause(ex); - throw se; } } - } - finally - { - if(selector != null) + catch(java.io.InterruptedIOException ex) { - try - { - selector.close(); - } - catch(java.io.IOException ex) + continue; + } + catch(java.io.IOException ex) + { + if(Network.connectionLost(ex)) { - // Ignore. + Ice.ConnectionLostException se = new Ice.ConnectionLostException(); + se.initCause(ex); + throw se; } + + Ice.SocketException se = new Ice.SocketException(); + se.initCause(ex); + throw se; } } } @@ -361,4 +347,6 @@ final class TcpTransceiver implements Transceiver private Ice.Logger _logger; private Ice.Stats _stats; private String _desc; + private java.nio.channels.Selector _readSelector; + private java.nio.channels.Selector _writeSelector; } |