server loopback tool
authorhgn <hgodden00@gmail.com>
Mon, 8 Apr 2024 17:19:38 +0000 (18:19 +0100)
committerhgn <hgodden00@gmail.com>
Mon, 8 Apr 2024 17:19:38 +0000 (18:19 +0100)
gameserver.c
gameserver.h
player_remote.c

index 1d513f47b7e416b7ffa5f9a309d27db785de86c7..e315fd0e69b7c096cbadd1d8b6d6abca6df12345 100644 (file)
@@ -23,14 +23,26 @@ static void inthandler( int signum ) {
    sig_stop = 1;
 }
 
+static void release_message( SteamNetworkingMessage_t *msg )
+{
+   msg->m_nUserData --;
+
+   if( msg->m_nUserData == 0 )
+      SteamAPI_SteamNetworkingMessage_t_Release( msg );
+}
+
 /*
  * Send message to single client, with authentication checking
  */
 static void gameserver_send_to_client( i32 client_id,
                                        const void *pData, u32 cbData,
-                                       int nSendFlags ){
+                                       int nSendFlags )
+{
    struct gameserver_client *client = &gameserver.clients[ client_id ];
 
+   if( gameserver.loopback_test && !client->connection ) 
+      return;
+
    if( !client->steamid )
       return;
 
@@ -44,8 +56,10 @@ static void gameserver_send_to_client( i32 client_id,
  */
 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++ ){
+                                    int nSendFlags )
+{
+   for( int i=0; i<vg_list_size(gameserver.clients); i++ )
+   {
       struct gameserver_client *client = &gameserver.clients[i];
 
       if( i != ignore )
@@ -53,9 +67,13 @@ static void gameserver_send_to_all( int ignore,
    }
 }
 
-static void gameserver_send_version_to_client( int index ){
+static void gameserver_send_version_to_client( int index )
+{
    struct gameserver_client *client = &gameserver.clients[index];
 
+   if( gameserver.loopback_test && !client->connection ) 
+      return;
+
    netmsg_version version;
    version.inetmsg_id = k_inetmsg_version;
    version.version = NETWORK_SKATERIFT_VERSION;
@@ -68,7 +86,8 @@ static void gameserver_send_version_to_client( int index ){
 /*
  * handle server update that client #'index' has joined
  */
-static void gameserver_player_join( int index ){
+static void gameserver_player_join( int index )
+{
    struct gameserver_client *joiner = &gameserver.clients[index];
    
    netmsg_playerjoin join = { .inetmsg_id = k_inetmsg_playerjoin,
@@ -94,7 +113,8 @@ static void gameserver_player_join( int index ){
    netmsg_region *region = alloca( sizeof(netmsg_region) + NETWORK_REGION_MAX );
    region->inetmsg_id = k_inetmsg_region;
 
-   for( int i=0; i<vg_list_size(gameserver.clients); i++ ){
+   for( int i=0; i<vg_list_size(gameserver.clients); i++ )
+   {
       struct gameserver_client *client = &gameserver.clients[i];
 
       if( (i == index) || !client->steamid )
@@ -117,7 +137,8 @@ static void gameserver_player_join( int index ){
                                  k_nSteamNetworkingSend_Reliable );
 
       /* items */
-      for( int j=0; j<k_netmsg_playeritem_max; j++ ){
+      for( int j=0; j<k_netmsg_playeritem_max; j++ )
+      {
          chs = vg_strncpy( client->items[j].uid, item->uid, ADDON_UID_MAX, 
                            k_strncpy_always_add_null );
          item->type_index = j;
@@ -182,7 +203,8 @@ static void remove_client( int index ){
  * Handle incoming new connection and init flags on the steam handle. if the 
  * server is full the userdata (client_id) will be set to -1 on the handle.
  */
-static void handle_new_connection( HSteamNetConnection conn ){
+static void handle_new_connection( HSteamNetConnection conn )
+{
    SteamAPI_ISteamNetworkingSockets_SetConnectionUserData(
          hSteamNetworkingSockets, conn, -1 );
 
@@ -208,7 +230,8 @@ static void handle_new_connection( HSteamNetConnection conn ){
    EResult accept_status = SteamAPI_ISteamNetworkingSockets_AcceptConnection(
             hSteamNetworkingSockets, conn );
 
-   if( accept_status == k_EResultOK ){
+   if( accept_status == k_EResultOK )
+   {
       vg_success( "Accepted client (id: %u, index: %d)\n", conn, index );
 
       client->active = 1;
@@ -219,8 +242,17 @@ static void handle_new_connection( HSteamNetConnection conn ){
       
       SteamAPI_ISteamNetworkingSockets_SetConnectionUserData(
             hSteamNetworkingSockets, conn, index );
+
+      if( gameserver.loopback_test )
+      {
+         vg_warn( "[DEV] Creating loopback client\n" );
+         struct gameserver_client *loopback = &gameserver.clients[1];
+         loopback->active = 1;
+         loopback->connection = 0;
+      }
    }
-   else{
+   else
+   {
       vg_warn( "Error accepting connection (id: %u)\n", conn );
       SteamAPI_ISteamNetworkingSockets_CloseConnection( 
             hSteamNetworkingSockets, conn, 
@@ -240,8 +272,19 @@ static void on_auth_status( CallbackMsg_t *msg ){
  * Get client id of connection handle. Will be -1 if unkown to us either because
  * the server is full or we already disconnected them
  */
-static i32 gameserver_conid( HSteamNetConnection hconn ){
-   i64 id = SteamAPI_ISteamNetworkingSockets_GetConnectionUserData(
+static i32 gameserver_conid( HSteamNetConnection hconn )
+{
+   i64 id;
+
+   if( hconn == 0 )
+   {
+      if( gameserver.loopback_test )
+         return 1;
+      else
+         return -1;
+   }
+   else
+      id = SteamAPI_ISteamNetworkingSockets_GetConnectionUserData(
                hSteamNetworkingSockets, hconn );
 
    if( (id < 0) || (id >= NETWORK_MAX_PLAYERS) )
@@ -253,7 +296,8 @@ static i32 gameserver_conid( HSteamNetConnection hconn ){
 /*
  * Callback for steam connection state change
  */
-static void on_connect_status( CallbackMsg_t *msg ){
+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 );
 
@@ -261,7 +305,8 @@ static void on_connect_status( CallbackMsg_t *msg ){
          string_ESteamNetworkingConnectionState(info->m_eOldState),
          string_ESteamNetworkingConnectionState(info->m_info.m_eState) );
 
-   if( info->m_info.m_eState==k_ESteamNetworkingConnectionState_Connecting ){
+   if( info->m_info.m_eState==k_ESteamNetworkingConnectionState_Connecting )
+   {
       handle_new_connection( info->m_hConn );
    }
 
@@ -277,22 +322,32 @@ static void on_connect_status( CallbackMsg_t *msg ){
       vg_info( "End reason: %d\n", info->m_info.m_eEndReason );
 
       int client_id = gameserver_conid( info->m_hConn );
-      if( client_id != -1 ){
+      if( client_id != -1 )
+      {
          gameserver_player_leave( client_id );
          remove_client( client_id );
+
+         if( gameserver.loopback_test )
+         {
+            gameserver_player_leave( 1 );
+            remove_client( 1 );
+         }
       }
-      else {
+      else 
+      {
          SteamAPI_ISteamNetworkingSockets_CloseConnection( 
                hSteamNetworkingSockets, info->m_hConn, 0, NULL, 0 );
       }
    }
 }
 
-static void gameserver_rx_version( SteamNetworkingMessage_t *msg ){
+static void gameserver_rx_version( SteamNetworkingMessage_t *msg )
+{
    netmsg_version *version = msg->m_pData;
 
    int client_id = gameserver_conid( msg->m_conn );
-   if( client_id == -1 ) {
+   if( client_id == -1 ) 
+   {
       vg_warn( "Recieved version from unkown connection (%u)\n", msg->m_conn );
       SteamAPI_ISteamNetworkingSockets_CloseConnection( 
             hSteamNetworkingSockets, msg->m_conn,
@@ -303,7 +358,8 @@ static void gameserver_rx_version( SteamNetworkingMessage_t *msg ){
 
    struct gameserver_client *client = &gameserver.clients[ client_id ];
 
-   if( client->version ){
+   if( client->version )
+   {
       vg_warn( "Already have version for this client (%d conn: %u)", 
                client_id, msg->m_conn );
       return;
@@ -311,7 +367,8 @@ static void gameserver_rx_version( SteamNetworkingMessage_t *msg ){
 
    client->version = version->version;
 
-   if( client->version != NETWORK_SKATERIFT_VERSION ){
+   if( client->version != NETWORK_SKATERIFT_VERSION )
+   {
       gameserver_send_version_to_client( client_id );
       remove_client( client_id );
       return;
@@ -320,9 +377,17 @@ static void gameserver_rx_version( SteamNetworkingMessage_t *msg ){
    /* this is the sign on point for non-auth servers,
     * for auth servers it comes at the end of rx_auth 
     */
-   if( gameserver.auth_mode != eServerModeAuthentication ){
+   if( gameserver.auth_mode != eServerModeAuthentication )
+   {
       client->steamid = k_steamid_max;
       gameserver_player_join( client_id );
+
+      if( gameserver.loopback_test )
+      {
+         struct gameserver_client *loopback = &gameserver.clients[1];
+         loopback->steamid = k_steamid_max;
+         gameserver_player_join( 1 );
+      }
    }
 }
 
@@ -518,7 +583,8 @@ static void gameserver_propogate_player_frame( int client_id,
    struct gameserver_client *c0 = &gameserver.clients[client_id];
    c0->instance = frame->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
 
-   for( int i=0; i<vg_list_size(gameserver.clients); i++ ){
+   for( int i=0; i<vg_list_size(gameserver.clients); i++ )
+   {
       if( i == client_id )
          continue;
 
@@ -526,7 +592,8 @@ static void gameserver_propogate_player_frame( int client_id,
 
       int send_full = 0;
 
-      if( c0->instance == ci->instance ){
+      if( c0->instance == ci->instance )
+      {
          u32 k_index = network_pair_index( client_id, i );
          u8 k_mask = gameserver.client_knowledge_mask[ k_index ];
          
@@ -534,18 +601,21 @@ static void gameserver_propogate_player_frame( int client_id,
             send_full = 1;
       }
 
-      if( send_full ){
+      if( send_full )
+      {
          gameserver_send_to_client( i, full, size, 
                                     k_nSteamNetworkingSend_Unreliable );
       }
-      else {
+      else 
+      {
          gameserver_send_to_client( i, basic, basic_size, 
                                     k_nSteamNetworkingSend_Unreliable );
       }
    }
 }
 
-static void gameserver_rx_200_300( SteamNetworkingMessage_t *msg ){
+static void gameserver_rx_200_300( SteamNetworkingMessage_t *msg )
+{
    netmsg_blank *tmp = msg->m_pData;
 
    int client_id = gameserver_conid( msg->m_conn );
@@ -553,7 +623,8 @@ static void gameserver_rx_200_300( SteamNetworkingMessage_t *msg ){
 
    struct gameserver_client *client = &gameserver.clients[ client_id ];
 
-   if( tmp->inetmsg_id == k_inetmsg_playerusername ){
+   if( tmp->inetmsg_id == k_inetmsg_playerusername )
+   {
       if( !packet_minsize( msg, sizeof(netmsg_playerusername)+1 ))
          return;
       
@@ -589,16 +660,18 @@ static void gameserver_rx_200_300( SteamNetworkingMessage_t *msg ){
       call->handler = gameserver_update_db_username;
       db_send_request( call );
    }
-   else if( tmp->inetmsg_id == k_inetmsg_playerframe ){
+   else if( tmp->inetmsg_id == k_inetmsg_playerframe )
+   {
       gameserver_propogate_player_frame( client_id, 
                                          msg->m_pData, msg->m_cbSize );
    }
-   else if( tmp->inetmsg_id == k_inetmsg_playeritem ){
+   else if( tmp->inetmsg_id == k_inetmsg_playeritem )
+   {
       netmsg_playeritem *item = msg->m_pData;
 
       /* record */
-
-      if( item->type_index >= k_netmsg_playeritem_max ){
+      if( item->type_index >= k_netmsg_playeritem_max )
+      {
          vg_warn( "Client #%d invalid equip type %u\n", 
                   client_id, (u32)item->type_index );
          return;
@@ -626,7 +699,8 @@ static void gameserver_rx_200_300( SteamNetworkingMessage_t *msg ){
       gameserver_send_to_all( client_id, prop, msg->m_cbSize, 
                               k_nSteamNetworkingSend_Reliable );
    }
-   else if( tmp->inetmsg_id == k_inetmsg_chat ){
+   else if( tmp->inetmsg_id == k_inetmsg_chat )
+   {
       netmsg_chat *chat = msg->m_pData,
                   *prop = alloca( sizeof(netmsg_chat) + NETWORK_MAX_CHAT );
       prop->inetmsg_id = k_inetmsg_chat;
@@ -639,7 +713,8 @@ static void gameserver_rx_200_300( SteamNetworkingMessage_t *msg ){
       gameserver_send_to_all( client_id, prop, sizeof(netmsg_chat)+l+1, 
                               k_nSteamNetworkingSend_Reliable );
    }
-   else if( tmp->inetmsg_id == k_inetmsg_region ){
+   else if( tmp->inetmsg_id == k_inetmsg_region )
+   {
       netmsg_region *region = msg->m_pData,
                     *prop = alloca( sizeof(netmsg_region) + NETWORK_REGION_MAX );
 
@@ -659,7 +734,8 @@ static void gameserver_rx_200_300( SteamNetworkingMessage_t *msg ){
                               k_nSteamNetworkingSend_Reliable );
       vg_info( "client %d moved to region: %s\n", client_id, client->region );
    }
-   else {
+   else 
+   {
       vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
                tmp->inetmsg_id );
    }
@@ -678,11 +754,18 @@ static void gameserver_request_respond( enum request_status status,
 
    res->status = status;
 
+   if( gameserver.loopback_test && !msg->m_conn )
+   {
+      release_message( msg );
+      return;
+   }
+
    SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
          hSteamNetworkingSockets, msg->m_conn,
          res, sizeof(netmsg_request) + len,
          k_nSteamNetworkingSend_Reliable, NULL );
-   SteamAPI_SteamNetworkingMessage_t_Release( msg );
+
+   release_message( msg );
 }
 
 struct user_request_thread_data {
@@ -749,13 +832,15 @@ static enum request_status gameserver_cat_table(
    return k_request_status_ok;
 }
 
-static void gameserver_process_user_request( db_request *db_req ){
+static void gameserver_process_user_request( db_request *db_req )
+{
    struct user_request_thread_data *inf = (void *)db_req->data;
    SteamNetworkingMessage_t *msg = inf->msg;
 
    int client_id = gameserver_conid( msg->m_conn );
-   if( client_id == -1 ){
-      SteamAPI_SteamNetworkingMessage_t_Release( msg );
+   if( client_id == -1 )
+   {
+      release_message( msg );
       return;
    }
 
@@ -846,18 +931,30 @@ static void gameserver_process_user_request( db_request *db_req ){
    }
 }
 
-static void gameserver_rx_300_400( SteamNetworkingMessage_t *msg ){
+static void gameserver_rx_300_400( SteamNetworkingMessage_t *msg )
+{
    netmsg_blank *tmp = msg->m_pData;
 
    int client_id = gameserver_conid( msg->m_conn );
-   if( client_id == -1 ){
-      SteamAPI_SteamNetworkingMessage_t_Release( msg );
+   if( client_id == -1 )
+   {
+      release_message( msg );
       return;
    }
 
-   if( tmp->inetmsg_id == k_inetmsg_request ){
+   if( tmp->inetmsg_id == k_inetmsg_request )
+   {
+      if( gameserver.loopback_test && (client_id == 1) )
+      {
+         release_message( msg );
+         return;
+      }
+
       if( !packet_minsize( msg, sizeof(netmsg_request)+1 ))
+      {
+         release_message( msg );
          return;
+      }
 
       db_request *call = db_alloc_request( 
                            sizeof(struct user_request_thread_data) );
@@ -866,18 +963,54 @@ static void gameserver_rx_300_400( SteamNetworkingMessage_t *msg ){
       call->handler = gameserver_process_user_request;
       db_send_request( call );
    }
-   else {
+   else 
+   {
       vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
                tmp->inetmsg_id );
-      SteamAPI_SteamNetworkingMessage_t_Release( msg );
+      release_message( msg );
+   }
+}
+
+static void process_network_message( SteamNetworkingMessage_t *msg )
+{
+   if( msg->m_cbSize < sizeof(netmsg_blank) ){
+      vg_warn( "Discarding message (too small: %d)\n", 
+            msg->m_cbSize );
+      return;
+   }
+
+   netmsg_blank *tmp = msg->m_pData;
+
+   if( (tmp->inetmsg_id >= 200) && (tmp->inetmsg_id < 300) )
+   {
+      gameserver_rx_200_300( msg );
+      release_message( msg );
+   }
+   else if( (tmp->inetmsg_id >= 300) && (tmp->inetmsg_id < 400) )
+   {
+      gameserver_rx_300_400( msg );
+   }
+   else{
+      if( tmp->inetmsg_id == k_inetmsg_auth )
+         gameserver_rx_auth( msg );
+      else if( tmp->inetmsg_id == k_inetmsg_version ){
+         gameserver_rx_version( msg );
+      }
+      else {
+         vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
+                  tmp->inetmsg_id );
+      }
+      release_message( msg );
    }
 }
 
-static void poll_connections(void){
+static void poll_connections(void)
+{
    SteamNetworkingMessage_t *messages[32];
    int len;
 
-   while(1){
+   while(1)
+   {
       len = SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnPollGroup(
             hSteamNetworkingSockets,
             gameserver.client_group, messages, vg_list_size(messages) );
@@ -885,36 +1018,21 @@ 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) ){
-            vg_warn( "Discarding message (too small: %d)\n", 
-                  msg->m_cbSize );
-            continue;
+         msg->m_nUserData = 1;
+
+         if( gameserver.loopback_test )
+         {
+            HSteamNetConnection conid = msg->m_conn;
+            msg->m_conn = 0;
+            msg->m_nUserData ++;
+            process_network_message( msg );
+            msg->m_conn = conid;
          }
 
-         netmsg_blank *tmp = msg->m_pData;
-
-         if( (tmp->inetmsg_id >= 200) && (tmp->inetmsg_id < 300) ){
-            gameserver_rx_200_300( msg );
-            SteamAPI_SteamNetworkingMessage_t_Release( msg );
-         }
-         else if( (tmp->inetmsg_id >= 300) && (tmp->inetmsg_id < 400) ){
-            gameserver_rx_300_400( msg );
-         }
-         else{
-            if( tmp->inetmsg_id == k_inetmsg_auth )
-               gameserver_rx_auth( msg );
-            else if( tmp->inetmsg_id == k_inetmsg_version ){
-               gameserver_rx_version( msg );
-            }
-            else {
-               vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
-                        tmp->inetmsg_id );
-            }
-            SteamAPI_SteamNetworkingMessage_t_Release( msg );
-         }
+         process_network_message( msg );
       }
    }
 }
@@ -929,9 +1047,13 @@ int main( int argc, char *argv[] ){
    signal( SIGPIPE, SIG_IGN );
 
    char *arg;
-   while( vg_argp( argc, argv ) ){
+   while( vg_argp( argc, argv ) )
+   {
       if( vg_long_opt( "noauth" ) )
          gameserver.auth_mode = eServerModeNoAuthentication;
+
+      if( vg_long_opt( "loopback" ) )
+         gameserver.loopback_test = 1;
    }
    
    vg_set_mem_quota( 80*1024*1024 );
index 00df20e2f0a5570f8b9cba777f2d84e05a6531b0..3d0e71383bf9b5a8a2d0afe73e1b77ffde16d923 100644 (file)
@@ -46,6 +46,8 @@ struct {
    /* TODO: Consider removing this */
    int monitor_fd;
    int monitor_clients[ 4 ];
+
+   bool loopback_test;
 }
 static gameserver = {
    .auth_mode = eServerModeAuthentication
index f829ec0512de5585cc54a5a21c2feb1e53af0439..b08f5eee6e52ddfb45b56839cd5b8e38f8dbef45 100644 (file)
@@ -148,6 +148,8 @@ void player_remote_rx_200_300( SteamNetworkingMessage_t *msg )
       }
       
       netmsg_playerframe *frame = msg->m_pData;
+      vg_info( "recv %i from %i.\n", (i32)tmp->inetmsg_id, (i32)frame->client );
+
       if( frame->client >= vg_list_size(netplayers.list) ){
          vg_error( "inetmsg_playerframe: player index out of range\n" );
          return;
@@ -157,6 +159,8 @@ void player_remote_rx_200_300( SteamNetworkingMessage_t *msg )
          vg_error( "inetmsg_playerframe: subsystem out of range\n" );
          return;
       }
+      vg_info( "subsystem: %i, sfx: %i.\n", (i32)frame->subsystem, 
+               (i32)frame->sound_effects );
 
       struct interp_buffer *ib = &netplayers.interp_data[ frame->client ];
       struct interp_frame *dest = NULL;