Fix major overstep with last commit
[carveJwlIkooP6JGAAIwe30JlM.git] / example_client.c
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 #define _DEFAULT_SOURCE
4 #include <unistd.h>
5 #include <signal.h>
6
7 #define VG_SERVER
8 #include "vg/vg.h"
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"
13
14 volatile sig_atomic_t sig_stop;
15
16 void inthandler( int signum )
17 {
18 sig_stop = 1;
19 }
20
21 /*
22 * Runtime connection stuff
23 */
24
25 static u8 steam_app_ticket[ 1024 ];
26 static u32 steam_app_ticket_length;
27
28 static HSteamNetConnection cremote;
29 static ESteamNetworkingConnectionState cremote_state =
30 k_ESteamNetworkingConnectionState_None;
31
32 /*
33 * Steam API
34 */
35 void *hSteamNetworkingSockets,
36 *hSteamUser;
37
38 static void scores_update(void);
39
40 static void handle_steam_callback( CallbackMsg_t *msg )
41 {
42 if( msg->m_iCallback == k_iSteamNetConnectionStatusChangedCallBack )
43 {
44 SteamNetConnectionStatusChangedCallback_t *info = (void *)msg->m_pubParam;
45 vg_info( " Connection status changed for %lu\n", info->m_hConn );
46
47 vg_info( " %s -> %s\n",
48 string_ESteamNetworkingConnectionState(info->m_info.m_eState) );
49 string_ESteamNetworkingConnectionState(info->m_eOldState) );
50
51 if( info->m_hConn == cremote )
52 {
53 cremote_state = info->m_info.m_eState;
54 if( info->m_info.m_eState ==
55 k_ESteamNetworkingConnectionState_Connected )
56 {
57 vg_success(" Connected to remote server\n");
58 scores_update();
59 }
60 }
61 else
62 {
63 vg_warn( " Recieved signal from unkown connection\n" );
64 }
65 }
66 }
67
68 static void on_auth_ticket_recieved( void *result, void *context )
69 {
70 EncryptedAppTicketResponse_t *response = result;
71
72 if( response->m_eResult == k_EResultOK )
73 {
74 vg_info( " New app ticket ready\n" );
75 }
76 else
77 {
78 vg_warn( " Could not request new encrypted app ticket (%u)\n",
79 response->m_eResult );
80 }
81
82 if( SteamAPI_ISteamUser_GetEncryptedAppTicket( hSteamUser,
83 steam_app_ticket,
84 vg_list_size(steam_app_ticket),
85 &steam_app_ticket_length ))
86 {
87 vg_success( " Loaded app ticket (%u bytes)\n", steam_app_ticket_length );
88 }
89 else
90 {
91 vg_error( " No ticket availible\n" );
92 steam_app_ticket_length = 0;
93 }
94 }
95
96 static void request_auth_ticket(void)
97 {
98 /*
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
101 */
102
103 vg_info( "Requesting new authorization ticket\n" );
104 steam_async *call = steam_new_async();
105 call->data = NULL;
106 call->p_handler = on_auth_ticket_recieved;
107 call->id = SteamAPI_ISteamUser_RequestEncryptedAppTicket( hSteamUser,
108 NULL, 0 );
109 }
110
111 static void server_connect(void)
112 {
113 /* Connect to server if not connected */
114
115 SteamNetworkingIPAddr remoteAddr;
116
117 #define USE_LOCALHOST
118 #ifdef USE_LOCALHOST
119 SteamAPI_SteamNetworkingIPAddr_SetIPv6LocalHost( &remoteAddr, 27402 );
120 #else
121 const char *server_lon1 = "46.101.34.155:27402";
122 SteamAPI_SteamNetworkingIPAddr_ParseString( &remoteAddr, server_lon1 );
123 #endif
124
125 char buf[256];
126 SteamAPI_SteamNetworkingIPAddr_ToString( &remoteAddr, buf, 256, 1 );
127 vg_info( "connect to: %s\n", buf );
128
129 cremote = SteamAPI_ISteamNetworkingSockets_ConnectByIPAddress(
130 hSteamNetworkingSockets, &remoteAddr, 0, NULL );
131 }
132
133 static void scores_update(void)
134 {
135 vg_log( "scores_update()\n" );
136
137 if( cremote_state == k_ESteamNetworkingConnectionState_Connected )
138 {
139 /*
140 * request updated scores
141 */
142 netmsg_scores_request req;
143 req.inetmsg_id = k_inetmsg_scores_request;
144
145 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
146 hSteamNetworkingSockets, cremote, &req,
147 sizeof(netmsg_scores_request),
148 k_nSteamNetworkingSend_Reliable, NULL );
149 }
150 else
151 {
152 /*
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
155 */
156 server_connect();
157 }
158 }
159
160 static void poll_connection(void)
161 {
162 SteamNetworkingMessage_t *messages[32];
163 int len;
164
165 while(1)
166 {
167 len = SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnConnection(
168 hSteamNetworkingSockets, cremote, messages, vg_list_size(messages));
169
170 if( len <= 0 )
171 return;
172
173 for( int i=0; i<len; i++ )
174 {
175 SteamNetworkingMessage_t *msg = messages[i];
176
177 if( msg->m_cbSize < sizeof(netmsg_blank) )
178 {
179 vg_warn( "Discarding message (too small: %d)\n",
180 msg->m_cbSize );
181 continue;
182 }
183
184 netmsg_blank *tmp = msg->m_pData;
185 if( tmp->inetmsg_id == k_inetmsg_scores_info )
186 {
187 netmsg_scores_info *info = msg->m_pData;
188 vg_log( "Recieved %u score records\n", info->record_count );
189
190 SteamAPI_ISteamNetworkingSockets_CloseConnection(
191 hSteamNetworkingSockets, cremote, 0, NULL, 1 );
192 cremote_state = k_ESteamNetworkingConnectionState_None;
193 }
194
195 SteamAPI_SteamNetworkingMessage_t_Release( msg );
196 }
197 }
198 }
199
200 static u64 in_server_ticks( double seconds )
201 {
202 return (u64)(seconds / 0.1);
203 }
204
205 int main( int argc, char *argv[] )
206 {
207 signal( SIGINT, inthandler );
208
209 if( !SteamAPI_Init() )
210 {
211 vg_error( "Steamworks failed to initialize\n" );
212 return 0;
213 }
214
215 SteamAPI_ManualDispatch_Init();
216 HSteamPipe hsteampipe = SteamAPI_GetHSteamPipe();
217 vg_success( "Steamworks API running\n" );
218
219 /* Connect interfaces */
220 hSteamNetworkingSockets = SteamAPI_SteamNetworkingSockets_SteamAPI();
221 hSteamUser = SteamAPI_SteamUser();
222
223 request_auth_ticket();
224
225 /* TODO: Request current scores */
226
227 #if 0
228
229 #endif
230
231 u64 server_ticks = 8000, last_update = 0;
232
233 while( !sig_stop )
234 {
235 steamworks_event_loop( hsteampipe, handle_steam_callback );
236 poll_connection();
237
238 usleep(100000);
239 server_ticks ++;
240
241 if( server_ticks > (last_update + in_server_ticks(60.0)) )
242 {
243 last_update = server_ticks;
244 scores_update();
245 }
246 }
247
248 vg_info( "Shutting down\n..." );
249 SteamAPI_Shutdown();
250
251 return 0;
252 }