12 #include "../stdafx.h"
16 #include "../gfx_func.h"
17 #include "../network/network.h"
18 #include "../network/network_internal.h"
19 #include "../console_func.h"
20 #include "../genworld.h"
21 #include "../fileio_type.h"
23 #include "../blitter/factory.hpp"
24 #include "../company_func.h"
25 #include "../core/random_func.hpp"
26 #include "../saveload/saveload.h"
29 #ifdef BEOS_NET_SERVER
30 #include <net/socket.h>
34 # include <sys/time.h>
35 # include <sys/types.h>
48 static void OS2_SwitchToConsoleMode()
53 DosGetInfoBlocks(&tib, &pib);
60 #if defined(UNIX) || defined(PSP)
61 # include <sys/time.h>
62 # include <sys/types.h>
67 # include <sys/fd_set.h>
68 # include <sys/select.h>
72 static void DedicatedSignalHandler(
int sig)
76 signal(sig, DedicatedSignalHandler);
87 static HANDLE _hInputReady, _hWaitForInputHandling;
88 static HANDLE _hThread;
89 static char _win_console_thread_buffer[200];
92 static void WINAPI CheckForConsoleInput()
99 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
101 ReadFile(hStdin, _win_console_thread_buffer,
lengthof(_win_console_thread_buffer), &nb, NULL);
102 if (nb >=
lengthof(_win_console_thread_buffer)) nb =
lengthof(_win_console_thread_buffer) - 1;
103 _win_console_thread_buffer[nb] =
'\0';
107 SetEvent(_hInputReady);
108 WaitForSingleObject(_hWaitForInputHandling, INFINITE);
113 static void CreateWindowsConsoleThread()
117 _hInputReady = CreateEvent(NULL,
false,
false, NULL);
118 _hWaitForInputHandling = CreateEvent(NULL,
false,
false, NULL);
119 if (_hInputReady == NULL || _hWaitForInputHandling == NULL)
usererror(
"Cannot create console event!");
121 _hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, NULL, 0, &dwThreadId);
122 if (_hThread == NULL)
usererror(
"Cannot create console thread!");
124 DEBUG(driver, 2,
"Windows console thread started");
127 static void CloseWindowsConsoleThread()
129 CloseHandle(_hThread);
130 CloseHandle(_hInputReady);
131 CloseHandle(_hWaitForInputHandling);
132 DEBUG(driver, 2,
"Windows console thread shut down");
137 #include "../safeguards.h"
140 static void *_dedicated_video_mem;
143 bool _dedicated_forks;
157 _screen.dst_ptr = _dedicated_video_mem;
166 CreateWindowsConsoleThread();
167 SetConsoleTitle(_T(
"OpenTTD Dedicated Server"));
172 _set_error_mode(_OUT_TO_STDERR);
177 OS2_SwitchToConsoleMode();
180 DEBUG(driver, 1,
"Loading dedicated server");
187 CloseWindowsConsoleThread();
189 free(_dedicated_video_mem);
196 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
197 static bool InputWaiting()
206 FD_SET(STDIN, &readfds);
209 return select(STDIN + 1, &readfds, NULL, NULL, &tv) > 0;
212 static uint32 GetTime()
216 gettimeofday(&tim, NULL);
217 return tim.tv_usec / 1000 + tim.tv_sec * 1000;
222 static bool InputWaiting()
224 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
227 static uint32 GetTime()
229 return GetTickCount();
234 static void DedicatedHandleKeyInput()
236 static char input_line[1024] =
"";
238 if (!InputWaiting())
return;
240 if (_exit_game)
return;
242 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
243 if (fgets(input_line,
lengthof(input_line), stdin) == NULL)
return;
246 assert_compile(
lengthof(_win_console_thread_buffer) <=
lengthof(input_line));
247 strecpy(input_line, _win_console_thread_buffer,
lastof(input_line));
248 SetEvent(_hWaitForInputHandling);
252 for (
char *c = input_line; *c !=
'\0'; c++) {
253 if (*c ==
'\n' || *c ==
'\r' || c ==
lastof(input_line)) {
265 uint32 cur_ticks = GetTime();
269 #if defined(UNIX) || defined(PSP)
270 signal(SIGTERM, DedicatedSignalHandler);
271 signal(SIGINT, DedicatedSignalHandler);
272 signal(SIGQUIT, DedicatedSignalHandler);
291 DEBUG(net, 0,
"Loading requested map failed, aborting");
302 DEBUG(net, 0,
"Dedicated server could not be started, aborting");
306 while (!_exit_game) {
307 uint32 prev_cur_ticks = cur_ticks;
310 if (!_dedicated_forks) DedicatedHandleKeyInput();
312 cur_ticks = GetTime();
314 if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks ||
_ddc_fastforward) {