diff options
-rw-r--r-- | communication_source.c | 2 | ||||
-rw-r--r-- | server_communication.c | 17 | ||||
-rw-r--r-- | server_communication.h | 2 |
3 files changed, 11 insertions, 10 deletions
diff --git a/communication_source.c b/communication_source.c index d2e7179..509366f 100644 --- a/communication_source.c +++ b/communication_source.c @@ -22,7 +22,7 @@ gboolean comsrc_check(GSource *source) { gboolean comsrc_dispatch(GSource *source, GSourceFuncs callback, gpointer user_data) { g_debug("comsrc_dispatch(), callback is %x, user_data is %x", callback, user_data); struct server *server = g_queue_pop_tail(jobqueue); - server_sync(server); + server_ping(server); return TRUE; } diff --git a/server_communication.c b/server_communication.c index f2f5946..ee854d3 100644 --- a/server_communication.c +++ b/server_communication.c @@ -1,6 +1,7 @@ #include "server_communication.h" #include <gio/gio.h> +#include <string.h> static GSocket *server_connect(struct server *server) { GError *error = NULL, *conn_error = NULL; @@ -9,7 +10,6 @@ static GSocket *server_connect(struct server *server) { GSocketAddress *sockaddr; GSocketConnection *conn = NULL; GSocketClient *client; - GTimer *timer = g_timer_new(); addr = g_network_address_new(server->host, server->port); enumerator = g_socket_connectable_enumerate(addr); @@ -46,26 +46,27 @@ static GSocket *server_connect(struct server *server) { return socket; } -gboolean server_sync(struct server *server) { +gboolean server_ping(struct server *server) { + GTimer *timer = g_timer_new(); GSocket *socket = server_connect(server); if(socket == NULL) { return FALSE; } + gboolean result = FALSE; g_socket_send(socket, "ping\n", 5, NULL, NULL); gchar buffer[0x400]; - g_socket_receive(socket, buffer, 0x400, NULL, NULL); - if(buffer[4] == '\n') { - buffer[4] = '\0'; - g_debug("got response: %s", buffer); - g_debug("time: %f ms", g_timer_elapsed(timer, NULL) * 1000); + gssize size = g_socket_receive(socket, buffer, 0x400, NULL, NULL); + if(size == 5 && strncmp(buffer, "pong", 4) == 0) { + g_debug("ping response: %f ms", g_timer_elapsed(timer, NULL) * 1000); + result = TRUE; } g_timer_destroy(timer); g_socket_close(socket, NULL); - return TRUE; + return result; } diff --git a/server_communication.h b/server_communication.h index 17c1b89..552d12e 100644 --- a/server_communication.h +++ b/server_communication.h @@ -5,6 +5,6 @@ #include <glib.h> -gboolean server_sync(struct server *server); +gboolean server_ping(struct server *server); #endif |