@@ -184,8 +184,8 @@ add_executable( | |||||
add_executable( | add_executable( | ||||
server | 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.h | ||||
dependencies/minIni/dev/minIni.c | dependencies/minIni/dev/minIni.c | ||||
src/packages/server/IZ_common.h | src/packages/server/IZ_common.h | ||||
@@ -200,7 +200,7 @@ add_executable( | |||||
src/packages/server/net/svc/IZ_wsserver.h | src/packages/server/net/svc/IZ_wsserver.h | ||||
src/packages/server/net/core/IZ_websocket.h | src/packages/server/net/core/IZ_websocket.h | ||||
src/packages/server/net/core/IZ_websocket.c | 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( | target_link_libraries( | ||||
server | server | ||||
@@ -1,5 +1,18 @@ | |||||
#include "IZ_net.h" | #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) { | void IZ_NetLoadConfig(IZ_NetState* state, const char* config_path) { | ||||
char buffer[32]; | char buffer[32]; | ||||
@@ -76,19 +89,6 @@ void IZ_NetDisconnect(IZ_NetState* state) { | |||||
IZ_WSClientCancelService(&state->ws); | 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) { | void IZ_NetSendBinaryMessage(IZ_NetState* state, void* in, size_t len) { | ||||
if (!IZ_NetIsConnected(state)) { | if (!IZ_NetIsConnected(state)) { | ||||
return; | return; | ||||
@@ -57,6 +57,4 @@ void IZ_NetSendBinaryMessage(IZ_NetState*, void*, size_t); | |||||
void IZ_NetSendTextMessage(IZ_NetState*, char*, size_t); | void IZ_NetSendTextMessage(IZ_NetState*, char*, size_t); | ||||
bool IZ_NetIsConnected(IZ_NetState*); | |||||
#endif | #endif |
@@ -28,9 +28,19 @@ IZ_ProcedureResult IZ_AppInitialize(IZ_App *app, u8 argc, const char **argv) { | |||||
return -1; | return -1; | ||||
} | } | ||||
if (IZ_RepoInitialize(&app->repo_state, config_path, argc, argv)) { | |||||
return -1; | |||||
} | |||||
return 0; | 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) { | IZ_ProcedureResult IZ_AppRun(IZ_App *app, u8 argc, const char **argv) { | ||||
if (IZ_AppInitialize(app, argc, argv)) { | if (IZ_AppInitialize(app, argc, argv)) { | ||||
return -1; | 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; | return result; | ||||
} | } | ||||
@@ -8,9 +8,11 @@ | |||||
#include "IZ_common.h" | #include "IZ_common.h" | ||||
#include "IZ_config.h" | #include "IZ_config.h" | ||||
#include "net/IZ_net.h" | #include "net/IZ_net.h" | ||||
#include "db/IZ_repo.h" | |||||
typedef struct { | typedef struct { | ||||
IZ_NetState net_state; | IZ_NetState net_state; | ||||
IZ_RepoState repo_state; | |||||
} IZ_App; | } IZ_App; | ||||
IZ_ProcedureResult IZ_AppRun(IZ_App*, u8, const char**); | IZ_ProcedureResult IZ_AppRun(IZ_App*, u8, const char**); | ||||
@@ -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); | |||||
} |
@@ -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 |
@@ -35,7 +35,7 @@ void IZ_NetOverrideConfig(IZ_NetState* state, u8 argc, const char* argv[]) { | |||||
} | } | ||||
if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-n"))) { | 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"))) { | if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-m"))) { | ||||
@@ -50,15 +50,15 @@ IZ_ProcedureResult IZ_NetInitialize( | |||||
u8 argc, | u8 argc, | ||||
const char* argv[] | const char* argv[] | ||||
) { | ) { | ||||
if (!user_data) { | |||||
return -1; | |||||
} | |||||
memcpy_s(state, sizeof(IZ_NetState), &IZ_NET_DEFAULT_STATE, sizeof(IZ_NetState)); | memcpy_s(state, sizeof(IZ_NetState), &IZ_NET_DEFAULT_STATE, sizeof(IZ_NetState)); | ||||
IZ_NetLoadConfig(state, config_path); | IZ_NetLoadConfig(state, config_path); | ||||
if (IZ_NetSaveConfig(state, config_path) < 0) { | if (IZ_NetSaveConfig(state, config_path) < 0) { | ||||
return -1; | |||||
} | |||||
IZ_NetOverrideConfig(state, argc, argv); | |||||
if (!user_data) { | |||||
return -2; | return -2; | ||||
} | } | ||||
IZ_NetOverrideConfig(state, argc, argv); | |||||
state->ws.user_data = user_data; | state->ws.user_data = user_data; | ||||
return 0; | return 0; | ||||
} | } | ||||