Applies Jakub Adamek patch that fixes Pseudo TCP on Android, improves logging and fixes serious bug in the int read() method.
This commit is contained in:
parent
08633a3b66
commit
d61650957a
@ -17,6 +17,10 @@ import java.net.*;
|
||||
public class PseudoTcpSocket extends Socket
|
||||
{
|
||||
private final PseudoTcpSocketImpl socketImpl;
|
||||
|
||||
private final Object connectLock = new Object();
|
||||
|
||||
private final Object closeLock = new Object();
|
||||
|
||||
PseudoTcpSocket(PseudoTcpSocketImpl socketImpl)
|
||||
throws SocketException
|
||||
@ -144,4 +148,126 @@ public class PseudoTcpSocket extends Socket
|
||||
return getState() == PseudoTcpState.TCP_CLOSED;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Connects without the timeout.
|
||||
*/
|
||||
@Override
|
||||
public void connect(SocketAddress endpoint)
|
||||
throws IOException
|
||||
{
|
||||
this.connect(endpoint, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks destination port number.
|
||||
*
|
||||
* @param dstPort the destination port to check.
|
||||
*/
|
||||
private void checkDestination(int dstPort)
|
||||
{
|
||||
if (dstPort < 0 || dstPort > 65535)
|
||||
{
|
||||
throw new IllegalArgumentException("Port out of range: " + dstPort);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* On Android, we must not use the default <tt>connect</tt> implementation,
|
||||
* because that one deals directly with physical resources, while we create
|
||||
* a socket on top of another socket.
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void connect(SocketAddress remoteAddr, int timeout)
|
||||
throws IOException
|
||||
{
|
||||
if (isClosed())
|
||||
{
|
||||
throw new SocketException("Socket is closed");
|
||||
}
|
||||
if (timeout < 0)
|
||||
{
|
||||
throw new IllegalArgumentException("timeout < 0");
|
||||
}
|
||||
if (isConnected())
|
||||
{
|
||||
throw new SocketException("Already connected");
|
||||
}
|
||||
if (remoteAddr == null)
|
||||
{
|
||||
throw new IllegalArgumentException("remoteAddr == null");
|
||||
}
|
||||
if (!(remoteAddr instanceof InetSocketAddress))
|
||||
{
|
||||
throw new IllegalArgumentException(
|
||||
"Remote address not an InetSocketAddress: " +
|
||||
remoteAddr.getClass());
|
||||
}
|
||||
InetSocketAddress inetAddr = (InetSocketAddress) remoteAddr;
|
||||
if (inetAddr.getAddress() == null)
|
||||
{
|
||||
throw new UnknownHostException(
|
||||
"Host is unresolved: " + inetAddr.getHostName());
|
||||
}
|
||||
|
||||
int port = inetAddr.getPort();
|
||||
checkDestination(port);
|
||||
|
||||
synchronized (connectLock)
|
||||
{
|
||||
try
|
||||
{
|
||||
socketImpl.connect(remoteAddr, timeout);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
socketImpl.close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public synchronized void close()
|
||||
throws IOException
|
||||
{
|
||||
synchronized (closeLock)
|
||||
{
|
||||
if (isClosed())
|
||||
return;
|
||||
socketImpl.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows to set up the remote address directly.
|
||||
* Otherwise, when using the other <tt>accept</tt> methods,
|
||||
* the first address from which a packet is received, is considered
|
||||
* the remote address.
|
||||
*
|
||||
* @param remoteAddress the one and only remote address that will be
|
||||
* accepted as remote packet's source
|
||||
* @param timeout connection accept timeout value in milliseconds, after
|
||||
* which the exception will be thrown.
|
||||
*/
|
||||
public void accept(SocketAddress remoteAddress, int timeout)
|
||||
throws IOException
|
||||
{
|
||||
socketImpl.accept(remoteAddress, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the <tt>FileDescriptor</tt> of the underlying socket.
|
||||
* @return the <tt>FileDescriptor</tt> of the underlying socket.
|
||||
*/
|
||||
public FileDescriptor getFileDescriptor()
|
||||
{
|
||||
return socketImpl.getFileDescriptor();
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,6 +95,15 @@ class PseudoTcpSocketImpl
|
||||
//Default MTU
|
||||
setMTU(1450);
|
||||
this.socket = sock;
|
||||
//TODO: find out if this call is required
|
||||
/*try
|
||||
{
|
||||
setOption(SO_TIMEOUT, 0);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}*/
|
||||
}
|
||||
|
||||
/**
|
||||
@ -358,7 +367,23 @@ class PseudoTcpSocketImpl
|
||||
throw new IOException("Connect aborted");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Blocking method waits for connection.
|
||||
*
|
||||
* @param remoteAddress the one and only address that will be
|
||||
* accepted as the source for remote packets
|
||||
* @param timeout for this operation in ms
|
||||
* @throws IOException If socket gets closed or timeout expires
|
||||
*/
|
||||
void accept(SocketAddress remoteAddress, int timeout)
|
||||
throws IOException
|
||||
{
|
||||
this.remoteAddr = remoteAddress;
|
||||
accept(timeout);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Blocking method waits for connection.
|
||||
*
|
||||
@ -586,7 +611,9 @@ class PseudoTcpSocketImpl
|
||||
{
|
||||
if (logger.isLoggable(Level.FINEST))
|
||||
{
|
||||
logger.log(Level.FINEST, "write packet to network " + len);
|
||||
logger.log(Level.FINEST,
|
||||
"write packet to network length " + len
|
||||
+ " address " + remoteAddr);
|
||||
}
|
||||
try
|
||||
{
|
||||
@ -617,7 +644,8 @@ class PseudoTcpSocketImpl
|
||||
private void receivePackets()
|
||||
{
|
||||
byte[] buffer = new byte[DATAGRAM_RCV_BUFFER_SIZE];
|
||||
DatagramPacket packet = new DatagramPacket(buffer, DATAGRAM_RCV_BUFFER_SIZE);
|
||||
DatagramPacket packet = new DatagramPacket(buffer,
|
||||
DATAGRAM_RCV_BUFFER_SIZE);
|
||||
while (runReceive)
|
||||
{
|
||||
try
|
||||
@ -628,6 +656,9 @@ class PseudoTcpSocketImpl
|
||||
if (remoteAddr == null)
|
||||
{
|
||||
remoteAddr = packet.getSocketAddress();
|
||||
logger.log(Level.WARNING,
|
||||
"Remote addr not set previously, setting to "
|
||||
+ remoteAddr);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -635,7 +666,8 @@ class PseudoTcpSocketImpl
|
||||
{
|
||||
logger.log(Level.WARNING,
|
||||
"Ignoring packet from " + packet.getAddress()
|
||||
+ ":" + packet.getPort() + " should be: " + remoteAddr);
|
||||
+ ":" + packet.getPort()
|
||||
+ " should be: " + remoteAddr);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@ -698,7 +730,7 @@ class PseudoTcpSocketImpl
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.log(Level.FINEST, "Clock sleep for " + sleep);
|
||||
//logger.log(Level.FINEST, "Clock sleep for " + sleep);
|
||||
clock_notify.wait(sleep);
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
@ -826,7 +858,7 @@ class PseudoTcpSocketImpl
|
||||
{
|
||||
byte[] buff = new byte[1];
|
||||
int readCount = read(buff, 0, 1);
|
||||
return readCount == 1 ? buff[0] : -1;
|
||||
return readCount == 1 ? (buff[0] & 0xFF) : -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -920,6 +952,33 @@ class PseudoTcpSocketImpl
|
||||
public void close() throws IOException
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public long skip(long n) throws IOException
|
||||
{
|
||||
throw new UnsupportedOperationException("skip");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public synchronized void mark(int readlimit)
|
||||
{
|
||||
throw new UnsupportedOperationException("mark");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public synchronized void reset() throws IOException
|
||||
{
|
||||
throw new UnsupportedOperationException("reset");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1064,4 +1123,80 @@ class PseudoTcpSocketImpl
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected FileDescriptor getFileDescriptor()
|
||||
{
|
||||
return fd;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected void shutdownInput()
|
||||
throws IOException
|
||||
{
|
||||
throw new IOException("Method not implemented!");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected void shutdownOutput()
|
||||
throws IOException
|
||||
{
|
||||
throw new IOException("Method not implemented!");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected InetAddress getInetAddress()
|
||||
{
|
||||
return ((InetSocketAddress) remoteAddr).getAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected int getPort()
|
||||
{
|
||||
return ((InetSocketAddress) remoteAddr).getPort();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected boolean supportsUrgentData()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected int getLocalPort()
|
||||
{
|
||||
return socket.getLocalPort();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected void setPerformancePreferences(int connectionTime,
|
||||
int latency,
|
||||
int bandwidth)
|
||||
{
|
||||
throw new UnsupportedOperationException("setPerformancePreferences");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user