summaryrefslogtreecommitdiff
path: root/src/server/publicServer.c~
diff options
context:
space:
mode:
authorTomas Chvatal (scarabeus) <tomas.chvatal@gmail.com>2008-10-22 22:50:54 +0200
committerTomas Chvatal (scarabeus) <tomas.chvatal@gmail.com>2008-10-22 22:50:54 +0200
commite12753d2ec9db83ec08bb9db63902a82fb245cdd (patch)
tree6e4a63b790d6335f969f88b797a927e0fb25f37a /src/server/publicServer.c~
parent0b4d9821c7333b8c4bdf7e0eeb750aef502a1137 (diff)
X
Diffstat (limited to 'src/server/publicServer.c~')
-rw-r--r--src/server/publicServer.c~372
1 files changed, 372 insertions, 0 deletions
diff --git a/src/server/publicServer.c~ b/src/server/publicServer.c~
new file mode 100644
index 0000000..8770300
--- /dev/null
+++ b/src/server/publicServer.c~
@@ -0,0 +1,372 @@
+
+#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
+# include <windows.h>
+# include <wininet.h>
+#endif
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.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 "heightScore.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 countRoundInc()
+{
+}
+
+char *getSetting(char *env, char *param, char *default_val)
+{
+ return getParamElse(param, getServerConfigFileValue(env, default_val) );
+}
+
+static int registerPublicServer()
+{
+#ifndef __WIN32
+ int s;
+#else
+ SOCKET s;
+#endif
+
+ /* TODO: dodelat TCP makro */
+ struct sockaddr_in server;
+ char *master_server_ip;
+
+ master_server_ip = getIPFormDNS(NET_MASTER_SERVER_DOMAIN);
+
+ //printf("master_server_ip = %s\n", master_server_ip);
+
+ if( master_server_ip == NULL ) // master server is down
+ {
+ 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, 0)) < 0) {
+ return 0;
+ }
+
+ /* Set to nonblocking socket mode */
+#ifndef __WIN32__
+ int oldFlag;
+
+ oldFlag = fcntl (s, F_GETFL, 0);
+
+ if( fcntl(s, F_SETFL, oldFlag | O_NONBLOCK) == -1 )
+ {
+ return -1;
+ }
+#else
+ 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
+
+ if( connect (s, (struct sockaddr *) &server, sizeof (server)) == -1 ) {
+#ifndef __WIN32__
+ if (errno != EINPROGRESS)
+ return -1;
+#else
+ if (WSAGetLastError() != WSAEWOULDBLOCK) {
+ WSACleanup();
+ return -1;
+ }
+#endif
+ }
+
+ struct timeval tv;
+
+ fd_set myset;
+
+ FD_ZERO(&myset);
+ FD_SET(s, &myset);
+
+ tv.tv_sec = 3;
+ tv.tv_usec = 0;
+
+ int ret = select (s+1, NULL, &myset, NULL, &tv);
+
+ if (ret == -1)
+ return -1;
+
+ if (ret == 0)
+ return -1;
+
+ FD_ZERO(&myset);
+ FD_SET(s, &myset);
+
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+
+ ret = select (s+1, NULL, &myset, NULL, &tv);
+
+ if (ret == -1)
+ return -1;
+
+ if (ret == 0)
+ return -1;
+
+
+ typedef struct {
+ unsigned char cmd;
+ unsigned port;
+ unsigned ip;
+ } __PACKED__ register_head;
+
+ char *str = (char *) malloc (sizeof (register_head));
+
+ register_head *head = (register_head *) str;
+
+ head->cmd = 'p';
+ head->port = atoi( getSetting("PORT4", "--port4", "2200") );
+ head->ip = 0; // TODO
+
+ /* send request for server list */
+ int r = send (s, str, 9, 0);
+
+ free (str);
+
+ if (r == -1) {
+#ifndef __WIN32
+ close(s);
+#else
+ closesocket(s);
+#endif
+ return -1;
+ }
+
+#ifndef __WIN32
+ close(s);
+#else
+ closesocket(s);
+#endif
+
+ return 0;
+}
+
+static int initPublicServerNetwork()
+{
+ char ip4[STR_IP_SIZE];
+ char ip6[STR_IP_SIZE];
+ char *p_ip4, *p_ip6;
+ int port4;
+ int port6;
+ int ret;
+
+ ret = -1;
+
+ strcpy(ip4, getSetting("IP4", "--ip4", "none" ) );
+ strcpy(ip6, getSetting("IP6", "--ip6", "none" ) );
+
+ p_ip4 = ip4;
+
+ if( strcmp(ip4, "none") == 0 )
+ {
+ p_ip4 = NULL;
+ }
+
+ p_ip6 = ip6;
+
+ if( strcmp(ip6, "none") == 0 )
+ {
+ p_ip6 = NULL;
+ }
+
+ port4 = atoi( getSetting("PORT4", "--port4", "2200") );
+ port6 = atoi( getSetting("PORT6", "--port6", "2200") );
+
+ //ret = initNetMulitplayerPublicServer(p_ip4, port4, p_ip6, port6);
+
+ ret = initNetMuliplayerForGameServer(p_ip4, port4, p_ip6, port6);
+
+ return ret;
+}
+
+static void loadArena()
+{
+ choice_arenaFile = getArenaFileFormNetName( getSetting("ARENA", "--arena", "FAGN") );
+ arena = getArena(choice_arenaFile);
+ setCurrentArena(arena);
+}
+
+int initPublicServer()
+{
+ int ret;
+ int i;
+
+ initListID();
+ initModule();
+ initArenaFile();
+ initTux();
+ initItem();
+ initShot();
+ initServerConfigFile();
+
+ ret = initLog(getSetting("LOG_FILE", "--log-file", "/tmp/tuxanci-server.log") );
+
+ if( ret < 0 )
+ {
+ fprintf(stderr, _("I was unable to open config file!\n"));
+ return -1;
+ }
+
+ initHeightScore( getSetting("SCORE_FILE", "--score-file", "/tmp/heightscore") );
+
+ loadArena();
+
+ for( i = 0 ; i < atoi(getSetting("ITEM", "--item", "10")) ; i++ )
+ {
+ addNewItem(arena->spaceItem, ID_UNKNOWN);
+ }
+
+ isSignalEnd = FALSE;
+
+ ret = initPublicServerNetwork();
+
+ if( ret < 0 )
+ {
+ printf(_("Unable to initialize network socket!\n"));
+ return -1;
+ }
+
+ setServerMaxClients( atoi( getSetting("MAX_CLIENTS", "--max-clients", "32") ));
+
+ if (registerPublicServer() < 0)
+ {
+ printf(_("Unable to contact MasterServer!)\n"));
+ }
+
+ return 0;
+}
+
+arenaFile_t* getChoiceArena()
+{
+ return choice_arenaFile;
+}
+
+void eventPublicServer()
+{
+ static my_time_t lastActive = 0;
+ my_time_t interval;
+
+ eventNetMultiplayer();
+
+ if( isSignalEnd == TRUE )
+ {
+ quitPublicServer();
+ }
+
+ if( lastActive == 0 )
+ {
+ lastActive = getMyTime();
+ }
+
+ interval = getMyTime() - lastActive;
+
+ if( interval < 50 )
+ {
+ return;
+ }
+
+ //printf("interval = %d\n", interval);
+
+ lastActive = getMyTime();
+
+ eventArena(arena);
+}
+
+void my_handler_quit(int n)
+{
+#ifdef DEBUG
+ printf("my_handler_quit\n");
+#endif
+ isSignalEnd = TRUE;
+}
+
+void quitPublicServer()
+{
+#ifdef DEBUG
+ printf(_("Quitting public server\n"));
+#endif
+ quitNetMultiplayer();
+ destroyArena(arena);
+
+ quitTux();
+ quitItem();
+ quitShot();
+
+ quitArenaFile();
+ quitServerConfigFile();
+ quitModule();
+ quitListID();
+ quitHeightScore();
+ quitLog();
+
+ exit(0);
+}
+
+int startPublicServer()
+{
+#ifndef __WIN32__
+ signal(SIGINT, my_handler_quit);
+ signal(SIGTERM, my_handler_quit);
+ signal(SIGQUIT, my_handler_quit);
+#endif
+ if( initPublicServer() < 0 )
+ {
+ quitPublicServer();
+ return -1;
+ }
+
+ while(1)
+ {
+ eventPublicServer();
+ }
+
+ return 0;
+}