Cleanup to make sure monotonic time is used, not wall-time

System.currentTimeMillis is not precise and may move backwards.
This commit is contained in:
Ingo Bauersachs 2021-11-09 00:02:48 +01:00
parent 626c35e460
commit cd2d21a8d0
2 changed files with 36 additions and 39 deletions

View File

@ -387,12 +387,11 @@ public class PseudoTCPBase
} }
/** /**
*
* @return current timestamp limited to 32 bits * @return current timestamp limited to 32 bits
*/ */
public static long now() public static long now()
{ {
return System.currentTimeMillis() & 0xFFFFFFFFL; return (System.nanoTime() / 1000000) & 0xFFFFFFFFL;
} }
/** /**

View File

@ -78,15 +78,8 @@ class PseudoTcpSocketImpl
*/ */
private long readTimeout; private long readTimeout;
/**
*
*/
private PseudoTcpInputStream inputStream; private PseudoTcpInputStream inputStream;
/**
*
*/
private PseudoTcpOutputStream outputstream; private PseudoTcpOutputStream outputstream;
/** /**
@ -196,6 +189,7 @@ class PseudoTcpSocketImpl
* @param stream if true, create a stream socket; otherwise, create a datagram socket. * @param stream if true, create a stream socket; otherwise, create a datagram socket.
* @throws IOException if an I/O error occurs while creating the socket. * @throws IOException if an I/O error occurs while creating the socket.
*/ */
@Override
protected void create(boolean stream) protected void create(boolean stream)
throws IOException throws IOException
{ {
@ -208,6 +202,7 @@ class PseudoTcpSocketImpl
* @param port the port number. * @param port the port number.
* @throws IOException * @throws IOException
*/ */
@Override
protected void connect(String host, int port) protected void connect(String host, int port)
throws IOException throws IOException
{ {
@ -220,6 +215,7 @@ class PseudoTcpSocketImpl
* @param port the port number. * @param port the port number.
* @throws IOException if an I/O error occurs when attempting a connection. * @throws IOException if an I/O error occurs when attempting a connection.
*/ */
@Override
protected void connect(InetAddress address, int port) protected void connect(InetAddress address, int port)
throws IOException throws IOException
{ {
@ -234,6 +230,7 @@ class PseudoTcpSocketImpl
* @param timeout the timeout value, in milliseconds, or zero for no timeout. * @param timeout the timeout value, in milliseconds, or zero for no timeout.
* @throws IOException if an I/O error occurs when attempting a connection. * @throws IOException if an I/O error occurs when attempting a connection.
*/ */
@Override
protected void connect(SocketAddress address, int timeout) protected void connect(SocketAddress address, int timeout)
throws IOException throws IOException
{ {
@ -247,6 +244,7 @@ class PseudoTcpSocketImpl
* @param port the port number. * @param port the port number.
* @throws IOException * @throws IOException
*/ */
@Override
public void bind(InetAddress host, int port) public void bind(InetAddress host, int port)
throws IOException throws IOException
{ {
@ -264,6 +262,7 @@ class PseudoTcpSocketImpl
* @param backlog the maximum length of the queue. * @param backlog the maximum length of the queue.
* @throws IOException if an I/O error occurs when creating the queue. * @throws IOException if an I/O error occurs when creating the queue.
*/ */
@Override
protected void listen(int backlog) protected void listen(int backlog)
throws IOException throws IOException
{ {
@ -271,6 +270,7 @@ class PseudoTcpSocketImpl
} }
private Map<Integer, Object> options = new HashMap<>(); private Map<Integer, Object> options = new HashMap<>();
@Override
public void setOption(int optID, Object value) public void setOption(int optID, Object value)
throws SocketException throws SocketException
{ {
@ -278,6 +278,7 @@ class PseudoTcpSocketImpl
options.put(optID, value); options.put(optID, value);
} }
@Override
public Object getOption(int optID) public Object getOption(int optID)
throws SocketException throws SocketException
{ {
@ -334,7 +335,6 @@ class PseudoTcpSocketImpl
* *
* @param remoteAddress to which this socket connects to * @param remoteAddress to which this socket connects to
* @param timeout for this operation in ms * @param timeout for this operation in ms
* @throws IOException
*/ */
void doConnect(InetSocketAddress remoteAddress, long timeout) void doConnect(InetSocketAddress remoteAddress, long timeout)
throws IOException throws IOException
@ -355,10 +355,9 @@ class PseudoTcpSocketImpl
while (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED while (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED
&& (noTimeout || (elapsed < timeout)) ) && (noTimeout || (elapsed < timeout)) )
{ {
long start = System.currentTimeMillis(); long start = System.nanoTime();
state_notify.wait(timeout); state_notify.wait(timeout);
long end = System.currentTimeMillis(); elapsed += TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
elapsed += end - start;
} }
if (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED) if (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED)
{ {
@ -431,6 +430,7 @@ class PseudoTcpSocketImpl
* @param s the accepted connection. * @param s the accepted connection.
* @throws IOException if an I/O error occurs when accepting the connection. * @throws IOException if an I/O error occurs when accepting the connection.
*/ */
@Override
protected void accept(SocketImpl s) protected void accept(SocketImpl s)
throws IOException throws IOException
{ {
@ -464,6 +464,7 @@ class PseudoTcpSocketImpl
pseudoTcp.notifyClock(PseudoTCPBase.now()); pseudoTcp.notifyClock(PseudoTCPBase.now());
receiveThread = new Thread(new Runnable() receiveThread = new Thread(new Runnable()
{ {
@Override
public void run() public void run()
{ {
receivePackets(); receivePackets();
@ -483,6 +484,7 @@ class PseudoTcpSocketImpl
* @param tcp the {@link PseudoTCPBase} that caused an event * @param tcp the {@link PseudoTCPBase} that caused an event
* @see PseudoTcpNotify#onTcpOpen(PseudoTCPBase) * @see PseudoTcpNotify#onTcpOpen(PseudoTCPBase)
*/ */
@Override
public void onTcpOpen(PseudoTCPBase tcp) public void onTcpOpen(PseudoTCPBase tcp)
{ {
logger.log(Level.FINE, "tcp opened"); logger.log(Level.FINE, "tcp opened");
@ -501,6 +503,7 @@ class PseudoTcpSocketImpl
* @param tcp the {@link PseudoTCPBase} that caused an event * @param tcp the {@link PseudoTCPBase} that caused an event
* @see PseudoTcpNotify#onTcpReadable(PseudoTCPBase) * @see PseudoTcpNotify#onTcpReadable(PseudoTCPBase)
*/ */
@Override
public void onTcpReadable(PseudoTCPBase tcp) public void onTcpReadable(PseudoTCPBase tcp)
{ {
if (logger.isLoggable(Level.FINER)) if (logger.isLoggable(Level.FINER))
@ -522,6 +525,7 @@ class PseudoTcpSocketImpl
* @param tcp the {@link PseudoTCPBase} that caused an event * @param tcp the {@link PseudoTCPBase} that caused an event
* @see PseudoTcpNotify#onTcpWriteable(PseudoTCPBase) * @see PseudoTcpNotify#onTcpWriteable(PseudoTCPBase)
*/ */
@Override
public void onTcpWriteable(PseudoTCPBase tcp) public void onTcpWriteable(PseudoTCPBase tcp)
{ {
@ -545,6 +549,7 @@ class PseudoTcpSocketImpl
* *
* @see PseudoTcpNotify#onTcpClosed(PseudoTCPBase, IOException) * @see PseudoTcpNotify#onTcpClosed(PseudoTCPBase, IOException)
*/ */
@Override
public void onTcpClosed(PseudoTCPBase tcp, IOException e) public void onTcpClosed(PseudoTCPBase tcp, IOException e)
{ {
if (e != null) if (e != null)
@ -606,6 +611,7 @@ class PseudoTcpSocketImpl
* *
* @see PseudoTcpNotify#tcpWritePacket(PseudoTCPBase, byte[], int) * @see PseudoTcpNotify#tcpWritePacket(PseudoTCPBase, byte[], int)
*/ */
@Override
public WriteResult tcpWritePacket(PseudoTCPBase tcp, byte[] buffer, int len) public WriteResult tcpWritePacket(PseudoTCPBase tcp, byte[] buffer, int len)
{ {
if (logger.isLoggable(Level.FINEST)) if (logger.isLoggable(Level.FINEST))
@ -739,13 +745,7 @@ class PseudoTcpSocketImpl
} }
} }
private Runnable clockTaskRunner = new Runnable() private final Runnable clockTaskRunner = this::runClock;
{
@Override
public void run() {
runClock();
}
};
private void scheduleClockTask(long sleep) private void scheduleClockTask(long sleep)
{ {
@ -777,6 +777,7 @@ class PseudoTcpSocketImpl
* @return an output stream for writing to this socket. * @return an output stream for writing to this socket.
* @throws IOException if an I/O error occurs when creating the output stream. * @throws IOException if an I/O error occurs when creating the output stream.
*/ */
@Override
public OutputStream getOutputStream() public OutputStream getOutputStream()
throws IOException throws IOException
{ {
@ -793,6 +794,7 @@ class PseudoTcpSocketImpl
* @return a stream for reading from this socket. * @return a stream for reading from this socket.
* @throws IOException * @throws IOException
*/ */
@Override
public InputStream getInputStream() public InputStream getInputStream()
throws IOException throws IOException
{ {
@ -808,6 +810,7 @@ class PseudoTcpSocketImpl
* @return the number of bytes that can be read from this socket without blocking. * @return the number of bytes that can be read from this socket without blocking.
* @throws IOException if an I/O error occurs when determining the number of bytes available. * @throws IOException if an I/O error occurs when determining the number of bytes available.
*/ */
@Override
protected int available() protected int available()
throws IOException throws IOException
{ {
@ -816,16 +819,16 @@ class PseudoTcpSocketImpl
/** /**
* Closes this socket. * Closes this socket.
* @throws IOException
*/ */
@Override
public void close() public void close()
throws IOException throws IOException
{ {
try try
{ {
pseudoTcp.close(true); pseudoTcp.close(true);
//System.out.println("ON CLOSE: in flight "+pseudoTcp.GetBytesInFlight()); //System.out.println("ON CLOSE: in flight "+pseudoTcp.getBytesInFlight());
//System.out.println("ON CLOSE: buff not sent "+pseudoTcp.GetBytesBufferedNotSent()); //System.out.println("ON CLOSE: buff not sent "+pseudoTcp.getBytesBufferedNotSent());
onTcpClosed(pseudoTcp, null); onTcpClosed(pseudoTcp, null);
socket.close(); socket.close();
joinAllThreads(); joinAllThreads();
@ -851,6 +854,7 @@ class PseudoTcpSocketImpl
* @param data The byte of data to send * @param data The byte of data to send
* @throws IOException if there is an error sending the data. * @throws IOException if there is an error sending the data.
*/ */
@Override
protected void sendUrgentData(int data) protected void sendUrgentData(int data)
throws IOException throws IOException
{ {
@ -908,7 +912,7 @@ class PseudoTcpSocketImpl
public int read(byte[] buffer, int offset, int length) public int read(byte[] buffer, int offset, int length)
throws IOException throws IOException
{ {
long start = System.currentTimeMillis(); long start = System.nanoTime();
int read; int read;
while (true) while (true)
{ {
@ -929,7 +933,7 @@ class PseudoTcpSocketImpl
if (readTimeout > 0) if (readTimeout > 0)
{ {
//Check for timeout //Check for timeout
long elapsed = System.currentTimeMillis() - start; long elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
long left = readTimeout - elapsed; long left = readTimeout - elapsed;
if (left <= 0) if (left <= 0)
{ {
@ -1033,12 +1037,6 @@ class PseudoTcpSocketImpl
write(bytes); write(bytes);
} }
@Override
public void write(byte[] bytes) throws IOException
{
write(bytes, 0, bytes.length);
}
/** /**
* This method blocks until all data has been written. * This method blocks until all data has been written.
* *
@ -1053,7 +1051,7 @@ class PseudoTcpSocketImpl
{ {
int toSend = length; int toSend = length;
int sent; int sent;
long start = System.currentTimeMillis(); long start = System.nanoTime();
while (toSend > 0) while (toSend > 0)
{ {
synchronized (pseudoTcp) synchronized (pseudoTcp)
@ -1073,7 +1071,7 @@ class PseudoTcpSocketImpl
{ {
if (writeTimeout > 0) if (writeTimeout > 0)
{ {
long elapsed = System.currentTimeMillis() - start; long elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
long left = writeTimeout - elapsed; long left = writeTimeout - elapsed;
if (left <= 0) if (left <= 0)
{ {
@ -1121,7 +1119,7 @@ class PseudoTcpSocketImpl
public synchronized void flush() throws IOException public synchronized void flush() throws IOException
{ {
logger.log(Level.FINE, "Flushing..."); logger.log(Level.FINE, "Flushing...");
long start = System.currentTimeMillis(); long start = System.nanoTime();
final Object ackNotify = pseudoTcp.getAckNotify(); final Object ackNotify = pseudoTcp.getAckNotify();
synchronized (ackNotify) synchronized (ackNotify)
{ {
@ -1132,7 +1130,7 @@ class PseudoTcpSocketImpl
if (writeTimeout > 0) if (writeTimeout > 0)
{ {
//Check write timeout //Check write timeout
long elapsed = System.currentTimeMillis() - start; long elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
long left = writeTimeout - elapsed; long left = writeTimeout - elapsed;
if (left <= 0) if (left <= 0)
{ {