diff options
Diffstat (limited to 'java/src/IceInternal/UdpTransceiver.java')
-rw-r--r-- | java/src/IceInternal/UdpTransceiver.java | 490 |
1 files changed, 245 insertions, 245 deletions
diff --git a/java/src/IceInternal/UdpTransceiver.java b/java/src/IceInternal/UdpTransceiver.java index 5c68802d5ea..92ad59db9e3 100644 --- a/java/src/IceInternal/UdpTransceiver.java +++ b/java/src/IceInternal/UdpTransceiver.java @@ -21,66 +21,66 @@ final class UdpTransceiver implements Transceiver public synchronized void close() { - // - // NOTE: closeSocket() may have already been invoked by shutdownReadWrite(). - // - closeSocket(); - - if(_readSelector != null) - { - try - { - _readSelector.close(); - } - catch(java.io.IOException ex) - { - // Ignore. - } - _readSelector = null; - } + // + // NOTE: closeSocket() may have already been invoked by shutdownReadWrite(). + // + closeSocket(); + + if(_readSelector != null) + { + try + { + _readSelector.close(); + } + catch(java.io.IOException ex) + { + // Ignore. + } + _readSelector = null; + } } public void shutdownWrite() { - // - // NOTE: DatagramSocket does not support shutdownOutput. - // + // + // NOTE: DatagramSocket does not support shutdownOutput. + // } public synchronized void shutdownReadWrite() { - // - // NOTE: DatagramSocket does not support shutdownInput, and we - // cannot use the C++ technique of sending a "wakeup" packet to - // this socket because the Java implementation deadlocks when we - // call disconnect() while receive() is in progress. Therefore - // we close the socket here and wake up the selector. - // - closeSocket(); - - if(_readSelector != null) - { - _readSelector.wakeup(); - } + // + // NOTE: DatagramSocket does not support shutdownInput, and we + // cannot use the C++ technique of sending a "wakeup" packet to + // this socket because the Java implementation deadlocks when we + // call disconnect() while receive() is in progress. Therefore + // we close the socket here and wake up the selector. + // + closeSocket(); + + if(_readSelector != null) + { + _readSelector.wakeup(); + } } public void write(BasicStream stream, int timeout) // NOTE: timeout is not used - throws LocalExceptionWrapper + throws LocalExceptionWrapper { java.nio.ByteBuffer buf = stream.prepareWrite(); assert(buf.position() == 0); final int packetSize = java.lang.Math.min(_maxPacketSize, _sndSize - _udpOverhead); if(packetSize < buf.limit()) - { - // - // We don't log a warning here because the client gets an exception anyway. - // - throw new Ice.DatagramLimitException(); - } + { + // + // We don't log a warning here because the client gets an exception anyway. + // + throw new Ice.DatagramLimitException(); + } while(buf.hasRemaining()) { @@ -103,12 +103,12 @@ final class UdpTransceiver implements Transceiver assert(ret == buf.limit()); break; } - catch(java.nio.channels.AsynchronousCloseException ex) - { + catch(java.nio.channels.AsynchronousCloseException ex) + { Ice.ConnectionLostException se = new Ice.ConnectionLostException(); se.initCause(ex); throw se; - } + } catch(java.net.PortUnreachableException ex) { Ice.ConnectionLostException se = new Ice.ConnectionLostException(); @@ -131,128 +131,128 @@ final class UdpTransceiver implements Transceiver public boolean read(BasicStream stream, int timeout) // NOTE: timeout is not used { - assert(stream.pos() == 0); - - final int packetSize = java.lang.Math.min(_maxPacketSize, _rcvSize - _udpOverhead); - if(packetSize < stream.size()) - { - // - // We log a warning here because this is the server side -- without the - // the warning, there would only be silence. - // - if(_warn) - { - _logger.warning("DatagramLimitException: maximum size of " + packetSize + " exceeded"); - } - throw new Ice.DatagramLimitException(); - } + assert(stream.pos() == 0); + + final int packetSize = java.lang.Math.min(_maxPacketSize, _rcvSize - _udpOverhead); + if(packetSize < stream.size()) + { + // + // We log a warning here because this is the server side -- without the + // the warning, there would only be silence. + // + if(_warn) + { + _logger.warning("DatagramLimitException: maximum size of " + packetSize + " exceeded"); + } + throw new Ice.DatagramLimitException(); + } stream.resize(packetSize, true); java.nio.ByteBuffer buf = stream.prepareRead(); buf.position(0); - synchronized(this) - { - assert(_fd != null); - if(_readSelector == null) - { - try - { - _readSelector = java.nio.channels.Selector.open(); - _fd.register(_readSelector, java.nio.channels.SelectionKey.OP_READ, null); - } - catch(java.io.IOException ex) - { - if(Network.connectionLost(ex)) - { - Ice.ConnectionLostException se = new Ice.ConnectionLostException(); - se.initCause(ex); - throw se; - } - - Ice.SocketException se = new Ice.SocketException(); - se.initCause(ex); - throw se; - } - } - } + synchronized(this) + { + assert(_fd != null); + if(_readSelector == null) + { + try + { + _readSelector = java.nio.channels.Selector.open(); + _fd.register(_readSelector, java.nio.channels.SelectionKey.OP_READ, null); + } + catch(java.io.IOException ex) + { + if(Network.connectionLost(ex)) + { + Ice.ConnectionLostException se = new Ice.ConnectionLostException(); + se.initCause(ex); + throw se; + } + + Ice.SocketException se = new Ice.SocketException(); + se.initCause(ex); + throw se; + } + } + } int ret = 0; while(true) { - // - // Check for shutdown. - // - java.nio.channels.DatagramChannel fd = null; - synchronized(this) - { - if(_fd == null) - { - throw new Ice.ConnectionLostException(); - } - fd = _fd; - } - - try - { - java.net.InetSocketAddress sender = (java.net.InetSocketAddress)fd.receive(buf); - if(sender == null || buf.position() == 0) - { - // - // Wait until packet arrives or socket is closed. - // - _readSelector.select(); - continue; - } - - ret = buf.position(); - - if(_connect) - { - // - // If we must connect, then we connect to the first peer that - // sends us a packet. - // - Network.doConnect(fd, sender, -1); - _connect = false; // We're connected now - - if(_traceLevels.network >= 1) - { - String s = "connected udp socket\n" + toString(); - _logger.trace(_traceLevels.networkCat, s); - } - } - - break; - } - catch(java.nio.channels.AsynchronousCloseException ex) - { + // + // Check for shutdown. + // + java.nio.channels.DatagramChannel fd = null; + synchronized(this) + { + if(_fd == null) + { + throw new Ice.ConnectionLostException(); + } + fd = _fd; + } + + try + { + java.net.InetSocketAddress sender = (java.net.InetSocketAddress)fd.receive(buf); + if(sender == null || buf.position() == 0) + { + // + // Wait until packet arrives or socket is closed. + // + _readSelector.select(); + continue; + } + + ret = buf.position(); + + if(_connect) + { + // + // If we must connect, then we connect to the first peer that + // sends us a packet. + // + Network.doConnect(fd, sender, -1); + _connect = false; // We're connected now + + if(_traceLevels.network >= 1) + { + String s = "connected udp socket\n" + toString(); + _logger.trace(_traceLevels.networkCat, s); + } + } + + break; + } + catch(java.nio.channels.AsynchronousCloseException ex) + { Ice.ConnectionLostException se = new Ice.ConnectionLostException(); se.initCause(ex); throw se; - } + } catch(java.net.PortUnreachableException ex) { Ice.ConnectionLostException se = new Ice.ConnectionLostException(); se.initCause(ex); throw se; } - catch(java.io.InterruptedIOException ex) - { - continue; - } - catch(java.io.IOException ex) - { - if(Network.connectionLost(ex)) - { - Ice.ConnectionLostException se = new Ice.ConnectionLostException(); - se.initCause(ex); - throw se; - } - - Ice.SocketException se = new Ice.SocketException(); - se.initCause(ex); - throw se; - } + catch(java.io.InterruptedIOException ex) + { + continue; + } + catch(java.io.IOException ex) + { + if(Network.connectionLost(ex)) + { + Ice.ConnectionLostException se = new Ice.ConnectionLostException(); + se.initCause(ex); + throw se; + } + + Ice.SocketException se = new Ice.SocketException(); + se.initCause(ex); + throw se; + } } if(_traceLevels.network >= 3) @@ -269,7 +269,7 @@ final class UdpTransceiver implements Transceiver stream.resize(ret, true); stream.pos(ret); - return false; + return false; } public String @@ -287,15 +287,15 @@ final class UdpTransceiver implements Transceiver public void checkSendSize(BasicStream stream, int messageSizeMax) { - if(stream.size() > messageSizeMax) - { - throw new Ice.MemoryLimitException(); - } + if(stream.size() > messageSizeMax) + { + throw new Ice.MemoryLimitException(); + } final int packetSize = java.lang.Math.min(_maxPacketSize, _sndSize - _udpOverhead); - if(packetSize < stream.size()) - { - throw new Ice.DatagramLimitException(); - } + if(packetSize < stream.size()) + { + throw new Ice.DatagramLimitException(); + } } public final boolean @@ -321,13 +321,13 @@ final class UdpTransceiver implements Transceiver _stats = instance.initializationData().stats; _incoming = false; _connect = true; - _warn = instance.initializationData().properties.getPropertyAsInt("Ice.Warn.Datagrams") > 0; + _warn = instance.initializationData().properties.getPropertyAsInt("Ice.Warn.Datagrams") > 0; try { _fd = Network.createUdpSocket(); - setBufSize(instance); - Network.setBlock(_fd, false); + setBufSize(instance); + Network.setBlock(_fd, false); _addr = Network.getAddress(host, port); Network.doConnect(_fd, _addr, -1); _connect = false; // We're connected now @@ -355,19 +355,19 @@ final class UdpTransceiver implements Transceiver _stats = instance.initializationData().stats; _incoming = true; _connect = connect; - _warn = instance.initializationData().properties.getPropertyAsInt("Ice.Warn.Datagrams") > 0; + _warn = instance.initializationData().properties.getPropertyAsInt("Ice.Warn.Datagrams") > 0; try { _fd = Network.createUdpSocket(); - setBufSize(instance); - Network.setBlock(_fd, false); + setBufSize(instance); + Network.setBlock(_fd, false); _addr = new java.net.InetSocketAddress(host, port); - if(_traceLevels.network >= 2) - { - String s = "attempting to bind to udp socket " + Network.addrToString(_addr); - _logger.trace(_traceLevels.networkCat, s); - } + if(_traceLevels.network >= 2) + { + String s = "attempting to bind to udp socket " + Network.addrToString(_addr); + _logger.trace(_traceLevels.networkCat, s); + } _addr = Network.doBind(_fd, _addr); if(_traceLevels.network >= 1) @@ -388,89 +388,89 @@ final class UdpTransceiver implements Transceiver { assert(_fd != null); - for(int i = 0; i < 2; ++i) - { - String direction; - String prop; - int dfltSize; - if(i == 0) - { - direction = "receive"; - prop = "Ice.UDP.RcvSize"; - dfltSize = Network.getRecvBufferSize(_fd); - _rcvSize = dfltSize; - } - else - { - direction = "send"; - prop = "Ice.UDP.SndSize"; - dfltSize = Network.getSendBufferSize(_fd); - _sndSize = dfltSize; - } - - // - // Get property for buffer size and check for sanity. - // - int sizeRequested = instance.initializationData().properties.getPropertyAsIntWithDefault(prop, dfltSize); - if(sizeRequested < _udpOverhead) - { - _logger.warning("Invalid " + prop + " value of " + sizeRequested + " adjusted to " + dfltSize); - sizeRequested = dfltSize; - } - - if(sizeRequested != dfltSize) - { - // - // Try to set the buffer size. The kernel will silently adjust - // the size to an acceptable value. Then read the size back to - // get the size that was actually set. - // - int sizeSet; - if(i == 0) - { - Network.setRecvBufferSize(_fd, sizeRequested); - _rcvSize = Network.getRecvBufferSize(_fd); - sizeSet = _rcvSize; - } - else - { - Network.setSendBufferSize(_fd, sizeRequested); - _sndSize = Network.getSendBufferSize(_fd); - sizeSet = _sndSize; - } - - // - // Warn if the size that was set is less than the requested size. - // - if(sizeSet < sizeRequested) - { - _logger.warning("UDP " + direction + " buffer size: requested size of " - + sizeRequested + " adjusted to " + sizeSet); - } - } - } + for(int i = 0; i < 2; ++i) + { + String direction; + String prop; + int dfltSize; + if(i == 0) + { + direction = "receive"; + prop = "Ice.UDP.RcvSize"; + dfltSize = Network.getRecvBufferSize(_fd); + _rcvSize = dfltSize; + } + else + { + direction = "send"; + prop = "Ice.UDP.SndSize"; + dfltSize = Network.getSendBufferSize(_fd); + _sndSize = dfltSize; + } + + // + // Get property for buffer size and check for sanity. + // + int sizeRequested = instance.initializationData().properties.getPropertyAsIntWithDefault(prop, dfltSize); + if(sizeRequested < _udpOverhead) + { + _logger.warning("Invalid " + prop + " value of " + sizeRequested + " adjusted to " + dfltSize); + sizeRequested = dfltSize; + } + + if(sizeRequested != dfltSize) + { + // + // Try to set the buffer size. The kernel will silently adjust + // the size to an acceptable value. Then read the size back to + // get the size that was actually set. + // + int sizeSet; + if(i == 0) + { + Network.setRecvBufferSize(_fd, sizeRequested); + _rcvSize = Network.getRecvBufferSize(_fd); + sizeSet = _rcvSize; + } + else + { + Network.setSendBufferSize(_fd, sizeRequested); + _sndSize = Network.getSendBufferSize(_fd); + sizeSet = _sndSize; + } + + // + // Warn if the size that was set is less than the requested size. + // + if(sizeSet < sizeRequested) + { + _logger.warning("UDP " + direction + " buffer size: requested size of " + + sizeRequested + " adjusted to " + sizeSet); + } + } + } } private void closeSocket() { if(_fd != null) - { - if(_traceLevels.network >= 1) - { - String s = "closing udp connection\n" + toString(); - _logger.trace(_traceLevels.networkCat, s); - } - - try - { - _fd.close(); - } - catch(java.io.IOException ex) - { - } - _fd = null; - } + { + if(_traceLevels.network >= 1) + { + String s = "closing udp connection\n" + toString(); + _logger.trace(_traceLevels.networkCat, s); + } + + try + { + _fd.close(); + } + catch(java.io.IOException ex) + { + } + _fd = null; + } } protected synchronized void |