diff --git a/src/commonMain/kotlin/net.sergeych.parsec3/Parsec3Transport.kt b/src/commonMain/kotlin/net.sergeych.parsec3/Parsec3Transport.kt new file mode 100644 index 0000000..c109f0d --- /dev/null +++ b/src/commonMain/kotlin/net.sergeych.parsec3/Parsec3Transport.kt @@ -0,0 +1,15 @@ +package net.sergeych.parsec3 + +import kotlinx.coroutines.flow.StateFlow + +/** + * Transport level protocol (conforms to parsec 3.0). It is not specially secured could be used as is, as it provides bidirectional + * asynchronous (push capable) calls, but normally it should be used as a transport for parsec 3.1 secure + * protocol which uses this transport interface to run. + */ +interface Parsec3Transport { + val connectedFlow: StateFlow + fun close() + + suspend fun adapter(): Adapter +} \ No newline at end of file diff --git a/src/commonMain/kotlin/net.sergeych.parsec3/Parsec3WSClient.kt b/src/commonMain/kotlin/net.sergeych.parsec3/Parsec3WSClient.kt index 6f72e47..52fbada 100644 --- a/src/commonMain/kotlin/net.sergeych.parsec3/Parsec3WSClient.kt +++ b/src/commonMain/kotlin/net.sergeych.parsec3/Parsec3WSClient.kt @@ -16,27 +16,27 @@ class Parsec3WSClient>( val api: H, val exceptionsRegistry: ExceptionsRegistry = ExceptionsRegistry(), f: AdapterBuilder.() -> Unit, -) : LogTag("P3WSC") { +) : LogTag("P3WSC"), Parsec3Transport { val builder = AdapterBuilder(api, exceptionsRegistry, f) private val _connectionFlow = MutableStateFlow(false) private val closeFlow = MutableStateFlow(false) - val connectedFlow: StateFlow = _connectionFlow + override val connectedFlow: StateFlow = _connectionFlow init { start() } - fun close() { + override fun close() { if( closeFlow.value == false ) closeFlow.value = true } var deferredAdapter = CompletableDeferred>() private set - suspend fun adapter(): Adapter = deferredAdapter.await() + override suspend fun adapter(): Adapter = deferredAdapter.await() fun start() { globalLaunch { diff --git a/src/jvmMain/kotlin/net/sergeych/parsec3/WsServer.kt b/src/jvmMain/kotlin/net/sergeych/parsec3/WsServer.kt index 800b789..7c1bfa3 100644 --- a/src/jvmMain/kotlin/net/sergeych/parsec3/WsServer.kt +++ b/src/jvmMain/kotlin/net/sergeych/parsec3/WsServer.kt @@ -10,7 +10,7 @@ import java.time.Duration import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicLong -fun >Application.parsec3Server( +fun >Application.parsec3TransportServer( api: H, exceptionsRegistry: ExceptionsRegistry = ExceptionsRegistry(), path: String = "/api/p3", diff --git a/src/jvmTest/kotlin/net/sergeych/parsec3/WsServerKtTest.kt b/src/jvmTest/kotlin/net/sergeych/parsec3/WsServerKtTest.kt index a04e43c..1432922 100644 --- a/src/jvmTest/kotlin/net/sergeych/parsec3/WsServerKtTest.kt +++ b/src/jvmTest/kotlin/net/sergeych/parsec3/WsServerKtTest.kt @@ -21,7 +21,7 @@ internal class WsServerKtTest { fun testWsServer() { embeddedServer(Netty, port = 8080) { - parsec3Server( + parsec3TransportServer( TestApiServer, ) { newSession { TestSession() }