【发布时间】:2019-11-18 02:08:27
【问题描述】:
我是rabbitmq和protobuf的新手,有没有一些代码示例可以通过rabbitmq用c语言发送和接收protobuf数据?谢谢
【问题讨论】:
-
和你通过 rabbitmq 使用 C 发送任何其他数据的方式一样吗?
-
@RobertHarvey 我已经粘贴了下面的工作代码。
标签: c rabbitmq protocol-buffers
我是rabbitmq和protobuf的新手,有没有一些代码示例可以通过rabbitmq用c语言发送和接收protobuf数据?谢谢
【问题讨论】:
标签: c rabbitmq protocol-buffers
操作定义如下:
syntax = "proto2";
message Operation {
required string operation = 1;
optional int32 tracking_id = 2;
optional double x = 3;
optional double y = 4;
}
发送代码:
Operation msg = OPERATION__INIT;
uint8_t *buf;
size_t len;
msg.operation = "d";
msg.tracking_id = 1;
msg.x = 0.5566;
msg.y = 0.2666;
msg.has_tracking_id = 1;
msg.has_x = 1;
msg.has_y = 1;
len = operation__get_packed_size(&msg);
buf = malloc(len);
operation__pack(&msg, buf);
amqp_bytes_t body;
body.len = len;
body.bytes = buf;
amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange),amqp_cstring_bytes(routingkey), 0, 0, &props, body);
free(buf);
接收代码:
amqp_rpc_reply_t res;
amqp_envelope_t envelope;
amqp_maybe_release_buffers(conn);
res = amqp_consume_message(conn, &envelope, NULL, 0);
if (AMQP_RESPONSE_NORMAL != res.reply_type)
{
break;
}
fprintf(stderr, "%s\n", envelope.message.body.bytes);
fprintf(stderr, "%zu\n", envelope.message.body.len);
Operation *msg = operation__unpack(NULL, envelope.message.body.len, (const uint8_t *)envelope.message.body.bytes);
printf("operation:%s\n", msg->operation);
if (msg->has_tracking_id)
printf("%d\n", msg->tracking_id);
if (msg->has_x)
printf("%f\n", msg->x);
if (msg->has_y)
printf("%f\n", msg->y);
amqp_destroy_envelope(&envelope);
【讨论】: