From 34e7ad43ea209994a0576e87ffc8dd5a6f4f801c Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sun, 12 Jun 2022 18:36:06 +0800 Subject: [PATCH] Attempt connection to server The client app is able to connect to the server, however no further communication is happening. Must've been the callbacks? Also the client app enters into an infinite loop when lws_service() is called inside the game loop. --- src/packages/game/IZ_app.c | 34 ++++++++++++++------- src/packages/game/IZ_app.h | 9 ++++++ src/packages/game/network/IZ_wsclient.c | 40 +++++++++++++++++++------ src/packages/game/network/IZ_wsclient.h | 5 ++-- src/packages/server/main.c | 2 +- 5 files changed, 67 insertions(+), 23 deletions(-) diff --git a/src/packages/game/IZ_app.c b/src/packages/game/IZ_app.c index 6827034..8777f20 100644 --- a/src/packages/game/IZ_app.c +++ b/src/packages/game/IZ_app.c @@ -9,30 +9,30 @@ IZ_ProcedureResult IZ_AppInitialize(IZ_App* app) { if (SDL_Init(flags) < 0) { // TODO fix logging - fprintf_s(stderr, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); - return 1; + SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + return IZ_APP_RUN_SDL_INIT_ERROR; } char config_path[128]; IZ_ConfigGetPath(config_path, 128); if (IZ_VideoInitialize(&app->video_state, config_path)) { - return 2; + return IZ_APP_RUN_VIDEO_INIT_ERROR; } if (IZ_InputInitialize(&app->input_state, config_path)) { - return 3; + return IZ_APP_RUN_INPUT_INIT_ERROR; } IZ_PoolInitialize(&app->pool, POOL_MAX_SIZE); - IZ_WSClientInitialize(&app->client, app); + IZ_WSClientInitialize(&app->client); // TODO put into its timer module app->ticks = 0; - return 0; + return IZ_APP_RUN_RESULT_OK; } void IZ_AppTeardown(IZ_App* app) { - IZ_WSClientTeardown(&app->client); + IZ_WSClientDisconnect(&app->client); IZ_PoolTeardown(&app->pool); IZ_InputTeardown(&app->input_state); IZ_VideoTeardown(&app->video_state); @@ -90,19 +90,33 @@ IZ_ProcedureResult IZ_AppRun(IZ_App* app, u8 arg_count, char* arg_values[]) { printf_s(" %s\n", arg_values[arg_index]); } - IZ_ProcedureResult init_result = IZ_AppInitialize(app); if (init_result) { return init_result; } + if (IZ_WSClientConnect(&app->client, (IZ_WSClientConnectParams) { + .userdata = app, + .path = "/", + .protocol = "http", // TODO handle ws protocol correctly + .address = "localhost", + .port = 6969, + })) { + return IZ_APP_RUN_NETWORKING_ERROR; + } + while (true) { app->ticks = SDL_GetTicks64(); // TODO do audio processing - // TODO do networking? + // TODO do networking + if (app->client.connection) { + // FIXME stuck in infinite loop + IZ_WSClientHandle(&app->client); + } if (IZ_AppHandleEvents(app)) { + // TODO refactor input handlers to have same function signature. break; } @@ -110,5 +124,5 @@ IZ_ProcedureResult IZ_AppRun(IZ_App* app, u8 arg_count, char* arg_values[]) { } IZ_AppTeardown(app); - return 0; + return IZ_APP_RUN_RESULT_OK; } diff --git a/src/packages/game/IZ_app.h b/src/packages/game/IZ_app.h index e52f566..7bd53bd 100644 --- a/src/packages/game/IZ_app.h +++ b/src/packages/game/IZ_app.h @@ -12,6 +12,15 @@ #include "memory/IZ_pool.h" #include "network/IZ_wsclient.h" +typedef enum { + IZ_APP_RUN_RESULT_OK, + IZ_APP_RUN_SDL_INIT_ERROR, + IZ_APP_RUN_VIDEO_INIT_ERROR, + IZ_APP_RUN_INPUT_INIT_ERROR, + IZ_APP_RUN_POOL_INIT_ERROR, + IZ_APP_RUN_NETWORKING_ERROR, +} IZ_AppRunResult; + typedef struct { IZ_InputState input_state; IZ_VideoState video_state; diff --git a/src/packages/game/network/IZ_wsclient.c b/src/packages/game/network/IZ_wsclient.c index d72ff0f..f87ae08 100644 --- a/src/packages/game/network/IZ_wsclient.c +++ b/src/packages/game/network/IZ_wsclient.c @@ -9,11 +9,17 @@ IZ_ProcedureResult IZ_WSClientCallback( ) { switch (reason) { case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: + printf("Client Connection Error %llu\n", len); return 1; case LWS_CALLBACK_CLIENT_CLOSED: + printf("Client Closed %llu\n", len); return 0; case LWS_CALLBACK_CLIENT_RECEIVE: + printf("Client Receive %llu\n", len); + break; case LWS_CALLBACK_CLIENT_ESTABLISHED: + printf("Client Established %llu\n", len); + break; default: break; } @@ -21,7 +27,7 @@ IZ_ProcedureResult IZ_WSClientCallback( return lws_callback_http_dummy(wsi, reason, user, in, len); } -IZ_ProcedureResult IZ_WSClientConnect(IZ_WSClient* client, IZ_WSClientConnectParams params) { +IZ_ProcedureResult IZ_WSClientCreateConnection(IZ_WSClient* client, IZ_WSClientConnectParams params) { static struct lws_client_connect_info info; memset(&info, 0, sizeof info); @@ -48,13 +54,10 @@ IZ_ProcedureResult IZ_WSClientConnect(IZ_WSClient* client, IZ_WSClientConnectPar return 0; } -IZ_ProcedureResult IZ_WSClientInitialize(IZ_WSClient* client, void* app) { - // TODO delegate logs to SDL - lws_set_log_level(0, NULL); - +IZ_ProcedureResult IZ_WSClientCreateContext(IZ_WSClient* client, void* userdata) { static struct lws_protocols protocols[] = { { - .name = "lws-minimal-client", + .name = "http", .callback = IZ_WSClientCallback, .per_session_data_size = 0, .rx_buffer_size = 0, @@ -64,14 +67,14 @@ IZ_ProcedureResult IZ_WSClientInitialize(IZ_WSClient* client, void* app) { }, LWS_PROTOCOL_LIST_TERM, }; - protocols[0].user = app; + protocols[0].user = userdata; static struct lws_context_creation_info info; memset(&info, 0, sizeof info); info.port = CONTEXT_PORT_NO_LISTEN, info.protocols = protocols, info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT, - client->context = lws_create_context(&info); + client->context = lws_create_context(&info); if (!client->context) { return 1; } @@ -79,6 +82,25 @@ IZ_ProcedureResult IZ_WSClientInitialize(IZ_WSClient* client, void* app) { return 0; } +void IZ_WSClientInitialize(IZ_WSClient* client) { + // TODO delegate logs to SDL + //lws_set_log_level(0, NULL); + client->context = NULL; + client->connection = NULL; +} + +IZ_ProcedureResult IZ_WSClientConnect(IZ_WSClient* client, IZ_WSClientConnectParams params) { + if (IZ_WSClientCreateContext(client, params.userdata)) { + return 1; + } + + if (IZ_WSClientCreateConnection(client, params)) { + return 2; + } + + return 0; +} + IZ_ProcedureResult IZ_WSClientHandle(IZ_WSClient* client) { i32 response = lws_service(client->context, 0); if (response < 0) { @@ -88,6 +110,6 @@ IZ_ProcedureResult IZ_WSClientHandle(IZ_WSClient* client) { return 0; } -void IZ_WSClientTeardown(IZ_WSClient* client) { +void IZ_WSClientDisconnect(IZ_WSClient* client) { lws_context_destroy(client->context); } diff --git a/src/packages/game/network/IZ_wsclient.h b/src/packages/game/network/IZ_wsclient.h index 28be87e..0b35f9b 100644 --- a/src/packages/game/network/IZ_wsclient.h +++ b/src/packages/game/network/IZ_wsclient.h @@ -7,7 +7,6 @@ typedef struct { struct lws_context* context; struct lws* connection; - i32 n; } IZ_WSClient; typedef struct { @@ -18,12 +17,12 @@ typedef struct { void* userdata; } IZ_WSClientConnectParams; -IZ_ProcedureResult IZ_WSClientInitialize(IZ_WSClient*, void*); +void IZ_WSClientInitialize(IZ_WSClient*); IZ_ProcedureResult IZ_WSClientConnect(IZ_WSClient*, IZ_WSClientConnectParams); IZ_ProcedureResult IZ_WSClientHandle(IZ_WSClient*); -void IZ_WSClientTeardown(IZ_WSClient*); +void IZ_WSClientDisconnect(IZ_WSClient*); #endif diff --git a/src/packages/server/main.c b/src/packages/server/main.c index 23f18a9..aac4eb2 100644 --- a/src/packages/server/main.c +++ b/src/packages/server/main.c @@ -63,7 +63,7 @@ IZ_ProcedureResult main(i32 arg_count, char* arg_values[]) { lwsl_user("LWS minimal ws server | visit http://localhost:7681 (-s = use TLS / https)\n"); memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ - info.port = 7681; + info.port = 6969; info.mounts = &mount; info.protocols = protocols; info.vhost_name = "localhost";