Skip to content

Commit

Permalink
Specify close reason if available
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed May 19, 2023
1 parent e81f8ff commit 6792cf0
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/src/main/java/be/mygod/librootkotlinx/RootServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class RootServer {
var active = true

abstract fun cancel()
abstract fun error(e: Throwable)
abstract fun shouldRemove(result: Byte): Boolean
abstract operator fun invoke(input: DataInputStream, result: Byte)
fun sendClosed() = server.execute(CancelCommand(index))
Expand Down Expand Up @@ -58,6 +59,9 @@ class RootServer {
class Ordinary(server: RootServer, index: Long, classLoader: ClassLoader?,
private val callback: CompletableDeferred<Parcelable?>) : Callback(server, index, classLoader) {
override fun cancel() = callback.cancel()
override fun error(e: Throwable) {
callback.completeExceptionally(e)
}
override fun shouldRemove(result: Byte) = true
override fun invoke(input: DataInputStream, result: Byte) {
if (result.toInt() == SUCCESS) callback.complete(input.readParcelable(classLoader))
Expand All @@ -69,6 +73,9 @@ class RootServer {
private val channel: SendChannel<Parcelable?>) : Callback(server, index, classLoader) {
val finish: CompletableDeferred<Unit> = CompletableDeferred()
override fun cancel() = finish.cancel()
override fun error(e: Throwable) {
finish.completeExceptionally(e)
}
override fun shouldRemove(result: Byte) = result.toInt() != SUCCESS
override fun invoke(input: DataInputStream, result: Byte) {
when (result.toInt()) {
Expand Down Expand Up @@ -202,7 +209,7 @@ class RootServer {
Logger.me.d("Received callback #$index: $result")
callback(input, result)
} catch (e: Throwable) {
callback.cancel()
callback.error(e)
throw e
}
}
Expand All @@ -225,18 +232,20 @@ class RootServer {
process.errorStream.bufferedReader().forEachLine(Logger.me::w)
} catch (_: IOException) { }
}
var cause: Throwable? = null
try {
callbackSpin()
if (active) throw UnexpectedExitException()
} catch (e: Throwable) {
cause = e
Logger.me.d("Shutting down from worker due to error", e)
process.destroy()
if (e !is EOFException) throw e
} finally {
Logger.me.d("Waiting for exit")
withContext(NonCancellable) { errorReader.await() }
process.waitFor()
closeInternal()
closeInternal(cause)
}
}
}
Expand Down Expand Up @@ -312,7 +321,7 @@ class RootServer {
}
}

private fun closeInternal() = synchronized(callbackLookup) {
private fun closeInternal(cause: Throwable? = null) = synchronized(callbackLookup) {
if (active) {
active = false
try {
Expand All @@ -325,7 +334,7 @@ class RootServer {
}
Logger.me.d("Client closed")
}
for (callback in callbackLookup.valueIterator()) callback.cancel()
for (callback in callbackLookup.valueIterator()) if (cause != null) callback.error(cause) else callback.cancel()
callbackLookup.clear()
}
/**
Expand Down

0 comments on commit 6792cf0

Please sign in to comment.