From e364c19156187c346c19295c0480062e57eb6d7c Mon Sep 17 00:00:00 2001 From: hgn Date: Tue, 9 Aug 2022 12:59:36 +0100 Subject: [PATCH] example client code --- example_client.c | 252 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100644 example_client.c diff --git a/example_client.c b/example_client.c new file mode 100644 index 0000000..d6a08a3 --- /dev/null +++ b/example_client.c @@ -0,0 +1,252 @@ +// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved + +#define _DEFAULT_SOURCE +#include +#include + +#define VG_SERVER +#include "vg/vg.h" +#include "vg/vg_steam.h" +#include "vg/vg_steam_networking.h" +#include "vg/vg_steam_auth.h" +#include "/home/harry/Documents/carve/network_msg.h" + +volatile sig_atomic_t sig_stop; + +void inthandler( int signum ) +{ + sig_stop = 1; +} + +/* + * 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; + +/* + * Steam API + */ +void *hSteamNetworkingSockets, + *hSteamUser; + +static void scores_update(void); + +static void handle_steam_callback( CallbackMsg_t *msg ) +{ + if( msg->m_iCallback == k_iSteamNetConnectionStatusChangedCallBack ) + { + 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 unkown connection\n" ); + } + } +} + +static void on_auth_ticket_recieved( void *result, void *context ) +{ + EncryptedAppTicketResponse_t *response = result; + + 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; + } +} + +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 + + 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 scores_update(void) +{ + vg_log( "scores_update()\n" ); + + if( cremote_state == k_ESteamNetworkingConnectionState_Connected ) + { + /* + * request updated scores + */ + 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 ); + } + 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; im_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); +} + +int main( int argc, char *argv[] ) +{ + signal( SIGINT, inthandler ); + + if( !SteamAPI_Init() ) + { + vg_error( "Steamworks failed to initialize\n" ); + return 0; + } + + SteamAPI_ManualDispatch_Init(); + HSteamPipe hsteampipe = SteamAPI_GetHSteamPipe(); + vg_success( "Steamworks API running\n" ); + + /* Connect interfaces */ + hSteamNetworkingSockets = SteamAPI_SteamNetworkingSockets_SteamAPI(); + hSteamUser = SteamAPI_SteamUser(); + + request_auth_ticket(); + + /* TODO: Request current scores */ + +#if 0 + +#endif + + u64 server_ticks = 8000, last_update = 0; + + while( !sig_stop ) + { + steamworks_event_loop( hsteampipe, handle_steam_callback ); + poll_connection(); + + usleep(100000); + server_ticks ++; + + if( server_ticks > (last_update + in_server_ticks(60.0)) ) + { + last_update = server_ticks; + scores_update(); + } + } + + vg_info( "Shutting down\n..." ); + SteamAPI_Shutdown(); + + return 0; +} -- 2.25.1