Do actual domain resolution
[carveJwlIkooP6JGAAIwe30JlM.git] / network.c
1 #include "skaterift.h"
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"
6 #include "player.h"
7 #include "network.h"
8 #include "network_msg.h"
9 #include "network_common.h"
10 #include "player_remote.h"
11 #include "world.h"
12 #include "world_sfd.h"
13 #include "world_routes.h"
14 #include "vg/vg_imgui.h"
15 #include "gui.h"
16 #include "ent_region.h"
17 #include "vg/vg_loader.h"
18
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <netdb.h>
22
23 struct network_client network_client =
24 {
25 .auth_mode = eServerModeAuthentication,
26 .state = k_ESteamNetworkingConnectionState_None,
27 .last_intent_change = -99999.9
28 };
29
30 static void scores_update(void);
31
32 int packet_minsize( SteamNetworkingMessage_t *msg, u32 size ){
33 if( msg->m_cbSize < size ) {
34 vg_error( "Invalid packet size (must be at least %u)\n", size );
35 return 0;
36 }
37 else{
38 return 1;
39 }
40 }
41
42 static void on_auth_ticket_recieved( void *result, void *context ){
43 EncryptedAppTicketResponse_t *response = result;
44
45 if( response->m_eResult == k_EResultOK ){
46 vg_info( " New app ticket ready\n" );
47 }
48 else{
49 vg_warn( " Could not request new encrypted app ticket (%u)\n",
50 response->m_eResult );
51 }
52
53 if( SteamAPI_ISteamUser_GetEncryptedAppTicket( hSteamUser,
54 network_client.app_symmetric_key,
55 vg_list_size(network_client.app_symmetric_key),
56 &network_client.app_key_length )){
57 vg_success( " Loaded app ticket\n" );
58 }
59 else{
60 vg_error( " No ticket availible\n" );
61 network_client.app_key_length = 0;
62 }
63 }
64
65 static void request_auth_ticket(void){
66 /*
67 * TODO Check for one thats cached on the disk and load it.
68 * This might be OK though because steam seems to cache the result
69 */
70
71 vg_info( "Requesting new authorization ticket\n" );
72
73 vg_steam_async_call *call = vg_alloc_async_steam_api_call();
74 call->userdata = NULL;
75 call->p_handler = on_auth_ticket_recieved;
76 call->id =
77 SteamAPI_ISteamUser_RequestEncryptedAppTicket( hSteamUser, NULL, 0 );
78 }
79
80 static void network_send_username(void){
81 if( !network_connected() )
82 return;
83
84 netmsg_playerusername *update = alloca( sizeof(netmsg_playerusername)+
85 NETWORK_USERNAME_MAX );
86 update->inetmsg_id = k_inetmsg_playerusername;
87 update->index = 0xff;
88
89 ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
90 const char *username = SteamAPI_ISteamFriends_GetPersonaName(hSteamFriends);
91 u32 chs = str_utf8_collapse( username, update->name, NETWORK_USERNAME_MAX );
92
93 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
94 hSteamNetworkingSockets, network_client.remote,
95 update, sizeof(netmsg_playerusername)+chs+1,
96 k_nSteamNetworkingSend_Reliable, NULL );
97 }
98
99 void network_send_region(void)
100 {
101 if( !network_connected() )
102 return;
103
104 netmsg_region *region = alloca( sizeof(netmsg_region) + NETWORK_REGION_MAX );
105
106 region->inetmsg_id = k_inetmsg_region;
107 region->client = 0;
108 region->flags = global_ent_region.flags;
109
110 u32 l = vg_strncpy( global_ent_region.location, region->loc,
111 NETWORK_REGION_MAX, k_strncpy_always_add_null );
112
113 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
114 hSteamNetworkingSockets, network_client.remote,
115 region, sizeof(netmsg_region)+l+1,
116 k_nSteamNetworkingSend_Reliable, NULL );
117 }
118
119 static void network_send_request( netmsg_request *req, vg_msg *body,
120 void (*callback)(
121 netmsg_request *res, vg_msg *body,
122 u64 userdata),
123 u64 userdata ){
124 u32 len = 0;
125 if( body ){
126 len = body->cur.co;
127 vg_info( "Request scoreboard. Info (%u):\n", body->cur.co );
128 vg_msg_print( body, len );
129
130 if( body->error != k_vg_msg_error_OK ){
131 vg_error( "Body not OK\n" );
132 return;
133 }
134 }
135
136 if( callback ){
137 req->id = vg_pool_lru( &network_client.request_pool );
138 if( req->id ){
139 vg_pool_watch( &network_client.request_pool, req->id );
140 struct network_request *pn =
141 vg_pool_item( &network_client.request_pool, req->id );
142 pn->callback = callback;
143 pn->sendtime = vg.time_real;
144 pn->userdata = userdata;
145 }
146 else{
147 vg_error( "Unable to send request. Pool is full.\n" );
148 return;
149 }
150 }
151 else
152 req->id = 0;
153
154 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
155 hSteamNetworkingSockets, network_client.remote,
156 req, sizeof(netmsg_request)+len,
157 k_nSteamNetworkingSend_Reliable, NULL );
158 }
159
160 static void network_scoreboard_callback( netmsg_request *res, vg_msg *body,
161 u64 userdata ){
162 world_instance *world = world_current_instance();
163
164 world_routes_recv_scoreboard( world, body, userdata, res->status );
165 if( userdata == world_sfd.active_route_board )
166 world_sfd_compile_active_scores();
167 }
168
169
170
171 /* mod_uid: world mod uid,
172 * route_uid: run name (just a string)
173 * week:
174 * 0 ALL TIME
175 * 1 CURRENT WEEK
176 * 2 ALL TIME + CURRENT WEEK
177 * .
178 * 10+ specific week index
179 */
180 void network_request_scoreboard( const char *mod_uid,
181 const char *route_uid,
182 u32 week, u64 userdata ){
183 if( !network_connected() )
184 return;
185
186 netmsg_request *req = alloca( sizeof(netmsg_request) + 512 );
187 req->inetmsg_id = k_inetmsg_request;
188
189 vg_msg data;
190 vg_msg_init( &data, req->q, 512 );
191 vg_msg_wkvstr( &data, "endpoint", "scoreboard" );
192 vg_msg_wkvstr( &data, "mod", mod_uid );
193 vg_msg_wkvstr( &data, "route", route_uid );
194 vg_msg_wkvnum( &data, "week", k_vg_msg_u32, 1, &week );
195 network_send_request( req, &data, network_scoreboard_callback, userdata );
196 }
197
198 static void network_publish_callback( netmsg_request *res, vg_msg *body,
199 u64 userdata ){
200 if( res->status != k_request_status_ok ){
201 vg_error( "Publish laptime, server error #%d\n", (i32)res->status );
202 }
203 }
204
205 void network_publish_laptime( const char *mod_uid,
206 const char *route_uid, f64 lap_time ){
207 if( !network_connected() )
208 return;
209
210 i32 time_centiseconds = lap_time * 100.0;
211
212 netmsg_request *req = alloca( sizeof(netmsg_request) + 512 );
213 req->inetmsg_id = k_inetmsg_request;
214
215 vg_msg data;
216 vg_msg_init( &data, req->q, 512 );
217 vg_msg_wkvstr( &data, "endpoint", "setlap" );
218 vg_msg_wkvstr( &data, "mod", mod_uid );
219 vg_msg_wkvstr( &data, "route", route_uid );
220 vg_msg_wkvnum( &data, "time", k_vg_msg_i32, 1, &time_centiseconds );
221 network_send_request( req, &data, network_publish_callback, 0 );
222 }
223
224 static void network_request_rx_300_400( SteamNetworkingMessage_t *msg ){
225 netmsg_blank *tmp = msg->m_pData;
226
227 if( tmp->inetmsg_id == k_inetmsg_request ){
228
229 }
230 else if( tmp->inetmsg_id == k_inetmsg_response ){
231 netmsg_request *res = (netmsg_request *)msg->m_pData;
232
233 vg_msg *body = NULL;
234
235 vg_msg data;
236 if( res->status == k_request_status_ok ){
237 vg_msg_init( &data, res->q, msg->m_cbSize - sizeof(netmsg_request) );
238 vg_success( "Response to #%d:\n", (i32)res->id );
239 vg_msg_print( &data, data.max );
240 body = &data;
241 }
242 else {
243 vg_warn( "Server response to #%d: %d\n", (i32)res->id, res->status );
244 }
245
246 if( res->id ){
247 struct network_request *pn =
248 vg_pool_item( &network_client.request_pool, res->id );
249 pn->callback( res, body, pn->userdata );
250 vg_pool_unwatch( &network_client.request_pool, res->id );
251 }
252 }
253 }
254
255 void network_send_item( enum netmsg_playeritem_type type )
256 {
257 if( !network_connected() )
258 return;
259
260 netmsg_playeritem *item =
261 alloca( sizeof(netmsg_playeritem) + ADDON_UID_MAX );
262 item->inetmsg_id = k_inetmsg_playeritem;
263 item->type_index = type;
264 item->client = 0;
265
266 if( (type == k_netmsg_playeritem_world0) ||
267 (type == k_netmsg_playeritem_world1) ){
268
269 enum world_purpose purpose = type - k_netmsg_playeritem_world0;
270 addon_reg *reg = world_static.instance_addons[ purpose ];
271
272 if( reg )
273 addon_alias_uid( &reg->alias, item->uid );
274 else
275 item->uid[0] = '\0';
276 }
277 else{
278 u16 view_id = 0;
279 enum addon_type addon_type = k_addon_type_none;
280 if( type == k_netmsg_playeritem_board ){
281 view_id = localplayer.board_view_slot;
282 addon_type = k_addon_type_board;
283 }
284 else if( type == k_netmsg_playeritem_player ){
285 view_id = localplayer.playermodel_view_slot;
286 addon_type = k_addon_type_player;
287 }
288
289 struct addon_cache *cache = &addon_system.cache[addon_type];
290 vg_pool *pool = &cache->pool;
291
292 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
293 addon_cache_entry *entry = vg_pool_item( pool, view_id );
294 addon_alias_uid( &entry->reg_ptr->alias, item->uid );
295 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
296 }
297
298 vg_info( "send equip: [%u] %s\n",
299 item->type_index, item->uid );
300 u32 chs = strlen(item->uid);
301
302 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
303 hSteamNetworkingSockets, network_client.remote,
304 item, sizeof(netmsg_playeritem)+chs+1,
305 k_nSteamNetworkingSend_Reliable, NULL );
306 }
307
308 static void network_disconnect(void){
309 SteamAPI_ISteamNetworkingSockets_CloseConnection(
310 hSteamNetworkingSockets, network_client.remote, 0, NULL, 0 );
311 network_client.remote = 0;
312 network_client.state = k_ESteamNetworkingConnectionState_None;
313
314 for( int i=0; i<vg_list_size(netplayers.list); i++ ){
315 netplayers.list[i].active = 0;
316 }
317 }
318
319 void network_status_string( vg_str *str, u32 *colour )
320 {
321 if( skaterift.demo_mode ){
322 vg_strcat( str, "Offline" );
323 return;
324 }
325
326 if( steam_ready ){
327 if( network_client.user_intent == k_server_intent_offline ){
328 vg_strcat( str, "Offline" );
329 }
330 else {
331 ESteamNetworkingConnectionState state = network_client.state;
332
333 if( state == k_ESteamNetworkingConnectionState_None )
334 vg_strcat( str, "No Connection" );
335 else if( state == k_ESteamNetworkingConnectionState_Connecting )
336 {
337 vg_strcatf( str, "Connecting...\n%s", network_client.host_adress );
338
339 if( network_client.retries ){
340 vg_strcat( str, "\n(" );
341 vg_strcati32( str, network_client.retries );
342 vg_strcat( str, " retries)" );
343 }
344 }
345 else if( state == k_ESteamNetworkingConnectionState_Connected ){
346 vg_strcatf( str, "Connected to:\n%s", network_client.host_adress );
347 *colour = 0xff00a020;
348 }
349 else if( state == k_ESteamNetworkingConnectionState_ClosedByPeer )
350 vg_strcat( str, "Connection Closed" );
351 else if( state == k_ESteamNetworkingConnectionState_FindingRoute )
352 vg_strcat( str, "Finding Route" );
353 else if( state ==
354 k_ESteamNetworkingConnectionState_ProblemDetectedLocally){
355 vg_strcat( str, "Problem Detected\nLocally" );
356 *colour = 0xff0000a0;
357 }
358 else
359 vg_strcat( str, "???" );
360 }
361 }
362 else {
363 vg_strcat( str, "Steam Offline" );
364 *colour = 0xff0000a0;
365 }
366 }
367
368 void render_server_status_gui(void)
369 {
370 render_fb_bind( gpipeline.fb_workshop_preview, 0 );
371
372 /* HACK */
373 vg_ui.cur_vert = 0;
374 vg_ui.cur_indice = 0;
375 vg_ui.vert_start = 0;
376 vg_ui.indice_start = 0;
377
378 ui_rect r = { 0, 0, 128, 48 };
379
380 char buf[128];
381 vg_str str;
382 vg_strnull( &str, buf, sizeof(buf) );
383
384 u32 bg = 0xff000000;
385 network_status_string( &str, &bg );
386
387 ui_fill( r, bg );
388 ui_text( r, buf, 1, k_ui_align_center, 0 );
389 ui_flush( k_ui_shader_colour, 128, 48 );
390
391 skaterift.rt_textures[ k_skaterift_rt_server_status ] =
392 gpipeline.fb_workshop_preview->attachments[0].id;
393 }
394
395 static void on_server_connect_status( CallbackMsg_t *msg ){
396 SteamNetConnectionStatusChangedCallback_t *info = (void *)msg->m_pubParam;
397 vg_info( " Connection status changed for %lu\n", info->m_hConn );
398 vg_info( " %s -> %s\n",
399 string_ESteamNetworkingConnectionState(info->m_eOldState),
400 string_ESteamNetworkingConnectionState(info->m_info.m_eState) );
401
402 if( info->m_hConn == network_client.remote ){
403 network_client.state = info->m_info.m_eState;
404
405 if( info->m_info.m_eState ==
406 k_ESteamNetworkingConnectionState_Connected ){
407 vg_success(" Connected to remote server.. authenticating\n");
408
409 /* send version info to server */
410 netmsg_version version;
411 version.inetmsg_id = k_inetmsg_version;
412 version.version = NETWORK_SKATERIFT_VERSION;
413 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
414 hSteamNetworkingSockets, network_client.remote, &version,
415 sizeof(netmsg_version), k_nSteamNetworkingSend_Reliable, NULL );
416
417 /* TODO: We should really wait to see if the server is in auth mode
418 * first... */
419 u32 size = sizeof(netmsg_auth) + network_client.app_key_length;
420 netmsg_auth *auth = alloca(size);
421 auth->inetmsg_id = k_inetmsg_auth;
422 auth->ticket_length = network_client.app_key_length;
423 for( int i=0; i<network_client.app_key_length; i++ )
424 auth->ticket[i] = network_client.app_symmetric_key[i];
425
426 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
427 hSteamNetworkingSockets, network_client.remote, auth, size,
428 k_nSteamNetworkingSend_Reliable, NULL );
429 }
430 else if( info->m_info.m_eState ==
431 k_ESteamNetworkingConnectionState_ClosedByPeer ){
432
433 if( info->m_info.m_eEndReason ==
434 k_ESteamNetConnectionEnd_Misc_InternalError ){
435 network_client.retries = 40;
436 }
437 network_disconnect();
438 }
439 else if( info->m_info.m_eState ==
440 k_ESteamNetworkingConnectionState_ProblemDetectedLocally ){
441 network_disconnect();
442 }
443 }
444 else{
445 //vg_warn( " Recieved signal from unknown connection\n" );
446 }
447
448 render_server_status_gui();
449 }
450
451 static void on_persona_state_change( CallbackMsg_t *msg ){
452 if( !network_connected() )
453 return;
454
455 PersonaStateChange_t *info = (void *)msg->m_pubParam;
456 ISteamUser *hSteamUser = SteamAPI_SteamUser();
457
458 vg_info( "User: %llu, change: %u\n", info->m_ulSteamID,
459 info->m_nChangeFlags );
460
461 if( info->m_ulSteamID == SteamAPI_ISteamUser_GetSteamID(hSteamUser) ){
462 if( info->m_nChangeFlags & k_EPersonaChangeName ){
463 network_send_username();
464 }
465 }
466
467 if( info->m_nChangeFlags & k_EPersonaChangeRelationshipChanged ){
468 for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
469 struct network_player *rp = &netplayers.list[i];
470 if( rp->steamid == info->m_ulSteamID ){
471 player_remote_update_friendflags( rp );
472 }
473 }
474 }
475 }
476
477 void network_set_host( const char *host_str, const char *port_str )
478 {
479 vg_strncpy( host_str, network_client.host_adress,
480 sizeof(network_client.host_adress), k_strncpy_overflow_fatal );
481
482 memset( &network_client.ip, 0, sizeof(network_client.ip) );
483 network_client.ip_resolved = 0;
484
485 if( port_str )
486 {
487 vg_strncpy( port_str, network_client.host_port,
488 sizeof(network_client.host_port), k_strncpy_overflow_fatal );
489 }
490 else
491 {
492 vg_str str;
493 vg_strnull( &str, network_client.host_port,
494 sizeof(network_client.host_port) );
495 vg_strcati32( &str, NETWORK_PORT );
496 }
497
498 network_client.ip.m_port = atoi( network_client.host_port );
499 }
500
501 static void network_connect(void)
502 {
503 VG_ASSERT( network_client.ip_resolved );
504
505 vg_info( "connecting...\n" );
506 network_client.remote = SteamAPI_ISteamNetworkingSockets_ConnectByIPAddress(
507 hSteamNetworkingSockets, &network_client.ip, 0, NULL );
508 }
509
510 static void network_sign_on_complete(void){
511 vg_success( "Sign on completed\n" );
512
513 /* send our init info */
514 network_send_username();
515 for( u32 i=0; i<k_netmsg_playeritem_max; i ++ ){
516 network_send_item(i);
517 }
518 network_send_region();
519 }
520
521 static void poll_remote_connection(void){
522 SteamNetworkingMessage_t *messages[32];
523 int len;
524
525 for( int i=0; i<10; i++ ){
526 len = SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnConnection(
527 hSteamNetworkingSockets, network_client.remote,
528 messages, vg_list_size(messages));
529
530 if( len <= 0 )
531 return;
532
533 for( int i=0; i<len; i++ ){
534 SteamNetworkingMessage_t *msg = messages[i];
535
536 if( msg->m_cbSize < sizeof(netmsg_blank) ){
537 vg_warn( "Discarding message (too small: %d)\n", msg->m_cbSize );
538 continue;
539 }
540
541 netmsg_blank *tmp = msg->m_pData;
542
543 if( (tmp->inetmsg_id >= 200) && (tmp->inetmsg_id < 300) ){
544 player_remote_rx_200_300( msg );
545 }
546 else if( (tmp->inetmsg_id >= 300) && (tmp->inetmsg_id < 400) ){
547 network_request_rx_300_400( msg );
548 }
549 else {
550 if( tmp->inetmsg_id == k_inetmsg_version ){
551 netmsg_version *version = msg->m_pData;
552 if( version->version != NETWORK_SKATERIFT_VERSION ){
553 network_disconnect();
554 /* we dont want to connect to this server ever */
555 network_client.retries = 999;
556 network_client.last_attempt = 999999999.9;
557 vg_error( "version mismatch with server\n" );
558 }
559 else {
560 network_client.remote_version = version->version;
561 network_sign_on_complete();
562 }
563 }
564 }
565
566 SteamAPI_SteamNetworkingMessage_t_Release( msg );
567 }
568 }
569 }
570
571 static void network_resolve_host_async( void *payload, u32 size )
572 {
573 u32 *status = payload;
574 network_client.ip_resolved = *status;
575
576 char buf[256];
577 SteamAPI_SteamNetworkingIPAddr_ToString( &network_client.ip, buf, 256, 1 );
578 vg_info( "Resolved host address to: %s\n", buf );
579 }
580
581 static void network_resolve_host_thread( void *_ )
582 {
583 vg_async_item *call = vg_async_alloc(8);
584 u32 *status = call->payload;
585 *status = 0;
586
587 if( (network_client.host_adress[0] >= '0') &&
588 (network_client.host_adress[0] <= '9') )
589 {
590 SteamAPI_SteamNetworkingIPAddr_ParseString(
591 &network_client.ip,
592 network_client.host_adress );
593 network_client.ip.m_port = atoi( network_client.host_port );
594 *status = 1;
595 goto end;
596 }
597
598 vg_info( "Resolving host.. %s (:%s)\n",
599 network_client.host_adress, network_client.host_port );
600
601 struct addrinfo hints;
602 struct addrinfo *result;
603
604 /* Obtain address(es) matching host/port. */
605
606 memset( &hints, 0, sizeof(hints) );
607 hints.ai_family = AF_INET6;
608 hints.ai_socktype = SOCK_DGRAM;
609 hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG;
610 hints.ai_protocol = 0;
611
612 int s = getaddrinfo( network_client.host_adress, network_client.host_port,
613 &hints, &result);
614 if( s != 0 )
615 {
616 vg_error( "getaddrinfo: %s\n", gai_strerror(s) );
617 goto end;
618 }
619
620 struct sockaddr_in6 *inaddr = (struct sockaddr_in6 *)result->ai_addr;
621 memcpy( network_client.ip.m_ipv6, &inaddr->sin6_addr, 16 );
622 freeaddrinfo( result );
623
624 *status = 1;
625
626 end: vg_async_dispatch( call, network_resolve_host_async );
627 }
628
629 void network_update(void)
630 {
631 if( !steam_ready )
632 return;
633
634 ESteamNetworkingConnectionState state = network_client.state;
635
636 if( network_client.user_intent == k_server_intent_offline )
637 {
638 if( state != k_ESteamNetworkingConnectionState_None )
639 network_disconnect();
640
641 return;
642 }
643
644 if( state == k_ESteamNetworkingConnectionState_Connected )
645 {
646 poll_remote_connection();
647 f64 frame_delta = vg.time_real - network_client.last_frame;
648
649 if( frame_delta > NETWORK_FRAMERATE )
650 {
651 network_client.last_frame = vg.time_real;
652 remote_player_send_playerframe();
653 localplayer.sfx_buffer_count = 0;
654 }
655
656 remote_player_debug_update();
657 }
658 else
659 {
660 if( (state == k_ESteamNetworkingConnectionState_Connecting) ||
661 (state == k_ESteamNetworkingConnectionState_FindingRoute) )
662 {
663 return;
664 }
665 else
666 {
667 f64 waited = vg.time_real - network_client.last_attempt,
668 min_wait = 1.0;
669
670 if( network_client.retries > 5 )
671 min_wait = 60.0;
672
673 if( waited < min_wait )
674 return;
675
676 if( !network_client.ip_resolved )
677 {
678 if( vg_loader_availible() )
679 {
680 vg_loader_start( network_resolve_host_thread, NULL );
681 }
682 else return;
683 }
684 else
685 network_connect();
686
687 network_client.retries ++;
688 network_client.last_attempt = vg.time_real;
689 }
690 }
691 }
692
693 void chat_send_message( const char *message )
694 {
695 if( !network_connected() ){
696 return;
697 }
698
699 netmsg_chat *chat = alloca( sizeof(netmsg_chat) + NETWORK_MAX_CHAT );
700 chat->inetmsg_id = k_inetmsg_chat;
701 chat->client = 0;
702
703 u32 l = vg_strncpy( message, chat->msg, NETWORK_MAX_CHAT,
704 k_strncpy_always_add_null );
705
706 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
707 hSteamNetworkingSockets, network_client.remote,
708 chat, sizeof(netmsg_chat)+l+1,
709 k_nSteamNetworkingSend_Reliable, NULL );
710 }
711
712 static int cmd_network_send_message( int argc, const char *argv[] ){
713 char buf[ NETWORK_MAX_CHAT ];
714 vg_str str;
715 vg_strnull( &str, buf, NETWORK_MAX_CHAT );
716
717 for( int i=0; i<argc; i ++ ){
718 vg_strcat( &str, argv[i] );
719
720 if( i < argc-1 )
721 vg_strcatch( &str, ' ' );
722 }
723
724 chat_send_message( buf );
725 return 0;
726 }
727
728 void network_init(void)
729 {
730 vg_console_reg_var( "network_info", &network_client.network_info,
731 k_var_dtype_i32, VG_VAR_PERSISTENT );
732 vg_console_reg_var( "auto_connect", &network_client.auto_connect,
733 k_var_dtype_i32, VG_VAR_PERSISTENT );
734 if( steam_ready ){
735 u32 alloc_size = sizeof(struct network_request)*NETWORK_MAX_REQUESTS;
736 network_client.request_buffer =
737 vg_linear_alloc( vg_mem.rtmemory, alloc_size );
738 memset( network_client.request_buffer, 0, alloc_size );
739
740 vg_pool *pool = &network_client.request_pool;
741 pool->buffer = network_client.request_buffer;
742 pool->count = NETWORK_MAX_REQUESTS;
743 pool->stride = sizeof( struct network_request );
744 pool->offset = offsetof( struct network_request, poolnode );
745 vg_pool_init( pool );
746
747 steam_register_callback( k_iSteamNetConnectionStatusChangedCallBack,
748 on_server_connect_status );
749 steam_register_callback( k_iPersonaStateChange,
750 on_persona_state_change );
751 request_auth_ticket();
752
753 vg_console_reg_cmd( "say", cmd_network_send_message, NULL );
754 }
755 }
756
757 void network_end(void)
758 {
759 /* TODO: Send buffered highscores that were not already */
760 if( (network_client.state == k_ESteamNetworkingConnectionState_Connected) ||
761 (network_client.state == k_ESteamNetworkingConnectionState_Connecting) )
762 {
763 SteamAPI_ISteamNetworkingSockets_CloseConnection(
764 hSteamNetworkingSockets, network_client.remote, 0, NULL, 1 );
765 }
766 }