Stop the timer when all callbacks removed
This commit is contained in:
Submodule src/main/jni/shadowsocks-libev updated: f226d47ea4...d462e4cd8d
@@ -73,6 +73,10 @@ trait BaseService {
|
||||
callbacks.unregister(cb)
|
||||
callbackCount -= 1
|
||||
}
|
||||
if (callbackCount == 0 && timer != null) {
|
||||
timer.cancel()
|
||||
timer = null
|
||||
}
|
||||
if (callbackCount == 0 && state != State.CONNECTING && state != State.CONNECTED) {
|
||||
stopBackgroundService()
|
||||
}
|
||||
@@ -80,6 +84,18 @@ trait BaseService {
|
||||
|
||||
override def registerCallback(cb: IShadowsocksServiceCallback) {
|
||||
if (cb != null) {
|
||||
if (callbackCount == 0 && timer == null) {
|
||||
val task = new TimerTask {
|
||||
def run {
|
||||
TrafficMonitor.updateRate()
|
||||
updateTrafficTotal(TrafficMonitor.getDeltaTx, TrafficMonitor.getDeltaRx)
|
||||
updateTrafficRate(TrafficMonitor.getTxRate, TrafficMonitor.getRxRate,
|
||||
TrafficMonitor.getTxTotal, TrafficMonitor.getRxTotal)
|
||||
}
|
||||
}
|
||||
timer = new Timer(true)
|
||||
timer.schedule(task, 1000, 1000)
|
||||
}
|
||||
callbacks.register(cb)
|
||||
callbackCount += 1
|
||||
}
|
||||
@@ -102,30 +118,19 @@ trait BaseService {
|
||||
this.config = config
|
||||
|
||||
TrafficMonitor.reset()
|
||||
trafficMonitorThread = new TrafficMonitorThread(this)
|
||||
trafficMonitorThread = new TrafficMonitorThread()
|
||||
trafficMonitorThread.start()
|
||||
|
||||
val task = new TimerTask {
|
||||
def run {
|
||||
clearChildProcessStream()
|
||||
}
|
||||
}
|
||||
|
||||
timer = new Timer(true)
|
||||
timer.schedule(task, 60 * 1000, 60 * 1000)
|
||||
}
|
||||
|
||||
def stopRunner() {
|
||||
// Make sure update total traffic when stopping the runner
|
||||
updateTrafficTotal(TrafficMonitor.getDeltaTx, TrafficMonitor.getDeltaRx)
|
||||
|
||||
TrafficMonitor.reset()
|
||||
if (trafficMonitorThread != null) {
|
||||
trafficMonitorThread.stopThread()
|
||||
trafficMonitorThread = null
|
||||
}
|
||||
|
||||
if (timer != null) {
|
||||
timer.cancel()
|
||||
timer = null
|
||||
}
|
||||
}
|
||||
|
||||
def updateTrafficTotal(tx: Long, rx: Long) {
|
||||
@@ -136,7 +141,6 @@ trait BaseService {
|
||||
case Some(profile) =>
|
||||
profile.tx += tx
|
||||
profile.rx += rx
|
||||
Log.d("test", profile.tx + " " + profile.rx)
|
||||
ShadowsocksApplication.profileManager.updateProfile(profile)
|
||||
case None => // Ignore
|
||||
}
|
||||
@@ -144,7 +148,6 @@ trait BaseService {
|
||||
})
|
||||
}
|
||||
|
||||
def clearChildProcessStream()
|
||||
def stopBackgroundService()
|
||||
def getServiceMode: Int
|
||||
def getTag: String
|
||||
|
||||
@@ -55,6 +55,13 @@ trait ServiceBoundContext extends Context {
|
||||
|
||||
def deattachService() {
|
||||
if (bgService != null) {
|
||||
if (callback != null) {
|
||||
try {
|
||||
bgService.unregisterCallback(callback)
|
||||
} catch {
|
||||
case ignored: RemoteException => // Nothing
|
||||
}
|
||||
}
|
||||
unbindService(connection)
|
||||
bgService = null
|
||||
}
|
||||
|
||||
@@ -429,17 +429,6 @@ class ShadowsocksNatService extends Service with BaseService {
|
||||
Console.runRootCommand(http_sb.toArray)
|
||||
}
|
||||
|
||||
override def clearChildProcessStream() {
|
||||
try {
|
||||
sslocalProcess.getInputStream.skip(65535)
|
||||
sstunnelProcess.getInputStream.skip(65535)
|
||||
pdnsdProcess.getInputStream.skip(65535)
|
||||
redsocksProcess.getInputStream.skip(65535)
|
||||
} catch {
|
||||
case ex: Exception => // Ignore
|
||||
}
|
||||
}
|
||||
|
||||
override def startRunner(config: Config) {
|
||||
|
||||
super.startRunner(config)
|
||||
|
||||
@@ -214,17 +214,6 @@ class ShadowsocksVpnService extends VpnService with BaseService {
|
||||
}
|
||||
}
|
||||
|
||||
override def clearChildProcessStream() {
|
||||
try {
|
||||
sslocalProcess.getInputStream.skip(65535)
|
||||
sstunnelProcess.getInputStream.skip(65535)
|
||||
pdnsdProcess.getInputStream.skip(65535)
|
||||
tun2socksProcess.getInputStream.skip(65535)
|
||||
} catch {
|
||||
case ex: Exception => // Ignore
|
||||
}
|
||||
}
|
||||
|
||||
override def startRunner(config: Config) {
|
||||
|
||||
super.startRunner(config)
|
||||
|
||||
@@ -39,20 +39,23 @@ object TrafficMonitor {
|
||||
else numberFormat.format(n) + ' ' + units(i)
|
||||
}
|
||||
|
||||
def update(tx: Long, rx: Long) {
|
||||
val now = getTraffic(tx, rx)
|
||||
def updateRate() {
|
||||
val now = getTraffic(txTotal, rxTotal)
|
||||
val delta = now.timestamp - last.timestamp
|
||||
txLast = now.tx - last.tx
|
||||
rxLast = now.rx - last.rx
|
||||
val deltaTx = now.tx - last.tx
|
||||
val deltaRx = now.rx - last.rx
|
||||
if (delta != 0) {
|
||||
txRate = txLast * 1000 / delta
|
||||
rxRate = rxLast * 1000 / delta
|
||||
txRate = deltaTx * 1000 / delta
|
||||
rxRate = deltaRx * 1000 / delta
|
||||
}
|
||||
txTotal += txLast
|
||||
rxTotal += rxLast
|
||||
last = now
|
||||
}
|
||||
|
||||
def update(tx: Long, rx: Long) {
|
||||
txTotal = tx;
|
||||
rxTotal = rx;
|
||||
}
|
||||
|
||||
def reset() {
|
||||
txRate = 0
|
||||
rxRate = 0
|
||||
@@ -84,5 +87,17 @@ object TrafficMonitor {
|
||||
def getRate(): String = {
|
||||
formatTraffic(txRate + rxRate)
|
||||
}
|
||||
|
||||
def getDeltaTx(): Long = {
|
||||
val last = txLast
|
||||
txLast = txTotal
|
||||
txTotal - last
|
||||
}
|
||||
|
||||
def getDeltaRx(): Long = {
|
||||
val last = rxLast
|
||||
rxLast = rxTotal
|
||||
rxTotal - last
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ import android.net.{LocalServerSocket, LocalSocket, LocalSocketAddress}
|
||||
import android.util.Log
|
||||
import com.github.shadowsocks.BaseService
|
||||
|
||||
class TrafficMonitorThread(service: BaseService) extends Thread {
|
||||
class TrafficMonitorThread() extends Thread {
|
||||
|
||||
val TAG = "TrafficMonitorThread"
|
||||
val PATH = "/data/data/com.github.shadowsocks/stat_path"
|
||||
@@ -108,9 +108,6 @@ class TrafficMonitorThread(service: BaseService) extends Thread {
|
||||
val stat = new String(array, "UTF-8").split("\\|")
|
||||
if (stat.length == 2) {
|
||||
TrafficMonitor.update(stat(0).toLong, stat(1).toLong)
|
||||
service.updateTrafficTotal(TrafficMonitor.txLast, TrafficMonitor.rxLast)
|
||||
service.updateTrafficRate(TrafficMonitor.getTxRate, TrafficMonitor.getRxRate,
|
||||
TrafficMonitor.getTxTotal, TrafficMonitor.getRxTotal)
|
||||
}
|
||||
|
||||
output.write(0)
|
||||
|
||||
Reference in New Issue
Block a user