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 26496 2014-04-24 17:49:31Z frosch $ */
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 static HANDLE _hInputReady, _hWaitForInputHandling;
88 static HANDLE _hThread; // Thread to close
89 static char _win_console_thread_buffer[200];
90
91 /* Windows Console thread. Just loop and signal when input has been received */
92 static void WINAPI CheckForConsoleInput()
93 {
94 #if defined(WINCE)
95 /* WinCE doesn't support console stuff */
96 return;
97 #else
98 DWORD nb;
99 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
100 for (;;) {
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';
104
105 /* Signal input waiting that input is read and wait for it being handled
106 * SignalObjectAndWait() should be used here, but it's unsupported in Win98< */
107 SetEvent(_hInputReady);
108 WaitForSingleObject(_hWaitForInputHandling, INFINITE);
109 }
110 #endif
111 }
112
113 static void CreateWindowsConsoleThread()
114 {
115 DWORD dwThreadId;
116 /* Create event to signal when console input is ready */
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!");
120
121 _hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, NULL, 0, &dwThreadId);
122 if (_hThread == NULL) usererror("Cannot create console thread!");
123
124 DEBUG(driver, 2, "Windows console thread started");
125 }
126
127 static void CloseWindowsConsoleThread()
128 {
129 CloseHandle(_hThread);
130 CloseHandle(_hInputReady);
131 CloseHandle(_hWaitForInputHandling);
132 DEBUG(driver, 2, "Windows console thread shut down");
133 }
134
135 #endif
136
137 #include "../safeguards.h"
138
139
140 static void *_dedicated_video_mem;
141
142 /* Whether a fork has been done. */
143 bool _dedicated_forks;
144
145 extern bool SafeLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL);
146
147 static FVideoDriver_Dedicated iFVideoDriver_Dedicated;
148
149
150 const char *VideoDriver_Dedicated::Start(const char * const *parm)
151 {
152 int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
153 _dedicated_video_mem = (bpp == 0) ? NULL : MallocT<byte>(_cur_resolution.width * _cur_resolution.height * (bpp / 8));
154
155 _screen.width = _screen.pitch = _cur_resolution.width;
156 _screen.height = _cur_resolution.height;
157 _screen.dst_ptr = _dedicated_video_mem;
158 ScreenSizeChanged();
159 BlitterFactory::GetCurrentBlitter()->PostResize();
160
161 #if defined(WINCE)
162 /* WinCE doesn't support console stuff */
163 #elif defined(WIN32)
164 /* For win32 we need to allocate a console (debug mode does the same) */
165 CreateConsole();
166 CreateWindowsConsoleThread();
167 SetConsoleTitle(_T("OpenTTD Dedicated Server"));
168 #endif
169
170 #ifdef _MSC_VER
171 /* Disable the MSVC assertion message box. */
172 _set_error_mode(_OUT_TO_STDERR);
173 #endif
174
175 #ifdef __OS2__
176 /* For OS/2 we also need to switch to console mode instead of PM mode */
177 OS2_SwitchToConsoleMode();
178 #endif
179
180 DEBUG(driver, 1, "Loading dedicated server");
181 return NULL;
182 }
183
184 void VideoDriver_Dedicated::Stop()
185 {
186 #ifdef WIN32
187 CloseWindowsConsoleThread();
188 #endif
189 free(_dedicated_video_mem);
190 }
191
192 void VideoDriver_Dedicated::MakeDirty(int left, int top, int width, int height) {}
193 bool VideoDriver_Dedicated::ChangeResolution(int w, int h) { return false; }
194 bool VideoDriver_Dedicated::ToggleFullscreen(bool fs) { return false; }
195
196 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
197 static bool InputWaiting()
198 {
199 struct timeval tv;
200 fd_set readfds;
201
202 tv.tv_sec = 0;
203 tv.tv_usec = 1;
204
205 FD_ZERO(&readfds);
206 FD_SET(STDIN, &readfds);
207
208 /* don't care about writefds and exceptfds: */
209 return select(STDIN + 1, &readfds, NULL, NULL, &tv) > 0;
210 }
211
212 static uint32 GetTime()
213 {
214 struct timeval tim;
215
216 gettimeofday(&tim, NULL);
217 return tim.tv_usec / 1000 + tim.tv_sec * 1000;
218 }
219
220 #else
221
222 static bool InputWaiting()
223 {
224 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
225 }
226
227 static uint32 GetTime()
228 {
229 return GetTickCount();
230 }
231
232 #endif
233
234 static void DedicatedHandleKeyInput()
235 {
236 static char input_line[1024] = "";
237
238 if (!InputWaiting()) return;
239
240 if (_exit_game) return;
241
242 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
243 if (fgets(input_line, lengthof(input_line), stdin) == NULL) return;
244 #else
245 /* Handle console input, and signal console thread, it can accept input again */
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);
249 #endif
250
251 /* Remove trailing \r or \n */
252 for (char *c = input_line; *c != '\0'; c++) {
253 if (*c == '\n' || *c == '\r' || c == lastof(input_line)) {
254 *c = '\0';
255 break;
256 }
257 }
258 str_validate(input_line, lastof(input_line));
259
260 IConsoleCmdExec(input_line); // execute command
261 }
262
263 void VideoDriver_Dedicated::MainLoop()
264 {
265 uint32 cur_ticks = GetTime();
266 uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
267
268 /* Signal handlers */
269 #if defined(UNIX) || defined(PSP)
270 signal(SIGTERM, DedicatedSignalHandler);
271 signal(SIGINT, DedicatedSignalHandler);
272 signal(SIGQUIT, DedicatedSignalHandler);
273 #endif
274
275 /* Load the dedicated server stuff */
276 _is_network_server = true;
277 _network_dedicated = true;
278 _current_company = _local_company = COMPANY_SPECTATOR;
279
280 /* If SwitchMode is SM_LOAD_GAME, it means that the user used the '-g' options */
281 if (_switch_mode != SM_LOAD_GAME) {
282 StartNewGameWithoutGUI(GENERATE_NEW_SEED);
283 SwitchToMode(_switch_mode);
284 _switch_mode = SM_NONE;
285 } else {
286 _switch_mode = SM_NONE;
287 /* First we need to test if the savegame can be loaded, else we will end up playing the
288 * intro game... */
289 if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, BASE_DIR)) {
290 /* Loading failed, pop out.. */
291 DEBUG(net, 0, "Loading requested map failed, aborting");
292 _networking = false;
293 } else {
294 /* We can load this game, so go ahead */
295 SwitchToMode(SM_LOAD_GAME);
296 }
297 }
298
299 /* Done loading, start game! */
300
301 if (!_networking) {
302 DEBUG(net, 0, "Dedicated server could not be started, aborting");
303 return;
304 }
305
306 while (!_exit_game) {
307 uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
308 InteractiveRandom(); // randomness
309
310 if (!_dedicated_forks) DedicatedHandleKeyInput();
311
312 cur_ticks = GetTime();
313 _realtime_tick += cur_ticks - prev_cur_ticks;
314 if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks || _ddc_fastforward) {
315 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
316
317 GameLoop();
318 UpdateWindows();
319 }
320
321 /* Don't sleep when fast forwarding (for desync debugging) */
322 if (!_ddc_fastforward) {
323 /* Sleep longer on a dedicated server, if the game is paused and no clients connected.
324 * That can allow the CPU to better use deep sleep states. */
325 if (_pause_mode != 0 && !HasClients()) {
326 CSleep(100);
327 } else {
328 CSleep(1);
329 }
330 }
331 }
332 }
333
334 #endif /* ENABLE_NETWORK */