// SONIC ROBO BLAST 2 //----------------------------------------------------------------------------- // Copyright (C) 1998-2000 by DooM Legacy Team. // Copyright (C) 1999-2018 by Sonic Team Junior. // Copyright (C) 2020 by James R. // // 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 /// \brief Commands used to communicate with the master server #if !defined (UNDER_CE) #include #endif #include "doomstat.h" #include "doomdef.h" #include "console.h" // con_startup #include "command.h" #include "i_threads.h" #include "mserv.h" #include "m_menu.h" #include "z_zone.h" #ifdef HAVE_DISCORDRPC #include "discord.h" #endif #ifdef MASTERSERVER static int MSId; static int MSRegisteredId = -1; static boolean MSRegistered; static boolean MSInProgress; static boolean MSUpdateAgain; static time_t MSLastPing; static char *MSRules; #ifdef HAVE_THREADS static I_Mutex MSMutex; static I_Cond MSCond; # define Lock_state() I_LockMutex (&MSMutex) # define Unlock_state() I_UnlockMutex (MSMutex) #else/*HAVE_THREADS*/ # define Lock_state() # define Unlock_state() #endif/*HAVE_THREADS*/ static void Command_Listserv_f(void); #endif/*MASTERSERVER*/ static void Update_parameters(void); #ifdef MASTERSERVER static void MasterServer_OnChange(void); static void Advertise_OnChange(void); static CV_PossibleValue_t masterserver_update_rate_cons_t[] = { {2, "MIN"}, {60, "MAX"}, {0, NULL} }; #endif #ifdef MASTERSERVER consvar_t cv_masterserver = {"masterserver", "https://ms.kartkrew.org/ms/api", CV_SAVE|CV_CALL, NULL, MasterServer_OnChange, 0, NULL, NULL, 0, 0, NULL}; #endif #ifdef HOLEPUNCH consvar_t cv_rendezvousserver = {"holepunchserver", "relay.kartkrew.org", CV_SAVE, NULL, NULL, 0, NULL, NULL, 0, 0, NULL}; #endif consvar_t cv_servername = {"servername", "SRB2Kart server", CV_SAVE|CV_CALL|CV_NOINIT, NULL, Update_parameters, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_server_contact = {"server_contact", "", CV_SAVE|CV_CALL|CV_NOINIT, NULL, Update_parameters, 0, NULL, NULL, 0, 0, NULL}; #ifdef MASTERSERVER consvar_t cv_masterserver_update_rate = {"masterserver_update_rate", "15", CV_SAVE|CV_CALL|CV_NOINIT, masterserver_update_rate_cons_t, MasterClient_Ticker, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_advertise = {"advertise", "No", CV_NETVAR|CV_CALL|CV_NOINIT, CV_YesNo, Advertise_OnChange, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_masterserver_nagattempts = {"masterserver_nagattempts", "5", CV_SAVE, CV_Unsigned, NULL, 0, NULL, NULL, 0, 0, NULL}; #endif #if defined (MASTERSERVER) && defined (HAVE_THREADS) int ms_QueryId = 0; I_Mutex ms_QueryId_mutex; msg_server_t *ms_ServerList = NULL; I_Mutex ms_ServerList_mutex; #endif UINT16 current_port = 0; /** Adds variables and commands relating to the master server. * * \sa cv_masterserver, cv_servername, * Command_Listserv_f */ void AddMServCommands(void) { #ifdef MASTERSERVER CV_RegisterVar(&cv_masterserver); CV_RegisterVar(&cv_masterserver_update_rate); CV_RegisterVar(&cv_masterserver_timeout); CV_RegisterVar(&cv_masterserver_debug); CV_RegisterVar(&cv_masterserver_token); CV_RegisterVar(&cv_masterserver_nagattempts); CV_RegisterVar(&cv_advertise); #endif #ifdef HOLEPUNCH CV_RegisterVar(&cv_rendezvousserver); #endif CV_RegisterVar(&cv_servername); CV_RegisterVar(&cv_server_contact); #ifdef MASTERSERVER COM_AddCommand("listserv", Command_Listserv_f); #endif } #ifdef MASTERSERVER static void WarnGUI (void) { #ifdef HAVE_THREADS I_LockMutex(&m_menu_mutex); #endif M_PopupMasterServerConnectError(); #ifdef HAVE_THREADS I_UnlockMutex(m_menu_mutex); #endif } #define NUM_LIST_SERVER MAXSERVERLIST msg_server_t *GetShortServersList(int id) { msg_server_t *server_list; // +1 for easy test server_list = malloc(( NUM_LIST_SERVER + 1 ) * sizeof *server_list); if (HMS_fetch_servers(server_list, id)) return server_list; else { free(server_list); WarnGUI(); return NULL; } } #ifdef UPDATE_ALERT char *GetMODVersion(int id) { char *buffer; int c; (void)id; buffer = malloc(16); c = HMS_compare_mod_version(buffer, 16); #ifdef HAVE_THREADS I_LockMutex(&ms_QueryId_mutex); { if (id != ms_QueryId) c = -1; } I_UnlockMutex(ms_QueryId_mutex); #endif if (c > 0) return buffer; else { free(buffer); if (! c) WarnGUI(); return NULL; } } #endif /** 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(); } static boolean firstmsrules = false; static void Get_masterserver_rules(boolean checkfirst) { char rules[256]; if (checkfirst) { boolean MSRulesExist; Lock_state(); MSRulesExist = (MSRules != NULL); Unlock_state(); if (MSRulesExist) return; } if (HMS_fetch_rules(rules, sizeof rules)) { Lock_state(); if (MSRules) free(MSRules); MSRules = strdup(rules); if (MSRegistered == true) { CONS_Printf("\n"); CONS_Alert(CONS_NOTICE, "%s\n", rules); } firstmsrules = true; Unlock_state(); } } static void Finish_registration (void) { int registered; CONS_Printf("Registering this server on the master server...\n"); registered = HMS_register(); Lock_state(); { MSRegistered = registered; MSRegisteredId = MSId; time(&MSLastPing); } Unlock_state(); char *rules = GetMasterServerRules(); if (rules == NULL) { Get_masterserver_rules(true); } else { CONS_Printf("\n"); CONS_Alert(CONS_NOTICE, "%s\n", rules); free(rules); } if (registered) CONS_Printf("Master server registration successful.\n"); } static void Finish_update (void) { int registered; int done; Lock_state(); { registered = MSRegistered; MSUpdateAgain = false;/* this will happen anyway */ } Unlock_state(); if (registered) { if (HMS_update()) { Lock_state(); { time(&MSLastPing); MSRegistered = true; } Unlock_state(); CONS_Printf("Updated master server listing.\n"); } else Finish_registration(); } else Finish_registration(); Lock_state(); { done = ! MSUpdateAgain; if (done) MSInProgress = false; } Unlock_state(); if (! done) Finish_update(); } static void Finish_unlist (void) { int registered; Lock_state(); { registered = MSRegistered; if (MSId == MSRegisteredId) MSId++; } Unlock_state(); if (registered) { CONS_Printf("Removing this server from the master server...\n"); if (HMS_unlist()) CONS_Printf("Server deregistration request successfully sent.\n"); Lock_state(); { MSRegistered = false; } Unlock_state(); #ifdef HAVE_THREADS I_WakeAllCond(&MSCond); #endif } } static void Finish_masterserver_change (char *api) { HMS_set_api(api); if (!con_startup) Get_masterserver_rules(false); } #ifdef HAVE_THREADS static int * Server_id (void) { int *id; id = malloc(sizeof *id); Lock_state(); { *id = MSId; } Unlock_state(); return id; } static int * New_server_id (void) { int *id; id = malloc(sizeof *id); Lock_state(); { *id = ++MSId; I_WakeAllCond(&MSCond); } Unlock_state(); return id; } static void Register_server_thread (int *id) { int same; Lock_state(); { /* wait for previous unlist to finish */ while (*id == MSId && MSRegistered) I_HoldCond(&MSCond, MSMutex); same = ( *id == MSId );/* it could have been a while */ } Unlock_state(); if (same)/* it could have been a while */ Finish_registration(); free(id); } static void Update_server_thread (int *id) { int same; Lock_state(); { same = ( *id == MSRegisteredId ); } Unlock_state(); if (same) Finish_update(); free(id); } static void Unlist_server_thread (int *id) { int same; Lock_state(); { same = ( *id == MSRegisteredId ); } Unlock_state(); if (same) Finish_unlist(); free(id); } static void Change_masterserver_thread (char *api) { Lock_state(); { while (MSRegistered) I_HoldCond(&MSCond, MSMutex); } Unlock_state(); Finish_masterserver_change(api); } static void Get_masterserver_rules_thread (void) { // THIS FUNC has its own lock check in it Get_masterserver_rules(true); } #endif/*HAVE_THREADS*/ void RegisterServer(void) { #ifdef MASTERSERVER #ifdef HAVE_THREADS (void)!I_SpawnThread( "register-server", (I_ThreadFn)Register_server_thread, New_server_id() ); #else Finish_registration(); #endif #endif/*MASTERSERVER*/ } static void UpdateServer(void) { #ifdef HAVE_THREADS (void)!I_SpawnThread( "update-server", (I_ThreadFn)Update_server_thread, Server_id() ); #else Finish_update(); #endif } void UnregisterServer(void) { #ifdef MASTERSERVER #ifdef HAVE_THREADS (void)!I_SpawnThread( "unlist-server", (I_ThreadFn)Unlist_server_thread, Server_id() ); #else Finish_unlist(); #endif #endif/*MASTERSERVER*/ } char *GetMasterServerRules(void) { char *rules; Lock_state(); rules = MSRules ? strdup(MSRules) : NULL; Unlock_state(); return rules; } static boolean Online (void) { return ( serverrunning && netgame && cv_advertise.value ); } static inline void SendPingToMasterServer(void) { int ready; time_t now; if (Online()) { time(&now); Lock_state(); { ready = ( MSRegisteredId == MSId && ! MSInProgress && now >= ( MSLastPing + 60 * cv_masterserver_update_rate.value ) ); if (ready) MSInProgress = true; } Unlock_state(); if (ready) UpdateServer(); } } void MasterClient_Ticker(void) { #ifdef MASTERSERVER SendPingToMasterServer(); #endif } static void Set_api (const char *api) { char *dapi = strdup(api); #ifdef HAVE_THREADS if (!I_SpawnThread( "change-masterserver", (I_ThreadFn)Change_masterserver_thread, dapi )) { free(dapi); } #else Finish_masterserver_change(dapi); #endif } void Get_rules (void) { #ifdef HAVE_THREADS (void)!I_SpawnThread( "get-masterserver-rules", (I_ThreadFn)Get_masterserver_rules_thread, NULL ); #else Get_masterserver_rules(true); #endif } #endif/*MASTERSERVER*/ static void Update_parameters (void) { #ifdef MASTERSERVER int registered; int delayed; if (Online()) { Lock_state(); { delayed = MSInProgress; if (delayed)/* do another update after the current one */ { registered = 0; MSUpdateAgain = true; } else { registered = MSRegistered; } } Unlock_state(); if (! delayed && registered) UpdateServer(); } #endif/*MASTERSERVER*/ } #ifdef MASTERSERVER void Update_MS(void) { Update_parameters(); } #endif #ifdef MASTERSERVER static void MasterServer_OnChange(void) { UnregisterServer(); Set_api(cv_masterserver.string); if (Online()) RegisterServer(); } #endif/*MASTERSERVER*/ #ifdef MASTERSERVER static void Advertise_OnChange(void) { int different; if (cv_advertise.value) { if (serverrunning && netgame) { Lock_state(); { different = ( MSId != MSRegisteredId ); } Unlock_state(); if (different) { RegisterServer(); } } } else { UnregisterServer(); } #ifdef HAVE_DISCORDRPC DRPC_UpdatePresence(); #endif if (!dedicated) { M_PopupMasterServerRules(); } } #endif