refactor model api
[carveJwlIkooP6JGAAIwe30JlM.git] / gameserver.c
diff --git a/gameserver.c b/gameserver.c
deleted file mode 100644 (file)
index 818b22d..0000000
+++ /dev/null
@@ -1,573 +0,0 @@
-/*
- * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
- */
-
-#define _DEFAULT_SOURCE
-#include <signal.h>
-#include <unistd.h>
-#include <time.h>
-
-volatile sig_atomic_t sig_stop;
-
-static void inthandler( int signum ) {
-   sig_stop = 1;
-}
-
-#include "gameserver.h" 
-#include "highscores.c"
-#include "servermonitor_server.c"
-#include "vg/vg_opt.h"
-
-static const u64 k_connection_unauthorized = 0xffffffffffffffff;
-
-static u64_steamid get_connection_authsteamid( SteamNetworkingMessage_t *msg ){
-   i64 userdata = SteamAPI_ISteamNetworkingSockets_GetConnectionUserData(
-            hSteamNetworkingSockets, msg->m_conn );
-
-   return *((u64_steamid *)&userdata);
-}
-
-static void set_connection_authsteamid(HSteamNetConnection con, u64_steamid id){
-   i64 userdata = *((i64 *)&id);
-   
-   SteamAPI_ISteamNetworkingSockets_SetConnectionUserData(
-         hSteamNetworkingSockets, con, userdata );
-}
-
-static void gameserver_send_to_all( int ignore, 
-                                    const void *pData, u32 cbData, 
-                                    int nSendFlags ){
-   for( int i=0; i<vg_list_size(gameserver.clients); i++ ){
-      struct gameserver_client *client = &gameserver.clients[i];
-
-      if( (i==ignore) || !client->active )
-         continue;
-
-      SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
-            hSteamNetworkingSockets, client->connection,
-            pData, cbData, nSendFlags, NULL );
-   }
-}
-
-static void gameserver_populate_join_msg( int index, netmsg_playerjoin *msg ){
-   memset( msg, 0, sizeof(*msg) );
-   msg->inetmsg_id = k_inetmsg_playerjoin;
-   msg->index = index;
-   vg_strncpy( gameserver.clients[index].username, msg->username, 
-               sizeof(msg->username), k_strncpy_always_add_null );
-}
-
-static void gameserver_player_join( int index ){
-   struct gameserver_client *joiner = &gameserver.clients[index];
-   
-   netmsg_playerjoin join;
-   gameserver_populate_join_msg( index, &join );
-   gameserver_send_to_all( index, &join, sizeof(join),
-                           k_nSteamNetworkingSend_Reliable );
-
-   /* update the joining user about current connections */
-   for( int i=0; i<vg_list_size(gameserver.clients); i++ ){
-      struct gameserver_client *client = &gameserver.clients[i];
-
-      if( (i==index) || !client->active )
-         continue;
-
-      netmsg_playerjoin init;
-      gameserver_populate_join_msg( i, &init );
-
-      SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
-            hSteamNetworkingSockets, joiner->connection,
-            &init, sizeof(init), k_nSteamNetworkingSend_Reliable, NULL );
-   }
-}
-
-static void gameserver_player_leave( int index ){
-   netmsg_playerjoin leave;
-   leave.inetmsg_id = k_inetmsg_playerleave;
-   leave.index = index;
-
-   vg_info( "Player leave (%d)\n", index );
-   gameserver_send_to_all( index, &leave, sizeof(leave),
-                           k_nSteamNetworkingSend_Reliable );
-}
-
-static void new_client_connecting( HSteamNetConnection client ){
-   int index = -1;
-
-   /* TODO: LRU */
-   for( int i=0; i<vg_list_size(gameserver.clients); i++ ){
-      if( !gameserver.clients[i].active ){
-         index = i;
-         break;
-      }
-   }
-
-   if( index == -1 ){
-      vg_error( "Server full\n" );
-      SteamAPI_ISteamNetworkingSockets_CloseConnection( 
-            hSteamNetworkingSockets, client, 
-            4500,
-            NULL, 1 );
-      return;
-   }
-
-   EResult accept_status = SteamAPI_ISteamNetworkingSockets_AcceptConnection(
-            hSteamNetworkingSockets, client );
-   if( accept_status == k_EResultOK ){
-      vg_success( "Accepted client (id: %u, index: %d)\n", client, index );
-
-      gameserver.clients[index].active = 1;
-      gameserver.clients[index].connection = client;
-
-      SteamAPI_ISteamNetworkingSockets_SetConnectionPollGroup(
-            hSteamNetworkingSockets,
-            client, gameserver.client_group );
-      
-      /* Just to be sure */
-      set_connection_authsteamid( client, -1 );
-      gameserver_player_join( index );
-   }
-   else{
-      vg_warn( "Error accepting client (id: %u)\n", client );
-      SteamAPI_ISteamNetworkingSockets_CloseConnection( 
-            hSteamNetworkingSockets, client, 
-            k_ESteamNetConnectionEnd_Misc_InternalError,
-            NULL, 1 );
-      gameserver.clients[index].active = 0;
-      gameserver.clients[index].connection = 0;
-   }
-}
-
-static void on_auth_status( CallbackMsg_t *msg ){
-   SteamNetAuthenticationStatus_t *info = (void *)msg->m_pubParam;
-   vg_info( "  Authentication availibility: %s\n", 
-         string_ESteamNetworkingAvailability(info->m_eAvail) );
-   vg_info( "  %s\n", info->m_debugMsg );
-}
-
-static int gameserver_client_index( HSteamNetConnection hconn ){
-   for( int i=0; i<vg_list_size(gameserver.clients); i++ ){
-      struct gameserver_client *client = &gameserver.clients[i];
-
-      if( client->active ){
-         if( client->connection == hconn ){
-            return i;
-         }
-      }
-   }
-   return -1;
-}
-
-static void on_connect_status( CallbackMsg_t *msg ){
-   SteamNetConnectionStatusChangedCallback_t *info = (void *)msg->m_pubParam;
-   vg_info( "  Connection status changed for %lu\n", info->m_hConn );
-
-   vg_info( "  %s -> %s\n", 
-         string_ESteamNetworkingConnectionState(info->m_eOldState),
-         string_ESteamNetworkingConnectionState(info->m_info.m_eState) );
-
-   if( info->m_info.m_eState==k_ESteamNetworkingConnectionState_Connecting ){
-      new_client_connecting( info->m_hConn );
-   }
-
-   if( (info->m_info.m_eState == 
-            k_ESteamNetworkingConnectionState_ClosedByPeer ) ||
-       (info->m_info.m_eState == 
-        k_ESteamNetworkingConnectionState_ProblemDetectedLocally ) ){
-
-      int client_id = gameserver_client_index( info->m_hConn );
-      if( client_id != -1 ){
-         struct gameserver_client *client = &gameserver.clients[client_id];
-         client->connection = 0;
-         client->active = 0;
-         gameserver_player_leave(client_id);
-      }
-
-      vg_info( "End reason: %d\n", info->m_info.m_eEndReason );
-      SteamAPI_ISteamNetworkingSockets_CloseConnection( 
-            hSteamNetworkingSockets, info->m_hConn, 0, NULL, 0 );
-   }
-}
-
-static void gameserver_rx_auth( SteamNetworkingMessage_t *msg ){
-   if( gameserver.auth_mode != eServerModeAuthentication ){
-      vg_error( "Running server without authentication. "
-                "Connection %u tried to authenticate.\n", msg->m_conn );
-      return;
-   }
-
-   if( get_connection_authsteamid( msg ) != k_connection_unauthorized ){
-      vg_warn( "Already authorized this user but app ticket was sent"
-               " again (%u)\n", msg->m_conn );
-      return;
-   }
-
-   vg_low( "Attempting to verify user\n" );
-
-   if( msg->m_cbSize < sizeof(netmsg_auth) ){
-      vg_error( "Malformed auth ticket, too small (%u)\n", msg->m_conn );
-      return;
-   }
-
-   netmsg_auth *auth = msg->m_pData;
-
-   if( msg->m_cbSize < sizeof(netmsg_auth)+auth->ticket_length ||
-       auth->ticket_length > 1024 ){
-      vg_error( "Malformed auth ticket, ticket_length incorrect (%u)\n",
-            auth->ticket_length );
-      return;
-   }
-
-   u8 decrypted[1024];
-   u32 ticket_len = 1024;
-
-   int success = SteamEncryptedAppTicket_BDecryptTicket(
-         auth->ticket, auth->ticket_length, decrypted,
-         &ticket_len, gameserver.app_symmetric_key,
-         k_nSteamEncryptedAppTicketSymmetricKeyLen );
-
-   if( !success ){
-      vg_error( "Failed to decrypt users ticket (client %u)\n", msg->m_conn );
-      vg_error( "  ticket length: %u\n", auth->ticket_length );
-
-      SteamAPI_ISteamNetworkingSockets_CloseConnection(
-            hSteamNetworkingSockets,
-            msg->m_conn, 0, NULL, 1 );
-      return;
-   }
-
-   if( SteamEncryptedAppTicket_GetTicketIssueTime( decrypted, ticket_len )){
-      RTime32 ctime = time(NULL),
-              tickettime = SteamEncryptedAppTicket_GetTicketIssueTime(
-                    decrypted, ticket_len ),
-              expiretime = tickettime + 24*3*60*60;
-      
-      if( ctime > expiretime ){
-         vg_error( "Ticket expired (client %u)\n", msg->m_conn );
-
-         /* TODO: Send expired information */
-         SteamAPI_ISteamNetworkingSockets_CloseConnection(
-               hSteamNetworkingSockets,
-               msg->m_conn, 0, NULL, 1 );
-         return;
-      }
-   }
-
-   CSteamID steamid;
-   SteamEncryptedAppTicket_GetTicketSteamID( decrypted, ticket_len, &steamid );
-   vg_success( "User is authenticated! steamid %lu (%u)\n", 
-         steamid.m_unAll64Bits, msg->m_conn );
-
-   set_connection_authsteamid( msg->m_conn, steamid.m_unAll64Bits );
-}
-
-static int inet_require_auth( SteamNetworkingMessage_t *msg ){
-   if( gameserver.auth_mode == eServerModeNoAuthentication )
-      return 1;
-
-   if( get_connection_authsteamid( msg ) == k_connection_unauthorized ){
-      vg_warn( "Unauthorized request! Disconnecting client: %u\n", 
-               msg->m_conn );
-
-      SteamAPI_ISteamNetworkingSockets_CloseConnection(
-            hSteamNetworkingSockets,
-            msg->m_conn, 0, NULL, 1 );
-
-      return 0;
-   }
-   else return 1;
-}
-
-/*
- * Player updates sent to us
- * -----------------------------------------------------------------------------
- */
-
-static int packet_minsize( SteamNetworkingMessage_t *msg, u32 size ){
-   if( msg->m_cbSize < size ) {
-      vg_error( "Invalid packet size (must be at least %u)\n", size );
-      return 0;
-   }
-   else{
-      return 1;
-   }
-}
-
-static void gameserver_rx_200_300( SteamNetworkingMessage_t *msg ){
-   netmsg_blank *tmp = msg->m_pData;
-
-   if( tmp->inetmsg_id == k_inetmsg_playerusername ){
-      if( !packet_minsize( msg, sizeof(netmsg_playerusername) ))
-         return;
-      
-      int client_id = gameserver_client_index( msg->m_conn );
-      if( client_id != -1 ){
-         struct gameserver_client *client = &gameserver.clients[ client_id ];
-         netmsg_playerusername *src = msg->m_pData;
-
-         vg_strncpy( src->username, client->username, sizeof(client->username),
-                     k_strncpy_always_add_null );
-
-         /* update other users about this change */
-         netmsg_playerusername msg;
-         memset( &msg, 0, sizeof(msg) );
-         msg.inetmsg_id = k_inetmsg_playerusername;
-         msg.index = client_id;
-         vg_strncpy( client->username, msg.username, sizeof(msg.username),
-                     k_strncpy_always_add_null );
-
-         gameserver_send_to_all( client_id, &msg, sizeof(msg), 
-                                 k_nSteamNetworkingSend_Reliable );
-      }
-   }
-}
-
-#if 0
-static void on_inet_score_request( SteamNetworkingMessage_t *msg ){
-   if( !inet_require_auth(msg) ) return;
-
-   SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
-         hSteamNetworkingSockets, msg->m_conn, 
-         &scoreboard_client_data, sizeof(netmsg_scoreboard),
-         k_nSteamNetworkingSend_Reliable, NULL );
-}
-
-static void on_inet_set_nickname( SteamNetworkingMessage_t *msg ){
-   if(!inet_require_auth(msg)) return;
-
-   u64_steamid steamid = get_connection_authsteamid(msg);
-   netmsg_set_nickname *setnick = msg->m_pData;
-   if( msg->m_cbSize < sizeof(netmsg_set_nickname) ){
-      vg_warn( "Invalid nickname request from client: %u, steamid: %lu\n", 
-            msg->m_conn, steamid );
-      return;
-   }
-   
-   highscore_set_user_nickname( steamid, setnick->nickname );
-}
-
-static void on_inet_set_score( SteamNetworkingMessage_t *msg ){
-   if(!inet_require_auth(msg)) return;
-
-   u64_steamid steamid = get_connection_authsteamid(msg);
-   
-   if( msg->m_cbSize < sizeof(netmsg_set_score) ){
-      vg_warn( "Invalid set score post from client: %u, steamid: %lu\n",
-            msg->m_conn, steamid );
-      return;
-   }
-
-   netmsg_set_score *info = msg->m_pData;
-
-   if( msg->m_cbSize < sizeof(netmsg_set_score) +
-                       sizeof(struct netmsg_score_record)*info->record_count ){
-      vg_warn( "Malformed set score post from client: %u, steamid: %lu\n",
-            msg->m_conn, steamid );
-      return;
-   }
-
-   for( int i=0; i<info->record_count; i++ ){
-      highscore_record temp;
-      temp.trackid  = info->records[i].trackid;
-      temp.datetime = time(NULL);
-      temp.playerid = steamid;
-      temp.points   = info->records[i].points;
-      temp.time     = info->records[i].time;
-
-      highscores_push_record( &temp );
-   }
-}
-#endif
-
-static void poll_connections(void){
-   SteamNetworkingMessage_t *messages[32];
-   int len;
-
-   while(1){
-      len = SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnPollGroup(
-            hSteamNetworkingSockets,
-            gameserver.client_group, messages, vg_list_size(messages) );
-
-      if( len <= 0 )
-         return;
-
-      for( int i=0; i<len; i++ ){
-         SteamNetworkingMessage_t *msg = messages[i];
-
-         if( msg->m_cbSize < sizeof(netmsg_blank) ){
-            vg_warn( "Discarding message (too small: %d)\n", 
-                  msg->m_cbSize );
-            continue;
-         }
-
-         netmsg_blank *tmp = msg->m_pData;
-
-         if( (tmp->inetmsg_id >= 200) && (tmp->inetmsg_id < 300) ){
-            gameserver_rx_200_300( msg );
-         }
-         else{
-            if( tmp->inetmsg_id == k_inetmsg_auth )
-               gameserver_rx_auth( msg );
-#if 0
-            else if( tmp->inetmsg_id == k_inetmsg_scores_request )
-               on_inet_score_request( msg );
-            else if( tmp->inetmsg_id == k_inetmsg_set_nickname )
-               on_inet_set_nickname( msg );
-            else if( tmp->inetmsg_id == k_inetmsg_set_score )
-               on_inet_set_score( msg );
-            else if( tmp->inetmsg_id == k_inetmsg_playerframe )
-               on_inet_playerframe( msg );
-#endif
-            else {
-               vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
-                        tmp->inetmsg_id );
-            }
-         }
-
-
-         SteamAPI_SteamNetworkingMessage_t_Release( msg );
-      }
-   }
-}
-
-static u64 seconds_to_server_ticks( double s ){
-   return s / 0.01;
-}
-
-static void generate_boards(void){
-   FILE *fp = fopen( "www/html/srhighscores.txt", "w" );
-
-   if( !fp ){
-      vg_error( "Can't write boards to www/html/srhighscores.txt\n" );
-      return;
-   }
-
-   for( int i=0; i<vg_list_size(track_infos); i++ ){
-      struct netmsg_board *board = &scoreboard_client_data.boards[i];
-
-      highscores_board_generate( board->data, i, 10 );
-      highscores_board_printf( fp, board->data, 10 );
-   }
-
-   fclose( fp );
-}
-
-int main( int argc, char *argv[] ){
-   signal( SIGINT, inthandler );
-   signal( SIGQUIT, inthandler );
-   signal( SIGPIPE, SIG_IGN );
-
-   char *arg;
-   while( vg_argp( argc, argv ) ){
-      if( vg_long_opt( "noauth" ) )
-         gameserver.auth_mode = eServerModeNoAuthentication;
-   }
-   
-   /* TODO: Options to override, ammend, remove etc */
-
-   vg_set_mem_quota( 80*1024*1024 );
-   vg_alloc_quota();
-
-   highscores_init( 250000, 10000 );
-   
-   if( !highscores_read() )
-      highscores_create_db();
-
-   steamworks_ensure_txt( "2103940" );
-
-   if( gameserver.auth_mode == eServerModeAuthentication ){
-      if( !vg_load_steam_symetric_key( "application_key", 
-                                       gameserver.app_symmetric_key )){
-         return 0;
-      }
-   }
-   else{
-      vg_warn( "Running without user authentication.\n" );
-   }
-
-   if( !SteamGameServer_Init( 0, 27400, 27401, 
-                              gameserver.auth_mode, "1.0.0.0" ) ){
-      vg_error( "SteamGameServer_Init failed\n" );
-      return 0;
-   }
-
-   void *hSteamGameServer = SteamAPI_SteamGameServer();
-   SteamAPI_ISteamGameServer_LogOnAnonymous( hSteamGameServer );
-
-   SteamAPI_ManualDispatch_Init();
-   HSteamPipe hsteampipe = SteamGameServer_GetHSteamPipe();
-
-   //hSteamHTTP = SteamAPI_SteamGameServerHTTP();
-   hSteamNetworkingSockets = 
-      SteamAPI_SteamGameServerNetworkingSockets_SteamAPI();
-
-   /* 
-    * Server code
-    */
-
-   steam_register_callback( k_iSteamNetAuthenticationStatus, on_auth_status );
-   steam_register_callback( k_iSteamNetConnectionStatusChangedCallBack,
-                             on_connect_status );
-
-   vg_success( "Steamworks API running\n" );
-   steamworks_event_loop( hsteampipe );
-
-   /*
-    * Create a listener
-    */
-
-   HSteamListenSocket listener;
-   SteamNetworkingIPAddr localAddr;
-   SteamAPI_SteamNetworkingIPAddr_Clear( &localAddr );
-   localAddr.m_port = 27402;
-
-   listener = SteamAPI_ISteamNetworkingSockets_CreateListenSocketIP(
-                  hSteamNetworkingSockets, &localAddr, 0, NULL );
-   gameserver.client_group = SteamAPI_ISteamNetworkingSockets_CreatePollGroup(
-         hSteamNetworkingSockets );
-
-   u64 server_ticks = 8000,
-       last_record_save = 8000,
-       last_scoreboard_gen = 0,
-       last_monitor_heartbeat = 0;
-
-   generate_boards();
-   monitor_start_server();
-
-   while( !sig_stop ){
-      monitor_event_loop();
-      steamworks_event_loop( hsteampipe );
-      poll_connections();
-
-      usleep(10000);
-      server_ticks ++;
-
-      if( server_ticks > 
-            (last_monitor_heartbeat + seconds_to_server_ticks(10.0))){
-         last_monitor_heartbeat = server_ticks;
-         monitor_heartbeat();
-      }
-
-      if( server_ticks > last_scoreboard_gen + seconds_to_server_ticks(60.0) ){
-         last_scoreboard_gen = server_ticks;
-         generate_boards();
-      }
-
-      if( server_ticks > last_record_save + seconds_to_server_ticks(10.0*60.0)){
-         last_record_save = server_ticks;
-         highscores_serialize_all();
-      }
-   }
-   
-   highscores_serialize_all();
-   
-   SteamAPI_ISteamNetworkingSockets_DestroyPollGroup( hSteamNetworkingSockets,
-         gameserver.client_group );
-   SteamAPI_ISteamNetworkingSockets_CloseListenSocket( 
-         hSteamNetworkingSockets, listener );
-   
-   vg_info( "Shutting down\n..." );
-   SteamGameServer_Shutdown();
-
-   return 0;
-}