# dedicated_v.cpp

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