Skip to content
Snippets Groups Projects
Commit 6cbcb823 authored by Davide Pianca's avatar Davide Pianca
Browse files

Add runSuspend client function

parent 7d90ecbb
Branches
Tags
No related merge requests found
import kotlinx.atomicfu.AtomicBoolean
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
......@@ -354,6 +355,7 @@ public class MQTTClient(
/**
* Run a single iteration of the client (blocking)
* This function blocks the thread for a single iteration duration
*/
public fun step() {
if (running.value) {
......@@ -363,6 +365,7 @@ public class MQTTClient(
/**
* Run the client (blocking)
* This function blocks the thread until the client stops
*/
public fun run() {
while (running.value) {
......@@ -370,6 +373,17 @@ public class MQTTClient(
}
}
/**
* Run the client
* This function runs the thread on the specified dispatcher until the client stops
* @param dispatcher the dispatcher on which to run the client
*/
public fun runSuspend(dispatcher: CoroutineDispatcher = Dispatchers.Default) {
CoroutineScope(dispatcher).launch {
run()
}
}
private fun handlePacket(packet: MQTTPacket) {
when (packet) {
is MQTTConnack -> handleConnack(packet)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment