-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,498 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
rsocket-transports/netty-tcp/api/rsocket-transport-netty-tcp.api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
public abstract interface class io/rsocket/kotlin/transport/netty/tcp/NettyTcpClientTarget : io/rsocket/kotlin/transport/RSocketClientTarget { | ||
public abstract fun getRemoteAddress ()Ljava/net/SocketAddress; | ||
} | ||
|
||
public abstract interface class io/rsocket/kotlin/transport/netty/tcp/NettyTcpClientTransport : io/rsocket/kotlin/transport/RSocketTransport { | ||
public static final field Factory Lio/rsocket/kotlin/transport/netty/tcp/NettyTcpClientTransport$Factory; | ||
public fun target (Ljava/lang/String;I)Lio/rsocket/kotlin/transport/netty/tcp/NettyTcpClientTarget; | ||
} | ||
|
||
public final class io/rsocket/kotlin/transport/netty/tcp/NettyTcpClientTransport$Factory : io/rsocket/kotlin/transport/RSocketTransportFactory { | ||
} | ||
|
||
public abstract interface class io/rsocket/kotlin/transport/netty/tcp/NettyTcpClientTransportBuilder : 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 eventLoopGroup (Lio/netty/channel/EventLoopGroup;Z)V | ||
public abstract fun ssl (Lkotlin/jvm/functions/Function1;)V | ||
} | ||
|
||
public abstract interface class io/rsocket/kotlin/transport/netty/tcp/NettyTcpServerInstance : io/rsocket/kotlin/transport/RSocketServerInstance { | ||
public abstract fun getLocalAddress ()Ljava/net/InetSocketAddress; | ||
} | ||
|
||
public abstract interface class io/rsocket/kotlin/transport/netty/tcp/NettyTcpServerTarget : io/rsocket/kotlin/transport/RSocketServerTarget { | ||
public abstract fun getLocalAddress ()Ljava/net/InetSocketAddress; | ||
} | ||
|
||
public abstract interface class io/rsocket/kotlin/transport/netty/tcp/NettyTcpServerTransport : io/rsocket/kotlin/transport/RSocketTransport { | ||
public static final field Factory Lio/rsocket/kotlin/transport/netty/tcp/NettyTcpServerTransport$Factory; | ||
public fun target ()Lio/rsocket/kotlin/transport/netty/tcp/NettyTcpServerTarget; | ||
public fun target (Ljava/lang/String;I)Lio/rsocket/kotlin/transport/netty/tcp/NettyTcpServerTarget; | ||
public static synthetic fun target$default (Lio/rsocket/kotlin/transport/netty/tcp/NettyTcpServerTransport;Ljava/lang/String;IILjava/lang/Object;)Lio/rsocket/kotlin/transport/netty/tcp/NettyTcpServerTarget; | ||
} | ||
|
||
public final class io/rsocket/kotlin/transport/netty/tcp/NettyTcpServerTransport$Factory : io/rsocket/kotlin/transport/RSocketTransportFactory { | ||
} | ||
|
||
public abstract interface class io/rsocket/kotlin/transport/netty/tcp/NettyTcpServerTransportBuilder : 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 eventLoopGroup (Lio/netty/channel/EventLoopGroup;Lio/netty/channel/EventLoopGroup;Z)V | ||
public abstract fun eventLoopGroup (Lio/netty/channel/EventLoopGroup;Z)V | ||
public abstract fun ssl (Lkotlin/jvm/functions/Function1;)V | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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 TCP client/server transport implementation" | ||
|
||
kotlin { | ||
jvmTarget() | ||
|
||
sourceSets { | ||
jvmMain.dependencies { | ||
implementation(projects.rsocketInternalIo) | ||
api(projects.rsocketCore) | ||
api(libs.netty.handler) | ||
} | ||
jvmTest.dependencies { | ||
implementation(projects.rsocketTransportTests) | ||
implementation(libs.bouncycastle) | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...ty-tcp/src/jvmMain/kotlin/io/rsocket/kotlin/transport/netty/tcp/NettyTcpChannelHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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.tcp | ||
|
||
import io.ktor.utils.io.core.* | ||
import io.netty.buffer.* | ||
import io.netty.channel.* | ||
import io.netty.channel.socket.* | ||
import io.netty.handler.codec.* | ||
import io.netty.handler.ssl.* | ||
import io.rsocket.kotlin.internal.io.* | ||
import io.rsocket.kotlin.transport.* | ||
import kotlinx.coroutines.channels.* | ||
import kotlinx.coroutines.channels.Channel | ||
import java.net.* | ||
import kotlin.coroutines.* | ||
|
||
internal class NettyTcpChannelHandler( | ||
private val sslContext: SslContext?, | ||
private val remoteAddress: SocketAddress?, | ||
) : ChannelInitializer<DuplexChannel>() { | ||
private val frames = channelForCloseable<ByteReadPacket>(Channel.UNLIMITED) | ||
|
||
@RSocketTransportApi | ||
fun connect( | ||
context: CoroutineContext, | ||
channel: DuplexChannel, | ||
): NettyTcpSession = NettyTcpSession( | ||
coroutineContext = context, | ||
channel = channel, | ||
frames = frames | ||
) | ||
|
||
override fun initChannel(ch: DuplexChannel): Unit = with(ch.pipeline()) { | ||
if (sslContext != null) { | ||
val sslHandler = if ( | ||
remoteAddress is InetSocketAddress && | ||
ch.parent() == null // not server | ||
) { | ||
sslContext.newHandler(ch.alloc(), remoteAddress.hostName, remoteAddress.port) | ||
} else { | ||
sslContext.newHandler(ch.alloc()) | ||
} | ||
addLast("ssl", sslHandler) | ||
} | ||
addLast( | ||
"rsocket-length-encoder", | ||
LengthFieldPrepender( | ||
/* lengthFieldLength = */ 3 | ||
) | ||
) | ||
addLast( | ||
"rsocket-length-decoder", | ||
LengthFieldBasedFrameDecoder( | ||
/* maxFrameLength = */ Int.MAX_VALUE, | ||
/* lengthFieldOffset = */ 0, | ||
/* lengthFieldLength = */ 3, | ||
/* lengthAdjustment = */ 0, | ||
/* initialBytesToStrip = */ 3 | ||
) | ||
) | ||
addLast( | ||
"rsocket-frame-receiver", | ||
IncomingFramesChannelHandler(frames) | ||
) | ||
} | ||
|
||
private class IncomingFramesChannelHandler( | ||
private val channel: SendChannel<ByteReadPacket>, | ||
) : SimpleChannelInboundHandler<ByteBuf>() { | ||
override fun channelInactive(ctx: ChannelHandlerContext) { | ||
channel.close() //TODO? | ||
super.channelInactive(ctx) | ||
} | ||
|
||
override fun channelRead0(ctx: ChannelHandlerContext, msg: ByteBuf) { | ||
channel.trySend(buildPacket { | ||
writeFully(msg.nioBuffer()) | ||
}).getOrThrow() | ||
} | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
...y-tcp/src/jvmMain/kotlin/io/rsocket/kotlin/transport/netty/tcp/NettyTcpClientTransport.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
* 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.tcp | ||
|
||
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.handler.ssl.* | ||
import io.rsocket.kotlin.internal.io.* | ||
import io.rsocket.kotlin.transport.* | ||
import kotlinx.coroutines.* | ||
import java.net.* | ||
import kotlin.coroutines.* | ||
import kotlin.reflect.* | ||
|
||
public sealed interface NettyTcpClientTarget : RSocketClientTarget { | ||
public val remoteAddress: SocketAddress | ||
} | ||
|
||
public sealed interface NettyTcpClientTransport : RSocketTransport< | ||
InetSocketAddress, | ||
NettyTcpClientTarget> { | ||
|
||
public fun target(hostname: String, port: Int): NettyTcpClientTarget = target(InetSocketAddress(hostname, port)) | ||
|
||
public companion object Factory : RSocketTransportFactory< | ||
InetSocketAddress, | ||
NettyTcpClientTarget, | ||
NettyTcpClientTransport, | ||
NettyTcpClientTransportBuilder>(::NettyTcpClientTransportBuilderImpl) | ||
} | ||
|
||
public sealed interface NettyTcpClientTransportBuilder : RSocketTransportBuilder< | ||
InetSocketAddress, | ||
NettyTcpClientTarget, | ||
NettyTcpClientTransport> { | ||
|
||
public fun channel(cls: KClass<out DuplexChannel>) | ||
public fun channelFactory(factory: ChannelFactory<out DuplexChannel>) | ||
public fun eventLoopGroup(group: EventLoopGroup, manage: Boolean) | ||
|
||
public fun bootstrap(block: Bootstrap.() -> Unit) | ||
public fun ssl(block: SslContextBuilder.() -> Unit) | ||
} | ||
|
||
private class NettyTcpClientTransportBuilderImpl : NettyTcpClientTransportBuilder { | ||
private var channelFactory: ChannelFactory<out DuplexChannel>? = null | ||
private var eventLoopGroup: EventLoopGroup? = null | ||
private var manageEventLoopGroup: Boolean = false | ||
private var bootstrap: (Bootstrap.() -> Unit)? = null | ||
private var ssl: (SslContextBuilder.() -> Unit)? = null | ||
|
||
override fun channel(cls: KClass<out DuplexChannel>) { | ||
this.channelFactory = ReflectiveChannelFactory(cls.java) | ||
} | ||
|
||
override fun channelFactory(factory: ChannelFactory<out DuplexChannel>) { | ||
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 ssl(block: SslContextBuilder.() -> Unit) { | ||
ssl = block | ||
} | ||
|
||
@RSocketTransportApi | ||
override fun buildTransport(context: CoroutineContext): NettyTcpClientTransport { | ||
val group = eventLoopGroup ?: NioEventLoopGroup() | ||
val factory = channelFactory ?: ReflectiveChannelFactory(NioSocketChannel::class.java) | ||
|
||
val transportContext = context.supervisorContext() + group.asCoroutineDispatcher() | ||
if (manageEventLoopGroup) CoroutineScope(transportContext).invokeOnCancellation { | ||
group.shutdownGracefully().awaitFuture() | ||
} | ||
|
||
val sslContext = ssl?.let { | ||
SslContextBuilder | ||
.forClient() | ||
.apply(it) | ||
.build() | ||
} | ||
|
||
val bootstrap = Bootstrap().apply { | ||
bootstrap?.invoke(this) | ||
group(group) | ||
channelFactory(factory) | ||
} | ||
|
||
return NettyTcpClientTransportImpl( | ||
coroutineContext = transportContext, | ||
sslContext = sslContext, | ||
bootstrap = bootstrap | ||
) | ||
} | ||
} | ||
|
||
private class NettyTcpClientTransportImpl( | ||
override val coroutineContext: CoroutineContext, | ||
private val sslContext: SslContext?, | ||
private val bootstrap: Bootstrap, | ||
) : NettyTcpClientTransport { | ||
override fun target(address: InetSocketAddress): NettyTcpClientTarget = NettyTcpClientTargetImpl( | ||
coroutineContext = coroutineContext.supervisorContext(), | ||
remoteAddress = address, | ||
sslContext = sslContext, | ||
bootstrap = bootstrap | ||
) | ||
} | ||
|
||
private class NettyTcpClientTargetImpl( | ||
override val coroutineContext: CoroutineContext, | ||
override val remoteAddress: SocketAddress, | ||
private val sslContext: SslContext?, | ||
private val bootstrap: Bootstrap, | ||
) : NettyTcpClientTarget { | ||
@RSocketTransportApi | ||
override suspend fun createSession(): RSocketTransportSession { | ||
ensureActive() | ||
|
||
val handler = NettyTcpChannelHandler( | ||
sslContext = sslContext, | ||
remoteAddress = remoteAddress | ||
) | ||
val future = bootstrap.clone().apply { | ||
handler(handler) | ||
}.connect(remoteAddress) | ||
|
||
future.awaitFuture() | ||
|
||
return handler.connect(coroutineContext.childContext(), future.channel() as DuplexChannel) | ||
} | ||
} |
Oops, something went wrong.