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