From be3e17fd074693131d22503a6d01019e95f204fc Mon Sep 17 00:00:00 2001 From: Polonkai Gergely Date: Wed, 14 Mar 2012 19:03:54 +0000 Subject: [PATCH] Added wmud_client_send() and a client state field. * The wmudCLient struct now has a state field, which indicates the current state of the client (we are waiting her to log in, or to enter a command, etc.) * Created the wmud_client_send() function to send formatted strings to a client --- src/networking.c | 17 +++++++++++++++++ src/networking.h | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/src/networking.c b/src/networking.c index 30f6a46..d2c61f2 100644 --- a/src/networking.c +++ b/src/networking.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "main.h" #include "networking.h" @@ -113,6 +114,7 @@ game_source_callback(GSocket *socket, GIOCondition condition, struct AcceptData client_data = g_new0(wmudClient, 1); client_data->socket = client_socket; client_data->buffer = g_string_new(""); + client_data->state = WMUD_CLIENT_STATE_MENU; 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); @@ -235,3 +237,18 @@ wmud_networking_init(guint port_number) return TRUE; } +void +wmud_client_send(wmudClient *client, const gchar *fmt, ...) +{ + va_list ap; + GString *buf = g_string_new(""); + + va_start(ap, fmt); + g_string_vprintf(buf, fmt, ap); + va_end(ap); + + /* TODO: error checking */ + g_socket_send(client->socket, buf->str, buf->len, NULL, NULL); + g_string_free(buf, TRUE); +} + diff --git a/src/networking.h b/src/networking.h index 91ba287..f663744 100644 --- a/src/networking.h +++ b/src/networking.h @@ -4,13 +4,19 @@ #include #include +typedef enum { + WMUD_CLIENT_STATE_MENU +} wmudClientState; + typedef struct _wmudClient { GSocket *socket; GString *buffer; + wmudClientState state; } wmudClient; extern GSList *clients; gboolean wmud_networking_init(guint port_number); +void wmud_client_send(wmudClient *client, const gchar *fmt, ...); #endif