kotlinx.rpc 0.2.1 Help

RPC Servers

RPCServer interface represents an RPC server, that accepts RPC messages and may contain multiple services to route to. RPCServer uses data from incoming RPC messages and routes it to the designated service and sends service's response back to the corresponding client.

You can provide your own RPCServer implementation or use the one provided out of the box. Note that client and server must use the same RPC protocol to communicate.

Use registerService function to add your own factory for implemented RPC services. This factory function should accept CoroutineContext argument and pass it to the service, which should use it to override coroutineContext property of parent interface. This ensures proper application lifetime for your services.

Example usage:

val server: RPCServer server.registerService<MyService> { ctx: CoroutineContext -> MyServiceImpl(ctx) }

The registerService function requires the explicit type of the declared RPC service. That means that the code will not work if you provide it with the type of the service implementation:

// Wrong! Should be `MyService` as type argument server.registerService<MyServiceImpl> { ctx: CoroutineContext -> MyServiceImpl(ctx) }

KRPCServer — RPCServer for kRPC Protocol

KRPCServer abstract class implements RPCServer and all the logic for processing RPC messages and again leaves RPCTransport methods for the specific implementations (see transports).

Example usage with custom transport:

// same MySimpleRPCTransport as in the client example above class MySimpleRPCServer : KRPCServer(rpcServerConfig(), MySimpleRPCTransport()) val server = MySimpleRPCServer() server.registerService<MyService> { ctx -> MyServiceImpl(ctx) }

Note that here we pass explicit MyService type parameter to the registerService method. You must explicitly specify the type of the service interface here, otherwise the server service will not be found.

Last modified: 13 June 2024