00001
00002
00003
00004
00005
00006
00007
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>
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
00043
00048 static void OS2_SwitchToConsoleMode()
00049 {
00050 PPIB pib;
00051 PTIB tib;
00052
00053 DosGetInfoBlocks(&tib, &pib);
00054
00055
00056 pib->pib_ultype = 3;
00057 }
00058 #endif
00059
00060 #if defined(UNIX) || defined(PSP)
00061 # include <sys/time.h>
00062 # include <sys/types.h>
00063 # include <unistd.h>
00064 # include <signal.h>
00065 # define STDIN 0
00066 # if defined(PSP)
00067 # include <sys/fd_set.h>
00068 # include <sys/select.h>
00069 # endif
00070
00071
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>
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;
00089 static char _win_console_thread_buffer[200];
00090
00091
00092 static void WINAPI CheckForConsoleInput()
00093 {
00094 #if defined(WINCE)
00095
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
00103
00104 SetEvent(_hInputReady);
00105 WaitForSingleObject(_hWaitForInputHandling, INFINITE);
00106 }
00107 #endif
00108 }
00109
00110 static void CreateWindowsConsoleThread()
00111 {
00112 DWORD dwThreadId;
00113
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
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
00159 #elif defined(WIN32)
00160
00161 CreateConsole();
00162 CreateWindowsConsoleThread();
00163 SetConsoleTitle(_T("OpenTTD Dedicated Server"));
00164 #endif
00165
00166 #ifdef _MSC_VER
00167
00168 _set_error_mode(_OUT_TO_STDERR);
00169 #endif
00170
00171 #ifdef __OS2__
00172
00173 OS2_SwitchToConsoleMode();
00174 #endif
00175
00176 DEBUG(driver, 1, "Loading dedicated server");
00177 return NULL;
00178 }
00179
00180 void VideoDriver_Dedicated::Stop()
00181 {
00182 #ifdef WIN32
00183 CloseWindowsConsoleThread();
00184 #endif
00185 free(_dedicated_video_mem);
00186 }
00187
00188 void VideoDriver_Dedicated::MakeDirty(int left, int top, int width, int height) {}
00189 bool VideoDriver_Dedicated::ChangeResolution(int w, int h) { return false; }
00190 bool VideoDriver_Dedicated::ToggleFullscreen(bool fs) { return false; }
00191
00192 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
00193 static bool InputWaiting()
00194 {
00195 struct timeval tv;
00196 fd_set readfds;
00197
00198 tv.tv_sec = 0;
00199 tv.tv_usec = 1;
00200
00201 FD_ZERO(&readfds);
00202 FD_SET(STDIN, &readfds);
00203
00204
00205 return select(STDIN + 1, &readfds, NULL, NULL, &tv) > 0;
00206 }
00207
00208 static uint32 GetTime()
00209 {
00210 struct timeval tim;
00211
00212 gettimeofday(&tim, NULL);
00213 return tim.tv_usec / 1000 + tim.tv_sec * 1000;
00214 }
00215
00216 #else
00217
00218 static bool InputWaiting()
00219 {
00220 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
00221 }
00222
00223 static uint32 GetTime()
00224 {
00225 return GetTickCount();
00226 }
00227
00228 #endif
00229
00230 static void DedicatedHandleKeyInput()
00231 {
00232 static char input_line[1024] = "";
00233
00234 if (!InputWaiting()) return;
00235
00236 if (_exit_game) return;
00237
00238 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
00239 if (fgets(input_line, lengthof(input_line), stdin) == NULL) return;
00240 #else
00241
00242 assert_compile(lengthof(_win_console_thread_buffer) <= lengthof(input_line));
00243 strecpy(input_line, _win_console_thread_buffer, lastof(input_line));
00244 SetEvent(_hWaitForInputHandling);
00245 #endif
00246
00247
00248
00249 strtok(input_line, "\r\n");
00250 for (char *c = input_line; *c != '\0'; c++) {
00251 if (*c == '\n' || *c == '\r' || c == lastof(input_line)) {
00252 *c = '\0';
00253 break;
00254 }
00255 }
00256 str_validate(input_line, lastof(input_line));
00257
00258 IConsoleCmdExec(input_line);
00259 }
00260
00261 void VideoDriver_Dedicated::MainLoop()
00262 {
00263 uint32 cur_ticks = GetTime();
00264 uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00265
00266
00267 #if defined(UNIX) || defined(PSP)
00268 signal(SIGTERM, DedicatedSignalHandler);
00269 signal(SIGINT, DedicatedSignalHandler);
00270 signal(SIGQUIT, DedicatedSignalHandler);
00271 #endif
00272
00273
00274 _is_network_server = true;
00275 _network_dedicated = true;
00276 _current_company = _local_company = COMPANY_SPECTATOR;
00277
00278
00279 if (_switch_mode != SM_LOAD_GAME) {
00280 StartNewGameWithoutGUI(GENERATE_NEW_SEED);
00281 SwitchToMode(_switch_mode);
00282 _switch_mode = SM_NONE;
00283 } else {
00284 _switch_mode = SM_NONE;
00285
00286
00287 if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, BASE_DIR)) {
00288
00289 DEBUG(net, 0, "Loading requested map failed, aborting");
00290 _networking = false;
00291 } else {
00292
00293 SwitchToMode(SM_LOAD_GAME);
00294 }
00295 }
00296
00297
00298
00299 if (!_networking) {
00300 DEBUG(net, 0, "Dedicated server could not be started, aborting");
00301 return;
00302 }
00303
00304 while (!_exit_game) {
00305 uint32 prev_cur_ticks = cur_ticks;
00306 InteractiveRandom();
00307
00308 if (!_dedicated_forks) DedicatedHandleKeyInput();
00309
00310 cur_ticks = GetTime();
00311 _realtime_tick += cur_ticks - prev_cur_ticks;
00312 if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks || _ddc_fastforward) {
00313 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00314
00315 GameLoop();
00316 UpdateWindows();
00317 }
00318
00319
00320 if (!_ddc_fastforward) CSleep(1);
00321 }
00322 }
00323
00324 #endif