# dedicated_v.cpp

Go to the documentation of this file.

    00001 /* $Id: dedicated_v.cpp 26209 2014-01-02 22:41:58Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "../stdafx.h"
    00013
    00014 #ifdef ENABLE_NETWORK
    00015
    00016 #include "../gfx_func.h"
    00017 #include "../network/network.h"
    00018 #include "../network/network_internal.h"
    00019 #include "../console_func.h"
    00020 #include "../genworld.h"
    00021 #include "../fileio_type.h"
    00022 #include "../fios.h"
    00023 #include "../blitter/factory.hpp"
    00024 #include "../company_func.h"
    00025 #include "../core/random_func.hpp"
    00026 #include "../saveload/saveload.h"
    00027 #include "dedicated_v.h"
    00028
    00029 #ifdef BEOS_NET_SERVER
    00030 #include <net/socket.h>
    00031 #endif
    00032
    00033 #ifdef __OS2__
    00034 # include <sys/time.h> /* gettimeofday */
    00035 # include <sys/types.h>
    00036 # include <unistd.h>
    00037 # include <conio.h>
    00038
    00039 # define INCL_DOS
    00040 # include <os2.h>
    00041
    00042 # define STDIN 0  /* file descriptor for standard input */
    00043
    00048 static void OS2_SwitchToConsoleMode()
    00049 {
    00050   PPIB pib;
    00051   PTIB tib;
    00052
    00053   DosGetInfoBlocks(&tib, &pib);
    00054
    00055   /* Change flag from PM to VIO */
    00056   pib->pib_ultype = 3;
    00057 }
    00058 #endif
    00059
    00060 #if defined(UNIX) || defined(PSP)
    00061 # include <sys/time.h> /* gettimeofday */
    00062 # include <sys/types.h>
    00063 # include <unistd.h>
    00064 # include <signal.h>
    00065 # define STDIN 0  /* file descriptor for standard input */
    00066 # if defined(PSP)
    00067 #   include <sys/fd_set.h>
    00068 #   include <sys/select.h>
    00069 # endif /* PSP */
    00070
    00071 /* Signal handlers */
    00072 static void DedicatedSignalHandler(int sig)
    00073 {
    00074   if (_game_mode == GM_NORMAL && _settings_client.gui.autosave_on_exit) DoExitSave();
    00075   _exit_game = true;
    00076   signal(sig, DedicatedSignalHandler);
    00077 }
    00078 #endif
    00079
    00080 #if defined(WIN32)
    00081 # include <windows.h> /* GetTickCount */
    00082 # if !defined(WINCE)
    00083 #  include <conio.h>
    00084 # endif
    00085 # include <time.h>
    00086 # include <tchar.h>
    00087 static HANDLE _hInputReady, _hWaitForInputHandling;
    00088 static HANDLE _hThread; // Thread to close
    00089 static char _win_console_thread_buffer[200];
    00090
    00091 /* Windows Console thread. Just loop and signal when input has been received */
    00092 static void WINAPI CheckForConsoleInput()
    00093 {
    00094 #if defined(WINCE)
    00095   /* WinCE doesn't support console stuff */
    00096   return;
    00097 #else
    00098   DWORD nb;
    00099   HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
    00100   for (;;) {
    00101     ReadFile(hStdin, _win_console_thread_buffer, lengthof(_win_console_thread_buffer), &nb, NULL);
    00102     /* Signal input waiting that input is read and wait for it being handled
    00103      * SignalObjectAndWait() should be used here, but it's unsupported in Win98< */
    00104     SetEvent(_hInputReady);
    00105     WaitForSingleObject(_hWaitForInputHandling, INFINITE);
    00106   }
    00107 #endif
    00108 }
    00109
    00110 static void CreateWindowsConsoleThread()
    00111 {
    00112   DWORD dwThreadId;
    00113   /* Create event to signal when console input is ready */
    00114   _hInputReady = CreateEvent(NULL, false, false, NULL);
    00115   _hWaitForInputHandling = CreateEvent(NULL, false, false, NULL);
    00116   if (_hInputReady == NULL || _hWaitForInputHandling == NULL) usererror("Cannot create console event!");
    00117
    00118   _hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, NULL, 0, &dwThreadId);
    00119   if (_hThread == NULL) usererror("Cannot create console thread!");
    00120
    00121   DEBUG(driver, 2, "Windows console thread started");
    00122 }
    00123
    00124 static void CloseWindowsConsoleThread()
    00125 {
    00126   CloseHandle(_hThread);
    00127   CloseHandle(_hInputReady);
    00128   CloseHandle(_hWaitForInputHandling);
    00129   DEBUG(driver, 2, "Windows console thread shut down");
    00130 }
    00131
    00132 #endif
    00133
    00134
    00135 static void *_dedicated_video_mem;
    00136
    00137 /* Whether a fork has been done. */
    00138 bool _dedicated_forks;
    00139
    00140 extern bool SafeLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL);
    00141
    00142 static FVideoDriver_Dedicated iFVideoDriver_Dedicated;
    00143
    00144
    00145 const char *VideoDriver_Dedicated::Start(const char * const *parm)
    00146 {
    00147   int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
    00148   _dedicated_video_mem = (bpp == 0) ? NULL : MallocT<byte>(_cur_resolution.width * _cur_resolution.height * (bpp / 8));
    00149
    00150   _screen.width  = _screen.pitch = _cur_resolution.width;
    00151   _screen.height = _cur_resolution.height;
    00152   _screen.dst_ptr = _dedicated_video_mem;
    00153   ScreenSizeChanged();
    00154   BlitterFactory::GetCurrentBlitter()->PostResize();
    00155
    00156 #if defined(WINCE)
    00157   /* WinCE doesn't support console stuff */
    00158 #elif defined(WIN32)
    00159   /* For win32 we need to allocate a console (debug mode does the same) */
    00160   CreateConsole();
    00161   CreateWindowsConsoleThread();
    00162   SetConsoleTitle(_T("OpenTTD Dedicated Server"));
    00163 #endif
    00164
    00165 #ifdef _MSC_VER
    00166   /* Disable the MSVC assertion message box. */
    00167   _set_error_mode(_OUT_TO_STDERR);
    00168 #endif
    00169
    00170 #ifdef __OS2__
    00171   /* For OS/2 we also need to switch to console mode instead of PM mode */
    00172   OS2_SwitchToConsoleMode();
    00173 #endif
    00174
    00175   DEBUG(driver, 1, "Loading dedicated server");
    00176   return NULL;
    00177 }
    00178
    00179 void VideoDriver_Dedicated::Stop()
    00180 {
    00181 #ifdef WIN32
    00182   CloseWindowsConsoleThread();
    00183 #endif
    00184   free(_dedicated_video_mem);
    00185 }
    00186
    00187 void VideoDriver_Dedicated::MakeDirty(int left, int top, int width, int height) {}
    00188 bool VideoDriver_Dedicated::ChangeResolution(int w, int h) { return false; }
    00189 bool VideoDriver_Dedicated::ToggleFullscreen(bool fs) { return false; }
    00190
    00191 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
    00192 static bool InputWaiting()
    00193 {
    00194   struct timeval tv;
    00195   fd_set readfds;
    00196
    00197   tv.tv_sec = 0;
    00198   tv.tv_usec = 1;
    00199
    00200   FD_ZERO(&readfds);
    00201   FD_SET(STDIN, &readfds);
    00202
    00203   /* don't care about writefds and exceptfds: */
    00204   return select(STDIN + 1, &readfds, NULL, NULL, &tv) > 0;
    00205 }
    00206
    00207 static uint32 GetTime()
    00208 {
    00209   struct timeval tim;
    00210
    00211   gettimeofday(&tim, NULL);
    00212   return tim.tv_usec / 1000 + tim.tv_sec * 1000;
    00213 }
    00214
    00215 #else
    00216
    00217 static bool InputWaiting()
    00218 {
    00219   return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
    00220 }
    00221
    00222 static uint32 GetTime()
    00223 {
    00224   return GetTickCount();
    00225 }
    00226
    00227 #endif
    00228
    00229 static void DedicatedHandleKeyInput()
    00230 {
    00231   static char input_line[1024] = "";
    00232
    00233   if (!InputWaiting()) return;
    00234
    00235   if (_exit_game) return;
    00236
    00237 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
    00238   if (fgets(input_line, lengthof(input_line), stdin) == NULL) return;
    00239 #else
    00240   /* Handle console input, and signal console thread, it can accept input again */
    00241   assert_compile(lengthof(_win_console_thread_buffer) <= lengthof(input_line));
    00242   strecpy(input_line, _win_console_thread_buffer, lastof(input_line));
    00243   SetEvent(_hWaitForInputHandling);
    00244 #endif
    00245
    00246   /* strtok() does not 'forget' \r\n if the string starts with it,
    00247    * so we have to manually remove that! */
    00248   strtok(input_line, "\r\n");
    00249   for (char *c = input_line; *c != '\0'; c++) {
    00250     if (*c == '\n' || *c == '\r' || c == lastof(input_line)) {
    00251       *c = '\0';
    00252       break;
    00253     }
    00254   }
    00255   str_validate(input_line, lastof(input_line));
    00256
    00257   IConsoleCmdExec(input_line); // execute command
    00258 }
    00259
    00260 void VideoDriver_Dedicated::MainLoop()
    00261 {
    00262   uint32 cur_ticks = GetTime();
    00263   uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
    00264
    00265   /* Signal handlers */
    00266 #if defined(UNIX) || defined(PSP)
    00267   signal(SIGTERM, DedicatedSignalHandler);
    00268   signal(SIGINT, DedicatedSignalHandler);
    00269   signal(SIGQUIT, DedicatedSignalHandler);
    00270 #endif
    00271
    00272   /* Load the dedicated server stuff */
    00273   _is_network_server = true;
    00274   _network_dedicated = true;
    00275   _current_company = _local_company = COMPANY_SPECTATOR;
    00276
    00277   /* If SwitchMode is SM_LOAD_GAME, it means that the user used the '-g' options */
    00278   if (_switch_mode != SM_LOAD_GAME) {
    00279     StartNewGameWithoutGUI(GENERATE_NEW_SEED);
    00280     SwitchToMode(_switch_mode);
    00281     _switch_mode = SM_NONE;
    00282   } else {
    00283     _switch_mode = SM_NONE;
    00284     /* First we need to test if the savegame can be loaded, else we will end up playing the
    00285      *  intro game... */
    00286     if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, BASE_DIR)) {
    00287       /* Loading failed, pop out.. */
    00288       DEBUG(net, 0, "Loading requested map failed, aborting");
    00289       _networking = false;
    00290     } else {
    00291       /* We can load this game, so go ahead */
    00292       SwitchToMode(SM_LOAD_GAME);
    00293     }
    00294   }
    00295
    00296   /* Done loading, start game! */
    00297
    00298   if (!_networking) {
    00299     DEBUG(net, 0, "Dedicated server could not be started, aborting");
    00300     return;
    00301   }
    00302
    00303   while (!_exit_game) {
    00304     uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
    00305     InteractiveRandom(); // randomness
    00306
    00307     if (!_dedicated_forks) DedicatedHandleKeyInput();
    00308
    00309     cur_ticks = GetTime();
    00310     _realtime_tick += cur_ticks - prev_cur_ticks;
    00311     if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks || _ddc_fastforward) {
    00312       next_tick = cur_ticks + MILLISECONDS_PER_TICK;
    00313
    00314       GameLoop();
    00315       UpdateWindows();
    00316     }
    00317
    00318     /* Don't sleep when fast forwarding (for desync debugging) */
    00319     if (!_ddc_fastforward) CSleep(1);
    00320   }
    00321 }
    00322
    00323 #endif /* ENABLE_NETWORK */