test network 1
[carveJwlIkooP6JGAAIwe30JlM.git] / gameserver.c
index c63711ab30883f6cfcea0d17f09e2f5b611635f7..8db34132f61adb0ebc5386bfcbe40492cc9a25ef 100644 (file)
@@ -34,21 +34,87 @@ static void set_connection_authsteamid(HSteamNetConnection con, u64_steamid id){
          hSteamNetworkingSockets, con, userdata );
 }
 
+static void gameserver_player_join( int index ){
+   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 join;
+      join.inetmsg_id = k_inetmsg_playerjoin;
+      join.index = index;
+      join.board_uid[0] = '\0';
+      join.playermodel_uid[0] = '\0';
+      join.username[0] = '\0';
+
+      SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
+            hSteamNetworkingSockets, client->connection,
+            &join, sizeof(join), k_nSteamNetworkingSend_Reliable, NULL );
+   }
+}
+
+static void gameserver_player_leave( int index ){
+   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 leave;
+      leave.inetmsg_id = k_inetmsg_playerjoin;
+      leave.index = index;
+
+      SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
+            hSteamNetworkingSockets, client->connection,
+            &leave, sizeof(leave), k_nSteamNetworkingSend_Reliable, NULL );
+   }
+}
+
 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)\n", client );
+      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;
    }
 }
 
@@ -70,6 +136,29 @@ static void on_connect_status( CallbackMsg_t *msg ){
    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 ) ){
+
+      for( int i=0; i<vg_list_size(gameserver.clients); i++ ){
+         struct gameserver_client *client = &gameserver.clients[i];
+
+         if( client->active ){
+            if( client->connection == info->m_hConn ){
+               client->connection = 0;
+               client->active = 0;
+               gameserver_player_leave(i);
+               break;
+            }
+         }
+      }
+
+      vg_info( "End reason: %d\n", info->m_info.m_eEndReason );
+      SteamAPI_ISteamNetworkingSockets_CloseConnection( 
+            hSteamNetworkingSockets, info->m_hConn, 0, NULL, 0 );
+   }
 }
 
 static void on_inet_auth( SteamNetworkingMessage_t *msg ){
@@ -150,7 +239,7 @@ static int inet_require_auth( SteamNetworkingMessage_t *msg ){
 
    if( get_connection_authsteamid( msg ) == k_connection_unauthorized ){
       vg_warn( "Unauthorized request! Disconnecting client: %u\n", 
-            msg->m_conn );
+               msg->m_conn );
 
       SteamAPI_ISteamNetworkingSockets_CloseConnection(
             hSteamNetworkingSockets,
@@ -221,11 +310,7 @@ static void on_inet_playerframe( SteamNetworkingMessage_t *msg ){
       return;
    }
 
-
    netmsg_playerframe *info = msg->m_pData;
-   vg_info( "... @: %.2f %.2f %.2f\n", 
-               //msg->m_identityPeer,
-               info->pos_temp[0], info->pos_temp[1], info->pos_temp[2] );
 }
 
 static void poll_connections(void){