wind rushing sound
[carveJwlIkooP6JGAAIwe30JlM.git] / server.c
index f8639965a2214e7e8495073ac3bd9760f4943c27..270e916c8c1a7e0a7bb335c6cba8477284f763bf 100644 (file)
--- a/server.c
+++ b/server.c
@@ -1,4 +1,6 @@
-// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
+/*
+ * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ */
 
 /*
  * This server application requires steamclient.so to be present in the 
 #define _DEFAULT_SOURCE
 #include <unistd.h>
 #include <signal.h>
+#include <time.h>
 
 volatile sig_atomic_t sig_stop;
 
-void inthandler( int signum ) 
-{
+static void inthandler( int signum ) {
    sig_stop = 1;
 }
 
@@ -26,6 +28,9 @@ void inthandler( int signum )
 #include "network_msg.h"
 #include "highscores.h"
 
+/* implementation */
+#include "highscores.c"
+
 static const u64 k_connection_unauthorized = 0xffffffffffffffff;
 
 static void  *hSteamHTTP,
@@ -34,6 +39,7 @@ static void  *hSteamHTTP,
 static u8 steam_symetric_key[ k_nSteamEncryptedAppTicketSymmetricKeyLen ];
 static HSteamNetPollGroup client_pollgroup;
 
+#if 0
 static void recieve_http( void *callresult, void *context )
 {
    HTTPRequestCompleted_t *result = callresult;
@@ -43,16 +49,17 @@ static void recieve_http( void *callresult, void *context )
 
    SteamAPI_ISteamHTTP_GetHTTPResponseBodySize( hSteamHTTP, request, &size );
 
-   u8 *buffer = malloc( size );
+   u8 *buffer = vg_alloc( size );
    SteamAPI_ISteamHTTP_GetHTTPResponseBodyData( 
          hSteamHTTP, request, buffer, size );
 
    buffer[size-1] = '\0';
    vg_info( "%s\n", (char *)buffer );
 
-   free( buffer );
+   vg_free( buffer );
    SteamAPI_ISteamHTTP_ReleaseHTTPRequest( hSteamHTTP, result->m_hRequest );
 }
+#endif
 
 static u64_steamid get_connection_authsteamid( SteamNetworkingMessage_t *msg )
 {
@@ -123,7 +130,7 @@ static void on_inet_auth( SteamNetworkingMessage_t *msg )
       return;
    }
 
-   vg_log( "Attempting to verify user\n" );
+   vg_low( "Attempting to verify user\n" );
 
    if( msg->m_cbSize < sizeof(netmsg_auth) )
    {
@@ -270,8 +277,7 @@ static void poll_connections(void)
    SteamNetworkingMessage_t *messages[32];
    int len;
 
-   while(1)
-   {
+   while(1){
       len = SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnPollGroup(
             hSteamNetworkingSockets,
             client_pollgroup, messages, vg_list_size(messages) );
@@ -279,12 +285,10 @@ static void poll_connections(void)
       if( len <= 0 )
          return;
 
-      for( int i=0; i<len; i++ )
-      {
+      for( int i=0; i<len; i++ ){
          SteamNetworkingMessage_t *msg = messages[i];
 
-         if( msg->m_cbSize < sizeof(netmsg_blank) )
-         {
+         if( msg->m_cbSize < sizeof(netmsg_blank) ){
             vg_warn( "Discarding message (too small: %d)\n", 
                   msg->m_cbSize );
             continue;
@@ -300,6 +304,10 @@ static void poll_connections(void)
             on_inet_set_nickname( msg );
          else if( tmp->inetmsg_id == k_inetmsg_set_score )
             on_inet_set_score( msg );
+         else {
+            vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
+                     tmp->inetmsg_id );
+         }
 
          SteamAPI_SteamNetworkingMessage_t_Release( msg );
       }
@@ -315,8 +323,7 @@ static void generate_boards(void)
 {
    FILE *fp = fopen( "www/html/srhighscores.txt", "w" );
 
-   for( int i=0; i<vg_list_size(track_infos); i++ )
-   {
+   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 );
@@ -330,8 +337,16 @@ int main( int argc, char *argv[] )
 {
    signal( SIGINT, inthandler );
    signal( SIGQUIT, inthandler );
+   
+   /* 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( !vg_load_steam_symetric_key( "application_key", steam_symetric_key ) )
@@ -379,17 +394,6 @@ int main( int argc, char *argv[] )
    client_pollgroup = SteamAPI_ISteamNetworkingSockets_CreatePollGroup(
          hSteamNetworkingSockets );
 
-#if 0
-   HTTPRequestHandle test_req = SteamAPI_ISteamHTTP_CreateHTTPRequest( 
-         hSteamHTTP, k_EHTTPMethodGET, 
-         "https://www.harrygodden.com/hello.txt" );
-
-   steam_async *call1 = steam_new_async();
-   call1->data = NULL;
-   call1->p_handler = recieve_http;
-   SteamAPI_ISteamHTTP_SendHTTPRequest( hSteamHTTP, test_req, &call1->id );
-#endif
-
    u64 server_ticks = 8000,
        last_record_save = 8000,
        last_scoreboard_gen = 0;
@@ -418,7 +422,6 @@ int main( int argc, char *argv[] )
    }
    
    highscores_serialize_all();
-   highscores_free();
    
    SteamAPI_ISteamNetworkingSockets_DestroyPollGroup( hSteamNetworkingSockets,
          client_pollgroup );