KsrpcNativeHost

JVM entry point for talking to a ksrpc service hosted inside a Kotlin/Native shared library.

The library must already be loaded (e.g. via NativeUtils.loadLibraryFromJar). The consumer declares the native binding on one of their own classes -- an external fun whose @CName is named after that class -- and passes a reference to it into connect. The binding takes a single JniHostInit, which the consumer forwards to ksrpcHostConnection:

// The consumer's own JVM class declares the binding:
object MyNativeService {
external fun initialize(host: JniHostInit)
}

// ... and connects with it:
NativeUtils.loadLibraryFromJar("/libmyservice.so")
val connection = KsrpcNativeHost.connect(scope, MyNativeService::initialize)
val service = connection.defaultChannel().toStub<MyService, JniSerialized>()

The matching native side (@CName("Java_..._MyNativeService_initialize")) just forwards the JniHostInit to com.monkopedia.ksrpc.jni.ksrpcHostConnection.

Functions

Link copied to clipboard
suspend fun connect(scope: CoroutineScope, initialize: (JniHostInit) -> Unit, environment: KsrpcEnvironment<JniSerialized> = ksrpcEnvironment(JniSerialization()) {}): JniConnection

Opens a Connection to the native host on the given scope: it builds the connection, invokes initialize (the consumer's native binding) so the host's setup lambda runs against this connection (building its own per-connection native environment and service instance(s)), and returns it ready to use. Each call is independent -- nothing is shared across connections.