Search code examples
cthrift

How do I create a Thrift server in C (with GLib)?


I am trying to implement a client/ server in c using thrift. I am able to generate the server skeleton in cpp using thrift. But while generating for c_glib, the server skeleton file is not being generated.

I searched about the same in the internet but its almost of no use. It would be so appreciable if someone could help me over this.

The .thrift file am using is given below

#!/usr/local/bin/thrift --gen cpp

namespace c_glib Test

struct packet {
  1:required i32 header,
  2:required i32 data,
  3:required i32 crc
}

service Something {
  i32 ping(),
  packet transfer()
}

Solution

  • Thrift's compiler for C doesn't generate a skeleton server; you'll need to write this code yourself. However there is not a lot of work involved.

    Thrift's source-code distribution includes a server tutorial for C (GLib) that shows what you need to do. (For context, refer to the matching tutorial for C++ and the tutorial .thrift file on which both are based.) I suggest you read through this as it provides both a working example and important information on how the Thrift framework expects to interact with your code.

    At a high level, the task of implementing a server in C is very much the same as it is for C++. First, create a handler class derived from the compiler-generated abstract base class (TestSomethingHandler, in your case) that implements your server's methods. Then to start a server, you simply

    • Create an instance of the compiler-generated processor (TestSomethingProcessor), passing it an instance of your handler implementation;
    • Create the necessary transport and factory objects that define how the server is to communicate; and finally
    • Create an instance of the server class with the above objects and start it running.

    The server tutorial includes a generic example of launching a server you can use as a reference.