Improved handling for invalid incoming messages.

This commit is contained in:
Felix Ableitner 2014-11-18 11:50:29 +02:00
parent 77237decb0
commit 21b014654c
2 changed files with 8 additions and 3 deletions

View File

@ -94,9 +94,9 @@ class TransferThread(device: Device, socket: BluetoothSocket, service: ChatServi
}
} catch {
case e: IOException =>
Log.w(Tag, "Connection to " + device.Name + " closed with exception", e)
service.onConnectionChanged(new Device(device.bluetoothDevice, false), null)
return
Log.i(Tag, "Failed to read incoming message", e)
case e: RuntimeException =>
Log.i(Tag, "Received invalid message", e)
}
}
service.onConnectionChanged(new Device(device.bluetoothDevice, false), null)

View File

@ -1,5 +1,6 @@
package com.nutomic.ensichat.messages
import java.io.IOException
import java.util.Date
import com.nutomic.ensichat.bluetooth.Device
@ -36,6 +37,8 @@ object Message {
def read(bytes: Array[Byte]): (Message, Array[Byte]) = {
val up = new ScalaMessagePack().createBufferUnpacker(bytes)
@throws[IOException]("If the message can't be parsed")
@throws[RuntimeException]("If the message has an unknown type")
val messageType = up.readInt()
val sender = new Device.ID(up.readString())
val receiver = new Device.ID(up.readString())
@ -46,6 +49,8 @@ object Message {
case Type.DeviceInfo => DeviceInfoMessage.read(sender, receiver, date, up)
case Type.RequestAddContact => RequestAddContactMessage.read(sender, receiver, date, up)
case Type.ResultAddContact => ResultAddContactMessage.read(sender, receiver, date, up)
case t =>
throw new RuntimeException("Received message of unknown type " + t)
}, sig)
}