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
00142 static FVideoDriver_Dedicated iFVideoDriver_Dedicated;
00143
00144
00145 const char *VideoDriver_Dedicated::Start(const char * const *parm)
00146 {
00147 int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
00148 _dedicated_video_mem = (bpp == 0) ? NULL : MallocT<byte>(_cur_resolution.width * _cur_resolution.height * (bpp / 8));
00149
00150 _screen.width = _screen.pitch = _cur_resolution.width;
00151 _screen.height = _cur_resolution.height;
00152 _screen.dst_ptr = _dedicated_video_mem;
00153 ScreenSizeChanged();
00154 BlitterFactoryBase::GetCurrentBlitter()->PostResize();
00155
00156 #if defined(WINCE)
00157
00158 #elif defined(WIN32)
00159
00160 CreateConsole();
00161 CreateWindowsConsoleThread();
00162 SetConsoleTitle(_T("OpenTTD Dedicated Server"));
00163 #endif
00164
00165 #ifdef _MSC_VER
00166
00167 _set_error_mode(_OUT_TO_STDERR);
00168 #endif
00169
00170 #ifdef __OS2__
00171
00172 OS2_SwitchToConsoleMode();
00173 #endif
00174
00175 DEBUG(driver, 1, "Loading dedicated server");
00176 return NULL;
00177 }
00178
00179 void VideoDriver_Dedicated::Stop()
00180 {
00181 #ifdef WIN32
00182 CloseWindowsConsoleThread();
00183 #endif
00184 free(_dedicated_video_mem);
00185 }
00186
00187 void VideoDriver_Dedicated::MakeDirty(int left, int top, int width, int height) {}
00188 bool VideoDriver_Dedicated::ChangeResolution(int w, int h) { return false; }
00189 bool VideoDriver_Dedicated::ToggleFullscreen(bool fs) { return false; }
00190
00191 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
00192 static bool InputWaiting()
00193 {
00194 struct timeval tv;
00195 fd_set readfds;
00196
00197 tv.tv_sec = 0;
00198 tv.tv_usec = 1;
00199
00200 FD_ZERO(&readfds);
00201 FD_SET(STDIN, &readfds);
00202
00203
00204 return select(STDIN + 1, &readfds, NULL, NULL, &tv) > 0;
00205 }
00206
00207 static uint32 GetTime()
00208 {
00209 struct timeval tim;
00210
00211 gettimeofday(&tim, NULL);
00212 return tim.tv_usec / 1000 + tim.tv_sec * 1000;
00213 }
00214
00215 #else
00216
00217 static bool InputWaiting()
00218 {
00219 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
00220 }
00221
00222 static uint32 GetTime()
00223 {
00224 return GetTickCount();
00225 }
00226
00227 #endif
00228
00229 static void DedicatedHandleKeyInput()
00230 {
00231 static char input_line[1024] = "";
00232
00233 if (!InputWaiting()) return;
00234
00235 if (_exit_game) return;
00236
00237 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
00238 if (fgets(input_line, lengthof(input_line), stdin) == NULL) return;
00239 #else
00240
00241 assert_compile(lengthof(_win_console_thread_buffer) <= lengthof(input_line));
00242 strecpy(input_line, _win_console_thread_buffer, lastof(input_line));
00243 SetEvent(_hWaitForInputHandling);
00244 #endif
00245
00246
00247
00248 strtok(input_line, "\r\n");
00249 for (char *c = input_line; *c != '\0'; c++) {
00250 if (*c == '\n' || *c == '\r' || c == lastof(input_line)) {
00251 *c = '\0';
00252 break;
00253 }
00254 }
00255 str_validate(input_line, lastof(input_line));
00256
00257 IConsoleCmdExec(input_line);
00258 }
00259
00260 void VideoDriver_Dedicated::MainLoop()
00261 {
00262 uint32 cur_ticks = GetTime();
00263 uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00264
00265
00266 #if defined(UNIX) || defined(PSP)
00267 signal(SIGTERM, DedicatedSignalHandler);
00268 signal(SIGINT, DedicatedSignalHandler);
00269 signal(SIGQUIT, DedicatedSignalHandler);
00270 #endif
00271
00272
00273 _is_network_server = true;
00274 _network_dedicated = true;
00275 _current_company = _local_company = COMPANY_SPECTATOR;
00276
00277
00278 if (_switch_mode != SM_LOAD_GAME) {
00279 StartNewGameWithoutGUI(GENERATE_NEW_SEED);
00280 SwitchToMode(_switch_mode);
00281 _switch_mode = SM_NONE;
00282 } else {
00283 _switch_mode = SM_NONE;
00284
00285
00286 if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, BASE_DIR)) {
00287
00288 DEBUG(net, 0, "Loading requested map failed, aborting");
00289 _networking = false;
00290 } else {
00291
00292 SwitchToMode(SM_LOAD_GAME);
00293 }
00294 }
00295
00296
00297
00298 if (!_networking) {
00299 DEBUG(net, 0, "Dedicated server could not be started, aborting");
00300 return;
00301 }
00302
00303 while (!_exit_game) {
00304 uint32 prev_cur_ticks = cur_ticks;
00305 InteractiveRandom();
00306
00307 if (!_dedicated_forks) DedicatedHandleKeyInput();
00308
00309 cur_ticks = GetTime();
00310 _realtime_tick += cur_ticks - prev_cur_ticks;
00311 if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks || _ddc_fastforward) {
00312 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00313
00314 GameLoop();
00315 UpdateWindows();
00316 }
00317
00318
00319 if (!_ddc_fastforward) CSleep(1);
00320 }
00321 }
00322
00323 #endif