Improved handling for invalid incoming messages.
This commit is contained in:
parent
77237decb0
commit
21b014654c
2 changed files with 8 additions and 3 deletions
|
@ -94,9 +94,9 @@ class TransferThread(device: Device, socket: BluetoothSocket, service: ChatServi
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
case e: IOException =>
|
case e: IOException =>
|
||||||
Log.w(Tag, "Connection to " + device.Name + " closed with exception", e)
|
Log.i(Tag, "Failed to read incoming message", e)
|
||||||
service.onConnectionChanged(new Device(device.bluetoothDevice, false), null)
|
case e: RuntimeException =>
|
||||||
return
|
Log.i(Tag, "Received invalid message", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
service.onConnectionChanged(new Device(device.bluetoothDevice, false), null)
|
service.onConnectionChanged(new Device(device.bluetoothDevice, false), null)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.nutomic.ensichat.messages
|
package com.nutomic.ensichat.messages
|
||||||
|
|
||||||
|
import java.io.IOException
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
|
||||||
import com.nutomic.ensichat.bluetooth.Device
|
import com.nutomic.ensichat.bluetooth.Device
|
||||||
|
@ -36,6 +37,8 @@ object Message {
|
||||||
def read(bytes: Array[Byte]): (Message, Array[Byte]) = {
|
def read(bytes: Array[Byte]): (Message, Array[Byte]) = {
|
||||||
val up = new ScalaMessagePack().createBufferUnpacker(bytes)
|
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 messageType = up.readInt()
|
||||||
val sender = new Device.ID(up.readString())
|
val sender = new Device.ID(up.readString())
|
||||||
val receiver = 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.DeviceInfo => DeviceInfoMessage.read(sender, receiver, date, up)
|
||||||
case Type.RequestAddContact => RequestAddContactMessage.read(sender, receiver, date, up)
|
case Type.RequestAddContact => RequestAddContactMessage.read(sender, receiver, date, up)
|
||||||
case Type.ResultAddContact => ResultAddContactMessage.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)
|
}, sig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue