|
OpenTTD Source
1.11.0-beta1
|
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"
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;
145 _screen.dst_ptr = _dedicated_video_mem;
152 CreateWindowsConsoleThread();
153 SetConsoleTitle(_T(
"OpenTTD Dedicated Server"));
158 _set_error_mode(_OUT_TO_STDERR);
163 OS2_SwitchToConsoleMode();
166 DEBUG(driver, 1,
"Loading dedicated server");
173 CloseWindowsConsoleThread();
175 free(_dedicated_video_mem);
182 #if defined(UNIX) || defined(__OS2__)
183 static bool InputWaiting()
192 FD_SET(STDIN, &readfds);
195 return select(STDIN + 1, &readfds,
nullptr,
nullptr, &tv) > 0;
198 static uint32 GetTime()
202 gettimeofday(&tim,
nullptr);
203 return tim.tv_usec / 1000 + tim.tv_sec * 1000;
208 static bool InputWaiting()
210 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
213 static uint32 GetTime()
215 return GetTickCount();
220 static void DedicatedHandleKeyInput()
222 static char input_line[1024] =
"";
224 if (!InputWaiting())
return;
226 if (_exit_game)
return;
228 #if defined(UNIX) || defined(__OS2__)
229 if (fgets(input_line,
lengthof(input_line), stdin) ==
nullptr)
return;
232 static_assert(
lengthof(_win_console_thread_buffer) <=
lengthof(input_line));
233 strecpy(input_line, _win_console_thread_buffer,
lastof(input_line));
234 SetEvent(_hWaitForInputHandling);
238 for (
char *c = input_line; *c !=
'\0'; c++) {
239 if (*c ==
'\n' || *c ==
'\r' || c ==
lastof(input_line)) {
251 uint32 cur_ticks = GetTime();
256 signal(SIGTERM, DedicatedSignalHandler);
257 signal(SIGINT, DedicatedSignalHandler);
258 signal(SIGQUIT, DedicatedSignalHandler);
277 DEBUG(net, 0,
"Loading requested map failed, aborting");
288 DEBUG(net, 0,
"Dedicated server could not be started, aborting");
292 while (!_exit_game) {
293 uint32 prev_cur_ticks = cur_ticks;
296 if (!_dedicated_forks) DedicatedHandleKeyInput();
298 cur_ticks = GetTime();
300 if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks ||
_ddc_fastforward) {
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
@ SM_LOAD_GAME
Load game, Play Scenario.
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
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...
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?...
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.
bool HasClients()
Return whether there is any client connected or trying to connect at all.
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
int height
Height in pixels of our display surface.
@ BASE_DIR
Base directory for all subdirectories.
#define DEBUG(name, level,...)
Output a line of debugging information.
virtual void PostResize()
Post resize event.
PauseMode _pause_mode
The current pause mode.
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.
bool _networking
are we in networking mode?
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.
int width
Width in pixels of our display surface.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
Interface for filtering a savegame till it is loaded.
#define lengthof(x)
Return the length of an fixed size array.
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.
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.
uint32 _realtime_tick
The real time in the game.
#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.