Skip to content

Commit 4185675

Browse files
author
olme04
authored
cleanups general code and public API (#175)
Co-authored-by: olme04 <olme04>
1 parent 27f93b1 commit 4185675

File tree

95 files changed

+304
-351
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+304
-351
lines changed

build.gradle.kts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import groovy.util.Node
18-
import groovy.util.NodeList
19-
import org.gradle.api.publish.maven.internal.artifact.FileBasedMavenArtifact
17+
import groovy.util.*
18+
import org.gradle.api.publish.maven.internal.artifact.*
2019
import org.jetbrains.kotlin.gradle.dsl.*
2120
import org.jetbrains.kotlin.gradle.plugin.mpp.*
2221
import org.jetbrains.kotlin.konan.target.*
@@ -162,20 +161,21 @@ subprojects {
162161
useExperimentalAnnotation("kotlinx.coroutines.InternalCoroutinesApi")
163162
useExperimentalAnnotation("kotlinx.coroutines.ObsoleteCoroutinesApi")
164163
useExperimentalAnnotation("kotlinx.coroutines.FlowPreview")
164+
useExperimentalAnnotation("kotlinx.coroutines.DelicateCoroutinesApi")
165165

166-
useExperimentalAnnotation("io.ktor.util.KtorExperimentalAPI")
167166
useExperimentalAnnotation("io.ktor.util.InternalAPI")
168167
useExperimentalAnnotation("io.ktor.utils.io.core.internal.DangerousInternalIoApi")
169168

170169
useExperimentalAnnotation("io.rsocket.kotlin.TransportApi")
171170
useExperimentalAnnotation("io.rsocket.kotlin.ExperimentalMetadataApi")
172171
useExperimentalAnnotation("io.rsocket.kotlin.ExperimentalStreamsApi")
172+
useExperimentalAnnotation("io.rsocket.kotlin.RSocketLoggingApi")
173173
}
174174
}
175175
}
176176

177177
if (isLibProject && !isTestProject) {
178-
explicitApiWarning() //TODO change to strict before release
178+
explicitApi()
179179
sourceSets["commonTest"].dependencies {
180180
implementation(project(":rsocket-test"))
181181
}
@@ -204,8 +204,10 @@ subprojects {
204204
}
205205
}
206206

207-
fun publishPlatformArtifactsInRootModule(platformPublication:MavenPublication,
208-
kotlinMultiplatformPublication: MavenPublication) {
207+
fun publishPlatformArtifactsInRootModule(
208+
platformPublication: MavenPublication,
209+
kotlinMultiplatformPublication: MavenPublication
210+
) {
209211
lateinit var platformXml: XmlProvider
210212

211213
platformPublication.pom.withXml { platformXml = this }
@@ -312,7 +314,7 @@ subprojects {
312314
dependsOn(tasks.withType<Sign>())
313315
}
314316

315-
tasks.matching { it.name == "generatePomFileForKotlinMultiplatformPublication"}.configureEach {
317+
tasks.matching { it.name == "generatePomFileForKotlinMultiplatformPublication" }.configureEach {
316318
dependsOn(tasks["generatePomFileForJvmPublication"])
317319
}
318320
}
@@ -421,7 +423,7 @@ if (sonatypeUsername != null && sonatypePassword != null) {
421423
it
422424
)
423425
}
424-
else -> it.artifactId = "${project.name}-$type"
426+
else -> it.artifactId = "${project.name}-$type"
425427
}
426428
}
427429
}

examples/interactions/src/jvmMain/kotlin/RequestResponseErrorExample.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import io.rsocket.kotlin.*
1818
import io.rsocket.kotlin.core.*
19-
import io.rsocket.kotlin.payload.*
2019
import io.rsocket.kotlin.transport.local.*
2120
import kotlinx.coroutines.*
2221

examples/interactions/src/jvmMain/kotlin/RequestResponseExample.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import io.rsocket.kotlin.*
1818
import io.rsocket.kotlin.core.*
19-
import io.rsocket.kotlin.payload.*
2019
import io.rsocket.kotlin.transport.local.*
2120
import kotlinx.coroutines.*
2221

examples/interactions/src/jvmMain/kotlin/ServerRequestExample.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import io.rsocket.kotlin.*
1818
import io.rsocket.kotlin.core.*
19-
import io.rsocket.kotlin.payload.*
2019
import io.rsocket.kotlin.transport.local.*
2120
import kotlinx.coroutines.*
2221

examples/multiplatform-chat/src/clientMain/kotlin/Api.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import io.ktor.client.*
1818
import io.ktor.client.features.websocket.*
1919
import io.ktor.network.selector.*
20-
import io.ktor.network.sockets.*
2120
import io.ktor.util.*
2221
import io.rsocket.kotlin.*
2322
import io.rsocket.kotlin.core.*

examples/multiplatform-chat/src/clientMain/kotlin/ChatApi.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import io.ktor.utils.io.core.*
1818
import io.rsocket.kotlin.*
19-
import io.rsocket.kotlin.payload.*
2019
import kotlinx.serialization.*
2120
import kotlinx.serialization.protobuf.*
2221

examples/multiplatform-chat/src/serverJvmMain/kotlin/App.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import io.ktor.application.*
1818
import io.ktor.network.selector.*
19-
import io.ktor.network.sockets.*
2019
import io.ktor.routing.*
2120
import io.ktor.server.cio.*
2221
import io.ktor.server.engine.*

examples/nodejs-tcp-transport/src/jsMain/kotlin/Server.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import io.ktor.utils.io.core.*
1818
import io.ktor.utils.io.js.*
1919
import io.rsocket.kotlin.*
2020
import io.rsocket.kotlin.core.*
21-
import io.rsocket.kotlin.frame.io.*
2221
import io.rsocket.kotlin.payload.*
2322
import io.rsocket.kotlin.transport.*
2423
import kotlinx.coroutines.*
@@ -145,3 +144,17 @@ class NodeJsTcpConnection(private val socket: Socket) : Connection {
145144
return receiveChannel.receive()
146145
}
147146
}
147+
148+
private fun ByteReadPacket.readLength(): Int {
149+
val b = readByte().toInt() and 0xFF shl 16
150+
val b1 = readByte().toInt() and 0xFF shl 8
151+
val b2 = readByte().toInt() and 0xFF
152+
return b or b1 or b2
153+
}
154+
155+
private fun BytePacketBuilder.writeLength(length: Int) {
156+
require(length and 0xFFFFFF.inv() == 0) { "Length is larger than 24 bits" }
157+
writeByte((length shr 16).toByte())
158+
writeByte((length shr 8).toByte())
159+
writeByte(length.toByte())
160+
}

playground/src/commonMain/kotlin/TCP.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import io.ktor.network.selector.*
18-
import io.ktor.network.sockets.*
1918
import io.rsocket.kotlin.core.*
2019
import io.rsocket.kotlin.payload.*
2120
import io.rsocket.kotlin.transport.ktor.*

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/Annotations.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,12 @@ public annotation class ExperimentalMetadataApi
3737
message = "This is an API to customize request strategy of streams. This API can change in future in non backwards-compatible manner."
3838
)
3939
public annotation class ExperimentalStreamsApi
40+
41+
@Retention(value = AnnotationRetention.BINARY)
42+
@RequiresOptIn(
43+
level = RequiresOptIn.Level.WARNING,
44+
message = "This is mostly internal API used for logging. This API can change in future in non backwards-compatible manner."
45+
)
46+
public annotation class RSocketLoggingApi
47+
48+

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/Connection.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,16 @@ import kotlinx.coroutines.*
3030
public interface Connection {
3131
public val job: Job
3232

33-
@DangerousInternalIoApi
34-
public val pool: ObjectPool<ChunkBuffer>
35-
get() = ChunkBuffer.Pool
33+
public val pool: ObjectPool<ChunkBuffer> get() = ChunkBuffer.Pool
3634

3735
public suspend fun send(packet: ByteReadPacket)
3836
public suspend fun receive(): ByteReadPacket
3937
}
4038

41-
@OptIn(DangerousInternalIoApi::class, TransportApi::class)
39+
@OptIn(TransportApi::class)
4240
internal suspend fun Connection.receiveFrame(): Frame = receive().readFrame(pool)
4341

44-
@OptIn(DangerousInternalIoApi::class, TransportApi::class)
42+
@OptIn(TransportApi::class)
4543
internal suspend fun Connection.sendFrame(frame: Frame) {
4644
val packet = frame.toPacket(pool)
4745
packet.closeOnError { send(packet) }

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/ConnectionAcceptor.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,10 @@ package io.rsocket.kotlin
1919
import io.rsocket.kotlin.keepalive.*
2020
import io.rsocket.kotlin.payload.*
2121

22-
23-
//TODO fun interfaces don't support `suspend` functions for now... (seems will work in kotlin 1.5)
24-
25-
public interface ConnectionAcceptor {
22+
public fun interface ConnectionAcceptor {
2623
public suspend fun ConnectionAcceptorContext.accept(): RSocket
2724
}
2825

29-
public inline fun ConnectionAcceptor(crossinline block: suspend ConnectionAcceptorContext.() -> RSocket): ConnectionAcceptor =
30-
object : ConnectionAcceptor {
31-
override suspend fun ConnectionAcceptorContext.accept(): RSocket = block()
32-
}
33-
3426
public class ConnectionAcceptorContext internal constructor(
3527
public val config: ConnectionConfig,
3628
public val requester: RSocket,

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/RSocketError.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ public sealed class RSocketError(public val errorCode: Int, message: String) : T
4646
public const val MinAllowedCode: Int = ErrorCode.CustomMin
4747
public const val MaxAllowedCode: Int = ErrorCode.CustomMax
4848

49-
public inline fun checkCodeInAllowedRange(errorCode: Int): Boolean =
50-
MinAllowedCode <= errorCode || errorCode <= MaxAllowedCode
49+
public fun checkCodeInAllowedRange(errorCode: Int): Boolean =
50+
MinAllowedCode <= errorCode || errorCode <= MaxAllowedCode
5151
}
5252
}
5353
}
5454

5555
@Suppress("FunctionName") // function name intentionally starts with an uppercase letter
5656
internal fun RSocketError(streamId: Int, errorCode: Int, message: String): Throwable =
5757
when (streamId) {
58-
0 -> when (errorCode) {
58+
0 -> when (errorCode) {
5959
ErrorCode.InvalidSetup -> RSocketError.Setup.Invalid(message)
6060
ErrorCode.UnsupportedSetup -> RSocketError.Setup.Unsupported(message)
6161
ErrorCode.RejectedSetup -> RSocketError.Setup.Rejected(message)
@@ -66,11 +66,11 @@ internal fun RSocketError(streamId: Int, errorCode: Int, message: String): Throw
6666
}
6767
else -> when (errorCode) {
6868
ErrorCode.ApplicationError -> RSocketError.ApplicationError(message)
69-
ErrorCode.Rejected -> RSocketError.Rejected(message)
70-
ErrorCode.Canceled -> RSocketError.Canceled(message)
71-
ErrorCode.Invalid -> RSocketError.Invalid(message)
72-
else -> when (RSocketError.Custom.checkCodeInAllowedRange(errorCode)) {
73-
true -> RSocketError.Custom(errorCode, message)
69+
ErrorCode.Rejected -> RSocketError.Rejected(message)
70+
ErrorCode.Canceled -> RSocketError.Canceled(message)
71+
ErrorCode.Invalid -> RSocketError.Invalid(message)
72+
else -> when (RSocketError.Custom.checkCodeInAllowedRange(errorCode)) {
73+
true -> RSocketError.Custom(errorCode, message)
7474
false -> IllegalArgumentException("Invalid Error frame in Stream ID $streamId: $errorCode '$message'")
7575
}
7676
}

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/core/MimeType.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ package io.rsocket.kotlin.core
1818

1919
import io.rsocket.kotlin.frame.io.*
2020

21-
public interface MimeType
21+
public sealed interface MimeType
2222

23-
public interface MimeTypeWithName : MimeType {
23+
public sealed interface MimeTypeWithName : MimeType {
2424
public val text: String
2525
}
2626

27-
public interface MimeTypeWithId : MimeType {
27+
public sealed interface MimeTypeWithId : MimeType {
2828
public val identifier: Byte
2929
}
3030

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/core/RSocketConnector.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@ package io.rsocket.kotlin.core
1919
import io.rsocket.kotlin.*
2020
import io.rsocket.kotlin.frame.*
2121
import io.rsocket.kotlin.frame.io.*
22+
import io.rsocket.kotlin.internal.*
2223
import io.rsocket.kotlin.logging.*
2324
import io.rsocket.kotlin.transport.*
2425

25-
@OptIn(TransportApi::class)
26-
class RSocketConnector internal constructor(
26+
@OptIn(TransportApi::class, RSocketLoggingApi::class)
27+
public class RSocketConnector internal constructor(
2728
private val loggerFactory: LoggerFactory,
2829
private val interceptors: Interceptors,
2930
private val connectionConfigProvider: () -> ConnectionConfig,
3031
private val acceptor: ConnectionAcceptor,
3132
private val reconnectPredicate: ReconnectPredicate?,
3233
) {
3334

34-
suspend fun connect(transport: ClientTransport): RSocket = when (reconnectPredicate) {
35+
public suspend fun connect(transport: ClientTransport): RSocket = when (reconnectPredicate) {
3536
null -> connectOnce(transport)
3637
else -> ReconnectableRSocket(
3738
logger = loggerFactory.logger("io.rsocket.kotlin.connection"),

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/core/RSocketConnectorBuilder.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
package io.rsocket.kotlin.core
1818

1919
import io.rsocket.kotlin.*
20+
import io.rsocket.kotlin.internal.*
2021
import io.rsocket.kotlin.keepalive.*
2122
import io.rsocket.kotlin.logging.*
2223
import io.rsocket.kotlin.payload.*
2324
import kotlinx.coroutines.*
2425

2526
public class RSocketConnectorBuilder internal constructor() {
27+
@RSocketLoggingApi
2628
public var loggerFactory: LoggerFactory = DefaultLoggerFactory
2729

2830
private val connectionConfig: ConnectionConfigBuilder = ConnectionConfigBuilder()
@@ -42,10 +44,6 @@ public class RSocketConnectorBuilder internal constructor() {
4244
acceptor = block
4345
}
4446

45-
public fun acceptor(block: suspend ConnectionAcceptorContext.() -> RSocket) {
46-
acceptor(ConnectionAcceptor(block))
47-
}
48-
4947
/**
5048
* When configured, [RSocketConnector.connect] will return custom [RSocket] implementation,
5149
* which will try to reconnect if connection lost and [retries] are not exhausted with [predicate] returning `true`.
@@ -95,6 +93,7 @@ public class RSocketConnectorBuilder internal constructor() {
9593
}
9694
}
9795

96+
@OptIn(RSocketLoggingApi::class)
9897
internal fun build(): RSocketConnector = RSocketConnector(
9998
loggerFactory,
10099
interceptors.build(),

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/core/RSocketServer.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,17 @@ package io.rsocket.kotlin.core
1919
import io.rsocket.kotlin.*
2020
import io.rsocket.kotlin.frame.*
2121
import io.rsocket.kotlin.frame.io.*
22+
import io.rsocket.kotlin.internal.*
2223
import io.rsocket.kotlin.logging.*
2324
import io.rsocket.kotlin.transport.*
2425
import kotlinx.coroutines.*
2526

26-
@OptIn(TransportApi::class)
27+
@OptIn(TransportApi::class, RSocketLoggingApi::class)
2728
public class RSocketServer internal constructor(
2829
private val loggerFactory: LoggerFactory,
2930
private val interceptors: Interceptors,
3031
) {
3132

32-
public fun <T> bind(
33-
transport: ServerTransport<T>,
34-
block: suspend ConnectionAcceptorContext.() -> RSocket
35-
): T = bind(transport, ConnectionAcceptor(block))
36-
3733
public fun <T> bind(
3834
transport: ServerTransport<T>,
3935
acceptor: ConnectionAcceptor,

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/core/RSocketServerBuilder.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
package io.rsocket.kotlin.core
1818

19+
import io.rsocket.kotlin.*
1920
import io.rsocket.kotlin.logging.*
2021

2122
public class RSocketServerBuilder internal constructor() {
23+
@RSocketLoggingApi
2224
public var loggerFactory: LoggerFactory = DefaultLoggerFactory
2325

2426
private val interceptors: InterceptorsBuilder = InterceptorsBuilder()
@@ -27,6 +29,7 @@ public class RSocketServerBuilder internal constructor() {
2729
interceptors.configure()
2830
}
2931

32+
@OptIn(RSocketLoggingApi::class)
3033
internal fun build(): RSocketServer = RSocketServer(loggerFactory, interceptors.build())
3134
}
3235

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/frame/CancelFrame.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ package io.rsocket.kotlin.frame
1919
import io.ktor.utils.io.core.*
2020

2121
internal class CancelFrame(
22-
override val streamId: Int,
23-
) : Frame(FrameType.Cancel) {
22+
override val streamId: Int
23+
) : Frame() {
24+
override val type: FrameType get() = FrameType.Cancel
2425
override val flags: Int get() = 0
2526

2627
override fun release(): Unit = Unit

0 commit comments

Comments
 (0)