now responds to ping and status commands via connectionless packets

This commit is contained in:
Bill Currie 2004-02-20 04:29:03 +00:00
parent e303db1687
commit 60d2cd1ed9
5 changed files with 338 additions and 3 deletions

View file

@ -1,4 +1,4 @@
## Process this file with automake to produce Makefile.in
AUTOMAKE_OPTIONS= foreign
EXTRA_DIST = server.h
EXTRA_DIST = connection.h server.h

48
qtv/include/connection.h Normal file
View file

@ -0,0 +1,48 @@
/*
#FILENAME#
#DESCRIPTION#
Copyright (C) 2004 #AUTHOR#
Author: #AUTHOR#
Date: #DATE#
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
$Id$
*/
#ifndef __connection_h
#define __connection_h
#include "netchan.h"
typedef struct connection_s {
netadr_t address;
void *object;
void (*handler) (void *obj);
} connection_t;
void Connection_Init (void);
void Connection_Add (netadr_t *address, void *object, void (*handler)(void*));
void Connection_Del (netadr_t *address);
connection_t *Connection_Find (netadr_t *address);
#endif//__connection_h

View file

@ -43,7 +43,7 @@ qtv_LIBS= \
$(top_builddir)/libs/console/libQFconsole.la \
$(top_builddir)/libs/util/libQFutil.la
qtv_SOURCES= qtv.c server.c
qtv_SOURCES= connection.c qtv.c server.c
qtv_LDADD= $(qtv_LIBS) $(NET_LIBS) $(DL_LIBS) $(CURSES_LIBS)
qtv_LDFLAGS= $(common_ldflags)
qtv_DEPENDENCIES= $(qtv_LIBS)

110
qtv/source/connection.c Normal file
View file

@ -0,0 +1,110 @@
/*
#FILENAME#
#DESCRIPTION#
Copyright (C) 2004 #AUTHOR#
Author: #AUTHOR#
Date: #DATE#
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
static __attribute__ ((unused)) const char rcsid[] =
"$Id$";
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "QF/cmd.h"
#include "QF/console.h"
#include "QF/hash.h"
#include "QF/sys.h"
#include "connection.h"
static hashtab_t *connections;
static unsigned long
connection_get_hash (void *_c, void *unused)
{
connection_t *c = (connection_t *) _c;
return Hash_Buffer (c->address.ip, sizeof (c->address.ip));
}
static int
connection_compare (void *_c1, void *_c2, void *unused)
{
connection_t *c1 = (connection_t *) _c1;
connection_t *c2 = (connection_t *) _c2;
return NET_CompareBaseAdr (c1->address, c2->address);
}
void
Connection_Init (void)
{
connections = Hash_NewTable (1023, 0, 0, 0);
Hash_SetHashCompare (connections, connection_get_hash, connection_compare);
}
void
Connection_Add (netadr_t *address, void *object, void (*handler)(void*))
{
connection_t *con;
con = malloc (sizeof (connection_t));
con->address = *address;
con->object = object;
con->handler = handler;
if (Hash_FindElement (connections, con))
Sys_Error ("duplicate connection");
Hash_AddElement (connections, con);
}
void
Connection_Del (netadr_t *address)
{
connection_t con;
con.address = *address;
Hash_DelElement (connections, &con);
}
connection_t *
Connection_Find (netadr_t *address)
{
connection_t con;
con.address = *address;
return Hash_FindElement (connections, &con);
}

View file

@ -34,35 +34,140 @@
static __attribute__ ((unused)) const char rcsid[] =
"$Id$";
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "QF/cbuf.h"
#include "QF/cmd.h"
#include "QF/console.h"
#include "QF/cvar.h"
#include "QF/dstring.h"
#include "QF/idparse.h"
#include "QF/plugin.h"
#include "QF/qargs.h"
#include "QF/quakefs.h"
#include "QF/sys.h"
#include "QF/va.h"
#include "QF/zone.h"
#include "compat.h"
#include "connection.h"
#include "netchan.h"
#include "server.h"
#define PORT_QTV 27501
#define A2A_PING 'k'
#define A2A_ACK 'l'
#define A2C_PRINT 'n'
typedef enum {
RD_NONE,
RD_CLIENT,
RD_PACKET,
} redirect_t;
SERVER_PLUGIN_PROTOS
static plugin_list_t server_plugin_list[] = {
SERVER_PLUGIN_LIST
};
cbuf_t *qtv_cbuf;
cbuf_args_t *qtv_args;
cvar_t *qtv_console_plugin;
cvar_t *qtv_port;
cvar_t *qtv_mem_size;
cvar_t *fs_globalcfg;
cvar_t *fs_usercfg;
redirect_t qtv_redirected;
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) {
Con_Printf ("%s", msg->str);
} else {
mytime = time (NULL);
local = localtime (&mytime);
strftime (stamp, sizeof (stamp), "[%b %e %X] ", local);
Con_Printf ("%s%s", stamp, msg->str);
}
if (msg->str[0] && msg->str[strlen (msg->str) - 1] != '\n') {
pending = 1;
} else {
pending = 0;
}
}
}
static void
qtv_flush_redirect (void)
{
char send[8000 + 6];
int count;
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;
}
}
dstring_clear (&outputbuf);
}
static void
qtv_begin_redirect (redirect_t rd)
{
qtv_redirected = rd;
dstring_clear (&outputbuf);
}
static void
qtv_end_redirect (void)
{
qtv_flush_redirect ();
qtv_redirected = RD_NONE;
}
static void
qtv_memory_init (void)
{
@ -95,10 +200,21 @@ qtv_quit_f (void)
Sys_Quit ();
}
static void
qtv_net_init (void)
{
qtv_port = Cvar_Get ("qtv_port", va ("%d", PORT_QTV), 0, 0,
"udp port to use");
NET_Init (qtv_port->int_val);
Connection_Init ();
Netchan_Init ();
}
static void
qtv_init (void)
{
qtv_cbuf = Cbuf_New (&id_interp);
qtv_args = Cbuf_ArgsNew ();
Sys_RegisterShutdown (qtv_shutdown);
@ -141,12 +257,14 @@ qtv_init (void)
Con_Init (qtv_console_plugin->string);
if (con_module)
con_module->data->console->cbuf = qtv_cbuf;
Sys_SetStdPrintf (qtv_print);
Netchan_Init_Cvars ();
Cmd_StuffCmds (qtv_cbuf);
Cbuf_Execute_Sets (qtv_cbuf);
qtv_net_init ();
Server_Init ();
Cmd_AddCommand ("quit", qtv_quit_f, "Shut down qtv");
@ -154,6 +272,62 @@ qtv_init (void)
Cmd_StuffCmds (qtv_cbuf);
}
static void
qtv_ping (void)
{
char data = A2A_ACK;
Netchan_SendPacket (1, &data, net_from);
}
static void
qtv_status (void)
{
qtv_begin_redirect (RD_PACKET);
Sys_Printf ("\\*version\\%s qtv %s", QW_VERSION, VERSION);
qtv_end_redirect ();
}
static void
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;
if (!strcmp (cmd, "ping")
|| (cmd[0] == A2A_PING && (cmd[1] == 0 || cmd[1] == '\n'))) {
qtv_ping ();
} else if (!strcmp (cmd, "status")) {
qtv_status ();
} else {
Con_Printf ("bad connectionless packet from %s:\n%s\n",
NET_AdrToString (net_from), str);
}
}
static void
qtv_read_packets (void)
{
connection_t *con;
while (NET_GetPacket ()) {
if (*(int *) net_message->message->data == -1) {
qtv_connectionless_packet ();
continue;
}
if ((con = Connection_Find (&net_from)))
con->handler (con->object);
}
}
int
main (int argc, const char *argv[])
{
@ -161,12 +335,15 @@ main (int argc, const char *argv[])
qtv_init ();
Con_Printf ("konnichi wa\n");
Con_Printf ("Ohayou gozaimasu\n");
while (1) {
Cbuf_Execute_Stack (qtv_cbuf);
Sys_CheckInput (1, net_socket);
qtv_read_packets ();
Con_ProcessInput ();
}
return 0;