Avoid runBlocking
This commit is contained in:
@@ -13,10 +13,12 @@ import be.mygod.vpnhotspot.room.AppDatabase
|
|||||||
import be.mygod.vpnhotspot.util.RootSession
|
import be.mygod.vpnhotspot.util.RootSession
|
||||||
import be.mygod.vpnhotspot.util.computeIfAbsentCompat
|
import be.mygod.vpnhotspot.util.computeIfAbsentCompat
|
||||||
import be.mygod.vpnhotspot.widget.SmartSnackbar
|
import be.mygod.vpnhotspot.widget.SmartSnackbar
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.net.*
|
import java.net.Inet4Address
|
||||||
|
import java.net.InetAddress
|
||||||
|
import java.net.NetworkInterface
|
||||||
|
import java.net.SocketException
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A transaction wrapper that helps set up routing environment.
|
* A transaction wrapper that helps set up routing environment.
|
||||||
@@ -221,9 +223,8 @@ class Routing(private val caller: Any, private val downstream: String) : IpNeigh
|
|||||||
override fun onIpNeighbourAvailable(neighbours: Collection<IpNeighbour>) = synchronized(this) {
|
override fun onIpNeighbourAvailable(neighbours: Collection<IpNeighbour>) = synchronized(this) {
|
||||||
val toRemove = HashSet(clients.keys)
|
val toRemove = HashSet(clients.keys)
|
||||||
for (neighbour in neighbours) {
|
for (neighbour in neighbours) {
|
||||||
if (neighbour.dev != downstream || neighbour.ip !is Inet4Address || runBlocking {
|
if (neighbour.dev != downstream || neighbour.ip !is Inet4Address ||
|
||||||
AppDatabase.instance.clientRecordDao.lookupOrDefault(neighbour.lladdr)
|
AppDatabase.instance.clientRecordDao.lookupOrDefaultBlocking(neighbour.lladdr).blocked) continue
|
||||||
}.blocked) continue
|
|
||||||
toRemove.remove(neighbour.ip)
|
toRemove.remove(neighbour.ip)
|
||||||
try {
|
try {
|
||||||
clients.computeIfAbsentCompat(neighbour.ip) { Client(neighbour.ip, neighbour.lladdr) }
|
clients.computeIfAbsentCompat(neighbour.ip) { Client(neighbour.ip, neighbour.lladdr) }
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import be.mygod.vpnhotspot.util.RootSession
|
|||||||
import be.mygod.vpnhotspot.util.parseNumericAddress
|
import be.mygod.vpnhotspot.util.parseNumericAddress
|
||||||
import be.mygod.vpnhotspot.util.putIfAbsentCompat
|
import be.mygod.vpnhotspot.util.putIfAbsentCompat
|
||||||
import be.mygod.vpnhotspot.widget.SmartSnackbar
|
import be.mygod.vpnhotspot.widget.SmartSnackbar
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
@@ -27,7 +26,7 @@ object TrafficRecorder {
|
|||||||
|
|
||||||
fun register(ip: InetAddress, downstream: String, mac: Long) {
|
fun register(ip: InetAddress, downstream: String, mac: Long) {
|
||||||
val record = TrafficRecord(mac = mac, ip = ip, downstream = downstream)
|
val record = TrafficRecord(mac = mac, ip = ip, downstream = downstream)
|
||||||
runBlocking { AppDatabase.instance.trafficRecordDao.insert(record) }
|
AppDatabase.instance.trafficRecordDao.insert(record)
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
DebugHelper.log(TAG, "Registering $ip%$downstream")
|
DebugHelper.log(TAG, "Registering $ip%$downstream")
|
||||||
check(records.putIfAbsentCompat(Pair(ip, downstream), record) == null)
|
check(records.putIfAbsentCompat(Pair(ip, downstream), record) == null)
|
||||||
@@ -120,7 +119,7 @@ object TrafficRecorder {
|
|||||||
check(record.sentBytes >= 0)
|
check(record.sentBytes >= 0)
|
||||||
check(record.receivedPackets >= 0)
|
check(record.receivedPackets >= 0)
|
||||||
check(record.receivedBytes >= 0)
|
check(record.receivedBytes >= 0)
|
||||||
runBlocking { AppDatabase.instance.trafficRecordDao.insert(record) }
|
AppDatabase.instance.trafficRecordDao.insert(record)
|
||||||
}
|
}
|
||||||
foregroundListeners(records.values, oldRecords)
|
foregroundListeners(records.values, oldRecords)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,10 @@ data class ClientRecord(@PrimaryKey
|
|||||||
var macLookupPending: Boolean = true) {
|
var macLookupPending: Boolean = true) {
|
||||||
@androidx.room.Dao
|
@androidx.room.Dao
|
||||||
abstract class Dao {
|
abstract class Dao {
|
||||||
|
@Query("SELECT * FROM `ClientRecord` WHERE `mac` = :mac")
|
||||||
|
protected abstract fun lookupBlocking(mac: Long): ClientRecord?
|
||||||
|
fun lookupOrDefaultBlocking(mac: Long) = lookupBlocking(mac) ?: ClientRecord(mac)
|
||||||
|
|
||||||
@Query("SELECT * FROM `ClientRecord` WHERE `mac` = :mac")
|
@Query("SELECT * FROM `ClientRecord` WHERE `mac` = :mac")
|
||||||
protected abstract suspend fun lookup(mac: Long): ClientRecord?
|
protected abstract suspend fun lookup(mac: Long): ClientRecord?
|
||||||
suspend fun lookupOrDefault(mac: Long) = lookup(mac) ?: ClientRecord(mac)
|
suspend fun lookupOrDefault(mac: Long) = lookup(mac) ?: ClientRecord(mac)
|
||||||
|
|||||||
@@ -41,8 +41,8 @@ data class TrafficRecord(
|
|||||||
@androidx.room.Dao
|
@androidx.room.Dao
|
||||||
abstract class Dao {
|
abstract class Dao {
|
||||||
@Insert
|
@Insert
|
||||||
protected abstract suspend fun insertInternal(value: TrafficRecord): Long
|
protected abstract fun insertInternal(value: TrafficRecord): Long
|
||||||
suspend fun insert(value: TrafficRecord) {
|
fun insert(value: TrafficRecord) {
|
||||||
check(value.id == null)
|
check(value.id == null)
|
||||||
value.id = insertInternal(value)
|
value.id = insertInternal(value)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user