sig_stop = 1;
}
+static void release_message( SteamNetworkingMessage_t *msg )
+{
+ msg->m_nUserData --;
+
+ if( msg->m_nUserData == 0 )
+ SteamAPI_SteamNetworkingMessage_t_Release( msg );
+}
+
/*
* Send message to single client, with authentication checking
*/
static void gameserver_send_to_client( i32 client_id,
const void *pData, u32 cbData,
- int nSendFlags ){
+ int nSendFlags )
+{
struct gameserver_client *client = &gameserver.clients[ client_id ];
+ if( gameserver.loopback_test && !client->connection )
+ return;
+
if( !client->steamid )
return;
*/
static void gameserver_send_to_all( int ignore,
const void *pData, u32 cbData,
- int nSendFlags ){
- for( int i=0; i<vg_list_size(gameserver.clients); i++ ){
+ int nSendFlags )
+{
+ for( int i=0; i<vg_list_size(gameserver.clients); i++ )
+ {
struct gameserver_client *client = &gameserver.clients[i];
if( i != ignore )
}
}
-static void gameserver_send_version_to_client( int index ){
+static void gameserver_send_version_to_client( int index )
+{
struct gameserver_client *client = &gameserver.clients[index];
+ if( gameserver.loopback_test && !client->connection )
+ return;
+
netmsg_version version;
version.inetmsg_id = k_inetmsg_version;
version.version = NETWORK_SKATERIFT_VERSION;
/*
* handle server update that client #'index' has joined
*/
-static void gameserver_player_join( int index ){
+static void gameserver_player_join( int index )
+{
struct gameserver_client *joiner = &gameserver.clients[index];
netmsg_playerjoin join = { .inetmsg_id = k_inetmsg_playerjoin,
netmsg_region *region = alloca( sizeof(netmsg_region) + NETWORK_REGION_MAX );
region->inetmsg_id = k_inetmsg_region;
- for( int i=0; i<vg_list_size(gameserver.clients); i++ ){
+ for( int i=0; i<vg_list_size(gameserver.clients); i++ )
+ {
struct gameserver_client *client = &gameserver.clients[i];
if( (i == index) || !client->steamid )
k_nSteamNetworkingSend_Reliable );
/* items */
- for( int j=0; j<k_netmsg_playeritem_max; j++ ){
+ for( int j=0; j<k_netmsg_playeritem_max; j++ )
+ {
chs = vg_strncpy( client->items[j].uid, item->uid, ADDON_UID_MAX,
k_strncpy_always_add_null );
item->type_index = j;
* Handle incoming new connection and init flags on the steam handle. if the
* server is full the userdata (client_id) will be set to -1 on the handle.
*/
-static void handle_new_connection( HSteamNetConnection conn ){
+static void handle_new_connection( HSteamNetConnection conn )
+{
SteamAPI_ISteamNetworkingSockets_SetConnectionUserData(
hSteamNetworkingSockets, conn, -1 );
EResult accept_status = SteamAPI_ISteamNetworkingSockets_AcceptConnection(
hSteamNetworkingSockets, conn );
- if( accept_status == k_EResultOK ){
+ if( accept_status == k_EResultOK )
+ {
vg_success( "Accepted client (id: %u, index: %d)\n", conn, index );
client->active = 1;
SteamAPI_ISteamNetworkingSockets_SetConnectionUserData(
hSteamNetworkingSockets, conn, index );
+
+ if( gameserver.loopback_test )
+ {
+ vg_warn( "[DEV] Creating loopback client\n" );
+ struct gameserver_client *loopback = &gameserver.clients[1];
+ loopback->active = 1;
+ loopback->connection = 0;
+ }
}
- else{
+ else
+ {
vg_warn( "Error accepting connection (id: %u)\n", conn );
SteamAPI_ISteamNetworkingSockets_CloseConnection(
hSteamNetworkingSockets, conn,
* Get client id of connection handle. Will be -1 if unkown to us either because
* the server is full or we already disconnected them
*/
-static i32 gameserver_conid( HSteamNetConnection hconn ){
- i64 id = SteamAPI_ISteamNetworkingSockets_GetConnectionUserData(
+static i32 gameserver_conid( HSteamNetConnection hconn )
+{
+ i64 id;
+
+ if( hconn == 0 )
+ {
+ if( gameserver.loopback_test )
+ return 1;
+ else
+ return -1;
+ }
+ else
+ id = SteamAPI_ISteamNetworkingSockets_GetConnectionUserData(
hSteamNetworkingSockets, hconn );
if( (id < 0) || (id >= NETWORK_MAX_PLAYERS) )
/*
* Callback for steam connection state change
*/
-static void on_connect_status( CallbackMsg_t *msg ){
+static void on_connect_status( CallbackMsg_t *msg )
+{
SteamNetConnectionStatusChangedCallback_t *info = (void *)msg->m_pubParam;
vg_info( " Connection status changed for %lu\n", info->m_hConn );
string_ESteamNetworkingConnectionState(info->m_eOldState),
string_ESteamNetworkingConnectionState(info->m_info.m_eState) );
- if( info->m_info.m_eState==k_ESteamNetworkingConnectionState_Connecting ){
+ if( info->m_info.m_eState==k_ESteamNetworkingConnectionState_Connecting )
+ {
handle_new_connection( info->m_hConn );
}
vg_info( "End reason: %d\n", info->m_info.m_eEndReason );
int client_id = gameserver_conid( info->m_hConn );
- if( client_id != -1 ){
+ if( client_id != -1 )
+ {
gameserver_player_leave( client_id );
remove_client( client_id );
+
+ if( gameserver.loopback_test )
+ {
+ gameserver_player_leave( 1 );
+ remove_client( 1 );
+ }
}
- else {
+ else
+ {
SteamAPI_ISteamNetworkingSockets_CloseConnection(
hSteamNetworkingSockets, info->m_hConn, 0, NULL, 0 );
}
}
}
-static void gameserver_rx_version( SteamNetworkingMessage_t *msg ){
+static void gameserver_rx_version( SteamNetworkingMessage_t *msg )
+{
netmsg_version *version = msg->m_pData;
int client_id = gameserver_conid( msg->m_conn );
- if( client_id == -1 ) {
+ if( client_id == -1 )
+ {
vg_warn( "Recieved version from unkown connection (%u)\n", msg->m_conn );
SteamAPI_ISteamNetworkingSockets_CloseConnection(
hSteamNetworkingSockets, msg->m_conn,
struct gameserver_client *client = &gameserver.clients[ client_id ];
- if( client->version ){
+ if( client->version )
+ {
vg_warn( "Already have version for this client (%d conn: %u)",
client_id, msg->m_conn );
return;
client->version = version->version;
- if( client->version != NETWORK_SKATERIFT_VERSION ){
+ if( client->version != NETWORK_SKATERIFT_VERSION )
+ {
gameserver_send_version_to_client( client_id );
remove_client( client_id );
return;
/* this is the sign on point for non-auth servers,
* for auth servers it comes at the end of rx_auth
*/
- if( gameserver.auth_mode != eServerModeAuthentication ){
+ if( gameserver.auth_mode != eServerModeAuthentication )
+ {
client->steamid = k_steamid_max;
gameserver_player_join( client_id );
+
+ if( gameserver.loopback_test )
+ {
+ struct gameserver_client *loopback = &gameserver.clients[1];
+ loopback->steamid = k_steamid_max;
+ gameserver_player_join( 1 );
+ }
}
}
struct gameserver_client *c0 = &gameserver.clients[client_id];
c0->instance = frame->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
- for( int i=0; i<vg_list_size(gameserver.clients); i++ ){
+ for( int i=0; i<vg_list_size(gameserver.clients); i++ )
+ {
if( i == client_id )
continue;
int send_full = 0;
- if( c0->instance == ci->instance ){
+ if( c0->instance == ci->instance )
+ {
u32 k_index = network_pair_index( client_id, i );
u8 k_mask = gameserver.client_knowledge_mask[ k_index ];
send_full = 1;
}
- if( send_full ){
+ if( send_full )
+ {
gameserver_send_to_client( i, full, size,
k_nSteamNetworkingSend_Unreliable );
}
- else {
+ else
+ {
gameserver_send_to_client( i, basic, basic_size,
k_nSteamNetworkingSend_Unreliable );
}
}
}
-static void gameserver_rx_200_300( SteamNetworkingMessage_t *msg ){
+static void gameserver_rx_200_300( SteamNetworkingMessage_t *msg )
+{
netmsg_blank *tmp = msg->m_pData;
int client_id = gameserver_conid( msg->m_conn );
struct gameserver_client *client = &gameserver.clients[ client_id ];
- if( tmp->inetmsg_id == k_inetmsg_playerusername ){
+ if( tmp->inetmsg_id == k_inetmsg_playerusername )
+ {
if( !packet_minsize( msg, sizeof(netmsg_playerusername)+1 ))
return;
call->handler = gameserver_update_db_username;
db_send_request( call );
}
- else if( tmp->inetmsg_id == k_inetmsg_playerframe ){
+ else if( tmp->inetmsg_id == k_inetmsg_playerframe )
+ {
gameserver_propogate_player_frame( client_id,
msg->m_pData, msg->m_cbSize );
}
- else if( tmp->inetmsg_id == k_inetmsg_playeritem ){
+ else if( tmp->inetmsg_id == k_inetmsg_playeritem )
+ {
netmsg_playeritem *item = msg->m_pData;
/* record */
-
- if( item->type_index >= k_netmsg_playeritem_max ){
+ if( item->type_index >= k_netmsg_playeritem_max )
+ {
vg_warn( "Client #%d invalid equip type %u\n",
client_id, (u32)item->type_index );
return;
gameserver_send_to_all( client_id, prop, msg->m_cbSize,
k_nSteamNetworkingSend_Reliable );
}
- else if( tmp->inetmsg_id == k_inetmsg_chat ){
+ else if( tmp->inetmsg_id == k_inetmsg_chat )
+ {
netmsg_chat *chat = msg->m_pData,
*prop = alloca( sizeof(netmsg_chat) + NETWORK_MAX_CHAT );
prop->inetmsg_id = k_inetmsg_chat;
gameserver_send_to_all( client_id, prop, sizeof(netmsg_chat)+l+1,
k_nSteamNetworkingSend_Reliable );
}
- else if( tmp->inetmsg_id == k_inetmsg_region ){
+ else if( tmp->inetmsg_id == k_inetmsg_region )
+ {
netmsg_region *region = msg->m_pData,
*prop = alloca( sizeof(netmsg_region) + NETWORK_REGION_MAX );
k_nSteamNetworkingSend_Reliable );
vg_info( "client %d moved to region: %s\n", client_id, client->region );
}
- else {
+ else
+ {
vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
tmp->inetmsg_id );
}
res->status = status;
+ if( gameserver.loopback_test && !msg->m_conn )
+ {
+ release_message( msg );
+ return;
+ }
+
SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
hSteamNetworkingSockets, msg->m_conn,
res, sizeof(netmsg_request) + len,
k_nSteamNetworkingSend_Reliable, NULL );
- SteamAPI_SteamNetworkingMessage_t_Release( msg );
+
+ release_message( msg );
}
struct user_request_thread_data {
return k_request_status_ok;
}
-static void gameserver_process_user_request( db_request *db_req ){
+static void gameserver_process_user_request( db_request *db_req )
+{
struct user_request_thread_data *inf = (void *)db_req->data;
SteamNetworkingMessage_t *msg = inf->msg;
int client_id = gameserver_conid( msg->m_conn );
- if( client_id == -1 ){
- SteamAPI_SteamNetworkingMessage_t_Release( msg );
+ if( client_id == -1 )
+ {
+ release_message( msg );
return;
}
}
}
-static void gameserver_rx_300_400( SteamNetworkingMessage_t *msg ){
+static void gameserver_rx_300_400( SteamNetworkingMessage_t *msg )
+{
netmsg_blank *tmp = msg->m_pData;
int client_id = gameserver_conid( msg->m_conn );
- if( client_id == -1 ){
- SteamAPI_SteamNetworkingMessage_t_Release( msg );
+ if( client_id == -1 )
+ {
+ release_message( msg );
return;
}
- if( tmp->inetmsg_id == k_inetmsg_request ){
+ if( tmp->inetmsg_id == k_inetmsg_request )
+ {
+ if( gameserver.loopback_test && (client_id == 1) )
+ {
+ release_message( msg );
+ return;
+ }
+
if( !packet_minsize( msg, sizeof(netmsg_request)+1 ))
+ {
+ release_message( msg );
return;
+ }
db_request *call = db_alloc_request(
sizeof(struct user_request_thread_data) );
call->handler = gameserver_process_user_request;
db_send_request( call );
}
- else {
+ else
+ {
vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
tmp->inetmsg_id );
- SteamAPI_SteamNetworkingMessage_t_Release( msg );
+ release_message( msg );
+ }
+}
+
+static void process_network_message( SteamNetworkingMessage_t *msg )
+{
+ if( msg->m_cbSize < sizeof(netmsg_blank) ){
+ vg_warn( "Discarding message (too small: %d)\n",
+ msg->m_cbSize );
+ return;
+ }
+
+ netmsg_blank *tmp = msg->m_pData;
+
+ if( (tmp->inetmsg_id >= 200) && (tmp->inetmsg_id < 300) )
+ {
+ gameserver_rx_200_300( msg );
+ release_message( msg );
+ }
+ else if( (tmp->inetmsg_id >= 300) && (tmp->inetmsg_id < 400) )
+ {
+ gameserver_rx_300_400( msg );
+ }
+ else{
+ if( tmp->inetmsg_id == k_inetmsg_auth )
+ gameserver_rx_auth( msg );
+ else if( tmp->inetmsg_id == k_inetmsg_version ){
+ gameserver_rx_version( msg );
+ }
+ else {
+ vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
+ tmp->inetmsg_id );
+ }
+ release_message( msg );
}
}
-static void poll_connections(void){
+static void poll_connections(void)
+{
SteamNetworkingMessage_t *messages[32];
int len;
- while(1){
+ while(1)
+ {
len = SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnPollGroup(
hSteamNetworkingSockets,
gameserver.client_group, messages, vg_list_size(messages) );
if( len <= 0 )
return;
- for( int i=0; i<len; i++ ){
+ for( int i=0; i<len; i++ )
+ {
SteamNetworkingMessage_t *msg = messages[i];
-
- if( msg->m_cbSize < sizeof(netmsg_blank) ){
- vg_warn( "Discarding message (too small: %d)\n",
- msg->m_cbSize );
- continue;
+ msg->m_nUserData = 1;
+
+ if( gameserver.loopback_test )
+ {
+ HSteamNetConnection conid = msg->m_conn;
+ msg->m_conn = 0;
+ msg->m_nUserData ++;
+ process_network_message( msg );
+ msg->m_conn = conid;
}
- netmsg_blank *tmp = msg->m_pData;
-
- if( (tmp->inetmsg_id >= 200) && (tmp->inetmsg_id < 300) ){
- gameserver_rx_200_300( msg );
- SteamAPI_SteamNetworkingMessage_t_Release( msg );
- }
- else if( (tmp->inetmsg_id >= 300) && (tmp->inetmsg_id < 400) ){
- gameserver_rx_300_400( msg );
- }
- else{
- if( tmp->inetmsg_id == k_inetmsg_auth )
- gameserver_rx_auth( msg );
- else if( tmp->inetmsg_id == k_inetmsg_version ){
- gameserver_rx_version( msg );
- }
- else {
- vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
- tmp->inetmsg_id );
- }
- SteamAPI_SteamNetworkingMessage_t_Release( msg );
- }
+ process_network_message( msg );
}
}
}
signal( SIGPIPE, SIG_IGN );
char *arg;
- while( vg_argp( argc, argv ) ){
+ while( vg_argp( argc, argv ) )
+ {
if( vg_long_opt( "noauth" ) )
gameserver.auth_mode = eServerModeNoAuthentication;
+
+ if( vg_long_opt( "loopback" ) )
+ gameserver.loopback_test = 1;
}
vg_set_mem_quota( 80*1024*1024 );