From b396ee26febb003374f877af841814940fefd034 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Fri, 16 Sep 2011 14:35:25 +0200 Subject: Typesafe callback initialization --- src/cmumble.c | 57 ++++++++++++++++++++++++++++----------------------------- src/cmumble.h | 8 +++++++- src/messages.c | 3 ++- 3 files changed, 37 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/cmumble.c b/src/cmumble.c index c72ba25..dea9a55 100644 --- a/src/cmumble.c +++ b/src/cmumble.c @@ -202,34 +202,33 @@ recv_user_state(MumbleProto__UserState *state, struct context *ctx) ctx->users = g_list_prepend(ctx->users, user); } - -static const callback_t callbacks[] = { - [Version] = (callback_t) recv_version, - [UDPTunnel] = (callback_t) recv_udp_tunnel, - [Authenticate] = (callback_t) NULL, - [Ping] = (callback_t) NULL, - [Reject] = (callback_t) NULL, - [ServerSync] = (callback_t) recv_server_sync, - [ChannelRemove] = (callback_t) NULL, - [ChannelState] = (callback_t) recv_channel_state, - [UserRemove] = (callback_t) recv_user_remove, - [UserState] = (callback_t) recv_user_state, - [BanList] = (callback_t) NULL, - [TextMessage] = (callback_t) NULL, - [PermissionDenied] = (callback_t) NULL, - [ACL] = (callback_t) NULL, - [QueryUsers] = (callback_t) NULL, - [CryptSetup] = (callback_t) recv_crypt_setup, - [ContextActionModify] = (callback_t) NULL, - [ContextAction] = (callback_t) NULL, - [UserList] = (callback_t) NULL, - [VoiceTarget] = (callback_t) NULL, - [PermissionQuery] = (callback_t) NULL, - [CodecVersion] = (callback_t) recv_codec_version, - [UserStats] = (callback_t) NULL, - [RequestBlob] = (callback_t) NULL, - [ServerConfig] = (callback_t) NULL, - [SuggestConfig] = (callback_t) NULL, +static struct mumble_callbacks callbacks = { + .Version = recv_version, + .UDPTunnel = recv_udp_tunnel, + .Authenticate = NULL, + .Ping = NULL, + .Reject = NULL, + .ServerSync = recv_server_sync, + .ChannelRemove = NULL, + .ChannelState = recv_channel_state, + .UserRemove = recv_user_remove, + .UserState = recv_user_state, + .BanList = NULL, + .TextMessage = NULL, + .PermissionDenied = NULL, + .ACL = NULL, + .QueryUsers = NULL, + .CryptSetup = recv_crypt_setup, + .ContextActionModify = NULL, + .ContextAction = NULL, + .UserList = NULL, + .VoiceTarget = NULL, + .PermissionQuery = NULL, + .CodecVersion = recv_codec_version, + .UserStats = NULL, + .RequestBlob = NULL, + .ServerConfig = NULL, + .SuggestConfig = NULL, }; static gboolean @@ -254,7 +253,7 @@ read_cb(GSocket *socket, GIOCondition condition, gpointer data) GInputStream *input = g_io_stream_get_input_stream(ctx->iostream); do { - recv_msg(ctx, callbacks, G_N_ELEMENTS(callbacks)); + recv_msg(ctx, &callbacks); } while (g_input_stream_has_pending(input)); /* FIXME */ diff --git a/src/cmumble.h b/src/cmumble.h index f7db59c..a6cc7f2 100644 --- a/src/cmumble.h +++ b/src/cmumble.h @@ -61,12 +61,18 @@ enum mumble_message { #undef MUMBLE_MSG }; +struct mumble_callbacks { +#define MUMBLE_MSG(a,b) void (* a)(MumbleProto__##a *, struct context *); + MUMBLE_MSGS +#undef MUMBLE_MSG +}; + typedef void (*callback_t)(ProtobufCMessage *msg, struct context *); void send_msg(struct context *ctx, ProtobufCMessage *msg); void -recv_msg(struct context *ctx, const callback_t *callbacks, uint32_t callback_size); +recv_msg(struct context *ctx, const struct mumble_callbacks *callbacks); #endif diff --git a/src/messages.c b/src/messages.c index 2fd846e..87f204e 100644 --- a/src/messages.c +++ b/src/messages.c @@ -72,7 +72,7 @@ send_msg(struct context *ctx, ProtobufCMessage *msg) } void -recv_msg(struct context *ctx, const callback_t *callbacks, uint32_t callback_size) +recv_msg(struct context *ctx, const struct mumble_callbacks *cbs) { uint8_t preamble[PREAMBLE_SIZE]; ProtobufCMessage *msg; @@ -80,6 +80,7 @@ recv_msg(struct context *ctx, const callback_t *callbacks, uint32_t callback_siz int type, len; gssize ret; GInputStream *input = g_io_stream_get_input_stream(ctx->iostream); + const callback_t *callbacks = (const callback_t *) cbs; ret = g_input_stream_read(input, preamble, PREAMBLE_SIZE, NULL, NULL); -- cgit