Skip to content

Commit 1e98bb8

Browse files
committed
Migrated RpcClient and kRPC to new descriptor API
1 parent d089657 commit 1e98bb8

File tree

21 files changed

+313
-266
lines changed

21 files changed

+313
-266
lines changed

core/src/commonMain/kotlin/kotlinx/rpc/RPCCall.kt

Lines changed: 0 additions & 50 deletions
This file was deleted.

core/src/commonMain/kotlin/kotlinx/rpc/RPCClient.kt

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.rpc
6+
7+
import kotlinx.rpc.descriptor.RpcServiceDescriptor
8+
9+
/**
10+
* Represents a method or field call of an RPC service.
11+
*
12+
* @property callableName The name of the callable. Can be the name of the method or field.
13+
* @property data The data for the call.
14+
* @property descriptor the descriptor of the service, that made the call.
15+
*/
16+
public data class RpcCall(
17+
val descriptor: RpcServiceDescriptor<*>,
18+
val callableName: String,
19+
val data: Any,
20+
val serviceId: Long,
21+
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.rpc
6+
7+
import kotlinx.coroutines.CoroutineScope
8+
import kotlinx.coroutines.Deferred
9+
import kotlin.coroutines.CoroutineContext
10+
11+
/**
12+
* [RpcClient] represents an abstraction of an RPC client, that can handle requests from several RPC services,
13+
* transform them, send to the server and handle responses and errors.
14+
* [CoroutineScope] defines the lifetime of the client.
15+
*/
16+
public interface RpcClient : CoroutineScope {
17+
/**
18+
* This method is used by generated clients to perform a call to the server.
19+
*
20+
* @param T type of the result
21+
* @param call an object that contains all required information about the called method,
22+
* that is needed to route it properly to the server.
23+
* @return actual result of the call, for example, data from the server.
24+
*/
25+
public suspend fun <T> call(call: RpcCall): T
26+
27+
/**
28+
* This method is used by generated clients to perform a call to the server.
29+
*
30+
* @param T type of the result
31+
* @param serviceScope service's coroutine scope
32+
* @param call an object that contains all required information about the called method,
33+
* that is needed to route it properly to the server.
34+
* @return actual result of the call, for example, data from the server
35+
*/
36+
public fun <T> callSync(serviceScope: CoroutineScope, call: RpcCall): Deferred<T>
37+
38+
/**
39+
* Provides child [CoroutineContext] for a new [RemoteService] service stub.
40+
*
41+
* This function shouldn't be called directly.
42+
*
43+
* @param serviceId id of the new service. Used for service cancellation messages.
44+
*/
45+
public fun provideStubContext(serviceId: Long): CoroutineContext
46+
}

core/src/commonMain/kotlin/kotlinx/rpc/annotations/Rpc.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import kotlinx.rpc.RemoteService
2323
* suspend fun sayHello(firstName: String, lastName: String, age: Int): String
2424
* }
2525
* // client code
26-
* val rpcClient: RPCClient
26+
* val rpcClient: RpcClient
2727
* val myService = rpcClient.withService<MyService>()
2828
* val greetingFromServer = myService.sayHello("Alex", "Smith", 35)
2929
* // server code

core/src/commonMain/kotlin/kotlinx/rpc/awaitFieldInitialization.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package kotlinx.rpc
66

77
import kotlinx.rpc.descriptor.serviceDescriptorOf
8-
import kotlinx.rpc.internal.RPCDeferredField
8+
import kotlinx.rpc.internal.RpcDeferredField
99
import kotlin.reflect.KClass
1010

1111
/**
@@ -29,9 +29,9 @@ import kotlin.reflect.KClass
2929
public suspend fun <T : RemoteService, R> T.awaitFieldInitialization(getter: T.() -> R): R {
3030
val field = getter()
3131

32-
if (field is RPCDeferredField<*>) {
32+
if (field is RpcDeferredField<*>) {
3333
@Suppress("UNCHECKED_CAST")
34-
return (field as RPCDeferredField<R>).await()
34+
return (field as RpcDeferredField<R>).await()
3535
}
3636

3737
error("Please choose required field for a valid RPC client generated by RPCClient.withService method")

core/src/commonMain/kotlin/kotlinx/rpc/descriptor/RpcServiceDescriptor.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
package kotlinx.rpc.descriptor
66

7-
import kotlinx.rpc.RPCClient
87
import kotlinx.rpc.RemoteService
8+
import kotlinx.rpc.RpcClient
99
import kotlinx.rpc.internal.*
1010
import kotlinx.rpc.internal.utils.ExperimentalRPCApi
1111
import kotlin.reflect.KClass
@@ -42,34 +42,34 @@ public fun <T : RemoteService> serviceDescriptorOf(kClass: KClass<T>): RpcServic
4242
public interface RpcServiceDescriptor<T : RemoteService> {
4343
public val fqName: String
4444

45-
public fun getFields(service: T): List<RPCDeferredField<*>>
45+
public fun getFields(service: T): List<RpcDeferredField<*>>
4646

4747
public fun getCallable(name: String): RpcCallable<T>?
4848

49-
public fun createInstance(serviceId: Long, client: RPCClient): T
49+
public fun createInstance(serviceId: Long, client: RpcClient): T
5050
}
5151

5252
@ExperimentalRPCApi
53-
public data class RpcCallable<T : RemoteService>(
53+
public class RpcCallable<T : RemoteService>(
5454
public val name: String,
5555
public val dataType: KType,
5656
public val returnType: KType,
5757
public val invokator: RpcInvokator<T>,
58-
public val parameters: List<RpcParameter>,
58+
public val parameters: Array<RpcParameter>,
5959
)
6060

6161
@ExperimentalRPCApi
6262
public sealed interface RpcInvokator<T : RemoteService> {
6363
@ExperimentalRPCApi
64-
public interface Method<T : RemoteService> : RpcInvokator<T> {
64+
public fun interface Method<T : RemoteService> : RpcInvokator<T> {
6565
public suspend fun call(service: T, data: Any?): Any?
6666
}
6767

6868
@ExperimentalRPCApi
69-
public interface Field<T : RemoteService> : RpcInvokator<T> {
69+
public fun interface Field<T : RemoteService> : RpcInvokator<T> {
7070
public fun call(service: T): Any?
7171
}
7272
}
7373

7474
@ExperimentalRPCApi
75-
public data class RpcParameter(val name: String, val type: KType)
75+
public class RpcParameter(public val name: String, public val type: KType)
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
package kotlinx.rpc.krpc.client.internal
5+
package kotlinx.rpc.internal
66

7+
import kotlinx.rpc.internal.utils.InternalRPCApi
78
import kotlinx.serialization.Serializable
89

910
/**
1011
* Used for field initialization call
1112
*/
1213
@Serializable
13-
internal object FieldDataObject
14+
@InternalRPCApi
15+
public object FieldDataObject

core/src/commonMain/kotlin/kotlinx/rpc/internal/RPCDeferredField.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ package kotlinx.rpc.internal
77
import kotlinx.rpc.internal.utils.InternalRPCApi
88

99
@InternalRPCApi
10-
public interface RPCDeferredField<Self> {
10+
public interface RpcDeferredField<Self> {
1111
public suspend fun await(): Self
1212
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
package kotlinx.rpc.krpc.client.internal
5+
package kotlinx.rpc.internal
66

77
import kotlinx.coroutines.CompletableDeferred
8+
import kotlinx.coroutines.Deferred
89
import kotlinx.coroutines.ExperimentalCoroutinesApi
910
import kotlinx.rpc.UninitializedRPCFieldException
1011
import kotlin.reflect.KProperty
1112

12-
internal class RPCFieldProvider<T, R>(
13+
internal class RpcFieldProvider<T, R>(
1314
private val serviceName: String,
14-
private val deferred: CompletableDeferred<T> = CompletableDeferred(),
15+
private val deferred: Deferred<T> = CompletableDeferred(),
1516
val getter: T.() -> R,
1617
) {
1718
@OptIn(ExperimentalCoroutinesApi::class)
@@ -25,9 +26,9 @@ internal class RPCFieldProvider<T, R>(
2526
}
2627

2728
@Suppress("unused")
28-
internal fun <T> RPCFieldProvider(
29+
internal fun <T> RpcFieldProvider(
2930
serviceName: String,
3031
deferred: CompletableDeferred<T> = CompletableDeferred()
31-
): RPCFieldProvider<T, T> {
32-
return RPCFieldProvider(serviceName, deferred) { this }
32+
): RpcFieldProvider<T, T> {
33+
return RpcFieldProvider(serviceName, deferred) { this }
3334
}

0 commit comments

Comments
 (0)