diff options
| author | xHire <xhire@tuxportal.cz> | 2009-05-19 12:17:10 +0200 |
|---|---|---|
| committer | xHire <xhire@tuxportal.cz> | 2009-05-26 01:09:32 +0200 |
| commit | afb63da1374aadd4a5feccff020e65b616d9b84a (patch) | |
| tree | 3ee47e151ff8ec186bec3ffd5262d63ddca15f08 /src/server | |
| parent | a1d6339b26fc38d374cd400481a8703ba3199596 (diff) | |
Rewritten messages/comments in code of server
Rectified the coding style in code of server
Changed the empty score line "no_name" -> "---"
Fixed __WIN32 to __WIN32__
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/highScore.c | 16 | ||||
| -rw-r--r-- | src/server/highScore.h | 8 | ||||
| -rw-r--r-- | src/server/log.c | 21 | ||||
| -rw-r--r-- | src/server/log.h | 14 | ||||
| -rw-r--r-- | src/server/publicServer.c | 209 | ||||
| -rw-r--r-- | src/server/publicServer.h | 12 | ||||
| -rw-r--r-- | src/server/serverConfigFile.c | 11 | ||||
| -rw-r--r-- | src/server/serverConfigFile.h | 7 |
8 files changed, 146 insertions, 152 deletions
diff --git a/src/server/highScore.c b/src/server/highScore.c index 50e6a13..cbab164 100644 --- a/src/server/highScore.c +++ b/src/server/highScore.c @@ -1,4 +1,3 @@ - #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -18,22 +17,21 @@ void high_score_init(char *file) textFile = text_file_load(file); if (textFile == NULL) { - fprintf(stderr, _("I am unable to load: \"%s\"!\n"), file); - fprintf(stderr, _("Creating: \"%s\"\n"), file); + fprintf(stderr, _("[Error] Unable to load high score [%s]\n"), file); + fprintf(stderr, _("[Debug] Creating high score file [%s]\n"), file); textFile = text_file_new(file); } else { - - DEBUG_MSG(_("Scorefile: \"%s\"\n"), file); + DEBUG_MSG(_("[Debug] Loading high score file [%s]\n"), file); return; } if (textFile == NULL) { - fprintf(stderr, _("I was unable to create: \"%s\"!\n"), file); + fprintf(stderr, _("[Error] Unable to create high score file [%s]\n"), file); return; } for (i = 0; i < HIGHSCORE_MAX_PLAYERS; i++) { - list_add(textFile->text, strdup("no_name 0")); + list_add(textFile->text, strdup("--- 0")); } text_file_save(textFile); @@ -44,7 +42,7 @@ int table_add(char *name, int score) int i; if (score <= 0) { - return -1; // Ha ha ha + return -1; /* Ha ha ha */ } for (i = 0; i < HIGHSCORE_MAX_PLAYERS; i++) { @@ -61,7 +59,7 @@ int table_add(char *name, int score) 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_print(textFile);*/ text_file_save(textFile); return 0; diff --git a/src/server/highScore.h b/src/server/highScore.h index 8c90db5..604afb7 100644 --- a/src/server/highScore.h +++ b/src/server/highScore.h @@ -1,13 +1,11 @@ - #ifndef HIGH_SCORE_H +#define HIGH_SCORE_H -# define HIGH_SCORE_H - -# define HIGHSCORE_MAX_PLAYERS 100 +#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 +#endif /* HIGH_SCORE_H */ diff --git a/src/server/log.c b/src/server/log.c index fcfb6e0..2f7bc13 100644 --- a/src/server/log.c +++ b/src/server/log.c @@ -1,4 +1,3 @@ - #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -16,13 +15,13 @@ int log_init(char *name) logFile = fopen(name, "a"); if (logFile == NULL) { - fprintf(stderr, _("Opening logfile \"%s\" failed!\n"), name); + fprintf(stderr, _("[Error] Unable to open log file [%s]\n"), name); return -1; } - DEBUG_MSG(_("I use logfile: \"%s\")\n"), name); + DEBUG_MSG(_("[Debug] Using log file [%s]\n"), name); - log_add(LOG_INF, "open log file"); + log_add(LOG_INF, "Logging started"); return 0; } @@ -39,23 +38,23 @@ void log_add(int type, char *msg) switch (type) { case LOG_INF: - str_type = "INF"; + str_type = "INFO"; break; case LOG_DBG: - str_type = "DBG"; + str_type = "DEBUG"; break; case LOG_WRN: - str_type = "WRN"; + str_type = "WARN"; break; case LOG_ERR: - str_type = "ERR"; + str_type = "ERROR"; break; default: - assert(!_("Really bad log value!")); + assert(!_("[Error] Unknown type of the logging string")); break; } - sprintf(str, "(%02d-%02d-%02d %02d:%02d:%02d) %s %s\n", + sprintf(str, "[%02d-%02d-%02d %02d:%02d:%02d] [%s] %s\n", 1900 + tm_struct->tm_year, tm_struct->tm_mon, tm_struct->tm_mday, tm_struct->tm_hour, tm_struct->tm_min, tm_struct->tm_sec, str_type, msg); @@ -67,6 +66,6 @@ void log_add(int type, char *msg) void log_quit() { - log_add(LOG_INF, "close log file"); + log_add(LOG_INF, "Logging finished"); fclose(logFile); } diff --git a/src/server/log.h b/src/server/log.h index 6b61529..b256e08 100644 --- a/src/server/log.h +++ b/src/server/log.h @@ -1,15 +1,13 @@ - #ifndef LOG_H +#define LOG_H -# define LOG_H - -# define LOG_INF 1 -# define LOG_DBG 2 -# define LOG_WRN 3 -# define LOG_ERR 4 +#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 +#endif /* LOG_H */ diff --git a/src/server/publicServer.c b/src/server/publicServer.c index c112634..2ac3ec1 100644 --- a/src/server/publicServer.c +++ b/src/server/publicServer.c @@ -1,4 +1,3 @@ - #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -6,14 +5,14 @@ #include <signal.h> #include <time.h> -#ifndef __WIN32 -# include <sys/socket.h> -# include <sys/select.h> -# include <arpa/inet.h> -#else -# include <windows.h> -# include <wininet.h> -#endif +#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> @@ -56,72 +55,77 @@ char *public_server_get_setting(char *env, char *param, char *default_val) return getParamElse(param, server_configFile_get_value(env, default_val)); } -void daemonize () +void daemonize() { #ifndef __WIN32__ - int i, lockfd; - char pid[16]; + int i, lockfd, ipid; + char pid[16]; char spid[64]; - int ipid = getpid (); + ipid = getpid(); - /* detach if asked */ - if (ipid == 1) + /* detach if asked */ + if (ipid == 1) { return; /* already a daemon */ + } - /* fork to guarantee we are not process group leader */ - i = fork (); + /* fork to guarantee we are not process group leader */ + i = fork(); - if (i < 0) - exit (1); /* fork error */ - if (i > 0) - exit (0); /* parent exits */ + if (i < 0) { + exit(1); /* fork error */ + } else if (i > 0) { + exit(0); /* parent exits */ + } - /* child (daemon) continues */ - setsid (); /* obtain a new process group */ - ipid = getpid ()+1; + /* child (daemon) continues */ + setsid(); /* obtain a new process group */ + ipid = getpid() + 1; - printf ("> started with pid -> %d\n", ipid); - /* fork again so we become process group leader - * and cannot regain a controlling tty - */ - i = fork (); + printf("The Tuxanci game server started with PID %d\n", ipid); + /* fork again so we become process group leader + * and cannot regain a controlling tty + */ + i = fork(); - if (i < 0) - exit (1); /* fork error */ - else if (i > 0) - exit (0); /* parent exits */ + if (i < 0) { + exit(1); /* fork error */ + } else if (i > 0) { + exit(0); /* parent exits */ + } - /* close all fds */ - for (i = getdtablesize (); i >= 0; --i) - close (i); /* close all descriptors */ + /* close all fds (descriptors) */ + for (i = getdtablesize(); i >= 0; --i) { + close(i); + } - /* close parent fds and send output to fds 0, 1 and 2 to bitbucket */ - i = open ("/dev/null", O_RDWR); + /* close parent fds and send output to fds 0, 1 and 2 to bitbucket */ + i = open("/dev/null", O_RDWR); - if (i < 0) - exit (1); + if (i < 0) { + exit(1); + } - dup (i); - dup (i); /* handle standart I/O */ + dup(i); + dup(i); /* handle standart I/O */ - sprintf (spid, "/tmp/tuxanci-server-%d.pid", ipid); + sprintf(spid, "/tmp/tuxanci-server.pid"); - /* create local lock */ - lockfd = open (spid, O_RDWR | O_CREAT, 0640); + /* create local lock */ + lockfd = open(spid, O_RDWR | O_CREAT, 0640); - if (lockfd < 0) { - perror ("lock: open"); - exit (1); - } + if (lockfd < 0) { + perror("[Error] Lock: open\n"); + exit(1); + } #ifndef __CYGWIN__ /* lock the file */ - if (lockf (lockfd, F_TLOCK, 0) < 0) { - perror ("lock: lockf"); - printf ("> tuxanci-server is already running.\n"); - exit (0); + if (lockf(lockfd, F_TLOCK, 0) < 0) { + perror("[Error] Lock: lockf\n"); + printf("[Warning] The Tuxanci game server is already running\n"); + exit(0); } - #else + #else /* __CYGWIN__ */ /* lock the file */ { struct flock lock; @@ -130,39 +134,37 @@ void daemonize () lock.l_whence = SEEK_SET; lock.l_len = 0; - if (fcntl (lockfd, F_SETLK, &lock) < 0) { - printf ("> tuxanci-server is already running.\n"); - exit (0); + if (fcntl(lockfd, F_SETLK, &lock) < 0) { + printf("[Warning] The Tuxanci game server is already running\n"); + exit(0); } } - #endif + #endif /* __CYGWIN__ */ + /* write to pid to lockfile */ - snprintf (pid, 16, "%d\n", getpid ()); - write (lockfd, pid, strlen (pid)); + snprintf(pid, 16, "%d\n", getpid()); + write(lockfd, pid, strlen(pid)); /* restrict created files to 0750 */ - umask (027); -#endif + umask(027); +#endif /* __WIN32__ */ } static int public_server_register() { -#ifndef __WIN32 +#ifndef __WIN32__ int s; -#else +#else /* __WIN32__ */ SOCKET s; -#endif +#endif /* __WIN32__ */ - /* TODO: dodelat TCP makro */ + /* TODO: TCP macro */ struct sockaddr_in server; char *master_server_ip; master_server_ip = dns_resolv(NET_MASTER_SERVER_DOMAIN); - //printf("master_server_ip = %s\n", master_server_ip); - - if (master_server_ip == NULL) // master server is down - { + if (master_server_ip == NULL) { /* master server is down? */ return -1; } @@ -185,25 +187,25 @@ static int public_server_register() if (fcntl(s, F_SETFL, oldFlag | O_NONBLOCK) == -1) { return -1; } -#else +#else /* __WIN32__ */ unsigned long arg = 1; // Operation is FIONBIO. Parameter is pointer on non-zero number. if (ioctlsocket(s, FIONBIO, &arg) == SOCKET_ERROR) { WSACleanup(); return -1; } -#endif +#endif /* __WIN32__ */ if (connect(s, (struct sockaddr *) &server, sizeof(server)) == -1) { #ifndef __WIN32__ if (errno != EINPROGRESS) return -1; -#else +#else /* __WIN32__ */ if (WSAGetLastError() != WSAEWOULDBLOCK) { WSACleanup(); return -1; } -#endif +#endif /* __WIN32__ */ } struct timeval tv; @@ -218,11 +220,11 @@ static int public_server_register() int ret = select(s + 1, NULL, &myset, NULL, &tv); - if (ret == -1) + if (ret == -1) { return -1; - - if (ret == 0) + } else if (ret == 0) { return -1; + } FD_ZERO(&myset); FD_SET(s, &myset); @@ -232,12 +234,11 @@ static int public_server_register() ret = select(s + 1, NULL, &myset, NULL, &tv); - if (ret == -1) + if (ret == -1) { return -1; - - if (ret == 0) + } else if (ret == 0) { return -1; - + } typedef struct { unsigned char cmd; @@ -251,7 +252,7 @@ static int public_server_register() head->cmd = 'p'; head->port = atoi(public_server_get_setting("PORT4", "--port4", "6800")); - head->ip = 0; // TODO + head->ip = 0; /* TODO */ /* send request for server list */ int r = send(s, str, 9, 0); @@ -259,18 +260,18 @@ static int public_server_register() free(str); if (r == -1) { -#ifndef __WIN32 +#ifndef __WIN32__ close(s); -#else +#else /* __WIN32__ */ closesocket(s); -#endif +#endif /* __WIN32__ */ return -1; } -#ifndef __WIN32 +#ifndef __WIN32__ close(s); -#else +#else /* __WIN32__ */ closesocket(s); -#endif +#endif /* __WIN32__ */ return 0; } @@ -304,7 +305,7 @@ static int public_server_initNetwork() port4 = atoi(public_server_get_setting("PORT4", "--port4", "6800")); port6 = atoi(public_server_get_setting("PORT6", "--port6", "6800")); - //ret = initNetMulitplayerPublicServer(p_ip4, port4, p_ip6, port6); + /*ret = initNetMulitplayerPublicServer(p_ip4, port4, p_ip6, port6);*/ ret = net_multiplayer_init_for_game_server(p_ip4, port4, p_ip6, port6); @@ -316,7 +317,8 @@ static void load_arena() choice_arenaFile = arena_file_get_file_format_net_name(public_server_get_setting("ARENA", "--arena", "FAGN")); if (choice_arenaFile == NULL) { - fprintf(stderr, _("I dont load arena %s!\n"), public_server_get_setting("ARENA", "--arena", "FAGN")); + fprintf(stderr, _("[Error] Unable to load arena [%s]\n"), public_server_get_setting("ARENA", "--arena", "FAGN")); + /* TODO: Why not to log it? */ exit(-1); } @@ -341,7 +343,7 @@ int public_server_init() ret = log_init(public_server_get_setting("LOG_FILE", "--log-file", "/tmp/tuxanci-server.log")); if (ret < 0) { - fprintf(stderr, _("I was unable to open config file!\n")); + /* Error message has been already printed */ return -1; } @@ -358,14 +360,16 @@ int public_server_init() ret = public_server_initNetwork(); if (ret < 0) { - printf(_("Unable to initialize network socket!\n")); + fprintf(stderr, _("[Error] Unable to initialize network socket\n")); + /* TODO: Why not to log it? */ return -1; } - server_set_max_clients(atoi (public_server_get_setting("MAX_CLIENTS", "--max-clients", "32"))); + server_set_max_clients(atoi(public_server_get_setting("MAX_CLIENTS", "--max-clients", "32"))); if (public_server_register() < 0) { - printf(_("Unable to contact MasterServer!)\n")); + printf(stderr, _("[Error] Unable to contact MasterServer\n")); + /* TODO: Why not to log it? */ } return 0; @@ -396,7 +400,6 @@ void public_server_event() if (interval < 50) { return; } - //printf("interval = %d\n", interval); lastActive = timer_get_current_time(); @@ -405,14 +408,18 @@ void public_server_event() void my_handler_quit(int n) { - DEBUG_MSG("my_handler_quit\n"); + DEBUG_MSG("[Debug] Received signal %d\n", n); + /* TODO: Why not to log it? */ isSignalEnd = TRUE; + + public_server_quit(); } void public_server_quit() { - DEBUG_MSG(_("Quitting public server\n")); + DEBUG_MSG(_("[Debug] Shutting down the Tuxanci game server\n")); + /* TODO: Why not to log it? */ net_multiplayer_quit(); arena_destroy(arena); @@ -437,7 +444,7 @@ int public_server_start() signal(SIGINT, my_handler_quit); signal(SIGTERM, my_handler_quit); signal(SIGQUIT, my_handler_quit); -#endif +#endif /* __WIN32__ */ if (public_server_init() < 0) { public_server_quit(); return -1; @@ -445,11 +452,13 @@ int public_server_start() char *s = public_server_get_setting("DAEMON", "--daemon", "none"); - if (atoi(s)) + if (atoi(s)) { daemonize(); + } - for (;;) + for (;;) { public_server_event(); + } return 0; } diff --git a/src/server/publicServer.h b/src/server/publicServer.h index f4402d2..59c029e 100644 --- a/src/server/publicServer.h +++ b/src/server/publicServer.h @@ -1,12 +1,10 @@ - #ifndef PUBLIC_SERVER_H +#define PUBLIC_SERVER_H -# define PUBLIC_SERVER_H - -# include "arena.h" -# include "arenaFile.h" +#include "arena.h" +#include "arenaFile.h" -# define WORLD_COUNT_ROUND_UNLIMITED -1 +#define WORLD_COUNT_ROUND_UNLIMITED -1 extern char *public_server_get_setting(char *env, char *param, char *default_val); extern void world_inc_round(); @@ -17,4 +15,4 @@ extern void my_handler_quit(int n); extern void public_server_quit(); extern int public_server_start(); -#endif +#endif /* PUBLIC_SERVER_H */ diff --git a/src/server/serverConfigFile.c b/src/server/serverConfigFile.c index 8458de2..dbf1c63 100644 --- a/src/server/serverConfigFile.c +++ b/src/server/serverConfigFile.c @@ -1,4 +1,3 @@ - #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -24,7 +23,7 @@ static void prepareConfigFile(textFile_t * ts) len = strlen(line); for (j = 0; j < len; j++) { - if (line[j] == ' ') // [TAB] + if (line[j] == ' ') /* [TAB] */ { line[j] = ' '; } @@ -37,15 +36,13 @@ void server_configFile_init() char *configFile; configFile = getParamElse("--config-file", SERVER_CONFIG); - printf(_("Loading configuration from: \"%s\"\n"), - getParamElse("--config-file", SERVER_CONFIG)); + printf(_("[Debug] Loading configuration file [%s]\n"), + getParamElse("--config-file", SERVER_CONFIG)); serverTextFile = text_file_load(configFile); if (serverTextFile == NULL) { - fprintf(stderr, - _ - ("I was unable to load config file. Falling back to defaults!\n")); + fprintf(stderr, _("[Warning] Unable to load config file - using defaults\n")); return; } diff --git a/src/server/serverConfigFile.h b/src/server/serverConfigFile.h index 4388df2..b17b90f 100644 --- a/src/server/serverConfigFile.h +++ b/src/server/serverConfigFile.h @@ -1,11 +1,8 @@ - - #ifndef SERVER_CONFIG_FILE_H - -# define 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 +#endif /* SERVER_CONFIG_FILE_H */ |