I am trying to implement client/ server program in c (c_glib) using thrift.
I have implemented the client/ server passing variables as function arguments. Now I need to pass structure as function argument.
The thrift file am using is given below
#!/usr/local/bin/thrift --gen c_glib
struct packet {
1: i32 header,
2: i32 data
}
service Calculator {
void ping(),
i32 calculate(1:i32 id, 2:i32 num),
void stop_transfer(),
void set_packet(1:packet pac_data)
}
The steps which I followed for passing the structure is :
The client side relevant code is given below
int main (void)
{
gint head;
gint dat;
packet *trans_packet;
trans_packet = g_object_new (TYPE_PACKET, NULL);
if(!error && calculator_if_set_packet (client, trans_packet, &error)) {
g_object_get((packet *) trans_packet,
"header", &head,
"data", &dat,
NULL);
printf("struct->header : %d\n", head);
printf("struct->data : %d\n", dat);
}
g_object_unref (trans_packet);
}
The server side function is given below
static gboolean
tutorial_calculator_handler_set_packet(CalculatorIf *iface,
const packet * pac_data,
GError **error)
{
gint header;
gint data;
THRIFT_UNSED_VAR (iface);
THRIFT_UNUSED_VAR (error);
g_object_get((packet *) pac_data,
"header", &header,
"data", &data,
NULL);
g_object_set((packet *) pac_data,
"header", 123,
"data", 999,
NULL);
return TRUE;
}
When am doing this, the server is not returning anything to the client when am calling calculator_if_set_packet().
Can anyone help me on this?
I could find the solution for the above problem.
I changed the thrift file as follows :
#!/usr/local/bin/thrift --gen c_glib
struct packet {
1: i32 header,
2: i32 data
}
service Calculator {
void ping(),
i32 calculate(1:i32 id, 2:i32 num),
void stop_transfer(),
packet set_packet(1:packet pac_data)
}
The corresponding modification in client and server part as given below :
In client part
int main (void)
{
gint head;
gint dat;
packet *trans_packet;
packet *ret_packet;
trans_packet = g_object_new (TYPE_PACKET, NULL);
ret_packet = g_object_new (TYPE_PACKET, NULL);
if(!error && calculator_if_set_packet (client, &ret_packet, trans_packet, &error)) {
g_object_get((packet *) ret_packet,
"header", &head,
"data", &dat,
NULL);
printf("struct->header : %d\n", head);
printf("struct->data : %d\n", dat);
}
g_object_unref (trans_packet);
g_object_unref (ret_packet);
}
In server part
static gboolean
tutorial_calculator_handler_set_packet(CalculatorIf *iface,
packet ** _return,
const packet * pac_data,
GError **error)
{
gint header;
gint data;
THRIFT_UNSED_VAR (iface);
THRIFT_UNUSED_VAR (error);
g_object_get((packet *) pac_data,
"header", &header,
"data", &data,
NULL);
g_object_set(*_return,
"header", 123,
"data", 999,
NULL);
return TRUE;
}
Now whatever I set in the server am getting that in client.
If there is any other solution, do update the answer.