2004-02-19 00:49:34 +00:00
|
|
|
/*
|
2005-06-09 03:07:13 +00:00
|
|
|
qtv.c
|
2004-02-19 00:49:34 +00:00
|
|
|
|
2005-06-09 03:07:13 +00:00
|
|
|
QTV main file
|
2004-02-19 00:49:34 +00:00
|
|
|
|
2005-06-09 03:07:13 +00:00
|
|
|
Copyright (C) 2004 Bill Currie <bill@taniwha.org>
|
2004-02-19 00:49:34 +00:00
|
|
|
|
2005-06-09 03:07:13 +00:00
|
|
|
Author: Bill Currie
|
|
|
|
Date: 2004/02/18
|
2004-02-19 00:49:34 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2004-02-19 00:49:34 +00:00
|
|
|
"$Id$";
|
|
|
|
|
2004-02-20 04:29:03 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2004-02-18 23:21:11 +00:00
|
|
|
#include <stdio.h>
|
2004-02-19 00:49:34 +00:00
|
|
|
#include <stdlib.h>
|
2004-02-20 04:29:03 +00:00
|
|
|
#include <time.h>
|
2004-02-19 00:49:34 +00:00
|
|
|
|
|
|
|
#include "QF/cbuf.h"
|
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/console.h"
|
|
|
|
#include "QF/cvar.h"
|
2004-02-20 04:29:03 +00:00
|
|
|
#include "QF/dstring.h"
|
2004-02-19 00:49:34 +00:00
|
|
|
#include "QF/idparse.h"
|
2004-02-20 05:37:47 +00:00
|
|
|
#include "QF/info.h"
|
2004-02-19 00:49:34 +00:00
|
|
|
#include "QF/plugin.h"
|
|
|
|
#include "QF/qargs.h"
|
|
|
|
#include "QF/quakefs.h"
|
|
|
|
#include "QF/sys.h"
|
2004-02-20 04:29:03 +00:00
|
|
|
#include "QF/va.h"
|
2004-02-19 00:49:34 +00:00
|
|
|
#include "QF/zone.h"
|
|
|
|
|
2004-02-21 02:31:22 +00:00
|
|
|
#include "qw/protocol.h"
|
|
|
|
|
2004-02-20 05:37:47 +00:00
|
|
|
#include "client.h"
|
2004-02-20 04:29:03 +00:00
|
|
|
#include "compat.h"
|
|
|
|
#include "connection.h"
|
2004-02-19 00:49:34 +00:00
|
|
|
#include "netchan.h"
|
2004-02-21 05:09:02 +00:00
|
|
|
#include "qtv.h"
|
2004-02-20 02:39:34 +00:00
|
|
|
#include "server.h"
|
2004-02-19 00:49:34 +00:00
|
|
|
|
2005-05-05 07:31:31 +00:00
|
|
|
#undef qtv_print
|
|
|
|
|
2004-02-19 00:49:34 +00:00
|
|
|
SERVER_PLUGIN_PROTOS
|
|
|
|
static plugin_list_t server_plugin_list[] = {
|
|
|
|
SERVER_PLUGIN_LIST
|
|
|
|
};
|
|
|
|
|
2004-02-20 05:37:47 +00:00
|
|
|
double realtime;
|
|
|
|
|
2010-01-13 06:46:26 +00:00
|
|
|
cvar_t *sv_timeout;
|
|
|
|
|
2004-02-19 00:49:34 +00:00
|
|
|
cbuf_t *qtv_cbuf;
|
2004-02-20 04:29:03 +00:00
|
|
|
cbuf_args_t *qtv_args;
|
2004-02-19 00:49:34 +00:00
|
|
|
|
2004-02-21 05:09:02 +00:00
|
|
|
static cvar_t *qtv_console_plugin;
|
|
|
|
static cvar_t *qtv_port;
|
|
|
|
static cvar_t *qtv_mem_size;
|
|
|
|
static cvar_t *fs_globalcfg;
|
|
|
|
static cvar_t *fs_usercfg;
|
2004-02-19 00:49:34 +00:00
|
|
|
|
2004-02-20 04:29:03 +00:00
|
|
|
redirect_t qtv_redirected;
|
2004-02-22 06:19:38 +00:00
|
|
|
client_t *qtv_redirect_client;
|
2004-02-20 04:29:03 +00:00
|
|
|
dstring_t outputbuf = {&dstring_default_mem};
|
|
|
|
|
|
|
|
static void
|
|
|
|
qtv_print (const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
static int pending;
|
|
|
|
static dstring_t *msg;
|
|
|
|
|
|
|
|
if (!msg)
|
|
|
|
msg = dstring_new ();
|
|
|
|
|
|
|
|
dvsprintf (msg, fmt, args);
|
|
|
|
if (qtv_redirected)
|
|
|
|
dstring_appendstr (&outputbuf, msg->str);
|
|
|
|
else {
|
|
|
|
time_t mytime;
|
|
|
|
struct tm *local;
|
|
|
|
char stamp[123];
|
|
|
|
|
|
|
|
if (pending) {
|
2010-01-13 06:43:27 +00:00
|
|
|
Con_Printf ("%s", msg->str);
|
2004-02-20 04:29:03 +00:00
|
|
|
} else {
|
|
|
|
mytime = time (NULL);
|
|
|
|
local = localtime (&mytime);
|
2010-12-23 02:40:16 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
strftime (stamp, sizeof (stamp), "[%b %d %X] ", local);
|
|
|
|
#else
|
2004-02-20 04:29:03 +00:00
|
|
|
strftime (stamp, sizeof (stamp), "[%b %e %X] ", local);
|
2010-12-23 02:40:16 +00:00
|
|
|
#endif
|
2010-01-13 06:43:27 +00:00
|
|
|
Con_Printf ("%s%s", stamp, msg->str);
|
2004-02-20 04:29:03 +00:00
|
|
|
}
|
|
|
|
if (msg->str[0] && msg->str[strlen (msg->str) - 1] != '\n') {
|
|
|
|
pending = 1;
|
|
|
|
} else {
|
|
|
|
pending = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-21 05:09:02 +00:00
|
|
|
void
|
|
|
|
qtv_printf (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
|
|
|
|
va_start (argptr, fmt);
|
|
|
|
qtv_print (fmt, argptr);
|
|
|
|
va_end (argptr);
|
|
|
|
}
|
|
|
|
|
2010-12-21 00:23:59 +00:00
|
|
|
static void
|
2004-02-20 04:29:03 +00:00
|
|
|
qtv_flush_redirect (void)
|
|
|
|
{
|
|
|
|
char send[8000 + 6];
|
2004-07-13 19:14:01 +00:00
|
|
|
size_t count;
|
2004-02-20 04:29:03 +00:00
|
|
|
int bytes;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (!outputbuf.size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
count = strlen (outputbuf.str);
|
|
|
|
if (qtv_redirected == RD_PACKET) {
|
|
|
|
send[0] = 0xff;
|
|
|
|
send[1] = 0xff;
|
|
|
|
send[2] = 0xff;
|
|
|
|
send[3] = 0xff;
|
|
|
|
send[4] = A2C_PRINT;
|
|
|
|
|
|
|
|
p = outputbuf.str;
|
|
|
|
while (count) {
|
|
|
|
bytes = min (count, sizeof (send) - 5);
|
|
|
|
memcpy (send + 5, p, bytes);
|
|
|
|
Netchan_SendPacket (bytes + 5, send, net_from);
|
|
|
|
p += bytes;
|
|
|
|
count -= bytes;
|
|
|
|
}
|
2004-02-21 05:09:02 +00:00
|
|
|
} else if (qtv_redirected == RD_CLIENT) {
|
2004-02-22 06:19:38 +00:00
|
|
|
client_t *cl = qtv_redirect_client;
|
2004-02-21 05:09:02 +00:00
|
|
|
p = outputbuf.str;
|
|
|
|
while (count) {
|
|
|
|
// +/- 3 for svc_print, PRINT_HIGH and nul byte
|
|
|
|
// min of 4 because we don't want to send an effectively empty
|
|
|
|
// message
|
2004-02-22 06:19:38 +00:00
|
|
|
bytes = MSG_ReliableCheckSize (&cl->backbuf, count + 3, 4) - 3;
|
2004-02-21 05:09:02 +00:00
|
|
|
// if writing another packet would overflow the client, just drop
|
|
|
|
// the rest of the data. getting rudely disconnected would be much
|
|
|
|
// more annoying than losing the tail end of the output
|
|
|
|
if (bytes <= 0)
|
|
|
|
break;
|
2004-02-22 06:19:38 +00:00
|
|
|
MSG_ReliableWrite_Begin (&cl->backbuf, svc_print, bytes + 3);
|
|
|
|
MSG_ReliableWrite_Byte (&cl->backbuf, PRINT_HIGH);
|
|
|
|
MSG_ReliableWrite_SZ (&cl->backbuf, p, bytes);
|
|
|
|
MSG_ReliableWrite_Byte (&cl->backbuf, 0);
|
2004-02-21 05:09:02 +00:00
|
|
|
p += bytes;
|
|
|
|
count -= bytes;
|
|
|
|
}
|
2004-02-20 04:29:03 +00:00
|
|
|
}
|
|
|
|
dstring_clear (&outputbuf);
|
|
|
|
}
|
|
|
|
|
2004-02-21 05:09:02 +00:00
|
|
|
void
|
2004-02-22 06:19:38 +00:00
|
|
|
qtv_begin_redirect (redirect_t rd, client_t *cl)
|
2004-02-20 04:29:03 +00:00
|
|
|
{
|
|
|
|
qtv_redirected = rd;
|
2004-02-22 06:19:38 +00:00
|
|
|
qtv_redirect_client = cl;
|
2004-02-20 04:29:03 +00:00
|
|
|
dstring_clear (&outputbuf);
|
|
|
|
}
|
|
|
|
|
2004-02-21 05:09:02 +00:00
|
|
|
void
|
2004-02-20 04:29:03 +00:00
|
|
|
qtv_end_redirect (void)
|
|
|
|
{
|
|
|
|
qtv_flush_redirect ();
|
|
|
|
qtv_redirected = RD_NONE;
|
2004-02-22 06:19:38 +00:00
|
|
|
qtv_redirect_client = 0;
|
2004-02-20 04:29:03 +00:00
|
|
|
}
|
|
|
|
|
2004-02-19 00:49:34 +00:00
|
|
|
static void
|
|
|
|
qtv_memory_init (void)
|
|
|
|
{
|
|
|
|
int mem_size;
|
|
|
|
void *mem_base;
|
|
|
|
|
|
|
|
qtv_mem_size = Cvar_Get ("qtv_mem_size", "8", CVAR_NONE, NULL,
|
|
|
|
"Amount of memory (in MB) to allocate for the "
|
2010-08-19 06:01:43 +00:00
|
|
|
PACKAGE_NAME " heap");
|
2004-02-19 00:49:34 +00:00
|
|
|
|
|
|
|
Cvar_SetFlags (qtv_mem_size, qtv_mem_size->flags | CVAR_ROM);
|
|
|
|
mem_size = (int) (qtv_mem_size->value * 1024 * 1024);
|
|
|
|
mem_base = malloc (mem_size);
|
|
|
|
if (!mem_base)
|
|
|
|
Sys_Error ("Can't allocate %d", mem_size);
|
|
|
|
Memory_Init (mem_base, mem_size);
|
|
|
|
}
|
|
|
|
|
2004-02-20 02:39:34 +00:00
|
|
|
static void
|
|
|
|
qtv_shutdown (void)
|
|
|
|
{
|
|
|
|
NET_Shutdown ();
|
|
|
|
Con_Shutdown ();
|
2005-05-05 22:50:09 +00:00
|
|
|
Cbuf_Delete (qtv_cbuf);
|
|
|
|
Cbuf_ArgsDelete (qtv_args);
|
2004-02-20 02:39:34 +00:00
|
|
|
}
|
|
|
|
|
2004-02-19 00:49:34 +00:00
|
|
|
static void
|
|
|
|
qtv_quit_f (void)
|
|
|
|
{
|
|
|
|
Sys_Printf ("Shutting down.\n");
|
|
|
|
Sys_Quit ();
|
|
|
|
}
|
|
|
|
|
2004-02-20 04:29:03 +00:00
|
|
|
static void
|
|
|
|
qtv_net_init (void)
|
|
|
|
{
|
|
|
|
qtv_port = Cvar_Get ("qtv_port", va ("%d", PORT_QTV), 0, 0,
|
|
|
|
"udp port to use");
|
2010-01-13 06:46:26 +00:00
|
|
|
sv_timeout = Cvar_Get ("sv_timeout", "60", 0, 0, "server timeout");
|
2004-02-20 04:29:03 +00:00
|
|
|
NET_Init (qtv_port->int_val);
|
|
|
|
Connection_Init ();
|
2004-02-20 05:37:47 +00:00
|
|
|
net_realtime = &realtime;
|
2004-02-20 04:29:03 +00:00
|
|
|
Netchan_Init ();
|
|
|
|
}
|
|
|
|
|
2004-02-19 00:49:34 +00:00
|
|
|
static void
|
|
|
|
qtv_init (void)
|
|
|
|
{
|
|
|
|
qtv_cbuf = Cbuf_New (&id_interp);
|
2004-02-20 04:29:03 +00:00
|
|
|
qtv_args = Cbuf_ArgsNew ();
|
2004-02-19 00:49:34 +00:00
|
|
|
|
2004-02-20 02:39:34 +00:00
|
|
|
Sys_RegisterShutdown (qtv_shutdown);
|
|
|
|
|
2004-02-19 00:49:34 +00:00
|
|
|
Cvar_Init_Hash ();
|
|
|
|
Cmd_Init_Hash ();
|
|
|
|
Cvar_Init ();
|
|
|
|
Sys_Init_Cvars ();
|
|
|
|
Sys_Init ();
|
|
|
|
Cvar_Get ("cmd_warncmd", "1", CVAR_NONE, NULL, NULL);
|
|
|
|
Cmd_Init ();
|
|
|
|
|
|
|
|
Cmd_StuffCmds (qtv_cbuf);
|
|
|
|
Cbuf_Execute_Sets (qtv_cbuf);
|
|
|
|
|
2004-02-19 23:38:48 +00:00
|
|
|
fs_globalcfg = Cvar_Get ("fs_globalcfg", FS_GLOBALCFG,
|
|
|
|
CVAR_ROM, 0, "global configuration file");
|
|
|
|
Cmd_Exec_File (qtv_cbuf, fs_globalcfg->string, 0);
|
|
|
|
Cbuf_Execute_Sets (qtv_cbuf);
|
|
|
|
|
|
|
|
// execute +set again to override the config file
|
|
|
|
Cmd_StuffCmds (qtv_cbuf);
|
|
|
|
Cbuf_Execute_Sets (qtv_cbuf);
|
|
|
|
fs_usercfg = Cvar_Get ("fs_usercfg", FS_USERCFG,
|
|
|
|
CVAR_ROM, 0, "user configuration file");
|
|
|
|
Cmd_Exec_File (qtv_cbuf, fs_usercfg->string, 0);
|
|
|
|
Cbuf_Execute_Sets (qtv_cbuf);
|
|
|
|
|
|
|
|
// execute +set again to override the config file
|
|
|
|
Cmd_StuffCmds (qtv_cbuf);
|
|
|
|
Cbuf_Execute_Sets (qtv_cbuf);
|
|
|
|
|
2004-02-19 00:49:34 +00:00
|
|
|
qtv_memory_init ();
|
|
|
|
|
|
|
|
QFS_Init ("qw");
|
|
|
|
PI_Init ();
|
|
|
|
|
|
|
|
qtv_console_plugin = Cvar_Get ("qtv_console_plugin", "server",
|
|
|
|
CVAR_ROM, 0, "Plugin used for the console");
|
|
|
|
PI_RegisterPlugins (server_plugin_list);
|
|
|
|
Con_Init (qtv_console_plugin->string);
|
|
|
|
if (con_module)
|
|
|
|
con_module->data->console->cbuf = qtv_cbuf;
|
2004-02-20 04:29:03 +00:00
|
|
|
Sys_SetStdPrintf (qtv_print);
|
2004-02-19 00:49:34 +00:00
|
|
|
|
2010-12-21 09:15:31 +00:00
|
|
|
qtv_sbar_init ();
|
|
|
|
|
2004-02-19 00:49:34 +00:00
|
|
|
Netchan_Init_Cvars ();
|
|
|
|
|
|
|
|
Cmd_StuffCmds (qtv_cbuf);
|
|
|
|
Cbuf_Execute_Sets (qtv_cbuf);
|
|
|
|
|
2004-02-20 04:29:03 +00:00
|
|
|
qtv_net_init ();
|
2004-02-20 02:39:34 +00:00
|
|
|
Server_Init ();
|
2004-02-21 05:09:02 +00:00
|
|
|
Client_Init ();
|
2004-02-20 02:39:34 +00:00
|
|
|
|
2004-02-19 00:49:34 +00:00
|
|
|
Cmd_AddCommand ("quit", qtv_quit_f, "Shut down qtv");
|
|
|
|
|
|
|
|
Cmd_StuffCmds (qtv_cbuf);
|
|
|
|
}
|
2004-02-18 23:21:11 +00:00
|
|
|
|
2004-02-20 04:29:03 +00:00
|
|
|
static void
|
|
|
|
qtv_ping (void)
|
|
|
|
{
|
|
|
|
char data = A2A_ACK;
|
|
|
|
|
|
|
|
Netchan_SendPacket (1, &data, net_from);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
qtv_status (void)
|
|
|
|
{
|
2004-02-22 06:19:38 +00:00
|
|
|
qtv_begin_redirect (RD_PACKET, 0);
|
2010-08-19 06:01:43 +00:00
|
|
|
Sys_Printf ("\\*version\\%s qtv %s", QW_VERSION, PACKAGE_VERSION);
|
2004-02-20 04:29:03 +00:00
|
|
|
qtv_end_redirect ();
|
|
|
|
}
|
|
|
|
|
2010-12-21 00:23:59 +00:00
|
|
|
static void
|
2004-02-20 04:29:03 +00:00
|
|
|
qtv_connectionless_packet (void)
|
|
|
|
{
|
|
|
|
const char *cmd, *str;
|
|
|
|
|
|
|
|
MSG_BeginReading (net_message);
|
|
|
|
MSG_ReadLong (net_message); // skip the -1 marker
|
|
|
|
|
|
|
|
str = MSG_ReadString (net_message);
|
|
|
|
COM_TokenizeString (str, qtv_args);
|
|
|
|
cmd_args = qtv_args;
|
|
|
|
|
|
|
|
cmd = qtv_args->argv[0]->str;
|
2004-02-21 02:31:22 +00:00
|
|
|
if (!strcmp (cmd, "ping")) {
|
2004-02-20 04:29:03 +00:00
|
|
|
qtv_ping ();
|
|
|
|
} else if (!strcmp (cmd, "status")) {
|
|
|
|
qtv_status ();
|
2004-02-20 05:37:47 +00:00
|
|
|
} else if (!strcmp (cmd, "getchallenge")) {
|
2004-02-21 05:09:02 +00:00
|
|
|
Client_NewConnection ();
|
2004-02-21 02:31:22 +00:00
|
|
|
} else if (cmd[0]) {
|
|
|
|
switch (cmd[0]) {
|
|
|
|
default:
|
|
|
|
goto bad_packet;
|
|
|
|
case A2C_PRINT:
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("%s", str + 1);
|
2004-02-21 02:31:22 +00:00
|
|
|
break;
|
|
|
|
case A2A_PING:
|
|
|
|
qtv_ping ();
|
|
|
|
break;
|
|
|
|
}
|
2004-02-20 04:29:03 +00:00
|
|
|
} else {
|
2004-02-21 02:31:22 +00:00
|
|
|
bad_packet:
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("bad connectionless packet from %s:\n%s\n",
|
2004-02-20 04:29:03 +00:00
|
|
|
NET_AdrToString (net_from), str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
qtv_read_packets (void)
|
|
|
|
{
|
|
|
|
connection_t *con;
|
|
|
|
|
|
|
|
while (NET_GetPacket ()) {
|
2004-02-21 02:31:22 +00:00
|
|
|
if ((con = Connection_Find (&net_from))) {
|
|
|
|
con->handler (con, con->object);
|
|
|
|
} else if (*(int *) net_message->message->data == -1) {
|
2004-02-20 04:29:03 +00:00
|
|
|
qtv_connectionless_packet ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-18 23:21:11 +00:00
|
|
|
int
|
2004-02-19 00:49:34 +00:00
|
|
|
main (int argc, const char *argv[])
|
2004-02-18 23:21:11 +00:00
|
|
|
{
|
2010-12-21 09:15:31 +00:00
|
|
|
int frame = 0;
|
|
|
|
|
2004-02-19 00:49:34 +00:00
|
|
|
COM_InitArgv (argc, argv);
|
|
|
|
|
|
|
|
qtv_init ();
|
|
|
|
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("Ohayou gozaimasu\n");
|
2004-02-19 00:49:34 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
Cbuf_Execute_Stack (qtv_cbuf);
|
|
|
|
|
|
|
|
Sys_CheckInput (1, net_socket);
|
2005-05-02 09:00:17 +00:00
|
|
|
realtime = Sys_DoubleTime () + 1;
|
2004-02-20 04:29:03 +00:00
|
|
|
|
|
|
|
qtv_read_packets ();
|
|
|
|
|
2004-02-19 00:49:34 +00:00
|
|
|
Con_ProcessInput ();
|
2005-05-02 04:09:15 +00:00
|
|
|
|
|
|
|
Server_Frame ();
|
2005-05-08 00:48:21 +00:00
|
|
|
Client_Frame ();
|
2010-12-21 09:15:31 +00:00
|
|
|
|
|
|
|
if (++frame == 100) {
|
|
|
|
frame = 0;
|
|
|
|
Con_DrawConsole ();
|
|
|
|
}
|
2004-02-19 00:49:34 +00:00
|
|
|
}
|
2004-02-18 23:21:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|