now fall in immobile mode
[carveJwlIkooP6JGAAIwe30JlM.git] / network.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 * All trademarks are property of their respective owners
4 */
5
6 #ifndef NETWORK_H
7 #define NETWORK_H
8
9 #include "vg/vg_stdint.h"
10 #include "steam.h"
11 #include "network_msg.h"
12 #include "highscores.h"
13
14 VG_STATIC int network_scores_updated = 0;
15
16 /*
17 * Interface
18 */
19 //#define SR_USE_LOCALHOST
20
21 /* Call it at start; Connects us to the gameserver */
22 VG_STATIC void network_init(void);
23
24 /* Run this from main loop */
25 VG_STATIC void network_update(void);
26
27 /* Call it at shutdown */
28 VG_STATIC void network_end(void);
29
30 /*
31 * Can buffer up a bunch of these by calling many times, they will be
32 * sent at the next connection
33 */
34 VG_STATIC void network_submit_highscore( u32 trackid, u16 points, u16 time );
35
36 /*
37 * Game endpoints are provided with the same names to allow running without a
38 * network connection.
39 */
40 #ifdef SR_NETWORKED
41
42 /*
43 * Runtime connection stuff
44 */
45 VG_STATIC u8 steam_app_ticket[ 1024 ];
46 VG_STATIC u32 steam_app_ticket_length;
47 VG_STATIC int network_name_update = 1;
48
49 VG_STATIC HSteamNetConnection cremote;
50 VG_STATIC ESteamNetworkingConnectionState cremote_state =
51 k_ESteamNetworkingConnectionState_None;
52
53 /*
54 * Implementation
55 */
56
57 VG_STATIC void scores_update(void);
58
59 VG_STATIC void on_auth_ticket_recieved( void *result, void *context )
60 {
61 EncryptedAppTicketResponse_t *response = result;
62
63 if( response->m_eResult == k_EResultOK )
64 {
65 vg_info( " New app ticket ready\n" );
66 }
67 else
68 {
69 vg_warn( " Could not request new encrypted app ticket (%u)\n",
70 response->m_eResult );
71 }
72
73 if( SteamAPI_ISteamUser_GetEncryptedAppTicket( hSteamUser,
74 steam_app_ticket,
75 vg_list_size(steam_app_ticket),
76 &steam_app_ticket_length ))
77 {
78 vg_success( " Loaded app ticket (%u bytes)\n", steam_app_ticket_length );
79 }
80 else
81 {
82 vg_error( " No ticket availible\n" );
83 steam_app_ticket_length = 0;
84 }
85 }
86
87 VG_STATIC void request_auth_ticket(void)
88 {
89 /*
90 * TODO Check for one thats cached on the disk and load it.
91 * This might be OK though because steam seems to cache the result
92 */
93
94 vg_info( "Requesting new authorization ticket\n" );
95
96 vg_steam_async_call *call = vg_alloc_async_steam_api_call();
97 call->userdata = NULL;
98 call->p_handler = on_auth_ticket_recieved;
99 call->id =
100 SteamAPI_ISteamUser_RequestEncryptedAppTicket( hSteamUser, NULL, 0 );
101 }
102
103 VG_STATIC void send_auth_ticket(void)
104 {
105 u32 size = sizeof(netmsg_auth) + steam_app_ticket_length;
106 netmsg_auth *auth = alloca(size);
107
108 auth->inetmsg_id = k_inetmsg_auth;
109 auth->ticket_length = steam_app_ticket_length;
110 for( int i=0; i<steam_app_ticket_length; i++ )
111 auth->ticket[i] = steam_app_ticket[i];
112
113 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
114 hSteamNetworkingSockets, cremote, auth, size,
115 k_nSteamNetworkingSend_Reliable, NULL );
116 }
117
118 VG_STATIC void send_score_request(void)
119 {
120 vg_info( "Requesting scores\n" );
121 netmsg_scores_request req;
122 req.inetmsg_id = k_inetmsg_scores_request;
123
124 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
125 hSteamNetworkingSockets, cremote, &req, sizeof(netmsg_scores_request),
126 k_nSteamNetworkingSend_Reliable, NULL );
127 }
128
129 VG_STATIC void send_score_update(void)
130 {
131 vg_info( "Sending scores\n" );
132 u32 size = sizeof(netmsg_set_score) +
133 vg_list_size(track_infos)*sizeof(struct netmsg_score_record);
134 netmsg_set_score *setscore = alloca( size );
135 setscore->inetmsg_id = k_inetmsg_set_score;
136
137 int count = 0;
138 for( u32 i=0; i<vg_list_size(track_infos); i++ ){
139 if( track_infos[i].push ){
140 track_infos[i].push = 0;
141
142 #if 0
143 highscore_record *user_record = highscore_find_user_record( 0, i );
144
145 if( !user_record ){
146 vg_error( "No score set but tried to upload for track %u\n", i );
147 continue;
148 }
149 #endif
150 highscore_record *user_record = &track_infos[i].record;
151
152 setscore->records[count].trackid = i;
153 setscore->records[count].playerid = 0;
154 setscore->records[count].points = user_record->points;
155 setscore->records[count].time = user_record->time;
156
157 count ++;
158 }
159 }
160
161 if( count == 0 ) return;
162 u32 send_size = sizeof(netmsg_set_score) +
163 count*sizeof(struct netmsg_score_record);
164 setscore->record_count = count;
165
166 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
167 hSteamNetworkingSockets, cremote, setscore, send_size,
168 k_nSteamNetworkingSend_Reliable, NULL );
169 }
170
171 VG_STATIC void send_nickname(void)
172 {
173 netmsg_set_nickname nick;
174 nick.inetmsg_id = k_inetmsg_set_nickname;
175
176 memset( nick.nickname, 0, 16 );
177 vg_strncpy( steam_username_at_startup, nick.nickname, 16,
178 k_strncpy_allow_cutoff );
179
180 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
181 hSteamNetworkingSockets, cremote, &nick, sizeof(netmsg_set_nickname),
182 k_nSteamNetworkingSend_Reliable, NULL );
183
184 network_name_update = 0;
185 }
186
187 VG_STATIC void server_routine_update(void)
188 {
189 send_auth_ticket();
190
191 if( network_name_update )
192 send_nickname();
193
194 send_score_update();
195 send_score_request();
196 }
197
198 VG_STATIC void on_server_connect_status( CallbackMsg_t *msg )
199 {
200 SteamNetConnectionStatusChangedCallback_t *info = (void *)msg->m_pubParam;
201 vg_info( " Connection status changed for %lu\n", info->m_hConn );
202 vg_info( " %s -> %s\n",
203 string_ESteamNetworkingConnectionState(info->m_eOldState),
204 string_ESteamNetworkingConnectionState(info->m_info.m_eState) );
205
206 if( info->m_hConn == cremote )
207 {
208 cremote_state = info->m_info.m_eState;
209 if( info->m_info.m_eState ==
210 k_ESteamNetworkingConnectionState_Connected )
211 {
212 vg_success(" Connected to remote server.. running updates\n");
213 server_routine_update();
214 }
215 }
216 else
217 {
218 vg_warn( " Recieved signal from unknown connection\n" );
219 }
220 }
221
222 VG_STATIC void network_connect_gc(void)
223 {
224 /* Connect to server if not connected */
225 SteamNetworkingIPAddr remoteAddr;
226
227 #ifdef SR_USE_LOCALHOST
228 SteamAPI_SteamNetworkingIPAddr_SetIPv6LocalHost( &remoteAddr, 27402 );
229 #else
230 const char *server_lon1 = "46.101.34.155:27402";
231 SteamAPI_SteamNetworkingIPAddr_ParseString( &remoteAddr, server_lon1 );
232 #endif
233
234 char buf[256];
235 SteamAPI_SteamNetworkingIPAddr_ToString( &remoteAddr, buf, 256, 1 );
236 vg_info( "connect to: %s\n", buf );
237
238 cremote = SteamAPI_ISteamNetworkingSockets_ConnectByIPAddress(
239 hSteamNetworkingSockets, &remoteAddr, 0, NULL );
240 }
241
242 VG_STATIC void on_inet_scoreboard( SteamNetworkingMessage_t *msg )
243 {
244 netmsg_scoreboard *sb = msg->m_pData;
245
246 u32 base_size = sizeof(netmsg_scoreboard)-
247 sizeof(struct netmsg_board)*vg_list_size(track_infos),
248 expected = base_size+sizeof(struct netmsg_board)*sb->board_count;
249
250 if( msg->m_cbSize != expected ){
251 vg_error( "Server scoreboard was corrupted. Size: %u != %u\n",
252 msg->m_cbSize, expected );
253 }
254 else{
255 if( vg_list_size(track_infos) > sb->board_count )
256 vg_warn( "Server is out of date, not enough boards recieved\n");
257 else if( vg_list_size(track_infos) < sb->board_count )
258 vg_warn( "Client out of date, server sent more boards than we have\n");
259 else
260 vg_success( "Recieved new scoreboards from server\n" );
261
262 for( int i=0; i < vg_min(sb->board_count,vg_list_size(track_infos)); i++){
263 scoreboard_client_data.boards[i] = sb->boards[i];
264 highscores_board_printf( stdout, sb->boards[i].data, 10 );
265 }
266 }
267
268 /* We dont need to stay on the server currently */
269 SteamAPI_ISteamNetworkingSockets_CloseConnection(
270 hSteamNetworkingSockets, cremote, 0, NULL, 1 );
271
272 network_scores_updated = 1;
273 }
274
275 VG_STATIC void poll_connection(void)
276 {
277 SteamNetworkingMessage_t *messages[32];
278 int len;
279
280 while(1){
281 len = SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnConnection(
282 hSteamNetworkingSockets, cremote, messages, vg_list_size(messages));
283
284 if( len <= 0 )
285 return;
286
287 for( int i=0; i<len; i++ ){
288 SteamNetworkingMessage_t *msg = messages[i];
289
290 if( msg->m_cbSize < sizeof(netmsg_blank) ){
291 vg_warn( "Discarding message (too small: %d)\n", msg->m_cbSize );
292 continue;
293 }
294
295 netmsg_blank *tmp = msg->m_pData;
296
297 if( tmp->inetmsg_id == k_inetmsg_scoreboard )
298 on_inet_scoreboard( msg );
299
300 SteamAPI_SteamNetworkingMessage_t_Release( msg );
301 }
302 }
303 }
304
305 /*
306 * Subroutine to be connected to main game loop, runs all routines on timers
307 */
308 VG_STATIC void network_update(void)
309 {
310 if( steam_ready ){
311 static double last_update = 0.0;
312 poll_connection();
313
314 if( vg.time > (last_update + 60.0) ){
315 last_update = vg.time;
316
317 if( steam_app_ticket_length ){
318 network_connect_gc();
319 }
320 else{
321 vg_low( "Not making remote connection; app ticket not gotten\n" );
322 }
323 }
324
325 if( vg.time > (last_update + 10.0) &&
326 (cremote_state == k_ESteamNetworkingConnectionState_Connected ))
327 {
328 vg_warn( "Connected to server but no return... disconnecting\n" );
329 SteamAPI_ISteamNetworkingSockets_CloseConnection(
330 hSteamNetworkingSockets, cremote, 0, NULL, 1 );
331 }
332 }
333 }
334
335 VG_STATIC void network_init(void)
336 {
337 if( steam_ready ){
338 steam_register_callback( k_iSteamNetConnectionStatusChangedCallBack,
339 on_server_connect_status );
340 request_auth_ticket();
341 }
342 }
343
344 VG_STATIC void network_end(void)
345 {
346 /* TODO: Fire off any buffered highscores that need to be setn */
347 if( cremote_state == k_ESteamNetworkingConnectionState_Connected ||
348 cremote_state == k_ESteamNetworkingConnectionState_Connecting )
349 {
350 SteamAPI_ISteamNetworkingSockets_CloseConnection(
351 hSteamNetworkingSockets, cremote, 0, NULL, 1 );
352 }
353 }
354
355 #else /* SR_NETWORKED */
356
357 VG_STATIC void network_init(void){}
358 VG_STATIC void network_update(void){}
359 VG_STATIC void network_end(void){}
360
361 #endif /* SR_NETWORKED */
362 #endif /* NETWORK_H */