I need to serialize a thrift struct in C (glib) such that I can deserialize it in python. Note that I am using my own transport here.
Suppose I have a structed defined in thrift such as the following:
struct frame_header_s {
1: required i32 pattern = FRAME_PATTERN,
2: required i16 header_length,
3: required i32 frame_index,
4: required frame_type_e type,
5: required i32 frame_length_bytes
}
I can serialize/deserialize this header in python as follows:
from thrift.TSerialization import serialize, deserialize
import gen.ttypes as gen_types
def ser_deser_bytes():
hdr = gen_types.frame_header_s()
hdr.pattern = 0x5a5a5a5a
hdr.frame_index = 89
hdr.type = gen_types.frame_type_e.T1
hdr.frame_length_bytes = 1000
tst = serialize(hdr)
des = deserialize(gen_types.frame_header_s(), tst)
assert des.pattern == hdr.pattern, 'test didnt work'
assert des.frame_length_bytes == hdr.frame_length_bytes, 'test didnt work'
print('test worked')
if __name__ == '__main__':
ser_deser_bytes()
But when I try and use a naive approach of serializing the generated struct in C such as:
#include "gen-c_glib/gen.h"
frame_header_s *frame_header;
void server_test()
{
frame_header = g_object_new(TYPE_FRAME_HEADER_S, NULL);
frame_header->header_length = sizeof(frame_header_s);
// TCP send
int sent = send(fd, (uint8_T*)frame_header, sizeof(frame_header_s));
// etc.
}
I get the following error when I attempt to deserialize the data on the client in python:
thrift.protocol.TProtocol.TProtocolException: invalid TType
I am sure I am doing something wrong in my naive serialization in C, but I can’t find any document for thrift that gives me the kind of detail that I need. I would really appreciate any insight.