Thrift: Is it possible to do only serialization with the C (GLib) Thrift library?

淺唱寂寞╮ 提交于 2019-12-11 01:15:10

问题


I am trying to use this example but it always return write_len = 10 bytes.

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);

回答1:


This was a bug in the C (GLib) implementation that was reported and fixed just recently, so you'll need to fetch and build the latest Thrift source from git for the example to work correctly.

If you're curious, you can see the discussion on the user mailing list that led to the bug being identified.




回答2:


After some research and with the help of community I came up with serializing solution that works not only with master branch, but with 0.9.3 version as well. Actually we don't need variable write_len at all:

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);
}


来源:https://stackoverflow.com/questions/34317358/thrift-is-it-possible-to-do-only-serialization-with-the-c-glib-thrift-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!