Start of server configuration code.
This commit is contained in:
parent
cf73087076
commit
1d7dc89a8a
8 changed files with 282 additions and 6 deletions
|
@ -92,6 +92,6 @@ void configStartup(char *file) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// String defaults.
|
// String defaults.
|
||||||
if (!_configData.serverHost) _configData.serverHost = strdup("\"kanga.world\"");
|
if (!_configData.serverHost) _configData.serverHost = strdup("kanga.world");
|
||||||
if (!_configData.userName) _configData.userName = strdup("\"New User\"");
|
if (!_configData.userName) _configData.userName = strdup("New User");
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,9 @@
|
||||||
#define OS_H
|
#define OS_H
|
||||||
|
|
||||||
|
|
||||||
|
// https://sourceforge.net/p/predef/wiki/Architectures/
|
||||||
|
|
||||||
|
|
||||||
// Common platform includes.
|
// Common platform includes.
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
|
@ -20,6 +20,6 @@ TEMPLATE = subdirs
|
||||||
CONFIG *= ORDERED
|
CONFIG *= ORDERED
|
||||||
|
|
||||||
SUBDIRS = \
|
SUBDIRS = \
|
||||||
font \
|
# font \
|
||||||
client \
|
# client \
|
||||||
server
|
server
|
||||||
|
|
33
server/database.h
Normal file
33
server/database.h
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
* Kangaroo Punch MultiPlayer Game Server Mark II
|
||||||
|
* Copyright (C) 2020-2021 Scott Duensing
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef DATABASE_H
|
||||||
|
#define DATABASE_H
|
||||||
|
|
||||||
|
|
||||||
|
#include "os.h"
|
||||||
|
|
||||||
|
|
||||||
|
uint8_t dbConnect(char *host, uint16_t port, char *database, char *user, char *password);
|
||||||
|
uint8_t dbDisconnect(void);
|
||||||
|
uint8_t dbSettingsValueGet(char *key, int32_t *value);
|
||||||
|
|
||||||
|
|
||||||
|
#endif // DATABASE_H
|
|
@ -37,5 +37,12 @@
|
||||||
// Now our headers.
|
// Now our headers.
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
|
// Allocation helpers.
|
||||||
|
#define NEW(t,v) (v)=(t*)malloc(sizeof(t))
|
||||||
|
#define DEL(v) free(v); v=NULL
|
||||||
|
|
||||||
|
#define SUCCESS 1
|
||||||
|
#define FAIL 0
|
||||||
|
|
||||||
|
|
||||||
#endif // OS_H
|
#endif // OS_H
|
||||||
|
|
|
@ -19,11 +19,14 @@
|
||||||
|
|
||||||
TEMPLATE = app
|
TEMPLATE = app
|
||||||
CONFIG -= qt
|
CONFIG -= qt
|
||||||
|
CONFIG += c11
|
||||||
|
|
||||||
DESTDIR = $$OUT_PWD/bin
|
DESTDIR = $$OUT_PWD/bin
|
||||||
SHARED = $$PWD/../shared
|
SHARED = $$PWD/../shared
|
||||||
|
|
||||||
INCLUDEPATH += \
|
INCLUDEPATH += \
|
||||||
|
/usr/include/mariadb \
|
||||||
|
/usr/include/mariadb/mysql \
|
||||||
$$SHARED \
|
$$SHARED \
|
||||||
$$PWD/src
|
$$PWD/src
|
||||||
|
|
||||||
|
@ -37,6 +40,7 @@ HEADERS = \
|
||||||
$$SHARED/log.h \
|
$$SHARED/log.h \
|
||||||
$$SHARED/memory.h \
|
$$SHARED/memory.h \
|
||||||
$$SHARED/util.h \
|
$$SHARED/util.h \
|
||||||
|
database.h \
|
||||||
os.h
|
os.h
|
||||||
|
|
||||||
SOURCES = \
|
SOURCES = \
|
||||||
|
@ -47,6 +51,15 @@ SOURCES = \
|
||||||
$$SHARED/log.c \
|
$$SHARED/log.c \
|
||||||
$$SHARED/memory.c \
|
$$SHARED/memory.c \
|
||||||
$$SHARED/util.c \
|
$$SHARED/util.c \
|
||||||
|
src/database.c \
|
||||||
src/main.c
|
src/main.c
|
||||||
|
|
||||||
|
LIBS = \
|
||||||
|
-L/usr/lib/x86_64-linux-gnu/ \
|
||||||
|
-lmariadb \
|
||||||
|
-ldl \
|
||||||
|
-lm \
|
||||||
|
-lpthread \
|
||||||
|
-lgnutls
|
||||||
|
|
||||||
OTHER_FILES =
|
OTHER_FILES =
|
||||||
|
|
108
server/src/database.c
Normal file
108
server/src/database.c
Normal file
|
@ -0,0 +1,108 @@
|
||||||
|
/*
|
||||||
|
* Kangaroo Punch MultiPlayer Game Server Mark II
|
||||||
|
* Copyright (C) 2020-2021 Scott Duensing
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <mysql.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#include "database.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define STATEMENT_MAX 2048
|
||||||
|
|
||||||
|
|
||||||
|
static MYSQL *_sql = NULL;
|
||||||
|
static char _statement[STATEMENT_MAX];
|
||||||
|
static pthread_mutex_t _mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
|
|
||||||
|
uint8_t dbConnect(char *host, uint16_t port, char *database, char *user, char *password) {
|
||||||
|
if (_sql == NULL) {
|
||||||
|
_sql = mysql_init(NULL);
|
||||||
|
if (mysql_real_connect(_sql, host, user, password, database, port, NULL, 0) == NULL) {
|
||||||
|
logWrite("dbConnect: %s\n", mysql_error(_sql));
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
if (pthread_mutex_init(&_mutex, NULL)) {
|
||||||
|
logWrite("dbConnect: SQL mutex creation failed.\n");
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint8_t dbDisconnect(void) {
|
||||||
|
if (_sql) {
|
||||||
|
mysql_close(_sql);
|
||||||
|
_sql = NULL;
|
||||||
|
pthread_mutex_destroy(&_mutex);
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint8_t dbSettingsValueGet(char *key, int32_t *value) {
|
||||||
|
char *p = _statement;
|
||||||
|
MYSQL_RES *result = NULL;
|
||||||
|
MYSQL_ROW row;
|
||||||
|
int count;
|
||||||
|
|
||||||
|
pthread_mutex_lock(&_mutex);
|
||||||
|
|
||||||
|
if (!_sql) {
|
||||||
|
pthread_mutex_unlock(&_mutex);
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
p += sprintf(p, "SELECT data FROM config where NAME='");
|
||||||
|
p += mysql_real_escape_string(_sql, p, key, strlen(key));
|
||||||
|
p += sprintf(p, "'");
|
||||||
|
if (mysql_real_query(_sql, _statement, p - _statement) != 0) {
|
||||||
|
logWrite("dbConfigValueGet: %s\n", mysql_error(_sql));
|
||||||
|
pthread_mutex_unlock(&_mutex);
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = mysql_store_result(_sql);
|
||||||
|
count = mysql_num_rows(result);
|
||||||
|
if (count != 1) {
|
||||||
|
logWrite("dbConfigValueGet: Too many rows returned: %d.\n", count);
|
||||||
|
mysql_free_result(result);
|
||||||
|
pthread_mutex_unlock(&_mutex);
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((row = mysql_fetch_row(result)) == NULL) {
|
||||||
|
logWrite("dbConfigValueGet: %s\n", mysql_error(_sql));
|
||||||
|
pthread_mutex_unlock(&_mutex);
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
*value = atoi(row[0]);
|
||||||
|
|
||||||
|
mysql_free_result(result);
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&_mutex);
|
||||||
|
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
|
@ -20,20 +20,132 @@
|
||||||
|
|
||||||
#include "os.h"
|
#include "os.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "stddclmr.h"
|
||||||
|
#include "thirdparty/ini/src/ini.h"
|
||||||
|
#include "database.h"
|
||||||
|
#include "array.h"
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct HashValueS {
|
||||||
|
char *string;
|
||||||
|
int32_t integer;
|
||||||
|
} HashValueT;
|
||||||
|
|
||||||
|
typedef struct HashMapS {
|
||||||
|
char *key;
|
||||||
|
HashValueT value;
|
||||||
|
} HashMapT;
|
||||||
|
|
||||||
|
|
||||||
|
static char *_configServer = NULL;
|
||||||
|
static uint16_t _configPort = 0;
|
||||||
|
static char *_configDatabase = NULL;
|
||||||
|
static char *_configUser = NULL;
|
||||||
|
static char *_configPassword = NULL;
|
||||||
|
|
||||||
|
static HashMapT *_settings = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
static void configRead(char *file);
|
||||||
|
static void configWrite(char *file);
|
||||||
|
|
||||||
|
|
||||||
|
static void configRead(char *file) {
|
||||||
|
ini_t *ini = NULL;
|
||||||
|
|
||||||
|
// Numeric defaults.
|
||||||
|
_configPort = 16550;
|
||||||
|
|
||||||
|
ini = ini_load(file);
|
||||||
|
if (ini) {
|
||||||
|
ini_sget(ini, "SERVER", "PORT", "%d", &_configPort);
|
||||||
|
_configServer = strdup(ini_get(ini, "SERVER", "HOST"));
|
||||||
|
_configDatabase = strdup(ini_get(ini, "SERVER", "DATA"));
|
||||||
|
_configUser = strdup(ini_get(ini, "SERVER", "USER"));
|
||||||
|
_configPassword = strdup(ini_get(ini, "SERVER", "PASS"));
|
||||||
|
ini_free(ini);
|
||||||
|
}
|
||||||
|
|
||||||
|
// String defaults.
|
||||||
|
if (!_configServer) strdup("kanga.world");
|
||||||
|
if (!_configDatabase) strdup("kpmpgsmkii");
|
||||||
|
if (!_configUser) strdup("");
|
||||||
|
if (!_configPassword) strdup("");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void configWrite(char *file) {
|
||||||
|
FILE *out = NULL;
|
||||||
|
|
||||||
|
out = fopen(file, "w");
|
||||||
|
if (out) {
|
||||||
|
fprintf(out,
|
||||||
|
"[SERVER]\n"
|
||||||
|
"HOST=%s\n"
|
||||||
|
"PORT=%d\n"
|
||||||
|
"DATA=%s\n"
|
||||||
|
"USER=%s\n"
|
||||||
|
"PASS=%s\n",
|
||||||
|
_configServer,
|
||||||
|
_configPort,
|
||||||
|
_configDatabase,
|
||||||
|
_configUser,
|
||||||
|
_configPassword
|
||||||
|
);
|
||||||
|
fclose(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
DEL(_configServer);
|
||||||
|
DEL(_configDatabase);
|
||||||
|
DEL(_configUser);
|
||||||
|
DEL(_configPassword);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
char *configFile = NULL;
|
||||||
|
HashValueT tempValue;
|
||||||
|
|
||||||
(void)argc;
|
(void)argc;
|
||||||
|
|
||||||
memoryStartup(argv[0]);
|
memoryStartup(argv[0]);
|
||||||
logOpenByHandle(memoryLogHandleGet());
|
logOpenByHandle(memoryLogHandleGet());
|
||||||
|
configFile = utilAppNameWithNewExtensionGet(argv[0], "ini");
|
||||||
|
configRead(configFile);
|
||||||
|
|
||||||
// 0 1 2 3 4 5 6 7 8
|
// 0 1 2 3 4 5 6 7 8
|
||||||
// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
|
// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||||
logWrite("%s", "Kangaroo Punch MultiPlayer DOS Game Server Mark II\n");
|
logWrite("%s", "Kangaroo Punch MultiPlayer DOS Game Server Mark II\n");
|
||||||
logWrite("%s", "Copyright (C) 2020-2021 Scott Duensing scott@kangaroopunch.com\n\n");
|
logWrite("%s", "Copyright (C) 2020-2021 Scott Duensing scott@kangaroopunch.com\n\n");
|
||||||
|
|
||||||
|
if (dbConnect(_configServer, _configPort, _configDatabase, _configUser, _configPassword)) {
|
||||||
|
tempValue.string = NULL;
|
||||||
|
if (dbSettingsValueGet("maxClients", &tempValue.integer)) shput(_settings, "maxClients", tempValue);
|
||||||
|
if (dbSettingsValueGet("portNumber", &tempValue.integer)) shput(_settings, "portNumber", tempValue);
|
||||||
|
dbDisconnect();
|
||||||
|
} else {
|
||||||
|
logWrite("Unable to connect to database.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show hashmap contents.
|
||||||
|
for (size_t i=0; i<shlenu(_settings); i++) {
|
||||||
|
if (_settings[i].value.string) {
|
||||||
|
logWrite("%s: %s\n", _settings[i].key, _settings[i].value.string);
|
||||||
|
} else {
|
||||||
|
logWrite("%s: %d\n", _settings[i].key, _settings[i].value.integer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Free hashmap storage.
|
||||||
|
for (size_t i=0; i<shlenu(_settings); i++) {
|
||||||
|
if (_settings[i].value.string) DEL(_settings[i].value.string);
|
||||||
|
}
|
||||||
|
shfree(_settings);
|
||||||
|
_settings = NULL;
|
||||||
|
|
||||||
|
configWrite(configFile);
|
||||||
|
DEL(configFile);
|
||||||
logClose();
|
logClose();
|
||||||
memoryShutdown();
|
memoryShutdown();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue