Skip to content

Commit

Permalink
netty TCP, WebSocket and QUIC transports
Browse files Browse the repository at this point in the history
* QUIC implementation is still WIP
  • Loading branch information
whyoleg committed May 25, 2024
1 parent fc6b2a3 commit e9f1ff8
Show file tree
Hide file tree
Showing 28 changed files with 2,459 additions and 0 deletions.
12 changes: 12 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ kotlinx-bcv = "0.14.0"

ktor = "2.3.11"

netty = "4.1.110.Final"
netty-quic = "0.0.63.Final"

# for netty TLS tests
bouncycastle = "1.78.1"

turbine = "1.1.0"

rsocket-java = "1.1.3"
Expand Down Expand Up @@ -39,6 +45,12 @@ ktor-server-cio = { module = "io.ktor:ktor-server-cio", version.ref = "ktor" }
ktor-server-netty = { module = "io.ktor:ktor-server-netty", version.ref = "ktor" }
ktor-server-jetty = { module = "io.ktor:ktor-server-jetty", version.ref = "ktor" }

netty-handler = { module = "io.netty:netty-handler", version.ref = "netty" }
netty-codec-http = { module = "io.netty:netty-codec-http", version.ref = "netty" }
netty-codec-quic = { module = "io.netty.incubator:netty-incubator-codec-native-quic", version.ref = "netty-quic" }

bouncycastle = { module = "org.bouncycastle:bcpkix-jdk18on", version.ref = "bouncycastle" }

turbine = { module = "app.cash.turbine:turbine", version.ref = "turbine" }

rsocket-java-core = { module = 'io.rsocket:rsocket-core', version.ref = "rsocket-java" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public final class io/rsocket/kotlin/transport/netty/internal/CoroutinesKt {
public static final fun awaitChannel (Lio/netty/channel/ChannelFuture;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun awaitFuture (Lio/netty/util/concurrent/Future;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun callOnCancellation (Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function1;)V
public static final fun toByteBuf (Lio/ktor/utils/io/core/ByteReadPacket;)Lio/netty/buffer/ByteBuf;
public static final fun toByteReadPacket (Lio/netty/buffer/ByteBuf;)Lio/ktor/utils/io/core/ByteReadPacket;
}

35 changes: 35 additions & 0 deletions rsocket-transports/netty-internal/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2015-2024 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.multiplatform-library")
}

description = "rsocket-kotlin Netty transport utils"

kotlin {
jvmTarget()

sourceSets {
jvmMain.dependencies {
implementation(projects.rsocketInternalIo)
api(projects.rsocketCore)
api(libs.netty.handler)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2015-2024 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.netty.internal

import io.ktor.utils.io.core.*
import io.netty.buffer.*
import io.netty.channel.*
import io.netty.util.concurrent.*
import kotlinx.coroutines.*
import kotlin.coroutines.*

@Suppress("UNCHECKED_CAST")
public suspend inline fun <T> Future<T>.awaitFuture(): T = suspendCancellableCoroutine { cont ->
addListener {
when {
it.isSuccess -> cont.resume(it.now as T)
else -> cont.resumeWithException(it.cause())
}
}
cont.invokeOnCancellation {
cancel(true)
}
}

public suspend fun ChannelFuture.awaitChannel(): Channel {
awaitFuture()
return channel()
}

// it should be used only for cleanup and so should not really block, only suspend
public inline fun CoroutineScope.callOnCancellation(crossinline block: suspend () -> Unit) {
launch(Dispatchers.Unconfined) {
try {
awaitCancellation()
} catch (cause: Throwable) {
withContext(NonCancellable) {
try {
block()
} catch (suppressed: Throwable) {
cause.addSuppressed(suppressed)
}
}
throw cause
}
}
}

// TODO: what to use: this or ByteReadPacket(msg.nioBuffer())
public fun ByteBuf.toByteReadPacket(): ByteReadPacket = buildPacket { writeFully(nioBuffer()) }
public fun ByteReadPacket.toByteBuf(): ByteBuf = Unpooled.wrappedBuffer(readByteBuffer())
43 changes: 43 additions & 0 deletions rsocket-transports/netty-quic/api/rsocket-transport-netty-quic.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
public abstract interface class io/rsocket/kotlin/transport/netty/quic/NettyQuicClientTransport : io/rsocket/kotlin/transport/RSocketTransport {
public static final field Factory Lio/rsocket/kotlin/transport/netty/quic/NettyQuicClientTransport$Factory;
public abstract fun target (Ljava/lang/String;I)Lio/rsocket/kotlin/transport/RSocketClientTarget;
public abstract fun target (Ljava/net/InetSocketAddress;)Lio/rsocket/kotlin/transport/RSocketClientTarget;
}

public final class io/rsocket/kotlin/transport/netty/quic/NettyQuicClientTransport$Factory : io/rsocket/kotlin/transport/RSocketTransportFactory {
}

public abstract interface class io/rsocket/kotlin/transport/netty/quic/NettyQuicClientTransportBuilder : io/rsocket/kotlin/transport/RSocketTransportBuilder {
public abstract fun bootstrap (Lkotlin/jvm/functions/Function1;)V
public abstract fun channel (Lkotlin/reflect/KClass;)V
public abstract fun channelFactory (Lio/netty/channel/ChannelFactory;)V
public abstract fun codec (Lkotlin/jvm/functions/Function1;)V
public abstract fun eventLoopGroup (Lio/netty/channel/EventLoopGroup;Z)V
public abstract fun quicBootstrap (Lkotlin/jvm/functions/Function1;)V
public abstract fun ssl (Lkotlin/jvm/functions/Function1;)V
}

public abstract interface class io/rsocket/kotlin/transport/netty/quic/NettyQuicServerInstance : io/rsocket/kotlin/transport/RSocketServerInstance {
public abstract fun getLocalAddress ()Ljava/net/InetSocketAddress;
}

public abstract interface class io/rsocket/kotlin/transport/netty/quic/NettyQuicServerTransport : io/rsocket/kotlin/transport/RSocketTransport {
public static final field Factory Lio/rsocket/kotlin/transport/netty/quic/NettyQuicServerTransport$Factory;
public abstract fun target (Ljava/lang/String;I)Lio/rsocket/kotlin/transport/RSocketServerTarget;
public abstract fun target (Ljava/net/InetSocketAddress;)Lio/rsocket/kotlin/transport/RSocketServerTarget;
public static synthetic fun target$default (Lio/rsocket/kotlin/transport/netty/quic/NettyQuicServerTransport;Ljava/lang/String;IILjava/lang/Object;)Lio/rsocket/kotlin/transport/RSocketServerTarget;
public static synthetic fun target$default (Lio/rsocket/kotlin/transport/netty/quic/NettyQuicServerTransport;Ljava/net/InetSocketAddress;ILjava/lang/Object;)Lio/rsocket/kotlin/transport/RSocketServerTarget;
}

public final class io/rsocket/kotlin/transport/netty/quic/NettyQuicServerTransport$Factory : io/rsocket/kotlin/transport/RSocketTransportFactory {
}

public abstract interface class io/rsocket/kotlin/transport/netty/quic/NettyQuicServerTransportBuilder : io/rsocket/kotlin/transport/RSocketTransportBuilder {
public abstract fun bootstrap (Lkotlin/jvm/functions/Function1;)V
public abstract fun channel (Lkotlin/reflect/KClass;)V
public abstract fun channelFactory (Lio/netty/channel/ChannelFactory;)V
public abstract fun codec (Lkotlin/jvm/functions/Function1;)V
public abstract fun eventLoopGroup (Lio/netty/channel/EventLoopGroup;Z)V
public abstract fun ssl (Lkotlin/jvm/functions/Function1;)V
}

57 changes: 57 additions & 0 deletions rsocket-transports/netty-quic/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2015-2024 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.multiplatform-library")
}

description = "rsocket-kotlin Netty QUIC client/server transport implementation"

kotlin {
jvmTarget()

sourceSets {
jvmMain.dependencies {
implementation(projects.rsocketTransportNettyInternal)
implementation(projects.rsocketInternalIo)
api(projects.rsocketCore)
api(libs.netty.handler)
api(libs.netty.codec.quic)
}
jvmTest.dependencies {
implementation(projects.rsocketTransportTests)
implementation(libs.bouncycastle)
implementation(libs.netty.codec.quic.map {
val javaOsName = System.getProperty("os.name")
val javaOsArch = System.getProperty("os.arch")
val suffix = when {
javaOsName.contains("mac", ignoreCase = true) -> "osx"
javaOsName.contains("linux", ignoreCase = true) -> "linux"
javaOsName.contains("windows", ignoreCase = true) -> "windows"
else -> error("Unknown os.name: $javaOsName")
} + "-" + when (javaOsArch) {
"x86_64", "amd64" -> "x86_64"
"arm64", "aarch64" -> "aarch_64"
else -> error("Unknown os.arch: $javaOsArch")
}
"$it:$suffix"
})
//implementation("ch.qos.logback:logback-classic:1.2.11")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Copyright 2015-2024 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.netty.quic

import io.netty.bootstrap.*
import io.netty.channel.*
import io.netty.channel.ChannelFactory
import io.netty.channel.nio.*
import io.netty.channel.socket.*
import io.netty.channel.socket.nio.*
import io.netty.incubator.codec.quic.*
import io.rsocket.kotlin.internal.io.*
import io.rsocket.kotlin.transport.*
import io.rsocket.kotlin.transport.netty.internal.*
import kotlinx.coroutines.*
import java.net.*
import kotlin.coroutines.*
import kotlin.reflect.*

@OptIn(RSocketTransportApi::class)
public sealed interface NettyQuicClientTransport : RSocketTransport {
public fun target(remoteAddress: InetSocketAddress): RSocketClientTarget
public fun target(host: String, port: Int): RSocketClientTarget

public companion object Factory :
RSocketTransportFactory<NettyQuicClientTransport, NettyQuicClientTransportBuilder>(::NettyQuicClientTransportBuilderImpl)
}

@OptIn(RSocketTransportApi::class)
public sealed interface NettyQuicClientTransportBuilder : RSocketTransportBuilder<NettyQuicClientTransport> {
public fun channel(cls: KClass<out DatagramChannel>)
public fun channelFactory(factory: ChannelFactory<out DatagramChannel>)
public fun eventLoopGroup(group: EventLoopGroup, manage: Boolean)

public fun bootstrap(block: Bootstrap.() -> Unit)
public fun codec(block: QuicClientCodecBuilder.() -> Unit)
public fun ssl(block: QuicSslContextBuilder.() -> Unit)
public fun quicBootstrap(block: QuicChannelBootstrap.() -> Unit)
}

private class NettyQuicClientTransportBuilderImpl : NettyQuicClientTransportBuilder {
private var channelFactory: ChannelFactory<out DatagramChannel>? = null
private var eventLoopGroup: EventLoopGroup? = null
private var manageEventLoopGroup: Boolean = false
private var bootstrap: (Bootstrap.() -> Unit)? = null
private var codec: (QuicClientCodecBuilder.() -> Unit)? = null
private var ssl: (QuicSslContextBuilder.() -> Unit)? = null
private var quicBootstrap: (QuicChannelBootstrap.() -> Unit)? = null

override fun channel(cls: KClass<out DatagramChannel>) {
this.channelFactory = ReflectiveChannelFactory(cls.java)
}

override fun channelFactory(factory: ChannelFactory<out DatagramChannel>) {
this.channelFactory = factory
}

override fun eventLoopGroup(group: EventLoopGroup, manage: Boolean) {
this.eventLoopGroup = group
this.manageEventLoopGroup = manage
}

override fun bootstrap(block: Bootstrap.() -> Unit) {
bootstrap = block
}

override fun codec(block: QuicClientCodecBuilder.() -> Unit) {
codec = block
}

override fun ssl(block: QuicSslContextBuilder.() -> Unit) {
ssl = block
}

override fun quicBootstrap(block: QuicChannelBootstrap.() -> Unit) {
quicBootstrap = block
}

@RSocketTransportApi
override fun buildTransport(context: CoroutineContext): NettyQuicClientTransport {
val codecHandler = QuicClientCodecBuilder().apply {
// by default, we allow Int.MAX_VALUE of active stream
initialMaxData(Int.MAX_VALUE.toLong())
initialMaxStreamDataBidirectionalLocal(Int.MAX_VALUE.toLong())
initialMaxStreamDataBidirectionalRemote(Int.MAX_VALUE.toLong())
initialMaxStreamsBidirectional(Int.MAX_VALUE.toLong())
codec?.invoke(this)
ssl?.let {
sslContext(QuicSslContextBuilder.forClient().apply(it).build())
}
}.build()
val bootstrap = Bootstrap().apply {
bootstrap?.invoke(this)
localAddress(0)
handler(codecHandler)
channelFactory(channelFactory ?: ReflectiveChannelFactory(NioDatagramChannel::class.java))
group(eventLoopGroup ?: NioEventLoopGroup())
}

return NettyQuicClientTransportImpl(
coroutineContext = context.supervisorContext() + bootstrap.config().group().asCoroutineDispatcher(),
bootstrap = bootstrap,
quicBootstrap = quicBootstrap,
manageBootstrap = manageEventLoopGroup
)
}
}

private class NettyQuicClientTransportImpl(
override val coroutineContext: CoroutineContext,
private val bootstrap: Bootstrap,
private val quicBootstrap: (QuicChannelBootstrap.() -> Unit)?,
manageBootstrap: Boolean,
) : NettyQuicClientTransport {
init {
if (manageBootstrap) callOnCancellation {
bootstrap.config().group().shutdownGracefully().awaitFuture()
}
}

override fun target(remoteAddress: InetSocketAddress): NettyQuicClientTargetImpl = NettyQuicClientTargetImpl(
coroutineContext = coroutineContext.supervisorContext(),
bootstrap = bootstrap,
quicBootstrap = quicBootstrap,
remoteAddress = remoteAddress
)

override fun target(host: String, port: Int): RSocketClientTarget = target(InetSocketAddress(host, port))
}

@OptIn(RSocketTransportApi::class)
private class NettyQuicClientTargetImpl(
override val coroutineContext: CoroutineContext,
private val bootstrap: Bootstrap,
private val quicBootstrap: (QuicChannelBootstrap.() -> Unit)?,
private val remoteAddress: SocketAddress,
) : RSocketClientTarget {
@RSocketTransportApi
override fun connectClient(handler: RSocketConnectionHandler): Job = launch {
QuicChannel.newBootstrap(bootstrap.bind().awaitChannel()).also { quicBootstrap?.invoke(it) }
.handler(
NettyQuicConnectionInitializer(handler, coroutineContext, isClient = true)
).remoteAddress(remoteAddress).connect().awaitFuture()
}
}
Loading

0 comments on commit e9f1ff8

Please sign in to comment.