Ignore extra records coming later

This can happen when the application is terminated incorrectly. Due to how we add routing rules, new client-wise rules are always added to the top. Therefore, we should always assume that the input at the top is the correct one.
This commit is contained in:
Mygod
2018-10-02 22:36:45 +08:00
parent 38f95a382e
commit 8419734df8

View File

@@ -88,13 +88,21 @@ object TrafficRecorder {
ip = ip, ip = ip,
upstream = upstream, upstream = upstream,
downstream = downstream, downstream = downstream,
sentPackets = -1,
sentBytes = -1,
receivedPackets = -1,
receivedBytes = -1,
previousId = oldRecord.id) previousId = oldRecord.id)
if (isReceive) { if (isReceive) {
record.receivedPackets = columns[0].toLong() if (record.receivedPackets == -1L && record.receivedBytes == -1L) {
record.receivedBytes = columns[1].toLong() record.receivedPackets = columns[0].toLong()
record.receivedBytes = columns[1].toLong()
}
} else { } else {
record.sentPackets = columns[0].toLong() if (record.sentPackets == -1L && record.sentBytes == -1L) {
record.sentBytes = columns[1].toLong() record.sentPackets = columns[0].toLong()
record.sentBytes = columns[1].toLong()
}
} }
if (oldRecord.id != null) { if (oldRecord.id != null) {
check(records.put(key, record) == oldRecord) check(records.put(key, record) == oldRecord)
@@ -109,7 +117,13 @@ object TrafficRecorder {
Crashlytics.logException(e) Crashlytics.logException(e)
} }
} }
for ((_, record) in records) if (record.id == null) AppDatabase.instance.trafficRecordDao.insert(record) for ((_, record) in records) if (record.id == null) {
check(record.sentPackets >= 0)
check(record.sentBytes >= 0)
check(record.receivedPackets >= 0)
check(record.receivedBytes >= 0)
AppDatabase.instance.trafficRecordDao.insert(record)
}
foregroundListeners(records.values, oldRecords) foregroundListeners(records.values, oldRecords)
scheduleUpdateLocked() scheduleUpdateLocked()
} }