-
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.
Rework transport and internal implementation to support multiplexed t…
…ransports (#255) * new Transport API * migrate local to new Transport API and introduce multiplexed kind * fix some tests to use port 0 to auto-assign port
- Loading branch information
Showing
83 changed files
with
3,426 additions
and
1,642 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
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
151 changes: 151 additions & 0 deletions
151
rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/connection/Connection.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,151 @@ | ||
/* | ||
* 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.connection | ||
|
||
import io.ktor.utils.io.core.* | ||
import io.rsocket.kotlin.* | ||
import io.rsocket.kotlin.frame.* | ||
import io.rsocket.kotlin.internal.* | ||
import io.rsocket.kotlin.internal.io.* | ||
import io.rsocket.kotlin.operation.* | ||
import io.rsocket.kotlin.payload.* | ||
import io.rsocket.kotlin.transport.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.flow.* | ||
import kotlin.coroutines.* | ||
|
||
// TODO: rename to just `Connection` after root `Connection` will be dropped | ||
@RSocketTransportApi | ||
internal abstract class Connection2( | ||
protected val frameCodec: FrameCodec, | ||
// requestContext | ||
final override val coroutineContext: CoroutineContext, | ||
) : RSocket, Closeable { | ||
|
||
// connection establishment part | ||
|
||
abstract suspend fun establishConnection(handler: ConnectionEstablishmentHandler): ConnectionConfig | ||
|
||
// setup completed, start handling requests | ||
abstract suspend fun handleConnection(inbound: ConnectionInbound) | ||
|
||
// connection part | ||
|
||
protected abstract suspend fun sendConnectionFrame(frame: ByteReadPacket) | ||
private suspend fun sendConnectionFrame(frame: Frame): Unit = sendConnectionFrame(frameCodec.encodeFrame(frame)) | ||
|
||
suspend fun sendError(cause: Throwable) { | ||
sendConnectionFrame(ErrorFrame(0, cause)) | ||
} | ||
|
||
private suspend fun sendMetadataPush(metadata: ByteReadPacket) { | ||
sendConnectionFrame(MetadataPushFrame(metadata)) | ||
} | ||
|
||
suspend fun sendKeepAlive(respond: Boolean, data: ByteReadPacket, lastPosition: Long) { | ||
sendConnectionFrame(KeepAliveFrame(respond, lastPosition, data)) | ||
} | ||
|
||
// operations part | ||
|
||
protected abstract fun launchRequest(requestPayload: Payload, operation: RequesterOperation): Job | ||
private suspend fun ensureActiveOrClose(closeable: Closeable) { | ||
currentCoroutineContext().ensureActive { closeable.close() } | ||
coroutineContext.ensureActive { closeable.close() } | ||
} | ||
|
||
final override suspend fun metadataPush(metadata: ByteReadPacket) { | ||
ensureActiveOrClose(metadata) | ||
sendMetadataPush(metadata) | ||
} | ||
|
||
final override suspend fun fireAndForget(payload: Payload) { | ||
ensureActiveOrClose(payload) | ||
|
||
suspendCancellableCoroutine { cont -> | ||
val requestJob = launchRequest( | ||
requestPayload = payload, | ||
operation = RequesterFireAndForgetOperation(cont) | ||
) | ||
cont.invokeOnCancellation { cause -> | ||
requestJob.cancel("Request was cancelled", cause) | ||
} | ||
} | ||
} | ||
|
||
final override suspend fun requestResponse(payload: Payload): Payload { | ||
ensureActiveOrClose(payload) | ||
|
||
val responseDeferred = CompletableDeferred<Payload>() | ||
|
||
val requestJob = launchRequest( | ||
requestPayload = payload, | ||
operation = RequesterRequestResponseOperation(responseDeferred) | ||
) | ||
|
||
try { | ||
responseDeferred.join() | ||
} catch (cause: Throwable) { | ||
requestJob.cancel("Request was cancelled", cause) | ||
throw cause | ||
} | ||
return responseDeferred.await() | ||
} | ||
|
||
@OptIn(ExperimentalStreamsApi::class) | ||
final override fun requestStream( | ||
payload: Payload, | ||
): Flow<Payload> = payloadFlow { strategy, initialRequest -> | ||
ensureActiveOrClose(payload) | ||
|
||
val responsePayloads = PayloadChannel() | ||
|
||
val requestJob = launchRequest( | ||
requestPayload = payload, | ||
operation = RequesterRequestStreamOperation(initialRequest, responsePayloads) | ||
) | ||
|
||
throw try { | ||
responsePayloads.consumeInto(this, strategy) | ||
} catch (cause: Throwable) { | ||
requestJob.cancel("Request was cancelled", cause) | ||
throw cause | ||
} ?: return@payloadFlow | ||
} | ||
|
||
@OptIn(ExperimentalStreamsApi::class) | ||
final override fun requestChannel( | ||
initPayload: Payload, | ||
payloads: Flow<Payload>, | ||
): Flow<Payload> = payloadFlow { strategy, initialRequest -> | ||
ensureActiveOrClose(initPayload) | ||
|
||
val responsePayloads = PayloadChannel() | ||
|
||
val requestJob = launchRequest( | ||
initPayload, | ||
RequesterRequestChannelOperation(initialRequest, payloads, responsePayloads) | ||
) | ||
|
||
throw try { | ||
responsePayloads.consumeInto(this, strategy) | ||
} catch (cause: Throwable) { | ||
requestJob.cancel("Request was cancelled", cause) | ||
throw cause | ||
} ?: return@payloadFlow | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...core/src/commonMain/kotlin/io/rsocket/kotlin/connection/ConnectionEstablishmentContext.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,49 @@ | ||
/* | ||
* 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.connection | ||
|
||
import io.ktor.utils.io.core.* | ||
import io.rsocket.kotlin.frame.* | ||
import io.rsocket.kotlin.frame.io.* | ||
import io.rsocket.kotlin.keepalive.* | ||
import io.rsocket.kotlin.payload.* | ||
import io.rsocket.kotlin.transport.* | ||
|
||
// send/receive setup, resume, resume ok, lease, error | ||
@RSocketTransportApi | ||
internal abstract class ConnectionEstablishmentContext( | ||
private val frameCodec: FrameCodec, | ||
) { | ||
protected abstract suspend fun receiveFrameRaw(): ByteReadPacket? | ||
protected abstract suspend fun sendFrame(frame: ByteReadPacket) | ||
private suspend fun sendFrame(frame: Frame): Unit = sendFrame(frameCodec.encodeFrame(frame)) | ||
|
||
// only setup|lease|resume|resume_ok|error frames | ||
suspend fun receiveFrame(): Frame = frameCodec.decodeFrame( | ||
expectedStreamId = 0, | ||
frame = receiveFrameRaw() ?: error("Expected frame during connection establishment but nothing was received") | ||
) | ||
|
||
suspend fun sendSetup( | ||
version: Version, | ||
honorLease: Boolean, | ||
keepAlive: KeepAlive, | ||
resumeToken: ByteReadPacket?, | ||
payloadMimeType: PayloadMimeType, | ||
payload: Payload, | ||
): Unit = sendFrame(SetupFrame(version, honorLease, keepAlive, resumeToken, payloadMimeType, payload)) | ||
} |
Oops, something went wrong.