ListenService: Wrap Socket in a Result

This way we can handle the IOException in the same place
as in the java version, without throwing passing unchecked exceptions
This commit is contained in:
Fabian Wiesel
2024-03-16 20:50:46 +01:00
parent a784eed506
commit 3588622bd2

View File

@@ -133,8 +133,14 @@ class ListenService : Service() {
private var updateCallback: (() -> Unit)? = null
private fun doListen(address: String?, port: Int) {
val lt = Thread {
val socket = Socket(address, port)
streamAudio(socket).onFailure { e ->
try {
Result.success(Socket(address, port))
} catch (e : IOException) {
Result.failure(e)
}.fold(
{ streamAudio(it) }, // This also may return a failure
{ Result.failure(it) } // Or we already have one
).onFailure { e ->
when (e) {
is IOException -> Log.e(TAG, "Failed to stream audio", e)
else -> Log.e(TAG, "Failed to stream audio for other reason", e)