From 8c42f84d468adec655077431bc6d2e500a7aea9d Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Tue, 14 Jun 2022 08:59:01 +0800 Subject: [PATCH] Fix termination handling Ensure the same reference of the server is terminated in the server, while the client should check the context when connecting/disconnecting. --- src/packages/game/IZ_app.c | 9 +++++++-- src/packages/game/network/IZ_websocket.c | 3 ++- src/packages/server/IZ_app.c | 14 +++++++------- src/packages/server/IZ_app.h | 2 +- src/packages/server/main.c | 3 ++- 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/packages/game/IZ_app.c b/src/packages/game/IZ_app.c index 4508add..c5efa11 100644 --- a/src/packages/game/IZ_app.c +++ b/src/packages/game/IZ_app.c @@ -23,7 +23,7 @@ IZ_ProcedureResult IZ_AppConnect(void* app_raw) { i32 result = 0; while (true) { if (IZ_WSClientHandle(&app->client)) { - result = 1; + result = -1; break; } @@ -37,6 +37,10 @@ IZ_ProcedureResult IZ_AppConnect(void* app_raw) { } void IZ_AppEstablishConnection(IZ_App* app) { + if (app->client.ws.context) { + return; + } + app->client_thread = SDL_CreateThread(IZ_AppConnect, "networking", app); SDL_DetachThread(app->client_thread); } @@ -45,6 +49,7 @@ void IZ_AppCloseConnection(IZ_App* app) { if (!app->client.ws.context) { return; } + app->client.ws.interrupted = true; IZ_WSClientCancelService(&app->client); } @@ -101,7 +106,7 @@ IZ_ProcedureResult IZ_AppHandleSDLEvents(IZ_App* app) { } if (e.type == SDL_KEYDOWN) { - if (e.key.keysym.sym == SDLK_PAGEUP && !app->client.ws.context) { + if (e.key.keysym.sym == SDLK_PAGEUP) { IZ_AppEstablishConnection(app); } else if (e.key.keysym.sym == SDLK_PAGEDOWN) { IZ_AppCloseConnection(app); diff --git a/src/packages/game/network/IZ_websocket.c b/src/packages/game/network/IZ_websocket.c index 3bbd9a4..609328b 100644 --- a/src/packages/game/network/IZ_websocket.c +++ b/src/packages/game/network/IZ_websocket.c @@ -7,7 +7,8 @@ void IZ_WebsocketInitialize(IZ_Websocket* ws) { IZ_ProcedureResult IZ_WebsocketHandle(IZ_Websocket* ws) { // FIXME: https://libwebsockets.org/git/libwebsockets/tree/minimal-examples-lowlevel/http-server/minimal-http-server-eventlib-foreign - return lws_service(ws->context, 0); +// return lws_service(ws->context, 0); + return lws_service_tsi(ws->context, -1, 0); } void IZ_WebsocketCancelService(IZ_Websocket* ws) { diff --git a/src/packages/server/IZ_app.c b/src/packages/server/IZ_app.c index fab0c11..20a0d2e 100644 --- a/src/packages/server/IZ_app.c +++ b/src/packages/server/IZ_app.c @@ -1,7 +1,8 @@ #include "IZ_app.h" void IZ_AppHandleSignal(i32 _signal) { - IZ_WSServerCancelService(&global_app.server); + global_app->server.ws.interrupted = true; + IZ_WSServerCancelService(&global_app->server); } // TODO move to each subsystem @@ -21,6 +22,8 @@ void IZ_AppLoadConfig(IZ_App *app, u8 argc, const char **argv) { IZ_ProcedureResult IZ_AppInitialize(IZ_App *app, u8 argc, const char **argv) { signal(SIGINT, IZ_AppHandleSignal); + signal(9, IZ_AppHandleSignal); + signal(SIGTERM, IZ_AppHandleSignal); IZ_AppLoadConfig(app, argc, argv); IZ_LogInterceptWSMessages(app->config.log_level); @@ -33,6 +36,7 @@ IZ_ProcedureResult IZ_AppInitialize(IZ_App *app, u8 argc, const char **argv) { return -1; } + global_app = app; return 0; } @@ -42,22 +46,18 @@ IZ_ProcedureResult IZ_AppRun(IZ_App *app, u8 argc, const char **argv) { } i32 result = 0; - while (!app->server.ws.interrupted) { - printf("A\n"); + while (true) { if (IZ_WSServerHandle(&app->server)) { - printf("B\n"); result = -1; break; } if (app->server.ws.interrupted) { - printf("C\n"); break; } - printf("D\n"); } IZ_WSServerTeardown(&app->server); - printf("%u\n", result); + lwsl_user("Server closed. Bye!\n"); return result; } diff --git a/src/packages/server/IZ_app.h b/src/packages/server/IZ_app.h index c7550c0..a1561bc 100644 --- a/src/packages/server/IZ_app.h +++ b/src/packages/server/IZ_app.h @@ -24,7 +24,7 @@ static const IZ_App IZ_APP_DEFAULT_STATE = { }, }; -static IZ_App global_app; +static IZ_App* global_app; IZ_ProcedureResult IZ_AppRun(IZ_App*, u8, const char**); diff --git a/src/packages/server/main.c b/src/packages/server/main.c index 3642ec0..243bde6 100644 --- a/src/packages/server/main.c +++ b/src/packages/server/main.c @@ -1,5 +1,6 @@ #include "IZ_app.h" IZ_ProcedureResult main(i32 argc, const char *argv[]) { - return IZ_AppRun(&global_app, argc, argv); + IZ_App app; + return IZ_AppRun(&app, argc, argv); }