1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
3 #define _DEFAULT_SOURCE
9 #include "vg/vg_steam.h"
10 #include "vg/vg_steam_networking.h"
11 #include "vg/vg_steam_auth.h"
12 #include "/home/harry/Documents/carve/network_msg.h"
14 volatile sig_atomic_t sig_stop
;
16 void inthandler( int signum
)
22 * Runtime connection stuff
25 static u8 steam_app_ticket
[ 1024 ];
26 static u32 steam_app_ticket_length
;
28 static HSteamNetConnection cremote
;
29 static ESteamNetworkingConnectionState cremote_state
=
30 k_ESteamNetworkingConnectionState_None
;
35 void *hSteamNetworkingSockets
,
38 static void scores_update(void);
40 static void handle_steam_callback( CallbackMsg_t
*msg
)
42 if( msg
->m_iCallback
== k_iSteamNetConnectionStatusChangedCallBack
)
44 SteamNetConnectionStatusChangedCallback_t
*info
= (void *)msg
->m_pubParam
;
45 vg_info( " Connection status changed for %lu\n", info
->m_hConn
);
47 vg_info( " %s -> %s\n",
48 string_ESteamNetworkingConnectionState(info
->m_info
.m_eState
) );
49 string_ESteamNetworkingConnectionState(info
->m_eOldState
) );
51 if( info
->m_hConn
== cremote
)
53 cremote_state
= info
->m_info
.m_eState
;
54 if( info
->m_info
.m_eState
==
55 k_ESteamNetworkingConnectionState_Connected
)
57 vg_success(" Connected to remote server\n");
63 vg_warn( " Recieved signal from unkown connection\n" );
68 static void on_auth_ticket_recieved( void *result
, void *context
)
70 EncryptedAppTicketResponse_t
*response
= result
;
72 if( response
->m_eResult
== k_EResultOK
)
74 vg_info( " New app ticket ready\n" );
78 vg_warn( " Could not request new encrypted app ticket (%u)\n",
79 response
->m_eResult
);
82 if( SteamAPI_ISteamUser_GetEncryptedAppTicket( hSteamUser
,
84 vg_list_size(steam_app_ticket
),
85 &steam_app_ticket_length
))
87 vg_success( " Loaded app ticket (%u bytes)\n", steam_app_ticket_length
);
91 vg_error( " No ticket availible\n" );
92 steam_app_ticket_length
= 0;
96 static void request_auth_ticket(void)
99 * TODO Check for one thats cached on the disk and load it.
100 * This might be OK though because steam seems to cache the result
103 vg_info( "Requesting new authorization ticket\n" );
104 steam_async
*call
= steam_new_async();
106 call
->p_handler
= on_auth_ticket_recieved
;
107 call
->id
= SteamAPI_ISteamUser_RequestEncryptedAppTicket( hSteamUser
,
111 static void server_connect(void)
113 /* Connect to server if not connected */
115 SteamNetworkingIPAddr remoteAddr
;
117 #define USE_LOCALHOST
119 SteamAPI_SteamNetworkingIPAddr_SetIPv6LocalHost( &remoteAddr
, 27402 );
121 const char *server_lon1
= "46.101.34.155:27402";
122 SteamAPI_SteamNetworkingIPAddr_ParseString( &remoteAddr
, server_lon1
);
126 SteamAPI_SteamNetworkingIPAddr_ToString( &remoteAddr
, buf
, 256, 1 );
127 vg_info( "connect to: %s\n", buf
);
129 cremote
= SteamAPI_ISteamNetworkingSockets_ConnectByIPAddress(
130 hSteamNetworkingSockets
, &remoteAddr
, 0, NULL
);
133 static void scores_update(void)
135 vg_log( "scores_update()\n" );
137 if( cremote_state
== k_ESteamNetworkingConnectionState_Connected
)
140 * request updated scores
142 netmsg_scores_request req
;
143 req
.inetmsg_id
= k_inetmsg_scores_request
;
145 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
146 hSteamNetworkingSockets
, cremote
, &req
,
147 sizeof(netmsg_scores_request
),
148 k_nSteamNetworkingSend_Reliable
, NULL
);
153 * if we are not connected, make a connection to the server and then in
154 * the future this function will be called again when it is connected
160 static void poll_connection(void)
162 SteamNetworkingMessage_t
*messages
[32];
167 len
= SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnConnection(
168 hSteamNetworkingSockets
, cremote
, messages
, vg_list_size(messages
));
173 for( int i
=0; i
<len
; i
++ )
175 SteamNetworkingMessage_t
*msg
= messages
[i
];
177 if( msg
->m_cbSize
< sizeof(netmsg_blank
) )
179 vg_warn( "Discarding message (too small: %d)\n",
184 netmsg_blank
*tmp
= msg
->m_pData
;
185 if( tmp
->inetmsg_id
== k_inetmsg_scores_info
)
187 netmsg_scores_info
*info
= msg
->m_pData
;
188 vg_log( "Recieved %u score records\n", info
->record_count
);
190 SteamAPI_ISteamNetworkingSockets_CloseConnection(
191 hSteamNetworkingSockets
, cremote
, 0, NULL
, 1 );
192 cremote_state
= k_ESteamNetworkingConnectionState_None
;
195 SteamAPI_SteamNetworkingMessage_t_Release( msg
);
200 static u64
in_server_ticks( double seconds
)
202 return (u64
)(seconds
/ 0.1);
205 int main( int argc
, char *argv
[] )
207 signal( SIGINT
, inthandler
);
209 if( !SteamAPI_Init() )
211 vg_error( "Steamworks failed to initialize\n" );
215 SteamAPI_ManualDispatch_Init();
216 HSteamPipe hsteampipe
= SteamAPI_GetHSteamPipe();
217 vg_success( "Steamworks API running\n" );
219 /* Connect interfaces */
220 hSteamNetworkingSockets
= SteamAPI_SteamNetworkingSockets_SteamAPI();
221 hSteamUser
= SteamAPI_SteamUser();
223 request_auth_ticket();
225 /* TODO: Request current scores */
231 u64 server_ticks
= 8000, last_update
= 0;
235 steamworks_event_loop( hsteampipe
, handle_steam_callback
);
241 if( server_ticks
> (last_update
+ in_server_ticks(60.0)) )
243 last_update
= server_ticks
;
248 vg_info( "Shutting down\n..." );