I'm making a network server in C and trying to define packet handlers as generic as possible.
When I receive a completed packet, I want to create a struct and send it to the defined handler.
struct handler {
char *msg_name;
void (*handler)(network_client *, void *msg).
}
//global variable containing all handlers
struct handler const handlers[] = {
{"hello_msg", &helloMessageHandler}
};
struct hello_message_s {
int foo;
char *bar;
float foobar;
}
//called when received hello_cmd and deserialized hello_message_s bytes received by the network
void helloMessageHandler(network_client *c, void *msg) {
struct hello_message_s *casted = msg;
casted->foo; //etc..
}
My question is; is there a way to specify the message struct directly instead of void * forcing me to cast later?
i.e change void helloMessageHandler(network_client *c, void *msg)
to void helloMessageHandler(network_client *c, struct hello_message_s *msg)
.
It's not possible since handlers[] can hold handlers with void *
only.
struct handler const handlers[] = {
{"hello_msg", &helloMessageHandler}
};
Any solution?