OpenTTD: src/video/dedicated_v.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

dedicated_v.cpp
Go to the documentation of this file.
1 /* $Id: dedicated_v.cpp 27673 2016-10-30 18:22:55Z michi_cc $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "../stdafx.h"
13
14 #ifdef ENABLE_NETWORK
15
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"
22 #include "../fios.h"
23 #include "../blitter/factory.hpp"
24 #include "../company_func.h"
25 #include "../core/random_func.hpp"
26 #include "../saveload/saveload.h"
27 #include "dedicated_v.h"
28
29 #ifdef BEOS_NET_SERVER
30 #include <net/socket.h>
31 #endif
32
33 #ifdef __OS2__
34 # include <sys/time.h> /* gettimeofday */
35 # include <sys/types.h>
36 # include <unistd.h>
37 # include <conio.h>
38
39 # define INCL_DOS
40 # include <os2.h>
41
42 # define STDIN 0 /* file descriptor for standard input */
43
48 static void OS2_SwitchToConsoleMode()
49 {
50 PPIB pib;
51 PTIB tib;
52
53 DosGetInfoBlocks(&tib, &pib);
54
55 /* Change flag from PM to VIO */
56 pib->pib_ultype = 3;
57 }
58 #endif
59
60 #if defined(UNIX) || defined(PSP)
61 # include <sys/time.h> /* gettimeofday */
62 # include <sys/types.h>
63 # include <unistd.h>
64 # include <signal.h>
65 # define STDIN 0 /* file descriptor for standard input */
66 # if defined(PSP)
67 # include <sys/fd_set.h>
68 # include <sys/select.h>
69 # endif /* PSP */
70
71 /* Signal handlers */
72 static void DedicatedSignalHandler(int sig)
73 {
74 if (_game_mode == GM_NORMAL && _settings_client.gui.autosave_on_exit) DoExitSave();
75 _exit_game = true;
76 signal(sig, DedicatedSignalHandler);
77 }
78 #endif
79
80 #if defined(WIN32)
81 # include <windows.h> /* GetTickCount */
82 # if !defined(WINCE)
83 # include <conio.h>
84 # endif
85 # include <time.h>
86 # include <tchar.h>
87 # include "../os/windows/win32.h"
88 static HANDLE _hInputReady, _hWaitForInputHandling;
89 static HANDLE _hThread; // Thread to close
90 static char _win_console_thread_buffer[200];
91
92 /* Windows Console thread. Just loop and signal when input has been received */
93 static void WINAPI CheckForConsoleInput()
94 {
95 #if defined(WINCE)
96 /* WinCE doesn't support console stuff */
97 return;
98 #else
99 SetWin32ThreadName(-1, "ottd:win-console");
100
101 DWORD nb;
102 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
103 for (;;) {
104 ReadFile(hStdin, _win_console_thread_buffer, lengthof(_win_console_thread_buffer), &nb, NULL);
105 if (nb >= lengthof(_win_console_thread_buffer)) nb = lengthof(_win_console_thread_buffer) - 1;
106 _win_console_thread_buffer[nb] = '\0';
107
108 /* Signal input waiting that input is read and wait for it being handled
109 * SignalObjectAndWait() should be used here, but it's unsupported in Win98< */
110 SetEvent(_hInputReady);
111 WaitForSingleObject(_hWaitForInputHandling, INFINITE);
112 }
113 #endif
114 }
115
116 static void CreateWindowsConsoleThread()
117 {
118 DWORD dwThreadId;
119 /* Create event to signal when console input is ready */
120 _hInputReady = CreateEvent(NULL, false, false, NULL);
121 _hWaitForInputHandling = CreateEvent(NULL, false, false, NULL);
122 if (_hInputReady == NULL || _hWaitForInputHandling == NULL) usererror("Cannot create console event!");
123
124 _hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, NULL, 0, &dwThreadId);
125 if (_hThread == NULL) usererror("Cannot create console thread!");
126
127 DEBUG(driver, 2, "Windows console thread started");
128 }
129
130 static void CloseWindowsConsoleThread()
131 {
132 CloseHandle(_hThread);
133 CloseHandle(_hInputReady);
134 CloseHandle(_hWaitForInputHandling);
135 DEBUG(driver, 2, "Windows console thread shut down");
136 }
137
138 #endif
139
140 #include "../safeguards.h"
141
142
143 static void *_dedicated_video_mem;
144
145 /* Whether a fork has been done. */
146 bool _dedicated_forks;
147
148 extern bool SafeLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL);
149
150 static FVideoDriver_Dedicated iFVideoDriver_Dedicated;
151
152
153 const char *VideoDriver_Dedicated::Start(const char * const *parm)
154 {
155 int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
156 _dedicated_video_mem = (bpp == 0) ? NULL : MallocT<byte>(_cur_resolution.width * _cur_resolution.height * (bpp / 8));
157
158 _screen.width = _screen.pitch = _cur_resolution.width;
159 _screen.height = _cur_resolution.height;
160 _screen.dst_ptr = _dedicated_video_mem;
161 ScreenSizeChanged();
162 BlitterFactory::GetCurrentBlitter()->PostResize();
163
164 #if defined(WINCE)
165 /* WinCE doesn't support console stuff */
166 #elif defined(WIN32)
167 /* For win32 we need to allocate a console (debug mode does the same) */
168 CreateConsole();
169 CreateWindowsConsoleThread();
170 SetConsoleTitle(_T("OpenTTD Dedicated Server"));
171 #endif
172
173 #ifdef _MSC_VER
174 /* Disable the MSVC assertion message box. */
175 _set_error_mode(_OUT_TO_STDERR);
176 #endif
177
178 #ifdef __OS2__
179 /* For OS/2 we also need to switch to console mode instead of PM mode */
180 OS2_SwitchToConsoleMode();
181 #endif
182
183 DEBUG(driver, 1, "Loading dedicated server");
184 return NULL;
185 }
186
187 void VideoDriver_Dedicated::Stop()
188 {
189 #ifdef WIN32
190 CloseWindowsConsoleThread();
191 #endif
192 free(_dedicated_video_mem);
193 }
194
195 void VideoDriver_Dedicated::MakeDirty(int left, int top, int width, int height) {}
196 bool VideoDriver_Dedicated::ChangeResolution(int w, int h) { return false; }
197 bool VideoDriver_Dedicated::ToggleFullscreen(bool fs) { return false; }
198
199 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
200 static bool InputWaiting()
201 {
202 struct timeval tv;
203 fd_set readfds;
204
205 tv.tv_sec = 0;
206 tv.tv_usec = 1;
207
208 FD_ZERO(&readfds);
209 FD_SET(STDIN, &readfds);
210
211 /* don't care about writefds and exceptfds: */
212 return select(STDIN + 1, &readfds, NULL, NULL, &tv) > 0;
213 }
214
215 static uint32 GetTime()
216 {
217 struct timeval tim;
218
219 gettimeofday(&tim, NULL);
220 return tim.tv_usec / 1000 + tim.tv_sec * 1000;
221 }
222
223 #else
224
225 static bool InputWaiting()
226 {
227 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
228 }
229
230 static uint32 GetTime()
231 {
232 return GetTickCount();
233 }
234
235 #endif
236
237 static void DedicatedHandleKeyInput()
238 {
239 static char input_line[1024] = "";
240
241 if (!InputWaiting()) return;
242
243 if (_exit_game) return;
244
245 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
246 if (fgets(input_line, lengthof(input_line), stdin) == NULL) return;
247 #else
248 /* Handle console input, and signal console thread, it can accept input again */
249 assert_compile(lengthof(_win_console_thread_buffer) <= lengthof(input_line));
250 strecpy(input_line, _win_console_thread_buffer, lastof(input_line));
251 SetEvent(_hWaitForInputHandling);
252 #endif
253
254 /* Remove trailing \r or \n */
255 for (char *c = input_line; *c != '\0'; c++) {
256 if (*c == '\n' || *c == '\r' || c == lastof(input_line)) {
257 *c = '\0';
258 break;
259 }
260 }
261 str_validate(input_line, lastof(input_line));
262
263 IConsoleCmdExec(input_line); // execute command
264 }
265
266 void VideoDriver_Dedicated::MainLoop()
267 {
268 uint32 cur_ticks = GetTime();
269 uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
270
271 /* Signal handlers */
272 #if defined(UNIX) || defined(PSP)
273 signal(SIGTERM, DedicatedSignalHandler);
274 signal(SIGINT, DedicatedSignalHandler);
275 signal(SIGQUIT, DedicatedSignalHandler);
276 #endif
277
278 /* Load the dedicated server stuff */
279 _is_network_server = true;
280 _network_dedicated = true;
281 _current_company = _local_company = COMPANY_SPECTATOR;
282
283 /* If SwitchMode is SM_LOAD_GAME, it means that the user used the '-g' options */
284 if (_switch_mode != SM_LOAD_GAME) {
285 StartNewGameWithoutGUI(GENERATE_NEW_SEED);
286 SwitchToMode(_switch_mode);
287 _switch_mode = SM_NONE;
288 } else {
289 _switch_mode = SM_NONE;
290 /* First we need to test if the savegame can be loaded, else we will end up playing the
291 * intro game... */
292 if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.file_op, _file_to_saveload.detail_ftype, GM_NORMAL, BASE_DIR)) {
293 /* Loading failed, pop out.. */
294 DEBUG(net, 0, "Loading requested map failed, aborting");
295 _networking = false;
296 } else {
297 /* We can load this game, so go ahead */
298 SwitchToMode(SM_LOAD_GAME);
299 }
300 }
301
302 /* Done loading, start game! */
303
304 if (!_networking) {
305 DEBUG(net, 0, "Dedicated server could not be started, aborting");
306 return;
307 }
308
309 while (!_exit_game) {
310 uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
311 InteractiveRandom(); // randomness
312
313 if (!_dedicated_forks) DedicatedHandleKeyInput();
314
315 cur_ticks = GetTime();
316 _realtime_tick += cur_ticks - prev_cur_ticks;
317 if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks || _ddc_fastforward) {
318 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
319
320 GameLoop();
321 UpdateWindows();
322 }
323
324 /* Don't sleep when fast forwarding (for desync debugging) */
325 if (!_ddc_fastforward) {
326 /* Sleep longer on a dedicated server, if the game is paused and no clients connected.
327 * That can allow the CPU to better use deep sleep states. */
328 if (_pause_mode != 0 && !HasClients()) {
329 CSleep(100);
330 } else {
331 CSleep(1);
332 }
333 }
334 }
335 }
336
337 #endif /* ENABLE_NETWORK */