diff options
Diffstat (limited to 'control_service.c')
-rw-r--r-- | control_service.c | 59 |
1 files changed, 59 insertions, 0 deletions
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 <glib.h> +#include <glib/gstdio.h> +#include <gio/gio.h> +#include <gio/gunixsocketaddress.h> + +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); +} |