a mess but stable
[carveJwlIkooP6JGAAIwe30JlM.git] / network.h
1 /*
2 * Copyright (C) 2021-2024 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 * All trademarks are property of their respective owners
4 */
5
6 #pragma once
7 #include "vg/vg_platform.h"
8 #include "vg/vg_steam_networking.h"
9 #include "vg/vg_mem_pool.h"
10 #include "vg/vg_msg.h"
11 #include "steam.h"
12 #include "network_common.h"
13 #include "network_msg.h"
14 #include "addon_types.h"
15
16 #define NETWORK_MAX_REQUESTS 8
17
18 /*
19 * Interface
20 */
21
22 /* Call it at start; Connects us to the gameserver */
23 void network_init(void);
24
25 /* Run this from main loop */
26 void network_update(void);
27
28 /* Call it at shutdown */
29 void network_end(void);
30
31 /*
32 * Can buffer up a bunch of these by calling many times, they will be
33 * sent at the next connection
34 */
35 void network_submit_highscore( u32 trackid, u16 points, u16 time );
36
37 /*
38 * Game endpoints are provided with the same names to allow running without a
39 * network connection.
40 */
41
42 struct network_client
43 {
44 u8 app_symmetric_key[ 1024 ];
45 u32 app_key_length;
46 EServerMode auth_mode;
47
48 HSteamNetConnection remote;
49 ESteamNetworkingConnectionState state;
50 u32 remote_version;
51
52 f64 last_attempt, last_frame;
53 u32 retries;
54
55 i32 network_info;
56 i32 auto_connect;
57
58 struct network_request {
59 vg_pool_node poolnode;
60 void (*callback)( netmsg_request *res, vg_msg *body, u64 userdata );
61 f64 sendtime;
62 u64 userdata;
63 }
64 *request_buffer;
65 vg_pool request_pool;
66
67 SteamNetworkingIPAddr ip;
68 char host_port[8], host_adress[256];
69 bool ip_resolved;
70
71 enum server_intent {
72 k_server_intent_offline,
73 k_server_intent_online
74 }
75 user_intent;
76 f64 last_intent_change;
77 f32 fintent; /* yeah this shit really shouldnt be here but oh well */
78 }
79 extern network_client;
80
81 int packet_minsize( SteamNetworkingMessage_t *msg, u32 size );
82 void network_send_item( enum netmsg_playeritem_type type );
83 void network_request_scoreboard( const char *mod_uid,
84 const char *route_uid,
85 u32 week, u64 userdata );
86 void network_publish_laptime( const char *mod_uid,
87 const char *route_uid, f64 lap_time );
88 void chat_send_message( const char *message );
89 void render_server_status_gui(void);
90 void network_status_string( vg_str *str, u32 *colour );
91 void network_send_region(void);
92 void network_set_host( const char *host_str, const char *port_str );
93
94 static inline int network_connected(void)
95 {
96 if( network_client.remote_version != NETWORK_SKATERIFT_VERSION ) return 0;
97 return network_client.state == k_ESteamNetworkingConnectionState_Connected;
98 }