summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-08-30 23:21:39 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2010-08-30 23:21:39 +0200
commit56761cf66047107f3459424d8b37bb41d40ccdf4 (patch)
treef97ae4f777198a9399edb64a6ebeec32a217366e
parent651ebc75e6dae751bdebffe018701618afcc49df (diff)
Use GDataInputStream to read data in the control and command services.
-rw-r--r--command_service.c42
-rw-r--r--control_service.c27
2 files changed, 28 insertions, 41 deletions
diff --git a/command_service.c b/command_service.c
index 5ef7fed..1174687 100644
--- a/command_service.c
+++ b/command_service.c
@@ -11,38 +11,28 @@ static gboolean service_incoming(GSocketService *service,
gpointer user_data) {
GError *error = NULL;
GSocket *socket = g_socket_connection_get_socket(connection);
- gchar buffer[0x400];
+ GDataInputStream *input = g_data_input_stream_new(g_io_stream_get_input_stream((GIOStream*)connection));
+ g_data_input_stream_set_newline_type(input, G_DATA_STREAM_NEWLINE_TYPE_LF);
+ gchar *line;
+ gsize size;
- while(g_socket_is_connected(socket) == TRUE) {
- gssize size = g_socket_receive(socket, buffer, 0x400, NULL, &error);
+ while(g_socket_is_connected(socket) == TRUE &&
+ (line = g_data_input_stream_read_line(input, &size, NULL, &error)) != NULL) {
+ commands_handle(connection, line, &error);
- if(size < 0) {
+ if(error != NULL) {
g_warning(error->message);
+ gchar *error_buffer = g_strdup_printf("%s\n", error->message);
+ g_socket_send(socket, error_buffer, strlen(error_buffer), NULL, NULL);
+ g_free(error_buffer);
g_error_free(error);
- return FALSE;
- } else if(size == 0) {
- break;
+ error = NULL;
}
+ }
- gchar *pos, *eol;
- pos = buffer;
- while((eol = g_strstr_len(pos, size, "\n")) != NULL || (eol = g_strstr_len(pos, size, "\r")) != NULL) {
- *eol = '\0';
-
- commands_handle(connection, pos, &error);
-
- if(error != NULL) {
- g_warning(error->message);
- gchar *error_buffer = g_strdup_printf("%s\n", error->message);
- g_socket_send(socket, error_buffer, strlen(error_buffer), NULL, NULL);
- g_free(error_buffer);
- g_error_free(error);
- error = NULL;
- }
-
- size -= eol - pos + 1;
- pos = eol + 1;
- }
+ if(error != NULL) {
+ g_warning(error->message);
+ g_error_free(error);
}
return FALSE;
diff --git a/control_service.c b/control_service.c
index a496812..097ae88 100644
--- a/control_service.c
+++ b/control_service.c
@@ -15,23 +15,15 @@ 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];
- gssize size;
-
- while((size = g_socket_receive(socket, buffer, 0x400, NULL, NULL)) > 0) {
- 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';
+ GDataInputStream *input = g_data_input_stream_new(g_io_stream_get_input_stream((GIOStream*)connection));
+ g_data_input_stream_set_newline_type(input, G_DATA_STREAM_NEWLINE_TYPE_LF);
+ gchar *line;
+ gsize size;
- commands_handle(connection, buffer, &error);
+ while(g_socket_is_connected(socket) == TRUE &&
+ (line = g_data_input_stream_read_line(input, &size, NULL, &error)) != NULL) {
+ commands_handle(connection, line, &error);
if(error != NULL) {
g_warning(error->message);
@@ -43,6 +35,11 @@ static gboolean service_incoming(GSocketService *service,
}
}
+ if(error != NULL) {
+ g_warning(error->message);
+ g_error_free(error);
+ }
+
return FALSE;
}