Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Juan Aguilar
Nebulo
Commits
601fe95e
Commit
601fe95e
authored
Jan 10, 2021
by
Daniel Wolf
Browse files
Use a floating average for the packet loss
parent
d23f373a
Changes
1
Hide whitespace changes
Inline
Side-by-side
app/src/main/java/com/frostnerd/smokescreen/service/ConnectionWatchdog.kt
View file @
601fe95e
package
com.frostnerd.smokescreen.service
import
android.os.Debug
import
android.text.format.Formatter
import
com.frostnerd.smokescreen.Logger
import
com.frostnerd.vpntunnelproxy.TrafficStats
import
kotlinx.coroutines.*
...
...
@@ -38,7 +37,7 @@ class ConnectionWatchdog(private val trafficStats: TrafficStats,
private
val
scope
=
CoroutineScope
(
supervisor
+
Dispatchers
.
IO
)
private
var
running
=
true
private
var
latencyAtLastCheck
:
Int
?
=
null
private
var
p
acketLoss
AtLastCheck
:
Int
?
=
null
private
var
currentP
acketLoss
:
Int
=
0
private
var
packetCountAtLastCheck
:
Int
?
=
null
private
var
lastFailedAnswerCount
:
Int
=
0
private
var
lastTotalPacketCount
:
Int
=
0
...
...
@@ -68,20 +67,20 @@ class ConnectionWatchdog(private val trafficStats: TrafficStats,
&&
packetCountAtLastCheck
?.
let
{
trafficStats
.
packetsReceivedFromDevice
-
it
>
10
}
!=
false
)
{
// Not enough data to act on.
val
currentLatency
=
trafficStats
.
floatingAverageLatency
.
toInt
()
val
currentPacketLossPercent
=
(
100
*(
trafficStats
.
failedAnswers
-
lastFailedAnswerCount
))/((
trafficStats
.
packetsReceivedFromDevice
-
lastTotalPacketCount
)*
0.9
)
val
nextPacketLoss
=
(
100
*(
trafficStats
.
failedAnswers
-
lastFailedAnswerCount
))/((
trafficStats
.
packetsReceivedFromDevice
-
lastTotalPacketCount
)*
0.9
)
currentPacketLoss
=
maxOf
(
minOf
(
currentPacketLoss
,
(
nextPacketLoss
*
1.75
).
toInt
()),
(
nextPacketLoss
*
0.3
).
toInt
()).
toInt
()
lastFailedAnswerCount
=
trafficStats
.
failedAnswers
.
toInt
()
lastTotalPacketCount
=
trafficStats
.
packetsReceivedFromDevice
.
toInt
()
log
Fine
(
"Current latency: $currentLatency"
)
log
Fine
(
"Current packet loss: $currentPacketLoss
Percent
"
)
log
(
"Current latency: $currentLatency"
)
log
(
"Current packet loss: $currentPacketLoss"
)
val
hasBadConnection
=
if
(
currentLatency
>
badLatencyThresholdMs
*
1.3
||
(
latencyAtLastCheck
?.
let
{
it
>
badLatencyThresholdMs
}
==
true
&&
currentLatency
>
badLatencyThresholdMs
))
{
true
}
else
currentPacketLossPercent
>
badPacketLossThresholdPercent
*
1.3
||
(
packetLossAtLastCheck
?.
let
{
it
>
badPacketLossThresholdPercent
}
==
true
&&
currentPacketLossPercent
>
badPacketLossThresholdPercent
)
}
else
currentPacketLoss
>
badPacketLossThresholdPercent
*
1.3
log
Fine
(
"Deeming this connection bad: $hasBadConnection"
)
log
(
"Deeming this connection bad: $hasBadConnection"
)
if
(
hasBadConnection
)
{
measurementsWithBadConnection
++
...
...
@@ -110,7 +109,6 @@ class ConnectionWatchdog(private val trafficStats: TrafficStats,
}
latencyAtLastCheck
=
trafficStats
.
floatingAverageLatency
.
toInt
()
packetLossAtLastCheck
=
currentPacketLossPercent
.
toInt
()
}
if
(
running
)
{
log
(
"Connection check done."
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment