Skip to content

Commit 18d7049

Browse files
committed
gRPC/Common - Client/Server + Jvm Impl
1 parent 4db9cdd commit 18d7049

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1520
-170
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright 2023-2025 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.grpc
6+
7+
@Suppress("RedundantConstructorKeyword")
8+
public expect class GrpcTrailers constructor() {
9+
public fun merge(trailers: GrpcTrailers)
10+
}

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/ManagedChannel.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
package kotlinx.rpc.grpc
88

9+
import kotlinx.rpc.grpc.internal.GrpcChannel
910
import kotlin.time.Duration
1011

1112
/**
1213
* Same as [ManagedChannel], but is platform-exposed.
1314
*/
14-
public expect abstract class ManagedChannelPlatform
15+
public expect abstract class ManagedChannelPlatform : GrpcChannel
1516

1617
/**
1718
* A virtual connection to a conceptual endpoint, to perform RPCs.
@@ -66,7 +67,9 @@ public interface ManagedChannel {
6667
/**
6768
* Builder class for [ManagedChannel].
6869
*/
69-
public expect abstract class ManagedChannelBuilder<T : ManagedChannelBuilder<T>>
70+
public expect abstract class ManagedChannelBuilder<T : ManagedChannelBuilder<T>> {
71+
public fun usePlaintext(): T
72+
}
7073

7174
internal expect fun ManagedChannelBuilder(hostname: String, port: Int): ManagedChannelBuilder<*>
7275
internal expect fun ManagedChannelBuilder(target: String): ManagedChannelBuilder<*>

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/ServerServiceDefinition.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@
66

77
package kotlinx.rpc.grpc
88

9+
import kotlinx.rpc.grpc.internal.ServerMethodDefinition
10+
import kotlinx.rpc.grpc.internal.ServiceDescriptor
11+
import kotlinx.rpc.internal.utils.InternalRpcApi
12+
913
/**
1014
* Definition of a service to be exposed via a Server.
1115
*/
12-
public expect class ServerServiceDefinition
16+
public expect class ServerServiceDefinition {
17+
public fun getServiceDescriptor(): ServiceDescriptor
18+
public fun getMethods(): Collection<ServerMethodDefinition<*, *>>
19+
}
20+
21+
@InternalRpcApi
22+
public expect fun serverServiceDefinition(
23+
serviceDescriptor: ServiceDescriptor,
24+
methods: Collection<ServerMethodDefinition<*, *>>
25+
): ServerServiceDefinition

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/Status.kt

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
package kotlinx.rpc.grpc
88

99
/**
10-
* Defines the status of an operation by providing a standard [Code] in conjunction with an
10+
* Defines the status of an operation by providing a standard [StatusCode] in conjunction with an
1111
* optional descriptive message.
1212
*
1313
* For clients, every remote call will return a status on completion.
1414
* In the case of errors this
1515
* status may be propagated to blocking stubs as a [RuntimeException] or to a listener as an
1616
* explicit parameter.
1717
*
18-
* Similarly, servers can report a status by throwing [StatusRuntimeException]
18+
* Similarly, servers can report a status by throwing [StatusException]
1919
* or by passing the status to a callback.
2020
*
2121
* Utility functions are provided to convert a status to an exception and to extract them
@@ -25,30 +25,33 @@ package kotlinx.rpc.grpc
2525
* can be found at
2626
* [doc/statuscodes.md](https://github.com/grpc/grpc/blob/master/doc/statuscodes.md)
2727
*/
28-
public interface Status {
29-
public val code: Code
30-
public val description: String?
31-
public val cause: Throwable?
28+
public expect class Status {
29+
public fun getDescription(): String?
30+
public fun getCause(): Throwable?
31+
}
32+
33+
public expect fun Status(code: StatusCode, description: String? = null, cause: Throwable? = null): Status
34+
35+
public expect val Status.code: StatusCode
3236

33-
public enum class Code(public val value: Int) {
34-
OK(0),
35-
CANCELLED(1),
36-
UNKNOWN(2),
37-
INVALID_ARGUMENT(3),
38-
DEADLINE_EXCEEDED(4),
39-
NOT_FOUND(5),
40-
ALREADY_EXISTS(6),
41-
PERMISSION_DENIED(7),
42-
RESOURCE_EXHAUSTED(8),
43-
FAILED_PRECONDITION(9),
44-
ABORTED(10),
45-
OUT_OF_RANGE(11),
46-
UNIMPLEMENTED(12),
47-
INTERNAL(13),
48-
UNAVAILABLE(14),
49-
DATA_LOSS(15),
50-
UNAUTHENTICATED(16);
37+
public enum class StatusCode(public val value: Int) {
38+
OK(0),
39+
CANCELLED(1),
40+
UNKNOWN(2),
41+
INVALID_ARGUMENT(3),
42+
DEADLINE_EXCEEDED(4),
43+
NOT_FOUND(5),
44+
ALREADY_EXISTS(6),
45+
PERMISSION_DENIED(7),
46+
RESOURCE_EXHAUSTED(8),
47+
FAILED_PRECONDITION(9),
48+
ABORTED(10),
49+
OUT_OF_RANGE(11),
50+
UNIMPLEMENTED(12),
51+
INTERNAL(13),
52+
UNAVAILABLE(14),
53+
DATA_LOSS(15),
54+
UNAUTHENTICATED(16);
5155

52-
public val valueAscii: ByteArray = value.toString().encodeToByteArray()
53-
}
56+
public val valueAscii: ByteArray = value.toString().encodeToByteArray()
5457
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2023-2025 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.grpc
6+
7+
/**
8+
* [Status] in Exception form, for propagating Status information via exceptions.
9+
*/
10+
public expect class StatusException : Exception {
11+
public constructor(status: Status)
12+
public constructor(status: Status, trailers: GrpcTrailers?)
13+
14+
public fun getStatus(): Status
15+
public fun getTrailers(): GrpcTrailers?
16+
}
17+
18+
public expect class StatusRuntimeException : RuntimeException {
19+
public constructor(status: Status)
20+
public constructor(status: Status, trailers: GrpcTrailers?)
21+
22+
public fun getStatus(): Status
23+
public fun getTrailers(): GrpcTrailers?
24+
}

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/StatusRuntimeException.kt

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2023-2025 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.grpc.internal
6+
7+
import kotlinx.rpc.grpc.GrpcTrailers
8+
import kotlinx.rpc.grpc.Status
9+
import kotlinx.rpc.internal.utils.InternalRpcApi
10+
11+
@InternalRpcApi
12+
public expect abstract class ClientCall<Request, Response> {
13+
@InternalRpcApi
14+
public abstract class Listener<Message> {
15+
public open fun onHeaders(headers: GrpcTrailers)
16+
public open fun onMessage(message: Message)
17+
public open fun onClose(status: Status, trailers: GrpcTrailers)
18+
public open fun onReady()
19+
}
20+
21+
public abstract fun start(responseListener: Listener<Response>, headers: GrpcTrailers)
22+
public abstract fun request(numMessages: Int)
23+
public abstract fun cancel(message: String?, cause: Throwable?)
24+
public abstract fun halfClose()
25+
public abstract fun sendMessage(message: Request)
26+
public open fun isReady(): Boolean
27+
}
28+
29+
@InternalRpcApi
30+
public expect fun <Message> clientCallListener(
31+
onHeaders: (headers: GrpcTrailers) -> Unit,
32+
onMessage: (message: Message) -> Unit,
33+
onClose: (status: Status, trailers: GrpcTrailers) -> Unit,
34+
onReady: () -> Unit,
35+
): ClientCall.Listener<Message>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2023-2025 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.grpc.internal
6+
7+
import kotlinx.rpc.internal.utils.InternalRpcApi
8+
9+
@InternalRpcApi
10+
public expect class GrpcCallOptions
11+
12+
@InternalRpcApi
13+
public expect val GrpcDefaultCallOptions: GrpcCallOptions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright 2023-2025 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.grpc.internal
6+
7+
import kotlinx.rpc.internal.utils.InternalRpcApi
8+
9+
@InternalRpcApi
10+
public expect abstract class GrpcChannel {
11+
public abstract fun <RequestT, ResponseT> newCall(
12+
methodDescriptor: MethodDescriptor<RequestT, ResponseT>,
13+
callOptions: GrpcCallOptions,
14+
): ClientCall<RequestT, ResponseT>
15+
16+
public abstract fun authority(): String
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2023-2025 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.grpc.internal
6+
7+
import kotlin.coroutines.CoroutineContext
8+
9+
internal expect class GrpcContext
10+
internal expect val CurrentGrpcContext: GrpcContext
11+
12+
internal expect class GrpcContextElement : CoroutineContext.Element {
13+
companion object Key : CoroutineContext.Key<GrpcContextElement> {
14+
fun current(): GrpcContextElement
15+
}
16+
17+
override val key: CoroutineContext.Key<GrpcContextElement>
18+
}

0 commit comments

Comments
 (0)