-
Notifications
You must be signed in to change notification settings - Fork 40
grpc-native: Intial setup with minimal C++/C/Kotlin interface #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4d8bc92
grpc-native: Add grpcpp-c C/C++ library to grpc/
Jozott00 16c76c3
grpc-native: Add gradle configuration for cInterop and grpcpp_c library
Jozott00 7f76590
grpc-native: Add basic C Kotlin wrapper classes
Jozott00 c60a41d
grpc-native: Update platforms.topic
Jozott00 0f86094
grpc-native: Resolve review requests
Jozott00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
headers = grpcpp_c.h | ||
headerFilter= grpcpp_c.h grpc/slice.h grpc/byte_buffer.h | ||
|
||
noStringConversion = grpc_slice_from_copied_buffer my_grpc_slice_from_copied_buffer | ||
|
||
staticLibraries = libgrpcpp_c_static.a |
31 changes: 31 additions & 0 deletions
31
grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/bridge/GrpcByteBuffer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.rpc.grpc.bridge | ||
|
||
import kotlinx.cinterop.* | ||
import libgrpcpp_c.* | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
internal class GrpcByteBuffer internal constructor( | ||
internal val cByteBuffer: CPointer<grpc_byte_buffer> | ||
): AutoCloseable { | ||
|
||
constructor(slice: GrpcSlice): this(memScoped { | ||
grpc_raw_byte_buffer_create(slice.cSlice, 1u) ?: error("Failed to create byte buffer") | ||
}) | ||
|
||
fun intoSlice(): GrpcSlice { | ||
memScoped { | ||
val resp_slice = alloc<grpc_slice>() | ||
grpc_byte_buffer_dump_to_single_slice(cByteBuffer, resp_slice.ptr) | ||
return GrpcSlice(resp_slice.readValue()) | ||
} | ||
} | ||
|
||
override fun close() { | ||
grpc_byte_buffer_destroy(cByteBuffer) | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/bridge/GrpcClient.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.rpc.grpc.bridge | ||
|
||
import kotlinx.cinterop.* | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
import libgrpcpp_c.* | ||
|
||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
internal class GrpcClient(target: String): AutoCloseable { | ||
private var clientPtr: CPointer<grpc_client_t> = grpc_client_create_insecure(target) ?: error("Failed to create client") | ||
|
||
fun callUnaryBlocking(method: String, req: GrpcSlice): GrpcSlice { | ||
memScoped { | ||
val result = alloc<grpc_slice>() | ||
grpc_client_call_unary_blocking(clientPtr, method, req.cSlice, result.ptr) | ||
return GrpcSlice(result.readValue()) | ||
} | ||
} | ||
|
||
suspend fun callUnary(method: String, req: GrpcByteBuffer): GrpcByteBuffer = suspendCancellableCoroutine { continuation -> | ||
val context = grpc_context_create() | ||
val method = grpc_method_create(method) | ||
|
||
val req_raw_buf = nativeHeap.alloc<CPointerVar<grpc_byte_buffer>>() | ||
req_raw_buf.value = req.cByteBuffer | ||
|
||
val resp_raw_buf: CPointerVar<grpc_byte_buffer> = nativeHeap.alloc() | ||
|
||
val continueCb = { st: grpc_status_code_t -> | ||
// cleanup allocations owned by this method (this runs always) | ||
grpc_method_delete(method) | ||
grpc_context_delete(context) | ||
nativeHeap.free(req_raw_buf) | ||
|
||
if (st != GRPC_C_STATUS_OK) { | ||
continuation.resumeWithException(RuntimeException("Call failed with code: $st")) | ||
} else { | ||
val result = resp_raw_buf.value | ||
if (result == null) { | ||
continuation.resumeWithException(RuntimeException("No response received")) | ||
} else { | ||
continuation.resume(GrpcByteBuffer(result)) | ||
} | ||
} | ||
|
||
nativeHeap.free(resp_raw_buf) | ||
} | ||
val cbCtxStable = StableRef.create(continueCb) | ||
|
||
grpc_client_call_unary_callback(clientPtr, method, context, req_raw_buf.ptr, resp_raw_buf.ptr, cbCtxStable.asCPointer(), staticCFunction { st, ctx -> | ||
val cbCtxStable = ctx!!.asStableRef<(grpc_status_code_t) -> Unit>() | ||
cbCtxStable.get()(st) | ||
cbCtxStable.dispose() | ||
}) | ||
} | ||
|
||
override fun close() { | ||
grpc_client_delete(clientPtr) | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/bridge/GrpcSlice.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.rpc.grpc.bridge | ||
|
||
import kotlinx.cinterop.CValue | ||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import kotlinx.cinterop.addressOf | ||
import kotlinx.cinterop.usePinned | ||
import libgrpcpp_c.grpc_slice | ||
import libgrpcpp_c.grpc_slice_from_copied_buffer | ||
import libgrpcpp_c.grpc_slice_unref | ||
|
||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
internal class GrpcSlice internal constructor(internal val cSlice: CValue<grpc_slice>) : AutoCloseable { | ||
|
||
constructor(buffer: ByteArray) : this( | ||
buffer.usePinned { pinned -> | ||
grpc_slice_from_copied_buffer(pinned.addressOf(0), buffer.size.toULong()) | ||
} | ||
) | ||
|
||
override fun close() { | ||
grpc_slice_unref(cSlice) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
grpc/grpc-core/src/nativeTest/kotlin/kotlinx/rpc/grpc/BridgeTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.rpc.grpc | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.rpc.grpc.bridge.GrpcByteBuffer | ||
import kotlinx.rpc.grpc.bridge.GrpcClient | ||
import kotlinx.rpc.grpc.bridge.GrpcSlice | ||
import kotlin.test.Test | ||
import libgrpcpp_c.* | ||
|
||
@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) | ||
class BridgeTest { | ||
|
||
@Test | ||
fun `test basic unary async call`() { | ||
runBlocking { | ||
GrpcClient("localhost:50051").use { client -> | ||
GrpcSlice(byteArrayOf(8, 4)).use { request -> | ||
GrpcByteBuffer(request).use { req_buf -> | ||
client.callUnary("/Greeter/SayHello", req_buf) | ||
.use { result -> | ||
result.intoSlice().use { response -> | ||
val value = pb_decode_greeter_sayhello_response(response.cSlice) | ||
println("Response received: $value") | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# we build the cc_static library bundled with all dependencies | ||
build --experimental_cc_static_library | ||
|
||
build:release --compilation_mode=opt --strip=always |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# gitignore template for Bazel build system | ||
# website: https://bazel.build/ | ||
|
||
# Ignore all bazel-* symlinks. There is no full list since this can change | ||
# based on the name of the directory bazel is cloned into. | ||
/bazel-* | ||
|
||
# Directories for the Bazel IntelliJ plugin containing the generated | ||
# IntelliJ project files and plugin configuration. Separate directories are | ||
# for the IntelliJ, Android Studio and CLion versions of the plugin. | ||
/.ijwb/ | ||
/.aswb/ | ||
/.clwb/ | ||
.idea/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
load("@rules_cc//cc:defs.bzl", "cc_library") | ||
|
||
cc_library( | ||
name = "grpcpp_c", | ||
srcs = ["src/grpcpp_c.cpp"], | ||
hdrs = glob(["include/**/*.h"]), | ||
copts = ["-std=c++20"], | ||
includes = ["include"], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@com_github_grpc_grpc//:grpc++", | ||
"@com_google_protobuf//:protobuf", | ||
], | ||
) | ||
|
||
cc_static_library( | ||
name = "grpcpp_c_static", | ||
deps = [ | ||
"grpcpp_c", | ||
], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module( | ||
name = "grpcpp_c", | ||
version = "0.1", | ||
) | ||
|
||
# rules_cc for cc_library support | ||
bazel_dep( | ||
name = "rules_cc", | ||
version = "0.1.1", | ||
) | ||
|
||
# Protobuf | ||
bazel_dep( | ||
name = "protobuf", | ||
version = "31.1", | ||
repo_name = "com_google_protobuf", | ||
) | ||
|
||
# gRPC C++ library | ||
bazel_dep( | ||
name = "grpc", | ||
version = "1.73.1", | ||
repo_name = "com_github_grpc_grpc", | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.