OpenTTD Source
12.0-beta2
|
Go to the documentation of this file.
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"
24 #include "../window_func.h"
28 # include <sys/time.h>
29 # include <sys/types.h>
42 static void OS2_SwitchToConsoleMode()
47 DosGetInfoBlocks(&tib, &pib);
55 # include <sys/time.h>
56 # include <sys/types.h>
62 static void DedicatedSignalHandler(
int sig)
66 signal(sig, DedicatedSignalHandler);
75 # include "../os/windows/win32.h"
77 static HANDLE _hInputReady, _hWaitForInputHandling;
78 static HANDLE _hThread;
79 static char _win_console_thread_buffer[200];
82 static void WINAPI CheckForConsoleInput()
87 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
89 ReadFile(hStdin, _win_console_thread_buffer,
lengthof(_win_console_thread_buffer), &nb,
nullptr);
90 if (nb >=
lengthof(_win_console_thread_buffer)) nb =
lengthof(_win_console_thread_buffer) - 1;
91 _win_console_thread_buffer[nb] =
'\0';
95 SetEvent(_hInputReady);
96 WaitForSingleObject(_hWaitForInputHandling, INFINITE);
100 static void CreateWindowsConsoleThread()
104 _hInputReady = CreateEvent(
nullptr,
false,
false,
nullptr);
105 _hWaitForInputHandling = CreateEvent(
nullptr,
false,
false,
nullptr);
106 if (_hInputReady ==
nullptr || _hWaitForInputHandling ==
nullptr)
usererror(
"Cannot create console event!");
108 _hThread = CreateThread(
nullptr, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput,
nullptr, 0, &dwThreadId);
109 if (_hThread ==
nullptr)
usererror(
"Cannot create console thread!");
111 Debug(driver, 2,
"Windows console thread started");
114 static void CloseWindowsConsoleThread()
116 CloseHandle(_hThread);
117 CloseHandle(_hInputReady);
118 CloseHandle(_hWaitForInputHandling);
119 Debug(driver, 2,
"Windows console thread shut down");
124 #include "../safeguards.h"
127 static void *_dedicated_video_mem;
130 bool _dedicated_forks;
146 _screen.dst_ptr = _dedicated_video_mem;
153 CreateWindowsConsoleThread();
154 SetConsoleTitle(L
"OpenTTD Dedicated Server");
159 _set_error_mode(_OUT_TO_STDERR);
164 OS2_SwitchToConsoleMode();
167 Debug(driver, 1,
"Loading dedicated server");
174 CloseWindowsConsoleThread();
176 free(_dedicated_video_mem);
183 #if defined(UNIX) || defined(__OS2__)
184 static bool InputWaiting()
193 FD_SET(STDIN, &readfds);
196 return select(STDIN + 1, &readfds,
nullptr,
nullptr, &tv) > 0;
201 static bool InputWaiting()
203 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
208 static void DedicatedHandleKeyInput()
210 static char input_line[1024] =
"";
212 if (!InputWaiting())
return;
214 if (_exit_game)
return;
216 #if defined(UNIX) || defined(__OS2__)
217 if (fgets(input_line,
lengthof(input_line), stdin) ==
nullptr)
return;
220 static_assert(
lengthof(_win_console_thread_buffer) <=
lengthof(input_line));
221 strecpy(input_line, _win_console_thread_buffer,
lastof(input_line));
222 SetEvent(_hWaitForInputHandling);
226 for (
char *c = input_line; *c !=
'\0'; c++) {
227 if (*c ==
'\n' || *c ==
'\r' || c ==
lastof(input_line)) {
241 signal(SIGTERM, DedicatedSignalHandler);
242 signal(SIGINT, DedicatedSignalHandler);
243 signal(SIGQUIT, DedicatedSignalHandler);
259 Debug(net, 0,
"Loading requested map failed; closing server.");
267 this->is_game_threaded =
false;
271 while (!_exit_game) {
272 if (!_dedicated_forks) DedicatedHandleKeyInput();
void Tick()
Give the video-driver a tick.
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
@ SM_LOAD_GAME
Load game, Play Scenario.
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit)
bool autosave_on_exit
save an autosave when you quit the game, but do not ask "Do you really want to quit?...
SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
Main Save or Load function where the high-level saveload functions are handled.
SaveLoadOperation
Operation performed on the file.
std::string name
Name of the file.
virtual uint8 GetScreenDepth()=0
Get the screen depth this blitter works for.
ClientSettings _settings_client
The current settings for this game.
void SetCurrentThreadName(const char *)
Name the thread this function is called on for the debugger.
bool SafeLoad(const std::string &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.
@ BASE_DIR
Base directory for all subdirectories.
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
virtual void PostResize()
Post resize event.
Factory for the dedicated server video driver.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
std::vector< std::string > StringList
Type for a list of strings.
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
bool _network_dedicated
are we a dedicated server?
GameMode
Mode which defines the state of the game.
void StartNewGameWithoutGUI(uint32 seed)
Start a normal game without the GUI.
const char * Start(const StringList ¶m) override
Start this driver.
SwitchMode _switch_mode
The next mainloop command.
#define _ddc_fastforward
Helper variable to make the dedicated server go fast until the (first) join.
CompanyID _current_company
Company currently doing an action.
DetailedFileType
Kinds of files in each AbstractFileType.
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
@ COMPANY_SPECTATOR
The client is spectating.
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
void DrainCommandQueue()
Execute all queued commands.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Interface for filtering a savegame till it is loaded.
#define lengthof(x)
Return the length of an fixed size array.
void SleepTillNextTick()
Sleep till the next tick is about to happen.
void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
Execute a given command passed to us.
void MainLoop() override
Perform the actual drawing.
void Stop() override
Stop this driver.
@ SL_ERROR
error that was caught before internal structures were modified
DetailedFileType detail_ftype
Concrete file type (PNG, BMP, old save, etc).
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
#define lastof(x)
Get the last element of an fixed size array.
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Dimension _cur_resolution
The current resolution.
bool _is_network_server
Does this client wants to be a network-server?
GUISettings gui
settings related to the GUI
SaveLoadOperation file_op
File operation to perform.