#include "vg/vg_imgui.h"
#include "gui.h"
#include "ent_region.h"
+#include "vg/vg_loader.h"
+
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netdb.h>
struct network_client network_client =
{
.auth_mode = eServerModeAuthentication,
.state = k_ESteamNetworkingConnectionState_None,
- .server_adress = "46.101.34.155",
.last_intent_change = -99999.9
};
if( state == k_ESteamNetworkingConnectionState_None )
vg_strcat( str, "No Connection" );
- else if( state == k_ESteamNetworkingConnectionState_Connecting ){
- vg_strcat( str, "Connecting to:\nskaterift.com" );
+ else if( state == k_ESteamNetworkingConnectionState_Connecting )
+ {
+ vg_strcatf( str, "Connecting...\n%s", network_client.host_adress );
if( network_client.retries ){
vg_strcat( str, "\n(" );
}
}
else if( state == k_ESteamNetworkingConnectionState_Connected ){
- vg_strcat( str, "Connected to:\nskaterift.com" );
+ vg_strcatf( str, "Connected to:\n%s", network_client.host_adress );
*colour = 0xff00a020;
}
else if( state == k_ESteamNetworkingConnectionState_ClosedByPeer )
}
}
-static void network_connect(void){
- char ip_buf[128];
- vg_str str;
- vg_strnull( &str, ip_buf, sizeof(ip_buf) );
- vg_strcat( &str, network_client.server_adress );
- vg_strcat( &str, ":" );
- vg_strcati32( &str, NETWORK_PORT );
+void network_set_host( const char *host_str, const char *port_str )
+{
+ vg_strncpy( host_str, network_client.host_adress,
+ sizeof(network_client.host_adress), k_strncpy_overflow_fatal );
- if( !vg_strgood(&str) ) return;
+ memset( &network_client.ip, 0, sizeof(network_client.ip) );
+ network_client.ip_resolved = 0;
- /* Connect to server if not connected */
- SteamNetworkingIPAddr remoteAddr;
- SteamAPI_SteamNetworkingIPAddr_ParseString( &remoteAddr, str.buffer );
+ if( port_str )
+ {
+ vg_strncpy( port_str, network_client.host_port,
+ sizeof(network_client.host_port), k_strncpy_overflow_fatal );
+ }
+ else
+ {
+ vg_str str;
+ vg_strnull( &str, network_client.host_port,
+ sizeof(network_client.host_port) );
+ vg_strcati32( &str, NETWORK_PORT );
+ }
- char buf[256];
- SteamAPI_SteamNetworkingIPAddr_ToString( &remoteAddr, buf, 256, 1 );
- vg_info( "connect to: %s\n", buf );
+ network_client.ip.m_port = atoi( network_client.host_port );
+}
+
+static void network_connect(void)
+{
+ VG_ASSERT( network_client.ip_resolved );
+ vg_info( "connecting...\n" );
network_client.remote = SteamAPI_ISteamNetworkingSockets_ConnectByIPAddress(
- hSteamNetworkingSockets, &remoteAddr, 0, NULL );
+ hSteamNetworkingSockets, &network_client.ip, 0, NULL );
}
static void network_sign_on_complete(void){
}
}
+static void network_resolve_host_async( void *payload, u32 size )
+{
+ u32 *status = payload;
+ network_client.ip_resolved = *status;
+
+ char buf[256];
+ SteamAPI_SteamNetworkingIPAddr_ToString( &network_client.ip, buf, 256, 1 );
+ vg_info( "Resolved host address to: %s\n", buf );
+}
+
+static void network_resolve_host_thread( void *_ )
+{
+ vg_async_item *call = vg_async_alloc(8);
+ u32 *status = call->payload;
+ *status = 0;
+
+ if( (network_client.host_adress[0] >= '0') &&
+ (network_client.host_adress[0] <= '9') )
+ {
+ SteamAPI_SteamNetworkingIPAddr_ParseString(
+ &network_client.ip,
+ network_client.host_adress );
+ network_client.ip.m_port = atoi( network_client.host_port );
+ *status = 1;
+ goto end;
+ }
+
+ vg_info( "Resolving host.. %s (:%s)\n",
+ network_client.host_adress, network_client.host_port );
+
+ struct addrinfo hints;
+ struct addrinfo *result;
+
+ /* Obtain address(es) matching host/port. */
+
+ memset( &hints, 0, sizeof(hints) );
+ hints.ai_family = AF_INET6;
+ hints.ai_socktype = SOCK_DGRAM;
+ hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG;
+ hints.ai_protocol = 0;
+
+ int s = getaddrinfo( network_client.host_adress, network_client.host_port,
+ &hints, &result);
+ if( s != 0 )
+ {
+ vg_error( "getaddrinfo: %s\n", gai_strerror(s) );
+ goto end;
+ }
+
+ struct sockaddr_in6 *inaddr = (struct sockaddr_in6 *)result->ai_addr;
+ memcpy( network_client.ip.m_ipv6, &inaddr->sin6_addr, 16 );
+ freeaddrinfo( result );
+
+ *status = 1;
+
+end: vg_async_dispatch( call, network_resolve_host_async );
+}
+
void network_update(void)
{
if( !steam_ready )
ESteamNetworkingConnectionState state = network_client.state;
- if( network_client.user_intent == k_server_intent_offline ){
+ if( network_client.user_intent == k_server_intent_offline )
+ {
if( state != k_ESteamNetworkingConnectionState_None )
network_disconnect();
return;
}
- if( state == k_ESteamNetworkingConnectionState_Connected ){
+ if( state == k_ESteamNetworkingConnectionState_Connected )
+ {
poll_remote_connection();
f64 frame_delta = vg.time_real - network_client.last_frame;
- if( frame_delta > NETWORK_FRAMERATE ){
+ if( frame_delta > NETWORK_FRAMERATE )
+ {
network_client.last_frame = vg.time_real;
remote_player_send_playerframe();
localplayer.sfx_buffer_count = 0;
remote_player_debug_update();
}
- else {
+ else
+ {
if( (state == k_ESteamNetworkingConnectionState_Connecting) ||
- (state == k_ESteamNetworkingConnectionState_FindingRoute) ){
+ (state == k_ESteamNetworkingConnectionState_FindingRoute) )
+ {
return;
}
- else {
+ else
+ {
f64 waited = vg.time_real - network_client.last_attempt,
min_wait = 1.0;
if( waited < min_wait )
return;
+
+ if( !network_client.ip_resolved )
+ {
+ if( vg_loader_availible() )
+ {
+ vg_loader_start( network_resolve_host_thread, NULL );
+ }
+ else return;
+ }
+ else
+ network_connect();
- network_connect();
network_client.retries ++;
network_client.last_attempt = vg.time_real;
}
static int k_tools_mode = 0;
-int main( int argc, char *argv[] ){
+int main( int argc, char *argv[] )
+{
+ network_set_host( "skaterift.com", NULL );
vg_mem.use_libc_malloc = 0;
vg_set_mem_quota( 160*1024*1024 );
vg_enter( argc, argv, "Voyager Game Engine" );
network_client.auth_mode = eServerModeNoAuthentication;
}
- if( (arg = vg_long_opt_arg( "server" )) ){
- vg_strncpy( arg, network_client.server_adress, 64,
- k_strncpy_overflow_fatal );
+ if( (arg = vg_long_opt_arg( "server" )) )
+ {
+ network_set_host( arg, NULL );
}
if( vg_long_opt( "tools" ) ){
/* TODO: how can we compress this? */
ent_miniworld_preupdate();
world_entity_focus_preupdate();
- player__pre_update();
+
+ if( skaterift.activity != k_skaterift_menu )
+ {
+ player__pre_update();
+ }
+
skaterift_replay_pre_update();
remote_sfx_pre_update();
skateshop_world_preupdate( world_current_instance() );