# dedicated_v.cpp

Go to the documentation of this file.

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