Significantly decreased CPU usage.
Calls to InputStream#available() weren't blocking, so the loops in both classes were running hot. Replaced the conditional with a blocking call instead. CPU usage is down from 100% to barely noticable.
This commit is contained in:
parent
b58bc9d198
commit
02a506608f
|
@ -65,12 +65,13 @@ class BluetoothTransferThread(context: Context, device: Device, socket: Bluetoot
|
||||||
|
|
||||||
while (socket.isConnected) {
|
while (socket.isConnected) {
|
||||||
try {
|
try {
|
||||||
if (inStream.available() > 0) {
|
// Block until data arrives.
|
||||||
|
inStream.read(Array[Byte](), 0, 0)
|
||||||
|
|
||||||
val msg = Message.read(inStream)
|
val msg = Message.read(inStream)
|
||||||
|
Log.v(Tag, "Received " + msg)
|
||||||
|
|
||||||
onReceive(msg, device.id)
|
onReceive(msg, device.id)
|
||||||
Log.v(Tag, "Receiving " + msg)
|
|
||||||
}
|
|
||||||
} catch {
|
} catch {
|
||||||
case e @ (_: ReadMessageException | _: IOException) =>
|
case e @ (_: ReadMessageException | _: IOException) =>
|
||||||
Log.w(Tag, "Failed to read incoming message", e)
|
Log.w(Tag, "Failed to read incoming message", e)
|
||||||
|
|
|
@ -50,12 +50,14 @@ class InternetConnectionThread(socket: Socket, crypto: Crypto, onDisconnected: (
|
||||||
try {
|
try {
|
||||||
socket.setKeepAlive(true)
|
socket.setKeepAlive(true)
|
||||||
while (socket.isConnected) {
|
while (socket.isConnected) {
|
||||||
if (inStream.available() > 0) {
|
// Block until data arrives.
|
||||||
|
inStream.read(Array[Byte](), 0, 0)
|
||||||
|
|
||||||
val msg = Message.read(inStream)
|
val msg = Message.read(inStream)
|
||||||
|
Log.v(Tag, "Received " + msg)
|
||||||
|
|
||||||
onReceive(msg, this)
|
onReceive(msg, this)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} catch {
|
} catch {
|
||||||
case e @ (_: ReadMessageException | _: IOException) =>
|
case e @ (_: ReadMessageException | _: IOException) =>
|
||||||
Log.w(Tag, "Failed to read incoming message", e)
|
Log.w(Tag, "Failed to read incoming message", e)
|
||||||
|
|
Reference in New Issue