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);
60 #if defined(UNIX) || defined(PSP) 61 # include <sys/time.h> 62 # include <sys/types.h> 67 # include <sys/fd_set.h> 68 # include <sys/select.h> 72 static void DedicatedSignalHandler(
int sig)
76 signal(sig, DedicatedSignalHandler);
87 # include "../os/windows/win32.h" 88 static HANDLE _hInputReady, _hWaitForInputHandling;
89 static HANDLE _hThread;
90 static char _win_console_thread_buffer[200];
93 static void WINAPI CheckForConsoleInput()
99 SetWin32ThreadName(-1,
"ottd:win-console");
102 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
104 ReadFile(hStdin, _win_console_thread_buffer,
lengthof(_win_console_thread_buffer), &nb, NULL);
105 if (nb >=
lengthof(_win_console_thread_buffer)) nb =
lengthof(_win_console_thread_buffer) - 1;
106 _win_console_thread_buffer[nb] =
'\0';
110 SetEvent(_hInputReady);
111 WaitForSingleObject(_hWaitForInputHandling, INFINITE);
116 static void CreateWindowsConsoleThread()
120 _hInputReady = CreateEvent(NULL,
false,
false, NULL);
121 _hWaitForInputHandling = CreateEvent(NULL,
false,
false, NULL);
122 if (_hInputReady == NULL || _hWaitForInputHandling == NULL)
usererror(
"Cannot create console event!");
124 _hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, NULL, 0, &dwThreadId);
125 if (_hThread == NULL)
usererror(
"Cannot create console thread!");
127 DEBUG(driver, 2,
"Windows console thread started");
130 static void CloseWindowsConsoleThread()
132 CloseHandle(_hThread);
133 CloseHandle(_hInputReady);
134 CloseHandle(_hWaitForInputHandling);
135 DEBUG(driver, 2,
"Windows console thread shut down");
140 #include "../safeguards.h" 143 static void *_dedicated_video_mem;
146 bool _dedicated_forks;
160 _screen.dst_ptr = _dedicated_video_mem;
169 CreateWindowsConsoleThread();
170 SetConsoleTitle(_T(
"OpenTTD Dedicated Server"));
175 _set_error_mode(_OUT_TO_STDERR);
180 OS2_SwitchToConsoleMode();
183 DEBUG(driver, 1,
"Loading dedicated server");
190 CloseWindowsConsoleThread();
192 free(_dedicated_video_mem);
199 #if defined(UNIX) || defined(__OS2__) || defined(PSP) 200 static bool InputWaiting()
209 FD_SET(STDIN, &readfds);
212 return select(STDIN + 1, &readfds, NULL, NULL, &tv) > 0;
215 static uint32 GetTime()
219 gettimeofday(&tim, NULL);
220 return tim.tv_usec / 1000 + tim.tv_sec * 1000;
225 static bool InputWaiting()
227 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
230 static uint32 GetTime()
232 return GetTickCount();
237 static void DedicatedHandleKeyInput()
239 static char input_line[1024] =
"";
241 if (!InputWaiting())
return;
243 if (_exit_game)
return;
245 #if defined(UNIX) || defined(__OS2__) || defined(PSP) 246 if (fgets(input_line,
lengthof(input_line), stdin) == NULL)
return;
249 assert_compile(
lengthof(_win_console_thread_buffer) <=
lengthof(input_line));
250 strecpy(input_line, _win_console_thread_buffer,
lastof(input_line));
251 SetEvent(_hWaitForInputHandling);
255 for (
char *c = input_line; *c !=
'\0'; c++) {
256 if (*c ==
'\n' || *c ==
'\r' || c ==
lastof(input_line)) {
268 uint32 cur_ticks = GetTime();
272 #if defined(UNIX) || defined(PSP) 273 signal(SIGTERM, DedicatedSignalHandler);
274 signal(SIGINT, DedicatedSignalHandler);
275 signal(SIGQUIT, DedicatedSignalHandler);
294 DEBUG(net, 0,
"Loading requested map failed, aborting");
305 DEBUG(net, 0,
"Dedicated server could not be started, aborting");
309 while (!_exit_game) {
310 uint32 prev_cur_ticks = cur_ticks;
313 if (!_dedicated_forks) DedicatedHandleKeyInput();
315 cur_ticks = GetTime();
317 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.
void StartNewGameWithoutGUI(uint seed)
Start a normal game without the GUI.
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.
static const uint GENERATE_NEW_SEED
Create a new random seed.
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.
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.
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.