Skip to content

Commit 67a968f

Browse files
committed
grpc-native: Remove unnecessary C header definitions
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 2bf610e commit 67a968f

File tree

10 files changed

+26
-507
lines changed

10 files changed

+26
-507
lines changed

cinterop-c/include/grpcpp_c.h

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,35 @@
1-
//
2-
// Created by Johannes Zottele on 11.07.25.
3-
//
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+
/*
6+
* Helper functions required for gRPC Core cinterop.
7+
*/
48

59
#ifndef GRPCPP_C_H
610
#define GRPCPP_C_H
711

8-
#include <stdint.h>
912
#include <stdbool.h>
1013
#include <grpc/grpc.h>
11-
#include <grpc/slice.h>
12-
#include <grpc/byte_buffer.h>
1314

1415
#ifdef __cplusplus
1516
extern "C" {
1617
#endif
1718

18-
typedef struct grpc_client grpc_client_t;
19-
typedef struct grpc_method grpc_method_t;
20-
typedef struct grpc_context grpc_context_t;
21-
22-
typedef enum StatusCode {
23-
GRPC_C_STATUS_OK = 0,
24-
GRPC_C_STATUS_CANCELLED = 1,
25-
GRPC_C_STATUS_UNKNOWN = 2,
26-
GRPC_C_STATUS_INVALID_ARGUMENT = 3,
27-
GRPC_C_STATUS_DEADLINE_EXCEEDED = 4,
28-
GRPC_C_STATUS_NOT_FOUND = 5,
29-
GRPC_C_STATUS_ALREADY_EXISTS = 6,
30-
GRPC_C_STATUS_PERMISSION_DENIED = 7,
31-
GRPC_C_STATUS_UNAUTHENTICATED = 16,
32-
GRPC_C_STATUS_RESOURCE_EXHAUSTED = 8,
33-
GRPC_C_STATUS_FAILED_PRECONDITION = 9,
34-
GRPC_C_STATUS_ABORTED = 10,
35-
GRPC_C_STATUS_OUT_OF_RANGE = 11,
36-
GRPC_C_STATUS_UNIMPLEMENTED = 12,
37-
GRPC_C_STATUS_INTERNAL = 13,
38-
GRPC_C_STATUS_UNAVAILABLE = 14,
39-
GRPC_C_STATUS_DATA_LOSS = 15,
40-
GRPC_C_STATUS_DO_NOT_USE = -1
41-
} grpc_status_code_t;
42-
43-
19+
/*
20+
* Struct that layouts a grpc_completion_queue_functor and user opaque data pointer,
21+
* to implement the callback mechanism in the K/N CompletionQueue.
22+
*/
4423
typedef struct {
4524
grpc_completion_queue_functor functor;
4625
void *user_data;
4726
} grpc_cb_tag;
4827

49-
50-
grpc_client_t *grpc_client_create_insecure(const char *target);
51-
void grpc_client_delete(const grpc_client_t *client);
52-
53-
grpc_method_t *grpc_method_create(const char *method_name);
54-
void grpc_method_delete(const grpc_method_t *method);
55-
56-
const char *grpc_method_name(const grpc_method_t *method);
57-
58-
grpc_context_t *grpc_context_create();
59-
void grpc_context_delete(const grpc_context_t *context);
60-
61-
grpc_status_code_t grpc_client_call_unary_blocking(grpc_client_t *client, const char *method,
62-
grpc_slice req_slice, grpc_slice *resp_slice);
63-
64-
void grpc_client_call_unary_callback(grpc_client_t *client, grpc_method_t *method, grpc_context_t *context,
65-
grpc_byte_buffer **req_buf, grpc_byte_buffer **resp_buf, void* callback_context, void (*callback)(grpc_status_code_t,void*));
66-
67-
uint32_t pb_decode_greeter_sayhello_response(grpc_slice response);
68-
69-
grpc_status_code_t grpc_byte_buffer_dump_to_single_slice(grpc_byte_buffer *byte_buffer, grpc_slice *slice);
70-
71-
72-
/////// CHANNEL ///////
73-
74-
typedef struct grpc_channel grpc_channel_t;
75-
typedef struct grpc_channel_credentials grpc_channel_credentials_t;
76-
28+
/*
29+
* Call to grpc_iomgr_run_in_background(), which is not exposed as extern "C" and therefore must be wrapped.
30+
*/
7731
bool kgrpc_iomgr_run_in_background();
7832

79-
8033
#ifdef __cplusplus
8134
}
8235
#endif

cinterop-c/include/protowire.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
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+
15
#ifndef PROTOWIRE_H
26
#define PROTOWIRE_H
37

cinterop-c/src/grpcpp_c.cpp

Lines changed: 3 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -1,214 +1,17 @@
1-
//
2-
// Created by Johannes Zottele on 11.07.25.
3-
//
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+
*/
44

55
#include <grpcpp_c.h>
66

7-
#include <memory>
8-
#include <grpcpp/grpcpp.h>
9-
#include <grpcpp/generic/generic_stub.h>
10-
#include <grpcpp/impl/client_unary_call.h>
11-
#include <google/protobuf/io/coded_stream.h>
12-
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
137
#include "src/core/lib/iomgr/iomgr.h"
148

15-
namespace pb = google::protobuf;
16-
17-
struct grpc_client {
18-
std::shared_ptr<grpc::Channel> channel;
19-
std::unique_ptr<grpc::GenericStub> stub;
20-
};
21-
22-
struct grpc_method {
23-
std::string name_str;
24-
std::unique_ptr<grpc::internal::RpcMethod> method;
25-
};
26-
27-
struct grpc_context {
28-
std::unique_ptr<grpc::ClientContext> context;
29-
};
30-
31-
// struct grpc_channel {
32-
// std::shared_ptr<grpc::Channel> channel;
33-
// };
34-
359
extern "C" {
3610

37-
grpc_client_t *grpc_client_create_insecure(const char *target) {
38-
std::string target_str = target;
39-
auto client = new grpc_client;
40-
client->channel = grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials());
41-
client->stub = std::make_unique<grpc::GenericStub>(client->channel);
42-
return client;
43-
}
44-
45-
void grpc_client_delete(const grpc_client_t *client) {
46-
delete client;
47-
}
48-
49-
grpc_method_t *grpc_method_create(const char *method_name) {
50-
auto *method = new grpc_method;
51-
method->name_str = method_name;
52-
method->method = std::make_unique<grpc::internal::RpcMethod>(method->name_str.c_str(), grpc::internal::RpcMethod::NORMAL_RPC);
53-
return method;
54-
}
55-
56-
void grpc_method_delete(const grpc_method_t *method) {
57-
delete method;
58-
}
59-
60-
const char *grpc_method_name(const grpc_method_t *method) {
61-
return method->method->name();
62-
}
63-
64-
grpc_context_t *grpc_context_create() {
65-
auto *context = new grpc_context;
66-
context->context = std::make_unique<grpc::ClientContext>();
67-
return context;
68-
}
69-
70-
void grpc_context_delete(const grpc_context_t *context) {
71-
delete context;
72-
}
73-
74-
static grpc_status_code_t status_to_c(grpc::StatusCode status);
75-
76-
grpc_status_code_t grpc_client_call_unary_blocking(grpc_client_t *client, const char *method,
77-
grpc_slice req_slice, grpc_slice *resp_slice) {
78-
79-
if (!client || !method) return GRPC_C_STATUS_INVALID_ARGUMENT;
80-
81-
grpc::Slice cc_req_slice(req_slice, grpc::Slice::ADD_REF);
82-
grpc::ByteBuffer req_bb(&cc_req_slice, 1);
83-
84-
grpc::ClientContext context;
85-
grpc::ByteBuffer resp_bb;
86-
87-
const std::string method_path = "/Greeter/SayHello";
88-
grpc::internal::RpcMethod rpc(method_path.c_str(),
89-
grpc::internal::RpcMethod::NORMAL_RPC);
90-
91-
grpc::Status st =
92-
grpc::internal::BlockingUnaryCall<grpc::ByteBuffer, grpc::ByteBuffer>(
93-
client->channel.get(), rpc, &context, req_bb, &resp_bb);
94-
95-
96-
if (!st.ok()) {
97-
// if not ok, no resp_buf is left null
98-
return status_to_c(st.error_code());
99-
}
100-
101-
grpc::Slice cc_resp_slice;
102-
resp_bb.DumpToSingleSlice(&cc_resp_slice);
103-
*resp_slice = cc_resp_slice.c_slice();
104-
105-
grpc::Slice test_slice(*resp_slice, grpc::Slice::ADD_REF);
106-
pb::io::ArrayInputStream ais(test_slice.begin(), test_slice.size());
107-
pb::io::CodedInputStream cis(&ais);
108-
109-
110-
cis.ReadTag();
111-
uint32_t id = 0;
112-
if (!cis.ReadVarint32(&id)) {
113-
std::cerr << "Failed to read id field\n";
114-
}
115-
116-
return status_to_c(st.error_code());
117-
}
118-
119-
void grpc_client_call_unary_callback(grpc_client_t *client, grpc_method_t *method, grpc_context_t *context,
120-
grpc_byte_buffer **req_buf, grpc_byte_buffer **resp_buf, void* callback_context, void (*callback)(grpc_status_code_t,void*)) {
121-
// the grpc::ByteBuffer representation is identical to (* grpc_byte_buffer) so we can safely cast it.
122-
// so a **grpc_byte_buffer can be cast to *grpc::ByteBuffer.
123-
static_assert(sizeof(grpc::ByteBuffer) == sizeof(grpc_byte_buffer*),
124-
"ByteBuffer must have same representation as "
125-
"grpc_byte_buffer*");
126-
const auto req_bb = reinterpret_cast<grpc::ByteBuffer *>(req_buf);
127-
const auto resp_bb = reinterpret_cast<grpc::ByteBuffer *>(resp_buf);
128-
grpc::internal::CallbackUnaryCall<grpc::ByteBuffer, grpc::ByteBuffer>(client->channel.get(), *method->method, context->context.get(), req_bb, resp_bb, [callback, callback_context](grpc::Status st) {
129-
const auto c_st = status_to_c(st.error_code());
130-
callback(c_st, callback_context);
131-
});
132-
}
133-
134-
grpc_status_code_t status_to_c(grpc::StatusCode status) {
135-
switch (status) {
136-
case grpc::OK:
137-
return GRPC_C_STATUS_OK;
138-
case grpc::CANCELLED:
139-
return GRPC_C_STATUS_CANCELLED;
140-
case grpc::UNKNOWN:
141-
return GRPC_C_STATUS_UNKNOWN;
142-
case grpc::INVALID_ARGUMENT:
143-
return GRPC_C_STATUS_INVALID_ARGUMENT;
144-
case grpc::DEADLINE_EXCEEDED:
145-
return GRPC_C_STATUS_DEADLINE_EXCEEDED;
146-
case grpc::NOT_FOUND:
147-
return GRPC_C_STATUS_NOT_FOUND;
148-
case grpc::ALREADY_EXISTS:
149-
return GRPC_C_STATUS_ALREADY_EXISTS;
150-
case grpc::PERMISSION_DENIED:
151-
return GRPC_C_STATUS_PERMISSION_DENIED;
152-
case grpc::UNAUTHENTICATED:
153-
return GRPC_C_STATUS_UNAUTHENTICATED;
154-
case grpc::RESOURCE_EXHAUSTED:
155-
return GRPC_C_STATUS_RESOURCE_EXHAUSTED;
156-
case grpc::FAILED_PRECONDITION:
157-
return GRPC_C_STATUS_FAILED_PRECONDITION;
158-
case grpc::ABORTED:
159-
return GRPC_C_STATUS_ABORTED;
160-
case grpc::UNIMPLEMENTED:
161-
return GRPC_C_STATUS_UNIMPLEMENTED;
162-
case grpc::OUT_OF_RANGE:
163-
return GRPC_C_STATUS_OUT_OF_RANGE;
164-
case grpc::INTERNAL:
165-
return GRPC_C_STATUS_INTERNAL;
166-
case grpc::UNAVAILABLE:
167-
return GRPC_C_STATUS_UNAVAILABLE;
168-
case grpc::DATA_LOSS:
169-
return GRPC_C_STATUS_DATA_LOSS;
170-
case grpc::DO_NOT_USE:
171-
return GRPC_C_STATUS_DO_NOT_USE;
172-
}
173-
}
174-
175-
176-
uint32_t pb_decode_greeter_sayhello_response(grpc_slice response) {
177-
grpc::Slice cc_resp_slice(response, grpc::Slice::ADD_REF);
178-
pb::io::ArrayInputStream asi(cc_resp_slice.begin(), cc_resp_slice.size());
179-
pb::io::CodedInputStream cis(&asi);
180-
181-
const auto tag = cis.ReadTag();
182-
if (tag != 8) {
183-
std::cerr << "Failed to read tag. Got: " << tag << std::endl;
184-
}
185-
186-
uint32_t result;
187-
if (!cis.ReadVarint32(&result)) {
188-
std::cerr << "Failed to read result" << std::endl;
189-
} else {
190-
191-
}
192-
return result;
193-
}
194-
195-
196-
grpc_status_code_t grpc_byte_buffer_dump_to_single_slice(grpc_byte_buffer *byte_buffer, grpc_slice *slice) {
197-
auto bb = reinterpret_cast<grpc::ByteBuffer*>(&byte_buffer);
198-
grpc::Slice cc_slice;
199-
bb->DumpToSingleSlice(&cc_slice);
200-
*slice = cc_slice.c_slice();
201-
return GRPC_C_STATUS_OK;
202-
}
203-
204-
205-
//// CHANNEL ////
206-
20711
bool kgrpc_iomgr_run_in_background() {
20812
return grpc_iomgr_run_in_background();
20913
}
21014

211-
21215
}
21316

21417

cinterop-c/src/protowire.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//
2-
// Created by Johannes Zottele on 17.07.25.
3-
//
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+
*/
44

55
#include "protowire.h"
66

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/NativeManagedChannel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package kotlinx.rpc.grpc.internal
88

99
import cnames.structs.grpc_channel
10+
import cnames.structs.grpc_channel_credentials
1011
import kotlinx.cinterop.CPointer
1112
import kotlinx.cinterop.ExperimentalForeignApi
1213
import kotlinx.coroutines.*
@@ -19,7 +20,7 @@ import kotlin.native.ref.createCleaner
1920
import kotlin.time.Duration
2021

2122
internal sealed class GrpcCredentials(
22-
internal val raw: CPointer<grpc_channel_credentials_t>,
23+
internal val raw: CPointer<grpc_channel_credentials>,
2324
) {
2425
val rawCleaner = createCleaner(raw) {
2526
grpc_channel_credentials_release(it)

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/bridge/GrpcByteBuffer.kt

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

0 commit comments

Comments
 (0)