From d1fac4d941aaa9dbf9e72540f97d928af241cb5e Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Fri, 23 Sep 2011 10:53:51 +0200 Subject: Store callbacks in context --- src/cmumble.c | 10 ++++++++-- src/cmumble.h | 13 ++++--------- src/connection.c | 6 ++---- src/connection.h | 6 +----- src/messages.c | 13 +++++++------ 5 files changed, 22 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/cmumble.c b/src/cmumble.c index bb2e934..5cc61c0 100644 --- a/src/cmumble.c +++ b/src/cmumble.c @@ -205,7 +205,12 @@ recv_user_state(MumbleProto__UserState *state, struct context *ctx) ctx->users = g_list_prepend(ctx->users, user); } -static struct mumble_callbacks callbacks = { + +static const struct { +#define MUMBLE_MSG(a,b) void (* a)(MumbleProto__##a *, struct context *); + MUMBLE_MSGS +#undef MUMBLE_MSG +} callbacks = { .Version = recv_version, .UDPTunnel = recv_udp_tunnel, .Authenticate = NULL, @@ -380,8 +385,9 @@ int main(int argc, char **argv) g_type_init(); ctx.loop = g_main_loop_new(NULL, FALSE); + ctx.callbacks = (const callback_t *) &callbacks; - if (cmumble_connection_init(&ctx, host, port, &callbacks) < 0) + if (cmumble_connection_init(&ctx, host, port) < 0) return 1; { diff --git a/src/cmumble.h b/src/cmumble.h index f315368..9f40d31 100644 --- a/src/cmumble.h +++ b/src/cmumble.h @@ -18,9 +18,12 @@ #include "io.h" #include "connection.h" +typedef void (*callback_t)(ProtobufCMessage *msg, struct context *); + struct context { struct cmumble_connection con; struct cmumble_io io; + const callback_t *callbacks; GMainLoop *loop; uint32_t session; @@ -60,18 +63,10 @@ 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); int -recv_msg(struct context *ctx, const struct mumble_callbacks *callbacks); +recv_msg(struct context *ctx); #endif diff --git a/src/connection.c b/src/connection.c index 3494d08..7cb48e6 100644 --- a/src/connection.c +++ b/src/connection.c @@ -12,7 +12,7 @@ read_cb(GObject *pollable_stream, gpointer data) gint count; do { - count = recv_msg(ctx, ctx->con.callbacks); + count = recv_msg(ctx); } while (count && g_pollable_input_stream_is_readable(input)); return TRUE; @@ -46,13 +46,11 @@ setup_ping_timer(struct context *ctx) int cmumble_connection_init(struct context *ctx, - const char *host, int port, - struct mumble_callbacks *callbacks) + const char *host, int port) { struct cmumble_connection *con = &ctx->con; GError *error = NULL; - con->callbacks = callbacks; con->sock_client = g_socket_client_new(); g_socket_client_set_tls(con->sock_client, TRUE); g_socket_client_set_tls_validation_flags(con->sock_client, diff --git a/src/connection.h b/src/connection.h index e38150d..cf3dbc0 100644 --- a/src/connection.h +++ b/src/connection.h @@ -12,17 +12,13 @@ struct cmumble_connection { GOutputStream *output; GSource *source; - - struct mumble_callbacks *callbacks; }; struct context; -struct mumble_callbacks; int cmumble_connection_init(struct context *ctx, - const char *host, int port, - struct mumble_callbacks *cbs); + const char *host, int port); int cmumble_connection_fini(struct context *ctx); diff --git a/src/messages.c b/src/messages.c index 8807166..98b53e3 100644 --- a/src/messages.c +++ b/src/messages.c @@ -71,16 +71,17 @@ send_msg(struct context *ctx, ProtobufCMessage *msg) } int -recv_msg(struct context *ctx, const struct mumble_callbacks *cbs) +recv_msg(struct context *ctx) { uint8_t preamble[PREAMBLE_SIZE]; ProtobufCMessage *msg; gchar *data; int type, len; gssize ret; - const callback_t *callbacks = (const callback_t *) cbs; GError *error = NULL; + g_assert(ctx->callbacks); + ret = g_pollable_input_stream_read_nonblocking(ctx->con.input, preamble, PREAMBLE_SIZE, NULL, &error); @@ -121,8 +122,8 @@ recv_msg(struct context *ctx, const struct mumble_callbacks *cbs) udptunnel.packet.len = len; udptunnel.packet.data = (uint8_t *) data; - if (callbacks[UDPTunnel]) - callbacks[UDPTunnel](&udptunnel.base, ctx); + if (ctx->callbacks[UDPTunnel]) + ctx->callbacks[UDPTunnel](&udptunnel.base, ctx); g_free(data); return 0; @@ -136,8 +137,8 @@ recv_msg(struct context *ctx, const struct mumble_callbacks *cbs) } g_print("debug: received message: %s type:%d, len:%d\n", messages[type].name, type, len); - if (callbacks[type]) - callbacks[type](msg, ctx); + if (ctx->callbacks[type]) + ctx->callbacks[type](msg, ctx); protobuf_c_message_free_unpacked(msg, NULL); g_free(data); -- cgit