dedicated_v.cpp

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

Generated on Wed Mar 31 22:43:31 2010 for OpenTTD by  doxygen 1.6.1