diff options
Diffstat (limited to 'command_service.c')
-rw-r--r-- | command_service.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/command_service.c b/command_service.c new file mode 100644 index 0000000..bfbc2cb --- /dev/null +++ b/command_service.c @@ -0,0 +1,58 @@ +#include "command_service.h" +#include "server_commands.h" + +#include <gio/gio.h> + +static GSocketService *ss = NULL; + +static gboolean service_incoming(GSocketService *service, + GSocketConnection *connection, GObject *source_object, + gpointer user_data) { + GError *error = NULL; + GSocket *socket = g_socket_connection_get_socket(connection); + gchar buffer[0x400]; + + while(g_socket_is_connected(socket) == TRUE) { + gssize size = g_socket_receive(socket, buffer, 0x400, NULL, &error); + + if(size < 0) { + g_warning(error->message); + return FALSE; + } else if(size == 0) { + break; + } + + g_debug("size == %d", size); + g_debug(buffer); + + 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'; + + server_commands_handle(connection, buffer); + } + + return FALSE; +} + +gboolean server_start() { + ss = g_threaded_socket_service_new(10); + + g_socket_listener_add_inet_port((GSocketListener*)ss, 7681, NULL, NULL); + + g_signal_connect(ss, "incoming", (GCallback)service_incoming, NULL); + g_socket_service_start(ss); + + return TRUE; +} + +void server_stop() { + g_socket_service_stop(ss); + g_object_unref(ss); +} |