From 83ef91e7e00cb3921bfe242b8a7e5b969996843c Mon Sep 17 00:00:00 2001 From: Oleg Yukhnevich Date: Mon, 13 Nov 2023 08:38:42 +0200 Subject: [PATCH] wip --- rsocket-transport-api/build.gradle.kts | 38 ++++++++++++++++ .../transport/RSocketClientTransport.kt | 23 ++++++++++ .../transport/RSocketServerTransport.kt | 35 +++++++++++++++ .../kotlin/transport/RSocketTransport.kt | 37 +++++++++++++++ .../kotlin/transport/RSocketTransportApi.kt | 24 ++++++++++ .../transport/RSocketTransportConnection.kt | 45 +++++++++++++++++++ settings.gradle.kts | 1 + 7 files changed, 203 insertions(+) create mode 100644 rsocket-transport-api/build.gradle.kts create mode 100644 rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketClientTransport.kt create mode 100644 rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketServerTransport.kt create mode 100644 rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketTransport.kt create mode 100644 rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketTransportApi.kt create mode 100644 rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketTransportConnection.kt diff --git a/rsocket-transport-api/build.gradle.kts b/rsocket-transport-api/build.gradle.kts new file mode 100644 index 00000000..0a095b1d --- /dev/null +++ b/rsocket-transport-api/build.gradle.kts @@ -0,0 +1,38 @@ +/* + * Copyright 2015-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import rsocketbuild.* + +plugins { + id("rsocketbuild.template.library") +} + +kotlin { + jvmTarget() + jsTarget() + nativeTargets() + + sourceSets { + commonMain { + dependencies { + api(libs.kotlinx.coroutines.core) + api(libs.ktor.io) + } + } + } +} + +description = "RSocket Transport API" diff --git a/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketClientTransport.kt b/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketClientTransport.kt new file mode 100644 index 00000000..6806e8b6 --- /dev/null +++ b/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketClientTransport.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2015-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.rsocket.kotlin.transport + +@SubclassOptInRequired(RSocketTransportApi::class) +public interface RSocketClientTransport : RSocketTransport { + @RSocketTransportApi + public suspend fun connect(): RSocketTransportConnection +} diff --git a/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketServerTransport.kt b/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketServerTransport.kt new file mode 100644 index 00000000..e7a6330e --- /dev/null +++ b/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketServerTransport.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2015-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.rsocket.kotlin.transport + +import kotlinx.coroutines.* + +@SubclassOptInRequired(RSocketTransportApi::class) +public interface RSocketServerTransport : RSocketTransport { + @RSocketTransportApi + public suspend fun bind(acceptor: RSocketServerAcceptor): Instance +} + +@RSocketTransportApi +public interface RSocketServerAcceptor { + public suspend fun accept(connection: RSocketTransportConnection) +} + +// cancelling it will cancel server +// coroutineContext of transport should contain SupervisorJob +@SubclassOptInRequired(RSocketTransportApi::class) +public interface RSocketServerInstance : CoroutineScope diff --git a/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketTransport.kt b/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketTransport.kt new file mode 100644 index 00000000..617fe60e --- /dev/null +++ b/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketTransport.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2015-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.rsocket.kotlin.transport + +import kotlinx.coroutines.* +import kotlin.coroutines.* + +// coroutineContext of transport should contain SupervisorJob +@SubclassOptInRequired(RSocketTransportApi::class) +public interface RSocketTransport : CoroutineScope + +// coroutineContext of transport should contain SupervisorJob +@SubclassOptInRequired(RSocketTransportApi::class) +public interface RSocketTransportEngine : CoroutineScope { + public fun createTransport(target: Target): Transport +} + +// TODO: split into transport factory and RSocketTransportEngineFactory +@SubclassOptInRequired(RSocketTransportApi::class) +public interface RSocketTransportFactory, Builder> { + public operator fun invoke(context: CoroutineContext, target: Target, block: Builder.() -> Unit = {}): Transport + public fun Engine(context: CoroutineContext, block: Builder.() -> Unit = {}): Engine +} diff --git a/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketTransportApi.kt b/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketTransportApi.kt new file mode 100644 index 00000000..b913622c --- /dev/null +++ b/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketTransportApi.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2015-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.rsocket.kotlin.transport + +@Retention(value = AnnotationRetention.BINARY) +@RequiresOptIn( + level = RequiresOptIn.Level.WARNING, + message = "This is an API which is used to implement transport for RSocket, such as WS or TCP. This API should not be used from general code" +) +public annotation class RSocketTransportApi diff --git a/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketTransportConnection.kt b/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketTransportConnection.kt new file mode 100644 index 00000000..7766592e --- /dev/null +++ b/rsocket-transport-api/src/commonMain/kotlin/io/rsocket/kotlin/transport/RSocketTransportConnection.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2015-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.rsocket.kotlin.transport + +import io.ktor.utils.io.core.* +import io.ktor.utils.io.core.internal.* +import io.ktor.utils.io.pool.* +import kotlinx.coroutines.* + +// todo: rename to session +// coroutineContext of connection should NOT contain SupervisorJob +@RSocketTransportApi +public sealed interface RSocketTransportConnection : CoroutineScope { + public val bufferPool: ObjectPool get() = ChunkBuffer.Pool + + public interface Sequential : RSocketTransportConnection { + public suspend fun sendFrame(frame: ByteReadPacket) + public suspend fun receiveFrame(): ByteReadPacket + } + + public interface Multiplexed : RSocketTransportConnection { + public interface Stream : Closeable { + public fun prioritize() + public suspend fun sendFrame(frame: ByteReadPacket) + public suspend fun receiveFrame(): ByteReadPacket + } + + public suspend fun createStream(): Stream + public suspend fun awaitStream(): Stream + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 3b05c558..003537f1 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -34,6 +34,7 @@ include("rsocket-test") // standalone transport modules include( + "rsocket-transport-api", "rsocket-transport-local", "rsocket-transport-ktor-tcp", "rsocket-transport-ktor-websocket-shared",