C++ GPB序列化到函数时出现问题

C++ GPB序列化到函数时出现问题,c++,rabbitmq,protocol-buffers,C++,Rabbitmq,Protocol Buffers,我有下面的代码 main() { test::RouteMessage *Rtmesg = new test::RouteMessage; test::RouteV4Prefix *prefix = new test::RouteV4Prefix; test::RouteMessage testRtmesg; prefix->set_family(test::RouteV4Prefix::RT_AFI_V4); prefix->set_pre

我有下面的代码

main()
{
    test::RouteMessage *Rtmesg = new test::RouteMessage;
    test::RouteV4Prefix *prefix = new test::RouteV4Prefix;
    test::RouteMessage testRtmesg;

    prefix->set_family(test::RouteV4Prefix::RT_AFI_V4);
    prefix->set_prefix_len(24);
    prefix->set_prefix(1000);

    Rtmesg->set_routetype(test::RouteMessage::RT_TYPE_BGP);
    Rtmesg->set_allocated_v4prefix(prefix);
    Rtmesg->set_flags(test::RouteMessage::RT_FLGS_NONE);
    Rtmesg->set_routeevnt(test::RouteMessage::BGP_EVNT_V4_RT_ADD);
    Rtmesg->set_nexthop(100);
    Rtmesg->set_ifindex(200); Rtmesg->set_metric(99);
    Rtmesg->set_pref(1);
    int size = Rtmesg->ByteSize();

    char const *rt_msg = (char *)malloc(size);

    google::protobuf::io::ArrayOutputStream oarr(rt_msg, size);
    google::protobuf::io::CodedOutputStream output (&oarr)

    Rtmesg->SerializeToCodedStream(&output);


    // Below code is just to see if everything is fine.
    google::protobuf::io::ArrayInputtStream iarr(rt_msg, size);
    google::protobuf::io::CodedInputStream Input (&iarr)

    testRtmesg.ParseFromCodedStream(&Input);

    Vpe::RouteV4Prefix test_v4Prefix = testRtmesg.v4prefix();
    cout << std::endl;
    std::cout << "Family " << test_v4Prefix.family() << std::endl;
    std::cout << "Prefix " << test_v4Prefix.prefix()<< std::endl;
    std::cout << "PrefixLen " << test_v4Prefix.prefix_len() << std::endl;

    // All the above outputs are fine.

    cout << std::endl;
    cout << rt_msg; <<------------ This prints absolutely junk.
    cout << std::endl;

    amqp_bytes_t str2; 
    str2 = amqp_cstring_bytes(rt_msg);  <<----- This just crashes.
    printf("\n str2=%s %d", str2.bytes, str2.len);
main()
{
测试::路由消息*Rtmesg=新测试::路由消息;
测试::RouteV4Prefix*前缀=新测试::RouteV4Prefix;
test::路由消息testRtmesg;
前缀->设置_族(测试::RouteV4Prefix::RT_AFI_V4);
前缀->设置前缀长度(24);
前缀->设置前缀(1000);
Rtmesg->set_routetype(测试::RouteMessage::RT_TYPE_BGP);
Rtmesg->set\u allocated\u v4prefix(前缀);
Rtmesg->set_标志(测试::路由消息::RT_FLGS_NONE);
Rtmesg->set_routeevnt(测试::路由消息::BGP_EVNT_V4_RT_添加);
Rtmesg->set_nexthop(100);
Rtmesg->set\U ifindex(200);Rtmesg->set\U公制(99);
Rtmesg->set_pref(1);
int size=Rtmesg->ByteSize();
char const*rt_msg=(char*)malloc(大小);
google::protobuf::io::ArrayOutputStream oarr(rt_msg,大小);
google::protobuf::io::CodeDoutput流输出(&oarr)
Rtmesg->SerializeToCodedStream(&output);
//下面的代码只是为了看看是否一切正常。
google::protobuf::io::ArrayInputStream iarr(rt_msg,size);
google::protobuf::io::CodedInputStream输入(&iarr)
testRtmesg.ParseFromCodedStream(&Input);
Vpe::RouteV4Prefix test_v4Prefix=testRtmesg.v4Prefix();

cout协议缓冲区是二进制序列化格式,而不是文本。这意味着:

  • 是的,如果您将二进制数据写入cout,它将看起来像垃圾(或崩溃)
  • 数据不像C字符串那样以NUL结尾。因此,您不能将其传递到像
    amqp\u cstring\u bytes
    这样的函数中,该函数需要以NUL结尾的
    char*
    ——它可能会在第一个0字节处缩短数据,也可能会在缓冲区结束后搜索0字节并崩溃。通常,任何采用
    Char*
    但不占用长度也不起作用
我不熟悉
amqp
,但它看起来像是您试图调用的函数,
amqp\u cstring\u bytes
,只是构建了一个
amqp\u bytes\t
,定义如下:

typedef struct amqp_bytes_t_ {
  size_t len;
  void *bytes;
} amqp_bytes_t;
所以,你所要做的就是:

amqp_bytes_t str2;
str2.bytes = rt_msg;
str2.len = size;

谢谢。我找到并修好了。很高兴再次从你那里得知这一点。