|
| 1 | +/* |
| 2 | + * Copyright 2015-2025 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.quic |
| 18 | + |
| 19 | +import io.netty.channel.* |
| 20 | +import io.netty.incubator.codec.quic.* |
| 21 | +import io.netty.util.* |
| 22 | +import io.rsocket.kotlin.internal.io.* |
| 23 | +import io.rsocket.kotlin.transport.* |
| 24 | +import io.rsocket.kotlin.transport.netty.internal.* |
| 25 | +import kotlinx.coroutines.* |
| 26 | +import kotlinx.coroutines.channels.Channel |
| 27 | +import kotlin.coroutines.* |
| 28 | + |
| 29 | +@RSocketTransportApi |
| 30 | +internal class NettyQuicConnection( |
| 31 | + parentContext: CoroutineContext, |
| 32 | + private val channel: QuicChannel, |
| 33 | + private val isClient: Boolean, |
| 34 | +) : RSocketMultiplexedConnection, ChannelInboundHandlerAdapter() { |
| 35 | + override val coroutineContext: CoroutineContext = parentContext.childContext() + channel.eventLoop().asCoroutineDispatcher() |
| 36 | + private val streamsContext = coroutineContext.supervisorContext() |
| 37 | + |
| 38 | + private val inboundStreams = Channel<RSocketMultiplexedConnection.Stream>(Channel.UNLIMITED) { |
| 39 | + it.cancel("Connection closed") |
| 40 | + } |
| 41 | + |
| 42 | + init { |
| 43 | + @OptIn(DelicateCoroutinesApi::class) |
| 44 | + launch(start = CoroutineStart.ATOMIC) { |
| 45 | + try { |
| 46 | + awaitCancellation() |
| 47 | + } finally { |
| 48 | + nonCancellable { |
| 49 | + inboundStreams.cancel() |
| 50 | + // stop streams first |
| 51 | + streamsContext.job.cancelAndJoin() |
| 52 | + channel.close().awaitFuture() |
| 53 | + // close UDP channel |
| 54 | + if (isClient) channel.parent().close().awaitFuture() |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + fun initStreamChannel(streamChannel: QuicStreamChannel) { |
| 61 | + val stream = NettyQuicStream(streamsContext, streamChannel) |
| 62 | + streamChannel.attr(NettyQuicStream.ATTRIBUTE).set(stream) |
| 63 | + streamChannel.pipeline().addLast("rsocket-quic-stream", stream) |
| 64 | + |
| 65 | + if (streamChannel.isLocalCreated) return |
| 66 | + |
| 67 | + if (inboundStreams.trySend(stream).isFailure) stream.cancel("Connection closed") |
| 68 | + } |
| 69 | + |
| 70 | + override fun channelInactive(ctx: ChannelHandlerContext) { |
| 71 | + cancel("Channel is not active") |
| 72 | + ctx.fireChannelInactive() |
| 73 | + } |
| 74 | + |
| 75 | + override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable?) { |
| 76 | + cancel("exceptionCaught", cause) |
| 77 | + } |
| 78 | + |
| 79 | + override suspend fun createStream(): RSocketMultiplexedConnection.Stream { |
| 80 | + val streamChannel = channel.createStream(QuicStreamType.BIDIRECTIONAL, NettyQuicStreamInitializer).awaitFuture() |
| 81 | + return streamChannel.attr(NettyQuicStream.ATTRIBUTE).get() |
| 82 | + } |
| 83 | + |
| 84 | + override suspend fun acceptStream(): RSocketMultiplexedConnection.Stream? { |
| 85 | + return inboundStreams.receiveCatching().getOrNull() |
| 86 | + } |
| 87 | + |
| 88 | + companion object { |
| 89 | + val ATTRIBUTE: AttributeKey<NettyQuicConnection> = AttributeKey.newInstance<NettyQuicConnection>("rsocket-quic-connection") |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +@RSocketTransportApi |
| 94 | +internal class NettyQuicConnectionInitializer( |
| 95 | + private val parentContext: CoroutineContext, |
| 96 | + private val onConnection: ((RSocketConnection) -> Unit)?, |
| 97 | +) : ChannelInitializer<QuicChannel>() { |
| 98 | + override fun initChannel(channel: QuicChannel) { |
| 99 | + val connection = NettyQuicConnection( |
| 100 | + parentContext = parentContext, |
| 101 | + channel = channel, |
| 102 | + isClient = onConnection == null |
| 103 | + ) |
| 104 | + channel.attr(NettyQuicConnection.ATTRIBUTE).set(connection) |
| 105 | + |
| 106 | + channel.pipeline().addLast( |
| 107 | + "rsocket-connection", |
| 108 | + connection |
| 109 | + ) |
| 110 | + |
| 111 | + onConnection?.invoke(connection) |
| 112 | + } |
| 113 | +} |
0 commit comments