4 #include "vg/vg_stdint.h"
6 #include "network_msg.h"
12 /* Call it at start; Connects us to the gameserver */
13 static void network_init(void);
15 /* Run this from main loop */
16 static void network_update(void);
18 /* Call it at shutdown */
19 static void network_end(void);
22 * Can buffer up a bunch of these by calling many times, they will be
23 * sent at the next connection
25 static void network_submit_highscore( u32 trackid
, u16 points
, u16 time
);
29 * Game endpoints are provided with the same names to allow running without a
35 * Runtime connection stuff
37 static u8 steam_app_ticket
[ 1024 ];
38 static u32 steam_app_ticket_length
;
40 static HSteamNetConnection cremote
;
41 static ESteamNetworkingConnectionState cremote_state
=
42 k_ESteamNetworkingConnectionState_None
;
48 static void scores_update(void);
50 static void on_auth_ticket_recieved( void *result
, void *context
)
52 EncryptedAppTicketResponse_t
*response
= result
;
54 if( response
->m_eResult
== k_EResultOK
)
56 vg_info( " New app ticket ready\n" );
60 vg_warn( " Could not request new encrypted app ticket (%u)\n",
61 response
->m_eResult
);
64 if( SteamAPI_ISteamUser_GetEncryptedAppTicket( hSteamUser
,
66 vg_list_size(steam_app_ticket
),
67 &steam_app_ticket_length
))
69 vg_success( " Loaded app ticket (%u bytes)\n", steam_app_ticket_length
);
73 vg_error( " No ticket availible\n" );
74 steam_app_ticket_length
= 0;
78 static void request_auth_ticket(void)
81 * TODO Check for one thats cached on the disk and load it.
82 * This might be OK though because steam seems to cache the result
85 vg_info( "Requesting new authorization ticket\n" );
86 steam_async
*call
= steam_new_async();
88 call
->p_handler
= on_auth_ticket_recieved
;
89 call
->id
= SteamAPI_ISteamUser_RequestEncryptedAppTicket( hSteamUser
,
93 static void server_connect(void)
95 /* Connect to server if not connected */
97 SteamNetworkingIPAddr remoteAddr
;
101 SteamAPI_SteamNetworkingIPAddr_SetIPv6LocalHost( &remoteAddr
, 27402 );
103 const char *server_lon1
= "46.101.34.155:27402";
104 SteamAPI_SteamNetworkingIPAddr_ParseString( &remoteAddr
, server_lon1
);
108 SteamAPI_SteamNetworkingIPAddr_ToString( &remoteAddr
, buf
, 256, 1 );
109 vg_info( "connect to: %s\n", buf
);
111 cremote
= SteamAPI_ISteamNetworkingSockets_ConnectByIPAddress(
112 hSteamNetworkingSockets
, &remoteAddr
, 0, NULL
);
115 static void scores_update(void)
117 vg_log( "scores_update()\n" );
119 if( cremote_state
== k_ESteamNetworkingConnectionState_Connected
)
122 * request updated scores
124 netmsg_scores_request req
;
125 req
.inetmsg_id
= k_inetmsg_scores_request
;
127 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
128 hSteamNetworkingSockets
, cremote
, &req
,
129 sizeof(netmsg_scores_request
),
130 k_nSteamNetworkingSend_Reliable
, NULL
);
135 * if we are not connected, make a connection to the server and then in
136 * the future this function will be called again when it is connected
142 static void poll_connection(void)
144 SteamNetworkingMessage_t
*messages
[32];
149 len
= SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnConnection(
150 hSteamNetworkingSockets
, cremote
, messages
, vg_list_size(messages
));
155 for( int i
=0; i
<len
; i
++ )
157 SteamNetworkingMessage_t
*msg
= messages
[i
];
159 if( msg
->m_cbSize
< sizeof(netmsg_blank
) )
161 vg_warn( "Discarding message (too small: %d)\n",
166 netmsg_blank
*tmp
= msg
->m_pData
;
167 if( tmp
->inetmsg_id
== k_inetmsg_scores_info
)
169 netmsg_scores_info
*info
= msg
->m_pData
;
170 vg_log( "Recieved %u score records\n", info
->record_count
);
172 SteamAPI_ISteamNetworkingSockets_CloseConnection(
173 hSteamNetworkingSockets
, cremote
, 0, NULL
, 1 );
174 cremote_state
= k_ESteamNetworkingConnectionState_None
;
177 SteamAPI_SteamNetworkingMessage_t_Release( msg
);
182 static u64
in_server_ticks( double seconds
)
184 return (u64
)(seconds
/ 0.1);
187 static void on_server_connect_status( CallbackMsg_t
*msg
)
189 SteamNetConnectionStatusChangedCallback_t
*info
= (void *)msg
->m_pubParam
;
190 vg_info( " Connection status changed for %lu\n", info
->m_hConn
);
191 vg_info( " %s -> %s\n",
192 string_ESteamNetworkingConnectionState(info
->m_info
.m_eState
),
193 string_ESteamNetworkingConnectionState(info
->m_eOldState
) );
195 if( info
->m_hConn
== cremote
)
197 cremote_state
= info
->m_info
.m_eState
;
198 if( info
->m_info
.m_eState
==
199 k_ESteamNetworkingConnectionState_Connected
)
201 vg_success(" Connected to remote server\n");
207 vg_warn( " Recieved signal from unknown connection\n" );
211 static void network_init(void)
215 steam_register_callback( k_iSteamNetConnectionStatusChangedCallBack
,
216 on_server_connect_status
);
217 request_auth_ticket();
221 static void network_update(void)
225 static double last_update
= -9000.0;
228 if( vg_time
> (last_update
+ 60.0) )
230 last_update
= vg_time
;
236 static void network_end(void)
238 /* TODO: Fire off any buffered highscores that need to be setn */
242 #endif /* NETWORK_H */