浏览代码

Rewrite websocket for Lua (Step 16 of ?)

bel 11 年之前
父节点
当前提交
661a1bac39
共有 3 个文件被更改,包括 57 次插入57 次删除
  1. 9 20
      src/mod_lua.inl
  2. 37 35
      src/timer.inl
  3. 11 2
      test/websocket.lua

+ 9 - 20
src/mod_lua.inl

@@ -743,6 +743,11 @@ static void lua_action(struct laction_arg *arg)
     (void)pthread_mutex_lock(arg->pmutex);
     luaL_dostring(arg->state, arg->txt);
     (void)pthread_mutex_unlock(arg->pmutex);
+}
+
+static void lua_action_free(struct laction_arg *arg)
+{
+    lua_action(arg);
     mg_free(arg);
 }
 
@@ -782,7 +787,7 @@ static int lwebsocket_set_timer(lua_State *L, int is_periodic)
         arg->pmutex = &(ws->ws_mutex);
         memcpy(arg->txt, txt, txt_len);
         arg->txt[txt_len] = 0;
-        ok = (0==timer_add(ctx, timediff, is_periodic, 1, lua_action, (void*)arg));
+        ok = (0==timer_add(ctx, timediff, is_periodic, 1, is_periodic ? lua_action : lua_action_free, (void*)arg));
     } else if (type1==LUA_TFUNCTION && type2==LUA_TNUMBER)  {
         /* TODO: not implemented yet */
         return luaL_error(L, "invalid arguments for set_timer/interval() call");
@@ -1297,24 +1302,8 @@ static void lua_websocket_close(struct mg_connection * conn, void * ws_arg)
         }
     }
 /*
-    if (ws->references==0) {
-        (void)pthread_mutex_lock(&conn->ctx->nonce_mutex);
-        (void)pthread_mutex_unlock(&ws->ws_mutex);
-
-        while (*shared_websock_list) {
-            if (0==strcmp(ws->script,(*shared_websock_list)->ws.script)) {
-                break;
-            }
-            shared_websock_list = &((*shared_websock_list)->next);
-        }
-        assert(*shared_websock_list != NULL);
-        (void)pthread_mutex_unlock(&conn->ctx->nonce_mutex);
-        lua_close(ws->state);
-        mg_free(ws->script);
-        *shared_websock_list = (*shared_websock_list)->next;
-        mg_free(ws);
-    } else */ {
-        (void)pthread_mutex_unlock(&ws->ws_mutex);
-    }
+    TODO
+*/
+    (void)pthread_mutex_unlock(&ws->ws_mutex);
 }
 #endif

+ 37 - 35
src/timer.inl

@@ -19,6 +19,40 @@ struct timers {
     unsigned timer_count;             /* Current size of timer list */
 };
 
+static int timer_add(struct mg_context * ctx, double next_time, double period, int is_relative, taction action, void * arg)
+{
+    unsigned u, v;
+    int error = 0;
+    struct timespec now;
+
+    if (is_relative) {
+        clock_gettime(CLOCK_MONOTONIC, &now);
+        next_time += now.tv_sec;
+        next_time += now.tv_nsec * 1.0E-9;
+    }
+
+    pthread_mutex_lock(&ctx->timers->mutex);
+    if (ctx->timers->timer_count == MAX_TIMERS) {
+        error = 1;
+    } else {
+        for (u=0; u<ctx->timers->timer_count; u++) {
+            if (ctx->timers->timers[u].time < next_time) {
+                for (v=ctx->timers->timer_count; v>u; v--) {
+                    ctx->timers->timers[v] = ctx->timers->timers[v-1];
+                }
+                break;
+            }
+        }
+        ctx->timers->timers[u].time = next_time;
+        ctx->timers->timers[u].period = period;
+        ctx->timers->timers[u].action = action;
+        ctx->timers->timers[u].arg = arg;
+        ctx->timers->timer_count++;
+    }
+    pthread_mutex_unlock(&ctx->timers->mutex);
+    return error;
+}
+
 static void timer_thread_run(void *thread_func_param)
 {
     struct mg_context *ctx = (struct mg_context *) thread_func_param;
@@ -43,6 +77,9 @@ static void timer_thread_run(void *thread_func_param)
                 ctx->timers->timer_count--;
                 pthread_mutex_unlock(&ctx->timers->mutex);
                 t.action(t.arg);
+                if (t.period>0) {
+                    timer_add(ctx, t.time+t.period, t.period, 0, t.action, t.arg);
+                }
                 continue;
             } else {
                 pthread_mutex_unlock(&ctx->timers->mutex);
@@ -55,41 +92,6 @@ static void timer_thread_run(void *thread_func_param)
     }
 }
 
-static int timer_add(struct mg_context * ctx, double time, int is_periodic, int is_relative, taction action, void * arg)
-{
-    double t = time;
-    unsigned u, v;
-    int error = 0;
-    struct timespec now;
-
-    if (is_relative) {
-        clock_gettime(CLOCK_MONOTONIC, &now);
-        t += now.tv_sec;
-        t += now.tv_nsec * 1.0E-9;
-    }
-
-    pthread_mutex_lock(&ctx->timers->mutex);
-    if (ctx->timers->timer_count == MAX_TIMERS) {
-        error = 1;
-    } else {
-        for (u=0; u<ctx->timers->timer_count; u++) {
-            if (ctx->timers->timers[u].time < time) {
-                for (v=ctx->timers->timer_count; v>u; v--) {
-                    ctx->timers->timers[v] = ctx->timers->timers[v-1];
-                }
-                break;
-            }
-        }
-        ctx->timers->timers[u].time = t;
-        ctx->timers->timers[u].period = is_periodic ? time : 0.0;
-        ctx->timers->timers[u].action = action;
-        ctx->timers->timers[u].arg = arg;
-        ctx->timers->timer_count++;
-    }
-    pthread_mutex_unlock(&ctx->timers->mutex);
-    return error;
-}
-
 #ifdef _WIN32
 static unsigned __stdcall timer_thread(void *thread_func_param)
 {

+ 11 - 2
test/websocket.lua

@@ -1,3 +1,6 @@
+timerID = "timeout"
+--timerID = "interval"
+
 function trace(text)
     local f = io.open("R:\\websocket.trace", "a")
     f:write(os.date() .. " - " .. text .. "\n")
@@ -64,7 +67,11 @@ function ready(tab)
   mg.write(tab.client, 1, "-->h 180");
   mg.write(tab.client, "-->m 180");
   senddata()
-  mg.set_timeout("timer()", 1)
+  if timerID == "timeout" then
+    mg.set_timeout("timer()", 1)
+  elseif timerID == "interval" then
+    mg.set_interval("timer()", 1)
+  end
   return true
 end
 
@@ -102,6 +109,8 @@ end
 function timer()
     trace("timer")
     senddata()
-    mg.set_timeout("timer()", 1)
+    if timerID == "timeout" then
+        mg.set_timeout("timer()", 1)
+    end
 end