X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=gameserver_db.h;h=fe39931af843ae30b3715b6192367193dedc489a;hb=bececcbb7b2e886e72425e7c070e1fdc3aa126dc;hp=cffa2dc069a546b04fd5a64d72ee417265e5ad7b;hpb=524c05104673b95ef0841d6ee90bcd24f9b829dc;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/gameserver_db.h b/gameserver_db.h index cffa2dc..fe39931 100644 --- a/gameserver_db.h +++ b/gameserver_db.h @@ -2,17 +2,32 @@ #define GAMESERVER_DB_H #include "vg/vg_log.h" +#include "vg/vg_mem_queue.h" #include "network_common.h" #include "dep/sqlite3/sqlite3.h" -#include "highscores.h" +#include +#include #define DB_COURSE_UID_MAX 32 #define DB_TABLE_UID_MAX (ADDON_UID_MAX+DB_COURSE_UID_MAX+32) -#define DB_CRASH_ON_SQLITE_ERROR +//#define DB_CRASH_ON_SQLITE_ERROR #define DB_LOG_SQL_STATEMENTS +#define DB_REQUEST_BUFFER_SIZE (1024*2) + +typedef struct db_request db_request; +struct db_request { + void (*handler)( db_request *req ); + u32 size,_; + u8 data[]; +}; struct { sqlite3 *db; + pthread_t thread; + pthread_mutex_t mux; + + vg_queue queue; + int kill; } static database; @@ -76,8 +91,8 @@ static int db_verify_charset( const char *str, int mincount ){ /* * Find table name from mod UID and course UID, plus the week number */ -static int db_get_highscore_table_name( char mod_uid[ADDON_UID_MAX], - char run_uid[DB_COURSE_UID_MAX], +static int db_get_highscore_table_name( const char *mod_uid, + const char *run_uid, u32 week, char table_name[DB_TABLE_UID_MAX] ){ if( !db_verify_charset( mod_uid, 13 ) || @@ -110,9 +125,8 @@ static i32 db_readusertime( char table[DB_TABLE_UID_MAX], u64 steamid ){ if( !vg_strgood(&q) ) return 0; sqlite3_stmt *stmt = db_stmt( q.buffer ); - sqlite3_bind_int64( stmt, 1, *((i64 *)&steamid) ); - if( stmt ){ + sqlite3_bind_int64( stmt, 1, *((i64 *)&steamid) ); int fc = sqlite3_step( stmt ); i32 result = 0; @@ -140,7 +154,7 @@ static int db_writeusertime( char table[DB_TABLE_UID_MAX], u64 steamid, vg_strnull( &q, buf, 512 ); vg_strcat( &q, "CREATE TABLE IF NOT EXISTS \n \"" ); vg_strcat( &q, table ); - vg_strcat( &q, "\"\n (steamid BIGINT PRIMARY KEY, time INT);" ); + vg_strcat( &q, "\"\n (steamid BIGINT UNIQUE, time INT);" ); if( !vg_strgood(&q) ) return 0; vg_str str; @@ -216,20 +230,53 @@ static int db_updateuser( u64 steamid, const char *username, int admin ){ } /* - * Create database connection and users table + * Get user info */ -static int db_init(void){ +static int db_getuserinfo( u64 steamid, char *out_username, u32 username_max, + i32 *out_type ){ + sqlite3_stmt *stmt = db_stmt( "SELECT * FROM users WHERE steamid = ?;" ); + if( !stmt ) return 0; + + sqlite3_bind_int64( stmt, 1, *((i64 *)&steamid) ); + int fc = sqlite3_step( stmt ); + + if( fc != SQLITE_ROW ){ + log_sqlite3( fc ); + sqlite3_finalize( stmt ); + return 0; + } + + if( out_username ){ + const char *name = (const char *)sqlite3_column_text( stmt, 1 ); + vg_strncpy( name, out_username, username_max, k_strncpy_allow_cutoff ); + } + + if( out_type ) + *out_type = sqlite3_column_int( stmt, 2 ); + + sqlite3_finalize( stmt ); + return 1; +} + +static void _db_thread_end(void){ + pthread_mutex_lock( &database.mux ); + database.kill = 1; + pthread_mutex_unlock( &database.mux ); + sqlite3_close( database.db ); +} + +static void *db_loop(void *_){ int rc = sqlite3_open( "highscores.db", &database.db ); if( rc ){ vg_error( "database failure: %s\n", sqlite3_errmsg(database.db) ); - sqlite3_close( database.db ); - return 0; + _db_thread_end(); + return NULL; } sqlite3_stmt *stmt = db_stmt( "CREATE TABLE IF NOT EXISTS \n" - " users(steamid BIGINT PRIMARY KEY, name VARCHAR(128), type INT);" ); + " users(steamid BIGINT UNIQUE, name VARCHAR(128), type INT);" ); if( stmt ){ int fc = sqlite3_step( stmt ); @@ -238,18 +285,114 @@ static int db_init(void){ if( fc == SQLITE_DONE ){ vg_success( "Created users table\n" ); db_updateuser( 76561198072130043, "harry", 2 ); - return 1; } else{ log_sqlite3( fc ); - return 0; + _db_thread_end(); + return NULL; } } - else return 0; + else { + _db_thread_end(); + return NULL; + } + + /* + * Request processing loop + */ + while(1){ + pthread_mutex_lock( &database.mux ); + + if( database.kill ){ + pthread_mutex_unlock( &database.mux ); + _db_thread_end(); + break; + } + + u32 processed = 0; + + for( u32 i=0; i<16; i ++ ){ + db_request *req = NULL; + if( database.queue.tail ){ + req = (db_request *)database.queue.tail->data; + pthread_mutex_unlock( &database.mux ); + } + else{ + pthread_mutex_unlock( &database.mux ); + break; + } + + req->handler( req ); + processed ++; + + pthread_mutex_lock( &database.mux ); + vg_queue_pop( &database.queue ); + } + + if( processed ) + vg_low( "Processed %u database requests.\n", processed ); + + usleep(50000); + } + + vg_low( "Database thread terminates.\n" ); + return NULL; +} + +/* + * Create database connection and users table + */ +static int db_init(void){ + database.queue.buffer = + (u8 *)vg_linear_alloc( vg_mem.rtmemory, DB_REQUEST_BUFFER_SIZE ), + database.queue.size = DB_REQUEST_BUFFER_SIZE; + + if( pthread_mutex_init( &database.mux, NULL ) ) + return 0; + + if( pthread_create( &database.thread, NULL, db_loop, NULL ) ) + return 0; + + return 1; +} + +static int db_killed(void){ + pthread_mutex_lock( &database.mux ); + int result = database.kill; + pthread_mutex_unlock( &database.mux ); + return result; +} + +static void db_kill(void){ + pthread_mutex_lock( &database.mux ); + database.kill = 1; + pthread_mutex_unlock( &database.mux ); + pthread_join( database.thread, NULL ); } static void db_free(void){ - sqlite3_close( database.db ); + pthread_mutex_destroy( &database.mux ); +} + +static db_request *db_alloc_request( u32 size ){ + u32 total = sizeof(db_request) + size; + + pthread_mutex_lock( &database.mux ); + vg_queue_frame *frame = vg_queue_alloc( &database.queue, total ); + + if( frame ){ + db_request *req = (db_request *)frame->data; + req->size = size; + return req; + } + else { + pthread_mutex_unlock( &database.mux ); + return NULL; + } +} + +static void db_send_request( db_request *request ){ + pthread_mutex_unlock( &database.mux ); } #endif /* GAMESERVER_DB_H */