Moved the game thread to game.c
Signed-off-by: Gergely POLONKAI <polesz@w00d5t0ck.info>
This commit is contained in:
parent
a391b1e8f6
commit
f8b5feb361
@ -1,5 +1,5 @@
|
||||
bin_PROGRAMS = wmud
|
||||
AM_CPPFLAGS = -DWMUD_STATEDIR=\""$(localstatedir)"\" -DWMUD_CONFDIR=\""$(sysconfdir)"\" $(MEMCACHED_CFLAGS) $(GLIB_CFLAGS) $(GIO_CFLAGS) $(GTHREAD_CFLAGS) $(SQLITE3_CFLAGS)
|
||||
|
||||
wmud_SOURCES = main.c game-networking.c interpreter.c db.c players.c maintenance.c
|
||||
wmud_SOURCES = main.c game-networking.c interpreter.c db.c players.c maintenance.c game.c
|
||||
wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(SQLITE3_LIBS)
|
||||
|
@ -268,7 +268,7 @@ game_source_callback(GSocket *socket, GIOCondition condition, struct AcceptData
|
||||
* err is set accordingly (if not NULL)
|
||||
*/
|
||||
gboolean
|
||||
wmud_networking_init(guint port_number, GError **err)
|
||||
wmud_networking_init(guint port_number, GMainContext *game_context, GError **err)
|
||||
{
|
||||
struct AcceptData *accept_data;
|
||||
GSocketListener *game_listener;
|
||||
|
@ -50,7 +50,7 @@
|
||||
|
||||
extern GSList *clients;
|
||||
|
||||
gboolean wmud_networking_init(guint port_number, GError **err);
|
||||
gboolean wmud_networking_init(guint port_number, GMainContext *game_context, GError **err);
|
||||
void wmud_client_send(wmudClient *client, const gchar *fmt, ...);
|
||||
void wmud_client_start_login(wmudClient *client);
|
||||
void wmud_client_interpret_newplayer_answer(wmudClient *client);
|
||||
|
86
src/game.c
Normal file
86
src/game.c
Normal file
@ -0,0 +1,86 @@
|
||||
#include <glib.h>
|
||||
|
||||
#include "main.h"
|
||||
#include "game.h"
|
||||
|
||||
/**
|
||||
* elapsed_seconds:
|
||||
*
|
||||
* the number of seconds elapsed since game boot. May be inaccurate, as it
|
||||
* simply gets updated by a timeout function which should run every second
|
||||
*/
|
||||
guint32 elapsed_seconds = 0;
|
||||
/**
|
||||
* elapsed_cycle:
|
||||
*
|
||||
* yes, I'm optimistic. This counter is increased if, for some reason,
|
||||
* #elapsed_seconds reaches the maximum value
|
||||
*/
|
||||
guint32 elapsed_cycle = 0;
|
||||
/**
|
||||
*
|
||||
* rl_sec_elapsed:
|
||||
* @user_data: non-used pointer to callback's user data
|
||||
*
|
||||
* Keeps track of elapsed real-world time. It is inaccurate by design, but it
|
||||
* doesn't actually matter.
|
||||
*/
|
||||
gboolean
|
||||
rl_sec_elapsed(gpointer user_data)
|
||||
{
|
||||
elapsed_seconds++;
|
||||
if (elapsed_seconds == G_MAXUINT32)
|
||||
{
|
||||
elapsed_seconds = 0;
|
||||
elapsed_cycle++;
|
||||
}
|
||||
|
||||
if (elapsed_seconds % 30 == 0)
|
||||
{
|
||||
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Heartbeat");
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* game_thread_func:
|
||||
* @game_loop: the main loop to be associated with the game thread
|
||||
*
|
||||
* The game thread's main function.
|
||||
*
|
||||
* Return value: This function always returns %NULL.
|
||||
*/
|
||||
gpointer
|
||||
game_thread_func(GMainLoop *game_loop)
|
||||
{
|
||||
/* Run the game loop */
|
||||
g_main_loop_run(game_loop);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gboolean
|
||||
wmud_game_init(GThread **game_thread, GMainContext **game_context)
|
||||
{
|
||||
GMainLoop *game_loop;
|
||||
GSource *timeout_source;
|
||||
GError *err = NULL;
|
||||
|
||||
/* Create the game context and main loop */
|
||||
*game_context = g_main_context_new();
|
||||
game_loop = g_main_loop_new(*game_context, FALSE);
|
||||
|
||||
/* Create the timeout source which keeps track of elapsed real-world
|
||||
* time */
|
||||
timeout_source = g_timeout_source_new(1000);
|
||||
g_source_set_callback(timeout_source, rl_sec_elapsed, NULL, NULL);
|
||||
g_source_attach(timeout_source, *game_context);
|
||||
g_source_unref(timeout_source);
|
||||
|
||||
g_clear_error(&err);
|
||||
*game_thread = g_thread_create((GThreadFunc)game_thread_func, game_loop, TRUE, &err);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
7
src/game.h
Normal file
7
src/game.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef __WMUD_GAME_H__
|
||||
#define __WMUD_GAME_H__
|
||||
|
||||
gboolean wmud_game_init(GThread **game_thread, GMainContext **game_context);
|
||||
|
||||
#endif /* __WMUD_GAME_H__ */
|
||||
|
95
src/main.c
95
src/main.c
@ -33,6 +33,7 @@
|
||||
#include "db.h"
|
||||
#include "players.h"
|
||||
#include "maintenance.h"
|
||||
#include "game.h"
|
||||
|
||||
/**
|
||||
* SECTION:utils
|
||||
@ -51,26 +52,6 @@ struct {
|
||||
int line;
|
||||
} debug_context_loc = {NULL, 0};
|
||||
|
||||
/**
|
||||
* game_context:
|
||||
*
|
||||
* the game thread's main context
|
||||
*/
|
||||
GMainContext *game_context;
|
||||
/**
|
||||
* elapsed_seconds:
|
||||
*
|
||||
* the number of seconds elapsed since game boot. May be inaccurate, as it
|
||||
* simply gets updated by a timeout function which should run every second
|
||||
*/
|
||||
guint32 elapsed_seconds = 0;
|
||||
/**
|
||||
* elapsed_cycle:
|
||||
*
|
||||
* yes, I'm optimistic. This counter is increased if, for some reason,
|
||||
* #elapsed_seconds reaches the maximum value
|
||||
*/
|
||||
guint32 elapsed_cycle = 0;
|
||||
/**
|
||||
* main_rand:
|
||||
*
|
||||
@ -108,31 +89,6 @@ gchar *database_file = NULL;
|
||||
*/
|
||||
gchar *admin_email = NULL;
|
||||
|
||||
/**
|
||||
* rl_sec_elapsed:
|
||||
* @user_data: non-used pointer to callback's user data
|
||||
*
|
||||
* Keeps track of elapsed real-world time. It is inaccurate by design, but it
|
||||
* doesn't actually matter.
|
||||
*/
|
||||
gboolean
|
||||
rl_sec_elapsed(gpointer user_data)
|
||||
{
|
||||
elapsed_seconds++;
|
||||
if (elapsed_seconds == G_MAXUINT32)
|
||||
{
|
||||
elapsed_seconds = 0;
|
||||
elapsed_cycle++;
|
||||
}
|
||||
|
||||
if (elapsed_seconds % 30 == 0)
|
||||
{
|
||||
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Heartbeat");
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* wmud_random_string:
|
||||
* @len: the desired length of the generated random string
|
||||
@ -291,23 +247,6 @@ wmud_config_init(GError **err)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* game_thread_func:
|
||||
* @game_loop: the main loop to be associated with the game thread
|
||||
*
|
||||
* The game thread's main function.
|
||||
*
|
||||
* Return value: This function always returns %NULL.
|
||||
*/
|
||||
gpointer
|
||||
game_thread_func(GMainLoop *game_loop)
|
||||
{
|
||||
/* Run the game loop */
|
||||
g_main_loop_run(game_loop);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* main:
|
||||
* @argc: The number of arguments on the command line
|
||||
@ -318,10 +257,9 @@ game_thread_func(GMainLoop *game_loop)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
GMainLoop *game_loop;
|
||||
GSource *timeout_source;
|
||||
GError *err = NULL;
|
||||
GThread *game_thread;
|
||||
GMainContext *game_context;
|
||||
|
||||
/* Initialize the thread and type system */
|
||||
g_thread_init(NULL);
|
||||
@ -333,17 +271,6 @@ main(int argc, char **argv)
|
||||
/* Initialize random number generator */
|
||||
main_rand = g_rand_new();
|
||||
|
||||
/* Create the game context and main loop */
|
||||
game_context = g_main_context_new();
|
||||
game_loop = g_main_loop_new(game_context, FALSE);
|
||||
|
||||
/* Create the timeout source which keeps track of elapsed real-world
|
||||
* time */
|
||||
timeout_source = g_timeout_source_new(1000);
|
||||
g_source_set_callback(timeout_source, rl_sec_elapsed, NULL, NULL);
|
||||
g_source_attach(timeout_source, game_context);
|
||||
g_source_unref(timeout_source);
|
||||
|
||||
/* TODO: Create signal handlers! */
|
||||
|
||||
if (!wmud_config_init(&err))
|
||||
@ -377,8 +304,16 @@ main(int argc, char **argv)
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
g_clear_error(&err);
|
||||
if (!wmud_networking_init(port, &err))
|
||||
wmud_db_players_load(&err);
|
||||
|
||||
/* Initialization ends here */
|
||||
|
||||
wmud_game_init(&game_thread, &game_context);
|
||||
|
||||
g_clear_error(&err);
|
||||
if (!wmud_networking_init(port, game_context, &err))
|
||||
{
|
||||
if (err)
|
||||
{
|
||||
@ -392,14 +327,6 @@ main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
g_clear_error(&err);
|
||||
wmud_db_players_load(&err);
|
||||
|
||||
/* Initialization ends here */
|
||||
|
||||
g_clear_error(&err);
|
||||
game_thread = g_thread_create((GThreadFunc)game_thread_func, game_loop, TRUE, &err);
|
||||
|
||||
wmud_maintenance_init();
|
||||
|
||||
/* Initialize other threads here */
|
||||
|
Loading…
Reference in New Issue
Block a user