Browse Source

Add database setup

Database will be used for authentication.
feature/data-structs
TheoryOfNekomata 2 years ago
parent
commit
a7f9b45f71
8 changed files with 102 additions and 25 deletions
  1. +3
    -3
      CMakeLists.txt
  2. +13
    -13
      src/packages/game/net/IZ_net.c
  3. +0
    -2
      src/packages/game/net/IZ_net.h
  4. +11
    -2
      src/packages/server/IZ_app.c
  5. +2
    -0
      src/packages/server/IZ_app.h
  6. +38
    -0
      src/packages/server/db/IZ_repo.c
  7. +30
    -0
      src/packages/server/db/IZ_repo.h
  8. +5
    -5
      src/packages/server/net/IZ_net.c

+ 3
- 3
CMakeLists.txt View File

@@ -184,8 +184,8 @@ add_executable(

add_executable(
server
# dependencies/sqlite/sqlite3.h
# dependencies/sqlite/sqlite3.c
dependencies/sqlite/sqlite3.h
dependencies/sqlite/sqlite3.c
dependencies/minIni/dev/minIni.h
dependencies/minIni/dev/minIni.c
src/packages/server/IZ_common.h
@@ -200,7 +200,7 @@ add_executable(
src/packages/server/net/svc/IZ_wsserver.h
src/packages/server/net/core/IZ_websocket.h
src/packages/server/net/core/IZ_websocket.c
src/packages/server/net/IZ_net.c src/packages/server/net/IZ_net.h)
src/packages/server/net/IZ_net.c src/packages/server/net/IZ_net.h src/packages/server/db/IZ_repo.c src/packages/server/db/IZ_repo.h)

target_link_libraries(
server


+ 13
- 13
src/packages/game/net/IZ_net.c View File

@@ -1,5 +1,18 @@
#include "IZ_net.h"

bool IZ_NetIsConnected(IZ_NetState* state) {
if (state->ws.interrupted) {
return false;
}
if (!state->ws.context) {
return false;
}
if (!state->ws.connection) {
return false;
}
return true;
}

void IZ_NetLoadConfig(IZ_NetState* state, const char* config_path) {
char buffer[32];

@@ -76,19 +89,6 @@ void IZ_NetDisconnect(IZ_NetState* state) {
IZ_WSClientCancelService(&state->ws);
}

bool IZ_NetIsConnected(IZ_NetState* state) {
if (state->ws.interrupted) {
return false;
}
if (!state->ws.context) {
return false;
}
if (!state->ws.connection) {
return false;
}
return true;
}

void IZ_NetSendBinaryMessage(IZ_NetState* state, void* in, size_t len) {
if (!IZ_NetIsConnected(state)) {
return;


+ 0
- 2
src/packages/game/net/IZ_net.h View File

@@ -57,6 +57,4 @@ void IZ_NetSendBinaryMessage(IZ_NetState*, void*, size_t);

void IZ_NetSendTextMessage(IZ_NetState*, char*, size_t);

bool IZ_NetIsConnected(IZ_NetState*);

#endif

+ 11
- 2
src/packages/server/IZ_app.c View File

@@ -28,9 +28,19 @@ IZ_ProcedureResult IZ_AppInitialize(IZ_App *app, u8 argc, const char **argv) {
return -1;
}

if (IZ_RepoInitialize(&app->repo_state, config_path, argc, argv)) {
return -1;
}

return 0;
}

void IZ_AppTeardown(IZ_App* app) {
IZ_RepoTeardown(&app->repo_state);
IZ_WSServerTeardown(&app->net_state.ws);
lwsl_user("Server closed. Bye!\n");
}

IZ_ProcedureResult IZ_AppRun(IZ_App *app, u8 argc, const char **argv) {
if (IZ_AppInitialize(app, argc, argv)) {
return -1;
@@ -54,8 +64,7 @@ IZ_ProcedureResult IZ_AppRun(IZ_App *app, u8 argc, const char **argv) {
}
}

IZ_WSServerTeardown(&app->net_state.ws);
lwsl_user("Server closed. Bye!\n");
IZ_AppTeardown(app);
return result;
}



+ 2
- 0
src/packages/server/IZ_app.h View File

@@ -8,9 +8,11 @@
#include "IZ_common.h"
#include "IZ_config.h"
#include "net/IZ_net.h"
#include "db/IZ_repo.h"

typedef struct {
IZ_NetState net_state;
IZ_RepoState repo_state;
} IZ_App;

IZ_ProcedureResult IZ_AppRun(IZ_App*, u8, const char**);


+ 38
- 0
src/packages/server/db/IZ_repo.c View File

@@ -0,0 +1,38 @@
#include "IZ_repo.h"

void IZ_RepoLoadConfig(IZ_RepoState* state, const char* config_path) {
char buffer[64];

ini_gets("Database", "Path", IZ_REPO_DEFAULT_STATE.config.path, buffer, 64, config_path);
memcpy_s(state->config.path, 64, buffer, 64);
}

IZ_ProcedureResult IZ_RepoSaveConfig(IZ_RepoState* state, const char* config_path) {
if (!ini_puts("Database", "Path", state->config.path, config_path)) {
return -1;
}

return 0;
}

void IZ_RepoOverrideConfig(IZ_RepoState* state, u8 argc, const char* argv[]) {
const char* cmdline_buffer;
if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-n"))) {
memcpy_s(state->config.path, 64, cmdline_buffer, 64);
}
}

IZ_ProcedureResult IZ_RepoInitialize(IZ_RepoState* state, const char* config_path, u8 argc, const char* argv[]) {
memcpy_s(state, sizeof(IZ_RepoState), &IZ_REPO_DEFAULT_STATE, sizeof(IZ_RepoState));
IZ_RepoLoadConfig(state, config_path);
if (IZ_RepoSaveConfig(state, config_path)) {
return -1;
}
IZ_RepoOverrideConfig(state, argc, argv);
sqlite3_open_v2(state->config.path, &state->db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
return 0;
}

void IZ_RepoTeardown(IZ_RepoState* state) {
sqlite3_close_v2(state->db);
}

+ 30
- 0
src/packages/server/db/IZ_repo.h View File

@@ -0,0 +1,30 @@
#ifndef IZ_REPO_H
#define IZ_REPO_H

#include "minIni.h"
#include "sqlite3.h"
#include <string.h>
#include "../IZ_common.h"
#include "../IZ_config.h"

typedef struct {
char path[64];
} IZ_RepoConfig;

typedef struct {
IZ_RepoConfig config;
sqlite3* db;
} IZ_RepoState;

static IZ_RepoState IZ_REPO_DEFAULT_STATE = {
.config = {
.path = "db.sqlite",
},
.db = NULL,
};

IZ_ProcedureResult IZ_RepoInitialize(IZ_RepoState*, const char*, u8, const char**);

void IZ_RepoTeardown(IZ_RepoState*);

#endif

+ 5
- 5
src/packages/server/net/IZ_net.c View File

@@ -35,7 +35,7 @@ void IZ_NetOverrideConfig(IZ_NetState* state, u8 argc, const char* argv[]) {
}

if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-n"))) {
memcpy_s(state->config.name, 64, cmdline_buffer, 128);
memcpy_s(state->config.name, 64, cmdline_buffer, 64);
}

if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-m"))) {
@@ -50,15 +50,15 @@ IZ_ProcedureResult IZ_NetInitialize(
u8 argc,
const char* argv[]
) {
if (!user_data) {
return -1;
}
memcpy_s(state, sizeof(IZ_NetState), &IZ_NET_DEFAULT_STATE, sizeof(IZ_NetState));
IZ_NetLoadConfig(state, config_path);
if (IZ_NetSaveConfig(state, config_path) < 0) {
return -1;
}
IZ_NetOverrideConfig(state, argc, argv);
if (!user_data) {
return -2;
}
IZ_NetOverrideConfig(state, argc, argv);
state->ws.user_data = user_data;
return 0;
}


Loading…
Cancel
Save