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 int 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;
42 static HSteamNetConnection cremote
;
43 static ESteamNetworkingConnectionState cremote_state
=
44 k_ESteamNetworkingConnectionState_None
;
50 static void scores_update(void);
52 static void on_auth_ticket_recieved( void *result
, void *context
)
54 EncryptedAppTicketResponse_t
*response
= result
;
56 if( response
->m_eResult
== k_EResultOK
)
58 vg_info( " New app ticket ready\n" );
62 vg_warn( " Could not request new encrypted app ticket (%u)\n",
63 response
->m_eResult
);
66 if( SteamAPI_ISteamUser_GetEncryptedAppTicket( hSteamUser
,
68 vg_list_size(steam_app_ticket
),
69 &steam_app_ticket_length
))
71 vg_success( " Loaded app ticket (%u bytes)\n", steam_app_ticket_length
);
75 vg_error( " No ticket availible\n" );
76 steam_app_ticket_length
= 0;
80 static void request_auth_ticket(void)
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
87 vg_info( "Requesting new authorization ticket\n" );
88 steam_async
*call
= steam_new_async();
90 call
->p_handler
= on_auth_ticket_recieved
;
91 call
->id
= SteamAPI_ISteamUser_RequestEncryptedAppTicket( hSteamUser
,
95 static void send_auth_ticket(void)
97 u32 size
= sizeof(netmsg_auth
) + steam_app_ticket_length
;
98 netmsg_auth
*auth
= alloca(size
);
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
];
105 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
106 hSteamNetworkingSockets
, cremote
, auth
, size
,
107 k_nSteamNetworkingSend_Reliable
, NULL
);
110 static void send_score_request(void)
112 vg_info( "Requesting scores\n" );
113 netmsg_scores_request req
;
114 req
.inetmsg_id
= k_inetmsg_scores_request
;
116 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
117 hSteamNetworkingSockets
, cremote
, &req
, sizeof(netmsg_scores_request
),
118 k_nSteamNetworkingSend_Reliable
, NULL
);
121 static void send_score_update(void)
123 vg_info( "Sending scores\n" );
124 u32 size
= sizeof(netmsg_set_score
) +
125 vg_list_size(track_infos
)*sizeof(struct netmsg_score_record
);
126 netmsg_set_score
*setscore
= alloca( size
);
127 setscore
->inetmsg_id
= k_inetmsg_set_score
;
130 for( u32 i
=0; i
<vg_list_size(track_infos
); i
++ )
132 if( track_infos
[i
].push
)
134 track_infos
[i
].push
= 0;
135 highscore_record
*user_record
= highscore_find_user_record( 0, i
);
139 vg_error( "No score set but tried to upload for track %u\n", i
);
143 setscore
->records
[count
].trackid
= i
;
144 setscore
->records
[count
].playerid
= 0;
145 setscore
->records
[count
].points
= user_record
->points
;
146 setscore
->records
[count
].time
= user_record
->time
;
155 u32 send_size
= sizeof(netmsg_set_score
) +
156 count
*sizeof(struct netmsg_score_record
);
157 setscore
->record_count
= count
;
159 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
160 hSteamNetworkingSockets
, cremote
, setscore
, send_size
,
161 k_nSteamNetworkingSend_Reliable
, NULL
);
164 static void send_nickname(void)
166 netmsg_set_nickname nick
;
167 nick
.inetmsg_id
= k_inetmsg_set_nickname
;
169 memset( nick
.nickname
, 0, 10 );
170 strcpy( nick
.nickname
, "Harry" );
172 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
173 hSteamNetworkingSockets
, cremote
, &nick
, sizeof(netmsg_set_nickname
),
174 k_nSteamNetworkingSend_Reliable
, NULL
);
176 network_name_update
= 0;
179 static void server_routine_update(void)
183 if( network_name_update
)
187 send_score_request();
190 static void on_server_connect_status( CallbackMsg_t
*msg
)
192 SteamNetConnectionStatusChangedCallback_t
*info
= (void *)msg
->m_pubParam
;
193 vg_info( " Connection status changed for %lu\n", info
->m_hConn
);
194 vg_info( " %s -> %s\n",
195 string_ESteamNetworkingConnectionState(info
->m_eOldState
),
196 string_ESteamNetworkingConnectionState(info
->m_info
.m_eState
) );
198 if( info
->m_hConn
== cremote
)
200 cremote_state
= info
->m_info
.m_eState
;
201 if( info
->m_info
.m_eState
==
202 k_ESteamNetworkingConnectionState_Connected
)
204 vg_success(" Connected to remote server.. running updates\n");
205 server_routine_update();
210 vg_warn( " Recieved signal from unknown connection\n" );
214 static void network_connect_gc(void)
216 /* Connect to server if not connected */
217 SteamNetworkingIPAddr remoteAddr
;
219 #ifdef SR_USE_LOCALHOST
220 SteamAPI_SteamNetworkingIPAddr_SetIPv6LocalHost( &remoteAddr
, 27402 );
222 const char *server_lon1
= "46.101.34.155:27402";
223 SteamAPI_SteamNetworkingIPAddr_ParseString( &remoteAddr
, server_lon1
);
227 SteamAPI_SteamNetworkingIPAddr_ToString( &remoteAddr
, buf
, 256, 1 );
228 vg_info( "connect to: %s\n", buf
);
230 cremote
= SteamAPI_ISteamNetworkingSockets_ConnectByIPAddress(
231 hSteamNetworkingSockets
, &remoteAddr
, 0, NULL
);
234 static void on_inet_scoreboard( SteamNetworkingMessage_t
*msg
)
236 netmsg_scoreboard
*sb
= msg
->m_pData
;
238 u32 base_size
= sizeof(netmsg_scoreboard
)-
239 sizeof(struct netmsg_board
)*vg_list_size(track_infos
),
240 expected
= base_size
+sizeof(struct netmsg_board
)*sb
->board_count
;
242 if( msg
->m_cbSize
!= expected
)
244 vg_error( "Server scoreboard was corrupted. Size: %u != %u\n",
245 msg
->m_cbSize
, expected
);
249 if( vg_list_size(track_infos
) > sb
->board_count
)
250 vg_warn( "Server is out of date, not enough boards recieved\n");
251 else if( vg_list_size(track_infos
) < sb
->board_count
)
252 vg_warn( "Client out of date, server sent more boards than we have\n");
254 vg_success( "Recieved new scoreboards from server\n" );
256 for( int i
=0; i
< vg_min(sb
->board_count
,vg_list_size(track_infos
)); i
++)
258 scoreboard_client_data
.boards
[i
] = sb
->boards
[i
];
259 highscores_board_printf( stdout
, sb
->boards
[i
].data
, 10 );
263 /* We dont need to stay on the server currently */
264 SteamAPI_ISteamNetworkingSockets_CloseConnection(
265 hSteamNetworkingSockets
, cremote
, 0, NULL
, 1 );
267 network_scores_updated
= 1;
270 static void poll_connection(void)
272 SteamNetworkingMessage_t
*messages
[32];
277 len
= SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnConnection(
278 hSteamNetworkingSockets
, cremote
, messages
, vg_list_size(messages
));
283 for( int i
=0; i
<len
; i
++ )
285 SteamNetworkingMessage_t
*msg
= messages
[i
];
287 if( msg
->m_cbSize
< sizeof(netmsg_blank
) )
289 vg_warn( "Discarding message (too small: %d)\n", msg
->m_cbSize
);
293 netmsg_blank
*tmp
= msg
->m_pData
;
295 if( tmp
->inetmsg_id
== k_inetmsg_scoreboard
)
296 on_inet_scoreboard( msg
);
298 SteamAPI_SteamNetworkingMessage_t_Release( msg
);
304 * Subroutine to be connected to main game loop, runs all routines on timers
306 static void network_update(void)
310 static double last_update
= 0.0;
313 if( vg_time
> (last_update
+ 60.0) )
315 last_update
= vg_time
;
317 if( steam_app_ticket_length
)
319 network_connect_gc();
323 vg_low( "Not making remote connection; app ticket not gotten\n" );
327 if( vg_time
> (last_update
+ 10.0) &&
328 (cremote_state
== k_ESteamNetworkingConnectionState_Connected
))
330 vg_warn( "Connected to server but no return... disconnecting\n" );
331 SteamAPI_ISteamNetworkingSockets_CloseConnection(
332 hSteamNetworkingSockets
, cremote
, 0, NULL
, 1 );
337 static int network_init(void)
341 steam_register_callback( k_iSteamNetConnectionStatusChangedCallBack
,
342 on_server_connect_status
);
343 request_auth_ticket();
349 static void network_end(void*_
)
351 /* TODO: Fire off any buffered highscores that need to be setn */
352 if( cremote_state
== k_ESteamNetworkingConnectionState_Connected
||
353 cremote_state
== k_ESteamNetworkingConnectionState_Connecting
)
355 SteamAPI_ISteamNetworkingSockets_CloseConnection(
356 hSteamNetworkingSockets
, cremote
, 0, NULL
, 1 );
360 #else /* SR_NETWORKED */
362 static int network_init(void){ return 1; }
363 static void network_update(void){}
364 static void network_end(void*_
){}
366 #endif /* SR_NETWORKED */
367 #endif /* NETWORK_H */