From 21a7603f4c4d66549ec47b578b7cf4a66223a2b8 Mon Sep 17 00:00:00 2001 From: Polonkai Gergely Date: Mon, 12 Mar 2012 22:52:43 +0000 Subject: [PATCH] Created the wmudClient struct Created the wmudClient struct to store data of connected clients. --- src/networking.c | 20 ++++++++++++++++++-- src/networking.h | 7 +++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/networking.c b/src/networking.c index c00eafb..11f2ed4 100644 --- a/src/networking.c +++ b/src/networking.c @@ -2,6 +2,7 @@ #include #include "main.h" +#include "networking.h" #define MAX_RECV_LEN 1024 @@ -10,8 +11,10 @@ struct AcceptData { GSocketListener *listener; }; +GSList *clients; + gboolean -client_callback(GSocket *client, GIOCondition condition, struct AcceptData *accept_data) +client_callback(GSocket *client, GIOCondition condition, wmudClient *client_data) { GError *err = NULL; @@ -20,6 +23,9 @@ client_callback(GSocket *client, GIOCondition condition, struct AcceptData *acce g_print("Connection closed.\n"); /* TODO: Error checking */ g_socket_close(client, &err); + clients = g_slist_remove(clients, client_data); + if (client_data->buffer) + g_string_free(client_data->buffer, TRUE); return FALSE; } @@ -35,6 +41,9 @@ client_callback(GSocket *client, GIOCondition condition, struct AcceptData *acce /* TODO: Error checking */ g_socket_close(client, &err); g_free(buf); + clients = g_slist_remove(clients, client_data); + if (client_data->buffer) + g_string_free(client_data->buffer, TRUE); return FALSE; } g_print("Client data arrived (%d bytes): \"%s\"\n", len, buf); @@ -50,11 +59,17 @@ game_source_callback(GSocket *socket, GIOCondition condition, struct AcceptData GSocket *client_socket; GSource *client_source; GError *err = NULL; + wmudClient *client_data; /* TODO: Error checking */ client_socket = g_socket_listener_accept_socket(accept_data->listener, NULL, NULL, &err); + + client_data = g_new0(wmudClient, 1); + client_data->socket = client_socket; + clients = g_slist_prepend(clients, client_data); + client_source = g_socket_create_source(client_socket, G_IO_IN | G_IO_OUT | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL, NULL); - g_source_set_callback(client_source, (GSourceFunc)client_callback, client_socket, NULL); + g_source_set_callback(client_source, (GSourceFunc)client_callback, client_data, NULL); g_source_attach(client_source, accept_data->context); g_print("New connection.\n"); @@ -73,6 +88,7 @@ wmud_networking_init(guint port_number) GSource *game_net_source4 = NULL, *game_net_source6 = NULL; + clients = NULL; game_listener = g_socket_listener_new(); /* The following snippet is borrowed from GLib 2.30's gsocketlistener.c diff --git a/src/networking.h b/src/networking.h index 13c3c2f..6d71e6d 100644 --- a/src/networking.h +++ b/src/networking.h @@ -1,6 +1,13 @@ #ifndef __WMUD_NETWORKING_H__ # define __WMUD_NETWORKING_H__ +typedef struct _wmudClient { + GSocket *socket; + GString *buffer; +} wmudClient; + +extern GSList *clients; + gboolean wmud_networking_init(guint port_number); #endif