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:
Felix Ableitner 2016-02-10 21:18:51 +01:00
parent b58bc9d198
commit 02a506608f
2 changed files with 12 additions and 9 deletions

View file

@ -65,12 +65,13 @@ class BluetoothTransferThread(context: Context, device: Device, socket: Bluetoot
while (socket.isConnected) {
try {
if (inStream.available() > 0) {
val msg = Message.read(inStream)
// Block until data arrives.
inStream.read(Array[Byte](), 0, 0)
onReceive(msg, device.id)
Log.v(Tag, "Receiving " + msg)
}
val msg = Message.read(inStream)
Log.v(Tag, "Received " + msg)
onReceive(msg, device.id)
} catch {
case e @ (_: ReadMessageException | _: IOException) =>
Log.w(Tag, "Failed to read incoming message", e)

View file

@ -50,11 +50,13 @@ class InternetConnectionThread(socket: Socket, crypto: Crypto, onDisconnected: (
try {
socket.setKeepAlive(true)
while (socket.isConnected) {
if (inStream.available() > 0) {
val msg = Message.read(inStream)
// Block until data arrives.
inStream.read(Array[Byte](), 0, 0)
onReceive(msg, this)
}
val msg = Message.read(inStream)
Log.v(Tag, "Received " + msg)
onReceive(msg, this)
}
} catch {
case e @ (_: ReadMessageException | _: IOException) =>