summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/TcpTransceiver.java
diff options
context:
space:
mode:
authorBenoit Foucher <benoit@zeroc.com>2014-09-10 08:47:19 +0200
committerBenoit Foucher <benoit@zeroc.com>2014-09-10 08:47:19 +0200
commitb6c9d9a880f6f1a6908a3c62dfccdce3e68dad80 (patch)
treed3e9e9340064538a8dc7a645260d0eb3cdf55d63 /java/src/IceInternal/TcpTransceiver.java
parentUndo bogus change from an earlier commit. (diff)
downloadice-b6c9d9a880f6f1a6908a3c62dfccdce3e68dad80.tar.bz2
ice-b6c9d9a880f6f1a6908a3c62dfccdce3e68dad80.tar.xz
ice-b6c9d9a880f6f1a6908a3c62dfccdce3e68dad80.zip
ICE-5582 (SOCKs test), ICE-5314 (HTTP proxies), major refactoring of networking code (addition of StreamSocket class abstraction)
Diffstat (limited to 'java/src/IceInternal/TcpTransceiver.java')
-rw-r--r--java/src/IceInternal/TcpTransceiver.java296
1 files changed, 13 insertions, 283 deletions
diff --git a/java/src/IceInternal/TcpTransceiver.java b/java/src/IceInternal/TcpTransceiver.java
index 4d1c5577900..625ab8ce448 100644
--- a/java/src/IceInternal/TcpTransceiver.java
+++ b/java/src/IceInternal/TcpTransceiver.java
@@ -14,92 +14,14 @@ final class TcpTransceiver implements Transceiver
@Override
public java.nio.channels.SelectableChannel fd()
{
- assert(_fd != null);
- return _fd;
+ assert(_stream != null);
+ return _stream.fd();
}
@Override
public int initialize(Buffer readBuffer, Buffer writeBuffer, Ice.Holder<Boolean> moreData)
{
- if(_state == StateNeedConnect)
- {
- _state = StateConnectPending;
- return SocketOperation.Connect;
- }
- else if(_state <= StateConnectPending)
- {
- Network.doFinishConnect(_fd);
- _desc = Network.fdToString(_fd, _proxy, _addr);
-
- if(_proxy != null)
- {
- //
- // Prepare the read & write buffers in advance.
- //
- _proxy.beginWriteConnectRequest(_addr, writeBuffer);
- _proxy.beginReadConnectRequestResponse(readBuffer);
-
- //
- // Write the proxy connection message.
- //
- if(write(writeBuffer) == SocketOperation.None)
- {
- //
- // Write completed without blocking.
- //
- _proxy.endWriteConnectRequest(writeBuffer);
-
- //
- // Try to read the response.
- //
- if(read(readBuffer, moreData) == SocketOperation.None)
- {
- //
- // Read completed without blocking - fall through.
- //
- _proxy.endReadConnectRequestResponse(readBuffer);
- }
- else
- {
- //
- // Return SocketOperationRead to indicate we need to complete the read.
- //
- _state = StateProxyConnectRequestPending; // Wait for proxy response
- return SocketOperation.Read;
- }
- }
- else
- {
- //
- // Return SocketOperationWrite to indicate we need to complete the write.
- //
- _state = StateProxyConnectRequest; // Send proxy connect request
- return SocketOperation.Write;
- }
- }
-
- _state = StateConnected;
- }
- else if(_state == StateProxyConnectRequest)
- {
- //
- // Write completed.
- //
- _proxy.endWriteConnectRequest(writeBuffer);
- _state = StateProxyConnectRequestPending; // Wait for proxy response
- return SocketOperation.Read;
- }
- else if(_state == StateProxyConnectRequestPending)
- {
- //
- // Read completed.
- //
- _proxy.endReadConnectRequestResponse(readBuffer);
- _state = StateConnected;
- }
-
- assert(_state == StateConnected);
- return SocketOperation.None;
+ return _stream.connect(readBuffer, writeBuffer);
}
@Override
@@ -113,19 +35,7 @@ final class TcpTransceiver implements Transceiver
@Override
public void close()
{
- assert(_fd != null);
- try
- {
- _fd.close();
- }
- catch(java.io.IOException ex)
- {
- throw new Ice.SocketException(ex);
- }
- finally
- {
- _fd = null;
- }
+ _stream.close();
}
@Override
@@ -138,120 +48,13 @@ final class TcpTransceiver implements Transceiver
@Override
public int write(Buffer buf)
{
- final int size = buf.b.limit();
- int packetSize = size - buf.b.position();
-
- if(packetSize == 0)
- {
- return SocketOperation.None;
- }
-
- //
- // We don't want write to be called on Android's main thread as this will cause
- // NetworkOnMainThreadException to be thrown. If this is the Android main thread
- // we return false and this method will be called later from the thread pool.
- //
- if(Util.isAndroidMainThread(Thread.currentThread()))
- {
- return SocketOperation.Write;
- }
-
- //
- // Limit packet size to avoid performance problems on WIN32
- //
- if(_maxSendPacketSize > 0 && packetSize > _maxSendPacketSize)
- {
- packetSize = _maxSendPacketSize;
- buf.b.limit(buf.b.position() + packetSize);
- }
-
- while(buf.b.hasRemaining())
- {
- try
- {
- assert(_fd != null);
-
- int ret = _fd.write(buf.b);
- if(ret == -1)
- {
- throw new Ice.ConnectionLostException();
- }
- else if(ret == 0)
- {
- //
- // Writing would block, so we reset the limit (if necessary) and indicate
- // that more data must be sent.
- //
- if(packetSize == _maxSendPacketSize)
- {
- buf.b.limit(size);
- }
- return SocketOperation.Write;
- }
-
- if(packetSize == _maxSendPacketSize)
- {
- assert(buf.b.position() == buf.b.limit());
- packetSize = size - buf.b.position();
- if(packetSize > _maxSendPacketSize)
- {
- packetSize = _maxSendPacketSize;
- }
- buf.b.limit(buf.b.position() + packetSize);
- }
- }
- catch(java.io.InterruptedIOException ex)
- {
- continue;
- }
- catch(java.io.IOException ex)
- {
- throw new Ice.SocketException(ex);
- }
- }
-
- return SocketOperation.None;
+ return _stream.write(buf);
}
@Override
public int read(Buffer buf, Ice.Holder<Boolean> moreData)
{
- int packetSize = buf.b.remaining();
- if(packetSize == 0)
- {
- return SocketOperation.None;
- }
-
- while(buf.b.hasRemaining())
- {
- try
- {
- assert(_fd != null);
-
- int ret = _fd.read(buf.b);
- if(ret == -1)
- {
- throw new Ice.ConnectionLostException();
- }
-
- if(ret == 0)
- {
- return SocketOperation.Read;
- }
-
- packetSize = buf.b.remaining();
- }
- catch(java.io.InterruptedIOException ex)
- {
- continue;
- }
- catch(java.io.IOException ex)
- {
- throw new Ice.ConnectionLostException(ex);
- }
- }
-
- return SocketOperation.None;
+ return _stream.read(buf);
}
@Override
@@ -263,7 +66,7 @@ final class TcpTransceiver implements Transceiver
@Override
public String toString()
{
- return _desc;
+ return _stream.toString();
}
@Override
@@ -276,9 +79,9 @@ final class TcpTransceiver implements Transceiver
public Ice.ConnectionInfo getInfo()
{
Ice.TCPConnectionInfo info = new Ice.TCPConnectionInfo();
- if(_fd != null)
+ if(_stream.fd() != null)
{
- java.net.Socket socket = _fd.socket();
+ java.net.Socket socket = _stream.fd().socket();
info.localAddress = socket.getLocalAddress().getHostAddress();
info.localPort = socket.getLocalPort();
if(socket.getInetAddress() != null)
@@ -299,85 +102,12 @@ final class TcpTransceiver implements Transceiver
}
}
- TcpTransceiver(ProtocolInstance instance, java.nio.channels.SocketChannel fd, NetworkProxy proxy,
- java.net.InetSocketAddress addr)
+ TcpTransceiver(ProtocolInstance instance, StreamSocket stream)
{
_instance = instance;
- _fd = fd;
- _proxy = proxy;
- _addr = addr;
- _state = StateNeedConnect;
- _desc = Network.fdToString(_fd, _proxy, _addr);;
-
- _maxSendPacketSize = 0;
- if(System.getProperty("os.name").startsWith("Windows"))
- {
- //
- // On Windows, limiting the buffer size is important to prevent
- // poor throughput performances when transfering large amount of
- // data. See Microsoft KB article KB823764.
- //
- _maxSendPacketSize = Network.getSendBufferSize(_fd) / 2;
- if(_maxSendPacketSize < 512)
- {
- _maxSendPacketSize = 0;
- }
- }
+ _stream = stream;
}
- TcpTransceiver(ProtocolInstance instance, java.nio.channels.SocketChannel fd)
- {
- _instance = instance;
- _fd = fd;
- _state = StateConnected;
- _desc = Network.fdToString(_fd);
-
- _maxSendPacketSize = 0;
- if(System.getProperty("os.name").startsWith("Windows"))
- {
- //
- // On Windows, limiting the buffer size is important to prevent
- // poor throughput performances when transfering large amount of
- // data. See Microsoft KB article KB823764.
- //
- _maxSendPacketSize = Network.getSendBufferSize(_fd) / 2;
- if(_maxSendPacketSize < 512)
- {
- _maxSendPacketSize = 0;
- }
- }
- }
-
- @Override
- protected synchronized void
- finalize()
- throws Throwable
- {
- try
- {
- IceUtilInternal.Assert.FinalizerAssert(_fd == null);
- }
- catch(java.lang.Exception ex)
- {
- }
- finally
- {
- super.finalize();
- }
- }
-
- private ProtocolInstance _instance;
- private java.nio.channels.SocketChannel _fd;
- private NetworkProxy _proxy;
- private java.net.InetSocketAddress _addr;
-
- private int _state;
- private String _desc;
- private int _maxSendPacketSize;
-
- private static final int StateNeedConnect = 0;
- private static final int StateConnectPending = 1;
- private static final int StateProxyConnectRequest = 2;
- private static final int StateProxyConnectRequestPending = 3;
- private static final int StateConnected = 4;
+ final private ProtocolInstance _instance;
+ final private StreamSocket _stream;
}