12 #include "../stdafx.h" 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" 23 #include "../blitter/factory.hpp" 24 #include "../company_func.h" 25 #include "../core/random_func.hpp" 26 #include "../saveload/saveload.h" 29 #ifdef BEOS_NET_SERVER 30 #include <net/socket.h> 34 # include <sys/time.h> 35 # include <sys/types.h> 48 static void OS2_SwitchToConsoleMode()
53 DosGetInfoBlocks(&tib, &pib);
61 # include <sys/time.h> 62 # include <sys/types.h> 68 static void DedicatedSignalHandler(
int sig)
72 signal(sig, DedicatedSignalHandler);
81 # include "../os/windows/win32.h" 82 static HANDLE _hInputReady, _hWaitForInputHandling;
83 static HANDLE _hThread;
84 static char _win_console_thread_buffer[200];
87 static void WINAPI CheckForConsoleInput()
89 SetWin32ThreadName(-1,
"ottd:win-console");
92 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
94 ReadFile(hStdin, _win_console_thread_buffer,
lengthof(_win_console_thread_buffer), &nb, NULL);
95 if (nb >=
lengthof(_win_console_thread_buffer)) nb =
lengthof(_win_console_thread_buffer) - 1;
96 _win_console_thread_buffer[nb] =
'\0';
100 SetEvent(_hInputReady);
101 WaitForSingleObject(_hWaitForInputHandling, INFINITE);
105 static void CreateWindowsConsoleThread()
109 _hInputReady = CreateEvent(NULL,
false,
false, NULL);
110 _hWaitForInputHandling = CreateEvent(NULL,
false,
false, NULL);
111 if (_hInputReady == NULL || _hWaitForInputHandling == NULL)
usererror(
"Cannot create console event!");
113 _hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, NULL, 0, &dwThreadId);
114 if (_hThread == NULL)
usererror(
"Cannot create console thread!");
116 DEBUG(driver, 2,
"Windows console thread started");
119 static void CloseWindowsConsoleThread()
121 CloseHandle(_hThread);
122 CloseHandle(_hInputReady);
123 CloseHandle(_hWaitForInputHandling);
124 DEBUG(driver, 2,
"Windows console thread shut down");
129 #include "../safeguards.h" 132 static void *_dedicated_video_mem;
135 bool _dedicated_forks;
149 _screen.dst_ptr = _dedicated_video_mem;
156 CreateWindowsConsoleThread();
157 SetConsoleTitle(_T(
"OpenTTD Dedicated Server"));
162 _set_error_mode(_OUT_TO_STDERR);
167 OS2_SwitchToConsoleMode();
170 DEBUG(driver, 1,
"Loading dedicated server");
177 CloseWindowsConsoleThread();
179 free(_dedicated_video_mem);
186 #if defined(UNIX) || defined(__OS2__) 187 static bool InputWaiting()
196 FD_SET(STDIN, &readfds);
199 return select(STDIN + 1, &readfds, NULL, NULL, &tv) > 0;
202 static uint32 GetTime()
206 gettimeofday(&tim, NULL);
207 return tim.tv_usec / 1000 + tim.tv_sec * 1000;
212 static bool InputWaiting()
214 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
217 static uint32 GetTime()
219 return GetTickCount();
224 static void DedicatedHandleKeyInput()
226 static char input_line[1024] =
"";
228 if (!InputWaiting())
return;
230 if (_exit_game)
return;
232 #if defined(UNIX) || defined(__OS2__) 233 if (fgets(input_line,
lengthof(input_line), stdin) == NULL)
return;
236 assert_compile(
lengthof(_win_console_thread_buffer) <=
lengthof(input_line));
237 strecpy(input_line, _win_console_thread_buffer,
lastof(input_line));
238 SetEvent(_hWaitForInputHandling);
242 for (
char *c = input_line; *c !=
'\0'; c++) {
243 if (*c ==
'\n' || *c ==
'\r' || c ==
lastof(input_line)) {
255 uint32 cur_ticks = GetTime();
260 signal(SIGTERM, DedicatedSignalHandler);
261 signal(SIGINT, DedicatedSignalHandler);
262 signal(SIGQUIT, DedicatedSignalHandler);
281 DEBUG(net, 0,
"Loading requested map failed, aborting");
292 DEBUG(net, 0,
"Dedicated server could not be started, aborting");
296 while (!_exit_game) {
297 uint32 prev_cur_ticks = cur_ticks;
300 if (!_dedicated_forks) DedicatedHandleKeyInput();
302 cur_ticks = GetTime();
304 if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks ||
_ddc_fastforward) {
const char * Start(const char *const *param)
Start this driver.
bool _networking
are we in networking mode?
uint32 _realtime_tick
The real time in the game.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Load game, Play Scenario.
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
#define _ddc_fastforward
Helper variable to make the dedicated server go fast until the (first) join.
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.
The client is spectating.
virtual void PostResize()
Post resize event.
static const uint32 GENERATE_NEW_SEED
Create a new random seed.
bool _network_dedicated
are we a dedicated server?
CompanyByte _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
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?
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.
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 ToggleFullscreen(bool fullscreen)
Change the full screen setting.
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.
PauseModeByte _pause_mode
The current pause mode.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
void Stop()
Stop this driver.
SaveLoadOperation
Operation performed on the file.
#define DEBUG(name, level,...)
Output a line of debugging information.
void MainLoop()
Perform the actual drawing.
bool HasClients()
Return whether there is any client connected or trying to connect at all.
void MakeDirty(int left, int top, int width, int height)
Mark a particular area dirty.
bool SafeLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf=NULL)
Load the specified savegame but on error do different things.
GUISettings gui
settings related to the GUI
CompanyByte _current_company
Company currently doing an action.
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.
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.
DetailedFileType detail_ftype
Concrete file type (PNG, BMP, old save, etc).
DetailedFileType
Kinds of files in each AbstractFileType.
bool ChangeResolution(int w, int h)
Change the resolution of the window.
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.