SRB2/src/mserv.c

222 lines
4.6 KiB
C
Raw Normal View History

2014-03-15 09:59:03 -07:00
// SONIC ROBO BLAST 2
//-----------------------------------------------------------------------------
// Copyright (C) 1998-2000 by DooM Legacy Team.
2020-02-19 14:08:45 -08:00
// Copyright (C) 1999-2020 by Sonic Team Junior.
2020-04-13 22:23:01 -07:00
// Copyright (C) 2020 by James R.
2014-03-15 09:59:03 -07:00
//
// This program is free software distributed under the
// terms of the GNU General Public License, version 2.
// See the 'LICENSE' file for more details.
//-----------------------------------------------------------------------------
/// \file mserv.c
2020-04-13 21:20:29 -07:00
/// \brief Commands used to communicate with the master server
2014-03-15 09:59:03 -07:00
2020-04-13 22:23:01 -07:00
#if !defined (UNDER_CE)
2014-03-15 09:59:03 -07:00
#include <time.h>
#endif
#include "doomstat.h"
#include "doomdef.h"
#include "command.h"
#include "mserv.h"
#include "m_menu.h"
2020-04-13 22:23:01 -07:00
#include "z_zone.h"
2014-03-15 09:59:03 -07:00
static time_t MSLastPing;
static inline void SendPingToMasterServer(void);
2014-03-15 09:59:03 -07:00
#ifndef NONET
static void Command_Listserv_f(void);
#endif
static void MasterServer_OnChange(void);
static void ServerName_OnChange(void);
static CV_PossibleValue_t masterserver_update_rate_cons_t[] = {
{2, "MIN"},
{60, "MAX"},
{0}
};
2020-04-13 22:23:01 -07:00
consvar_t cv_masterserver = {"masterserver", "https://mb.srb2.org/MS/0", CV_SAVE|CV_CALL, NULL, MasterServer_OnChange, 0, NULL, NULL, 0, 0, NULL};
consvar_t cv_servername = {"servername", "SRB2Kart server", CV_SAVE|CV_CALL|CV_NOINIT, NULL, ServerName_OnChange, 0, NULL, NULL, 0, 0, NULL};
2014-03-15 09:59:03 -07:00
consvar_t cv_masterserver_update_rate = {"masterserver_update_rate", "15", CV_SAVE|CV_CALL|CV_NOINIT, masterserver_update_rate_cons_t, SendPingToMasterServer, 0, NULL, NULL, 0, 0, NULL};
2020-04-13 22:23:01 -07:00
char *ms_API;
2014-03-15 09:59:03 -07:00
INT16 ms_RoomId = -1;
static enum { MSCS_NONE, MSCS_WAITING, MSCS_REGISTERED, MSCS_FAILED } con_state = MSCS_NONE;
UINT16 current_port = 0;
// Room list is an external variable now.
// Avoiding having to get info ten thousand times...
msg_rooms_t room_list[NUM_LIST_ROOMS+1]; // +1 for easy test
/** Adds variables and commands relating to the master server.
*
* \sa cv_masterserver, cv_servername,
* Command_Listserv_f
*/
void AddMServCommands(void)
{
#ifndef NONET
CV_RegisterVar(&cv_masterserver);
CV_RegisterVar(&cv_masterserver_update_rate);
CV_RegisterVar(&cv_masterserver_debug);
2014-03-15 09:59:03 -07:00
CV_RegisterVar(&cv_servername);
COM_AddCommand("listserv", Command_Listserv_f);
#endif
}
2020-04-13 22:23:01 -07:00
static void WarnGUI (void)
{
2020-04-13 22:23:01 -07:00
M_StartMessage(M_GetText("There was a problem connecting to\nthe Master Server\n\nCheck the console for details.\n"), NULL, MM_NOTHING);
}
2014-03-15 09:59:03 -07:00
#define NUM_LIST_SERVER MAXSERVERLIST
const msg_server_t *GetShortServersList(INT32 room)
{
static msg_server_t server_list[NUM_LIST_SERVER+1]; // +1 for easy test
2020-04-13 22:23:01 -07:00
if (HMS_fetch_servers(server_list, room))
return server_list;
2020-04-13 22:23:01 -07:00
else
2014-03-15 09:59:03 -07:00
{
2020-04-13 22:23:01 -07:00
WarnGUI();
2014-03-15 09:59:03 -07:00
return NULL;
}
}
INT32 GetRoomsList(boolean hosting)
{
2020-04-13 22:23:01 -07:00
if (HMS_fetch_rooms( ! hosting ))
2014-03-15 09:59:03 -07:00
return 1;
else
{
2020-04-13 22:23:01 -07:00
WarnGUI();
2014-03-15 09:59:03 -07:00
return -1;
}
}
#ifdef UPDATE_ALERT
const char *GetMODVersion(void)
{
2020-04-13 22:23:01 -07:00
static char buffer[16];
int c;
2020-04-13 22:23:01 -07:00
c = HMS_compare_mod_version(buffer, sizeof buffer);
2014-03-15 09:59:03 -07:00
2020-04-13 22:23:01 -07:00
if (c > 0)
return buffer;
else
{
2020-04-13 22:23:01 -07:00
if (! c)
WarnGUI();
2014-03-15 09:59:03 -07:00
return NULL;
}
2014-03-15 09:59:03 -07:00
}
// Console only version of the above (used before game init)
void GetMODVersion_Console(void)
{
2020-04-13 22:23:01 -07:00
char buffer[16];
2014-03-15 09:59:03 -07:00
2020-04-13 22:23:01 -07:00
if (HMS_compare_mod_version(buffer, sizeof buffer) > 0)
I_Error(UPDATE_ALERT_STRING_CONSOLE, VERSIONSTRING, buffer);
2014-03-15 09:59:03 -07:00
}
#endif
#ifndef NONET
/** Gets a list of game servers. Called from console.
*/
static void Command_Listserv_f(void)
{
CONS_Printf(M_GetText("Retrieving server list...\n"));
{
HMS_list_servers();
2014-03-15 09:59:03 -07:00
}
}
#endif
void RegisterServer(void)
{
CONS_Printf(M_GetText("Registering this server on the Master Server...\n"));
{
if (HMS_register())
con_state = MSCS_REGISTERED;
2020-04-13 22:23:01 -07:00
else
con_state = MSCS_FAILED;
}
time(&MSLastPing);
2020-04-13 22:23:01 -07:00
}
2020-04-13 22:23:01 -07:00
static void UpdateServer(void)
{
if (( con_state == MSCS_REGISTERED && HMS_update() ))
{
time(&MSLastPing);
}
else
2014-03-15 09:59:03 -07:00
{
2020-04-13 22:23:01 -07:00
con_state = MSCS_FAILED;
RegisterServer();
2014-03-15 09:59:03 -07:00
}
}
static inline void SendPingToMasterServer(void)
{
// Here, have a simpler MS Ping... - Cue
if(time(NULL) > (MSLastPing+(60*cv_masterserver_update_rate.value)) && con_state != MSCS_NONE)
2014-03-15 09:59:03 -07:00
{
2020-04-13 22:23:01 -07:00
UpdateServer();
2014-03-15 09:59:03 -07:00
}
}
void UnregisterServer(void)
{
2020-04-13 22:23:01 -07:00
if (con_state == MSCS_REGISTERED)
2014-03-15 09:59:03 -07:00
{
2020-04-13 22:23:01 -07:00
CONS_Printf(M_GetText("Removing this server from the Master Server...\n"));
2014-03-15 09:59:03 -07:00
HMS_unlist();
}
2020-04-13 22:23:01 -07:00
con_state = MSCS_NONE;
2014-03-15 09:59:03 -07:00
}
void MasterClient_Ticker(void)
{
if (server && ms_RoomId > 0)
SendPingToMasterServer();
}
static void ServerName_OnChange(void)
{
2018-11-24 18:24:05 -08:00
if (con_state == MSCS_REGISTERED)
2020-04-13 22:23:01 -07:00
UpdateServer();
2014-03-15 09:59:03 -07:00
}
static void MasterServer_OnChange(void)
{
2020-04-13 22:23:01 -07:00
boolean auto_register;
2014-03-15 09:59:03 -07:00
2020-04-13 22:23:01 -07:00
auto_register = ( con_state != MSCS_NONE );
2014-03-15 09:59:03 -07:00
2020-04-13 22:23:01 -07:00
if (ms_API)
2014-03-15 09:59:03 -07:00
{
2020-04-13 22:23:01 -07:00
UnregisterServer();
Z_Free(ms_API);
2014-03-15 09:59:03 -07:00
}
2020-04-13 22:23:01 -07:00
ms_API = Z_StrDup(cv_masterserver.string);
if (auto_register)
RegisterServer();
2014-03-15 09:59:03 -07:00
}