From c7147f5ccc6d0fa4a019c54166085a36f86a1bf0 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 23 Aug 2010 20:07:01 +0200 Subject: Moved server_sync() to server_communication.c. --- communication_source.c | 1 + server_communication.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ server_communication.h | 10 +++++++++ servers.c | 46 ------------------------------------- 4 files changed, 72 insertions(+), 46 deletions(-) create mode 100644 server_communication.c create mode 100644 server_communication.h diff --git a/communication_source.c b/communication_source.c index b0a03cb..d2e7179 100644 --- a/communication_source.c +++ b/communication_source.c @@ -1,5 +1,6 @@ #include "communication_source.h" #include "servers.h" +#include "server_communication.h" static GQueue *jobqueue = NULL; diff --git a/server_communication.c b/server_communication.c new file mode 100644 index 0000000..227eb8f --- /dev/null +++ b/server_communication.c @@ -0,0 +1,61 @@ +#include "server_communication.h" + +#include + +gboolean server_sync(struct server *server) { + GError *error = NULL, *conn_error = NULL; + GSocketConnectable *addr; + GSocketAddressEnumerator *enumerator; + 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); + g_object_unref(addr); + + client = g_socket_client_new(); + + while(conn == NULL && (sockaddr = g_socket_address_enumerator_next(enumerator, NULL, &error))) { + conn = g_socket_client_connect(client, (GSocketConnectable*)sockaddr, NULL, &conn_error); + g_object_unref(sockaddr); + } + + if(sockaddr == NULL) { + if(error != NULL) { + g_warning(error->message); + g_error_free(error); + } else { + g_warning("server_sync: no connectable addresses found for host %s", + server->host); + } + return FALSE; + } + + if(conn == NULL) { + g_warning(conn_error->message); + g_error_free(conn_error); + return FALSE; + } + + GSocket *socket = g_socket_connection_get_socket(conn); + + g_debug("connected"); + + 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); + } + + g_timer_destroy(timer); + + g_socket_close(socket, NULL); + + return TRUE; +} + diff --git a/server_communication.h b/server_communication.h new file mode 100644 index 0000000..17c1b89 --- /dev/null +++ b/server_communication.h @@ -0,0 +1,10 @@ +#ifndef SERVER_COMMUNICATION_H +#define SERVER_COMMUNICATION_H + +#include "servers.h" + +#include + +gboolean server_sync(struct server *server); + +#endif diff --git a/servers.c b/servers.c index 94bbaf4..890f6b1 100644 --- a/servers.c +++ b/servers.c @@ -5,52 +5,6 @@ GSList *servers = NULL; -gboolean server_sync(struct server *server) { - GError *error = NULL; - GSocketConnectable *addr; - GSocketAddressEnumerator *enumerator; - 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); - g_object_unref(addr); - - client = g_socket_client_new(); - - while(conn == NULL && (sockaddr = g_socket_address_enumerator_next(enumerator, NULL, &error))) { - conn = g_socket_client_connect(client, (GSocketConnectable*)sockaddr, NULL, &error); - g_object_unref(sockaddr); - } - - if(conn == NULL) { - g_warning(error->message); - g_error_free(error); - return FALSE; - } - - GSocket *socket = g_socket_connection_get_socket(conn); - - g_debug("connected"); - - 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); - } - - g_timer_destroy(timer); - - g_socket_close(socket, NULL); - - return TRUE; -} - gboolean server_add(const gchar *host, const guint16 port) { struct server *server = g_new0(struct server, 1); -- cgit v1.2.3