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