0%

C语言实现RPC调用(Demo)

简单调研一圈,目前c语言可用的rpc框架有,Thrift,protobuf-c-rpc,rpcgen,这里只是简单记录一下调用demo,C语言用的还是不太熟悉,凑合看了。

Thrift

这里说一下Thrift的特点

  • 同步调用,相对比较容易处理
  • 需要修改request_service.c,也就是服务实现具体的方法,需要修改request_service.c文件(暂时没找到其他办法)
  • 依赖glibc

编译安装Thrift

1
2
3
4
5
6
# git clone https://github.com/apache/thrift.git
# cd thrift
# ./bootstrap.sh
# ./configure
# make
# make install

依赖

  • glibc-2.0

编写协议

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace cl shared


struct Param {
1: string key
2: i32 value
}

struct Result {
1: bool result
}

service RequestService {
Result sendMessage(1: Param param)
}

Server端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <glib-object.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>

#include <thrift/c_glib/thrift.h>
#include <thrift/c_glib/protocol/thrift_binary_protocol_factory.h>
#include <thrift/c_glib/protocol/thrift_protocol_factory.h>
#include <thrift/c_glib/server/thrift_server.h>
#include <thrift/c_glib/server/thrift_simple_server.h>
#include <thrift/c_glib/transport/thrift_buffered_transport_factory.h>
#include <thrift/c_glib/transport/thrift_server_socket.h>
#include <thrift/c_glib/transport/thrift_server_transport.h>

#include "gen-c_glib/request_service.h"




ThriftServer *server = NULL;
gboolean sigint_received = FALSE;

/* Handle SIGINT ("Ctrl-C") signals by gracefully stopping the
server */
static void
sigint_handler(int signal_number) {
THRIFT_UNUSED_VAR (signal_number);

/* Take note we were called */
sigint_received = TRUE;

/* Shut down the server gracefully */
if (server != NULL)
thrift_server_stop(server);
}

int main(void) {
RequestServiceHandler *handler;
RequestServiceProcessor *processor;
ThriftServerTransport *server_transport;
ThriftTransportFactory *transport_factory;
ThriftProtocolFactory *protocol_factory;

struct sigaction sigint_action;

GError *error = NULL;
int exit_status = 0;

handler = g_object_new(TYPE_REQUEST_SERVICE_HANDLER, NULL);
processor = g_object_new(TYPE_REQUEST_SERVICE_PROCESSOR, "handler", handler, NULL);
server_transport = g_object_new(THRIFT_TYPE_SERVER_SOCKET, "port", 9090, NULL);
transport_factory = g_object_new(THRIFT_TYPE_BUFFERED_TRANSPORT_FACTORY, NULL);
protocol_factory = g_object_new(THRIFT_TYPE_BINARY_PROTOCOL_FACTORY, NULL);
server = g_object_new(THRIFT_TYPE_SIMPLE_SERVER,
"processor", processor,
"server_transport", server_transport,
"input_transport_factory", transport_factory,
"output_transport_factory", transport_factory,
"input_protocol_factory", protocol_factory,
"output_protocol_factory", protocol_factory,
NULL);
memset(&sigint_action, 0, sizeof(sigint_action));
sigint_action.sa_handler = sigint_handler;
sigint_action.sa_flags = SA_RESETHAND;
sigaction(SIGINT, &sigint_action, NULL);
printf("Starting the server...\n");
thrift_server_serve(server, &error);
printf("server %p error %p\n",server,error);
if (!sigint_received) {
g_message ("thrift_server_serve: %s",
error != NULL ? error->message : "(null)");
g_clear_error(&error);
}

puts("done.");

g_object_unref(server);
g_object_unref(transport_factory);
g_object_unref(protocol_factory);
g_object_unref(server_transport);

g_object_unref(processor);
g_object_unref(handler);

return exit_status;
}

Server业务部分(request_service.c)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/**
* Autogenerated by Thrift Compiler (0.17.0)
*
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
* @generated
*/
#include <string.h>
#include <thrift/c_glib/thrift.h>
#include <thrift/c_glib/thrift_application_exception.h>
#include <stdio.h>
#include <stdbool.h>
#include "request_service.h"

gboolean
request_service_if_send_message(RequestServiceIf *iface, Result **_return, const Param *param, GError **error) {
return REQUEST_SERVICE_IF_GET_INTERFACE (iface)->send_message(iface, _return, param, error);
}

GType
request_service_if_get_type(void) {
static GType type = 0;
if (type == 0) {
static const GTypeInfo type_info =
{
sizeof(RequestServiceIfInterface),
NULL, /* base_init */
NULL, /* base_finalize */
NULL, /* class_init */
NULL, /* class_finalize */
NULL, /* class_data */
0, /* instance_size */
0, /* n_preallocs */
NULL, /* instance_init */
NULL /* value_table */
};
type = g_type_register_static(G_TYPE_INTERFACE,
"RequestServiceIf",
&type_info, 0);
}
return type;
}

static void
request_service_if_interface_init(RequestServiceIfInterface *iface);

G_DEFINE_TYPE_WITH_CODE (RequestServiceClient, request_service_client,
G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE(TYPE_REQUEST_SERVICE_IF,
request_service_if_interface_init))

enum _RequestServiceClientProperties {
PROP_0,
PROP_REQUEST_SERVICE_CLIENT_INPUT_PROTOCOL,
PROP_REQUEST_SERVICE_CLIENT_OUTPUT_PROTOCOL
};

void
request_service_client_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) {
RequestServiceClient *client = REQUEST_SERVICE_CLIENT (object);

THRIFT_UNUSED_VAR (pspec);

switch (property_id) {
case PROP_REQUEST_SERVICE_CLIENT_INPUT_PROTOCOL:
client->input_protocol = g_value_get_object(value);
break;
case PROP_REQUEST_SERVICE_CLIENT_OUTPUT_PROTOCOL:
client->output_protocol = g_value_get_object(value);
break;
}
}

void
request_service_client_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec) {
RequestServiceClient *client = REQUEST_SERVICE_CLIENT (object);

THRIFT_UNUSED_VAR (pspec);

switch (property_id) {
case PROP_REQUEST_SERVICE_CLIENT_INPUT_PROTOCOL:
g_value_set_object(value, client->input_protocol);
break;
case PROP_REQUEST_SERVICE_CLIENT_OUTPUT_PROTOCOL:
g_value_set_object(value, client->output_protocol);
break;
}
}

gboolean request_service_client_send_send_message(RequestServiceIf *iface, const Param *param, GError **error) {
gint32 cseqid = 0;
ThriftProtocol *protocol = REQUEST_SERVICE_CLIENT (iface)->output_protocol;

if (thrift_protocol_write_message_begin(protocol, "sendMessage", T_CALL, cseqid, error) < 0)
return FALSE;

{
gint32 ret;
gint32 xfer = 0;


if ((ret = thrift_protocol_write_struct_begin(protocol, "sendMessage_args", error)) < 0)
return 0;
xfer += ret;
if ((ret = thrift_protocol_write_field_begin(protocol, "param", T_STRUCT, 1, error)) < 0)
return 0;
xfer += ret;
if ((ret = thrift_struct_write(THRIFT_STRUCT (param), protocol, error)) < 0)
return 0;
xfer += ret;

if ((ret = thrift_protocol_write_field_end(protocol, error)) < 0)
return 0;
xfer += ret;
if ((ret = thrift_protocol_write_field_stop(protocol, error)) < 0)
return 0;
xfer += ret;
if ((ret = thrift_protocol_write_struct_end(protocol, error)) < 0)
return 0;
xfer += ret;

}

if (thrift_protocol_write_message_end(protocol, error) < 0)
return FALSE;
if (!thrift_transport_flush(protocol->transport, error))
return FALSE;
if (!thrift_transport_write_end(protocol->transport, error))
return FALSE;

return TRUE;
}

gboolean request_service_client_recv_send_message(RequestServiceIf *iface, Result **_return, GError **error) {
gint32 rseqid;
gchar *fname = NULL;
ThriftMessageType mtype;
ThriftProtocol *protocol = REQUEST_SERVICE_CLIENT (iface)->input_protocol;
ThriftApplicationException *xception;

if (thrift_protocol_read_message_begin(protocol, &fname, &mtype, &rseqid, error) < 0) {
if (fname) g_free(fname);
return FALSE;
}

if (mtype == T_EXCEPTION) {
if (fname) g_free(fname);
xception = g_object_new(THRIFT_TYPE_APPLICATION_EXCEPTION, NULL);
thrift_struct_read(THRIFT_STRUCT (xception), protocol, NULL);
thrift_protocol_read_message_end(protocol, NULL);
thrift_transport_read_end(protocol->transport, NULL);
g_set_error(error, THRIFT_APPLICATION_EXCEPTION_ERROR, xception->type, "application error: %s",
xception->message);
g_object_unref(xception);
return FALSE;
} else if (mtype != T_REPLY) {
if (fname) g_free(fname);
thrift_protocol_skip(protocol, T_STRUCT, NULL);
thrift_protocol_read_message_end(protocol, NULL);
thrift_transport_read_end(protocol->transport, NULL);
g_set_error(error, THRIFT_APPLICATION_EXCEPTION_ERROR, THRIFT_APPLICATION_EXCEPTION_ERROR_INVALID_MESSAGE_TYPE,
"invalid message type %d, expected T_REPLY", mtype);
return FALSE;
} else if (strncmp(fname, "sendMessage", 11) != 0) {
thrift_protocol_skip(protocol, T_STRUCT, NULL);
thrift_protocol_read_message_end(protocol, error);
thrift_transport_read_end(protocol->transport, error);
g_set_error(error, THRIFT_APPLICATION_EXCEPTION_ERROR, THRIFT_APPLICATION_EXCEPTION_ERROR_WRONG_METHOD_NAME,
"wrong method name %s, expected sendMessage", fname);
if (fname) g_free(fname);
return FALSE;
}
if (fname) g_free(fname);

{
gint32 ret;
gint32 xfer = 0;
gchar *name = NULL;
ThriftType ftype;
gint16 fid;
guint32 len = 0;
gpointer data = NULL;


/* satisfy -Wall in case these aren't used */
THRIFT_UNUSED_VAR (len);
THRIFT_UNUSED_VAR (data);

/* read the struct begin marker */
if ((ret = thrift_protocol_read_struct_begin(protocol, &name, error)) < 0) {
if (name) g_free(name);
return 0;
}
xfer += ret;
if (name) g_free(name);
name = NULL;

/* read the struct fields */
while (1) {
/* read the beginning of a field */
if ((ret = thrift_protocol_read_field_begin(protocol, &name, &ftype, &fid, error)) < 0) {
if (name) g_free(name);
return 0;
}
xfer += ret;
if (name) g_free(name);
name = NULL;

/* break if we get a STOP field */
if (ftype == T_STOP) {
break;
}

switch (fid) {
case 0:
if (ftype == T_STRUCT) {
if ((ret = thrift_struct_read(THRIFT_STRUCT (*_return), protocol, error)) < 0) {
return 0;
}
xfer += ret;
} else {
if ((ret = thrift_protocol_skip(protocol, ftype, error)) < 0)
return 0;
xfer += ret;
}
break;
default:
if ((ret = thrift_protocol_skip(protocol, ftype, error)) < 0)
return 0;
xfer += ret;
break;
}
if ((ret = thrift_protocol_read_field_end(protocol, error)) < 0)
return 0;
xfer += ret;
}

if ((ret = thrift_protocol_read_struct_end(protocol, error)) < 0)
return 0;
xfer += ret;

}

if (thrift_protocol_read_message_end(protocol, error) < 0)
return FALSE;

if (!thrift_transport_read_end(protocol->transport, error))
return FALSE;

return TRUE;
}

gboolean
request_service_client_send_message(RequestServiceIf *iface, Result **_return, const Param *param, GError **error) {
if (!request_service_client_send_send_message(iface, param, error))
return FALSE;
if (!request_service_client_recv_send_message(iface, _return, error))
return FALSE;
return TRUE;
}

static void
request_service_if_interface_init(RequestServiceIfInterface *iface) {
iface->send_message = request_service_client_send_message;
}

static void
request_service_client_init(RequestServiceClient *client) {
client->input_protocol = NULL;
client->output_protocol = NULL;
}

static void
request_service_client_class_init(RequestServiceClientClass *cls) {
GObjectClass *gobject_class = G_OBJECT_CLASS (cls);
GParamSpec *param_spec;

gobject_class->set_property = request_service_client_set_property;
gobject_class->get_property = request_service_client_get_property;

param_spec = g_param_spec_object("input_protocol",
"input protocol (construct)",
"Set the client input protocol",
THRIFT_TYPE_PROTOCOL,
G_PARAM_READWRITE);
g_object_class_install_property(gobject_class,
PROP_REQUEST_SERVICE_CLIENT_INPUT_PROTOCOL, param_spec);

param_spec = g_param_spec_object("output_protocol",
"output protocol (construct)",
"Set the client output protocol",
THRIFT_TYPE_PROTOCOL,
G_PARAM_READWRITE);
g_object_class_install_property(gobject_class,
PROP_REQUEST_SERVICE_CLIENT_OUTPUT_PROTOCOL, param_spec);
}

static void
request_service_handler_request_service_if_interface_init(RequestServiceIfInterface *iface);

G_DEFINE_TYPE_WITH_CODE (RequestServiceHandler,
request_service_handler,
G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE(TYPE_REQUEST_SERVICE_IF,
request_service_handler_request_service_if_interface_init))

gboolean
request_service_handler_send_message(RequestServiceIf *iface, Result **_return, const Param *param, GError **error) {
g_return_val_if_fail (IS_REQUEST_SERVICE_HANDLER(iface), FALSE);


printf("%p %p \n", param->key, error); // 这里写自己server的业务,有点头大。。。

return REQUEST_SERVICE_HANDLER_GET_CLASS (iface)->send_message(iface, _return, param, error);
}

static void
request_service_handler_request_service_if_interface_init(RequestServiceIfInterface *iface) {
iface->send_message = request_service_handler_send_message; //这里自己注册
}

static void
request_service_handler_init(RequestServiceHandler *self) {
THRIFT_UNUSED_VAR (self);
}

static gboolean
request_service_handler_sends_message(RequestServiceIf *iface, Result **_return, const Param *param, GError **error) {
THRIFT_UNUSED_VAR (iface);
THRIFT_UNUSED_VAR (error);
printf("service request_service_handler_sends_message run key-> %s, value -> %d \n",param->key,param->value);
puts("zip()");
Result *result = g_object_new(TYPE_RESULT, NULL);
result->result = true;
*_return = result;
return TRUE;
}

static void
request_service_handler_class_init(RequestServiceHandlerClass *cls) {
printf("request_service_handler_class_init in request\n");

cls->send_message = request_service_handler_sends_message;
}

enum _RequestServiceProcessorProperties {
PROP_REQUEST_SERVICE_PROCESSOR_0,
PROP_REQUEST_SERVICE_PROCESSOR_HANDLER
};

G_DEFINE_TYPE (RequestServiceProcessor,
request_service_processor,
THRIFT_TYPE_DISPATCH_PROCESSOR)

typedef gboolean (*RequestServiceProcessorProcessFunction)(RequestServiceProcessor *,
gint32,
ThriftProtocol *,
ThriftProtocol *,
GError **);

typedef struct {
gchar *name;
RequestServiceProcessorProcessFunction function;
} request_service_processor_process_function_def;

static gboolean
request_service_processor_process_send_message(RequestServiceProcessor *,
gint32,
ThriftProtocol *,
ThriftProtocol *,
GError **);

static request_service_processor_process_function_def
request_service_processor_process_function_defs[1] = {
{
"sendMessage",
request_service_processor_process_send_message
}
};

static gboolean
request_service_processor_process_send_message(RequestServiceProcessor *self,
gint32 sequence_id,
ThriftProtocol *input_protocol,
ThriftProtocol *output_protocol,
GError **error) {
gboolean result = TRUE;
ThriftTransport *transport;
ThriftApplicationException *xception;
RequestServiceSendMessageArgs *args =
g_object_new(TYPE_REQUEST_SERVICE_SEND_MESSAGE_ARGS, NULL);

printf("request_service_processor_process_send_message\n");
g_object_get(input_protocol, "transport", &transport, NULL);

if ((thrift_struct_read(THRIFT_STRUCT (args), input_protocol, error) != -1) &&
(thrift_protocol_read_message_end(input_protocol, error) != -1) &&
(thrift_transport_read_end(transport, error) != FALSE)) {
Param *param;
Result *return_value;
RequestServiceSendMessageResult *result_struct;

g_object_get(args,
"param", &param,
NULL);

g_object_unref(transport);
g_object_get(output_protocol, "transport", &transport, NULL);

result_struct = g_object_new(TYPE_REQUEST_SERVICE_SEND_MESSAGE_RESULT, NULL);
g_object_get(result_struct, "success", &return_value, NULL);

if (request_service_handler_send_message(REQUEST_SERVICE_IF (self->handler),
&return_value,
param,
error) == TRUE) {
g_object_set(result_struct, "success", return_value, NULL);

result =
((thrift_protocol_write_message_begin(output_protocol,
"sendMessage",
T_REPLY,
sequence_id,
error) != -1) &&
(thrift_struct_write(THRIFT_STRUCT (result_struct),
output_protocol,
error) != -1));
} else {
if (*error == NULL)
g_warning ("RequestService.sendMessage implementation returned FALSE "
"but did not set an error");

xception =
g_object_new(THRIFT_TYPE_APPLICATION_EXCEPTION,
"type", *error != NULL ? (*error)->code :
THRIFT_APPLICATION_EXCEPTION_ERROR_UNKNOWN,
"message", *error != NULL ? (*error)->message : NULL,
NULL);
g_clear_error(error);

result =
((thrift_protocol_write_message_begin(output_protocol,
"sendMessage",
T_EXCEPTION,
sequence_id,
error) != -1) &&
(thrift_struct_write(THRIFT_STRUCT (xception),
output_protocol,
error) != -1));

g_object_unref(xception);
}

if (param != NULL)
g_object_unref(param);
if (return_value != NULL)
g_object_unref(return_value);
g_object_unref(result_struct);

if (result == TRUE)
result =
((thrift_protocol_write_message_end(output_protocol, error) != -1) &&
(thrift_transport_write_end(transport, error) != FALSE) &&
(thrift_transport_flush(transport, error) != FALSE));
} else
result = FALSE;

g_object_unref(transport);
g_object_unref(args);

return result;
}

static gboolean
request_service_processor_dispatch_call(ThriftDispatchProcessor *dispatch_processor,
ThriftProtocol *input_protocol,
ThriftProtocol *output_protocol,
gchar *method_name,
gint32 sequence_id,
GError **error) {
request_service_processor_process_function_def *process_function_def;
gboolean dispatch_result = FALSE;

RequestServiceProcessor *self = REQUEST_SERVICE_PROCESSOR (dispatch_processor);
ThriftDispatchProcessorClass *parent_class =
g_type_class_peek_parent(REQUEST_SERVICE_PROCESSOR_GET_CLASS (self));

process_function_def = g_hash_table_lookup(self->process_map, method_name);
if (process_function_def != NULL) {
g_free(method_name);
dispatch_result = (*process_function_def->function)(self,
sequence_id,
input_protocol,
output_protocol,
error);
} else {
dispatch_result = parent_class->dispatch_call(dispatch_processor,
input_protocol,
output_protocol,
method_name,
sequence_id,
error);
}

return dispatch_result;
}

static void
request_service_processor_set_property(GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec) {
RequestServiceProcessor *self = REQUEST_SERVICE_PROCESSOR (object);

switch (property_id) {
case PROP_REQUEST_SERVICE_PROCESSOR_HANDLER:
if (self->handler != NULL)
g_object_unref(self->handler);
self->handler = g_value_get_object(value);
g_object_ref (self->handler);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}

static void
request_service_processor_get_property(GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec) {
RequestServiceProcessor *self = REQUEST_SERVICE_PROCESSOR (object);

switch (property_id) {
case PROP_REQUEST_SERVICE_PROCESSOR_HANDLER:
g_value_set_object(value, self->handler);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}

static void
request_service_processor_dispose(GObject *gobject) {
RequestServiceProcessor *self = REQUEST_SERVICE_PROCESSOR (gobject);

if (self->handler != NULL) {
g_object_unref(self->handler);
self->handler = NULL;
}

G_OBJECT_CLASS (request_service_processor_parent_class)->dispose(gobject);
}

static void
request_service_processor_finalize(GObject *gobject) {
RequestServiceProcessor *self = REQUEST_SERVICE_PROCESSOR (gobject);

thrift_safe_hash_table_destroy(self->process_map);

G_OBJECT_CLASS (request_service_processor_parent_class)->finalize(gobject);
}

static void
request_service_processor_init(RequestServiceProcessor *self) {
guint index;

self->handler = NULL;
self->process_map = g_hash_table_new(g_str_hash, g_str_equal);

for (index = 0; index < 1; index += 1)
g_hash_table_insert(self->process_map,
request_service_processor_process_function_defs[index].name,
&request_service_processor_process_function_defs[index]);
}

static void
request_service_processor_class_init(RequestServiceProcessorClass *cls) {
GObjectClass *gobject_class = G_OBJECT_CLASS (cls);
ThriftDispatchProcessorClass *dispatch_processor_class =
THRIFT_DISPATCH_PROCESSOR_CLASS (cls);
GParamSpec *param_spec;

gobject_class->dispose = request_service_processor_dispose;
gobject_class->finalize = request_service_processor_finalize;
gobject_class->set_property = request_service_processor_set_property;
gobject_class->get_property = request_service_processor_get_property;

dispatch_processor_class->dispatch_call = request_service_processor_dispatch_call;
cls->dispatch_call = request_service_processor_dispatch_call;

param_spec = g_param_spec_object("handler",
"Service handler implementation",
"The service handler implementation "
"to which method calls are dispatched.",
TYPE_REQUEST_SERVICE_HANDLER,
G_PARAM_READWRITE);
g_object_class_install_property(gobject_class,
PROP_REQUEST_SERVICE_PROCESSOR_HANDLER,
param_spec);
}

Client段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <glib-object.h>

#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
#include <thrift/c_glib/transport/thrift_buffered_transport.h>
#include <thrift/c_glib/transport/thrift_socket.h>

#include "gen-c_glib/request_service.h"

int main() {
ThriftSocket *socket;
ThriftTransport *transport;
ThriftProtocol *protocol;
RequestServiceIf *client;
socket = g_object_new(THRIFT_TYPE_SOCKET,
"hostname", "localhost",
"port", 9090,
NULL);
transport = g_object_new(THRIFT_TYPE_BUFFERED_TRANSPORT,
"transport", socket,
NULL);
protocol = g_object_new(THRIFT_TYPE_BINARY_PROTOCOL,
"transport", transport,
NULL);
GError *error = NULL;


thrift_transport_open(transport, &error);
printf("open client err %p \n", error);
client = g_object_new(TYPE_REQUEST_SERVICE_CLIENT,
"input_protocol", protocol,
"output_protocol", protocol,
NULL);
printf("client init finish %p \n", client);
Result *result = g_object_new(TYPE_RESULT, NULL);
Param *param = g_object_new(TYPE_PARAM, NULL);
param->key = "test";
param->value = 2;
printf("param %s %d \n", param->key,param->value);
gboolean ret = request_service_if_send_message(client, &result, param, &error);
printf("ret %b \n", ret);
printf("result %d \n", result->result);
printf("error %s \n", error->message);

}

CMakeFile.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cmake_minimum_required(VERSION 3.24)
project(thrift_rpc C)

set(CMAKE_C_STANDARD 11)
include_directories(/usr/include/glib-2.0)
include_directories(/usr/lib/x86_64-linux-gnu/glib-2.0/include)
add_executable(thrift_rpc main.c gen-c_glib/request_service.c gen-c_glib/model_types.c)
target_link_libraries(thrift_rpc /usr/local/lib/libthrift_c_glib.so)
target_link_libraries(thrift_rpc /usr/lib/x86_64-linux-gnu/libgobject-2.0.so)
target_link_libraries(thrift_rpc /usr/lib/x86_64-linux-gnu/libglib-2.0.so)



project(thrift_rpc_server C)
set(CMAKE_C_STANDARD 11)

add_executable(thrift_rpc_server server.c gen-c_glib/request_service.c gen-c_glib/model_types.c)

include_directories(/usr/include/glib-2.0)
include_directories(/usr/lib/x86_64-linux-gnu/glib-2.0/include)

target_link_libraries(thrift_rpc_server /usr/local/lib/libthrift_c_glib.so)
target_link_libraries(thrift_rpc_server /usr/lib/x86_64-linux-gnu/libgobject-2.0.so)
target_link_libraries(thrift_rpc_server /usr/lib/x86_64-linux-gnu/libglib-2.0.so)



protobuf-c-rpc

这里说一下protobuf-c-rpc的特点

  • rpc的函数注册是基于前缀的,示例中的example__就是。
  • Client只能通过异步回调的方式(至少我没发现有方法可以同步获取结果)
  • 支持多种通信方式,tcp,sock等

编译安装protofbuf

1
2
3
4
5
6
7
# wget https://github.com/protocolbuffers/protobuf/releases/download/v22.0/protobuf-22.0.tar.gz
# tar -zxvf protobuf-22.0.tar.gz
# cd protobuf-22.0
# ./autogen.sh
# ./configure
# make
# make install

编译安装protofbuf-c

1
2
3
4
5
6
7
# wget https://github.com/protobuf-c/protobuf-c/releases/download/v1.4.1/protobuf-c-1.4.1.tar.gz
# tar -zxvf protobuf-c-1.4.1.tar.gz
# cd protobuf-c
# ./autogen.sh
# ./configure
# make
# make install

编译安装protofbuf-c-rpc

1
2
3
4
5
6
# git clone https://github.com/protobuf-c/protobuf-c-rpc.git
# cd protobuf-c-rpc
# ./autogen.sh
# ./configure
# make
# make install

编写协议

1
2
3
4
5
6
7
8
9
10
11
12
13
syntax = "proto3";

message Param {
string message = 1;
}
message Result {
bool result = 1;
}


service RpcSendMsg {
rpc Send (Param) returns (Result);
}

Server端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "stdio.h"
#include "model.pb-c.h"
#include <string.h>
#include <protobuf-c-rpc/protobuf-c-rpc.h>
#include <stdbool.h>

static void example__send(RpcSendMsg_Service *service, const Param *param, Result_Closure closure, void *closure_data) {
printf("example__send run message:%s \n",param->message);
(void) service;
Result result = RESULT__INIT;
if (strcmp(param->message, "hello") != 0) {
result.result = true;
} else {
result.result = false;
}
closure(&result, closure_data);
}

static RpcSendMsg_Service rpc_send_msg_service = RPC_SEND_MSG__INIT(example__);

int main(void) {
printf("service start \n");
ProtobufC_RPC_Server *server;
ProtobufC_RPC_AddressType address_type = PROTOBUF_C_RPC_ADDRESS_TCP;
server = protobuf_c_rpc_server_new(address_type, "12345", (ProtobufCService *) &rpc_send_msg_service, NULL);
printf("service start at 12345 port\n");
for (;;)
protobuf_c_rpc_dispatch_run(protobuf_c_rpc_dispatch_default());
}

Client段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//
// Created by Eviltuzki on 23-2-11.
//
#include "stdio.h"
#include <string.h>
#include "model.pb-c.h"
#include <protobuf-c-rpc//protobuf-c-rpc.h>


static void
handler_result(const Result *result, void *closure_data) {
printf("rpc result %d\n", result->result);
}

int main(void) {
ProtobufCService *service;
ProtobufC_RPC_Client *client;
ProtobufC_RPC_AddressType address_type = PROTOBUF_C_RPC_ADDRESS_TCP;
const char *name = "localhost:12345";
service = protobuf_c_rpc_client_new(address_type, name, &rpc_send_msg__descriptor, NULL);
printf("connenct to server service is %p \n",service);
if (service == NULL) {
printf("error creating client \n");
return 0;
}
client = (ProtobufC_RPC_Client *) service;
while (!protobuf_c_rpc_client_is_connected(client))
protobuf_c_rpc_dispatch_run(protobuf_c_rpc_dispatch_default());
protobuf_c_boolean is_done = 0;
Param param = PARAM__INIT;
param.message = "hello1";
rpc_send_msg__send(service, &param, handler_result, &is_done);
while (!is_done)
protobuf_c_rpc_dispatch_run(protobuf_c_rpc_dispatch_default());
return 0;
}

CMakeFile.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cmake_minimum_required(VERSION 3.24)
project(rpc C)

set(CMAKE_C_STANDARD 17)

add_executable(rpc main.c model.pb-c.h model.pb-c.c)
target_link_libraries(rpc /usr/local/lib//libprotobuf-c-rpc.so)
target_link_libraries(rpc /usr/local/lib/libprotobuf-c.so)
target_link_libraries(rpc /usr/local/lib/libprotoc.so)
target_link_libraries(rpc /usr/local/lib/libprotobuf-lite.so)
target_link_libraries(rpc /usr/local/lib/libprotobuf.so)


project(rpc-s C)
add_executable(rpc-s server.c model.pb-c.h model.pb-c.c)
target_link_libraries(rpc-s /usr/local/lib//libprotobuf-c-rpc.so)
target_link_libraries(rpc-s /usr/local/lib/libprotobuf-c.so)
target_link_libraries(rpc-s /usr/local/lib/libprotoc.so)
target_link_libraries(rpc-s /usr/local/lib/libprotobuf-lite.so)
target_link_libraries(rpc-s /usr/local/lib/libprotobuf.so)

rpcgen

网上例子比较多,这里就不列举了,需要注意的是
long long类型对应的是hyper
参考:
https://docs.oracle.com/cd/E26502_01/html/E35597/xdrproto-31244.html#xdrproto-18
https://docs.oracle.com/cd/E19683-01/816-1435/6m7rrfn7f/index.html