From 71ffb836260a9f80ab497da142a1bf09ea4de639 Mon Sep 17 00:00:00 2001 From: Gergely POLONKAI Date: Wed, 21 Mar 2012 16:07:02 +0100 Subject: [PATCH] Player data processing, minor changes * Added .anjuta and tags file to .gitignore * Added new client states * Moved client connection closing to a separate function * Added conditional processing of client buffer depending on client state * Created the basic player structure Signed-off-by: Gergely POLONKAI --- .gitignore | 2 ++ src/Makefile.am | 4 ++-- src/interpreter.c | 1 + src/networking.c | 59 +++++++++++++++++++++++++++++++++++------------ src/networking.h | 14 +++++++++-- src/players.c | 28 ++++++++++++++++++++++ src/players.h | 34 +++++++++++++++++++++++++++ 7 files changed, 123 insertions(+), 19 deletions(-) create mode 100644 src/players.c create mode 100644 src/players.h diff --git a/.gitignore b/.gitignore index bade499..246a272 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ stamp-h1 *~ *.exe *.db +*.anjuta +tags diff --git a/src/Makefile.am b/src/Makefile.am index 84fa832..b9bddec 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,5 @@ bin_PROGRAMS = wmud -AM_CFLAGS = $(MEMCACHED_CFLAGS) $(GLIB_CFLAGS) $(GIO_CFLAGS) $(GTHREAD_CFLAGS) $(SQLITE3_CFLAGS) +AM_CPPFLAGS = -DWMUD_STATEDIR=\""$(localstatedir)"\" -DWMUD_CONFDIR=\""$(sysconfdir)"\" $(MEMCACHED_CFLAGS) $(GLIB_CFLAGS) $(GIO_CFLAGS) $(GTHREAD_CFLAGS) $(SQLITE3_CFLAGS) -wmud_SOURCES = main.c networking.c interpreter.c db.c +wmud_SOURCES = main.c networking.c interpreter.c db.c players.c wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(SQLITE3_LIBS) diff --git a/src/interpreter.c b/src/interpreter.c index b2f7c4f..8de235b 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -167,5 +167,6 @@ wmud_interpreter_init(void) WMUD_COMMAND(gcmd_quit) { wmud_client_send(client, "Are you sure you want to get back to that freaky other reality? [y/N] "); + client->state = WMUD_CLIENT_STATE_QUITWAIT; } diff --git a/src/networking.c b/src/networking.c index d2c61f2..5c4189e 100644 --- a/src/networking.c +++ b/src/networking.c @@ -6,6 +6,7 @@ #include "main.h" #include "networking.h" #include "interpreter.h" +#include "players.h" #define MAX_RECV_LEN 1024 @@ -16,6 +17,24 @@ struct AcceptData { GSList *clients; +void +client_close(wmudClient *client, gboolean send_goodbye) +{ + GError *err = NULL; + if (send_goodbye) + { + /* TODO: Send some goodbye text */ + } + + g_print("Connection closed.\n"); + /* TODO: Error checking */ + g_socket_close(client->socket, &err); + clients = g_slist_remove(clients, client); + if (client->buffer) + g_string_free(client->buffer, TRUE); + g_free(client); +} + gboolean client_callback(GSocket *client, GIOCondition condition, wmudClient *client_data) { @@ -23,13 +42,7 @@ client_callback(GSocket *client, GIOCondition condition, wmudClient *client_data if (condition & G_IO_HUP) { - 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); - + client_close(client_data, FALSE); return FALSE; } else if ((condition & G_IO_IN) || (condition & G_IO_PRI)) @@ -41,13 +54,8 @@ client_callback(GSocket *client, GIOCondition condition, wmudClient *client_data /* TODO: Error checking */ if ((len = g_socket_receive(client, buf, MAX_RECV_LEN, NULL, &err)) == 0) { - g_print("Connection closed.\n"); - /* 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); + client_close(client_data, FALSE); return FALSE; } @@ -76,7 +84,24 @@ client_callback(GSocket *client, GIOCondition condition, wmudClient *client_data buf2 = n; } - wmud_interpret_game_command(client_data); + switch (client_data->state) + { + case WMUD_CLIENT_STATE_FRESH: + wmud_client_start_login(client_data); + break; + case WMUD_CLIENT_STATE_PASSWAIT: + wmud_player_auth(client_data); + break; + case WMUD_CLIENT_STATE_MENU: + //wmud_client_interpret_menu_command(client_data); + break; + case WMUD_CLIENT_STATE_INGAME: + wmud_interpret_game_command(client_data); + break; + case WMUD_CLIENT_STATE_QUITWAIT: + //wmud_interpret_quit_answer(client_data); + break; + } g_string_erase(client_data->buffer, 0, -1); for (; ((*buf2 == '\r') || (*buf2 == '\n')) && *buf2; buf2++); @@ -100,6 +125,10 @@ client_callback(GSocket *client, GIOCondition condition, wmudClient *client_data return TRUE; } +/* game_source_callback() + * + * This function is called whenever a new connection is available on the game socket + */ gboolean game_source_callback(GSocket *socket, GIOCondition condition, struct AcceptData *accept_data) { @@ -114,7 +143,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; + client_data->state = WMUD_CLIENT_STATE_FRESH; 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); diff --git a/src/networking.h b/src/networking.h index f663744..361e6d2 100644 --- a/src/networking.h +++ b/src/networking.h @@ -1,22 +1,32 @@ #ifndef __WMUD_NETWORKING_H__ # define __WMUD_NETWORKING_H__ -#include +#include #include typedef enum { - WMUD_CLIENT_STATE_MENU + WMUD_CLIENT_STATE_FRESH, /* Newly connected clients. We are waiting for + * a player name */ + WMUD_CLIENT_STATE_PASSWAIT, /* Player name entered, waiting for password */ + WMUD_CLIENT_STATE_MENU, /* Logged in players, waiting in the main menu. + * We are waiting for a menu item to be + * chosen.*/ + WMUD_CLIENT_STATE_INGAME, /* Player is in-game */ + WMUD_CLIENT_STATE_QUITWAIT /* Waiting for answer for the quit question */ } wmudClientState; typedef struct _wmudClient { GSocket *socket; GString *buffer; wmudClientState state; + gboolean authenticated; + wmudPlayer *player; } wmudClient; extern GSList *clients; gboolean wmud_networking_init(guint port_number); void wmud_client_send(wmudClient *client, const gchar *fmt, ...); +void wmud_client_start_login(wmudClient *client); #endif diff --git a/src/players.c b/src/players.c new file mode 100644 index 0000000..f3d8a60 --- /dev/null +++ b/src/players.c @@ -0,0 +1,28 @@ +/* + * players.c + * + * Copyright (C) 2012 - Gergely POLONKAI + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + +#include "networking.h" +#include "players.h" + +gboolean +wmud_player_auth(wmudClient *client) +{ + return FALSE; +} diff --git a/src/players.h b/src/players.h new file mode 100644 index 0000000..e844480 --- /dev/null +++ b/src/players.h @@ -0,0 +1,34 @@ +/* + * players.h + * + * Copyright (C) 2012 - Gergely POLONKAI + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef __WMUD_PLAYERS_H__ +#define __WMUD_PLAYERS_H__ + +#include + +#include "networking.h" + +typedef struct _wmudPlayer { + guint32 id; /* User ID */ + gchar *player_name; /* Player login name */ + gchar *cpassword; /* Crypted password */ +} wmudPlayer; + +gboolean wmud_player_auth(wmudClient *client); + +#endif /* __WMUD_PLAYERS_H__ */