2 #include "vg/vg_steam.h"
3 #include "vg/vg_steam_networking.h"
4 #include "vg/vg_steam_auth.h"
5 #include "vg/vg_steam_friends.h"
8 #include "network_msg.h"
9 #include "network_common.h"
10 #include "player_remote.h"
12 #include "world_sfd.h"
13 #include "world_routes.h"
14 #include "vg/vg_imgui.h"
16 #include "ent_region.h"
18 struct network_client network_client
=
20 .auth_mode
= eServerModeAuthentication
,
21 .state
= k_ESteamNetworkingConnectionState_None
,
22 .server_adress
= "46.101.34.155",
23 .last_intent_change
= -99999.9
26 static void scores_update(void);
28 int packet_minsize( SteamNetworkingMessage_t
*msg
, u32 size
){
29 if( msg
->m_cbSize
< size
) {
30 vg_error( "Invalid packet size (must be at least %u)\n", size
);
38 static void on_auth_ticket_recieved( void *result
, void *context
){
39 EncryptedAppTicketResponse_t
*response
= result
;
41 if( response
->m_eResult
== k_EResultOK
){
42 vg_info( " New app ticket ready\n" );
45 vg_warn( " Could not request new encrypted app ticket (%u)\n",
46 response
->m_eResult
);
49 if( SteamAPI_ISteamUser_GetEncryptedAppTicket( hSteamUser
,
50 network_client
.app_symmetric_key
,
51 vg_list_size(network_client
.app_symmetric_key
),
52 &network_client
.app_key_length
)){
53 vg_success( " Loaded app ticket\n" );
56 vg_error( " No ticket availible\n" );
57 network_client
.app_key_length
= 0;
61 static void request_auth_ticket(void){
63 * TODO Check for one thats cached on the disk and load it.
64 * This might be OK though because steam seems to cache the result
67 vg_info( "Requesting new authorization ticket\n" );
69 vg_steam_async_call
*call
= vg_alloc_async_steam_api_call();
70 call
->userdata
= NULL
;
71 call
->p_handler
= on_auth_ticket_recieved
;
73 SteamAPI_ISteamUser_RequestEncryptedAppTicket( hSteamUser
, NULL
, 0 );
76 static void network_send_username(void){
77 if( !network_connected() )
80 netmsg_playerusername
*update
= alloca( sizeof(netmsg_playerusername
)+
81 NETWORK_USERNAME_MAX
);
82 update
->inetmsg_id
= k_inetmsg_playerusername
;
85 ISteamFriends
*hSteamFriends
= SteamAPI_SteamFriends();
86 const char *username
= SteamAPI_ISteamFriends_GetPersonaName(hSteamFriends
);
87 u32 chs
= str_utf8_collapse( username
, update
->name
, NETWORK_USERNAME_MAX
);
89 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
90 hSteamNetworkingSockets
, network_client
.remote
,
91 update
, sizeof(netmsg_playerusername
)+chs
+1,
92 k_nSteamNetworkingSend_Reliable
, NULL
);
95 void network_send_region(void)
97 if( !network_connected() )
100 netmsg_region
*region
= alloca( sizeof(netmsg_region
) + NETWORK_REGION_MAX
);
102 region
->inetmsg_id
= k_inetmsg_region
;
104 region
->flags
= global_ent_region
.flags
;
106 u32 l
= vg_strncpy( global_ent_region
.location
, region
->loc
,
107 NETWORK_REGION_MAX
, k_strncpy_always_add_null
);
109 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
110 hSteamNetworkingSockets
, network_client
.remote
,
111 region
, sizeof(netmsg_region
)+l
+1,
112 k_nSteamNetworkingSend_Reliable
, NULL
);
115 static void network_send_request( netmsg_request
*req
, vg_msg
*body
,
117 netmsg_request
*res
, vg_msg
*body
,
123 vg_info( "Request scoreboard. Info (%u):\n", body
->cur
.co
);
124 vg_msg_print( body
, len
);
126 if( body
->error
!= k_vg_msg_error_OK
){
127 vg_error( "Body not OK\n" );
133 req
->id
= vg_pool_lru( &network_client
.request_pool
);
135 vg_pool_watch( &network_client
.request_pool
, req
->id
);
136 struct network_request
*pn
=
137 vg_pool_item( &network_client
.request_pool
, req
->id
);
138 pn
->callback
= callback
;
139 pn
->sendtime
= vg
.time_real
;
140 pn
->userdata
= userdata
;
143 vg_error( "Unable to send request. Pool is full.\n" );
150 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
151 hSteamNetworkingSockets
, network_client
.remote
,
152 req
, sizeof(netmsg_request
)+len
,
153 k_nSteamNetworkingSend_Reliable
, NULL
);
156 static void network_scoreboard_callback( netmsg_request
*res
, vg_msg
*body
,
158 world_instance
*world
= world_current_instance();
160 world_routes_recv_scoreboard( world
, body
, userdata
, res
->status
);
161 if( userdata
== world_sfd
.active_route_board
)
162 world_sfd_compile_active_scores();
167 /* mod_uid: world mod uid,
168 * route_uid: run name (just a string)
172 * 2 ALL TIME + CURRENT WEEK
174 * 10+ specific week index
176 void network_request_scoreboard( const char *mod_uid
,
177 const char *route_uid
,
178 u32 week
, u64 userdata
){
179 if( !network_connected() )
182 netmsg_request
*req
= alloca( sizeof(netmsg_request
) + 512 );
183 req
->inetmsg_id
= k_inetmsg_request
;
186 vg_msg_init( &data
, req
->q
, 512 );
187 vg_msg_wkvstr( &data
, "endpoint", "scoreboard" );
188 vg_msg_wkvstr( &data
, "mod", mod_uid
);
189 vg_msg_wkvstr( &data
, "route", route_uid
);
190 vg_msg_wkvnum( &data
, "week", k_vg_msg_u32
, 1, &week
);
191 network_send_request( req
, &data
, network_scoreboard_callback
, userdata
);
194 static void network_publish_callback( netmsg_request
*res
, vg_msg
*body
,
196 if( res
->status
!= k_request_status_ok
){
197 vg_error( "Publish laptime, server error #%d\n", (i32
)res
->status
);
201 void network_publish_laptime( const char *mod_uid
,
202 const char *route_uid
, f64 lap_time
){
203 if( !network_connected() )
206 i32 time_centiseconds
= lap_time
* 100.0;
208 netmsg_request
*req
= alloca( sizeof(netmsg_request
) + 512 );
209 req
->inetmsg_id
= k_inetmsg_request
;
212 vg_msg_init( &data
, req
->q
, 512 );
213 vg_msg_wkvstr( &data
, "endpoint", "setlap" );
214 vg_msg_wkvstr( &data
, "mod", mod_uid
);
215 vg_msg_wkvstr( &data
, "route", route_uid
);
216 vg_msg_wkvnum( &data
, "time", k_vg_msg_i32
, 1, &time_centiseconds
);
217 network_send_request( req
, &data
, network_publish_callback
, 0 );
220 static void network_request_rx_300_400( SteamNetworkingMessage_t
*msg
){
221 netmsg_blank
*tmp
= msg
->m_pData
;
223 if( tmp
->inetmsg_id
== k_inetmsg_request
){
226 else if( tmp
->inetmsg_id
== k_inetmsg_response
){
227 netmsg_request
*res
= (netmsg_request
*)msg
->m_pData
;
232 if( res
->status
== k_request_status_ok
){
233 vg_msg_init( &data
, res
->q
, msg
->m_cbSize
- sizeof(netmsg_request
) );
234 vg_success( "Response to #%d:\n", (i32
)res
->id
);
235 vg_msg_print( &data
, data
.max
);
239 vg_warn( "Server response to #%d: %d\n", (i32
)res
->id
, res
->status
);
243 struct network_request
*pn
=
244 vg_pool_item( &network_client
.request_pool
, res
->id
);
245 pn
->callback( res
, body
, pn
->userdata
);
246 vg_pool_unwatch( &network_client
.request_pool
, res
->id
);
251 void network_send_item( enum netmsg_playeritem_type type
)
253 if( !network_connected() )
256 netmsg_playeritem
*item
=
257 alloca( sizeof(netmsg_playeritem
) + ADDON_UID_MAX
);
258 item
->inetmsg_id
= k_inetmsg_playeritem
;
259 item
->type_index
= type
;
262 if( (type
== k_netmsg_playeritem_world0
) ||
263 (type
== k_netmsg_playeritem_world1
) ){
265 enum world_purpose purpose
= type
- k_netmsg_playeritem_world0
;
266 addon_reg
*reg
= world_static
.instance_addons
[ purpose
];
269 addon_alias_uid( ®
->alias
, item
->uid
);
275 enum addon_type addon_type
= k_addon_type_none
;
276 if( type
== k_netmsg_playeritem_board
){
277 view_id
= localplayer
.board_view_slot
;
278 addon_type
= k_addon_type_board
;
280 else if( type
== k_netmsg_playeritem_player
){
281 view_id
= localplayer
.playermodel_view_slot
;
282 addon_type
= k_addon_type_player
;
285 struct addon_cache
*cache
= &addon_system
.cache
[addon_type
];
286 vg_pool
*pool
= &cache
->pool
;
288 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
289 addon_cache_entry
*entry
= vg_pool_item( pool
, view_id
);
290 addon_alias_uid( &entry
->reg_ptr
->alias
, item
->uid
);
291 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
294 vg_info( "send equip: [%u] %s\n",
295 item
->type_index
, item
->uid
);
296 u32 chs
= strlen(item
->uid
);
298 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
299 hSteamNetworkingSockets
, network_client
.remote
,
300 item
, sizeof(netmsg_playeritem
)+chs
+1,
301 k_nSteamNetworkingSend_Reliable
, NULL
);
304 static void network_disconnect(void){
305 SteamAPI_ISteamNetworkingSockets_CloseConnection(
306 hSteamNetworkingSockets
, network_client
.remote
, 0, NULL
, 0 );
307 network_client
.remote
= 0;
308 network_client
.state
= k_ESteamNetworkingConnectionState_None
;
310 for( int i
=0; i
<vg_list_size(netplayers
.list
); i
++ ){
311 netplayers
.list
[i
].active
= 0;
315 void network_status_string( vg_str
*str
, u32
*colour
)
317 if( skaterift
.demo_mode
){
318 vg_strcat( str
, "Offline" );
323 if( network_client
.user_intent
== k_server_intent_offline
){
324 vg_strcat( str
, "Offline" );
327 ESteamNetworkingConnectionState state
= network_client
.state
;
329 if( state
== k_ESteamNetworkingConnectionState_None
)
330 vg_strcat( str
, "No Connection" );
331 else if( state
== k_ESteamNetworkingConnectionState_Connecting
){
332 vg_strcat( str
, "Connecting to:\nskaterift.com" );
334 if( network_client
.retries
){
335 vg_strcat( str
, "\n(" );
336 vg_strcati32( str
, network_client
.retries
);
337 vg_strcat( str
, " retries)" );
340 else if( state
== k_ESteamNetworkingConnectionState_Connected
){
341 vg_strcat( str
, "Connected to:\nskaterift.com" );
342 *colour
= 0xff00a020;
344 else if( state
== k_ESteamNetworkingConnectionState_ClosedByPeer
)
345 vg_strcat( str
, "Connection Closed" );
346 else if( state
== k_ESteamNetworkingConnectionState_FindingRoute
)
347 vg_strcat( str
, "Finding Route" );
349 k_ESteamNetworkingConnectionState_ProblemDetectedLocally
){
350 vg_strcat( str
, "Problem Detected\nLocally" );
351 *colour
= 0xff0000a0;
354 vg_strcat( str
, "???" );
358 vg_strcat( str
, "Steam Offline" );
359 *colour
= 0xff0000a0;
363 void render_server_status_gui(void)
365 render_fb_bind( gpipeline
.fb_workshop_preview
, 0 );
369 vg_ui
.cur_indice
= 0;
370 vg_ui
.vert_start
= 0;
371 vg_ui
.indice_start
= 0;
373 ui_rect r
= { 0, 0, 128, 48 };
377 vg_strnull( &str
, buf
, sizeof(buf
) );
380 network_status_string( &str
, &bg
);
383 ui_text( r
, buf
, 1, k_ui_align_center
, 0 );
384 ui_flush( k_ui_shader_colour
, 128, 48 );
386 skaterift
.rt_textures
[ k_skaterift_rt_server_status
] =
387 gpipeline
.fb_workshop_preview
->attachments
[0].id
;
390 static void on_server_connect_status( CallbackMsg_t
*msg
){
391 SteamNetConnectionStatusChangedCallback_t
*info
= (void *)msg
->m_pubParam
;
392 vg_info( " Connection status changed for %lu\n", info
->m_hConn
);
393 vg_info( " %s -> %s\n",
394 string_ESteamNetworkingConnectionState(info
->m_eOldState
),
395 string_ESteamNetworkingConnectionState(info
->m_info
.m_eState
) );
397 if( info
->m_hConn
== network_client
.remote
){
398 network_client
.state
= info
->m_info
.m_eState
;
400 if( info
->m_info
.m_eState
==
401 k_ESteamNetworkingConnectionState_Connected
){
402 vg_success(" Connected to remote server.. authenticating\n");
404 /* send version info to server */
405 netmsg_version version
;
406 version
.inetmsg_id
= k_inetmsg_version
;
407 version
.version
= NETWORK_SKATERIFT_VERSION
;
408 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
409 hSteamNetworkingSockets
, network_client
.remote
, &version
,
410 sizeof(netmsg_version
), k_nSteamNetworkingSend_Reliable
, NULL
);
412 /* TODO: We should really wait to see if the server is in auth mode
414 u32 size
= sizeof(netmsg_auth
) + network_client
.app_key_length
;
415 netmsg_auth
*auth
= alloca(size
);
416 auth
->inetmsg_id
= k_inetmsg_auth
;
417 auth
->ticket_length
= network_client
.app_key_length
;
418 for( int i
=0; i
<network_client
.app_key_length
; i
++ )
419 auth
->ticket
[i
] = network_client
.app_symmetric_key
[i
];
421 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
422 hSteamNetworkingSockets
, network_client
.remote
, auth
, size
,
423 k_nSteamNetworkingSend_Reliable
, NULL
);
425 else if( info
->m_info
.m_eState
==
426 k_ESteamNetworkingConnectionState_ClosedByPeer
){
428 if( info
->m_info
.m_eEndReason
==
429 k_ESteamNetConnectionEnd_Misc_InternalError
){
430 network_client
.retries
= 40;
432 network_disconnect();
434 else if( info
->m_info
.m_eState
==
435 k_ESteamNetworkingConnectionState_ProblemDetectedLocally
){
436 network_disconnect();
440 //vg_warn( " Recieved signal from unknown connection\n" );
443 render_server_status_gui();
446 static void on_persona_state_change( CallbackMsg_t
*msg
){
447 if( !network_connected() )
450 PersonaStateChange_t
*info
= (void *)msg
->m_pubParam
;
451 ISteamUser
*hSteamUser
= SteamAPI_SteamUser();
453 vg_info( "User: %llu, change: %u\n", info
->m_ulSteamID
,
454 info
->m_nChangeFlags
);
456 if( info
->m_ulSteamID
== SteamAPI_ISteamUser_GetSteamID(hSteamUser
) ){
457 if( info
->m_nChangeFlags
& k_EPersonaChangeName
){
458 network_send_username();
462 if( info
->m_nChangeFlags
& k_EPersonaChangeRelationshipChanged
){
463 for( u32 i
=0; i
<NETWORK_MAX_PLAYERS
; i
++ ){
464 struct network_player
*rp
= &netplayers
.list
[i
];
465 if( rp
->steamid
== info
->m_ulSteamID
){
466 player_remote_update_friendflags( rp
);
472 static void network_connect(void){
475 vg_strnull( &str
, ip_buf
, sizeof(ip_buf
) );
476 vg_strcat( &str
, network_client
.server_adress
);
477 vg_strcat( &str
, ":" );
478 vg_strcati32( &str
, NETWORK_PORT
);
480 if( !vg_strgood(&str
) ) return;
482 /* Connect to server if not connected */
483 SteamNetworkingIPAddr remoteAddr
;
484 SteamAPI_SteamNetworkingIPAddr_ParseString( &remoteAddr
, str
.buffer
);
487 SteamAPI_SteamNetworkingIPAddr_ToString( &remoteAddr
, buf
, 256, 1 );
488 vg_info( "connect to: %s\n", buf
);
490 network_client
.remote
= SteamAPI_ISteamNetworkingSockets_ConnectByIPAddress(
491 hSteamNetworkingSockets
, &remoteAddr
, 0, NULL
);
494 static void network_sign_on_complete(void){
495 vg_success( "Sign on completed\n" );
497 /* send our init info */
498 network_send_username();
499 for( u32 i
=0; i
<k_netmsg_playeritem_max
; i
++ ){
500 network_send_item(i
);
502 network_send_region();
505 static void poll_remote_connection(void){
506 SteamNetworkingMessage_t
*messages
[32];
509 for( int i
=0; i
<10; i
++ ){
510 len
= SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnConnection(
511 hSteamNetworkingSockets
, network_client
.remote
,
512 messages
, vg_list_size(messages
));
517 for( int i
=0; i
<len
; i
++ ){
518 SteamNetworkingMessage_t
*msg
= messages
[i
];
520 if( msg
->m_cbSize
< sizeof(netmsg_blank
) ){
521 vg_warn( "Discarding message (too small: %d)\n", msg
->m_cbSize
);
525 netmsg_blank
*tmp
= msg
->m_pData
;
527 if( (tmp
->inetmsg_id
>= 200) && (tmp
->inetmsg_id
< 300) ){
528 player_remote_rx_200_300( msg
);
530 else if( (tmp
->inetmsg_id
>= 300) && (tmp
->inetmsg_id
< 400) ){
531 network_request_rx_300_400( msg
);
534 if( tmp
->inetmsg_id
== k_inetmsg_version
){
535 netmsg_version
*version
= msg
->m_pData
;
536 if( version
->version
!= NETWORK_SKATERIFT_VERSION
){
537 network_disconnect();
538 /* we dont want to connect to this server ever */
539 network_client
.retries
= 999;
540 network_client
.last_attempt
= 999999999.9;
541 vg_error( "version mismatch with server\n" );
544 network_client
.remote_version
= version
->version
;
545 network_sign_on_complete();
550 SteamAPI_SteamNetworkingMessage_t_Release( msg
);
555 void network_update(void)
560 ESteamNetworkingConnectionState state
= network_client
.state
;
562 if( network_client
.user_intent
== k_server_intent_offline
){
563 if( state
!= k_ESteamNetworkingConnectionState_None
)
564 network_disconnect();
569 if( state
== k_ESteamNetworkingConnectionState_Connected
){
570 poll_remote_connection();
571 f64 frame_delta
= vg
.time_real
- network_client
.last_frame
;
573 if( frame_delta
> NETWORK_FRAMERATE
){
574 network_client
.last_frame
= vg
.time_real
;
575 remote_player_send_playerframe();
576 localplayer
.sfx_buffer_count
= 0;
579 remote_player_debug_update();
582 if( (state
== k_ESteamNetworkingConnectionState_Connecting
) ||
583 (state
== k_ESteamNetworkingConnectionState_FindingRoute
) ){
587 f64 waited
= vg
.time_real
- network_client
.last_attempt
,
590 if( network_client
.retries
> 5 )
593 if( waited
< min_wait
)
597 network_client
.retries
++;
598 network_client
.last_attempt
= vg
.time_real
;
603 void chat_send_message( const char *message
)
605 if( !network_connected() ){
609 netmsg_chat
*chat
= alloca( sizeof(netmsg_chat
) + NETWORK_MAX_CHAT
);
610 chat
->inetmsg_id
= k_inetmsg_chat
;
613 u32 l
= vg_strncpy( message
, chat
->msg
, NETWORK_MAX_CHAT
,
614 k_strncpy_always_add_null
);
616 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
617 hSteamNetworkingSockets
, network_client
.remote
,
618 chat
, sizeof(netmsg_chat
)+l
+1,
619 k_nSteamNetworkingSend_Reliable
, NULL
);
622 static int cmd_network_send_message( int argc
, const char *argv
[] ){
623 char buf
[ NETWORK_MAX_CHAT
];
625 vg_strnull( &str
, buf
, NETWORK_MAX_CHAT
);
627 for( int i
=0; i
<argc
; i
++ ){
628 vg_strcat( &str
, argv
[i
] );
631 vg_strcatch( &str
, ' ' );
634 chat_send_message( buf
);
638 void network_init(void)
640 vg_console_reg_var( "network_info", &network_client
.network_info
,
641 k_var_dtype_i32
, VG_VAR_PERSISTENT
);
643 u32 alloc_size
= sizeof(struct network_request
)*NETWORK_MAX_REQUESTS
;
644 network_client
.request_buffer
=
645 vg_linear_alloc( vg_mem
.rtmemory
, alloc_size
);
646 memset( network_client
.request_buffer
, 0, alloc_size
);
648 vg_pool
*pool
= &network_client
.request_pool
;
649 pool
->buffer
= network_client
.request_buffer
;
650 pool
->count
= NETWORK_MAX_REQUESTS
;
651 pool
->stride
= sizeof( struct network_request
);
652 pool
->offset
= offsetof( struct network_request
, poolnode
);
653 vg_pool_init( pool
);
655 steam_register_callback( k_iSteamNetConnectionStatusChangedCallBack
,
656 on_server_connect_status
);
657 steam_register_callback( k_iPersonaStateChange
,
658 on_persona_state_change
);
659 request_auth_ticket();
661 vg_console_reg_cmd( "say", cmd_network_send_message
, NULL
);
665 void network_end(void)
667 /* TODO: Send buffered highscores that were not already */
668 if( (network_client
.state
== k_ESteamNetworkingConnectionState_Connected
) ||
669 (network_client
.state
== k_ESteamNetworkingConnectionState_Connecting
) )
671 SteamAPI_ISteamNetworkingSockets_CloseConnection(
672 hSteamNetworkingSockets
, network_client
.remote
, 0, NULL
, 1 );