dedicated.c
changeset 704 e843dd369938
parent 702 5e80e4d69057
child 720 b9ecc658d224
equal deleted inserted replaced
703:3f64a428fcbc 704:e843dd369938
    24 // This file handles all dedicated-server in- and outputs
    24 // This file handles all dedicated-server in- and outputs
    25 
    25 
    26 static void *_dedicated_video_mem;
    26 static void *_dedicated_video_mem;
    27 
    27 
    28 #ifdef UNIX
    28 #ifdef UNIX
       
    29 /* We want to fork our dedicated server */
       
    30 void DedicatedFork()
       
    31 {
       
    32 	/* Fork the program */
       
    33 	_dedicated_pid = fork();
       
    34 	switch (_dedicated_pid) {
       
    35 		case -1:
       
    36 			perror("Unable to fork");
       
    37 			exit(1);
       
    38 		case 0:
       
    39 			// We're the child
       
    40 
       
    41 			/* Open the log-file to log all stuff too */
       
    42 			_log_file_fd = fopen(_log_file, "a");
       
    43 			if (!_log_file_fd) {
       
    44 				perror("Unable to open logfile");
       
    45 				exit(1);
       
    46 			}
       
    47 			/* Redirect stdout and stderr to log-file */
       
    48 			if (dup2(fileno(_log_file_fd), fileno(stdout)) == -1) {
       
    49 				perror("Re-routing stdout");
       
    50 				exit(1);
       
    51 			}
       
    52 			if (dup2(fileno(_log_file_fd), fileno(stderr)) == -1) {
       
    53 				perror("Re-routing stderr");
       
    54 				exit(1);
       
    55 			}
       
    56 			break;
       
    57 		default:
       
    58 			// We're the parent
       
    59 			printf("Loading dedicated server...\n");
       
    60 			printf("  - Forked to background with pid %d\n", _dedicated_pid);
       
    61 			exit(0);
       
    62 	}
       
    63 }
       
    64 
    29 /* Signal handlers */
    65 /* Signal handlers */
    30 void DedicatedSignalHandler(int sig)
    66 static void DedicatedSignalHandler(int sig)
    31 {
    67 {
    32 		_exit_game = true;
    68 		_exit_game = true;
    33 		signal(sig, DedicatedSignalHandler);
    69 		signal(sig, DedicatedSignalHandler);
    34 }
    70 }
    35 #endif
    71 #endif
    55 static void DedicatedVideoMakeDirty(int left, int top, int width, int height) {}
    91 static void DedicatedVideoMakeDirty(int left, int top, int width, int height) {}
    56 static bool DedicatedVideoChangeRes(int w, int h) { return false; }
    92 static bool DedicatedVideoChangeRes(int w, int h) { return false; }
    57 
    93 
    58 #ifdef UNIX
    94 #ifdef UNIX
    59 
    95 
    60 bool InputWaiting()
    96 static bool InputWaiting()
    61 {
    97 {
    62 	struct timeval tv;
    98 	struct timeval tv;
    63 	fd_set readfds;
    99 	fd_set readfds;
    64 
   100 
    65 	tv.tv_sec = 0;
   101 	tv.tv_sec = 0;
    75 		return true;
   111 		return true;
    76 	else
   112 	else
    77 		return false;
   113 		return false;
    78 }
   114 }
    79 #else
   115 #else
    80 bool InputWaiting()
   116 static bool InputWaiting()
    81 {
   117 {
    82 	return kbhit();
   118 	return kbhit();
    83 }
   119 }
    84 #endif
   120 #endif
    85 
   121 
    86 static int DedicatedVideoMainLoop() {
   122 static void DedicatedHandleKeyInput()
       
   123 {
       
   124 #ifdef WIN32
       
   125 	char input;
       
   126 #endif
       
   127 	char input_line[200];
       
   128 
       
   129 #ifdef UNIX
       
   130 	if (InputWaiting()) {
       
   131 		fgets(input_line, 200, stdin);
       
   132 		// Forget about the final \n (or \r)
       
   133 		strtok(input_line, "\r\n");
       
   134 		IConsoleCmdExec(input_line);
       
   135 	}
       
   136 #else
       
   137 	if (InputWaiting()) {
       
   138 		input = getch();
       
   139 		printf("%c", input);
       
   140 		if (input != '\r')
       
   141 			snprintf(input_line, 200, "%s%c", input_line, input);
       
   142 		else {
       
   143 			printf("\n");
       
   144 			IConsoleCmdExec(input_line);
       
   145 			sprintf(input_line, "");
       
   146 		}
       
   147 	}
       
   148 #endif
       
   149 }
       
   150 
       
   151 static int DedicatedVideoMainLoop()
       
   152 {
    87 #ifndef WIN32
   153 #ifndef WIN32
    88 	struct timeval tim;
   154 	struct timeval tim;
    89 #else
       
    90 	char input;
       
    91 #endif
   155 #endif
    92 	uint32 next_tick;
   156 	uint32 next_tick;
    93 	uint32 cur_ticks;
   157 	uint32 cur_ticks;
    94 	char input_line[200];
       
    95 
   158 
    96 #ifdef WIN32
   159 #ifdef WIN32
    97 	next_tick = GetTickCount() + 30;
   160 	next_tick = GetTickCount() + 30;
    98 #else
   161 #else
    99 	gettimeofday(&tim, NULL);
   162 	gettimeofday(&tim, NULL);
   123 	while (true) {
   186 	while (true) {
   124 		InteractiveRandom(); // randomness
   187 		InteractiveRandom(); // randomness
   125 
   188 
   126 		if (_exit_game) return ML_QUIT;
   189 		if (_exit_game) return ML_QUIT;
   127 
   190 
   128 #ifdef UNIX
   191 		if (!_dedicated_forks)
   129 		if (InputWaiting()) {
   192 			DedicatedHandleKeyInput();
   130 			fgets(input_line, 200, stdin);
       
   131 			// Forget about the final \n (or \r)
       
   132 			strtok(input_line, "\r\n");
       
   133 			IConsoleCmdExec(input_line);
       
   134 		}
       
   135 #else
       
   136 		if (InputWaiting()) {
       
   137 			input = getch();
       
   138 			printf("%c", input);
       
   139 			if (input != '\r')
       
   140 				snprintf(input_line, 200, "%s%c", input_line, input);
       
   141 			else {
       
   142 				printf("\n");
       
   143 				IConsoleCmdExec(input_line);
       
   144 				sprintf(input_line, "");
       
   145 			}
       
   146 		}
       
   147 #endif
       
   148 
   193 
   149 #ifdef WIN32
   194 #ifdef WIN32
   150 		cur_ticks = GetTickCount();
   195 		cur_ticks = GetTickCount();
   151 #else
   196 #else
   152 		gettimeofday(&tim, NULL);
   197 		gettimeofday(&tim, NULL);