Browse Source

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.
feature/data-structs
TheoryOfNekomata 2 years ago
parent
commit
8c42f84d46
5 changed files with 19 additions and 12 deletions
  1. +7
    -2
      src/packages/game/IZ_app.c
  2. +2
    -1
      src/packages/game/network/IZ_websocket.c
  3. +7
    -7
      src/packages/server/IZ_app.c
  4. +1
    -1
      src/packages/server/IZ_app.h
  5. +2
    -1
      src/packages/server/main.c

+ 7
- 2
src/packages/game/IZ_app.c View File

@@ -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);


+ 2
- 1
src/packages/game/network/IZ_websocket.c View File

@@ -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) {


+ 7
- 7
src/packages/server/IZ_app.c View File

@@ -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;
}

+ 1
- 1
src/packages/server/IZ_app.h View File

@@ -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**);



+ 2
- 1
src/packages/server/main.c View File

@@ -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);
}

Loading…
Cancel
Save