Pārlūkot izejas kodu

Move lua background script from main.c to civetweb.c

bel 8 gadi atpakaļ
vecāks
revīzija
24e22e286f
3 mainītis faili ar 43 papildinājumiem un 55 dzēšanām
  1. 38 2
      src/civetweb.c
  2. 4 52
      src/main.c
  3. 1 1
      test/timertest.c

+ 38 - 2
src/civetweb.c

@@ -52,7 +52,7 @@
 #endif
 #endif
 #endif
 #endif
 
 
-#if defined(USE_LUA) && defined(USE_WEBSOCKET)
+#if defined(USE_LUA)
 #define USE_TIMERS
 #define USE_TIMERS
 #endif
 #endif
 
 
@@ -1604,6 +1604,9 @@ enum {
 #if defined(_WIN32)
 #if defined(_WIN32)
 	CASE_SENSITIVE_FILES,
 	CASE_SENSITIVE_FILES,
 #endif
 #endif
+#if defined(USE_LUA)
+	LUA_BACKGROUND_SCRIPT,
+#endif
 
 
 	NUM_OPTIONS
 	NUM_OPTIONS
 };
 };
@@ -1686,6 +1689,9 @@ static struct mg_option config_options[] = {
 #if defined(_WIN32)
 #if defined(_WIN32)
     {"case_sensitive", CONFIG_TYPE_BOOLEAN, "no"},
     {"case_sensitive", CONFIG_TYPE_BOOLEAN, "no"},
 #endif
 #endif
+#if defined(USE_LUA)
+    {"lua_background_script", CONFIG_TYPE_FILE, NULL},
+#endif
 
 
     {NULL, CONFIG_TYPE_UNKNOWN, NULL}};
     {NULL, CONFIG_TYPE_UNKNOWN, NULL}};
 
 
@@ -1774,9 +1780,13 @@ struct mg_context {
 	struct mg_shared_lua_websocket_list *shared_lua_websockets;
 	struct mg_shared_lua_websocket_list *shared_lua_websockets;
 #endif
 #endif
 
 
-#ifdef USE_TIMERS
+#if defined(USE_TIMERS)
 	struct ttimers *timers;
 	struct ttimers *timers;
 #endif
 #endif
+
+#if defined(USE_LUA)
+	void *lua_background_state;
+#endif
 };
 };
 
 
 
 
@@ -13890,6 +13900,14 @@ master_thread_run(void *thread_func_param)
 		}
 		}
 	}
 	}
 
 
+#if defined(USE_LUA)
+	/* Free Lua state of lua background task */
+	if (ctx->lua_background_state) {
+		lua_close((lua_State *)ctx->lua_background_state);
+		ctx->lua_background_state = 0;
+	}
+#endif
+
 #if !defined(NO_SSL)
 #if !defined(NO_SSL)
 	if (ctx->ssl_ctx != NULL) {
 	if (ctx->ssl_ctx != NULL) {
 		uninitialize_ssl(ctx);
 		uninitialize_ssl(ctx);
@@ -14251,6 +14269,24 @@ mg_start(const struct mg_callbacks *callbacks,
 
 
 	get_system_name(&ctx->systemName);
 	get_system_name(&ctx->systemName);
 
 
+#if defined(USE_LUA)
+	/* If a Lua background script has been configured, start it. */
+	if (ctx->config[LUA_BACKGROUND_SCRIPT] != NULL) {
+		char ebuf[256];
+		void *state = (void *)mg_prepare_lua_context_script(
+		    ctx->config[LUA_BACKGROUND_SCRIPT], ctx, ebuf, sizeof(ebuf));
+		if (!state) {
+			mg_cry(fc(ctx), "lua_background_script error: %s", ebuf);
+			free_context(ctx);
+			pthread_setspecific(sTlsKey, NULL);
+			return NULL;
+		}
+		ctx->lua_background_state = state;
+	} else {
+		ctx->lua_background_state = 0;
+	}
+#endif
+
 	/* NOTE(lsm): order is important here. SSL certificates must
 	/* NOTE(lsm): order is important here. SSL certificates must
 	 * be initialized before listening ports. UID must be set last. */
 	 * be initialized before listening ports. UID must be set last. */
 	if (!set_gpass_option(ctx) ||
 	if (!set_gpass_option(ctx) ||

+ 4 - 52
src/main.c

@@ -149,9 +149,6 @@ static int g_exit_flag = 0;         /* Main loop should exit */
 static char g_server_base_name[40]; /* Set by init_server_name() */
 static char g_server_base_name[40]; /* Set by init_server_name() */
 static const char *g_server_name;   /* Set by init_server_name() */
 static const char *g_server_name;   /* Set by init_server_name() */
 static const char *g_icon_name;     /* Set by init_server_name() */
 static const char *g_icon_name;     /* Set by init_server_name() */
-#ifdef USE_LUA
-static const char *g_lua_script; /* Set by init_server_name() */
-#endif
 static char g_config_file_name[PATH_MAX] =
 static char g_config_file_name[PATH_MAX] =
     "";                          /* Set by process_command_line_arguments() */
     "";                          /* Set by process_command_line_arguments() */
 static struct mg_context *g_ctx; /* Set by start_civetweb() */
 static struct mg_context *g_ctx; /* Set by start_civetweb() */
@@ -171,22 +168,11 @@ static struct tuser_data
 #define CONFIG_FILE2 "/usr/local/etc/civetweb.conf"
 #define CONFIG_FILE2 "/usr/local/etc/civetweb.conf"
 #endif
 #endif
 
 
-enum {
-	OPTION_TITLE,
-	OPTION_ICON,
-#ifdef USE_LUA
-	OPTION_LUA_SCRIPT,
-#endif
-	NUM_MAIN_OPTIONS
-};
+enum { OPTION_TITLE, OPTION_ICON, NUM_MAIN_OPTIONS };
 
 
 static struct mg_option main_config_options[] = {
 static struct mg_option main_config_options[] = {
     {"title", CONFIG_TYPE_STRING, NULL},
     {"title", CONFIG_TYPE_STRING, NULL},
     {"icon", CONFIG_TYPE_STRING, NULL},
     {"icon", CONFIG_TYPE_STRING, NULL},
-#ifdef USE_LUA
-    /* TODO: Move from here to civetweb.c and stop with server */
-    {"lua_script", CONFIG_TYPE_STRING, NULL},
-#endif
     {NULL, CONFIG_TYPE_UNKNOWN, NULL}};
     {NULL, CONFIG_TYPE_UNKNOWN, NULL}};
 
 
 
 
@@ -658,16 +644,6 @@ init_server_name(int argc, const char *argv[])
 			g_icon_name = (const char *)(argv[i + 1]);
 			g_icon_name = (const char *)(argv[i + 1]);
 		}
 		}
 	}
 	}
-#ifdef USE_LUA
-	g_lua_script = NULL;
-	for (i = 0; i < argc - 1; i++) {
-		if ((argv[i][0] == '-')
-		    && (0 == strcmp(argv[i] + 1,
-		                    main_config_options[OPTION_LUA_SCRIPT].name))) {
-			g_lua_script = (const char *)(argv[i + 1]);
-		}
-	}
-#endif
 }
 }
 
 
 
 
@@ -780,19 +756,8 @@ set_absolute_path(char *options[],
 
 
 #ifdef USE_LUA
 #ifdef USE_LUA
 
 
-/* TODO: Move to civetweb.c (done), use config lua_background_script (open:
- * move from here), start in mg_start, allow access to server state, set
- * mg.sleep or use timer */
-
 #include "civetweb_private_lua.h"
 #include "civetweb_private_lua.h"
 
 
-static void *
-run_lua_thread(void *file_name)
-{
-	run_lua((const char *)file_name);
-	return NULL;
-}
-
 #endif
 #endif
 
 
 
 
@@ -805,10 +770,6 @@ run_duktape(const char *file_name)
 {
 {
 	duk_context *ctx = NULL;
 	duk_context *ctx = NULL;
 
 
-#ifdef WIN32
-	(void)MakeConsole();
-#endif
-
 	ctx = duk_create_heap_default();
 	ctx = duk_create_heap_default();
 	if (!ctx) {
 	if (!ctx) {
 		fprintf(stderr, "Failed to create a Duktape heap.\n");
 		fprintf(stderr, "Failed to create a Duktape heap.\n");
@@ -903,6 +864,9 @@ start_civetweb(int argc, char *argv[])
 		if (argc != 3) {
 		if (argc != 3) {
 			show_usage_and_exit(argv[0]);
 			show_usage_and_exit(argv[0]);
 		}
 		}
+#ifdef WIN32
+		(void)MakeConsole();
+#endif
 		exit(run_duktape(argv[2]));
 		exit(run_duktape(argv[2]));
 #else
 #else
 		show_server_name();
 		show_server_name();
@@ -943,18 +907,6 @@ start_civetweb(int argc, char *argv[])
 	verify_existence(options, "ssl_ca_file", 0);
 	verify_existence(options, "ssl_ca_file", 0);
 #ifdef USE_LUA
 #ifdef USE_LUA
 	verify_existence(options, "lua_preload_file", 0);
 	verify_existence(options, "lua_preload_file", 0);
-
-	if (g_lua_script) {
-		struct stat st;
-		if ((stat(g_lua_script, &st) != 0) || (S_ISDIR(st.st_mode))) {
-			fprintf(stderr, "\nError: lua_script not found\n");
-			exit(EXIT_FAILURE);
-		}
-		if (0 != mg_start_thread(run_lua_thread, (void *)g_lua_script)) {
-			fprintf(stderr, "\nError: Cannot create thread for lua_script\n");
-			exit(EXIT_FAILURE);
-		}
-	}
 #endif
 #endif
 
 
 	/* Setup signal handler: quit on Ctrl-C */
 	/* Setup signal handler: quit on Ctrl-C */

+ 1 - 1
test/timertest.c

@@ -244,7 +244,7 @@ TIMER_PRIVATE(void)
 
 
 	test_timer_cyclic(0);
 	test_timer_cyclic(0);
 	test_timer_oneshot_by_timer_add(0);
 	test_timer_oneshot_by_timer_add(0);
-    test_timer_oneshot_by_callback_retval(0);
+	test_timer_oneshot_by_callback_retval(0);
 
 
 #if defined(_WIN32)
 #if defined(_WIN32)
 	WSACleanup();
 	WSACleanup();