From e80c557d631c301d664e6e4d6d9c6a1eb2450e49 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Mon, 13 Jun 2022 12:42:09 +0800 Subject: [PATCH] Update server Ensure mounts are configurable. --- src/packages/game/IZ_config.h | 1 + src/packages/game/input/IZ_midi.c | 1 - src/packages/game/memory/memory.test.c | 2 - src/packages/game/output/IZ_video.c | 6 ++ src/packages/game/output/IZ_video.h | 10 ++- src/packages/server/IZ_app.c | 8 +- src/packages/server/network/IZ_wsserver.c | 105 ++++++++++++---------- src/packages/server/network/IZ_wsserver.h | 9 +- 8 files changed, 88 insertions(+), 54 deletions(-) diff --git a/src/packages/game/IZ_config.h b/src/packages/game/IZ_config.h index e007c7d..168daf4 100644 --- a/src/packages/game/IZ_config.h +++ b/src/packages/game/IZ_config.h @@ -4,6 +4,7 @@ #include #include +// TODO unify loading of config from cmdline and config file void IZ_ConfigGetDefaultPath(const char*, size_t); #endif diff --git a/src/packages/game/input/IZ_midi.c b/src/packages/game/input/IZ_midi.c index 1193731..402e0ad 100644 --- a/src/packages/game/input/IZ_midi.c +++ b/src/packages/game/input/IZ_midi.c @@ -181,7 +181,6 @@ IZ_ProcedureResult IZ_MIDIInputInitialize(const char* config_path, IZ_MIDIInputS } u8 player_index; - // TODO Pm_CountDevices(), only filter input devices u8 midi_devices_count = Pm_CountDevices(); u8 input_midi_devices_count = 0; u8 device_index; diff --git a/src/packages/game/memory/memory.test.c b/src/packages/game/memory/memory.test.c index 7981748..99175d9 100644 --- a/src/packages/game/memory/memory.test.c +++ b/src/packages/game/memory/memory.test.c @@ -62,8 +62,6 @@ spec("memory") { it("assigns contiguous memory") { IZ_PoolInitialize(&pool, POOL_MAX_SIZE); - - // FIXME: access violation error void* p1 = IZ_PoolAllocate(&pool, (IZ_PoolAllocationArgs){ .size = sizeof(struct DummyStruct), .priority = 0, diff --git a/src/packages/game/output/IZ_video.c b/src/packages/game/output/IZ_video.c index 70c5517..af28b22 100644 --- a/src/packages/game/output/IZ_video.c +++ b/src/packages/game/output/IZ_video.c @@ -108,6 +108,12 @@ void IZ_VideoUpdate(IZ_VideoState* video_state, u64 ticks, IZ_InputState* input_ // Update window SDL_SetRenderDrawColor(video_state->renderer, 0x00, 0x00, 0x00, 0xff); SDL_RenderClear(video_state->renderer); + for (u8 i = 0; i < MAX_ACTIVE_SPRITES; i += 1) { + if (!video_state->active_sprites[i]) { + continue; + } + // TODO draw sprites + } IZ_VideoUpdateForDebug(video_state, input_state, ticks); SDL_RenderPresent(video_state->renderer); video_state->last_update_at = ticks; diff --git a/src/packages/game/output/IZ_video.h b/src/packages/game/output/IZ_video.h index eff33e9..48562e5 100644 --- a/src/packages/game/output/IZ_video.h +++ b/src/packages/game/output/IZ_video.h @@ -5,11 +5,15 @@ #include #include -// TODO move this out from video, refer to app's state instead #include "../input/IZ_input.h" #include "../IZ_common.h" #include "../IZ_config.h" +#define MAX_ACTIVE_SPRITES 32 + +// TODO properly define sprites +typedef char IZ_Sprite; + typedef struct { u16 width; u16 height; @@ -18,9 +22,10 @@ typedef struct { typedef struct { IZ_VideoConfig config; - uint64_t last_update_at; + u64 last_update_at; SDL_Window* window; SDL_Renderer* renderer; + IZ_Sprite active_sprites[MAX_ACTIVE_SPRITES]; } IZ_VideoState; static const IZ_VideoState IZ_DEFAULT_VIDEO_STATE = { @@ -32,6 +37,7 @@ static const IZ_VideoState IZ_DEFAULT_VIDEO_STATE = { .last_update_at = 0, .renderer = NULL, .window = NULL, + .active_sprites = {}, }; IZ_ProcedureResult IZ_VideoInitialize(IZ_VideoState*, const char*); diff --git a/src/packages/server/IZ_app.c b/src/packages/server/IZ_app.c index a37b7a8..074a29a 100644 --- a/src/packages/server/IZ_app.c +++ b/src/packages/server/IZ_app.c @@ -22,10 +22,16 @@ void IZ_AppLoadConfig(IZ_App *app, u8 argc, const char **argv) { IZ_ProcedureResult IZ_AppInitialize(IZ_App *app, u8 argc, const char **argv) { interrupted = false; signal(SIGINT, IZ_AppHandleSignal); + //signal(SIGTERM, IZ_AppHandleSignal); IZ_AppLoadConfig(app, argc, argv); IZ_LogInterceptWSMessages(app->config.log_level); - if (IZ_WSServerCreateContext(&app->context, app->config.port)) { + if (IZ_WSServerCreateContext(&app->context, (IZ_WSServerCreateContextParams) { + .protocol = NULL, + .port = app->config.port, + .default_filename = NULL, + .origin = NULL, + })) { return 1; } diff --git a/src/packages/server/network/IZ_wsserver.c b/src/packages/server/network/IZ_wsserver.c index 4bcdb1f..14b6fe6 100644 --- a/src/packages/server/network/IZ_wsserver.c +++ b/src/packages/server/network/IZ_wsserver.c @@ -1,47 +1,5 @@ #include "IZ_wsserver.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, -}; - static void IZ_ProtocolCullLaggingClients(IZ_WSServerVHostData *vhd) { u32 oldest_tail = lws_ring_get_oldest_tail(vhd->ring); IZ_WSServerSessionData *old_pss = NULL; @@ -135,8 +93,8 @@ static void IZ_ProtocolCullLaggingClients(IZ_WSServerVHostData *vhd) { /* destroys the message when everyone has had a copy of it */ -void IZ_ProtocolDestroyMessage(void *_msg) { - IZ_WSServerMessage *msg = _msg; +void IZ_ProtocolDestroyMessage(void* msg_raw) { + IZ_WSServerMessage *msg = msg_raw; free(msg->payload); msg->payload = NULL; @@ -324,13 +282,66 @@ i32 IZ_WSServerCallback( return 0; } -IZ_ProcedureResult IZ_WSServerCreateContext(struct lws_context** context, u16 port) { +IZ_ProcedureResult IZ_WSServerCreateContext(struct lws_context** context, IZ_WSServerCreateContextParams params) { struct lws_context_creation_info info; memset(&info, 0, sizeof info); - info.port = port; + info.port = params.port; + + static struct lws_http_mount mount = { + .mount_next = NULL, /* linked-list "next" */ + .mountpoint = "/", /* mountpoint URL */ + .origin = "./public", /* serve from dir */ + .def = "index.html", /* default filename */ + .protocol = "http", + .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, + }; + + if (params.origin) { + mount.origin = params.origin; + } + + if (params.default_filename) { + mount.def = params.default_filename; + } + + if (params.protocol) { + mount.protocol = params.protocol; + } info.mounts = &mount; + + static const 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, + }; info.protocols = protocols; - // TODO initialize protocols info.options = ( LWS_SERVER_OPTION_VALIDATE_UTF8 | LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE diff --git a/src/packages/server/network/IZ_wsserver.h b/src/packages/server/network/IZ_wsserver.h index aa89a1e..df1fdc5 100644 --- a/src/packages/server/network/IZ_wsserver.h +++ b/src/packages/server/network/IZ_wsserver.h @@ -40,6 +40,13 @@ i32 IZ_WSServerCallback( size_t ); -IZ_ProcedureResult IZ_WSServerCreateContext(struct lws_context**, u16); +typedef struct { + u16 port; + const char* origin; + const char* default_filename; + const char* protocol; +} IZ_WSServerCreateContextParams; + +IZ_ProcedureResult IZ_WSServerCreateContext(struct lws_context**, IZ_WSServerCreateContextParams); #endif