Connection
A bidirectional channel that can both host and call services/sub-services.
(Meaning @KsServices can be used for both input and output of any @KsMethod)
Samples
import com.monkopedia.ksrpc.RpcService
import com.monkopedia.ksrpc.annotation.KsMethod
import com.monkopedia.ksrpc.annotation.KsService
import com.monkopedia.ksrpc.channels.connect
import com.monkopedia.ksrpc.ksrpcEnvironment
import com.monkopedia.ksrpc.serialized
import com.monkopedia.ksrpc.sockets.asConnection
import com.monkopedia.ksrpc.toStub
import io.ktor.utils.io.ByteChannel
fun main() {
//sampleStart
val env = ksrpcEnvironment { }
val readChannel = ByteChannel(autoFlush = true)
val writeChannel = ByteChannel(autoFlush = true)
val connection = (readChannel to writeChannel).asConnection(env)
// Host side: register the server's service.
val serverService = object : ServerApi {
override suspend fun fetchData(key: String): String = "value-for-$key"
}
connection.registerDefault(serverService.serialized(env))
// Client side: get the remote service via defaultChannel.
val remoteStub = connection.defaultChannel().toStub<ServerApi, String>()
val data = remoteStub.fetchData("my-key")
//sampleEnd
}Properties
The maximum ServiceTier this host supports. Defaults to ServiceTier.BIDI (full capability). Implementations with limited transport capabilities should override this to a lower tier.
A human-readable name for this transport, used in error messages when a service exceeds the supportedTier.
Functions
Connects both default channels for a connection (incoming and outgoing).
Raw version of connect, performing the same functionality with SerializedService directly.
Get a SerializedService that is the default on this client (i.e. using DEFAULT channel id). This should act as the root service for most scenarios.
Add a callback to be invoked when SuspendCloseable.close is called.
Register the primary service to be hosted on this communication channel.
Register a service to be hosted on the default channel.
Takes a given channel id and creates a service wrapper to make calls on that channel.