【问题标题】:Thrift: Is it possible to do only serialization with the C (GLib) Thrift library?Thrift:是否可以只使用 C (GLib) Thrift 库进行序列化?
【发布时间】:2016-03-22 21:42:14
【问题描述】:

我正在尝试使用 this 示例,但它总是返回 write_len = 10 个字节。

ThriftTransport* transport = THRIFT_TRANSPORT(
    g_object_new(THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 8096, NULL));
ThriftProtocol* protocol = THRIFT_PROTOCOL(
    g_object_new(THRIFT_TYPE_BINARY_PROTOCOL, "transport", transport,
        NULL));

Exception* src = g_object_new(TYPE_EXCEPTION, NULL);
ExceptionClass* cls = EXCEPTION_GET_CLASS(src);
g_object_set(src,
    "ex_sign", exception_signature,
    "cl_sign", class_signature,
    "caught", catch_method != NULL,
    NULL);

int write_len = THRIFT_STRUCT_CLASS(cls)->write(THRIFT_STRUCT(src), protocol, &error);

【问题讨论】:

    标签: c serialization thrift


    【解决方案1】:

    这是a bug in the C (GLib) implementation,最近才报告并修复,因此您需要从 git 获取并构建 the latest Thrift source 以使示例正常工作。

    如果您好奇,可以查看导致错误被识别的the discussion on the user mailing list

    【讨论】:

    • 谢谢!明天用最新的源码试试
    【解决方案2】:

    经过一些研究并在社区的帮助下,我想出了不仅适用于 master 分支,而且适用于 0.9.3 版本的序列化解决方案。实际上我们根本不需要变量 write_len:

    ThriftMemoryBuffer* tbuffer = g_object_new(THRIFT_TYPE_MEMORY_BUFFER,
        "buf_size", 2048, NULL);
    ThriftTransport *transport = NULL;
    ThriftProtocol* protocol = NULL;
    GError* error = NULL;
    
    if (tbuffer) {
        transport = THRIFT_TRANSPORT(tbuffer);
        thrift_transport_open(transport, &error);
    
        protocol =
        THRIFT_PROTOCOL(
            g_object_new(THRIFT_TYPE_BINARY_PROTOCOL, "transport", transport, NULL));
    
        if (protocol) {
            ExceptionData* exception_data = g_object_new(TYPE_EXCEPTION_DATA, "ex_sign",
                exception_signature, "cl_sign", class_signature, "caught",
                catch_method != NULL,
                NULL);
    
            if (exception_data) {
                ThriftStructClass* cls = THRIFT_STRUCT_CLASS(EXCEPTION_DATA_GET_CLASS(exception_data));
                cls->write(exception_data, protocol, &error);
    
                if(tbuffer->buf != NULL) {
    
                    printf("Buffer length %i bytes\n", tbuffer->buf->len);
    
                    send_kafka_message((const void *)tbuffer->buf->data, tbuffer->buf->len);
                }
    
                g_object_unref(exception_data);
            }
    
            g_object_unref(protocol);
        }
    
        if (thrift_transport_is_open(transport)) {
            thrift_transport_close(transport, &error);
        }
    
        g_object_unref(tbuffer);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      • 2015-06-07
      • 1970-01-01
      相关资源
      最近更新 更多