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