|
| 1 | +/* |
| 2 | + * Copyright 2015-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.rsocket.kotlin.transport.netty.tcp |
| 18 | + |
| 19 | +import io.netty.bootstrap.* |
| 20 | +import io.netty.channel.* |
| 21 | +import io.netty.channel.ChannelFactory |
| 22 | +import io.netty.channel.nio.* |
| 23 | +import io.netty.channel.socket.* |
| 24 | +import io.netty.channel.socket.nio.* |
| 25 | +import io.netty.handler.ssl.* |
| 26 | +import io.rsocket.kotlin.internal.io.* |
| 27 | +import io.rsocket.kotlin.transport.* |
| 28 | +import kotlinx.coroutines.* |
| 29 | +import java.net.* |
| 30 | +import kotlin.coroutines.* |
| 31 | +import kotlin.reflect.* |
| 32 | + |
| 33 | +public sealed interface NettyTcpClientTarget : RSocketClientTarget { |
| 34 | + public val remoteAddress: SocketAddress |
| 35 | +} |
| 36 | + |
| 37 | +public sealed interface NettyTcpClientTransport : RSocketTransport< |
| 38 | + InetSocketAddress, |
| 39 | + NettyTcpClientTarget> { |
| 40 | + |
| 41 | + public fun target(hostname: String, port: Int): NettyTcpClientTarget = target(InetSocketAddress(hostname, port)) |
| 42 | + |
| 43 | + public companion object Factory : RSocketTransportFactory< |
| 44 | + InetSocketAddress, |
| 45 | + NettyTcpClientTarget, |
| 46 | + NettyTcpClientTransport, |
| 47 | + NettyTcpClientTransportBuilder>(::NettyTcpClientTransportBuilderImpl) |
| 48 | +} |
| 49 | + |
| 50 | +public sealed interface NettyTcpClientTransportBuilder : RSocketTransportBuilder< |
| 51 | + InetSocketAddress, |
| 52 | + NettyTcpClientTarget, |
| 53 | + NettyTcpClientTransport> { |
| 54 | + |
| 55 | + public fun channel(cls: KClass<out DuplexChannel>) |
| 56 | + public fun channelFactory(factory: ChannelFactory<out DuplexChannel>) |
| 57 | + public fun eventLoopGroup(group: EventLoopGroup, manage: Boolean) |
| 58 | + |
| 59 | + public fun bootstrap(block: Bootstrap.() -> Unit) |
| 60 | + public fun ssl(block: SslContextBuilder.() -> Unit) |
| 61 | +} |
| 62 | + |
| 63 | +private class NettyTcpClientTransportBuilderImpl : NettyTcpClientTransportBuilder { |
| 64 | + private var channelFactory: ChannelFactory<out DuplexChannel>? = null |
| 65 | + private var eventLoopGroup: EventLoopGroup? = null |
| 66 | + private var manageEventLoopGroup: Boolean = false |
| 67 | + private var bootstrap: (Bootstrap.() -> Unit)? = null |
| 68 | + private var ssl: (SslContextBuilder.() -> Unit)? = null |
| 69 | + |
| 70 | + override fun channel(cls: KClass<out DuplexChannel>) { |
| 71 | + this.channelFactory = ReflectiveChannelFactory(cls.java) |
| 72 | + } |
| 73 | + |
| 74 | + override fun channelFactory(factory: ChannelFactory<out DuplexChannel>) { |
| 75 | + this.channelFactory = factory |
| 76 | + } |
| 77 | + |
| 78 | + override fun eventLoopGroup(group: EventLoopGroup, manage: Boolean) { |
| 79 | + this.eventLoopGroup = group |
| 80 | + this.manageEventLoopGroup = manage |
| 81 | + } |
| 82 | + |
| 83 | + override fun bootstrap(block: Bootstrap.() -> Unit) { |
| 84 | + bootstrap = block |
| 85 | + } |
| 86 | + |
| 87 | + override fun ssl(block: SslContextBuilder.() -> Unit) { |
| 88 | + ssl = block |
| 89 | + } |
| 90 | + |
| 91 | + @RSocketTransportApi |
| 92 | + override fun buildTransport(context: CoroutineContext): NettyTcpClientTransport { |
| 93 | + val group = eventLoopGroup ?: NioEventLoopGroup() |
| 94 | + val factory = channelFactory ?: ReflectiveChannelFactory(NioSocketChannel::class.java) |
| 95 | + |
| 96 | + val transportContext = context.supervisorContext() + group.asCoroutineDispatcher() |
| 97 | + if (manageEventLoopGroup) CoroutineScope(transportContext).invokeOnCancellation { |
| 98 | + group.shutdownGracefully().awaitFuture() |
| 99 | + } |
| 100 | + |
| 101 | + val sslContext = ssl?.let { |
| 102 | + SslContextBuilder |
| 103 | + .forClient() |
| 104 | + .apply(it) |
| 105 | + .build() |
| 106 | + } |
| 107 | + |
| 108 | + val bootstrap = Bootstrap().apply { |
| 109 | + bootstrap?.invoke(this) |
| 110 | + group(group) |
| 111 | + channelFactory(factory) |
| 112 | + } |
| 113 | + |
| 114 | + return NettyTcpClientTransportImpl( |
| 115 | + coroutineContext = transportContext, |
| 116 | + sslContext = sslContext, |
| 117 | + bootstrap = bootstrap |
| 118 | + ) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +private class NettyTcpClientTransportImpl( |
| 123 | + override val coroutineContext: CoroutineContext, |
| 124 | + private val sslContext: SslContext?, |
| 125 | + private val bootstrap: Bootstrap, |
| 126 | +) : NettyTcpClientTransport { |
| 127 | + override fun target(address: InetSocketAddress): NettyTcpClientTarget = NettyTcpClientTargetImpl( |
| 128 | + coroutineContext = coroutineContext.supervisorContext(), |
| 129 | + remoteAddress = address, |
| 130 | + sslContext = sslContext, |
| 131 | + bootstrap = bootstrap |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +private class NettyTcpClientTargetImpl( |
| 136 | + override val coroutineContext: CoroutineContext, |
| 137 | + override val remoteAddress: SocketAddress, |
| 138 | + private val sslContext: SslContext?, |
| 139 | + private val bootstrap: Bootstrap, |
| 140 | +) : NettyTcpClientTarget { |
| 141 | + @RSocketTransportApi |
| 142 | + override suspend fun createSession(): RSocketTransportSession { |
| 143 | + ensureActive() |
| 144 | + |
| 145 | + val handler = NettyTcpChannelHandler( |
| 146 | + sslContext = sslContext, |
| 147 | + remoteAddress = remoteAddress |
| 148 | + ) |
| 149 | + val future = bootstrap.clone().apply { |
| 150 | + handler(handler) |
| 151 | + }.connect(remoteAddress) |
| 152 | + |
| 153 | + future.awaitFuture() |
| 154 | + |
| 155 | + return handler.connect(coroutineContext.childContext(), future.channel() as DuplexChannel) |
| 156 | + } |
| 157 | +} |
0 commit comments