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 <polesz@w00d5t0ck.info>
This commit is contained in:
Gergely POLONKAI 2012-03-21 16:07:02 +01:00
parent 98967395c4
commit 71ffb83626
7 changed files with 123 additions and 19 deletions

2
.gitignore vendored
View File

@ -15,3 +15,5 @@ stamp-h1
*~
*.exe
*.db
*.anjuta
tags

View File

@ -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)

View File

@ -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;
}

View File

@ -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);

View File

@ -1,22 +1,32 @@
#ifndef __WMUD_NETWORKING_H__
# define __WMUD_NETWORKING_H__
#include <glib.h>
#include <glib.h>
#include <gio/gio.h>
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

28
src/players.c Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include "networking.h"
#include "players.h"
gboolean
wmud_player_auth(wmudClient *client)
{
return FALSE;
}

34
src/players.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef __WMUD_PLAYERS_H__
#define __WMUD_PLAYERS_H__
#include <glib.h>
#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__ */