fix #434
This commit is contained in:
@@ -3,15 +3,12 @@ package com.github.shadowsocks.utils
|
||||
import java.lang.{Math, System}
|
||||
import java.text.DecimalFormat
|
||||
|
||||
import android.net.TrafficStats
|
||||
import android.os.Process
|
||||
import com.github.shadowsocks.{R, ShadowsocksApplication}
|
||||
|
||||
case class Traffic(tx: Long, rx: Long, timestamp: Long)
|
||||
|
||||
object TrafficMonitor {
|
||||
val uid = Process.myUid
|
||||
var last: Traffic = getTraffic
|
||||
var last: Traffic = getTraffic(0, 0)
|
||||
|
||||
// Kilo bytes per second
|
||||
var txRate: Long = 0
|
||||
@@ -21,9 +18,8 @@ object TrafficMonitor {
|
||||
var txTotal: Long = 0
|
||||
var rxTotal: Long = 0
|
||||
|
||||
def getTraffic: Traffic = {
|
||||
new Traffic(Math.max(TrafficStats.getTotalTxBytes - TrafficStats.getUidTxBytes(uid), 0),
|
||||
Math.max(TrafficStats.getTotalRxBytes - TrafficStats.getUidRxBytes(uid), 0), System.currentTimeMillis())
|
||||
def getTraffic(tx: Long, rx: Long): Traffic = {
|
||||
new Traffic(tx, rx, System.currentTimeMillis())
|
||||
}
|
||||
|
||||
private val units = Array("KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "BB", "NB", "DB", "CB")
|
||||
@@ -39,8 +35,8 @@ object TrafficMonitor {
|
||||
else numberFormat.format(n) + ' ' + units(i)
|
||||
}
|
||||
|
||||
def update() {
|
||||
val now = getTraffic
|
||||
def update(tx: Long, rx: Long) {
|
||||
val now = getTraffic(tx, rx)
|
||||
val delta = now.timestamp - last.timestamp
|
||||
if (delta != 0) {
|
||||
txRate = (now.tx - last.tx) / delta
|
||||
@@ -56,7 +52,7 @@ object TrafficMonitor {
|
||||
rxRate = 0
|
||||
txTotal = 0
|
||||
rxTotal = 0
|
||||
last = getTraffic
|
||||
last = getTraffic(0, 0)
|
||||
}
|
||||
|
||||
def getTxTotal(): String = {
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Shadowsocks - A shadowsocks client for Android
|
||||
* Copyright (C) 2015 <max.c.lv@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*
|
||||
* ___====-_ _-====___
|
||||
* _--^^^#####// \\#####^^^--_
|
||||
* _-^##########// ( ) \\##########^-_
|
||||
* -############// |\^^/| \\############-
|
||||
* _/############// (@::@) \\############\_
|
||||
* /#############(( \\// ))#############\
|
||||
* -###############\\ (oo) //###############-
|
||||
* -#################\\ / VV \ //#################-
|
||||
* -###################\\/ \//###################-
|
||||
* _#/|##########/\######( /\ )######/\##########|\#_
|
||||
* |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \|
|
||||
* ` |/ V V ` V \#\| | | |/#/ V ' V V \| '
|
||||
* ` ` ` ` / | | | | \ ' ' ' '
|
||||
* ( | | | | )
|
||||
* __\ | | | | /__
|
||||
* (vvv(VVV)(VVV)vvv)
|
||||
*
|
||||
* HERE BE DRAGONS
|
||||
*
|
||||
*/
|
||||
|
||||
package com.github.shadowsocks.utils
|
||||
|
||||
import java.io.{File, FileDescriptor, IOException}
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
import android.net.{LocalServerSocket, LocalSocket, LocalSocketAddress}
|
||||
import android.util.Log
|
||||
import com.github.shadowsocks.BaseService
|
||||
|
||||
class TrafficMonitorThread(service: BaseService) extends Thread {
|
||||
|
||||
val TAG = "TrafficMonitorThread"
|
||||
val PATH = "/data/data/com.github.shadowsocks/stat_path"
|
||||
|
||||
@volatile var serverSocket: LocalServerSocket = null
|
||||
@volatile var isRunning: Boolean = true
|
||||
|
||||
def closeServerSocket() {
|
||||
if (serverSocket != null) {
|
||||
try {
|
||||
serverSocket.close()
|
||||
} catch {
|
||||
case _: Exception => // ignore
|
||||
}
|
||||
serverSocket = null
|
||||
}
|
||||
}
|
||||
|
||||
def stopThread() {
|
||||
isRunning = false
|
||||
closeServerSocket()
|
||||
}
|
||||
|
||||
override def run(): Unit = {
|
||||
|
||||
try {
|
||||
new File(PATH).delete()
|
||||
} catch {
|
||||
case _: Exception => // ignore
|
||||
}
|
||||
|
||||
try {
|
||||
val localSocket = new LocalSocket
|
||||
localSocket.bind(new LocalSocketAddress(PATH, LocalSocketAddress.Namespace.FILESYSTEM))
|
||||
serverSocket = new LocalServerSocket(localSocket.getFileDescriptor)
|
||||
} catch {
|
||||
case e: IOException =>
|
||||
Log.e(TAG, "unable to bind", e)
|
||||
return
|
||||
}
|
||||
|
||||
val pool = Executors.newFixedThreadPool(1)
|
||||
|
||||
while (isRunning) {
|
||||
try {
|
||||
val socket = serverSocket.accept()
|
||||
|
||||
pool.execute(() => {
|
||||
try {
|
||||
val input = socket.getInputStream
|
||||
val output = socket.getOutputStream
|
||||
|
||||
val buffer = new Array[Byte](256)
|
||||
val size = input.read(buffer)
|
||||
|
||||
val array = new Array[Byte](size)
|
||||
Array.copy(buffer, 0, array, 0, size)
|
||||
|
||||
val stat = new String(array, "UTF-8").split("\\|")
|
||||
if (stat.length == 2) {
|
||||
TrafficMonitor.update(stat(0).toLong, stat(1).toLong)
|
||||
service.updateTraffic(TrafficMonitor.getTxRate, TrafficMonitor.getRxRate,
|
||||
TrafficMonitor.getTxTotal, TrafficMonitor.getRxTotal)
|
||||
}
|
||||
|
||||
output.write(0)
|
||||
|
||||
input.close()
|
||||
output.close()
|
||||
|
||||
} catch {
|
||||
case e: Exception =>
|
||||
Log.e(TAG, "Error when recv traffic stat", e)
|
||||
}
|
||||
|
||||
// close socket
|
||||
try {
|
||||
socket.close()
|
||||
} catch {
|
||||
case _: Exception => // ignore
|
||||
}
|
||||
|
||||
})
|
||||
} catch {
|
||||
case e: IOException =>
|
||||
Log.e(TAG, "Error when accept socket", e)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user