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