10 #include "../stdafx.h" 12 #include "../gfx_func.h" 13 #include "../network/network.h" 14 #include "../network/network_internal.h" 15 #include "../console_func.h" 16 #include "../genworld.h" 17 #include "../fileio_type.h" 19 #include "../blitter/factory.hpp" 20 #include "../company_func.h" 21 #include "../core/random_func.hpp" 22 #include "../saveload/saveload.h" 23 #include "../thread.h" 27 # include <sys/time.h> 28 # include <sys/types.h> 41 static void OS2_SwitchToConsoleMode()
46 DosGetInfoBlocks(&tib, &pib);
54 # include <sys/time.h> 55 # include <sys/types.h> 61 static void DedicatedSignalHandler(
int sig)
65 signal(sig, DedicatedSignalHandler);
74 # include "../os/windows/win32.h" 76 static HANDLE _hInputReady, _hWaitForInputHandling;
77 static HANDLE _hThread;
78 static char _win_console_thread_buffer[200];
81 static void WINAPI CheckForConsoleInput()
86 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
88 ReadFile(hStdin, _win_console_thread_buffer,
lengthof(_win_console_thread_buffer), &nb,
nullptr);
89 if (nb >=
lengthof(_win_console_thread_buffer)) nb =
lengthof(_win_console_thread_buffer) - 1;
90 _win_console_thread_buffer[nb] =
'\0';
94 SetEvent(_hInputReady);
95 WaitForSingleObject(_hWaitForInputHandling, INFINITE);
99 static void CreateWindowsConsoleThread()
103 _hInputReady = CreateEvent(
nullptr,
false,
false,
nullptr);
104 _hWaitForInputHandling = CreateEvent(
nullptr,
false,
false,
nullptr);
105 if (_hInputReady ==
nullptr || _hWaitForInputHandling ==
nullptr)
usererror(
"Cannot create console event!");
107 _hThread = CreateThread(
nullptr, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput,
nullptr, 0, &dwThreadId);
108 if (_hThread ==
nullptr)
usererror(
"Cannot create console thread!");
110 DEBUG(driver, 2,
"Windows console thread started");
113 static void CloseWindowsConsoleThread()
115 CloseHandle(_hThread);
116 CloseHandle(_hInputReady);
117 CloseHandle(_hWaitForInputHandling);
118 DEBUG(driver, 2,
"Windows console thread shut down");
123 #include "../safeguards.h" 126 static void *_dedicated_video_mem;
129 bool _dedicated_forks;
143 _screen.dst_ptr = _dedicated_video_mem;
150 CreateWindowsConsoleThread();
151 SetConsoleTitle(_T(
"OpenTTD Dedicated Server"));
156 _set_error_mode(_OUT_TO_STDERR);
161 OS2_SwitchToConsoleMode();
164 DEBUG(driver, 1,
"Loading dedicated server");
171 CloseWindowsConsoleThread();
173 free(_dedicated_video_mem);
180 #if defined(UNIX) || defined(__OS2__) 181 static bool InputWaiting()
190 FD_SET(STDIN, &readfds);
193 return select(STDIN + 1, &readfds,
nullptr,
nullptr, &tv) > 0;
196 static uint32 GetTime()
200 gettimeofday(&tim,
nullptr);
201 return tim.tv_usec / 1000 + tim.tv_sec * 1000;
206 static bool InputWaiting()
208 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
211 static uint32 GetTime()
213 return GetTickCount();
218 static void DedicatedHandleKeyInput()
220 static char input_line[1024] =
"";
222 if (!InputWaiting())
return;
224 if (_exit_game)
return;
226 #if defined(UNIX) || defined(__OS2__) 227 if (fgets(input_line,
lengthof(input_line), stdin) ==
nullptr)
return;
230 assert_compile(
lengthof(_win_console_thread_buffer) <=
lengthof(input_line));
231 strecpy(input_line, _win_console_thread_buffer,
lastof(input_line));
232 SetEvent(_hWaitForInputHandling);
236 for (
char *c = input_line; *c !=
'\0'; c++) {
237 if (*c ==
'\n' || *c ==
'\r' || c ==
lastof(input_line)) {
249 uint32 cur_ticks = GetTime();
254 signal(SIGTERM, DedicatedSignalHandler);
255 signal(SIGINT, DedicatedSignalHandler);
256 signal(SIGQUIT, DedicatedSignalHandler);
275 DEBUG(net, 0,
"Loading requested map failed, aborting");
286 DEBUG(net, 0,
"Dedicated server could not be started, aborting");
290 while (!_exit_game) {
291 uint32 prev_cur_ticks = cur_ticks;
294 if (!_dedicated_forks) DedicatedHandleKeyInput();
296 cur_ticks = GetTime();
298 if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks ||
_ddc_fastforward) {
bool _networking
are we in networking mode?
uint32 _realtime_tick
The real time in the game.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
void MainLoop() override
Perform the actual drawing.
Load game, Play Scenario.
void SetCurrentThreadName(const char *)
Name the thread this function is called on for the debugger.
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
#define _ddc_fastforward
Helper variable to make the dedicated server go fast until the (first) join.
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
void Stop() override
Stop this driver.
Dimension _cur_resolution
The current resolution.
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit)
#define lastof(x)
Get the last element of an fixed size array.
virtual void PostResize()
Post resize event.
bool SafeLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf=nullptr)
Load the specified savegame but on error do different things.
static const uint32 GENERATE_NEW_SEED
Create a new random seed.
bool _network_dedicated
are we a dedicated server?
Interface for filtering a savegame till it is loaded.
void StartNewGameWithoutGUI(uint32 seed)
Start a normal game without the GUI.
bool _is_network_server
Does this client wants to be a network-server?
The client is spectating.
void str_validate(char *str, const char *last, StringValidationSettings settings)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
GameMode
Mode which defines the state of the game.
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
void IConsoleCmdExec(const char *cmdstr)
Execute a given command passed to us.
ClientSettings _settings_client
The current settings for this game.
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
Base directory for all subdirectories.
bool autosave_on_exit
save an autosave when you quit the game, but do not ask "Do you really want to quit?"
#define lengthof(x)
Return the length of an fixed size array.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
PauseMode _pause_mode
The current pause mode.
SaveLoadOperation
Operation performed on the file.
#define DEBUG(name, level,...)
Output a line of debugging information.
bool HasClients()
Return whether there is any client connected or trying to connect at all.
GUISettings gui
settings related to the GUI
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
SwitchMode _switch_mode
The next mainloop command.
Base for the dedicated video driver.
Factory for the dedicated server video driver.
char name[MAX_PATH]
Name of the file.
SaveLoadOperation file_op
File operation to perform.
CompanyID _current_company
Company currently doing an action.
virtual uint8 GetScreenDepth()=0
Get the screen depth this blitter works for.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
DetailedFileType detail_ftype
Concrete file type (PNG, BMP, old save, etc).
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
DetailedFileType
Kinds of files in each AbstractFileType.
const char * Start(const char *const *param) override
Start this driver.
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.