From 46f4e9ee87dc67402166e4c6b05efbe922cd7574 Mon Sep 17 00:00:00 2001 From: hgn Date: Fri, 29 Sep 2023 17:26:02 +0100 Subject: [PATCH] test network 1 --- gameserver.c | 99 +++++++++++++++++++++++++++++++++++++++++++++---- gameserver.h | 7 ++++ network.c | 34 ++++++++++++++++- network.h | 2 + network_msg.h | 26 ++++++++++++- player.h | 6 ++- player_dead.h | 3 +- player_drive.h | 3 +- player_remote.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++ player_remote.h | 28 ++++++++++++++ player_skate.h | 3 +- player_walk.h | 3 +- skaterift.c | 3 ++ 13 files changed, 296 insertions(+), 15 deletions(-) create mode 100644 player_remote.c create mode 100644 player_remote.h diff --git a/gameserver.c b/gameserver.c index c63711a..8db3413 100644 --- a/gameserver.c +++ b/gameserver.c @@ -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; iactive ) + 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; iactive ) + 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; im_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; iactive ){ + 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){ diff --git a/gameserver.h b/gameserver.h index 670a9ba..a1b5d6a 100644 --- a/gameserver.h +++ b/gameserver.h @@ -15,6 +15,13 @@ struct { HSteamNetPollGroup client_group; EServerMode auth_mode; + struct gameserver_client { + int active; + int authenticated; + HSteamNetConnection connection; + } + clients[ 32 ]; + u8 app_symmetric_key[ k_nSteamEncryptedAppTicketSymmetricKeyLen ]; int monitor_fd; diff --git a/network.c b/network.c index 63b70c3..777fab4 100644 --- a/network.c +++ b/network.c @@ -1,5 +1,7 @@ #include "player.h" #include "network.h" +#include "network_msg.h" +#include "player_remote.h" static void scores_update(void); @@ -151,13 +153,33 @@ 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(); } + 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" ); } } @@ -205,9 +227,11 @@ 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; } @@ -237,6 +261,10 @@ static void poll_remote_connection(void){ if( tmp->inetmsg_id == k_inetmsg_scoreboard ) on_inet_scoreboard( msg ); + if( (tmp->inetmsg_id >= 200) && (tmp->inetmsg_id < 300) ){ + player_remote_packet( msg ); + } + SteamAPI_SteamNetworkingMessage_t_Release( msg ); } } @@ -280,6 +308,8 @@ static void network_update(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 ); diff --git a/network.h b/network.h index 16594e8..066b9f8 100644 --- a/network.h +++ b/network.h @@ -50,6 +50,8 @@ struct { f64 last_attempt, last_frame; u32 retries; + + i32 network_info; } static network_client = { .state = k_ESteamNetworkingConnectionState_None, diff --git a/network_msg.h b/network_msg.h index 495f4ae..9ad5c87 100644 --- a/network_msg.h +++ b/network_msg.h @@ -8,6 +8,7 @@ #include "vg/vg_stdint.h" #include "world_info.h" #include "vg/vg_platform.h" +; #pragma pack(push,1) @@ -81,13 +82,36 @@ static scoreboard_client_data = { }; /* probably about 10k */ +/* server control 100 */ + + +/* player updates 200 */ + typedef struct netmsg_playerframe netmsg_playerframe; -enum{ k_inetmsg_playerframe = 20 }; +enum{ k_inetmsg_playerframe = 200 }; struct netmsg_playerframe{ u32 inetmsg_id; v3f pos_temp; }; +typedef struct netmsg_playerjoin netmsg_playerjoin; +enum{ k_inetmsg_playerjoin = 201 }; +struct netmsg_playerjoin{ + u32 inetmsg_id; + + u32 index; + char username[32]; /* UNUSED */ + char playermodel_uid[76]; /* UNUSED */ + char board_uid[76]; /* UNUSED */ +}; + +typedef struct netmsg_playerleave netmsg_playerleave; +enum{ k_inetmsg_playerleave = 202 }; +struct netmsg_playerleave{ + u32 inetmsg_id; + u32 index; +}; + #pragma pack(pop) #endif /* NETWORK_MSG_H */ diff --git a/player.h b/player.h index dedae18..36b37a9 100644 --- a/player.h +++ b/player.h @@ -5,10 +5,12 @@ #include "player_common.h" enum player_subsystem{ + k_player_subsystem_invalid = -1, k_player_subsystem_walk = 0, k_player_subsystem_skate = 1, k_player_subsystem_dead = 2, - k_player_subsystem_drive = 3 + k_player_subsystem_drive = 3, + k_player_subsystem_max }; struct player_cam_controller { @@ -44,6 +46,8 @@ struct player_subsystem_interface{ void *animator_data; u32 animator_size; + + const char *name; }; #include "player_ragdoll.h" diff --git a/player_dead.h b/player_dead.h index 03c9326..0c1de41 100644 --- a/player_dead.h +++ b/player_dead.h @@ -35,7 +35,8 @@ struct player_subsystem_interface static player_subsystem_dead = { .im_gui = player__dead_im_gui, .animator_data = &player_dead.animator, - .animator_size = sizeof(player_dead.animator) + .animator_size = sizeof(player_dead.animator), + .name = "Dead" }; #endif /* PLAYER_DEAD_H */ diff --git a/player_drive.h b/player_drive.h index 071fef4..46eedba 100644 --- a/player_drive.h +++ b/player_drive.h @@ -33,7 +33,8 @@ struct player_subsystem_interface static player_subsystem_drive = { .reset = player__drive_reset, .animator_data = NULL, - .animator_size = 0 + .animator_size = 0, + .name = "Drive" }; #endif /* PLAYER_DRIVE_H */ diff --git a/player_remote.c b/player_remote.c new file mode 100644 index 0000000..d9ee7b8 --- /dev/null +++ b/player_remote.c @@ -0,0 +1,94 @@ +#include "player_remote.h" + +static void player_remote_unwatch( struct network_player *player ){ + addon_cache_unwatch( k_addon_type_player, player->playermodel_view_slot ); + addon_cache_unwatch( k_addon_type_board, player->board_view_slot ); +} + +static void player_remote_clear( struct network_player *player ){ + player_remote_unwatch( player ); + memset( player, 0, sizeof(*player) ); + strcpy( player->username, "unknown" ); + player->subsystem = k_player_subsystem_invalid; +} + +static void player_remote_packet( SteamNetworkingMessage_t *msg ){ + netmsg_blank *tmp = msg->m_pData; + + if( tmp->inetmsg_id == k_inetmsg_playerjoin ){ + netmsg_playerjoin *playerjoin = msg->m_pData; + + if( playerjoin->index < vg_list_size(netplayers.list) ){ + struct network_player *player = &netplayers.list[ playerjoin->index ]; + player_remote_clear( player ); + player->active = 1; + + /* TODO: interpret the uids */ + player->board_view_slot = 0; + player->playermodel_view_slot = 0; + } + else { + vg_error( "inetmsg_playerjoin: player index out of range\n" ); + } + } + else if( tmp->inetmsg_id == k_inetmsg_playerleave ){ + netmsg_playerleave *playerleave = msg->m_pData; + + if( playerleave->index < vg_list_size(netplayers.list) ){ + struct network_player *player = &netplayers.list[ playerleave->index ]; + player_remote_unwatch( player ); + player->active = 0; + } + else { + vg_error( "inetmsg_playerleave: player index out of range\n" ); + } + } +} + +static void remote_player_network_imgui(void){ + if( !network_client.network_info ) + return; + + ui_rect panel = { (vg.window_x / 2) - 200, 0, 400, 600 }; + ui_fill( panel, (ui_colour(k_ui_bg)&0x00ffffff)|0x50000000 ); + + char buf[512]; + const char *netstatus = "PROGRAMMING ERROR"; + + struct { enum ESteamNetworkingConnectionState state; const char *str; } + states[] = { + { k_ESteamNetworkingConnectionState_None, "None" }, + { k_ESteamNetworkingConnectionState_Connecting, "Connecting" }, + { k_ESteamNetworkingConnectionState_FindingRoute, "Finding Route" }, + { k_ESteamNetworkingConnectionState_Connected, "Connected" }, + { k_ESteamNetworkingConnectionState_ClosedByPeer, "Closed by peer" }, + { k_ESteamNetworkingConnectionState_ProblemDetectedLocally, + "Problem Detected Locally" }, + { k_ESteamNetworkingConnectionState_FinWait, "Fin Wait" }, + { k_ESteamNetworkingConnectionState_Linger, "Linger" }, + { k_ESteamNetworkingConnectionState_Dead, "Dead" } + }; + for( u32 i=0; iactive ){ + const char *sysname = "invalid"; + + if( (player->subsystem >= 0) && + (player->subsystem < k_player_subsystem_max) ){ + sysname = player_subsystems[ player->subsystem ]->name; + } + snprintf( buf, 512, "#%u: %s [%s]", i, player->username, sysname ); + ui_info( panel, buf ); + } + } +} diff --git a/player_remote.h b/player_remote.h new file mode 100644 index 0000000..bd5f6dd --- /dev/null +++ b/player_remote.h @@ -0,0 +1,28 @@ +#ifndef PLAYER_REMOTE_H +#define PLAYER_REMOTE_H + +#include "player.h" +#include "network.h" + +struct { + struct network_player { + int active; + u16 board_view_slot, playermodel_view_slot; + player_pose pose; + + char username[32]; + + enum player_subsystem subsystem; + union { + struct player_skate_animator _skate; + struct player_walk_animator _walk; + struct player_dead_animator _dead; + }; + } + list[ 32 ]; +} +static netplayers; + +static void player_remote_packet( SteamNetworkingMessage_t *msg ); + +#endif /* PLAYER_REMOTE_H */ diff --git a/player_skate.h b/player_skate.h index 6dbb897..68f2dcb 100644 --- a/player_skate.h +++ b/player_skate.h @@ -305,7 +305,8 @@ struct player_subsystem_interface static player_subsystem_skate = { .post_animate = player__skate_post_animate, .animator_data = &player_skate.animator, - .animator_size = sizeof(player_skate.animator) + .animator_size = sizeof(player_skate.animator), + .name = "Skate" }; #endif /* PLAYER_SKATE_H */ diff --git a/player_walk.h b/player_walk.h index ae0f9c3..14acb70 100644 --- a/player_walk.h +++ b/player_walk.h @@ -112,7 +112,8 @@ struct player_subsystem_interface static player_subsystem_walk = { .pose = player__walk_pose, .animator_data = &player_walk.animator, - .animator_size = sizeof(player_walk.animator) + .animator_size = sizeof(player_walk.animator), + .name = "Walk" }; #endif /* PLAYER_WALK_H */ diff --git a/skaterift.c b/skaterift.c index 14c6410..7f384f6 100644 --- a/skaterift.c +++ b/skaterift.c @@ -39,6 +39,7 @@ #include "vehicle.h" #include "pointcloud.h" #include "save.h" +#include "player_remote.h" /* unity build * ----------------- */ @@ -53,6 +54,7 @@ #include "save.c" #include "respawn.c" #include "network.c" +#include "player_remote.c" static struct player_avatar localplayer_avatar; @@ -634,6 +636,7 @@ static void vg_gui(void){ skaterift_replay_imgui(); workshop_form_gui(); render_view_framebuffer_ui(); + remote_player_network_imgui(); } -- 2.25.1