X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;ds=sidebyside;f=gameserver.c;h=e315fd0e69b7c096cbadd1d8b6d6abca6df12345;hb=HEAD;hp=11721e7a4dc8abef2f45db49765d58ea1246dc1b;hpb=3b1909cad505b859b9c6524498b1969cd018af8c;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/gameserver.c b/gameserver.c deleted file mode 100644 index 11721e7..0000000 --- a/gameserver.c +++ /dev/null @@ -1,1045 +0,0 @@ -/* - * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved - */ - -#define _DEFAULT_SOURCE -#include -#include -#include - -volatile sig_atomic_t sig_stop; - -#include "gameserver.h" -#include "highscores.c" -#include "servermonitor_server.c" -#include "vg/vg_opt.h" -#include "network_common.h" -#include "gameserver_db.h" -#include "vg/vg_m.h" -#include "vg/vg_msg.h" - -static u64 const k_steamid_max = 0xffffffffffffffff; - -static void inthandler( int signum ) { - sig_stop = 1; -} - -/* - * Send message to single client, with authentication checking - */ -static void gameserver_send_to_client( i32 client_id, - const void *pData, u32 cbData, - int nSendFlags ){ - struct gameserver_client *client = &gameserver.clients[ client_id ]; - - if( !client->steamid ) - return; - - SteamAPI_ISteamNetworkingSockets_SendMessageToConnection( - hSteamNetworkingSockets, client->connection, - pData, cbData, nSendFlags, NULL ); -} - -/* - * Send message to all clients if they are authenticated - */ -static void gameserver_send_to_all( int ignore, - const void *pData, u32 cbData, - int nSendFlags ){ - for( int i=0; iconnection, - &version, sizeof(netmsg_version), - k_nSteamNetworkingSend_Reliable, NULL ); -} - -/* - * handle server update that client #'index' has joined - */ -static void gameserver_player_join( int index ){ - struct gameserver_client *joiner = &gameserver.clients[index]; - - netmsg_playerjoin join = { .inetmsg_id = k_inetmsg_playerjoin, - .index = index, - .steamid = joiner->steamid }; - - gameserver_send_to_all( index, &join, sizeof(join), - k_nSteamNetworkingSend_Reliable ); - - /* - * update the joining user about current connections and our version - */ - gameserver_send_version_to_client( index ); - - netmsg_playerusername *username = - alloca( sizeof(netmsg_playerusername) + NETWORK_USERNAME_MAX ); - username->inetmsg_id = k_inetmsg_playerusername; - - netmsg_playeritem *item = - alloca( sizeof(netmsg_playeritem) + ADDON_UID_MAX ); - item->inetmsg_id = k_inetmsg_playeritem; - - netmsg_region *region = alloca( sizeof(netmsg_region) + NETWORK_REGION_MAX ); - region->inetmsg_id = k_inetmsg_region; - - for( int i=0; isteamid ) - continue; - - /* join */ - netmsg_playerjoin init = { .inetmsg_id = k_inetmsg_playerjoin, - .index = i, - .steamid = client->steamid }; - gameserver_send_to_client( index, &init, sizeof(init), - k_nSteamNetworkingSend_Reliable ); - - /* username */ - username->index = i; - u32 chs = vg_strncpy( client->username, username->name, - NETWORK_USERNAME_MAX, - k_strncpy_always_add_null ); - u32 size = sizeof(netmsg_playerusername) + chs + 1; - gameserver_send_to_client( index, username, size, - k_nSteamNetworkingSend_Reliable ); - - /* items */ - for( int j=0; jitems[j].uid, item->uid, ADDON_UID_MAX, - k_strncpy_always_add_null ); - item->type_index = j; - item->client = i; - size = sizeof(netmsg_playeritem) + chs + 1; - gameserver_send_to_client( index, item, size, - k_nSteamNetworkingSend_Reliable ); - } - - /* region */ - - region->client = i; - region->flags = client->region_flags; - u32 l = vg_strncpy( client->region, region->loc, NETWORK_REGION_MAX, - k_strncpy_always_add_null ); - size = sizeof(netmsg_region) + l + 1; - - gameserver_send_to_client( index, region, size, - k_nSteamNetworkingSend_Reliable ); - } -} - -/* - * Handle server update that player has left - */ -static void gameserver_player_leave( int index ){ - if( gameserver.auth_mode == eServerModeAuthentication ){ - if( !gameserver.clients[ index ].steamid ) - return; - } - - netmsg_playerleave leave; - leave.inetmsg_id = k_inetmsg_playerleave; - leave.index = index; - - vg_info( "Player leave (%d)\n", index ); - gameserver_send_to_all( index, &leave, sizeof(leave), - k_nSteamNetworkingSend_Reliable ); -} - -static void gameserver_update_all_knowledge( int client, int clear ); - -/* - * Deletes client at index and disconnects the connection handle if it was - * set. - */ -static void remove_client( int index ){ - struct gameserver_client *client = &gameserver.clients[index]; - if( client->connection ){ - SteamAPI_ISteamNetworkingSockets_SetConnectionUserData( - hSteamNetworkingSockets, client->connection, -1 ); - SteamAPI_ISteamNetworkingSockets_CloseConnection( - hSteamNetworkingSockets, client->connection, - k_ESteamNetConnectionEnd_Misc_InternalError, - NULL, 1 ); - } - memset( client, 0, sizeof(struct gameserver_client) ); - gameserver_update_all_knowledge( index, 1 ); -} - -/* - * 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 ){ - SteamAPI_ISteamNetworkingSockets_SetConnectionUserData( - hSteamNetworkingSockets, conn, -1 ); - - int index = -1; - - for( int i=0; iactive = 1; - client->connection = conn; - - SteamAPI_ISteamNetworkingSockets_SetConnectionPollGroup( - hSteamNetworkingSockets, conn, gameserver.client_group ); - - SteamAPI_ISteamNetworkingSockets_SetConnectionUserData( - hSteamNetworkingSockets, conn, index ); - } - else{ - vg_warn( "Error accepting connection (id: %u)\n", conn ); - SteamAPI_ISteamNetworkingSockets_CloseConnection( - hSteamNetworkingSockets, conn, - k_ESteamNetConnectionEnd_Misc_InternalError, - NULL, 1 ); - } -} - -static void on_auth_status( CallbackMsg_t *msg ){ - SteamNetAuthenticationStatus_t *info = (void *)msg->m_pubParam; - vg_info( " Authentication availibility: %s\n", - string_ESteamNetworkingAvailability(info->m_eAvail) ); - vg_info( " %s\n", info->m_debugMsg ); -} - -/* - * 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( - hSteamNetworkingSockets, hconn ); - - if( (id < 0) || (id >= NETWORK_MAX_PLAYERS) ) - return -1; - - return id; -} - -/* - * Callback for steam connection state change - */ -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 ); - - vg_info( " %s -> %s\n", - string_ESteamNetworkingConnectionState(info->m_eOldState), - string_ESteamNetworkingConnectionState(info->m_info.m_eState) ); - - if( info->m_info.m_eState==k_ESteamNetworkingConnectionState_Connecting ){ - handle_new_connection( info->m_hConn ); - } - - if( (info->m_info.m_eState == - k_ESteamNetworkingConnectionState_ClosedByPeer ) || - (info->m_info.m_eState == - k_ESteamNetworkingConnectionState_ProblemDetectedLocally ) || - (info->m_info.m_eState == - k_ESteamNetworkingConnectionState_Dead) || - (info->m_info.m_eState == - k_ESteamNetworkingConnectionState_None) ) - { - vg_info( "End reason: %d\n", info->m_info.m_eEndReason ); - - int client_id = gameserver_conid( info->m_hConn ); - if( client_id != -1 ){ - gameserver_player_leave( client_id ); - remove_client( client_id ); - } - else { - SteamAPI_ISteamNetworkingSockets_CloseConnection( - hSteamNetworkingSockets, info->m_hConn, 0, NULL, 0 ); - } - } -} - -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 ) { - vg_warn( "Recieved version from unkown connection (%u)\n", msg->m_conn ); - SteamAPI_ISteamNetworkingSockets_CloseConnection( - hSteamNetworkingSockets, msg->m_conn, - k_ESteamNetConnectionEnd_Misc_InternalError, - NULL, 1 ); - return; - } - - struct gameserver_client *client = &gameserver.clients[ client_id ]; - - 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 ){ - 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 ){ - client->steamid = k_steamid_max; - gameserver_player_join( client_id ); - } -} - -/* - * recieve auth ticket from connection. will only accept it if we've added them - * to the client list first. - */ -static void gameserver_rx_auth( SteamNetworkingMessage_t *msg ){ - if( gameserver.auth_mode != eServerModeAuthentication ){ - vg_warn( "Running server without authentication. " - "Connection %u tried to authenticate.\n", msg->m_conn ); - return; - } - - int client_id = gameserver_conid( msg->m_conn ); - if( client_id == -1 ) { - vg_warn( "Recieved auth ticket from unkown connection (%u)\n", - msg->m_conn ); - SteamAPI_ISteamNetworkingSockets_CloseConnection( - hSteamNetworkingSockets, msg->m_conn, - k_ESteamNetConnectionEnd_Misc_InternalError, NULL, 1 ); - return; - } - - struct gameserver_client *client = &gameserver.clients[ client_id ]; - if( client->steamid ){ - vg_warn( "Already authorized this user but another app ticket was sent" - " again (%d conn: %u)\n", client_id, msg->m_conn ); - return; - } - - if( client->version == 0 ){ - vg_error( "Client has not sent their version yet (%u)\n", msg->m_conn ); - remove_client( client_id ); - return; - } - - vg_low( "Attempting to verify user\n" ); - - if( msg->m_cbSize < sizeof(netmsg_auth) ){ - vg_error( "Malformed auth ticket, too small (%u)\n", msg->m_conn ); - remove_client( client_id ); - return; - } - - netmsg_auth *auth = msg->m_pData; - - if( msg->m_cbSize < sizeof(netmsg_auth)+auth->ticket_length || - auth->ticket_length > 1024 ){ - vg_error( "Malformed auth ticket, ticket_length incorrect (%u)\n", - auth->ticket_length ); - remove_client( client_id ); - return; - } - - u8 decrypted[1024]; - u32 ticket_len = 1024; - - int success = SteamEncryptedAppTicket_BDecryptTicket( - auth->ticket, auth->ticket_length, decrypted, - &ticket_len, gameserver.app_symmetric_key, - k_nSteamEncryptedAppTicketSymmetricKeyLen ); - - if( !success ){ - vg_error( "Failed to decrypt users ticket (client %u)\n", msg->m_conn ); - vg_error( " ticket length: %u\n", auth->ticket_length ); - remove_client( client_id ); - return; - } - - if( SteamEncryptedAppTicket_GetTicketIssueTime( decrypted, ticket_len )){ - RTime32 ctime = time(NULL), - tickettime = SteamEncryptedAppTicket_GetTicketIssueTime( - decrypted, ticket_len ), - expiretime = tickettime + 24*3*60*60; - - if( ctime > expiretime ){ - vg_error( "Ticket expired (client %u)\n", msg->m_conn ); - remove_client( client_id ); - return; - } - } - - CSteamID steamid; - SteamEncryptedAppTicket_GetTicketSteamID( decrypted, ticket_len, &steamid ); - vg_success( "User is authenticated! steamid %lu (%u)\n", - steamid.m_unAll64Bits, msg->m_conn ); - - client->steamid = steamid.m_unAll64Bits; - gameserver_player_join( client_id ); -} - -/* - * Player updates sent to us - * ----------------------------------------------------------------------------- - */ - -static int packet_minsize( SteamNetworkingMessage_t *msg, u32 size ){ - if( msg->m_cbSize < size ) { - vg_error( "Invalid packet size (must be at least %u)\n", size ); - return 0; - } - else{ - return 1; - } -} - -struct db_set_username_thread_data { - u64 steamid; - char username[ NETWORK_USERNAME_MAX ]; -}; - -static void gameserver_update_db_username( db_request *db_req ){ - struct db_set_username_thread_data *inf = (void *)db_req->data; - - if( inf->steamid == k_steamid_max ) - return; - - int admin = 0; - if( inf->steamid == 76561198072130043 ) - admin = 2; - - db_updateuser( inf->steamid, inf->username, admin ); -} - -static int gameserver_item_eq( struct gameserver_item *ia, - struct gameserver_item *ib ){ - if( ia->hash == ib->hash ) - if( !strcmp(ia->uid,ib->uid) ) - return 1; - - return 0; -} - -/* - * Match addons between two player IDs. if clear is set, then the flags between - * those two IDs will all be set to 0. - */ -static void gameserver_update_knowledge_table( int client0, int client1, - int clear ){ - u32 idx = network_pair_index( client0, client1 ); - - struct gameserver_client *c0 = &gameserver.clients[client0], - *c1 = &gameserver.clients[client1]; - - u8 flags = 0x00; - - if( !clear ){ - if( gameserver_item_eq(&c0->items[k_netmsg_playeritem_world0], - &c1->items[k_netmsg_playeritem_world0])) - flags |= CLIENT_KNOWLEDGE_SAME_WORLD0; - - if( gameserver_item_eq(&c0->items[k_netmsg_playeritem_world1], - &c1->items[k_netmsg_playeritem_world1])) - flags |= CLIENT_KNOWLEDGE_SAME_WORLD1; - } - - gameserver.client_knowledge_mask[idx] = flags; -} - -/* - * If a change has been made on this client, then it will adjust the entire - * table of other players. if clear is set, all references to client will be set - * to 0. - */ -static void gameserver_update_all_knowledge( int client, int clear ){ - for( int i=0; isteamid ) - gameserver_update_knowledge_table( client, i, clear ); - } -} - -static void gameserver_propogate_player_frame( int client_id, - netmsg_playerframe *frame, - u32 size ){ - u32 basic_size = sizeof(netmsg_playerframe) + ((24*3)/8); - netmsg_playerframe *full = alloca(size), - *basic= alloca(basic_size); - - memcpy( full, frame, size ); - memcpy( basic, frame, basic_size ); - - full->client = client_id; - basic->client = client_id; - basic->subsystem = 4; /* (.._basic_info: 24f*3 animator ) */ - basic->sound_effects = 0; - - struct gameserver_client *c0 = &gameserver.clients[client_id]; - c0->instance = frame->instance_id; - - for( int i=0; iinstance == ci->instance ){ - u32 k_index = network_pair_index( client_id, i ); - u8 k_mask = gameserver.client_knowledge_mask[ k_index ]; - - if( (k_mask & (CLIENT_KNOWLEDGE_SAME_WORLD0<instance)) ) - send_full = 1; - } - - if( send_full ){ - gameserver_send_to_client( i, full, size, - k_nSteamNetworkingSend_Unreliable ); - } - else { - gameserver_send_to_client( i, basic, basic_size, - k_nSteamNetworkingSend_Unreliable ); - } - } -} - -static void gameserver_rx_200_300( SteamNetworkingMessage_t *msg ){ - netmsg_blank *tmp = msg->m_pData; - - int client_id = gameserver_conid( msg->m_conn ); - if( client_id == -1 ) return; - - struct gameserver_client *client = &gameserver.clients[ client_id ]; - - if( tmp->inetmsg_id == k_inetmsg_playerusername ){ - if( !packet_minsize( msg, sizeof(netmsg_playerusername)+1 )) - return; - - netmsg_playerusername *src = msg->m_pData; - - u32 name_len = network_msgstring( src->name, msg->m_cbSize, - sizeof(netmsg_playerusername), - client->username, - NETWORK_USERNAME_MAX ); - - /* update other users about this change */ - netmsg_playerusername *prop = alloca(sizeof(netmsg_playerusername)+ - NETWORK_USERNAME_MAX ); - - prop->inetmsg_id = k_inetmsg_playerusername; - prop->index = client_id; - u32 chs = vg_strncpy( client->username, prop->name, NETWORK_USERNAME_MAX, - k_strncpy_always_add_null ); - - vg_info( "client #%d changed name to: %s\n", client_id, prop->name ); - - u32 propsize = sizeof(netmsg_playerusername) + chs + 1; - gameserver_send_to_all( client_id, prop, propsize, - k_nSteamNetworkingSend_Reliable ); - - /* update database about this */ - db_request *call = db_alloc_request( - sizeof(struct db_set_username_thread_data) ); - struct db_set_username_thread_data *inf = (void *)call->data; - inf->steamid = client->steamid; - vg_strncpy( client->username, inf->username, - sizeof(inf->username), k_strncpy_always_add_null ); - call->handler = gameserver_update_db_username; - db_send_request( call ); - } - 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 ){ - netmsg_playeritem *item = msg->m_pData; - - /* record */ - - if( item->type_index >= k_netmsg_playeritem_max ){ - vg_warn( "Client #%d invalid equip type %u\n", - client_id, (u32)item->type_index ); - return; - } - - char *dest = client->items[ item->type_index ].uid; - - network_msgstring( item->uid, msg->m_cbSize, sizeof(netmsg_playeritem), - dest, ADDON_UID_MAX ); - - vg_info( "Client #%d equiped: [%s] %s\n", - client_id, - (const char *[]){[k_netmsg_playeritem_board]="board", - [k_netmsg_playeritem_player]="player", - [k_netmsg_playeritem_world0]="world0", - [k_netmsg_playeritem_world1]="world1" - }[item->type_index], item->uid ); - - gameserver_update_all_knowledge( client_id, 0 ); - - /* propogate */ - netmsg_playeritem *prop = alloca(msg->m_cbSize); - memcpy( prop, msg->m_pData, msg->m_cbSize ); - prop->client = client_id; - gameserver_send_to_all( client_id, prop, msg->m_cbSize, - k_nSteamNetworkingSend_Reliable ); - } - 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; - prop->client = client_id; - - u32 l = network_msgstring( chat->msg, msg->m_cbSize, sizeof(netmsg_chat), - prop->msg, NETWORK_MAX_CHAT ); - vg_info( "[%d]: %s\n", client_id, prop->msg ); - - gameserver_send_to_all( client_id, prop, sizeof(netmsg_chat)+l+1, - k_nSteamNetworkingSend_Reliable ); - } - else if( tmp->inetmsg_id == k_inetmsg_region ){ - netmsg_region *region = msg->m_pData, - *prop = alloca( sizeof(netmsg_region) + NETWORK_REGION_MAX ); - - prop->inetmsg_id = k_inetmsg_region; - prop->client = client_id; - prop->flags = region->flags; - - u32 l = network_msgstring( - region->loc, msg->m_cbSize, sizeof(netmsg_region), - client->region, NETWORK_REGION_MAX ); - client->region_flags = region->flags; - - l = vg_strncpy( client->region, prop->loc, NETWORK_REGION_MAX, - k_strncpy_always_add_null ); - - gameserver_send_to_all( client_id, prop, sizeof(netmsg_region)+l+1, - k_nSteamNetworkingSend_Reliable ); - vg_info( "client %d moved to region: %s\n", client_id, client->region ); - } - else { - vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n", - tmp->inetmsg_id ); - } -} - -static void gameserver_request_respond( enum request_status status, - netmsg_request *res, vg_msg *body, - SteamNetworkingMessage_t *msg ){ - int client_id = gameserver_conid( msg->m_conn ); - u32 len = 0; - if( body ){ - len = body->cur.co; - vg_low( "[%d#%d] Response: %d\n", client_id, (i32)res->id, status ); - vg_msg_print( body, len ); - } - - res->status = status; - - SteamAPI_ISteamNetworkingSockets_SendMessageToConnection( - hSteamNetworkingSockets, msg->m_conn, - res, sizeof(netmsg_request) + len, - k_nSteamNetworkingSend_Reliable, NULL ); - SteamAPI_SteamNetworkingMessage_t_Release( msg ); -} - -struct user_request_thread_data { - SteamNetworkingMessage_t *msg; -}; - -static u32 gameserver_get_current_week(void){ - return time(NULL) / (7*24*60*60); -} - -static enum request_status gameserver_cat_table( - vg_msg *msg, - const char *mod, const char *route, u32 week, const char *alias ) -{ - char table_name[ DB_TABLE_UID_MAX ]; - if( !db_get_highscore_table_name( mod, route, week, table_name ) ) - return k_request_status_out_of_memory; - - char buf[512]; - vg_str q; - vg_strnull( &q, buf, 512 ); - vg_strcat( &q, "SELECT * FROM \"" ); - vg_strcat( &q, table_name ); - vg_strcat( &q, "\" ORDER BY time DESC LIMIT 10;" ); - if( !vg_strgood(&q) ) - return k_request_status_out_of_memory; - - sqlite3_stmt *stmt = db_stmt( q.buffer ); - if( !stmt ) - return k_request_status_database_error; - - vg_msg_frame( msg, alias ); - for( u32 i=0; i<10; i ++ ){ - int fc = sqlite3_step( stmt ); - - if( fc == SQLITE_ROW ){ - i32 time = sqlite3_column_int( stmt, 1 ); - i64 steamid_i64 = sqlite3_column_int64( stmt, 0 ); - u64 steamid = *((u64 *)&steamid_i64); - - if( steamid == k_steamid_max ) - continue; - - vg_msg_frame( msg, "" ); - vg_msg_wkvu32( msg, "time", time ); - vg_msg_wkvu64( msg, "steamid", steamid ); - - char username[32]; - if( db_getuserinfo( steamid, username, sizeof(username), NULL ) ) - vg_msg_wkvstr( msg, "username", username ); - vg_msg_end_frame( msg ); - } - else if( fc == SQLITE_DONE ){ - break; - } - else { - log_sqlite3( fc ); - break; - } - } - - sqlite3_finalize( stmt ); - vg_msg_end_frame( msg ); - return k_request_status_ok; -} - -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 ); - return; - } - - struct gameserver_client *client = &gameserver.clients[ client_id ]; - - netmsg_request *req = (netmsg_request *)msg->m_pData; - vg_msg data; - vg_msg_init( &data, req->q, msg->m_cbSize - sizeof(netmsg_request) ); - - /* create response packet */ - netmsg_request *res = alloca( sizeof(netmsg_request) + NETWORK_REQUEST_MAX ); - res->inetmsg_id = k_inetmsg_response; - res->id = req->id; - vg_msg body; - vg_msg_init( &body, res->q, NETWORK_REQUEST_MAX ); - - const char *endpoint = vg_msg_getkvstr( &data, "endpoint" ); - - if( !endpoint ){ - gameserver_request_respond( k_request_status_invalid_endpoint, - res, NULL, msg ); - return; - } - - if( !strcmp( endpoint, "scoreboard" ) ){ - const char *mod = vg_msg_getkvstr( &data, "mod" ); - const char *route = vg_msg_getkvstr( &data, "route" ); - u32 week = vg_msg_getkvu32( &data, "week", 0 ); - - if( week == NETWORK_LEADERBOARD_CURRENT_WEEK ){ - gameserver_cat_table( &body, mod, route, - gameserver_get_current_week(), "rows_weekly" ); - } - else if( week == NETWORK_LEADERBOARD_ALLTIME_AND_CURRENT_WEEK ){ - gameserver_cat_table( &body, mod, route, 0, "rows" ); - gameserver_cat_table( &body, mod, route, - gameserver_get_current_week(), "rows_weekly" ); - } - else - gameserver_cat_table( &body, mod, route, week, "rows" ); - - if( body.error != k_vg_msg_error_OK ){ - gameserver_request_respond( k_request_status_out_of_memory, - res, NULL, msg ); - return; - } - - gameserver_request_respond( k_request_status_ok, res, &body, msg ); - } - else if( !strcmp( endpoint, "setlap" ) ){ - if( client->steamid == k_steamid_max ){ - gameserver_request_respond( k_request_status_unauthorized, - res, NULL, msg ); - return; - } - - const char *mod = vg_msg_getkvstr( &data, "mod" ); - const char *route = vg_msg_getkvstr( &data, "route" ); - - char weekly_table[ DB_TABLE_UID_MAX ], - alltime_table[ DB_TABLE_UID_MAX ]; - - u32 week = gameserver_get_current_week(); - - if( !db_get_highscore_table_name( mod, route, 0, alltime_table ) || - !db_get_highscore_table_name( mod, route, week, weekly_table ) ){ - gameserver_request_respond( k_request_status_out_of_memory, - res, NULL, msg ); - return; - } - - i32 centiseconds = vg_msg_getkvi32( &data, "time", -1 ); - if( centiseconds < 5*100 ){ - gameserver_request_respond( k_request_status_client_error, - res, NULL, msg ); - return; - } - - db_writeusertime( alltime_table, client->steamid, centiseconds, 1 ); - db_writeusertime( weekly_table, client->steamid, centiseconds, 1 ); - - gameserver_request_respond( k_request_status_ok, res, NULL, msg ); - } - else{ - gameserver_request_respond( k_request_status_invalid_endpoint, - res, NULL, 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 ); - return; - } - - if( tmp->inetmsg_id == k_inetmsg_request ){ - if( !packet_minsize( msg, sizeof(netmsg_request)+1 )) - return; - - db_request *call = db_alloc_request( - sizeof(struct user_request_thread_data) ); - struct user_request_thread_data *inf = (void *)call->data; - inf->msg = msg; - call->handler = gameserver_process_user_request; - db_send_request( call ); - } - else { - vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n", - tmp->inetmsg_id ); - SteamAPI_SteamNetworkingMessage_t_Release( msg ); - } -} - -static void poll_connections(void){ - SteamNetworkingMessage_t *messages[32]; - int len; - - while(1){ - len = SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnPollGroup( - hSteamNetworkingSockets, - gameserver.client_group, messages, vg_list_size(messages) ); - - if( len <= 0 ) - return; - - for( int i=0; im_cbSize < sizeof(netmsg_blank) ){ - vg_warn( "Discarding message (too small: %d)\n", - msg->m_cbSize ); - continue; - } - - 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 ); - } - } - } -} - -static u64 seconds_to_server_ticks( double s ){ - return s / 0.01; -} - -static void test_runner( db_request *req ){ - vg_warn( "RUNNER\n" ); - char table[DB_TABLE_UID_MAX]; - if( db_get_highscore_table_name( "sr002-local-mp_mtzero", - "Coastal Run", 0, table ) ){ - if( db_writeusertime( table, 76561198072130043, 232, 1 ) ){ - vg_success( "Written time\n" ); - i32 v = db_readusertime( table, 76561198072130043 ); - vg_success( "Returned time: %u\n", v ); - } - } -} - -int main( int argc, char *argv[] ){ - signal( SIGINT, inthandler ); - signal( SIGQUIT, inthandler ); - signal( SIGPIPE, SIG_IGN ); - - char *arg; - while( vg_argp( argc, argv ) ){ - if( vg_long_opt( "noauth" ) ) - gameserver.auth_mode = eServerModeNoAuthentication; - - /* TODO: Options to override, ammend, remove etc */ - } - - vg_set_mem_quota( 80*1024*1024 ); - vg_alloc_quota(); - - db_init(); - db_request *req = db_alloc_request(0); - if( req ){ - req->handler = test_runner; - db_send_request(req); - } - - monitor_start_server(); /* UNIX socket monitor */ - - /* steamworks init - * --------------------------------------------------------------- */ - steamworks_ensure_txt( "2103940" ); - if( gameserver.auth_mode == eServerModeAuthentication ){ - if( !vg_load_steam_symetric_key( "application_key", - gameserver.app_symmetric_key )){ - return 0; - } - } - else{ - vg_warn( "Running without user authentication.\n" ); - } - - if( !SteamGameServer_Init( 0, NETWORK_PORT, NETWORK_PORT+1, - gameserver.auth_mode, "1.0.0.0" ) ){ - vg_error( "SteamGameServer_Init failed\n" ); - return 0; - } - - void *hSteamGameServer = SteamAPI_SteamGameServer(); - SteamAPI_ISteamGameServer_LogOnAnonymous( hSteamGameServer ); - - SteamAPI_ManualDispatch_Init(); - HSteamPipe hsteampipe = SteamGameServer_GetHSteamPipe(); - hSteamNetworkingSockets = - SteamAPI_SteamGameServerNetworkingSockets_SteamAPI(); - - steam_register_callback( k_iSteamNetAuthenticationStatus, on_auth_status ); - steam_register_callback( k_iSteamNetConnectionStatusChangedCallBack, - on_connect_status ); - - vg_success( "Steamworks API running\n" ); - steamworks_event_loop( hsteampipe ); - - /* - * Create a listener - */ - HSteamListenSocket listener; - SteamNetworkingIPAddr localAddr; - SteamAPI_SteamNetworkingIPAddr_Clear( &localAddr ); - localAddr.m_port = NETWORK_PORT; - - listener = SteamAPI_ISteamNetworkingSockets_CreateListenSocketIP( - hSteamNetworkingSockets, &localAddr, 0, NULL ); - gameserver.client_group = SteamAPI_ISteamNetworkingSockets_CreatePollGroup( - hSteamNetworkingSockets ); - - u64 server_ticks = 8000, - last_record_save = 8000, - last_scoreboard_gen = 0, - last_monitor_heartbeat = 0; - - while( !sig_stop ){ - monitor_event_loop(); - steamworks_event_loop( hsteampipe ); - poll_connections(); - - usleep(10000); - server_ticks ++; - - if( server_ticks > - (last_monitor_heartbeat + seconds_to_server_ticks(10.0))){ - last_monitor_heartbeat = server_ticks; - monitor_heartbeat(); - } - - if( db_killed() ) - break; - } - - SteamAPI_ISteamNetworkingSockets_DestroyPollGroup( hSteamNetworkingSockets, - gameserver.client_group ); - SteamAPI_ISteamNetworkingSockets_CloseListenSocket( - hSteamNetworkingSockets, listener ); - - vg_info( "Shutting down\n..." ); - SteamGameServer_Shutdown(); - db_kill(); - db_free(); - - return 0; -}