|
| 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.local |
| 18 | + |
| 19 | +import io.ktor.utils.io.core.* |
| 20 | +import io.rsocket.kotlin.internal.io.* |
| 21 | +import io.rsocket.kotlin.transport.* |
| 22 | +import kotlinx.coroutines.* |
| 23 | +import kotlinx.coroutines.channels.* |
| 24 | +import kotlin.coroutines.* |
| 25 | + |
| 26 | +internal sealed class LocalServerConnector { |
| 27 | + @RSocketTransportApi |
| 28 | + abstract fun connect( |
| 29 | + acceptor: RSocketServerAcceptor, |
| 30 | + clientScope: CoroutineScope, |
| 31 | + serverScope: CoroutineScope, |
| 32 | + ): RSocketTransportSession |
| 33 | + |
| 34 | + class Sequential( |
| 35 | + private val connectionBufferCapacity: Int, |
| 36 | + ) : LocalServerConnector() { |
| 37 | + @RSocketTransportApi |
| 38 | + override fun connect( |
| 39 | + acceptor: RSocketServerAcceptor, |
| 40 | + clientScope: CoroutineScope, |
| 41 | + serverScope: CoroutineScope, |
| 42 | + ): RSocketTransportSession { |
| 43 | + clientScope.ensureActive() |
| 44 | + serverScope.ensureActive() |
| 45 | + |
| 46 | + val clientToServer = channelForCloseable<ByteReadPacket>(connectionBufferCapacity) |
| 47 | + val serverToClient = channelForCloseable<ByteReadPacket>(connectionBufferCapacity) |
| 48 | + |
| 49 | + serverScope.launch { |
| 50 | + acceptor.acceptSession( |
| 51 | + Session( |
| 52 | + coroutineContext = coroutineContext.childContext(), |
| 53 | + outbound = serverToClient, |
| 54 | + inbound = clientToServer |
| 55 | + ) |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + return Session( |
| 60 | + coroutineContext = clientScope.coroutineContext.childContext(), |
| 61 | + outbound = clientToServer, |
| 62 | + inbound = serverToClient |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + @RSocketTransportApi |
| 68 | + private class Session( |
| 69 | + override val coroutineContext: CoroutineContext, |
| 70 | + private val outbound: SendChannel<ByteReadPacket>, |
| 71 | + private val inbound: ReceiveChannel<ByteReadPacket>, |
| 72 | + ) : RSocketTransportSession.Sequential { |
| 73 | + |
| 74 | + init { |
| 75 | + coroutineContext.job.invokeOnCompletion { |
| 76 | + outbound.close(it) |
| 77 | + inbound.cancel(CancellationException("Local connection closed", it)) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + override suspend fun sendFrame(frame: ByteReadPacket) { |
| 82 | + outbound.send(frame) |
| 83 | + } |
| 84 | + |
| 85 | + override suspend fun receiveFrame(): ByteReadPacket { |
| 86 | + return inbound.receive() |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + class Multiplexed( |
| 93 | + private val connectionStreamsQueueCapacity: Int, |
| 94 | + private val streamBufferCapacity: Int, |
| 95 | + ) : LocalServerConnector() { |
| 96 | + @RSocketTransportApi |
| 97 | + override fun connect( |
| 98 | + acceptor: RSocketServerAcceptor, |
| 99 | + clientScope: CoroutineScope, |
| 100 | + serverScope: CoroutineScope, |
| 101 | + ): RSocketTransportSession { |
| 102 | + clientScope.ensureActive() |
| 103 | + serverScope.ensureActive() |
| 104 | + |
| 105 | + val clientToServer = channelForCloseable<Stream>(connectionStreamsQueueCapacity) |
| 106 | + val serverToClient = channelForCloseable<Stream>(connectionStreamsQueueCapacity) |
| 107 | + |
| 108 | + serverScope.launch { |
| 109 | + acceptor.acceptSession( |
| 110 | + Session( |
| 111 | + coroutineContext = coroutineContext.childContext(), |
| 112 | + outbound = serverToClient, |
| 113 | + inbound = clientToServer, |
| 114 | + streamBufferCapacity = streamBufferCapacity |
| 115 | + ) |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + return Session( |
| 120 | + coroutineContext = clientScope.coroutineContext.childContext(), |
| 121 | + outbound = clientToServer, |
| 122 | + inbound = serverToClient, |
| 123 | + streamBufferCapacity = streamBufferCapacity |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + @RSocketTransportApi |
| 128 | + private class Session( |
| 129 | + override val coroutineContext: CoroutineContext, |
| 130 | + private val outbound: SendChannel<Stream>, |
| 131 | + private val inbound: ReceiveChannel<Stream>, |
| 132 | + private val streamBufferCapacity: Int, |
| 133 | + ) : RSocketTransportSession.Multiplexed { |
| 134 | + private val streamsContext = coroutineContext.supervisorContext() |
| 135 | + |
| 136 | + init { |
| 137 | + coroutineContext.job.invokeOnCompletion { |
| 138 | + outbound.close(it) |
| 139 | + inbound.cancel(CancellationException("Local connection closed", it)) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + override suspend fun createStream(): RSocketTransportSession.Multiplexed.Stream { |
| 144 | + val clientToServer = channelForCloseable<ByteReadPacket>(streamBufferCapacity) |
| 145 | + val serverToClient = channelForCloseable<ByteReadPacket>(streamBufferCapacity) |
| 146 | + |
| 147 | + outbound.send( |
| 148 | + Stream( |
| 149 | + coroutineContext = streamsContext.childContext(), |
| 150 | + outbound = serverToClient, |
| 151 | + inbound = clientToServer |
| 152 | + ) |
| 153 | + ) |
| 154 | + |
| 155 | + return Stream( |
| 156 | + coroutineContext = streamsContext.childContext(), |
| 157 | + outbound = clientToServer, |
| 158 | + inbound = serverToClient |
| 159 | + ) |
| 160 | + } |
| 161 | + |
| 162 | + override suspend fun awaitStream(): RSocketTransportSession.Multiplexed.Stream { |
| 163 | + return inbound.receive() |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @RSocketTransportApi |
| 168 | + private class Stream( |
| 169 | + override val coroutineContext: CoroutineContext, |
| 170 | + private val outbound: SendChannel<ByteReadPacket>, |
| 171 | + private val inbound: ReceiveChannel<ByteReadPacket>, |
| 172 | + ) : RSocketTransportSession.Multiplexed.Stream, Closeable { |
| 173 | + |
| 174 | + init { |
| 175 | + coroutineContext.job.invokeOnCompletion { |
| 176 | + outbound.close(it) |
| 177 | + inbound.cancel(CancellationException("Local stream closed", it)) |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + override fun updatePriority(value: Int) {} // no effect |
| 182 | + |
| 183 | + override suspend fun sendFrame(frame: ByteReadPacket) { |
| 184 | + outbound.send(frame) |
| 185 | + } |
| 186 | + |
| 187 | + override suspend fun receiveFrame(): ByteReadPacket { |
| 188 | + return inbound.receive() |
| 189 | + } |
| 190 | + |
| 191 | + override fun close() { |
| 192 | + cancel("Stream is closed") // just for undelivered element case |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + } |
| 197 | +} |
0 commit comments