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
This commit is contained in:
Polonkai Gergely 2012-03-14 19:03:54 +00:00
parent 742e8ede7f
commit be3e17fd07
2 changed files with 23 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#include <glib.h> #include <glib.h>
#include <gio/gio.h> #include <gio/gio.h>
#include <string.h> #include <string.h>
#include <stdarg.h>
#include "main.h" #include "main.h"
#include "networking.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 = g_new0(wmudClient, 1);
client_data->socket = client_socket; client_data->socket = client_socket;
client_data->buffer = g_string_new(""); client_data->buffer = g_string_new("");
client_data->state = WMUD_CLIENT_STATE_MENU;
clients = g_slist_prepend(clients, client_data); 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); 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; 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);
}

View File

@ -4,13 +4,19 @@
#include <glib.h> #include <glib.h>
#include <gio/gio.h> #include <gio/gio.h>
typedef enum {
WMUD_CLIENT_STATE_MENU
} wmudClientState;
typedef struct _wmudClient { typedef struct _wmudClient {
GSocket *socket; GSocket *socket;
GString *buffer; GString *buffer;
wmudClientState state;
} wmudClient; } wmudClient;
extern GSList *clients; extern GSList *clients;
gboolean wmud_networking_init(guint port_number); gboolean wmud_networking_init(guint port_number);
void wmud_client_send(wmudClient *client, const gchar *fmt, ...);
#endif #endif