From 9c7db46be9933e36e79b4caac86a5bdc3183122d Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Thu, 26 Aug 2010 22:36:07 +0200 Subject: Merged commands.c and control_commands.c. --- commands.c | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) (limited to 'commands.c') diff --git a/commands.c b/commands.c index 3ff250d..db7e133 100644 --- a/commands.c +++ b/commands.c @@ -3,8 +3,7 @@ #include -static void commands_list(GSocketConnection *connection, const gchar *cmd) { - GError *error = NULL; +static void commands_list(GSocketConnection *connection, const gchar *cmd, GError **error) { gchar **data = g_strsplit(cmd, " ", 2); for(gint i = 0; data[i]; i++) { g_debug("\tdata[%d] = %s", i, data[i]); @@ -46,16 +45,14 @@ static void commands_list(GSocketConnection *connection, const gchar *cmd) { GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection); if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL, - &error) == FALSE) { - g_warning(error->message); - g_error_free(error); + error) == FALSE) { + return; } g_string_free(string, TRUE); } -static void commands_find(GSocketConnection *connection, const gchar *cmd) { - GError *error = NULL; +static void commands_find(GSocketConnection *connection, const gchar *cmd, GError **error) { gchar **data = g_strsplit(cmd, " ", 3); if(g_strv_length(data) != 3) { @@ -88,42 +85,34 @@ static void commands_find(GSocketConnection *connection, const gchar *cmd) { GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection); if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL, - &error) == FALSE) { - g_warning(error->message); - g_error_free(error); + error) == FALSE) { + return; } g_string_free(string, TRUE); } -static void commands_ping(GSocketConnection *connection, const gchar *cmd) { - GError *error = NULL; +static void commands_ping(GSocketConnection *connection, const gchar *cmd, GError **error) { GSocket *socket = g_socket_connection_get_socket(connection); - if(g_socket_send(socket, "pong\n", 5, NULL, NULL) == -1) { - g_warning(error->message); - g_error_free(error); - } + g_socket_send(socket, "pong\n", 5, NULL, error); } -static void commands_exit(GSocketConnection *connection, const gchar *cmd) { +static void commands_exit(GSocketConnection *connection, const gchar *cmd, GError **error) { GSocket *socket = g_socket_connection_get_socket(connection); g_socket_close(socket, NULL); } -void commands_handle(GSocketConnection *connection, const gchar *cmd) { +void commands_handle(GSocketConnection *connection, const gchar *cmd, GError **error) { g_debug(cmd); if(g_strncasecmp(cmd, "ping", 4) == 0) { - commands_ping(connection, cmd); + commands_ping(connection, cmd, error); } else if(g_strncasecmp(cmd, "list", 4) == 0) { - commands_list(connection, cmd); + commands_list(connection, cmd, error); } else if(g_strncasecmp(cmd, "find", 4) == 0) { - commands_find(connection, cmd); + commands_find(connection, cmd, error); } else if(g_strncasecmp(cmd, "exit", 4) == 0) { - commands_exit(connection, cmd); + commands_exit(connection, cmd, error); } else { g_debug("unknown command"); - gchar *buf = g_strdup_printf("error: unknown command %s\n", cmd); - GSocket *socket = g_socket_connection_get_socket(connection); - g_socket_send(socket, buf, strlen(buf), NULL, NULL); - g_free(buf); + *error = g_error_new(0, 0, "unknown command %s", cmd); } } -- cgit v1.2.3