X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=gameserver_db.h;fp=gameserver_db.h;h=d3d3429669c24ef3233b0c32c8c4808fad48c4c7;hb=9a63a9dde9257c8f140af3a96816bcda232fe739;hp=cffa2dc069a546b04fd5a64d72ee417265e5ad7b;hpb=524c05104673b95ef0841d6ee90bcd24f9b829dc;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/gameserver_db.h b/gameserver_db.h index cffa2dc..d3d3429 100644 --- a/gameserver_db.h +++ b/gameserver_db.h @@ -2,17 +2,33 @@ #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_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; @@ -215,16 +231,21 @@ static int db_updateuser( u64 steamid, const char *username, int admin ){ else return 0; } -/* - * Create database connection and users table - */ -static int db_init(void){ +static void _db_thread_end(void){ + pthread_mutex_lock( &database.mux ); + database.kill = 1; + pthread_mutex_unlock( &database.mux ); + sqlite3_close( database.db ); + pthread_mutex_destroy( &database.mux ); +} + +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( @@ -238,18 +259,109 @@ 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_free(void){ - sqlite3_close( database.db ); + pthread_mutex_lock( &database.mux ); + database.kill = 1; + pthread_mutex_unlock( &database.mux ); +} + +static db_request *db_alloc_request( u32 size ){ + u32 total = sizeof(db_request) + vg_align8(size); + + pthread_mutex_lock( &database.mux ); + vg_queue_frame *frame = vg_queue_alloc( &database.queue, size ); + + 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 */