使用 thrift c_glib 客户端未将结构返回给客户端
Structure not returned to client using thrift c_glib client
我正在尝试使用 thrift 在 c (c_glib) 中实现客户端/服务器程序。
我已经实现了将变量作为函数参数传递的客户端/服务器。现在我需要将结构作为函数参数传递。
下面给出了我正在使用的 thrift 文件
#!/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)
}
我传递结构所遵循的步骤是:
- 在客户端
1. g_object_new(TYPE_PACKET, NULL)
2。将结构指针传递给服务器。 calculator_if_set_packet(客户端,trans_packet,&错误)
3。 g_object_get(tans_packet, "header", &head, "data", &dat, NULL) 获取server
4中的数据集。打印结构显示server中的数据集在client中反映回来
客户端相关代码如下
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);
}
- 在服务器端
1。 g_object_get(pac_data, "header", &header, "data", &data, NULL) 得到pac_data
2的属性。 g_object_setg_object_get(tans_packet, "header", 111, "data", 999, NULL)
服务端函数如下
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;
}
执行此操作时,服务器在调用 calculator_if_set_packet() 时不会向客户端返回任何内容。
谁能帮我解决这个问题?
您显示的结构未使用 C 语法,即使对于位字段也是如此:
struct packet { //Not C syntax
1: i32 header, //1nvalid struct field declaration
2: i32 data //invalid struct field declaration
} //missing ;
您没有显示将结构作为参数的函数原型,而是举例说明您的结构是这样定义的:
typedef struct {
int header;
int data;
} PACKET; //symbol PACKET would be used like your "struct packet"
然后使用此 typedef,您可以创建一个实例,以及指向该结构的指针,如下所示:
PACKET 数据包,*pPacket;
在main中,可以使用实例来初始化指针,像这样:
int main(void)
{
//pointer instance
pPacket = &packet;
pPacket = malloc(sizeof(PACKET));//provide memory for pointer
...
free(pPacket);//free dynamic memory
return 0;
}
然后,举例来说,你有一个函数原型接受一个实例,另一个函数原型接受一个指向该结构的指针:
void someFunc1(PACKET pkt); // takes an instance of PACKET
void someFunc2(PACKET *pkt); //takes an instance of PACKET *
然后在main中,可以这样pass then:
int main(void)
{
pPacket = &packet; //initialize pointer using instance
pPacket = malloc(sizeof(PACKET));//provide memory for pointer
someFunc1(packet); //pass instance of PACKET
someFunc2(pPacket); //pass pointer to PACKET
someFunc2(&packet); //same as previous
...
free(pPacket);//free dynamic memory
return 0;
}
我可以找到上述问题的解决方案。
我更改了 thrift 文件如下:
#!/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)
}
客户端和服务器部分相应修改如下:
在客户端
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);
}
在服务器部分
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;
}
现在无论我在服务器中设置什么,客户端都会得到它。
如果有任何其他解决方案,请更新答案。
我正在尝试使用 thrift 在 c (c_glib) 中实现客户端/服务器程序。
我已经实现了将变量作为函数参数传递的客户端/服务器。现在我需要将结构作为函数参数传递。
下面给出了我正在使用的 thrift 文件
#!/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)
}
我传递结构所遵循的步骤是:
- 在客户端
1. g_object_new(TYPE_PACKET, NULL)
2。将结构指针传递给服务器。 calculator_if_set_packet(客户端,trans_packet,&错误)
3。 g_object_get(tans_packet, "header", &head, "data", &dat, NULL) 获取server
4中的数据集。打印结构显示server中的数据集在client中反映回来
客户端相关代码如下
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);
}
- 在服务器端
1。 g_object_get(pac_data, "header", &header, "data", &data, NULL) 得到pac_data
2的属性。 g_object_setg_object_get(tans_packet, "header", 111, "data", 999, NULL)
服务端函数如下
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;
}
执行此操作时,服务器在调用 calculator_if_set_packet() 时不会向客户端返回任何内容。
谁能帮我解决这个问题?
您显示的结构未使用 C 语法,即使对于位字段也是如此:
struct packet { //Not C syntax
1: i32 header, //1nvalid struct field declaration
2: i32 data //invalid struct field declaration
} //missing ;
您没有显示将结构作为参数的函数原型,而是举例说明您的结构是这样定义的:
typedef struct {
int header;
int data;
} PACKET; //symbol PACKET would be used like your "struct packet"
然后使用此 typedef,您可以创建一个实例,以及指向该结构的指针,如下所示:
PACKET 数据包,*pPacket;
在main中,可以使用实例来初始化指针,像这样:
int main(void)
{
//pointer instance
pPacket = &packet;
pPacket = malloc(sizeof(PACKET));//provide memory for pointer
...
free(pPacket);//free dynamic memory
return 0;
}
然后,举例来说,你有一个函数原型接受一个实例,另一个函数原型接受一个指向该结构的指针:
void someFunc1(PACKET pkt); // takes an instance of PACKET
void someFunc2(PACKET *pkt); //takes an instance of PACKET *
然后在main中,可以这样pass then:
int main(void)
{
pPacket = &packet; //initialize pointer using instance
pPacket = malloc(sizeof(PACKET));//provide memory for pointer
someFunc1(packet); //pass instance of PACKET
someFunc2(pPacket); //pass pointer to PACKET
someFunc2(&packet); //same as previous
...
free(pPacket);//free dynamic memory
return 0;
}
我可以找到上述问题的解决方案。
我更改了 thrift 文件如下:
#!/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)
}
客户端和服务器部分相应修改如下:
在客户端
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);
}
在服务器部分
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;
}
现在无论我在服务器中设置什么,客户端都会得到它。
如果有任何其他解决方案,请更新答案。