test network 3
[carveJwlIkooP6JGAAIwe30JlM.git] / network.c
index ef7dee51bbdebadf36e61707c38337a10ff3edd1..4042ef4ef3dd1f5882617141c63f15a555e00b22 100644 (file)
--- a/network.c
+++ b/network.c
@@ -1,6 +1,21 @@
-VG_STATIC void scores_update(void);
+#include "player.h"
+#include "network.h"
+#include "network_msg.h"
+#include "player_remote.h"
 
-VG_STATIC void on_auth_ticket_recieved( void *result, void *context ){
+static void scores_update(void);
+
+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 on_auth_ticket_recieved( void *result, void *context ){
    EncryptedAppTicketResponse_t *response = result;
 
    if( response->m_eResult == k_EResultOK ){
@@ -23,7 +38,7 @@ VG_STATIC void on_auth_ticket_recieved( void *result, void *context ){
    }
 }
 
-VG_STATIC void request_auth_ticket(void){
+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 
@@ -38,21 +53,8 @@ VG_STATIC void request_auth_ticket(void){
       SteamAPI_ISteamUser_RequestEncryptedAppTicket( hSteamUser, NULL, 0 );
 }
 
-VG_STATIC void send_auth_ticket(void){
-   u32 size = sizeof(netmsg_auth) + network_client.app_key_length;
-   netmsg_auth *auth = alloca(size);
-
-   auth->inetmsg_id = k_inetmsg_auth;
-   auth->ticket_length = network_client.app_key_length;
-   for( int i=0; i<network_client.app_key_length; i++ )
-      auth->ticket[i] = network_client.app_symmetric_key[i];
-
-   SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
-         hSteamNetworkingSockets, network_client.remote, auth, size,
-         k_nSteamNetworkingSend_Reliable, NULL );
-}
-
-VG_STATIC void send_score_request(void){
+#if 0
+static void send_score_request(void){
    vg_info( "Requesting scores\n" );
    netmsg_scores_request req;
    req.inetmsg_id = k_inetmsg_scores_request;
@@ -63,7 +65,7 @@ VG_STATIC void send_score_request(void){
          k_nSteamNetworkingSend_Reliable, NULL );
 }
 
-VG_STATIC void send_score_update(void){
+static void send_score_update(void){
    vg_info( "Sending scores\n" );
    u32 size = sizeof(netmsg_set_score) + 
                   vg_list_size(track_infos)*sizeof(struct netmsg_score_record);
@@ -104,7 +106,7 @@ VG_STATIC void send_score_update(void){
          k_nSteamNetworkingSend_Reliable, NULL );
 }
 
-VG_STATIC void send_nickname(void){
+static void send_nickname(void){
    netmsg_set_nickname nick;
    nick.inetmsg_id = k_inetmsg_set_nickname;
 
@@ -119,6 +121,7 @@ VG_STATIC void send_nickname(void){
 
    network_client.name_update = 0;
 }
+#endif
 
 static void network_send_playerframe(void){
    netmsg_playerframe frame;
@@ -131,15 +134,30 @@ static void network_send_playerframe(void){
          k_nSteamNetworkingSend_Unreliable, NULL );
 }
 
-VG_STATIC void server_routine_update(void){
-   if( network_client.name_update )
-      send_nickname();
-
+#if 0
+static void server_routine_update(void){
    send_score_update();
    send_score_request();
 }
+#endif
+
+static void network_send_username(void){
+   netmsg_playerusername update;
+   memset( &update, 0, sizeof(update) );
+   update.inetmsg_id = k_inetmsg_playerusername;
+   update.index = 0xffffffff;
+
+   ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
+   const char *username = SteamAPI_ISteamFriends_GetPersonaName(hSteamFriends);
+   str_utf8_collapse( username, update.username, sizeof(update.username) );
+
+   SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
+         hSteamNetworkingSockets, network_client.remote, 
+         &update, sizeof(update),
+         k_nSteamNetworkingSend_Reliable, NULL );
+}
 
-VG_STATIC void on_server_connect_status( CallbackMsg_t *msg ){
+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", 
@@ -148,17 +166,64 @@ VG_STATIC void on_server_connect_status( CallbackMsg_t *msg ){
 
    if( info->m_hConn == network_client.remote ){
       network_client.state = info->m_info.m_eState;
-      if( info->m_info.m_eState == k_ESteamNetworkingConnectionState_Connected ){
+
+      if( info->m_info.m_eState == 
+            k_ESteamNetworkingConnectionState_Connected ){
          vg_success("  Connected to remote server.. authenticating\n");
-         send_auth_ticket();
+
+         /* TODO: We should really wait to see if the server is in auth mode
+          * first... */
+         u32 size = sizeof(netmsg_auth) + network_client.app_key_length;
+         netmsg_auth *auth = alloca(size);
+         auth->inetmsg_id = k_inetmsg_auth;
+         auth->ticket_length = network_client.app_key_length;
+         for( int i=0; i<network_client.app_key_length; i++ )
+            auth->ticket[i] = network_client.app_symmetric_key[i];
+
+         SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
+               hSteamNetworkingSockets, network_client.remote, auth, size,
+               k_nSteamNetworkingSend_Reliable, NULL );
+
+         network_send_username();
+      }
+      else if( info->m_info.m_eState == 
+            k_ESteamNetworkingConnectionState_ClosedByPeer ){
+
+         if( info->m_info.m_eEndReason == 
+               k_ESteamNetConnectionEnd_Remote_Max ){
+            network_client.retries = 40;
+         }
+
+         SteamAPI_ISteamNetworkingSockets_CloseConnection( 
+               hSteamNetworkingSockets, info->m_hConn, 0, NULL, 0 );
+         network_client.remote = 0;
+      }
+      else if( info->m_info.m_eState == 
+            k_ESteamNetworkingConnectionState_ProblemDetectedLocally ){
+         SteamAPI_ISteamNetworkingSockets_CloseConnection( 
+               hSteamNetworkingSockets, info->m_hConn, 0, NULL, 0 );
+         network_client.remote = 0;
       }
    }
    else{
-      vg_warn( "  Recieved signal from unknown connection\n" );
+      //vg_warn( "  Recieved signal from unknown connection\n" );
+   }
+}
+
+static void on_persona_state_change( CallbackMsg_t *msg ){
+   if( network_client.remote ){
+      PersonaStateChange_t *info = (void *)msg->m_pubParam;
+
+      ISteamUser *hSteamUser = SteamAPI_SteamUser();
+      if( info->m_ulSteamID == SteamAPI_ISteamUser_GetSteamID(hSteamUser) ){
+         if( info->m_nChangeFlags == k_EPersonaChangeNickname ){
+            network_send_username();
+         }
+      }
    }
 }
 
-VG_STATIC void network_connect(void){
+static void network_connect(void){
    /* Connect to server if not connected */
    SteamNetworkingIPAddr remoteAddr;
 
@@ -168,6 +233,8 @@ VG_STATIC void network_connect(void){
    const char *server_lon1 = "46.101.34.155:27402";
    SteamAPI_SteamNetworkingIPAddr_ParseString( &remoteAddr, server_lon1 );
 #endif
+   const char *server_lan = "192.168.1.32:27402";
+   SteamAPI_SteamNetworkingIPAddr_ParseString( &remoteAddr, server_lan );
 
    char buf[256];
    SteamAPI_SteamNetworkingIPAddr_ToString( &remoteAddr, buf, 256, 1 );
@@ -177,7 +244,7 @@ VG_STATIC void network_connect(void){
                   hSteamNetworkingSockets, &remoteAddr, 0, NULL );
 }
 
-VG_STATIC void on_inet_scoreboard( SteamNetworkingMessage_t *msg ){
+static void on_inet_scoreboard( SteamNetworkingMessage_t *msg ){
    netmsg_scoreboard *sb = msg->m_pData;
 
    u32 base_size = sizeof(netmsg_scoreboard)-
@@ -202,14 +269,16 @@ VG_STATIC void on_inet_scoreboard( SteamNetworkingMessage_t *msg ){
       }
    }
 
+#if 0
    /* We dont need to stay on the server currently */
    SteamAPI_ISteamNetworkingSockets_CloseConnection(
          hSteamNetworkingSockets, network_client.remote, 0, NULL, 1 );
+#endif
 
    network_scores_updated = 1;
 }
 
-VG_STATIC void poll_remote_connection(void){
+static void poll_remote_connection(void){
    SteamNetworkingMessage_t *messages[32];
    int len;
 
@@ -231,15 +300,16 @@ VG_STATIC void poll_remote_connection(void){
 
          netmsg_blank *tmp = msg->m_pData;
 
-         if( tmp->inetmsg_id == k_inetmsg_scoreboard )
-            on_inet_scoreboard( msg );
+         if( (tmp->inetmsg_id >= 200) && (tmp->inetmsg_id < 300) ){
+            player_remote_rx_200_300( msg );
+         }
 
          SteamAPI_SteamNetworkingMessage_t_Release( msg );
       }
    }
 }
 
-VG_STATIC void network_update(void){
+static void network_update(void){
    if( !steam_ready )
       return;
 
@@ -276,15 +346,19 @@ VG_STATIC void network_update(void){
    }
 }
 
-VG_STATIC void network_init(void){
+static void network_init(void){
+   vg_console_reg_var( "network_info", &network_client.network_info,
+                       k_var_dtype_i32, VG_VAR_PERSISTENT );
    if( steam_ready ){
       steam_register_callback( k_iSteamNetConnectionStatusChangedCallBack,
                                on_server_connect_status );
+      steam_register_callback( k_iPersonaStateChange, 
+                               on_persona_state_change );
       request_auth_ticket();
    }
 }
 
-VG_STATIC void network_end(void){
+static void network_end(void){
    /* TODO: Send buffered highscores that were not already */
    if( (network_client.state == k_ESteamNetworkingConnectionState_Connected) ||
        (network_client.state == k_ESteamNetworkingConnectionState_Connecting) )