From 97cfbab7ad669d746ff67195388beb357c70d4a4 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Mon, 13 Jun 2022 12:06:58 +0800 Subject: [PATCH] Refactor network initialization Isolate network logic to each subsystem. --- CMakeLists.txt | 6 +- src/packages/game/IZ_common.h | 2 - src/packages/game/network/IZ_network.h | 6 ++ src/packages/game/network/IZ_wsclient.h | 1 + src/packages/server/IZ_app.c | 75 +---------------------- src/packages/server/IZ_app.h | 4 -- src/packages/server/network/IZ_network.h | 1 + src/packages/server/network/IZ_wsserver.c | 62 ++++++++++++++++++- src/packages/server/network/IZ_wsserver.h | 14 ++--- 9 files changed, 83 insertions(+), 88 deletions(-) create mode 100644 src/packages/game/network/IZ_network.h create mode 120000 src/packages/server/network/IZ_network.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a65eec..329621c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,7 +70,7 @@ add_executable( src/packages/game/input/IZ_midi.h src/packages/game/data/IZ_list.c src/packages/game/data/IZ_list.h - src/packages/game/network/IZ_wsclient.c src/packages/game/network/IZ_wsclient.h src/packages/game/log/IZ_log.c src/packages/game/log/IZ_log.h) + src/packages/game/network/IZ_wsclient.c src/packages/game/network/IZ_wsclient.h src/packages/game/log/IZ_log.c src/packages/game/log/IZ_log.h src/packages/game/network/IZ_network.h) target_link_libraries( game @@ -173,7 +173,9 @@ add_executable( src/packages/server/log/IZ_log.h src/packages/server/log/IZ_log.c src/packages/server/main.c - src/packages/server/network/IZ_wsserver.c src/packages/server/IZ_app.c src/packages/server/IZ_app.h src/packages/server/network/IZ_wsserver.h) + src/packages/server/network/IZ_wsserver.c src/packages/server/IZ_app.c src/packages/server/IZ_app.h src/packages/server/network/IZ_wsserver.h + src/packages/server/network/IZ_network.h +) target_link_libraries( server diff --git a/src/packages/game/IZ_common.h b/src/packages/game/IZ_common.h index b8c7efd..a36086b 100644 --- a/src/packages/game/IZ_common.h +++ b/src/packages/game/IZ_common.h @@ -7,8 +7,6 @@ #define PLAYERS (unsigned char) 1 #define APP_NAME "SDL2" -#define NETWORK_PROTOCOL "izanagi-networking" - typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; diff --git a/src/packages/game/network/IZ_network.h b/src/packages/game/network/IZ_network.h new file mode 100644 index 0000000..13ff580 --- /dev/null +++ b/src/packages/game/network/IZ_network.h @@ -0,0 +1,6 @@ +#ifndef IZ_NETWORK_H +#define IZ_NETWORK_H + +#define NETWORK_PROTOCOL "izanagi-networking" + +#endif diff --git a/src/packages/game/network/IZ_wsclient.h b/src/packages/game/network/IZ_wsclient.h index 54ba183..975b9d6 100644 --- a/src/packages/game/network/IZ_wsclient.h +++ b/src/packages/game/network/IZ_wsclient.h @@ -3,6 +3,7 @@ #include #include "../IZ_common.h" +#include "IZ_network.h" typedef struct { struct lws_context* context; diff --git a/src/packages/server/IZ_app.c b/src/packages/server/IZ_app.c index 68034de..a37b7a8 100644 --- a/src/packages/server/IZ_app.c +++ b/src/packages/server/IZ_app.c @@ -1,71 +1,11 @@ #include "IZ_app.h" -static struct lws_protocols protocols[] = { - { - .name = NETWORK_PROTOCOL, - .callback = IZ_WSServerCallback, - .per_session_data_size = sizeof(IZ_WSServerSessionData), - .rx_buffer_size = 0, - .id = 0, - .user = NULL, - .tx_packet_size = 0, - }, - { - .name = "http", - .callback = lws_callback_http_dummy, - .per_session_data_size = 0, - .rx_buffer_size = 0, - .id = 0, - .user = NULL, - .tx_packet_size = 0, - }, - LWS_PROTOCOL_LIST_TERM, -}; - -static const struct lws_http_mount mount = { - .mount_next = NULL, /* linked-list "next" */ - .mountpoint = "/", /* mountpoint URL */ - .origin = "./mount-origin", /* serve from dir */ - .def = "index.html", /* default filename */ - .protocol = NULL, - .cgienv = NULL, - .extra_mimetypes = NULL, - .interpret = NULL, - .cgi_timeout = 0, - .cache_max_age = 0, - .auth_mask = 0, - .cache_reusable = 0, - .cache_revalidate = 0, - .cache_intermediaries = 0, - .origin_protocol = LWSMPRO_FILE, /* files in a dir */ - .mountpoint_len = 1, /* char count */ - .basic_auth_login_file = NULL, -}; - void IZ_AppHandleSignal(i32 _signal) { interrupted = true; } -IZ_ProcedureResult IZ_AppCreateContext(IZ_App *app) { - struct lws_context_creation_info info; - memset(&info, 0, sizeof info); - info.port = app->config.port; - info.mounts = &mount; - info.protocols = protocols; - // TODO initialize protocols - info.options = ( - LWS_SERVER_OPTION_VALIDATE_UTF8 - | LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE - ); - - app->context = lws_create_context(&info); - if (!app->context) { - return 1; - } - - return 0; -} - +// TODO move to each subsystem +// TODO unify loading of config from cmdline and config file void IZ_AppLoadConfig(IZ_App *app, u8 argc, const char **argv) { memcpy_s(app, sizeof(IZ_App), &IZ_APP_DEFAULT_STATE, sizeof(IZ_App)); @@ -77,15 +17,6 @@ void IZ_AppLoadConfig(IZ_App *app, u8 argc, const char **argv) { if ((cmdline_buffer = lws_cmdline_option(argc, argv, "-p"))) { app->config.port = atoi(cmdline_buffer); } - - if (lws_cmdline_option(argc, argv, "-o")) { - // connect once - app->config.vhost_options |= 1; - } - - if (!lws_cmdline_option(argc, argv, "-n")) { - app->config.extensions_enabled = true; - } } IZ_ProcedureResult IZ_AppInitialize(IZ_App *app, u8 argc, const char **argv) { @@ -94,7 +25,7 @@ IZ_ProcedureResult IZ_AppInitialize(IZ_App *app, u8 argc, const char **argv) { IZ_AppLoadConfig(app, argc, argv); IZ_LogInterceptWSMessages(app->config.log_level); - if (IZ_AppCreateContext(app)) { + if (IZ_WSServerCreateContext(&app->context, app->config.port)) { return 1; } diff --git a/src/packages/server/IZ_app.h b/src/packages/server/IZ_app.h index ebfd300..bc1707f 100644 --- a/src/packages/server/IZ_app.h +++ b/src/packages/server/IZ_app.h @@ -12,8 +12,6 @@ static bool interrupted; typedef struct { i32 log_level; u16 port; - i32 vhost_options; - bool extensions_enabled; } IZ_AppConfig; typedef struct { @@ -25,8 +23,6 @@ static const IZ_App IZ_APP_DEFAULT_STATE = { .config = { .log_level = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE, .port = 42069, - .vhost_options = 0, - .extensions_enabled = false, }, .context = NULL, }; diff --git a/src/packages/server/network/IZ_network.h b/src/packages/server/network/IZ_network.h new file mode 120000 index 0000000..ded7f28 --- /dev/null +++ b/src/packages/server/network/IZ_network.h @@ -0,0 +1 @@ +E:/Projects/Games/izanagi/src/packages/game/network/IZ_network.h \ No newline at end of file diff --git a/src/packages/server/network/IZ_wsserver.c b/src/packages/server/network/IZ_wsserver.c index 18b669d..4bcdb1f 100644 --- a/src/packages/server/network/IZ_wsserver.c +++ b/src/packages/server/network/IZ_wsserver.c @@ -1,6 +1,46 @@ #include "IZ_wsserver.h" -/* one of these created for each message */ +static struct lws_protocols protocols[] = { + { + .name = NETWORK_PROTOCOL, + .callback = IZ_WSServerCallback, + .per_session_data_size = sizeof(IZ_WSServerSessionData), + .rx_buffer_size = 0, + .id = 0, + .user = NULL, + .tx_packet_size = 0, + }, + { + .name = "http", + .callback = lws_callback_http_dummy, + .per_session_data_size = 0, + .rx_buffer_size = 0, + .id = 0, + .user = NULL, + .tx_packet_size = 0, + }, + LWS_PROTOCOL_LIST_TERM, +}; + +static const struct lws_http_mount mount = { + .mount_next = NULL, /* linked-list "next" */ + .mountpoint = "/", /* mountpoint URL */ + .origin = "./mount-origin", /* serve from dir */ + .def = "index.html", /* default filename */ + .protocol = NULL, + .cgienv = NULL, + .extra_mimetypes = NULL, + .interpret = NULL, + .cgi_timeout = 0, + .cache_max_age = 0, + .auth_mask = 0, + .cache_reusable = 0, + .cache_revalidate = 0, + .cache_intermediaries = 0, + .origin_protocol = LWSMPRO_FILE, /* files in a dir */ + .mountpoint_len = 1, /* char count */ + .basic_auth_login_file = NULL, +}; static void IZ_ProtocolCullLaggingClients(IZ_WSServerVHostData *vhd) { u32 oldest_tail = lws_ring_get_oldest_tail(vhd->ring); @@ -283,3 +323,23 @@ i32 IZ_WSServerCallback( return 0; } + +IZ_ProcedureResult IZ_WSServerCreateContext(struct lws_context** context, u16 port) { + struct lws_context_creation_info info; + memset(&info, 0, sizeof info); + info.port = port; + info.mounts = &mount; + info.protocols = protocols; + // TODO initialize protocols + info.options = ( + LWS_SERVER_OPTION_VALIDATE_UTF8 + | LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE + ); + + *context = lws_create_context(&info); + if (!*context) { + return 1; + } + + return 0; +} diff --git a/src/packages/server/network/IZ_wsserver.h b/src/packages/server/network/IZ_wsserver.h index a4b2924..aa89a1e 100644 --- a/src/packages/server/network/IZ_wsserver.h +++ b/src/packages/server/network/IZ_wsserver.h @@ -4,33 +4,31 @@ #include "libwebsockets.h" #include #include "../IZ_common.h" +#include "IZ_network.h" #define RING_COUNT 32 +/* one of these created for each message */ typedef struct { - void *payload; /* is malloc'd */ + void* payload; /* is malloc'd */ size_t len; u8 binary: 1; } IZ_WSServerMessage; /* one of these is created for each client connecting to us */ - typedef struct IZ_WSServerSessionData { - struct IZ_WSServerSessionData *pss_list; - struct lws *wsi; + struct IZ_WSServerSessionData* pss_list; + struct lws* wsi; u32 tail; u8 culled: 1; } IZ_WSServerSessionData; /* one of these is created for each vhost our protocol is used with */ - typedef struct { struct lws_context *context; struct lws_vhost *vhost; const struct lws_protocols *protocol; - IZ_WSServerSessionData *pss_list; /* linked-list of live pss*/ - struct lws_ring *ring; /* ringbuffer holding unsent messages */ } IZ_WSServerVHostData; @@ -42,4 +40,6 @@ i32 IZ_WSServerCallback( size_t ); +IZ_ProcedureResult IZ_WSServerCreateContext(struct lws_context**, u16); + #endif