Skip to content

Commit

Permalink
Fix port disconnection issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti committed Nov 11, 2021
1 parent 141578f commit 1e63688
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class RunnableOSCExchange(
private val outPort: OSCPortOut
) {

init {
inPort.startListening()
}

/**
* Runs the exchange
*/
Expand Down Expand Up @@ -92,16 +96,13 @@ class RunnableOSCExchange(

val timeoutHandler = Handler(Looper.getMainLooper())
val timeoutRunnable = Runnable {
inPort.dispatcher.removeListener(selector, listener.value)
inPort.stopListening()

@Suppress("ThrowableNotThrown")
request.onError(OSCTimeoutException(request.timeout!!))
}

listener.value = OSCMessageListener {
inPort.dispatcher.removeListener(selector, listener.value)
inPort.stopListening()

val args = OSCArgs.ofList(it.message.arguments)
request.onReceived(args)
Expand All @@ -116,7 +117,6 @@ class RunnableOSCExchange(
timeoutHandler.postDelayed(timeoutRunnable, request.timeout)

inPort.dispatcher.addListener(selector, listener.value)
inPort.startListening()
}

private fun completeExchange() {
Expand All @@ -129,6 +129,7 @@ class RunnableOSCExchange(
}

try {
inPort.stopListening()
inPort.close()
} catch (e: Exception) {
throw OSCPortClosingException(
Expand Down

0 comments on commit 1e63688

Please sign in to comment.