dedicated_v.cpp

Go to the documentation of this file.
00001 /* $Id: dedicated_v.cpp 21396 2010-12-05 14:43:10Z 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   while (true) {
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 extern void SwitchToMode(SwitchMode new_mode);
00142 
00143 static FVideoDriver_Dedicated iFVideoDriver_Dedicated;
00144 
00145 
00146 const char *VideoDriver_Dedicated::Start(const char * const *parm)
00147 {
00148   int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
00149   _dedicated_video_mem = (bpp == 0) ? NULL : MallocT<byte>(_cur_resolution.width * _cur_resolution.height * (bpp / 8));
00150 
00151   _screen.width  = _screen.pitch = _cur_resolution.width;
00152   _screen.height = _cur_resolution.height;
00153   _screen.dst_ptr = _dedicated_video_mem;
00154   ScreenSizeChanged();
00155   BlitterFactoryBase::GetCurrentBlitter()->PostResize();
00156 
00157 #if defined(WINCE)
00158   /* WinCE doesn't support console stuff */
00159 #elif defined(WIN32)
00160   /* For win32 we need to allocate a console (debug mode does the same) */
00161   CreateConsole();
00162   CreateWindowsConsoleThread();
00163   SetConsoleTitle(_T("OpenTTD Dedicated Server"));
00164 #endif
00165 
00166 #ifdef __OS2__
00167   /* For OS/2 we also need to switch to console mode instead of PM mode */
00168   OS2_SwitchToConsoleMode();
00169 #endif
00170 
00171   DEBUG(driver, 1, "Loading dedicated server");
00172   return NULL;
00173 }
00174 
00175 void VideoDriver_Dedicated::Stop()
00176 {
00177 #ifdef WIN32
00178   CloseWindowsConsoleThread();
00179 #endif
00180   free(_dedicated_video_mem);
00181 }
00182 
00183 void VideoDriver_Dedicated::MakeDirty(int left, int top, int width, int height) {}
00184 bool VideoDriver_Dedicated::ChangeResolution(int w, int h) { return false; }
00185 bool VideoDriver_Dedicated::ToggleFullscreen(bool fs) { return false; }
00186 
00187 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
00188 static bool InputWaiting()
00189 {
00190   struct timeval tv;
00191   fd_set readfds;
00192 
00193   tv.tv_sec = 0;
00194   tv.tv_usec = 1;
00195 
00196   FD_ZERO(&readfds);
00197   FD_SET(STDIN, &readfds);
00198 
00199   /* don't care about writefds and exceptfds: */
00200   return select(STDIN + 1, &readfds, NULL, NULL, &tv) > 0;
00201 }
00202 
00203 static uint32 GetTime()
00204 {
00205   struct timeval tim;
00206 
00207   gettimeofday(&tim, NULL);
00208   return tim.tv_usec / 1000 + tim.tv_sec * 1000;
00209 }
00210 
00211 #else
00212 
00213 static bool InputWaiting()
00214 {
00215   return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
00216 }
00217 
00218 static uint32 GetTime()
00219 {
00220   return GetTickCount();
00221 }
00222 
00223 #endif
00224 
00225 static void DedicatedHandleKeyInput()
00226 {
00227   static char input_line[1024] = "";
00228 
00229   if (!InputWaiting()) return;
00230 
00231   if (_exit_game) return;
00232 
00233 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
00234   if (fgets(input_line, lengthof(input_line), stdin) == NULL) return;
00235 #else
00236   /* Handle console input, and singal console thread, it can accept input again */
00237   assert_compile(lengthof(_win_console_thread_buffer) <= lengthof(input_line));
00238   strecpy(input_line, _win_console_thread_buffer, lastof(input_line));
00239   SetEvent(_hWaitForInputHandling);
00240 #endif
00241 
00242   /* strtok() does not 'forget' \r\n if the string starts with it,
00243    * so we have to manually remove that! */
00244   strtok(input_line, "\r\n");
00245   for (char *c = input_line; *c != '\0'; c++) {
00246     if (*c == '\n' || *c == '\r' || c == lastof(input_line)) {
00247       *c = '\0';
00248       break;
00249     }
00250   }
00251   str_validate(input_line, lastof(input_line));
00252 
00253   IConsoleCmdExec(input_line); // execute command
00254 }
00255 
00256 void VideoDriver_Dedicated::MainLoop()
00257 {
00258   uint32 cur_ticks = GetTime();
00259   uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00260 
00261   /* Signal handlers */
00262 #if defined(UNIX) || defined(PSP)
00263   signal(SIGTERM, DedicatedSignalHandler);
00264   signal(SIGINT, DedicatedSignalHandler);
00265   signal(SIGQUIT, DedicatedSignalHandler);
00266 #endif
00267 
00268   /* Load the dedicated server stuff */
00269   _is_network_server = true;
00270   _network_dedicated = true;
00271   _current_company = _local_company = COMPANY_SPECTATOR;
00272 
00273   /* If SwitchMode is SM_LOAD, it means that the user used the '-g' options */
00274   if (_switch_mode != SM_LOAD) {
00275     StartNewGameWithoutGUI(GENERATE_NEW_SEED);
00276     SwitchToMode(_switch_mode);
00277     _switch_mode = SM_NONE;
00278   } else {
00279     _switch_mode = SM_NONE;
00280     /* First we need to test if the savegame can be loaded, else we will end up playing the
00281      *  intro game... */
00282     if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, BASE_DIR)) {
00283       /* Loading failed, pop out.. */
00284       DEBUG(net, 0, "Loading requested map failed, aborting");
00285       _networking = false;
00286     } else {
00287       /* We can load this game, so go ahead */
00288       SwitchToMode(SM_LOAD);
00289     }
00290   }
00291 
00292   /* Done loading, start game! */
00293 
00294   if (!_networking) {
00295     DEBUG(net, 0, "Dedicated server could not be started, aborting");
00296     return;
00297   }
00298 
00299   while (!_exit_game) {
00300     uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
00301     InteractiveRandom(); // randomness
00302 
00303     if (!_dedicated_forks) DedicatedHandleKeyInput();
00304 
00305     cur_ticks = GetTime();
00306     _realtime_tick += cur_ticks - prev_cur_ticks;
00307     if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks || _ddc_fastforward) {
00308       next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00309 
00310       GameLoop();
00311       UpdateWindows();
00312     }
00313 
00314     /* Don't sleep when fast forwarding (for desync debugging) */
00315     if (!_ddc_fastforward) CSleep(1);
00316   }
00317 }
00318 
00319 #endif /* ENABLE_NETWORK */

Generated on Sun Jan 9 16:02:04 2011 for OpenTTD by  doxygen 1.6.1