Fixing potential connection issue that could result in duplicate disconnect requests
@@ -36,6 +36,7 @@ class AppConnectionsDaemon(
private var peerConnectionJob :Job? = null
private var electrumConnectionJob: Job? = null
+ private var httpControlFlowEnabled: Boolean = false
private var networkStatus = MutableStateFlow(NetworkState.NotAvailable)
@@ -96,17 +97,20 @@ class AppConnectionsDaemon(
launch {
electrumControlFlow.collect {
when {
- it.networkIsAvailable && it.disconnectCount == 0 -> {
+ it.networkIsAvailable && it.disconnectCount <= 0 -> {
+ if (electrumConnectionJob == null) {
electrumConnectionJob = connectionLoop("Electrum", electrumClient.connectionState) {
val electrumServer = configurationManager.getElectrumServer()
electrumClient.connect(electrumServer.asServerAddress())
}
}
+ }
else -> {
electrumConnectionJob?.let {
it.cancel()
electrumClient.disconnect()
}
+ electrumConnectionJob = null
}
}
}
@@ -134,16 +138,19 @@ class AppConnectionsDaemon(
peerControlFlow.collect {
val peer = peerManager.getPeer()
when {
- it.networkIsAvailable && it.disconnectCount == 0 -> {
+ it.networkIsAvailable && it.disconnectCount <= 0 -> {
+ if (peerConnectionJob == null) {
peerConnectionJob = connectionLoop("Peer", peer.connectionState) {
peer.connect()
}
}
+ }
else -> {
peerConnectionJob?.let {
it.cancel()
peer.disconnect()
}
+ peerConnectionJob = null
}
}
}
@@ -152,17 +159,23 @@ class AppConnectionsDaemon(
launch {
httpApiControlFlow.collect {
when {
- it.networkIsAvailable && it.disconnectCount == 0 -> {
+ it.networkIsAvailable && it.disconnectCount <= 0 -> {
+ if (!httpControlFlowEnabled) {
+ httpControlFlowEnabled = true
configurationManager.startWalletParamsLoop()
currencyManager.start()
}
+ }
else -> {
+ if (httpControlFlowEnabled) {
+ httpControlFlowEnabled = false
configurationManager.stopWalletParamsLoop()
currencyManager.stop()
}
}
}
}
+ }
// Internet connection monitor
launch {
monitor.start()
