Do actual domain resolution
[carveJwlIkooP6JGAAIwe30JlM.git] / network.h
index ee0f334d5cb04156dda91cac833cdcbd8cd04b9c..4bd5a37ddcc6dbcad21e9b0022c60c8ed17e8199 100644 (file)
--- a/network.h
+++ b/network.h
-#ifndef NETWORK_H
-#define NETWORK_H
+/*
+ * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ * All trademarks are property of their respective owners
+ */
 
-#include "vg/vg_stdint.h"
+#pragma once
+#include "vg/vg_platform.h"
+#include "vg/vg_steam_networking.h"
 #include "steam.h"
+#include "network_common.h"
 #include "network_msg.h"
+#include "addon_types.h"
+
+#define NETWORK_MAX_REQUESTS 8
 
 /* 
  * Interface
  */
 
 /* Call it at start; Connects us to the gameserver */
-static void network_init(void);
+void network_init(void);
 
 /* Run this from main loop */
-static void network_update(void);
+void network_update(void);
 
 /* Call it at shutdown */
-static void network_end(void);
+void network_end(void);
 
 /* 
  * Can buffer up a bunch of these by calling many times, they will be
  * sent at the next connection 
  */
-static void network_submit_highscore( u32 trackid, u16 points, u16 time );
+void network_submit_highscore( u32 trackid, u16 points, u16 time );
 
 /*
  * Game endpoints are provided with the same names to allow running without a
  * network connection.
  */
-#ifdef SR_NETWORKED
-
-/* 
- * Runtime connection stuff
- */
-static u8 steam_app_ticket[ 1024 ];
-static u32 steam_app_ticket_length;
-
-static HSteamNetConnection cremote;
-static ESteamNetworkingConnectionState cremote_state = 
-     k_ESteamNetworkingConnectionState_None;
-
-/* 
- * Implementation
- */
 
-static void scores_update(void);
-
-static void on_auth_ticket_recieved( void *result, void *context )
+struct network_client
 {
-   EncryptedAppTicketResponse_t *response = result;
+   u8 app_symmetric_key[ 1024 ];
+   u32 app_key_length;
+   EServerMode auth_mode;
 
-   if( response->m_eResult == k_EResultOK )
-   {
-      vg_info( "  New app ticket ready\n" );
-   }
-   else
-   {
-      vg_warn( "  Could not request new encrypted app ticket (%u)\n",
-                  response->m_eResult );
-   }
-   
-   if( SteamAPI_ISteamUser_GetEncryptedAppTicket( hSteamUser, 
-            steam_app_ticket,
-            vg_list_size(steam_app_ticket),
-            &steam_app_ticket_length ))
-   {
-      vg_success( "  Loaded app ticket (%u bytes)\n", steam_app_ticket_length );
-   }
-   else
-   {
-      vg_error( "  No ticket availible\n" );
-      steam_app_ticket_length = 0;
-   }
-}
+   HSteamNetConnection remote;
+   ESteamNetworkingConnectionState state;
+   u32 remote_version;
 
-static void request_auth_ticket(void)
-{
-   /* 
-    * TODO Check for one thats cached on the disk and load it.
-    * This might be OK though because steam seems to cache the result 
-    */
-
-   vg_info( "Requesting new authorization ticket\n" );
-   steam_async *call = steam_new_async();
-   call->data = NULL;
-   call->p_handler = on_auth_ticket_recieved;
-   call->id =  SteamAPI_ISteamUser_RequestEncryptedAppTicket( hSteamUser, 
-                                                              NULL, 0 );
-}
-
-static void server_connect(void)
-{
-   /* Connect to server if not connected */
-   
-   SteamNetworkingIPAddr remoteAddr;
-
-#define USE_LOCALHOST
-#ifdef USE_LOCALHOST
-   SteamAPI_SteamNetworkingIPAddr_SetIPv6LocalHost( &remoteAddr, 27402 );
-#else
-   const char *server_lon1 = "46.101.34.155:27402";
-   SteamAPI_SteamNetworkingIPAddr_ParseString( &remoteAddr, server_lon1 );
-#endif
+   f64 last_attempt, last_frame;
+   u32 retries;
 
-   char buf[256];
-   SteamAPI_SteamNetworkingIPAddr_ToString( &remoteAddr, buf, 256, 1 );
-   vg_info( "connect to: %s\n", buf );
-
-   cremote = SteamAPI_ISteamNetworkingSockets_ConnectByIPAddress( 
-                  hSteamNetworkingSockets, &remoteAddr, 0, NULL );
-}
-
-static void send_auth_ticket(void)
-{
-   u32 size = sizeof(netmsg_auth) + steam_app_ticket_length;
-   netmsg_auth *auth = malloc(size);
-
-   auth.inetmsg_id = k_inetmsg_auth;
-   auth.ticket_length = steam_app_ticket_length; 
-   for( int i=0; i<steam_app_ticket_length; i++ )
-      auth.ticket[i] = steam_app_ticket[i];
-
-   SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
-         hSteamNetworkingSockets, cremote, auth, size,
-         k_nSteamNetworkingSend_Reliable, NULL );
-   
-   free( auth );
-}
-
-static void scores_update(void)
-{
-   vg_log( "scores_update()\n" );
+   i32 network_info;
+   i32 auto_connect;
 
-   if( cremote_state == k_ESteamNetworkingConnectionState_Connected )
-   {
-      /*
-       * request updated scores, this does not require any authentication.
-       */
-      netmsg_scores_request req;
-      req.inetmsg_id = k_inetmsg_scores_request;
-
-      SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
-            hSteamNetworkingSockets, cremote, &req, 
-            sizeof(netmsg_scores_request),
-            k_nSteamNetworkingSend_Reliable, NULL );
-
-      /*
-       * Send record update, it requires authentication
-       */
-      if( steam_app_ticket_length )
-      {
-      }
-   }
-   else
-   {
-      /* 
-       * if we are not connected, make a connection to the server and then in 
-       * the future this function will be called again when it is connected 
-       */
-      server_connect();
-   }
-}
-
-static void poll_connection(void)
-{
-   SteamNetworkingMessage_t *messages[32];
-   int len;
-
-   while(1)
-   {
-      len = SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnConnection(
-            hSteamNetworkingSockets, cremote, 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 == k_inetmsg_scores_info )
-         {
-            netmsg_scores_info *info = msg->m_pData;
-            vg_log( "Recieved %u score records\n", info->record_count );
-
-            SteamAPI_ISteamNetworkingSockets_CloseConnection(
-                  hSteamNetworkingSockets, cremote, 0, NULL, 1 );
-            cremote_state = k_ESteamNetworkingConnectionState_None;
-         }
-
-         SteamAPI_SteamNetworkingMessage_t_Release( msg );
-      }
-   }
-}
-
-static u64 in_server_ticks( double seconds )
-{
-   return (u64)(seconds / 0.1);
-}
-
-static void on_server_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_info.m_eState),
-         string_ESteamNetworkingConnectionState(info->m_eOldState) );
-
-   if( info->m_hConn == cremote )
-   {
-      cremote_state = info->m_info.m_eState;
-      if( info->m_info.m_eState ==
-            k_ESteamNetworkingConnectionState_Connected )
-      {
-         vg_success("  Connected to remote server\n");
-         scores_update();
-      }
-   }
-   else
-   {
-      vg_warn( "  Recieved signal from unknown connection\n" );
+   struct network_request {
+      vg_pool_node poolnode;
+      void (*callback)( netmsg_request *res, vg_msg *body, u64 userdata );
+      f64 sendtime;
+      u64 userdata;
    }
-}
+   *request_buffer;
+   vg_pool request_pool;
 
-static void network_init(void)
-{
-   if( steam_ready )
-   {
-      steam_register_callback( k_iSteamNetConnectionStatusChangedCallBack,
-                               on_server_connect_status );
-      request_auth_ticket();
-   }
-}
+   SteamNetworkingIPAddr ip;
+   char host_port[8], host_adress[256];
+   bool ip_resolved;
 
-static void network_update(void)
-{
-   if( steam_ready )
-   {
-      static double last_update = -9000.0;
-      poll_connection();
-      
-      if( vg_time > (last_update + 60.0) )
-      {
-         last_update = vg_time;
-         scores_update();
-      }
+   enum server_intent {
+      k_server_intent_offline,
+      k_server_intent_online
    }
+   user_intent;
+   f64 last_intent_change;
+   f32 fintent; /* yeah this shit really shouldnt be here but oh well */
 }
-
-static void network_end(void)
+extern network_client;
+
+int packet_minsize( SteamNetworkingMessage_t *msg, u32 size );
+void network_send_item( enum netmsg_playeritem_type type );
+void network_request_scoreboard( const char *mod_uid, 
+                                 const char *route_uid,
+                                 u32 week, u64 userdata );
+void network_publish_laptime( const char *mod_uid, 
+                              const char *route_uid, f64 lap_time );
+void chat_send_message( const char *message );
+void render_server_status_gui(void);
+void network_status_string( vg_str *str, u32 *colour );
+void network_send_region(void);
+void network_set_host( const char *host_str, const char *port_str );
+
+static inline int network_connected(void)
 {
-   /* TODO: Fire off any buffered highscores that need to be setn */
+   if( network_client.remote_version != NETWORK_SKATERIFT_VERSION ) return 0;
+   return network_client.state == k_ESteamNetworkingConnectionState_Connected;
 }
-
-#endif
-#endif /* NETWORK_H */