Browse Source

Rewrite websocket for Lua (Step 13 of ?)

bel 11 years ago
parent
commit
2a2efc26f7
3 changed files with 17 additions and 7 deletions
  1. 1 1
      VS2012/civetweb_lua/civetweb_lua.vcxproj
  2. 13 5
      src/civetweb.c
  3. 3 1
      src/mod_lua.inl

+ 1 - 1
VS2012/civetweb_lua/civetweb_lua.vcxproj

@@ -88,7 +88,7 @@
       </PrecompiledHeader>
       <WarningLevel>Level3</WarningLevel>
       <Optimization>Disabled</Optimization>
-      <PreprocessorDefinitions>LUA_COMPAT_ALL;USE_LUA;USE_LUA_SQLITE3;USE_LUA_FILE_SYSTEM;USE_WEBSOCKET;WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>LUA_COMPAT_ALL;USE_LUA;USE_LUA_SQLITE3;USE_LUA_FILE_SYSTEM;USE_WEBSOCKET;WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <AdditionalIncludeDirectories>$(ProjectDir)..\..\include;$(ProjectDir)..\..\src\third_party\lua-5.2.2\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
     </ClCompile>
     <Link>

+ 13 - 5
src/civetweb.c

@@ -181,6 +181,10 @@ typedef long off_t;
 #define sleep(x) Sleep((x) * 1000)
 #define rmdir(x) _rmdir(x)
 
+#if defined(USE_LUA) && defined(USE_WEBSOCKET)
+#define USE_TIMERS
+#endif
+
 #if !defined(va_copy)
 #define va_copy(x, y) x = y
 #endif /* !va_copy MINGW #defines va_copy */
@@ -789,8 +793,12 @@ struct mg_context {
 #if defined(USE_LUA) && defined(USE_WEBSOCKET)
     /* linked list of shared lua websockets */
     struct mg_shared_lua_websocket_list *shared_lua_websockets;
+#endif
+
+#ifdef USE_TIMERS
     pthread_t timerthreadid;        /* Time thread ID */
     pthread_mutex_t timer_mutex;    /* Protects timer lists */
+    struct timer_list *timers;      /* List of timers */
 #endif
 };
 
@@ -6739,7 +6747,7 @@ static void *master_thread(void *thread_func_param)
 }
 #endif /* _WIN32 */
 
-#if defined(USE_LUA) && defined(USE_WEBSOCKET)
+#if defined(USE_TIMERS)
 void timer_thread_run(void *thread_func_param)
 {
     struct mg_context *ctx = (struct mg_context *) thread_func_param;
@@ -6764,7 +6772,7 @@ static void *timer_thread(void *thread_func_param)
     return NULL;
 }
 #endif /* _WIN32 */
-#endif /* USE_LUA && USE_WEBSOCKET */
+#endif /* USE_TIMERS */
 
 static void free_context(struct mg_context *ctx)
 {
@@ -6783,7 +6791,7 @@ static void free_context(struct mg_context *ctx)
     /* Destroy other context global data structures mutex */
     (void) pthread_mutex_destroy(&ctx->nonce_mutex);
 
-#if defined(USE_LUA) && defined(USE_WEBSOCKET)
+#if defined(USE_TIMERS)
     (void) pthread_mutex_destroy(&ctx->timer_mutex);
 #endif
 
@@ -6979,7 +6987,7 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
 
     (void) pthread_mutex_init(&ctx->nonce_mutex, NULL);
 
-#if defined(USE_LUA) && defined(USE_WEBSOCKET)
+#if defined(USE_TIMERS)
     (void) pthread_mutex_init(&ctx->timer_mutex, NULL);
 #endif
 
@@ -7001,7 +7009,7 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
         }
     }
 
-#if defined(USE_LUA) && defined(USE_WEBSOCKET)
+#if defined(USE_TIMERS)
     /* Start timer thread */
     mg_start_thread_with_id(timer_thread, ctx, &ctx->timerthreadid);
 #endif

+ 3 - 1
src/mod_lua.inl

@@ -734,7 +734,7 @@ static int lwebsock_write(lua_State *L)
 
 static int lwebsocket_set_timer(lua_State *L, int is_periodic)
 {
-#ifdef USE_WEBSOCKET
+#ifdef USE_TIMERS
     int num_args = lua_gettop(L);
     struct lua_websock_data *ws;
     lua_Number timediff;
@@ -909,8 +909,10 @@ static void prepare_lua_environment(struct mg_context * ctx, struct mg_connectio
 
     if (lua_env_type==LUA_ENV_TYPE_LUA_WEBSOCKET) {
         reg_function(L, "write", lwebsock_write);
+#ifdef USE_TIMERS
         reg_function(L, "set_timeout", lwebsocket_set_timeout);
         reg_function(L, "set_interval", lwebsocket_set_interval);
+#endif
         /* reg_conn_function(L, "send_file", lsp_send_file, conn); */
     }