summaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
Diffstat (limited to 'src/server')
-rw-r--r--src/server/CMakeLists.txt7
-rw-r--r--src/server/highScore.c87
-rw-r--r--src/server/highScore.h11
-rw-r--r--src/server/log.c79
-rw-r--r--src/server/log.h13
-rw-r--r--src/server/publicServer.c450
-rw-r--r--src/server/publicServer.h18
-rw-r--r--src/server/serverConfigFile.c94
-rw-r--r--src/server/serverConfigFile.h8
9 files changed, 0 insertions, 767 deletions
diff --git a/src/server/CMakeLists.txt b/src/server/CMakeLists.txt
deleted file mode 100644
index 2e93cef..0000000
--- a/src/server/CMakeLists.txt
+++ /dev/null
@@ -1,7 +0,0 @@
-###############################################################################
-# tuxanci/src/*/CMakeLists.txt
-###############################################################################
-# Generate source_code files from .c files we find in directory
-###############################################################################
-MacroAddSources()
-###############################################################################
diff --git a/src/server/highScore.c b/src/server/highScore.c
deleted file mode 100644
index 75323fa..0000000
--- a/src/server/highScore.c
+++ /dev/null
@@ -1,87 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "main.h"
-#include "list.h"
-#include "textFile.h"
-#include "highScore.h"
-
-static textFile_t *textFile;
-
-void high_score_init(char *file)
-{
- int i;
-
- textFile = text_file_load(file);
-
- if (textFile == NULL) {
- error("Unable to load high score [%s]", file);
- debug("Creating high score file [%s]", file);
- textFile = text_file_new(file);
- } else {
- debug("Loading high score file [%s]", file);
- return;
- }
-
- if (textFile == NULL) {
- error("Unable to create high score file [%s]", file);
- return;
- }
-
- for (i = 0; i < HIGHSCORE_MAX_PLAYERS; i++) {
- list_add(textFile->text, strdup("--- 0"));
- }
-
- text_file_save(textFile);
-}
-
-int table_add(char *name, int score)
-{
- int i;
-
- if (score <= 0) {
- return -1; /* ha ha ha */
- }
-
- for (i = 0; i < HIGHSCORE_MAX_PLAYERS; i++) {
- char *line;
- char thisName[STR_NAME_SIZE];
- int thisCore;
-
- line = (char *) textFile->text->list[i];
- sscanf(line, "%s %d", thisName, &thisCore);
-
- if (score >= thisCore) {
- char new[STR_SIZE];
-
- sprintf(new, "%s %d", name, score);
- list_ins(textFile->text, i, strdup(new));
- list_del_item(textFile->text, HIGHSCORE_MAX_PLAYERS, free);
- /*text_file_print(textFile);*/
- text_file_save(textFile);
-
- return 0;
- }
- }
-
- return -1;
-}
-
-char *high_score_get_table(int index)
-{
- if (textFile != NULL && index >= 0 && index < textFile->text->count) {
- return (char *) textFile->text->list[index];
- }
-
- return NULL;
-}
-
-void high_score_quit()
-{
- if (textFile != NULL) {
- text_file_save(textFile);
- text_file_destroy(textFile);
- }
-}
diff --git a/src/server/highScore.h b/src/server/highScore.h
deleted file mode 100644
index 604afb7..0000000
--- a/src/server/highScore.h
+++ /dev/null
@@ -1,11 +0,0 @@
-#ifndef HIGH_SCORE_H
-#define HIGH_SCORE_H
-
-#define HIGHSCORE_MAX_PLAYERS 100
-
-extern void high_score_init(char *file);
-extern int table_add(char *name, int score);
-extern char *high_score_get_table(int index);
-extern void high_score_quit();
-
-#endif /* HIGH_SCORE_H */
diff --git a/src/server/log.c b/src/server/log.c
deleted file mode 100644
index 00cde64..0000000
--- a/src/server/log.c
+++ /dev/null
@@ -1,79 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <time.h>
-#include <assert.h>
-
-#include "log.h"
-#include "main.h"
-
-static FILE *logFile;
-
-int log_init(char *name)
-{
- logFile = fopen(name, "a");
-
- if (logFile == NULL) {
- error("Unable to open log file [%s]", name);
- return -1;
- }
-
- debug("Using log file [%s]", name);
-
- log_add(LOG_INF, "Logging started");
-
- return 0;
-}
-
-void log_add(int type, char *msg)
-{
- char str[STR_LOG_SIZE];
- struct tm *tm_struct;
- time_t currentTime;
- char *str_type;
-
- if (logFile == NULL) {
- return;
- }
-
- currentTime = time(NULL);
- tm_struct = localtime(&currentTime);
-
- switch (type) {
- case LOG_INF:
- str_type = "INFO";
- break;
- case LOG_DBG:
- str_type = "DEBUG";
- break;
- case LOG_WRN:
- str_type = "WARN";
- break;
- case LOG_ERR:
- str_type = "ERROR";
- break;
- default:
- fatal("Unknown type of the logging string");
- break;
- }
-
- sprintf(str, "[%02d-%02d-%02d %02d:%02d:%02d] [%s] %s\n",
- 1900 + tm_struct->tm_year, tm_struct->tm_mon + 1, tm_struct->tm_mday,
- tm_struct->tm_hour, tm_struct->tm_min, tm_struct->tm_sec,
- str_type, msg);
-
- fprintf(logFile, "%s", str);
-
- fflush(logFile);
-}
-
-void log_quit()
-{
- if (logFile == NULL) {
- return;
- }
-
- log_add(LOG_INF, "Logging finished");
- fclose(logFile);
-}
diff --git a/src/server/log.h b/src/server/log.h
deleted file mode 100644
index b256e08..0000000
--- a/src/server/log.h
+++ /dev/null
@@ -1,13 +0,0 @@
-#ifndef LOG_H
-#define LOG_H
-
-#define LOG_INF 1
-#define LOG_DBG 2
-#define LOG_WRN 3
-#define LOG_ERR 4
-
-extern int log_init(char *name);
-extern void log_add(int type, char *msg);
-extern void log_quit();
-
-#endif /* LOG_H */
diff --git a/src/server/publicServer.c b/src/server/publicServer.c
deleted file mode 100644
index ba97f7d..0000000
--- a/src/server/publicServer.c
+++ /dev/null
@@ -1,450 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-#include <signal.h>
-#include <time.h>
-
-#ifndef __WIN32__
-#include <sys/socket.h>
-#include <sys/select.h>
-#include <arpa/inet.h>
-#else /* __WIN32__ */
-#include <windows.h>
-#include <wininet.h>
-#endif /* __WIN32__ */
-
-#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-
-#include "main.h"
-#include "list.h"
-#include "tux.h"
-#include "idManager.h"
-#include "myTimer.h"
-#include "arena.h"
-#include "item.h"
-#include "shot.h"
-#include "arenaFile.h"
-#include "proto.h"
-#include "modules.h"
-#include "net_multiplayer.h"
-
-#include "publicServer.h"
-#include "serverConfigFile.h"
-#include "highScore.h"
-#include "log.h"
-
-#include "dns.h"
-
-static arena_t *arena;
-static bool_t isSignalEnd;
-static arenaFile_t *choice_arenaFile;
-
-extern int errno;
-
-#define __PACKED__ __attribute__ ((__packed__))
-
-void world_inc_round()
-{
-}
-
-char *public_server_get_setting(char *env, char *param, char *default_val)
-{
- return getParamElse(param, server_configFile_get_value(env, default_val));
-}
-
-/**
- * Send the process to the background
- */
-static void daemonize()
-{
-#ifndef __WIN32__
- pid_t pid, sid;
-
- FILE *pid_file;
-
- /* are we already a daemon? */
- if (getppid() == 1) {
- return;
- }
-
- /* fork off the parent process */
- pid = fork();
- if (pid < 0) {
- exit(1);
- /* if we got a good PID, then we can exit the parent process */
- } else if (pid > 0) {
- exit(0);
- }
-
- /* ↓ at this point we are executing as the child process ↓ */
-
- /* change the file mode mask */
- umask(027);
-
- /* create a new SID for the child process */
- sid = setsid();
- if (sid < 0) {
- exit(1);
- }
-
- /* Change the current working directory. This prevents the current
- * directory from being locked; hence not being able to remove it.
- * /tmp seems to be the best place where to create temporary files ;c)
- */
- if (chdir("/tmp") < 0) {
- exit(1);
- }
-
- /* say loudly what PID we got */
- printf("The Tuxanci game server started with the PID %d\n", sid);
-
- /* write the PID to the PID file */
- pid_file = fopen(public_server_get_setting("PID_FILE", "--pid-file", "/var/run/tuxanci-server.pid"), "w");
- fprintf(pid_file, "%d\n", sid);
- fclose(pid_file);
-
- /* redirect standard files to /dev/null */
- freopen("/dev/null", "r", stdin);
- freopen("/dev/null", "w", stdout);
- freopen("/dev/null", "w", stderr);
-#endif /* __WIN32__ */
-}
-
-/**
- * Tell MasterServer that we want to be propagated
- */
-static int public_server_register()
-{
-#ifndef __WIN32__
- int s;
-#else /* __WIN32__ */
- SOCKET s;
-#endif /* __WIN32__ */
-
- struct sockaddr_in server;
- char *master_server_ip;
-
- master_server_ip = dns_resolv(NET_MASTER_SERVER_DOMAIN);
-
- if (master_server_ip == NULL) {
- warning("Did not obtained IP of the MasterServer");
- return -1;
- }
-
- server.sin_family = AF_INET;
- server.sin_port = htons(NET_MASTER_PORT);
- server.sin_addr.s_addr = inet_addr(master_server_ip);
-
- free(master_server_ip);
-
- if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
- warning("Unable to open socket for connection to MasterServer");
- return -1;
- }
-
- /* connect to MasterServer */
- if (connect(s, (struct sockaddr *) &server, sizeof(server)) < 0) {
- warning("Unable to estabilish connection to MasterServer");
- return -1;
- }
-
- typedef struct {
- unsigned char cmd;
- unsigned port;
- unsigned ip;
- } __PACKED__ register_head;
-
- register_head *head = (register_head *) malloc(sizeof(register_head));
-
- head->cmd = 'p';
- head->port = atoi(public_server_get_setting("PORT4", "--port4", "6800"));
- head->ip = 0; /* TODO */
-
- /* send request for server list */
- int r = send(s, head, sizeof(register_head), 0);
-
- free(head);
-
-#ifndef __WIN32__
- close(s);
-#else /* __WIN32__ */
- closesocket(s);
-#endif /* __WIN32__ */
-
- if (r == -1) {
- return -1;
- } else {
- return 0;
- }
-}
-
-/**
- * Tell MasterServer that we are shutting down
- */
-static int public_server_unregister()
-{
-#ifndef __WIN32__
- int s;
-#else /* __WIN32__ */
- SOCKET s;
-#endif /* __WIN32__ */
-
- struct sockaddr_in server;
- char *master_server_ip;
-
- master_server_ip = dns_resolv(NET_MASTER_SERVER_DOMAIN);
-
- if (master_server_ip == NULL) {
- warning("Did not obtained IP of the MasterServer");
- return -1;
- }
-
- server.sin_family = AF_INET;
- server.sin_port = htons(NET_MASTER_PORT);
- server.sin_addr.s_addr = inet_addr(master_server_ip);
-
- free(master_server_ip);
-
- if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
- warning("Unable to open socket for connection to MasterServer");
- return -1;
- }
-
- /* connect to MasterServer */
- if (connect(s, (struct sockaddr *) &server, sizeof(server)) < 0) {
- warning("Unable to estabilish connection to MasterServer");
- return -1;
- }
-
- typedef struct {
- unsigned char cmd;
- unsigned port;
- unsigned ip;
- } __PACKED__ register_head;
-
- register_head *head = (register_head *) malloc(sizeof(register_head));
-
- head->cmd = 'u';
- head->port = atoi(public_server_get_setting("PORT4", "--port4", "6800"));
- head->ip = 0; /* TODO */
-
- /* send request for server list */
- int r = send(s, head, sizeof(register_head), 0);
-
- free(head);
-
-#ifndef __WIN32__
- close(s);
-#else /* __WIN32__ */
- closesocket(s);
-#endif /* __WIN32__ */
-
- if (r == -1) {
- return -1;
- } else {
- return 0;
- }
-}
-
-static int public_server_initNetwork()
-{
- char ip4[STR_IP_SIZE];
- char ip6[STR_IP_SIZE];
- char *p_ip4, *p_ip6;
- int port;
- int ret;
-
- ret = -1;
-
- strcpy(ip4, public_server_get_setting("IP4", "--ip4", "none"));
- strcpy(ip6, public_server_get_setting("IP6", "--ip6", "none"));
-
- p_ip4 = ip4;
-
- if (strcmp(ip4, "none") == 0 || strcmp(ip6, "::") == 0) {
- p_ip4 = NULL;
- }
-
- p_ip6 = ip6;
-
- if (strcmp(ip6, "none") == 0) {
- p_ip6 = NULL;
- }
-
- port = atoi(public_server_get_setting("PORT", "--port", "6800"));
-
- ret = net_multiplayer_init_for_game_server(p_ip4, p_ip6, port);
-
- return ret;
-}
-
-static void load_arena()
-{
- choice_arenaFile = arena_file_get_file_format_net_name(public_server_get_setting("ARENA", "--arena", "FAGN"));
-
- if (choice_arenaFile == NULL) {
- error("Unable to load arena [%s]", public_server_get_setting("ARENA", "--arena", "FAGN"));
- /* TODO: Why not to log it? */
- exit(-1);
- }
-
- arena = arena_file_get_arena(choice_arenaFile);
-
- arena_set_current(arena);
-}
-
-int public_server_init()
-{
- int ret;
- int i;
-
- id_init_list();
- module_init();
- arena_file_init();
- tux_init();
- item_init();
- shot_init();
- server_configFile_init();
-
- ret = log_init(public_server_get_setting("LOG_FILE", "--log-file", "/tmp/tuxanci-server.log"));
-
- if (ret < 0) {
- /* Error message has been already printed */
- return -1;
- }
-
- high_score_init(public_server_get_setting("SCORE_FILE", "--score-file", "/tmp/highscore.txt"));
-
- load_arena();
-
- for (i = 0; i < atoi(public_server_get_setting("ITEM", "--item", "10")); i++) {
- item_add_new_item(arena->spaceItem, ID_UNKNOWN);
- }
-
- isSignalEnd = FALSE;
-
- ret = public_server_initNetwork();
-
- if (ret < 0) {
- error("Unable to initialize network socket");
- /* TODO: Why not to log it? */
- return -1;
- }
-
- server_set_max_clients(atoi(public_server_get_setting("MAX_CLIENTS", "--max-clients", "32")));
-
- if (atoi(public_server_get_setting("LAN_ONLY", "--lan-only", "0")) == 0) {
- if (public_server_register() < 0) {
- error("Unable to contact MasterServer");
- /* TODO: Why not to log it? */
- }
- } else {
- debug("Starting LAN-only server");
- }
-
- return 0;
-}
-
-arenaFile_t *choice_arena_get()
-{
- return choice_arenaFile;
-}
-
-void public_server_event()
-{
- static my_time_t lastActive = 0;
- my_time_t interval;
-
- net_multiplayer_event();
-
- if (isSignalEnd == TRUE) {
- public_server_quit();
- }
-
- if (lastActive == 0) {
- lastActive = timer_get_current_time();
- }
-
- interval = timer_get_current_time() - lastActive;
-
- if (interval < 50) {
- return;
- }
-
- lastActive = timer_get_current_time();
-
- arena_event(arena);
-}
-
-void my_handler_quit(int n)
-{
- debug("Received signal %d", n);
- /* TODO: Why not to log it? */
-
- isSignalEnd = TRUE;
-
- public_server_quit();
-}
-
-void public_server_quit()
-{
- debug("Shutting down the Tuxanci game server");
- /* TODO: Why not to log it? */
-
- if (atoi(public_server_get_setting("LAN_ONLY", "--lan-only", "0")) == 0) {
- if (public_server_unregister() < 0) {
- error("Unable to contact MasterServer");
- /* TODO: Why not to log it? */
- }
- }
-
- net_multiplayer_quit();
- arena_destroy(arena);
-
- tux_quit();
- item_quiy();
- shot_quit();
-
- /* remove the PID file */
- remove(public_server_get_setting("PID_FILE", "--pid-file", "/var/run/tuxanci-server.pid"));
-
- arena_file_quit();
- server_configFile_quit();
- module_quit();
- id_quit_list();
- high_score_quit();
- log_quit();
-
- exit(0);
-}
-
-int public_server_start()
-{
-#ifndef __WIN32__
- signal(SIGINT, my_handler_quit);
- signal(SIGTERM, my_handler_quit);
- signal(SIGQUIT, my_handler_quit);
-#endif /* __WIN32__ */
- if (public_server_init() < 0) {
- public_server_quit();
- return -1;
- }
-
- char *s = public_server_get_setting("DAEMON", "--daemon", "0");
-
- if (atoi(s)) {
- daemonize();
- }
-
- for (;;) {
- public_server_event();
- }
-
- return 0;
-}
diff --git a/src/server/publicServer.h b/src/server/publicServer.h
deleted file mode 100644
index 59c029e..0000000
--- a/src/server/publicServer.h
+++ /dev/null
@@ -1,18 +0,0 @@
-#ifndef PUBLIC_SERVER_H
-#define PUBLIC_SERVER_H
-
-#include "arena.h"
-#include "arenaFile.h"
-
-#define WORLD_COUNT_ROUND_UNLIMITED -1
-
-extern char *public_server_get_setting(char *env, char *param, char *default_val);
-extern void world_inc_round();
-extern int public_server_init();
-extern arenaFile_t *choice_arena_get();
-extern void eventPublicServer();
-extern void my_handler_quit(int n);
-extern void public_server_quit();
-extern int public_server_start();
-
-#endif /* PUBLIC_SERVER_H */
diff --git a/src/server/serverConfigFile.c b/src/server/serverConfigFile.c
deleted file mode 100644
index d872827..0000000
--- a/src/server/serverConfigFile.c
+++ /dev/null
@@ -1,94 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "main.h"
-#include "list.h"
-#include "textFile.h"
-#include "serverConfigFile.h"
-
-static textFile_t *serverTextFile;
-
-static void prepareConfigFile(textFile_t *ts)
-{
- int i, j;
-
- for (i = 0; i < serverTextFile->text->count; i++) {
- char *line;
- int len;
-
- line = (char *) (serverTextFile->text->list[i]);
- len = strlen(line);
-
- for (j = 0; j < len; j++) {
- if (line[j] == ' ') { /* [TAB] */
- line[j] = ' ';
- }
- }
- }
-}
-
-void server_configFile_init()
-{
- char *configFile;
-
- configFile = getParamElse("--config-file", SERVER_CONFIG);
- debug("Loading configuration file [%s]", getParamElse("--config-file", SERVER_CONFIG));
-
- serverTextFile = text_file_load(configFile);
-
- if (serverTextFile == NULL) {
- warning("Unable to load config file - using defaults");
- return;
- }
-
- prepareConfigFile(serverTextFile);
-}
-
-static char *findValue(char *s)
-{
- int i;
- int len;
-
- len = strlen(s);
-
- for (i = 0; i < len - 1; i++) {
- if (s[i] == ' ' && s[i + 1] != ' ') {
- return s + i + 1;
- }
- }
-
- return NULL;
-}
-
-char *server_configFile_get_value(char *env, char *s)
-{
- int i;
- int len;
-
- if (serverTextFile == NULL) {
- return s;
- }
-
- len = strlen(env);
-
- for (i = 0; i < serverTextFile->text->count; i++) {
- char *line;
-
- line = (char *) (serverTextFile->text->list[i]);
-
- if (strncmp(line, env, len) == 0) {
- return findValue(line);
- }
- }
-
- return s;
-}
-
-void server_configFile_quit()
-{
- if (serverTextFile != NULL) {
- text_file_destroy(serverTextFile);
- }
-}
diff --git a/src/server/serverConfigFile.h b/src/server/serverConfigFile.h
deleted file mode 100644
index b17b90f..0000000
--- a/src/server/serverConfigFile.h
+++ /dev/null
@@ -1,8 +0,0 @@
-#ifndef SERVER_CONFIG_FILE_H
-#define SERVER_CONFIG_FILE_H
-
-extern void server_configFile_init();
-extern char *server_configFile_get_value(char *env, char *s);
-extern void server_configFile_quit();
-
-#endif /* SERVER_CONFIG_FILE_H */