4 #include "vg/vg_stdint.h"
6 #include "network_msg.h"
7 #include "highscores.h"
12 #define SR_USE_LOCALHOST
14 /* Call it at start; Connects us to the gameserver */
15 static void network_init(void);
17 /* Run this from main loop */
18 static void network_update(void);
20 /* Call it at shutdown */
21 static void network_end(void);
24 * Can buffer up a bunch of these by calling many times, they will be
25 * sent at the next connection
27 static void network_submit_highscore( u32 trackid
, u16 points
, u16 time
);
30 * Game endpoints are provided with the same names to allow running without a
36 * Runtime connection stuff
38 static u8 steam_app_ticket
[ 1024 ];
39 static u32 steam_app_ticket_length
;
40 static int network_name_update
= 1;
41 static int network_scores_updated
= 0;
43 static HSteamNetConnection cremote
;
44 static ESteamNetworkingConnectionState cremote_state
=
45 k_ESteamNetworkingConnectionState_None
;
51 static void scores_update(void);
53 static void on_auth_ticket_recieved( void *result
, void *context
)
55 EncryptedAppTicketResponse_t
*response
= result
;
57 if( response
->m_eResult
== k_EResultOK
)
59 vg_info( " New app ticket ready\n" );
63 vg_warn( " Could not request new encrypted app ticket (%u)\n",
64 response
->m_eResult
);
67 if( SteamAPI_ISteamUser_GetEncryptedAppTicket( hSteamUser
,
69 vg_list_size(steam_app_ticket
),
70 &steam_app_ticket_length
))
72 vg_success( " Loaded app ticket (%u bytes)\n", steam_app_ticket_length
);
76 vg_error( " No ticket availible\n" );
77 steam_app_ticket_length
= 0;
81 static void request_auth_ticket(void)
84 * TODO Check for one thats cached on the disk and load it.
85 * This might be OK though because steam seems to cache the result
88 vg_info( "Requesting new authorization ticket\n" );
89 steam_async
*call
= steam_new_async();
91 call
->p_handler
= on_auth_ticket_recieved
;
92 call
->id
= SteamAPI_ISteamUser_RequestEncryptedAppTicket( hSteamUser
,
96 static void send_auth_ticket(void)
98 u32 size
= sizeof(netmsg_auth
) + steam_app_ticket_length
;
99 netmsg_auth
*auth
= alloca(size
);
101 auth
->inetmsg_id
= k_inetmsg_auth
;
102 auth
->ticket_length
= steam_app_ticket_length
;
103 for( int i
=0; i
<steam_app_ticket_length
; i
++ )
104 auth
->ticket
[i
] = steam_app_ticket
[i
];
106 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
107 hSteamNetworkingSockets
, cremote
, auth
, size
,
108 k_nSteamNetworkingSend_Reliable
, NULL
);
111 static void send_score_request(void)
113 vg_info( "Requesting scores\n" );
114 netmsg_scores_request req
;
115 req
.inetmsg_id
= k_inetmsg_scores_request
;
117 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
118 hSteamNetworkingSockets
, cremote
, &req
, sizeof(netmsg_scores_request
),
119 k_nSteamNetworkingSend_Reliable
, NULL
);
122 static void send_score_update(void)
124 vg_info( "Sending scores\n" );
125 u32 size
= sizeof(netmsg_set_score
) +
126 vg_list_size(track_infos
)*sizeof(struct netmsg_score_record
);
127 netmsg_set_score
*setscore
= alloca( size
);
128 setscore
->inetmsg_id
= k_inetmsg_set_score
;
131 for( u32 i
=0; i
<vg_list_size(track_infos
); i
++ )
133 if( track_infos
[i
].push
)
135 track_infos
[i
].push
= 0;
136 highscore_record
*user_record
= highscore_find_user_record( 0, i
);
140 vg_error( "No score set but tried to upload for track %u\n", i
);
144 setscore
->records
[count
].trackid
= i
;
145 setscore
->records
[count
].playerid
= 0;
146 setscore
->records
[count
].points
= user_record
->points
;
147 setscore
->records
[count
].time
= user_record
->time
;
156 u32 send_size
= sizeof(netmsg_set_score
) +
157 count
*sizeof(struct netmsg_score_record
);
158 setscore
->record_count
= count
;
160 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
161 hSteamNetworkingSockets
, cremote
, setscore
, send_size
,
162 k_nSteamNetworkingSend_Reliable
, NULL
);
165 static void send_nickname(void)
167 netmsg_set_nickname nick
;
168 nick
.inetmsg_id
= k_inetmsg_set_nickname
;
170 memset( nick
.nickname
, 0, 10 );
171 strcpy( nick
.nickname
, "Harry" );
173 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
174 hSteamNetworkingSockets
, cremote
, &nick
, sizeof(netmsg_set_nickname
),
175 k_nSteamNetworkingSend_Reliable
, NULL
);
177 network_name_update
= 0;
180 static void server_routine_update(void)
184 if( network_name_update
)
188 send_score_request();
191 static void on_server_connect_status( CallbackMsg_t
*msg
)
193 SteamNetConnectionStatusChangedCallback_t
*info
= (void *)msg
->m_pubParam
;
194 vg_info( " Connection status changed for %lu\n", info
->m_hConn
);
195 vg_info( " %s -> %s\n",
196 string_ESteamNetworkingConnectionState(info
->m_eOldState
),
197 string_ESteamNetworkingConnectionState(info
->m_info
.m_eState
) );
199 if( info
->m_hConn
== cremote
)
201 cremote_state
= info
->m_info
.m_eState
;
202 if( info
->m_info
.m_eState
==
203 k_ESteamNetworkingConnectionState_Connected
)
205 vg_success(" Connected to remote server.. running updates\n");
206 server_routine_update();
211 vg_warn( " Recieved signal from unknown connection\n" );
215 static void network_connect_gc(void)
217 /* Connect to server if not connected */
218 SteamNetworkingIPAddr remoteAddr
;
220 #ifdef SR_USE_LOCALHOST
221 SteamAPI_SteamNetworkingIPAddr_SetIPv6LocalHost( &remoteAddr
, 27402 );
223 const char *server_lon1
= "46.101.34.155:27402";
224 SteamAPI_SteamNetworkingIPAddr_ParseString( &remoteAddr
, server_lon1
);
228 SteamAPI_SteamNetworkingIPAddr_ToString( &remoteAddr
, buf
, 256, 1 );
229 vg_info( "connect to: %s\n", buf
);
231 cremote
= SteamAPI_ISteamNetworkingSockets_ConnectByIPAddress(
232 hSteamNetworkingSockets
, &remoteAddr
, 0, NULL
);
235 static void on_inet_scoreboard( SteamNetworkingMessage_t
*msg
)
237 netmsg_scoreboard
*sb
= msg
->m_pData
;
239 u32 base_size
= sizeof(netmsg_scoreboard
)-
240 sizeof(struct netmsg_board
)*vg_list_size(track_infos
),
241 expected
= base_size
+sizeof(struct netmsg_board
)*sb
->board_count
;
243 if( msg
->m_cbSize
!= expected
)
245 vg_error( "Server scoreboard was corrupted. Size: %u != %u\n",
246 msg
->m_cbSize
, expected
);
250 if( vg_list_size(track_infos
) > sb
->board_count
)
251 vg_warn( "Server is out of date, not enough boards recieved\n");
252 else if( vg_list_size(track_infos
) < sb
->board_count
)
253 vg_warn( "Client out of date, server sent more boards than we have\n");
255 vg_success( "Recieved new scoreboards from server\n" );
257 for( int i
=0; i
< vg_min(sb
->board_count
,vg_list_size(track_infos
)); i
++)
259 scoreboard_client_data
.boards
[i
] = sb
->boards
[i
];
260 highscores_board_printf( stdout
, sb
->boards
[i
].data
, 10 );
264 /* We dont need to stay on the server currently */
265 SteamAPI_ISteamNetworkingSockets_CloseConnection(
266 hSteamNetworkingSockets
, cremote
, 0, NULL
, 1 );
268 network_scores_updated
= 1;
271 static void poll_connection(void)
273 SteamNetworkingMessage_t
*messages
[32];
278 len
= SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnConnection(
279 hSteamNetworkingSockets
, cremote
, messages
, vg_list_size(messages
));
284 for( int i
=0; i
<len
; i
++ )
286 SteamNetworkingMessage_t
*msg
= messages
[i
];
288 if( msg
->m_cbSize
< sizeof(netmsg_blank
) )
290 vg_warn( "Discarding message (too small: %d)\n", msg
->m_cbSize
);
294 netmsg_blank
*tmp
= msg
->m_pData
;
296 if( tmp
->inetmsg_id
== k_inetmsg_scoreboard
)
297 on_inet_scoreboard( msg
);
299 SteamAPI_SteamNetworkingMessage_t_Release( msg
);
305 * Subroutine to be connected to main game loop, runs all routines on timers
307 static void network_update(void)
311 static double last_update
= 0.0;
314 if( vg_time
> (last_update
+ 60.0) )
316 last_update
= vg_time
;
318 if( steam_app_ticket_length
)
320 network_connect_gc();
324 vg_log( "Not making remote connection; app ticket not gotten\n" );
328 if( vg_time
> (last_update
+ 10.0) &&
329 (cremote_state
== k_ESteamNetworkingConnectionState_Connected
))
331 vg_warn( "Connected to server but no return... disconnecting\n" );
332 SteamAPI_ISteamNetworkingSockets_CloseConnection(
333 hSteamNetworkingSockets
, cremote
, 0, NULL
, 1 );
338 static void network_init(void)
342 steam_register_callback( k_iSteamNetConnectionStatusChangedCallBack
,
343 on_server_connect_status
);
344 request_auth_ticket();
348 static void network_end(void)
350 /* TODO: Fire off any buffered highscores that need to be setn */
351 if( cremote_state
== k_ESteamNetworkingConnectionState_Connected
||
352 cremote_state
== k_ESteamNetworkingConnectionState_Connecting
)
354 SteamAPI_ISteamNetworkingSockets_CloseConnection(
355 hSteamNetworkingSockets
, cremote
, 0, NULL
, 1 );
359 #else /* SR_NETWORKED */
361 static void network_init(void){}
362 static void network_update(void){}
363 static void network_end(void){}
365 #endif /* SR_NETWORKED */
366 #endif /* NETWORK_H */