From 52cecf4f26a31ed55059ef9ff7aa4009635861e4 Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 3 Sep 2020 22:41:39 +0200 Subject: [PATCH] Added connection timeout to check when a connection is actually expired --- src/dorkbox/network/connection/Connection.kt | 26 ++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/dorkbox/network/connection/Connection.kt b/src/dorkbox/network/connection/Connection.kt index d9aec9cd..9ec211e3 100644 --- a/src/dorkbox/network/connection/Connection.kt +++ b/src/dorkbox/network/connection/Connection.kt @@ -281,12 +281,34 @@ open class Connection(connectionParameters: ConnectionParams<*>) { return messagesInProgress.value } + + var previousConnectionExpireTime = Long.MAX_VALUE + /** * @return `true` if this connection has no subscribers (which means this connection does not have a remote connection) */ internal fun isExpired(): Boolean { - // cannot use subscription.isConnected !!! images can be in a state of flux. We only care if there are NO images. - return subscription.hasNoImages() + return if (subscription.isConnected) { + false + } + else { + // images can be in a state of flux. Sometimes they come and go VERY quickly + val now = System.nanoTime() + when { + previousConnectionExpireTime == Long.MAX_VALUE -> { + // this means we haven't set an expire time yet. + val timeOut = TimeUnit.SECONDS.toNanos(endPoint.config.connectionCloseTimeoutInSeconds.toLong()) + previousConnectionExpireTime = now + timeOut + false + } + now < previousConnectionExpireTime -> { + false + } + else -> { + true + } + } + } } /**