From 0ef865bb301954df86b87cd1978ca711c01e71d5 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 21 Aug 2010 01:09:12 +0200 Subject: Added the basics for local control commands. --- control_service.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 control_service.c (limited to 'control_service.c') diff --git a/control_service.c b/control_service.c new file mode 100644 index 0000000..a6f035d --- /dev/null +++ b/control_service.c @@ -0,0 +1,59 @@ +#include "control_service.h" +#include "control_commands.h" + +#include +#include +#include +#include + +static GSocketService *ss = NULL; +static GSocketAddress *address; + +static gboolean service_incoming(GSocketService *service, + GSocketConnection *connection, GObject *source_object, + gpointer user_data) { + g_debug("local service got incoming connection"); + + GSocket *socket = g_socket_connection_get_socket(connection); + gchar buffer[0x400]; + gssize size = g_socket_receive(socket, buffer, 0x400, NULL, NULL); + + gchar *pos = g_strstr_len(buffer, size, "\r"); + if(pos == NULL) { + pos = g_strstr_len(buffer, size, "\n"); + } + if(pos == NULL) { + g_warning("EOL not found"); + return FALSE; + } + *pos = '\0'; + + control_commands_handle(buffer); + + return FALSE; +} + +gboolean control_service_start() { + address = g_unix_socket_address_new("audist.sock"); + + ss = g_threaded_socket_service_new(2); + + if(g_socket_listener_add_address((GSocketListener*)ss, address, + G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL, + NULL, NULL) == FALSE) { + g_error("g_socket_listener_add_socket() failed"); + } + + g_signal_connect(ss, "incoming", (GCallback)service_incoming, NULL); + + g_socket_service_start((GSocketService*)ss); + + return TRUE; +} + +void control_service_stop() { + g_socket_service_stop((GSocketService*)ss); + g_unlink(g_unix_socket_address_get_path((GUnixSocketAddress*)address)); + g_object_unref(ss); + g_object_unref(address); +} -- cgit v1.2.3