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:
@@ -133,8 +133,14 @@ class ListenService : Service() {
|
|||||||
private var updateCallback: (() -> Unit)? = null
|
private var updateCallback: (() -> Unit)? = null
|
||||||
private fun doListen(address: String?, port: Int) {
|
private fun doListen(address: String?, port: Int) {
|
||||||
val lt = Thread {
|
val lt = Thread {
|
||||||
val socket = Socket(address, port)
|
try {
|
||||||
streamAudio(socket).onFailure { e ->
|
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) {
|
when (e) {
|
||||||
is IOException -> Log.e(TAG, "Failed to stream audio", e)
|
is IOException -> Log.e(TAG, "Failed to stream audio", e)
|
||||||
else -> Log.e(TAG, "Failed to stream audio for other reason", e)
|
else -> Log.e(TAG, "Failed to stream audio for other reason", e)
|
||||||
|
|||||||
Reference in New Issue
Block a user