Ensure the same reference of the server is terminated in the server, while the client should check the context when connecting/disconnecting.feature/data-structs
@@ -23,7 +23,7 @@ IZ_ProcedureResult IZ_AppConnect(void* app_raw) { | |||||
i32 result = 0; | i32 result = 0; | ||||
while (true) { | while (true) { | ||||
if (IZ_WSClientHandle(&app->client)) { | if (IZ_WSClientHandle(&app->client)) { | ||||
result = 1; | |||||
result = -1; | |||||
break; | break; | ||||
} | } | ||||
@@ -37,6 +37,10 @@ IZ_ProcedureResult IZ_AppConnect(void* app_raw) { | |||||
} | } | ||||
void IZ_AppEstablishConnection(IZ_App* app) { | void IZ_AppEstablishConnection(IZ_App* app) { | ||||
if (app->client.ws.context) { | |||||
return; | |||||
} | |||||
app->client_thread = SDL_CreateThread(IZ_AppConnect, "networking", app); | app->client_thread = SDL_CreateThread(IZ_AppConnect, "networking", app); | ||||
SDL_DetachThread(app->client_thread); | SDL_DetachThread(app->client_thread); | ||||
} | } | ||||
@@ -45,6 +49,7 @@ void IZ_AppCloseConnection(IZ_App* app) { | |||||
if (!app->client.ws.context) { | if (!app->client.ws.context) { | ||||
return; | return; | ||||
} | } | ||||
app->client.ws.interrupted = true; | |||||
IZ_WSClientCancelService(&app->client); | IZ_WSClientCancelService(&app->client); | ||||
} | } | ||||
@@ -101,7 +106,7 @@ IZ_ProcedureResult IZ_AppHandleSDLEvents(IZ_App* app) { | |||||
} | } | ||||
if (e.type == SDL_KEYDOWN) { | 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); | IZ_AppEstablishConnection(app); | ||||
} else if (e.key.keysym.sym == SDLK_PAGEDOWN) { | } else if (e.key.keysym.sym == SDLK_PAGEDOWN) { | ||||
IZ_AppCloseConnection(app); | IZ_AppCloseConnection(app); | ||||
@@ -7,7 +7,8 @@ void IZ_WebsocketInitialize(IZ_Websocket* ws) { | |||||
IZ_ProcedureResult IZ_WebsocketHandle(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 | // 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) { | void IZ_WebsocketCancelService(IZ_Websocket* ws) { | ||||
@@ -1,7 +1,8 @@ | |||||
#include "IZ_app.h" | #include "IZ_app.h" | ||||
void IZ_AppHandleSignal(i32 _signal) { | 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 | // 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) { | IZ_ProcedureResult IZ_AppInitialize(IZ_App *app, u8 argc, const char **argv) { | ||||
signal(SIGINT, IZ_AppHandleSignal); | signal(SIGINT, IZ_AppHandleSignal); | ||||
signal(9, IZ_AppHandleSignal); | |||||
signal(SIGTERM, IZ_AppHandleSignal); | |||||
IZ_AppLoadConfig(app, argc, argv); | IZ_AppLoadConfig(app, argc, argv); | ||||
IZ_LogInterceptWSMessages(app->config.log_level); | IZ_LogInterceptWSMessages(app->config.log_level); | ||||
@@ -33,6 +36,7 @@ IZ_ProcedureResult IZ_AppInitialize(IZ_App *app, u8 argc, const char **argv) { | |||||
return -1; | return -1; | ||||
} | } | ||||
global_app = app; | |||||
return 0; | return 0; | ||||
} | } | ||||
@@ -42,22 +46,18 @@ IZ_ProcedureResult IZ_AppRun(IZ_App *app, u8 argc, const char **argv) { | |||||
} | } | ||||
i32 result = 0; | i32 result = 0; | ||||
while (!app->server.ws.interrupted) { | |||||
printf("A\n"); | |||||
while (true) { | |||||
if (IZ_WSServerHandle(&app->server)) { | if (IZ_WSServerHandle(&app->server)) { | ||||
printf("B\n"); | |||||
result = -1; | result = -1; | ||||
break; | break; | ||||
} | } | ||||
if (app->server.ws.interrupted) { | if (app->server.ws.interrupted) { | ||||
printf("C\n"); | |||||
break; | break; | ||||
} | } | ||||
printf("D\n"); | |||||
} | } | ||||
IZ_WSServerTeardown(&app->server); | IZ_WSServerTeardown(&app->server); | ||||
printf("%u\n", result); | |||||
lwsl_user("Server closed. Bye!\n"); | |||||
return result; | return result; | ||||
} | } |
@@ -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**); | IZ_ProcedureResult IZ_AppRun(IZ_App*, u8, const char**); | ||||
@@ -1,5 +1,6 @@ | |||||
#include "IZ_app.h" | #include "IZ_app.h" | ||||
IZ_ProcedureResult main(i32 argc, const char *argv[]) { | IZ_ProcedureResult main(i32 argc, const char *argv[]) { | ||||
return IZ_AppRun(&global_app, argc, argv); | |||||
IZ_App app; | |||||
return IZ_AppRun(&app, argc, argv); | |||||
} | } |