2004-12-04 17:54:56 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2004-12-04 17:54:56 +00:00
|
|
|
#include "network.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
|
|
|
#include "gfx.h"
|
|
|
|
#include "window.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "console.h"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "variables.h"
|
2004-12-04 17:54:56 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
# include <windows.h> /* GetTickCount */
|
|
|
|
# include <conio.h>
|
|
|
|
#endif
|
2004-12-23 14:46:16 +00:00
|
|
|
|
|
|
|
#ifdef __OS2__
|
|
|
|
# include <sys/time.h> /* gettimeofday */
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <conio.h>
|
|
|
|
# define STDIN 0 /* file descriptor for standard input */
|
2004-12-27 10:40:04 +00:00
|
|
|
|
|
|
|
extern void OS2_SwitchToConsoleMode();
|
2004-12-23 14:46:16 +00:00
|
|
|
#endif
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
#ifdef UNIX
|
|
|
|
# include <sys/time.h> /* gettimeofday */
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <unistd.h>
|
2004-12-18 13:15:41 +00:00
|
|
|
# include <signal.h>
|
2004-12-04 17:54:56 +00:00
|
|
|
# define STDIN 0 /* file descriptor for standard input */
|
|
|
|
#endif
|
2004-12-22 21:12:36 +00:00
|
|
|
#ifdef __MORPHOS__
|
2005-02-04 19:38:04 +00:00
|
|
|
/* Voids the fork, option will be disabled for MorphOS build anyway, because
|
|
|
|
* MorphOS doesn't support forking (could only implemented with lots of code
|
|
|
|
* changes here). */
|
|
|
|
int fork(void) { return -1; }
|
|
|
|
int dup2(int oldd, int newd) { return -1; }
|
2004-12-22 21:12:36 +00:00
|
|
|
#endif
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
// This file handles all dedicated-server in- and outputs
|
|
|
|
|
|
|
|
static void *_dedicated_video_mem;
|
|
|
|
|
2005-03-01 17:32:47 +00:00
|
|
|
extern bool SafeSaveOrLoad(const char *filename, int mode, int newgm);
|
|
|
|
extern void SwitchMode(int new_mode);
|
|
|
|
|
2004-12-18 13:15:41 +00:00
|
|
|
#ifdef UNIX
|
2004-12-18 14:19:21 +00:00
|
|
|
/* We want to fork our dedicated server */
|
2004-12-23 00:24:44 +00:00
|
|
|
void DedicatedFork(void)
|
2004-12-18 14:19:21 +00:00
|
|
|
{
|
|
|
|
/* Fork the program */
|
2005-02-23 09:13:12 +00:00
|
|
|
pid_t pid = fork();
|
|
|
|
switch (pid) {
|
2004-12-18 14:19:21 +00:00
|
|
|
case -1:
|
|
|
|
perror("Unable to fork");
|
|
|
|
exit(1);
|
|
|
|
case 0:
|
|
|
|
// We're the child
|
|
|
|
|
|
|
|
/* Open the log-file to log all stuff too */
|
|
|
|
_log_file_fd = fopen(_log_file, "a");
|
|
|
|
if (!_log_file_fd) {
|
|
|
|
perror("Unable to open logfile");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* Redirect stdout and stderr to log-file */
|
|
|
|
if (dup2(fileno(_log_file_fd), fileno(stdout)) == -1) {
|
2005-04-02 16:46:23 +00:00
|
|
|
perror("Rerouting stdout");
|
2004-12-18 14:19:21 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (dup2(fileno(_log_file_fd), fileno(stderr)) == -1) {
|
2005-04-02 16:46:23 +00:00
|
|
|
perror("Rerouting stderr");
|
2004-12-18 14:19:21 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// We're the parent
|
|
|
|
printf("Loading dedicated server...\n");
|
2005-02-23 09:13:12 +00:00
|
|
|
printf(" - Forked to background with pid %d\n", pid);
|
2004-12-18 14:19:21 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-18 13:15:41 +00:00
|
|
|
/* Signal handlers */
|
2004-12-18 14:19:21 +00:00
|
|
|
static void DedicatedSignalHandler(int sig)
|
2004-12-18 13:15:41 +00:00
|
|
|
{
|
2004-12-20 16:35:16 +00:00
|
|
|
_exit_game = true;
|
|
|
|
signal(sig, DedicatedSignalHandler);
|
2004-12-18 13:15:41 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-01-16 18:19:33 +00:00
|
|
|
#ifdef WIN32
|
2005-01-23 14:15:42 +00:00
|
|
|
#include <time.h>
|
2005-01-16 18:19:33 +00:00
|
|
|
HANDLE hEvent;
|
|
|
|
static HANDLE hThread; // Thread to close
|
|
|
|
static char _win_console_thread_buffer[200];
|
|
|
|
|
|
|
|
/* Windows Console thread. Just loop and signal when input has been received */
|
|
|
|
void WINAPI CheckForConsoleInput(void)
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
fgets(_win_console_thread_buffer, lengthof(_win_console_thread_buffer), stdin);
|
|
|
|
SetEvent(hEvent); // signal input waiting that the line is ready
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateWindowsConsoleThread(void)
|
|
|
|
{
|
2005-01-23 14:15:42 +00:00
|
|
|
static char tbuffer[9];
|
2005-05-04 19:09:25 +00:00
|
|
|
DWORD dwThreadId;
|
2005-01-16 18:19:33 +00:00
|
|
|
/* Create event to signal when console input is ready */
|
2005-01-23 14:15:42 +00:00
|
|
|
hEvent = CreateEvent(NULL, false, false, _strtime(tbuffer));
|
|
|
|
if (hEvent == NULL)
|
|
|
|
error("Cannot create console event!");
|
2005-01-16 18:19:33 +00:00
|
|
|
|
2005-05-04 19:09:25 +00:00
|
|
|
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, NULL, 0, &dwThreadId);
|
2005-01-16 18:19:33 +00:00
|
|
|
if (hThread == NULL)
|
|
|
|
error("Cannot create console thread!");
|
|
|
|
|
|
|
|
DEBUG(misc, 0) ("Windows console thread started...");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseWindowsConsoleThread(void)
|
|
|
|
{
|
|
|
|
CloseHandle(hThread);
|
2005-01-23 14:15:42 +00:00
|
|
|
CloseHandle(hEvent);
|
2005-01-16 18:19:33 +00:00
|
|
|
DEBUG(misc, 0) ("Windows console thread shut down...");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2005-02-05 18:05:42 +00:00
|
|
|
static const char *DedicatedVideoStart(const char * const *parm)
|
|
|
|
{
|
2004-12-04 17:54:56 +00:00
|
|
|
_screen.width = _screen.pitch = _cur_resolution[0];
|
|
|
|
_screen.height = _cur_resolution[1];
|
|
|
|
_dedicated_video_mem = malloc(_cur_resolution[0]*_cur_resolution[1]);
|
|
|
|
|
|
|
|
_debug_net_level = 6;
|
|
|
|
_debug_misc_level = 0;
|
|
|
|
|
2004-12-13 16:51:10 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
// For win32 we need to allocate an console (debug mode does the same)
|
|
|
|
CreateConsole();
|
2005-01-16 18:19:33 +00:00
|
|
|
CreateWindowsConsoleThread();
|
2004-12-13 16:51:10 +00:00
|
|
|
SetConsoleTitle("OpenTTD Dedicated Server");
|
|
|
|
#endif
|
|
|
|
|
2004-12-27 10:40:04 +00:00
|
|
|
#ifdef __OS2__
|
|
|
|
// For OS/2 we also need to switch to console mode instead of PM mode
|
|
|
|
OS2_SwitchToConsoleMode();
|
|
|
|
#endif
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
DEBUG(misc,0)("Loading dedicated server...");
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-01-16 18:19:33 +00:00
|
|
|
|
|
|
|
static void DedicatedVideoStop(void)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
CloseWindowsConsoleThread();
|
|
|
|
#endif
|
2005-01-23 13:09:35 +00:00
|
|
|
free(_dedicated_video_mem);
|
2005-01-16 18:19:33 +00:00
|
|
|
}
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
static void DedicatedVideoMakeDirty(int left, int top, int width, int height) {}
|
|
|
|
static bool DedicatedVideoChangeRes(int w, int h) { return false; }
|
2005-05-16 16:19:32 +00:00
|
|
|
static void DedicatedVideoFullScreen(bool fs) {}
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2005-01-22 18:59:53 +00:00
|
|
|
#if defined(UNIX) || defined(__OS2__)
|
2005-01-16 18:19:33 +00:00
|
|
|
static bool InputWaiting(void)
|
2004-12-04 17:54:56 +00:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set readfds;
|
2004-12-20 16:35:16 +00:00
|
|
|
byte ret;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 1;
|
|
|
|
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(STDIN, &readfds);
|
|
|
|
|
|
|
|
/* don't care about writefds and exceptfds: */
|
2004-12-20 16:35:16 +00:00
|
|
|
ret = select(STDIN + 1, &readfds, NULL, NULL, &tv);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2004-12-20 16:35:16 +00:00
|
|
|
if (ret > 0)
|
2004-12-04 17:54:56 +00:00
|
|
|
return true;
|
2005-01-16 18:19:33 +00:00
|
|
|
|
|
|
|
return false;
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
#else
|
2005-01-16 18:19:33 +00:00
|
|
|
static bool InputWaiting(void)
|
2004-12-04 17:54:56 +00:00
|
|
|
{
|
2005-01-16 18:19:33 +00:00
|
|
|
if (WaitForSingleObject(hEvent, 1) == WAIT_OBJECT_0)
|
|
|
|
return true;
|
2005-01-23 13:09:35 +00:00
|
|
|
|
2005-01-16 18:19:33 +00:00
|
|
|
return false;
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-01-16 18:19:33 +00:00
|
|
|
static void DedicatedHandleKeyInput(void)
|
2004-12-18 14:19:21 +00:00
|
|
|
{
|
2004-12-19 14:42:17 +00:00
|
|
|
static char input_line[200] = "";
|
2004-12-18 14:19:21 +00:00
|
|
|
|
2005-01-16 18:19:33 +00:00
|
|
|
if (!InputWaiting())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (_exit_game)
|
|
|
|
return;
|
|
|
|
|
2004-12-23 14:46:16 +00:00
|
|
|
#if defined(UNIX) || defined(__OS2__)
|
2005-01-16 18:19:33 +00:00
|
|
|
fgets(input_line, lengthof(input_line), stdin);
|
2004-12-18 14:19:21 +00:00
|
|
|
#else
|
2005-01-16 18:19:33 +00:00
|
|
|
strncpy(input_line, _win_console_thread_buffer, lengthof(input_line));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* XXX - strtok() does not 'forget' \n\r if it is the first character! */
|
|
|
|
strtok(input_line, "\r\n"); // Forget about the final \n (or \r)
|
|
|
|
{ /* Remove any special control characters */
|
|
|
|
uint i;
|
|
|
|
for (i = 0; i < lengthof(input_line); i++) {
|
|
|
|
if (input_line[i] == '\n' || input_line[i] == '\r') // cut missed beginning '\0'
|
|
|
|
input_line[i] = '\0';
|
|
|
|
|
|
|
|
if (input_line[i] == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!IS_INT_INSIDE(input_line[i], ' ', 256))
|
|
|
|
input_line[i] = ' ';
|
2004-12-18 14:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
2005-01-16 18:19:33 +00:00
|
|
|
|
|
|
|
IConsoleCmdExec(input_line); // execute command
|
2004-12-18 14:19:21 +00:00
|
|
|
}
|
|
|
|
|
2005-01-16 18:19:33 +00:00
|
|
|
static int DedicatedVideoMainLoop(void)
|
2004-12-18 14:19:21 +00:00
|
|
|
{
|
2004-12-04 17:54:56 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
struct timeval tim;
|
|
|
|
#endif
|
|
|
|
uint32 next_tick;
|
|
|
|
uint32 cur_ticks;
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
next_tick = GetTickCount() + 30;
|
|
|
|
#else
|
|
|
|
gettimeofday(&tim, NULL);
|
|
|
|
next_tick = (tim.tv_usec / 1000) + 30 + (tim.tv_sec * 1000);
|
|
|
|
#endif
|
|
|
|
|
2004-12-22 21:12:36 +00:00
|
|
|
/* Signal handlers */
|
2004-12-18 13:15:41 +00:00
|
|
|
#ifdef UNIX
|
|
|
|
signal(SIGTERM, DedicatedSignalHandler);
|
|
|
|
signal(SIGINT, DedicatedSignalHandler);
|
2004-12-20 16:35:16 +00:00
|
|
|
signal(SIGQUIT, DedicatedSignalHandler);
|
2004-12-18 13:15:41 +00:00
|
|
|
#endif
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
// Load the dedicated server stuff
|
|
|
|
_is_network_server = true;
|
|
|
|
_network_dedicated = true;
|
|
|
|
_network_playas = OWNER_SPECTATOR;
|
|
|
|
_local_player = OWNER_SPECTATOR;
|
2005-03-01 17:32:47 +00:00
|
|
|
|
|
|
|
/* If SwitchMode is SM_LOAD, it means that the user used the '-g' options */
|
|
|
|
if (_switch_mode != SM_LOAD) {
|
|
|
|
_switch_mode = SM_NONE;
|
2005-05-12 00:18:30 +00:00
|
|
|
GenRandomNewGame(Random(), InteractiveRandom());
|
2005-03-01 17:32:47 +00:00
|
|
|
} else {
|
|
|
|
_switch_mode = SM_NONE;
|
|
|
|
/* First we need to test if the savegame can be loaded, else we will end up playing the
|
|
|
|
* intro game... */
|
|
|
|
if (!SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL)) {
|
|
|
|
/* Loading failed, pop out.. */
|
2005-04-02 16:46:23 +00:00
|
|
|
DEBUG(net, 0)("Loading requested map failed. Aborting.");
|
2005-03-01 17:32:47 +00:00
|
|
|
_networking = false;
|
|
|
|
} else {
|
|
|
|
/* We can load this game, so go ahead */
|
|
|
|
SwitchMode(SM_LOAD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
// Done loading, start game!
|
|
|
|
|
|
|
|
if (!_networking) {
|
2005-04-02 16:46:23 +00:00
|
|
|
DEBUG(net, 1)("Dedicated server could not be launched. Aborting.");
|
2004-12-04 17:54:56 +00:00
|
|
|
return ML_QUIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
InteractiveRandom(); // randomness
|
|
|
|
|
2004-12-18 13:15:41 +00:00
|
|
|
if (_exit_game) return ML_QUIT;
|
|
|
|
|
2004-12-18 14:19:21 +00:00
|
|
|
if (!_dedicated_forks)
|
|
|
|
DedicatedHandleKeyInput();
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
cur_ticks = GetTickCount();
|
|
|
|
#else
|
|
|
|
gettimeofday(&tim, NULL);
|
|
|
|
cur_ticks = (tim.tv_usec / 1000) + (tim.tv_sec * 1000);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (cur_ticks >= next_tick) {
|
2005-03-24 17:31:55 +00:00
|
|
|
next_tick += 30;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
GameLoop();
|
|
|
|
_screen.dst_ptr = _dedicated_video_mem;
|
|
|
|
UpdateWindows();
|
|
|
|
}
|
|
|
|
CSleep(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const HalVideoDriver _dedicated_video_driver = {
|
|
|
|
DedicatedVideoStart,
|
|
|
|
DedicatedVideoStop,
|
|
|
|
DedicatedVideoMakeDirty,
|
|
|
|
DedicatedVideoMainLoop,
|
|
|
|
DedicatedVideoChangeRes,
|
2005-05-16 16:19:32 +00:00
|
|
|
DedicatedVideoFullScreen,
|
2004-12-04 17:54:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static void *_dedicated_video_mem;
|
|
|
|
|
2005-04-02 00:17:16 +00:00
|
|
|
static const char *DedicatedVideoStart(const char * const *parm)
|
2005-02-05 18:05:42 +00:00
|
|
|
{
|
2005-04-02 16:46:23 +00:00
|
|
|
DEBUG(misc, 0) ("OpenTTD compiled without network support, exiting.");
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-12-23 00:24:44 +00:00
|
|
|
void DedicatedFork(void) {}
|
2005-01-16 18:19:33 +00:00
|
|
|
static void DedicatedVideoStop(void) { free(_dedicated_video_mem); }
|
2004-12-04 17:54:56 +00:00
|
|
|
static void DedicatedVideoMakeDirty(int left, int top, int width, int height) {}
|
|
|
|
static bool DedicatedVideoChangeRes(int w, int h) { return false; }
|
2005-05-16 16:19:32 +00:00
|
|
|
static void DedicatedVideoFullScreen(bool fs) {}
|
2005-01-16 18:19:33 +00:00
|
|
|
static int DedicatedVideoMainLoop(void) { return ML_QUIT; }
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
const HalVideoDriver _dedicated_video_driver = {
|
|
|
|
DedicatedVideoStart,
|
|
|
|
DedicatedVideoStop,
|
|
|
|
DedicatedVideoMakeDirty,
|
|
|
|
DedicatedVideoMainLoop,
|
|
|
|
DedicatedVideoChangeRes,
|
2005-05-16 16:19:32 +00:00
|
|
|
DedicatedVideoFullScreen,
|
2004-12-04 17:54:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* ENABLE_NETWORK */
|